Guest User

SetShaderValues.cs

a guest
Dec 13th, 2019
905
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SetShadervalues : MonoBehaviour
  6. {
  7.  
  8.     MaterialPropertyBlock props;
  9.     [SerializeField]
  10.     Transform target;
  11.     [SerializeField]
  12.     string shaderID = "_PositionMoving";
  13.     [SerializeField]
  14.     float appearSpeed = 10f;
  15.     [SerializeField]
  16.     float disappearSpeed = 5f;
  17.     [SerializeField]
  18.     float radius = 12f;
  19.     [SerializeField]
  20.     float radiusRandomRange;
  21.     [SerializeField]
  22.     bool keep = false;
  23.  
  24.     [SerializeField]
  25.     float minRangeRandomOffset = -3f;
  26.     [SerializeField]
  27.     float maxRangeRandomOffset = 3f;
  28.  
  29.     [SerializeField]
  30.     MeshRenderer[] objects;
  31.     float[] values;
  32.     float[] offsets;
  33.     float[] radiusRandomRanges;
  34.  
  35.     // Start is called before the first frame update
  36.     void Start()
  37.     {
  38.         props = new MaterialPropertyBlock();
  39.         values = new float[objects.Length];
  40.         offsets = new float[objects.Length];
  41.         radiusRandomRanges = new float[objects.Length];
  42.         SetRandomOffset();
  43.         MeshBounds(); // hack to stop culling because the object is so far from its origin
  44.     }
  45.  
  46.     // Update is called once per frame
  47.     void Update()
  48.     {
  49.         Shader.SetGlobalVector(shaderID, target.transform.position); // set position to follow
  50.         for (int i = 0; i < objects.Length; i++)
  51.         {
  52.             Vector3 offset = objects[i].transform.position - target.position;
  53.             float sqrLen = offset.sqrMagnitude;
  54.             if (sqrLen < radius * radius + radiusRandomRanges[i])
  55.             {
  56.                 values[i] = Mathf.Lerp(values[i], 1, Time.deltaTime * appearSpeed);// set property float to 1 over time
  57.             }
  58.             else if (!keep)
  59.             {
  60.                 values[i] = Mathf.Lerp(values[i], 0, Time.deltaTime * disappearSpeed);// set property float to 0 over time if keep is not true
  61.             }
  62.             props.SetFloat("_Moved", values[i]);
  63.             props.SetFloat("_RandomOffset", offsets[i]);
  64.             objects[i].SetPropertyBlock(props);
  65.         }
  66.     }
  67.  
  68.     void SetRandomOffset()
  69.     {
  70.         for (int i = 0; i < objects.Length; i++)
  71.         {
  72.             offsets[i] = Random.Range(minRangeRandomOffset, maxRangeRandomOffset);
  73.             radiusRandomRanges[i] = Random.Range(-radiusRandomRange, radiusRandomRange);
  74.  
  75.  
  76.         }
  77.     }
  78.  
  79.     void MeshBounds()
  80.     {
  81.  
  82.         for (int i = 0; i < objects.Length; i++)
  83.         {
  84.             Mesh mesh = objects[i].GetComponent<MeshFilter>().mesh;
  85.             mesh.bounds = new Bounds(Vector3.zero, 100f * Vector3.one);
  86.         }
  87.  
  88.     }
  89. }
RAW Paste Data