SceneBuilder.Cameras.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using BabylonExport.Entities;
  5. using UnityEngine;
  6. using UnityEditor;
  7. namespace Unity3D2Babylon
  8. {
  9. partial class SceneBuilder
  10. {
  11. private void ConvertUnityCameraToBabylon(Camera camera, GameObject gameObject, float progress, ref UnityMetaData metaData, ref List<BabylonExport.Entities.BabylonParticleSystem> particleSystems, ref List<UnityFlareSystem> lensFlares, ref string componentTags)
  12. {
  13. ExporterWindow.ReportProgress(progress, "Exporting camera: " + camera.name);
  14. BabylonUniversalCamera babylonCamera = new BabylonUniversalCamera
  15. {
  16. name = camera.name,
  17. id = GetID(camera.gameObject),
  18. fov = camera.fieldOfView * (float)Math.PI / 180,
  19. minZ = camera.nearClipPlane,
  20. maxZ = camera.farClipPlane,
  21. parentId = GetParentID(camera.transform),
  22. position = camera.transform.localPosition.ToFloat()
  23. };
  24. metaData.type = "Camera";
  25. metaData.properties.Add("hdr", camera.hdr);
  26. metaData.properties.Add("clearFlags", camera.clearFlags.ToString());
  27. metaData.properties.Add("cullingMask", camera.cullingMask);
  28. metaData.properties.Add("stereoEnabled", camera.stereoEnabled);
  29. metaData.properties.Add("isOrthographic", camera.orthographic);
  30. metaData.properties.Add("useOcclusionCulling", camera.useOcclusionCulling);
  31. babylonCamera.tags = componentTags;
  32. var target = new Vector3(0, 0, 1);
  33. var transformedTarget = camera.transform.TransformDirection(target);
  34. babylonCamera.target = (camera.transform.position + transformedTarget).ToFloat();
  35. babylonCamera.isStereoscopicSideBySide = camera.stereoEnabled;
  36. if (camera.orthographic)
  37. {
  38. float vert = camera.orthographicSize;
  39. float horz = vert * exportationOptions.DefaultAspectRatio;
  40. babylonCamera.orthoTop = vert;
  41. babylonCamera.orthoBottom = -vert;
  42. babylonCamera.orthoLeft = -horz;
  43. babylonCamera.orthoRight = horz;
  44. babylonCamera.mode = 1;
  45. }
  46. else
  47. {
  48. babylonCamera.mode = 0;
  49. }
  50. babylonCamera.metadata = metaData;
  51. babylonScene.CamerasList.Add(babylonCamera);
  52. if (Camera.main == camera)
  53. {
  54. babylonScene.activeCameraID = babylonCamera.id;
  55. babylonScene.clearColor = camera.backgroundColor.ToFloat();
  56. }
  57. // Animations
  58. ExportAnimations(camera.transform, babylonCamera);
  59. // Collisions
  60. if (exportationOptions.ExportCollisions)
  61. {
  62. babylonCamera.checkCollisions = true;
  63. if (SceneController != null) {
  64. babylonCamera.applyGravity = (SceneController.sceneOptions.defaultGravity.y == 0 && SceneController.sceneOptions.defaultGravity.y == 0 && SceneController.sceneOptions.defaultGravity.z == 0) ? false : true;
  65. babylonCamera.ellipsoid = SceneController.sceneOptions.cameraEllipsoid.ToFloat();
  66. }
  67. }
  68. // Lens Flares
  69. ParseLensFlares(gameObject, babylonCamera.id, ref lensFlares);
  70. // Particles Systems
  71. ParseParticleSystems(gameObject, babylonCamera.id, ref particleSystems);
  72. }
  73. private void ConvertUnitySkyboxToBabylon(Camera camera, float progress)
  74. {
  75. // Skybox
  76. bool png = (exportationOptions.DefaultImageFormat == (int)BabylonImageFormat.PNG);
  77. string fext = (png == true) ? ".png" : ".jpg";
  78. if ((camera.clearFlags & CameraClearFlags.Skybox) == CameraClearFlags.Skybox)
  79. {
  80. if (RenderSettings.skybox != null)
  81. {
  82. BabylonTexture skytex = null;
  83. if (RenderSettings.skybox.shader.name == "Skybox/Cubemap")
  84. {
  85. var cubeMap = RenderSettings.skybox.GetTexture("_Tex") as Cubemap;
  86. if (cubeMap != null)
  87. {
  88. var srcTexturePath = AssetDatabase.GetAssetPath(cubeMap);
  89. if (srcTexturePath.EndsWith(".hdr", StringComparison.OrdinalIgnoreCase))
  90. {
  91. var hdr = new BabylonHDRCubeTexture();
  92. hdr.size = cubeMap.width;
  93. skytex = hdr;
  94. CopyTextureCube(String.Format("{0}Skybox.hdr", SceneName), cubeMap, skytex, true);
  95. }
  96. else
  97. {
  98. skytex = new BabylonTexture();
  99. CopyTextureCube(String.Format("{0}Skybox.hdr", SceneName), cubeMap, skytex, false);
  100. if (png) skytex.extensions = new string[] { "_px.png", "_py.png", "_pz.png", "_nx.png", "_ny.png", "_nz.png" };
  101. }
  102. }
  103. }
  104. else if (RenderSettings.skybox.shader.name == "Skybox/6 Sided")
  105. {
  106. var frontTexture = RenderSettings.skybox.GetTexture("_FrontTex") as Texture2D;
  107. var backTexture = RenderSettings.skybox.GetTexture("_BackTex") as Texture2D;
  108. var leftTexture = RenderSettings.skybox.GetTexture("_LeftTex") as Texture2D;
  109. var rightTexture = RenderSettings.skybox.GetTexture("_RightTex") as Texture2D;
  110. var upTexture = RenderSettings.skybox.GetTexture("_UpTex") as Texture2D;
  111. var downTexture = RenderSettings.skybox.GetTexture("_DownTex") as Texture2D;
  112. if (frontTexture != null && backTexture != null && leftTexture != null && rightTexture != null && upTexture != null && downTexture != null)
  113. {
  114. skytex = new BabylonTexture();
  115. skytex.name = String.Format("{0}Skybox", SceneName);
  116. var frontTextureName = String.Format("{0}_pz{1}", skytex.name, fext);
  117. var frontTexturePath = Path.Combine(babylonScene.OutputPath, frontTextureName);
  118. CopyTextureFace(frontTexturePath, frontTextureName, frontTexture);
  119. var backTextureName = String.Format("{0}_nz{1}", skytex.name, fext);
  120. var backTexturePath = Path.Combine(babylonScene.OutputPath, backTextureName);
  121. CopyTextureFace(backTexturePath, backTextureName, backTexture);
  122. var leftTextureName = String.Format("{0}_px{1}", skytex.name, fext);
  123. var leftTexturePath = Path.Combine(babylonScene.OutputPath, leftTextureName);
  124. CopyTextureFace(leftTexturePath, leftTextureName, leftTexture);
  125. var rightTextureName = String.Format("{0}_nx{1}", skytex.name, fext);
  126. var rightTexturePath = Path.Combine(babylonScene.OutputPath, rightTextureName);
  127. CopyTextureFace(rightTexturePath, rightTextureName, rightTexture);
  128. var upTextureName = String.Format("{0}_py{1}", skytex.name, fext);
  129. var upTexturePath = Path.Combine(babylonScene.OutputPath, upTextureName);
  130. CopyTextureFace(upTexturePath, upTextureName, upTexture);
  131. var downTextureName = String.Format("{0}_ny{1}", skytex.name, fext);
  132. var downTexturePath = Path.Combine(babylonScene.OutputPath, downTextureName);
  133. CopyTextureFace(downTexturePath, downTexturePath, downTexture);
  134. if (png) skytex.extensions = new string[] { "_px.png", "_py.png", "_pz.png", "_nx.png", "_ny.png", "_nz.png" };
  135. }
  136. }
  137. if (skytex != null)
  138. {
  139. skytex.isCube = true;
  140. skytex.coordinatesMode = 5;
  141. skytex.level = (SceneController != null) ? SceneController.skyboxOptions.lightIntensity : 1.0f;
  142. var skybox = new BabylonMesh();
  143. skybox.name = "sceneSkyboxMesh";
  144. skybox.id = Guid.NewGuid().ToString();
  145. skybox.indices = new[] { 0, 1, 2, 0, 2, 3, 4, 5, 6, 4, 6, 7, 8, 9, 10, 8, 10, 11, 12, 13, 14, 12, 14, 15, 16, 17, 18, 16, 18, 19, 20, 21, 22, 20, 22, 23 };
  146. skybox.positions = new[] { 50.0f, -50.0f, 50.0f, -50.0f, -50.0f, 50.0f, -50.0f, 50.0f, 50.0f, 50.0f, 50.0f, 50.0f, 50.0f, 50.0f, -50.0f, -50.0f, 50.0f, -50.0f, -50.0f, -50.0f, -50.0f, 50.0f, -50.0f, -50.0f, 50.0f, 50.0f, -50.0f, 50.0f, -50.0f, -50.0f, 50.0f, -50.0f, 50.0f, 50.0f, 50.0f, 50.0f, -50.0f, 50.0f, 50.0f, -50.0f, -50.0f, 50.0f, -50.0f, -50.0f, -50.0f, -50.0f, 50.0f, -50.0f, -50.0f, 50.0f, 50.0f, -50.0f, 50.0f, -50.0f, 50.0f, 50.0f, -50.0f, 50.0f, 50.0f, 50.0f, 50.0f, -50.0f, 50.0f, 50.0f, -50.0f, -50.0f, -50.0f, -50.0f, -50.0f, -50.0f, -50.0f, 50.0f };
  147. skybox.uvs = new[] { 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f };
  148. skybox.normals = new[] { 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f, -1.0f, 0, 0f };
  149. if (SceneController != null)
  150. {
  151. Color skycolor = SceneController.skyboxOptions.albedoColor;
  152. Color skyreflect = SceneController.skyboxOptions.reflectivityColor;
  153. var skyboxMaterial = new BabylonPBRMaterial()
  154. {
  155. name = "sceneSkyboxMaterial",
  156. id = Guid.NewGuid().ToString(),
  157. albedo = skycolor.ToFloat(),
  158. reflectivity = skyreflect.ToFloat(),
  159. microSurface = SceneController.skyboxOptions.microSurface,
  160. cameraContrast = SceneController.skyboxOptions.cameraContrast,
  161. cameraExposure = SceneController.skyboxOptions.cameraExposure,
  162. directIntensity = SceneController.skyboxOptions.directIntensity,
  163. emissiveIntensity = SceneController.skyboxOptions.emissiveIntensity,
  164. specularIntensity = SceneController.skyboxOptions.specularIntensity,
  165. environmentIntensity = SceneController.skyboxOptions.environmentIntensity
  166. };
  167. skyboxMaterial.reflectionTexture = skytex;
  168. skyboxMaterial.backFaceCulling = false;
  169. skyboxMaterial.disableLighting = true;
  170. skybox.materialId = skyboxMaterial.id;
  171. skybox.infiniteDistance = true;
  172. babylonScene.MeshesList.Add(skybox);
  173. babylonScene.MaterialsList.Add(skyboxMaterial);
  174. babylonScene.AddTextureCube("sceneSkyboxMaterial");
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }