SceneBuilder.Lights.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using System;
  2. using System.Collections.Generic;
  3. using BabylonExport.Entities;
  4. using UnityEngine;
  5. using UnityEngine.Rendering;
  6. namespace Unity3D2Babylon
  7. {
  8. partial class SceneBuilder
  9. {
  10. private void GenerateShadowsGenerator(Light light)
  11. {
  12. var generator = new BabylonShadowGenerator
  13. {
  14. lightId = GetID(light.gameObject),
  15. usePoissonSampling = light.shadows == LightShadows.Soft,
  16. mapSize = 256 + 256 * QualitySettings.GetQualityLevel(),
  17. bias = light.shadowBias / 10.0f
  18. };
  19. var renderList = new List<string>();
  20. foreach (var gameObject in gameObjects)
  21. {
  22. var meshFilter = gameObject.GetComponent<MeshFilter>();
  23. var renderer = gameObject.GetComponent<Renderer>();
  24. if (meshFilter != null && renderer != null && renderer.shadowCastingMode != ShadowCastingMode.Off)
  25. {
  26. renderList.Add(GetID(gameObject));
  27. continue;
  28. }
  29. var skinnedMesh = gameObject.GetComponent<SkinnedMeshRenderer>();
  30. if (skinnedMesh != null && renderer != null && renderer.shadowCastingMode != ShadowCastingMode.Off)
  31. {
  32. renderList.Add(GetID(gameObject));
  33. }
  34. }
  35. generator.renderList = renderList.ToArray();
  36. babylonScene.ShadowGeneratorsList.Add(generator);
  37. }
  38. private void ConvertUnityLightToBabylon(Light light, float progress)
  39. {
  40. if (!light.isActiveAndEnabled || light.alreadyLightmapped)
  41. {
  42. return;
  43. }
  44. ExporterWindow.ReportProgress(progress, "Exporting light: " + light.name);
  45. BabylonLight babylonLight = new BabylonLight
  46. {
  47. name = light.name,
  48. id = GetID(light.gameObject),
  49. parentId = GetParentID(light.transform)
  50. };
  51. switch (light.type)
  52. {
  53. case LightType.Spot:
  54. babylonLight.type = 2;
  55. break;
  56. case LightType.Directional:
  57. babylonLight.type = 1;
  58. break;
  59. case LightType.Point:
  60. babylonLight.type = 0;
  61. babylonLight.range = light.range;
  62. break;
  63. case LightType.Area:
  64. // TODO
  65. break;
  66. }
  67. babylonLight.position = light.transform.localPosition.ToFloat();
  68. var direction = new Vector3(0, 0, 1);
  69. var transformedDirection = light.transform.TransformDirection(direction);
  70. babylonLight.direction = transformedDirection.ToFloat();
  71. babylonLight.diffuse = light.color.ToFloat();
  72. babylonLight.intensity = light.intensity;
  73. babylonLight.angle = light.spotAngle * (float)Math.PI / 180;
  74. babylonLight.exponent = 1.0f;
  75. babylonScene.LightsList.Add(babylonLight);
  76. // Animations
  77. ExportAnimations(light.transform, babylonLight);
  78. // Shadows
  79. if (exportationOptions.ExportShadows)
  80. {
  81. if ((light.type == LightType.Directional || light.type == LightType.Spot) && light.shadows != LightShadows.None)
  82. {
  83. GenerateShadowsGenerator(light);
  84. }
  85. }
  86. }
  87. }
  88. }