SceneBuilder.Cameras.cs 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. babylonCamera.applyGravity = (exportationOptions.Gravity.X == 0 && exportationOptions.Gravity.Y == 0 && exportationOptions.Gravity.Z == 0) ? false : true;
  64. babylonCamera.ellipsoid = exportationOptions.CameraEllipsoid.ToFloat();
  65. }
  66. // Lens Flares
  67. ParseLensFlares(gameObject, babylonCamera.id, ref lensFlares);
  68. // Particles Systems
  69. ParseParticleSystems(gameObject, babylonCamera.id, ref particleSystems);
  70. }
  71. private void ConvertUnitySkyboxToBabylon(Camera camera, float progress)
  72. {
  73. // Skybox
  74. bool png = (exportationOptions.DefaultImageFormat == (int)BabylonImageFormat.PNG);
  75. string fext = (png == true) ? ".png" : ".jpg";
  76. if ((camera.clearFlags & CameraClearFlags.Skybox) == CameraClearFlags.Skybox)
  77. {
  78. if (RenderSettings.skybox != null)
  79. {
  80. BabylonTexture skytex = null;
  81. if (RenderSettings.skybox.shader.name == "Skybox/Cubemap")
  82. {
  83. var cubeMap = RenderSettings.skybox.GetTexture("_Tex") as Cubemap;
  84. if (cubeMap != null)
  85. {
  86. var srcTexturePath = AssetDatabase.GetAssetPath(cubeMap);
  87. if (srcTexturePath.EndsWith(".hdr", StringComparison.OrdinalIgnoreCase))
  88. {
  89. var hdr = new BabylonHDRCubeTexture();
  90. hdr.size = cubeMap.width;
  91. skytex = hdr;
  92. CopyTextureCube(String.Format("{0}Skybox.hdr", SceneName), cubeMap, skytex, true);
  93. }
  94. else
  95. {
  96. skytex = new BabylonTexture();
  97. CopyTextureCube(String.Format("{0}Skybox.hdr", SceneName), cubeMap, skytex, false);
  98. if (png) skytex.extensions = new string[] { "_px.png", "_py.png", "_pz.png", "_nx.png", "_ny.png", "_nz.png" };
  99. }
  100. }
  101. }
  102. else if (RenderSettings.skybox.shader.name == "Skybox/6 Sided")
  103. {
  104. var frontTexture = RenderSettings.skybox.GetTexture("_FrontTex") as Texture2D;
  105. var backTexture = RenderSettings.skybox.GetTexture("_BackTex") as Texture2D;
  106. var leftTexture = RenderSettings.skybox.GetTexture("_LeftTex") as Texture2D;
  107. var rightTexture = RenderSettings.skybox.GetTexture("_RightTex") as Texture2D;
  108. var upTexture = RenderSettings.skybox.GetTexture("_UpTex") as Texture2D;
  109. var downTexture = RenderSettings.skybox.GetTexture("_DownTex") as Texture2D;
  110. if (frontTexture != null && backTexture != null && leftTexture != null && rightTexture != null && upTexture != null && downTexture != null)
  111. {
  112. skytex = new BabylonTexture();
  113. skytex.name = String.Format("{0}Skybox", SceneName);
  114. var frontTextureName = String.Format("{0}_pz{1}", skytex.name, fext);
  115. var frontTexturePath = Path.Combine(babylonScene.OutputPath, frontTextureName);
  116. CopyTextureFace(frontTexturePath, frontTextureName, frontTexture);
  117. var backTextureName = String.Format("{0}_nz{1}", skytex.name, fext);
  118. var backTexturePath = Path.Combine(babylonScene.OutputPath, backTextureName);
  119. CopyTextureFace(backTexturePath, backTextureName, backTexture);
  120. var leftTextureName = String.Format("{0}_px{1}", skytex.name, fext);
  121. var leftTexturePath = Path.Combine(babylonScene.OutputPath, leftTextureName);
  122. CopyTextureFace(leftTexturePath, leftTextureName, leftTexture);
  123. var rightTextureName = String.Format("{0}_nx{1}", skytex.name, fext);
  124. var rightTexturePath = Path.Combine(babylonScene.OutputPath, rightTextureName);
  125. CopyTextureFace(rightTexturePath, rightTextureName, rightTexture);
  126. var upTextureName = String.Format("{0}_py{1}", skytex.name, fext);
  127. var upTexturePath = Path.Combine(babylonScene.OutputPath, upTextureName);
  128. CopyTextureFace(upTexturePath, upTextureName, upTexture);
  129. var downTextureName = String.Format("{0}_ny{1}", skytex.name, fext);
  130. var downTexturePath = Path.Combine(babylonScene.OutputPath, downTextureName);
  131. CopyTextureFace(downTexturePath, downTexturePath, downTexture);
  132. if (png) skytex.extensions = new string[] { "_px.png", "_py.png", "_pz.png", "_nx.png", "_ny.png", "_nz.png" };
  133. }
  134. }
  135. if (skytex != null)
  136. {
  137. skytex.isCube = true;
  138. skytex.coordinatesMode = 5;
  139. skytex.level = (SceneController != null) ? SceneController.skyboxOptions.lightIntensity : 1.0f;
  140. var skybox = new BabylonMesh();
  141. skybox.name = "sceneSkyboxMesh";
  142. skybox.id = Guid.NewGuid().ToString();
  143. 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 };
  144. 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 };
  145. 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 };
  146. 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 };
  147. if (SceneController != null)
  148. {
  149. Color skycolor = SceneController.skyboxOptions.albedoColor;
  150. Color skyreflect = SceneController.skyboxOptions.reflectivityColor;
  151. var skyboxMaterial = new BabylonPBRMaterial()
  152. {
  153. name = "sceneSkyboxMaterial",
  154. id = Guid.NewGuid().ToString(),
  155. albedo = skycolor.ToFloat(),
  156. reflectivity = skyreflect.ToFloat(),
  157. microSurface = SceneController.skyboxOptions.microSurface,
  158. cameraContrast = SceneController.skyboxOptions.cameraContrast,
  159. cameraExposure = SceneController.skyboxOptions.cameraExposure,
  160. directIntensity = SceneController.skyboxOptions.directIntensity,
  161. emissiveIntensity = SceneController.skyboxOptions.emissiveIntensity,
  162. specularIntensity = SceneController.skyboxOptions.specularIntensity,
  163. environmentIntensity = SceneController.skyboxOptions.environmentIntensity
  164. };
  165. skyboxMaterial.reflectionTexture = skytex;
  166. skyboxMaterial.backFaceCulling = false;
  167. skyboxMaterial.disableLighting = true;
  168. skybox.materialId = skyboxMaterial.id;
  169. skybox.infiniteDistance = true;
  170. babylonScene.MeshesList.Add(skybox);
  171. babylonScene.MaterialsList.Add(skyboxMaterial);
  172. babylonScene.AddTextureCube("sceneSkyboxMaterial");
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }