LightGenerator.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. using UnityEngine;
  2. #if UNITY_EDITOR
  3. using UnityEditor;
  4. #endif
  5. namespace VLB_Samples
  6. {
  7. public class LightGenerator : MonoBehaviour
  8. {
  9. [Range(1, 100)]
  10. [SerializeField] int CountX = 10;
  11. [Range(1, 100)]
  12. [SerializeField] int CountY = 10;
  13. [SerializeField] float OffsetUnits = 1;
  14. [SerializeField] float PositionY = 1;
  15. [SerializeField] bool NoiseEnabled = false;
  16. [SerializeField] bool AddLight = true;
  17. public void Generate()
  18. {
  19. for (int i = 0; i < CountX; ++i)
  20. {
  21. for (int j = 0; j < CountY; ++j)
  22. {
  23. GameObject gao = null;
  24. if(AddLight)
  25. gao = new GameObject("Light_" + i + "_" + j, typeof(Light), typeof(VLB.VolumetricLightBeam), typeof(Rotater));
  26. else
  27. gao = new GameObject("Light_" + i + "_" + j, typeof(VLB.VolumetricLightBeam), typeof(Rotater));
  28. gao.transform.position = new Vector3(i * OffsetUnits, PositionY, j * OffsetUnits);
  29. gao.transform.rotation = Quaternion.Euler(Random.Range(-45, 45) + 90f, Random.Range(0, 360), 0);
  30. var beam = gao.GetComponent<VLB.VolumetricLightBeam>();
  31. if (AddLight)
  32. {
  33. var light = gao.GetComponent<Light>();
  34. light.type = LightType.Spot;
  35. light.color = new Color(Random.value, Random.value, Random.value, 1.0f);
  36. light.range = Random.Range(3f, 8f);
  37. light.intensity = Random.Range(0.2f, 5f);
  38. light.spotAngle = Random.Range(10f, 90f);
  39. if(VLB.Config.Instance.geometryOverrideLayer)
  40. {
  41. // remove the layer of the beams from the light's culling mask to prevent from breaking GPU Instancing
  42. light.cullingMask = ~(1 << VLB.Config.Instance.geometryLayerID);
  43. }
  44. }
  45. else
  46. {
  47. beam.color = new Color(Random.value, Random.value, Random.value, 1.0f);
  48. beam.fallOffEnd = Random.Range(3f, 8f);
  49. beam.spotAngle = Random.Range(10f, 90f);
  50. }
  51. beam.coneRadiusStart = Random.Range(0f, 0.1f);
  52. beam.geomCustomSides = Random.Range(12, 36);
  53. beam.fresnelPow = Random.Range(1, 7.5f);
  54. beam.noiseMode = NoiseEnabled ? VLB.NoiseMode.WorldSpace : VLB.NoiseMode.Disabled;
  55. var rotater = gao.GetComponent<Rotater>();
  56. rotater.EulerSpeed = new Vector3(0, Random.Range(-500, 500), 0);
  57. }
  58. }
  59. }
  60. }
  61. #if UNITY_EDITOR
  62. [CustomEditor(typeof(LightGenerator))]
  63. public class LightGeneratorEditor : Editor
  64. {
  65. public override void OnInspectorGUI()
  66. {
  67. base.OnInspectorGUI();
  68. if (Application.isPlaying) return;
  69. if (GUILayout.Button("Generate"))
  70. {
  71. (target as LightGenerator).Generate();
  72. }
  73. }
  74. }
  75. #endif
  76. }