using UnityEngine; using System.Collections; public class OilReveal : MonoBehaviour { [SerializeField] private float diffuseSpeed = 1f;//diffuse offset Y的变化速度 [SerializeField] private float nodeSpeed = 1f;//node4372 offset Y的变化速度 [SerializeField] private float alphaSpeed = 1f;//alpha变化速度 [SerializeField] private float bulgeIntervalPeriod = 0.1f;//波动周期 取随机数的间隔时间 [SerializeField] private float bulgeLateTime = 0f;//波动延迟时间 [SerializeField] private float alphaLateTime = 1f;//透明变化延迟时间 [SerializeField] private float colorLateTime = 1f;//颜色变化延迟时间 [SerializeField] private float bulgeScaleMin = 0f;//bulgeScale最小值 [SerializeField] private float bulgeScaleMax = 1f;//bulgeScale最大值 [SerializeField] private float nodeYMin = 0f;//node4372 offset Y的最小值 //[SerializeField] public float nodeYMax = 1f;//node4372 offset Y的最大值 [SerializeField] private float alphaMin = 0f;//alpha的最小值 [SerializeField] private float alphaMax = 1f;//alpha最大值 [SerializeField] private Color color = new Color();//颜色 private MeshRenderer oil;//燃油的shander private void Start () { oil = GetComponent(); //初始化数值 nodeValue = nodeYMin;//oil.material.GetTextureOffset("_node_4372").y; diffuseValue = oil.material.GetTextureOffset("_Diffuse").y; bulgeValue = bulgeScaleMin;//oil.material.GetFloat("_BulgeScale"); alphaValue = alphaMin;//oil.material.GetFloat("_alpha"); if (bulgeIsOn) InvokeRepeating("Fluctuate", bulgeLateTime, bulgeIntervalPeriod); if (alphaIsOn) InvokeRepeating("AlphaChange", alphaLateTime, Time.deltaTime); if (colorIsOn) Invoke("SetColor", colorLateTime); } [SerializeField] private bool diffuseIsOn;//燃油流动开关 [SerializeField] private bool nodeIsOn;//延伸开关 [SerializeField] private bool bulgeIsOn;//波动开关 [SerializeField] private bool alphaIsOn;//透明通道变化开关 [SerializeField] private bool colorIsOn;//透明通道变化开关 [SerializeField] private bool bulgeTransitionIsOn;//开启波动过度 //面板数值 [SerializeField] public float nodeValue; private float diffuseValue; private float bulgeValue; private float alphaValue; private void Update () { //燃油流动效果 if (diffuseIsOn) { diffuseValue -= Time.deltaTime * diffuseSpeed; oil.material.SetTextureOffset("_Diffuse", new Vector2(0, diffuseValue)); } //燃油延伸效果 if (nodeIsOn && nodeValue < nodeYMax) { nodeValue += Time.deltaTime * nodeSpeed / 20; oil.material.SetTextureOffset("_node_4372", new Vector2(0, nodeValue)); } //波动效果 if (bulgeIsOn && bulgeUpdate) { if (bulgeTransitionIsOn) bulgeValue = Lerp(bulgeValue, bulgeValue2); oil.material.SetFloat("_BulgeScale", bulgeValue); } } //private float bulgeValue1; private float bulgeValue2; private bool bulgeUpdate; //波动效果 private void Fluctuate() { bulgeUpdate = true; //波动效果 //bulgeValue = GetRandom(bulgeScaleMin, bulgeScaleMax); //oil.material.SetFloat("_BulgeScale", bulgeValue); if (bulgeTransitionIsOn)//开启过度 { //bulgeValue1 = bulgeValue; bulgeValue2 = GetRandom(bulgeScaleMin, bulgeScaleMax); } else//不使用过度 bulgeValue = GetRandom(bulgeScaleMin, bulgeScaleMax); } //开启透明变化 private void AlphaChange() { alphaValue += Time.deltaTime * alphaSpeed; oil.material.SetFloat("_alpha", alphaValue); if(alphaValue > alphaMax) { CancelInvoke("AlphaChange"); } } //颜色变化 private void SetColor() { oil.material.SetColor("_color", color); } private float random = 0;//上一次的随机数 //随机数 private float GetRandom(float min, float max) { float number = Random.Range(min, max); if (number == random) return GetRandom(min, max); else { random = number; return number; } } //返回两个值之间周期内过渡数 private float Lerp(float value1,float value2) { //print(Time.time % bulgeIntervalPeriod / bulgeIntervalPeriod); return Mathf.Lerp(value1, value2, Time.time % bulgeIntervalPeriod / bulgeIntervalPeriod); } }