Search Unity

How to make a shader's alpha editable in script?

Discussion in 'Shaders' started by Samssonart, Jan 24, 2012.

  1. Samssonart

    Samssonart

    Joined:
    Dec 14, 2011
    Posts:
    25
    I need a shader that loads an image alpha as transparency, and is also self illuminated, this shader works for that:

    Shader "Self-Illumin/Transparent" {
    Properties {
    _Color ("Color Tint", Color) = (1,1,1,1)
    _MainTex ("SelfIllum Color (RGB) Alpha (A)", 2D) = "white"
    }
    Category {
    Lighting On
    ZWrite Off
    Cull Back
    Blend SrcAlpha OneMinusSrcAlpha
    Tags {Queue=Transparent}
    SubShader {
    Material {
    Emission [_Color]
    }
    Pass {
    SetTexture [_MainTex] {
    Combine Texture * Primary, Texture * Primary
    }
    }
    }
    }
    }

    But now I also need to be able to tweak the whole texture's transparency in runtime in script, like this:

    nuColor.a = Time.time*0.001;
    renderer.material.color = nuColor;

    I've noticed that the scipt works with some Unity's built-in shaders, and doesn't work on others, sadly, it doesn't work with my trasparent/self-illum shader, why is that?, what parameter(s) control that?
     
  2. Jessy

    Jessy

    Joined:
    Jun 7, 2007
    Posts:
    7,325
    Please use the code tags.

    http://unity3d.com/support/documentation/ScriptReference/Material.SetColor.html

    You have to incorporate the alpha channel of _Color somewhere in your shader. Sadly, it doesn't come through the Material block, for any term, as far as I remember, anyway. Just multiply by the constant instead of primary, on the last line, for the alpha channel. Also, realize that you do not need to write a separate alpha function if it's the same thing as the RGB calculation.

    Some of Unity's shaders use something other than "_Color", like "_TintColor". There's no good reason for it, aside from compatibility with old code.