Search Unity

Shader with animated transparency map

Discussion in 'Shaders' started by StridingDragon, Nov 16, 2020.

  1. StridingDragon

    StridingDragon

    Joined:
    Jan 16, 2013
    Posts:
    78
    I am looking for a way to animate an object's transparency map. The object has a static main texture but the map that is used to alpha blend the main texture should be animated by manipulating the UV coordinates of the transparency map over time, like a frame animation. It's not just a matter of setting an object's opacity, but I am looking for something that would allow me to essentially define a different alpha map whenever I want to, like a transparency sprite animation to play through, without ever changing the main texture at all.

    Does such a shader exist and where would I find it?
     
  2. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,549
    Any shader you find can be modified to do this really. It's just a matter of defining another texture input and sampling that input (with modified UV value) to use as your output Alpha value instead of using the alpha channel from the sampled _MainTex.

    So, in the Properties section of your shader you'd add
    _AlphaTex ("Alpha Texture", 2D) = "white" {}


    Then, somewhere in your CGPROGRAM section of the shader, you'd have
    sampler2D _AlphaTex;
    to create the actual shader field for that material property above to assign its texture to.

    Then, in your frag or surf function of the shader, you would do
    float alpha = tex2D(_AlphaTex, animatedUV).r;
    (assuming you've already done your UV calculation), and now you've got your alpha value to use however you want. In the case of a transparent surface shader it's as simple as just assigning it to
    o.Alpha
    .
     
    Last edited: Nov 16, 2020
    bb8_1 and StridingDragon like this.
  3. StridingDragon

    StridingDragon

    Joined:
    Jan 16, 2013
    Posts:
    78
    Thank you so much!