SceneBuilder.Cameras.cs 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using BabylonExport.Entities;
  3. using UnityEngine;
  4. namespace Unity3D2Babylon
  5. {
  6. partial class SceneBuilder
  7. {
  8. private void ConvertUnityCameraToBabylon(Camera camera, float progress)
  9. {
  10. ExporterWindow.ReportProgress(progress, "Exporting camera: " + camera.name);
  11. BabylonCamera babylonCamera = new BabylonCamera
  12. {
  13. name = camera.name,
  14. id = GetID(camera.gameObject),
  15. fov = camera.fieldOfView*(float) Math.PI/180,
  16. minZ = camera.nearClipPlane,
  17. maxZ = camera.farClipPlane,
  18. parentId = GetParentID(camera.transform),
  19. position = camera.transform.localPosition.ToFloat()
  20. };
  21. var target = new Vector3(0, 0, 1);
  22. var transformedTarget = camera.transform.TransformDirection(target);
  23. babylonCamera.target = (camera.transform.position + transformedTarget).ToFloat();
  24. babylonScene.CamerasList.Add(babylonCamera);
  25. if (Camera.main == camera)
  26. {
  27. babylonScene.activeCameraID = babylonCamera.id;
  28. babylonScene.clearColor = camera.backgroundColor.ToFloat();
  29. }
  30. // Animations
  31. ExportAnimations(camera.transform, babylonCamera);
  32. // Collisions
  33. if (exportationOptions.ExportCollisions)
  34. {
  35. babylonCamera.checkCollisions = true;
  36. babylonCamera.applyGravity = true;
  37. babylonCamera.ellipsoid = exportationOptions.CameraEllipsoid.ToFloat();
  38. }
  39. }
  40. private void ConvertUnitySkyboxToBabylon(Camera camera, float progress)
  41. {
  42. // Skybox
  43. if ((camera.clearFlags & CameraClearFlags.Skybox) == CameraClearFlags.Skybox)
  44. {
  45. if (RenderSettings.skybox != null)
  46. {
  47. if (RenderSettings.skybox.shader.name == "Skybox/Cubemap")
  48. {
  49. var cubeMap = RenderSettings.skybox.GetTexture("_Tex") as Cubemap;
  50. if (cubeMap != null)
  51. {
  52. var skytex = new BabylonTexture();
  53. CopyTextureCube("sceneSkybox.hdr", cubeMap, skytex);
  54. skytex.coordinatesMode = 5;
  55. skytex.level = RenderSettings.reflectionIntensity;
  56. BabylonMesh skybox = new BabylonMesh();
  57. 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 };
  58. 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 };
  59. 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 };
  60. 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 };
  61. var skyboxMaterial = new BabylonPBRMaterial()
  62. {
  63. name = "sceneSkyboxMaterial",
  64. id = Guid.NewGuid().ToString(),
  65. albedo = new[] { 1.0f, 1.0f, 1.0f, 1.0f },
  66. reflectivity = new[] { 0.0f, 0.0f, 0.0f },
  67. microSurface = 1.0f,
  68. directIntensity = 0.0f,
  69. specularIntensity = 0.0f,
  70. environmentIntensity = 1.0f
  71. };
  72. skyboxMaterial.backFaceCulling = false;
  73. skybox.materialId = skyboxMaterial.id;
  74. skybox.infiniteDistance = true;
  75. skyboxMaterial.reflectionTexture = skytex;
  76. babylonScene.MeshesList.Add(skybox);
  77. babylonScene.MaterialsList.Add(skyboxMaterial);
  78. babylonScene.AddTextureCube("sceneSkyboxMaterial");
  79. }
  80. }
  81. }
  82. }
  83. }
  84. }
  85. }