Search Unity

Rotating a texture behind a mask

Discussion in 'Shaders' started by saswilson, Sep 12, 2014.

  1. saswilson

    saswilson

    Joined:
    Jun 6, 2013
    Posts:
    44
    Anyone know of a shader which takes in 2 textures, one that can use alpha and the other that can be rotated? So you end up with a texture spinning behind a masked texture. Thanks for any help
     
  2. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    I dont have time to write it now, it's a pretty enlightening thing to program :)

    to rotate X and Y of the shader, you have to change the TEXCOORD U , V, values that are used to read the texture using

    this will rotate a texture:

    http://forum.unity3d.com/threads/rotation-of-texture-uvs-directly-from-a-shader.150482/

    then take the second texture, read its rgba values as well just straight
    half4 d = tex2D (_MainTex, IN.uv_MainTex);

    and at the end of the shader do

    o.albedo = c.r*d.a , c.g*d.a , c.b*d.a;

    its a float3 rgb value...

    multing it means that when alpha pixel is zero, rotating tex underneat it is zero.
    you dont have to use an alpha on second texture you can use black and white grey ones, and use r,g,or b value. can be easier sometimes.

    i dont have a 2 texture blend shader at hand that averages the pixel values of rgb of 2 textures at hand in surf function, right now. you can find some.
     
  3. drudiverse

    drudiverse

    Joined:
    May 16, 2013
    Posts:
    218
    ok sry here it is, there maybe a tiny syntax error where i added the alphatex, i didnt run it, and teh UV_alphatex. it's you have the 2 shaders so you can scramble it from this probably


    Code (csharp):
    1.  
    2. [LIST=1]
    3. [*]
    4.  
    5. [*]Shader "Custom/RotateUVs" {
    6. [*]    Properties {
    7. [*]        _MainTex ("Base (RGB)", 2D) = "white" {}
    8. [*]        _alphaTex ("Base (RGB)", 2D) = "white" {}
    9.  
    10. [*]        _RotationSpeed ("Rotation Speed", Float) = 2.0
    11. [*]   }
    12. [*]    SubShader {
    13. [*]        Tags { "RenderType"="Opaque" }
    14. [*]        LOD 200
    15. [*]      
    16. [*]        CGPROGRAM
    17. [*]       #pragma surface surf Lambert vertex:vert
    18. [*]
    19.  
    20. [*]        sampler2D _MainTex,_alphaTex ;
    21. [*]
    22.  
    23. [*]       struct Input {
    24. [*]            float2 uv_MainTex;
    25. [*]            float2 uv_alphaTex ;
    26.  
    27. [*]       };
    28. [*]
    29.  
    30. [*]       float _RotationSpeed;
    31. [*]       void vert (inout appdata_full v) {
    32. [*]           float sinX = sin ( _RotationSpeed * _Time );
    33. [*]           float cosX = cos ( _RotationSpeed * _Time );
    34. [*]           float sinY = sin ( _RotationSpeed * _Time );
    35. [*]            float2x2 rotationMatrix = float2x2( cosX, -sinX, sinY, cosX);
    36. [*]            v.texcoord.xy = mul ( v.texcoord.xy, rotationMatrix );
    37. [*]       }
    38. [*]
    39.  
    40. [*]       void surf (Input IN, inout SurfaceOutput o) {
    41. [*]            half4 c = tex2D (_MainTex, IN.uv_MainTex);
    42. [*]           half4 at = tex2D (_alphaTex , IN.uv_alphaTex );
    43.  
    44. [*]            o.Albedo = c.rgb * at.a;
    45. [*]            o.Alpha = c.a;
    46. [*]       }
    47. [*]        ENDCG
    48. [*]   }
    49. [*]    FallBack "Diffuse"
    50. [*]}
    51. [*]
    52. [/LIST]
    53.  
     
  4. saswilson

    saswilson

    Joined:
    Jun 6, 2013
    Posts:
    44
    Thanks a lot for this ill have a look at it!