Guest User

MaterialPropertyBlockTest.cs

a guest
Dec 4th, 2018
2,568
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.37 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class MaterialPropertyBlockTest : MonoBehaviour {
  6.  
  7.  
  8.     public bool lerp;  
  9.     public float amplitude = 3f;
  10.  
  11.     [Header("Color Primary")]
  12.     [Range(0, 1)]
  13.     public float HueMin1 ;
  14.     [Range(0, 1)]
  15.     public float HueMax1 =1f;
  16.     [Range(0, 1)]
  17.     public float SaturationMin1;
  18.     [Range(0, 1)]
  19.     public float SaturationMax1=1f;
  20.     [Range(0, 1)]
  21.     public float ValueMin1;
  22.     [Range(0, 1)]
  23.     public float ValueMax1=1f;
  24.     [Header("Color Secondary")]
  25.     [Range(0, 1)]
  26.     public float HueMin2;
  27.     [Range(0, 1)]
  28.     public float HueMax2=1f;
  29.     [Range(0, 1)]
  30.     public float SaturationMin2;
  31.     [Range(0, 1)]
  32.     public float SaturationMax2=1f;
  33.     [Range(0, 1)]
  34.     public float ValueMin2;
  35.     [Range(0, 1)]
  36.     public float ValueMax2=1f;
  37.     [Header("Color Tertiary")]
  38.     [Range(0, 1)]
  39.     public float HueMin3;
  40.     [Range(0, 1)]
  41.     public float HueMax3=1f;
  42.     [Range(0, 1)]
  43.     public float SaturationMin3;
  44.     [Range(0, 1)]
  45.     public float SaturationMax3=1f;
  46.     [Range(0, 1)]
  47.     public float ValueMin3;
  48.     [Range(0, 1)]
  49.     public float ValueMax3=1f;
  50.  
  51.     Color randomColor;
  52.     Color randomColor2;
  53.     Color randomColor3;
  54.     MeshRenderer renderer;
  55.     MaterialPropertyBlock props;
  56.  
  57.     // Use this for initialization
  58.     void Start () {
  59.         props = new MaterialPropertyBlock();      
  60.        
  61.         randomColor =  Random.ColorHSV(HueMin1, HueMax1, SaturationMin1, SaturationMax1, ValueMin1, ValueMax1);
  62.         randomColor2 = Random.ColorHSV(HueMin2, HueMax2, SaturationMin2, SaturationMax2, ValueMin2, ValueMax2);
  63.         randomColor3 = Random.ColorHSV(HueMin3, HueMax3, SaturationMin3, SaturationMax3, ValueMin3, ValueMax3);
  64.  
  65.         props.SetColor("_Color", randomColor);
  66.         props.SetColor("_ColorSec", randomColor2);
  67.         props.SetColor("_ColorTert", randomColor3);
  68.         renderer = GetComponent<MeshRenderer>();
  69.         renderer.SetPropertyBlock(props);
  70.     }
  71.    
  72.     // Update is called once per frame
  73.     void Update () {
  74.        if (lerp)
  75.        {
  76.             props.SetColor("_Color", Color.Lerp(Color.cyan, Color.magenta, (Mathf.Sin(Time.time * amplitude + (transform.position.x + transform.position.z)) + 1) / 2f));
  77.             renderer.SetPropertyBlock(props);
  78.         }                        
  79.     }
  80.  
  81.  
  82.  
  83. }
Advertisement
Add Comment
Please, Sign In to add comment