You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.4 KiB

5 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class SpotLightSetUp : MonoBehaviour {
  5. public string ShaderName = "Paco/PointLight/ColorDiffuse";
  6. private Light pointLight;
  7. private void Awake()
  8. {
  9. UpdateShaders();
  10. }
  11. private void UpdateShaders()
  12. {
  13. float xPos = transform.position.x;
  14. float yPos = transform.position.y;
  15. float zPos = transform.position.z;
  16. pointLight = GetComponent<Light>();
  17. foreach (Material mat in findAllMaterialsInRange(pointLight.range)) {
  18. if (mat != null && mat.shader != null && mat.shader.name != null && mat.shader.name == ShaderName) {
  19. mat.SetFloat("_LightX", xPos);
  20. mat.SetFloat("_LightY", yPos);
  21. mat.SetFloat("_LightZ", zPos);
  22. }
  23. }
  24. }
  25. private Material[] findAllMaterialsInRange(float range)
  26. {
  27. List<Material> retVal = new List<Material>();
  28. Renderer[] arrend = (Renderer[])FindObjectsOfType(typeof(Renderer));
  29. foreach (Renderer rend in arrend) {
  30. foreach (Material mat in rend.materials) {
  31. if (!retVal.Contains(mat) && Vector3.Distance(transform.position,rend.transform.position) <= range) {
  32. retVal.Add(mat);
  33. }
  34. }
  35. }
  36. return retVal.ToArray();
  37. }
  38. }