Advertisement
Guest User

SetShaderValues.cs

a guest
Nov 30th, 2019
1,628
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.40 KB | None | 0 0
  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.     bool keep = false;
  21.  
  22.     [SerializeField]
  23.     float minRangeRandomOffset = -3f;
  24.     [SerializeField]
  25.     float maxRangeRandomOffset = 3f;
  26.  
  27.     [SerializeField]
  28.     MeshRenderer[] objects;
  29.     float[] values;
  30.     float[] offsets;
  31.    
  32.     // Start is called before the first frame update
  33.     void Start()
  34.     {
  35.         props = new MaterialPropertyBlock();
  36.         values = new float[objects.Length];
  37.         offsets = new float[objects.Length];
  38.         SetRandomOffset();
  39.         MeshBounds(); // hack to stop culling because the object is so far from its origin
  40.     }
  41.  
  42.     // Update is called once per frame
  43.     void Update()
  44.     {
  45.         Shader.SetGlobalVector(shaderID, target.transform.position); // set position to follow
  46.         for (int i = 0; i < objects.Length; i++)
  47.         {
  48.             Vector3 offset = objects[i].transform.position - target.position;
  49.             float sqrLen = offset.sqrMagnitude;
  50.             if (sqrLen < radius * radius)
  51.             {
  52.                 values[i] = Mathf.Lerp(values[i], 1, Time.deltaTime * appearSpeed);// set property float to 1 over time
  53.             }            
  54.             else if (!keep)
  55.             {
  56.                 values[i] = Mathf.Lerp(values[i], 0, Time.deltaTime * disappearSpeed);// set property float to 0 over time if keep is not true
  57.             }
  58.             props.SetFloat("_Moved", values[i]);
  59.             props.SetFloat("_RandomOffset", offsets[i]);
  60.             objects[i].SetPropertyBlock(props);
  61.         }
  62.     }
  63.  
  64.     void SetRandomOffset()
  65.     {
  66.         for (int i = 0; i < objects.Length; i++)
  67.         {
  68.             offsets[i] = Random.Range(minRangeRandomOffset, maxRangeRandomOffset);
  69.          
  70.         }
  71.     }
  72.  
  73.     void MeshBounds()
  74.     {
  75.  
  76.         for (int i = 0; i < objects.Length; i++)
  77.         {
  78.             Mesh mesh = objects[i].GetComponent<MeshFilter>().mesh;
  79.             mesh.bounds = new Bounds(Vector3.zero, 100f * Vector3.one);
  80.         }
  81.  
  82.     }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement