Search Unity

Scripting a material/ shader change(w or w/o lerp)

Discussion in 'Scripting' started by arumiat, Aug 26, 2014.

  1. arumiat

    arumiat

    Joined:
    Apr 26, 2014
    Posts:
    321
    Hi guys

    I currently have an animation that switches the color of a mesh between 2 different diffuse material colours using a collider and

    Code (CSharp):
    1. scL4S3LRenderer.renderer.material.color = new Color (20, 15, 10);

    I recently got working the very excellent GlowEffect asset from the UAS. I'd like to make it so that instead of turning red, my mesh takes on this glow effect shader. Initially I'd like it just to change instantly, but it would be cool to try lerping it as well later down the line.

    I'm very impressed with myself ;) as I've managed to get it to change from the material I assigned in the scene view (a plain grey) to the glow shader using this script;

    Code (CSharp):
    1. public class PerinealCollide2 : MonoBehaviour {
    2.  
    3.     private MeshRenderer scL4S3LRenderer;
    4.     private SplineAnimator impS1;
    5.     private Shader shader1;
    6.  
    7.     // Use this for initialization
    8.     void Start () {
    9.  
    10.         impS1 = GameObject.Find ("imp_S1").GetComponent <SplineAnimator> ();
    11.         scL4S3LRenderer = GameObject.Find ("sc_L4S3 L").GetComponent <MeshRenderer> ();  
    12.         shader1 = Shader.Find("Diffuse (Glow)");
    13.  
    14.     }
    15.  
    16.     // Update is called once per frame
    17.     void OnTriggerEnter (Collider col) {
    18.    
    19.         if (col.collider.name == impS1.name)
    20.         {
    21.             scL4S3LRenderer.renderer.material.shader = shader1;
    22.             impS1.tag = "Player3";
    23.         }
    24.  
    25.     }
    26. }
    What I'd like to do though is be able to access the individual 'variables' on the glow shader like these to set the color of the glow, and I'm not sure how to go about this. Do I need to create variables for each of these and then get a handle on them in the Start function? I don't think so as they're not traditional components (plus I tried and it didn't work!)

    Capture.PNG


    Thanks in advance
    T
     
  2. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  3. arumiat

    arumiat

    Joined:
    Apr 26, 2014
    Posts:
    321
    Thanks hpjohn

    By opening up the shader script I found out that the Main Color has the property of _Color so I can now switch to the correct shader and change the main color to black, and the glow color to blue, using:

    Code (CSharp):
    1. scL4S3LRenderer.renderer.material.shader = shader1;
    2. scL4S3LRenderer.renderer.material.SetColor ("_Color", Color.black);
    3. scL4S3LRenderer.renderer.material.SetColor ("_GlowColor", Color.blue);
    4.  

    I can't see in the shader script the way to switch on this property, i.e. enable the actual glow however? If I play the game the above scripts work but the glow is quickly switched on and then off..
    Capture.PNG
     
    Last edited: Aug 27, 2014
  4. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
  5. arumiat

    arumiat

    Joined:
    Apr 26, 2014
    Posts:
    321
    I actually ended up going even simpler and making a public Material variable that was the glow material, and just assigning this material using the OnTriggerEvent class.

    Thanks!