SceneBuilder.Lights.cs 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.shadowCastingMode != ShadowCastingMode.Off)
  25. {
  26. renderList.Add(GetID(gameObject));
  27. continue;
  28. }
  29. var skinnedMesh = gameObject.GetComponent<SkinnedMeshRenderer>();
  30. if (skinnedMesh != 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. ExporterWindow.ReportProgress(progress, "Exporting light: " + light.name);
  41. BabylonLight babylonLight = new BabylonLight
  42. {
  43. name = light.name,
  44. id = GetID(light.gameObject),
  45. parentId = GetParentID(light.transform)
  46. };
  47. switch (light.type)
  48. {
  49. case LightType.Spot:
  50. babylonLight.type = 2;
  51. break;
  52. case LightType.Directional:
  53. babylonLight.type = 1;
  54. break;
  55. case LightType.Point:
  56. babylonLight.type = 0;
  57. babylonLight.range = light.range;
  58. break;
  59. case LightType.Area:
  60. // TODO
  61. break;
  62. }
  63. babylonLight.position = light.transform.localPosition.ToFloat();
  64. var direction = new Vector3(0, 0, 1);
  65. var transformedDirection = light.transform.TransformDirection(direction);
  66. babylonLight.direction = transformedDirection.ToFloat();
  67. babylonLight.diffuse = light.color.ToFloat();
  68. babylonLight.intensity = light.intensity;
  69. babylonLight.angle = light.spotAngle * (float)Math.PI / 180;
  70. babylonLight.exponent = 1.0f;
  71. babylonScene.LightsList.Add(babylonLight);
  72. // Animations
  73. ExportAnimations(light.transform, babylonLight);
  74. // Shadows
  75. if ((light.type == LightType.Directional || light.type == LightType.Spot) && light.shadows != LightShadows.None)
  76. {
  77. GenerateShadowsGenerator(light);
  78. }
  79. }
  80. }
  81. }