SceneBuilder.Cameras.cs 12 KB

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