Advertisement
Guest User

Cullable.cs

a guest
Dec 23rd, 2021
1,088
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.69 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class Cullable : MonoBehaviour
  6. {
  7.     //speed of the transition
  8.     public float m_alphaChangeSpeed = 3f;
  9.  
  10.     // name of the variable in the shader that will be adjusted
  11.     public string m_shaderVariableName = "_PosSlider";
  12.  
  13.     // end point of culling
  14.     public float m_fadeTo = 0;
  15.     // start point of culling
  16.     public float m_fadeFrom = 1;
  17.  
  18.     // the float for the transition
  19.     private float m_currentAlpha = 1.0f;
  20.  
  21.     // The material that will be affected by the transition
  22.     private Material m_mat;
  23.     // Set to true if we want to fade the object out because it's in the way
  24.     private bool m_occluding;
  25.  
  26.     // Set to true when the fade control co-routine is active
  27.     private bool m_inCoroutine = false;
  28.  
  29.  
  30.     public bool Occluding
  31.     {
  32.         get { return m_occluding; }
  33.         set
  34.         {
  35.             if (m_occluding != value)
  36.             {
  37.                 m_occluding = value;
  38.                 OnOccludingChanged();
  39.             }
  40.         }
  41.  
  42.     }
  43.  
  44.     // Called when the Occluding value is changed
  45.     private void OnOccludingChanged()
  46.     {
  47.         if (!m_inCoroutine)
  48.         {
  49.  
  50.             StartCoroutine("FadeAlphaRoutine");
  51.             m_inCoroutine = true;
  52.  
  53.  
  54.         }
  55.     }
  56.     // Start is called before the first frame update
  57.     void Start()
  58.     {
  59.         // grab the renderer's material and set the current alpha
  60.         m_mat = GetComponent<Renderer>().material;
  61.         m_currentAlpha = m_fadeFrom;
  62.     }
  63.  
  64.  
  65.     // Return the alpha value we want on all of our models
  66.     private float GetTargetAlpha()
  67.     {
  68.         if (m_occluding)
  69.         {
  70.             return m_fadeTo;
  71.         }
  72.         else
  73.         {
  74.             return m_fadeFrom;
  75.         }
  76.     }
  77.  
  78.     private IEnumerator FadeAlphaRoutine()
  79.     {
  80.         while (m_currentAlpha != GetTargetAlpha())
  81.         {
  82.             float alphaShift = m_alphaChangeSpeed * Time.deltaTime;
  83.  
  84.             float targetAlpha = GetTargetAlpha();
  85.             if (m_currentAlpha < targetAlpha)
  86.             {
  87.                 m_currentAlpha += alphaShift;
  88.                 if (m_currentAlpha > targetAlpha)
  89.                 {
  90.                     m_currentAlpha = targetAlpha;
  91.                 }
  92.             }
  93.             else
  94.             {
  95.                 m_currentAlpha -= alphaShift;
  96.                 if (m_currentAlpha < targetAlpha)
  97.                 {
  98.                     m_currentAlpha = targetAlpha;
  99.                 }
  100.             }
  101.  
  102.             m_mat.SetFloat(m_shaderVariableName, m_currentAlpha);
  103.  
  104.             yield return null;
  105.         }
  106.         m_inCoroutine = false;
  107.     }
  108. }
  109.  
  110.  
  111.  
  112.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement