ShaderParameterSetter.cs 766 B

12345678910111213141516171819202122232425262728
  1. using UnityEngine;
  2. namespace Assets.MaterializationFX.Scripts.Utils
  3. {
  4. internal sealed class ShaderParameterSetter : MonoBehaviour
  5. {
  6. public string ShaderName = "QFX/Materialization_World";
  7. public string ParameterName = "_WorldPos";
  8. public Vector3 ParameterOffset;
  9. public GameObject TargetObjects;
  10. public bool ChangeChilds;
  11. private Renderer[] _rends;
  12. private void Start()
  13. {
  14. _rends = !ChangeChilds ? new[] { TargetObjects.GetComponent<Renderer>() } : TargetObjects.GetComponentsInChildren<Renderer>();
  15. foreach (var rend in _rends)
  16. rend.material.shader = Shader.Find(ShaderName);
  17. }
  18. private void Update()
  19. {
  20. foreach (var rend in _rends)
  21. rend.material.SetVector(ParameterName, transform.position + ParameterOffset);
  22. }
  23. }
  24. }