Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Rotating Unlit/TransparentCutout

Discussion in 'Shaders' started by WrexialMT, Mar 5, 2014.

  1. WrexialMT

    WrexialMT

    Joined:
    Mar 28, 2013
    Posts:
    7
    Hi,

    I am trying to merge the code from https://docs.unity3d.com/Documentation/ScriptReference/Material.SetMatrix.html specifically the part of the shader where the rotation matrix is being multiplied to the shader and the built-in unity Unlit/TransparentCutout. Unfortunately I do not have any experience in dealing with Shaders.

    My best attempt (I think) is the following Full Code Below

    Code (csharp):
    1.  
    2. SubShader {
    3.     Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    4.     LOD 100
    5.  
    6.     Lighting Off
    7.  
    8.     Pass {  
    9.         SetTexture [_MainTex] {
    10.             matrix [_Rotation]
    11.             combine texture * primary double
    12.         }
    13.        
    14.     Program "vp" {
    15. // Vertex combos: 1
    16. //   opengl - ALU: 5 to 5
    17. //   d3d9 - ALU: 5 to 5
    18. //   d3d11 - ALU: 5 to 5, TEX: 0 to 0, FLOW: 1 to 1
    19. //   d3d11_9x - ALU: 5 to 5, TEX: 0 to 0, FLOW: 1 to 1
    20. SubProgram "opengl " {
    21. Keywords { }
    22. Bind "vertex" Vertex
    23. Bind "texcoord" TexCoord0
    24. Vector 5 [_MainTex_ST]
    25. "!!ARBvp1.0
    26. # 5 ALU
    27.  
    28. ...
    Modified Shader - View attachment $MarkerShader.shader
    Original Shader - View attachment $Compiled-Unlit-AlphaTest.shader

    If anybody knowledgeable would be able to assist me in this I'd be glad.

    Thanks :)
     
  2. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    The link you have given is a script, which basically creates a material/shader and then attached this to the object it's attached too. It then sets the matrix of that shader in the update of the script.

    Now what you could do, is use that shader, and then have a script which sets the _Rotation variable for the shader which can be used with this line:
    Code (csharp):
    1.  
    2. matrix [_Rotation]
    3.  
     
  3. WrexialMT

    WrexialMT

    Joined:
    Mar 28, 2013
    Posts:
    7
    Thanks Duney for replying, but I think I wasn't clear on my problem.

    Basically the end result should be a Transparent Cutout Shader which is able to be Rotated at runtime through the code. Now I have found that script which creates a shader and the controls, but I would like to have the Shader as a prefab externally and not created at the "Start()" of the script. In addition, I would like for my new Shader to have the properties from the Original Shader ( Compiled-Unlit-AlphaTest.shader) in addition to the new Rotation support.

    Sorry if im not being clear.

    Thanks :)
     
  4. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    Here is the code for Unlit-AlphaTest.shader, you can always modify that if you need too and then look-up and attach that to your material at run-time:

    Code (csharp):
    1. // Unlit alpha-cutout shader.
    2. // - no lighting
    3. // - no lightmap support
    4. // - no per-material color
    5.  
    6. Shader "Unlit/Transparent Cutout" {
    7. Properties {
    8.     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    9.     _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    10. }
    11. SubShader {
    12.     Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    13.     LOD 100
    14.  
    15.     Lighting Off
    16.  
    17.     Pass {  
    18.         CGPROGRAM
    19.             #pragma vertex vert
    20.             #pragma fragment frag
    21.            
    22.             #include "UnityCG.cginc"
    23.  
    24.             struct appdata_t {
    25.                 float4 vertex : POSITION;
    26.                 float2 texcoord : TEXCOORD0;
    27.             };
    28.  
    29.             struct v2f {
    30.                 float4 vertex : SV_POSITION;
    31.                 half2 texcoord : TEXCOORD0;
    32.             };
    33.  
    34.             sampler2D _MainTex;
    35.             float4 _MainTex_ST;
    36.             fixed _Cutoff;
    37.  
    38.             v2f vert (appdata_t v)
    39.             {
    40.                 v2f o;
    41.                 o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    42.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    43.                 return o;
    44.             }
    45.            
    46.             fixed4 frag (v2f i) : COLOR
    47.             {
    48.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    49.                 clip(col.a - _Cutoff);
    50.                 return col;
    51.             }
    52.         ENDCG
    53.     }
    54. }
    55.  
    56. SubShader {
    57.     Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    58.     LOD 100
    59.    
    60.     Pass {
    61.         Lighting Off
    62.         Alphatest Greater [_Cutoff]
    63.         SetTexture [_MainTex] { combine texture }
    64.     }
    65. }
    66. }
    67.  
    I'm not sure what you mean by rotating the shader exactly though? You would have to look at manipulating the UV's of the textures to make the material appear to be moving/rotating I believe, a shader can't 'physically' rotate an object to my knowledge.
     
  5. WrexialMT

    WrexialMT

    Joined:
    Mar 28, 2013
    Posts:
    7
    Thanks Duney for the Code, with that I made a large improvement since I only had the Compiled version of the shader. I think I'm not making myself clear on the end result I want so let me use a couple of images this time and their appropriate shaders attached.

    I have

    Transparency; From the code you supplied to me so far;
    $Transparent.png
    View attachment $Transparent.shader

    and

    Rotation; From some code I found Online which works as intended for that scenario
    $Rotation.png
    View attachment $Rotation.shader

    Now, what I want is a merge between those two shaders to create a Transparent Rotating Material, currently as it stands I have the following:

    Code (csharp):
    1. // Unlit alpha-cutout shader.
    2. // - no lighting
    3. // - no lightmap support
    4. // - no per-material color
    5.  
    6. Shader "Custom/Combined" {
    7.     Properties {
    8.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    9.         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    10.         _RotationSpeed ("Rotation Speed", Float) = 2.0
    11.         _RotationDegrees ("Rotation Degrees", Float) = 0.0
    12.     }
    13.    
    14.     SubShader {
    15.         Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    16.         LOD 100
    17.         Lighting Off
    18.         Pass {  
    19.             CGPROGRAM
    20.                 #pragma surface surf Lambert vertex:vert fragment:frag
    21.                 #include "UnityCG.cginc"
    22.                
    23.                 sampler2D _MainTex;
    24.                 float4 _MainTex_ST;
    25.                 fixed _Cutoff;
    26.                 float _RotationDegrees;
    27.                
    28.                struct appdata_t {
    29.                     float4 vertex : POSITION;
    30.                     float2 texcoord : TEXCOORD0;
    31.                 };
    32.                
    33.                 struct Input {
    34.                     float2 uv_MainTex;
    35.                 };
    36.  
    37.                 struct v2f {
    38.                     float4 vertex : SV_POSITION;
    39.                     half2 texcoord : TEXCOORD0;
    40.                 };
    41.  
    42.                 v2f vert (inout appdata_full v)
    43.                 {
    44.                     v2f o;
    45.                     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    46.                     o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    47.                    
    48.                     o.texcoord.xy -=0.5;
    49.                     float s = sin ( _RotationDegrees);
    50.                     float c = cos ( _RotationDegrees);
    51.            
    52.                     float2x2 rotationMatrix = float2x2( c, -s, s, c);
    53.                     rotationMatrix *=0.5;
    54.                     rotationMatrix +=0.5;
    55.                     rotationMatrix = rotationMatrix * 2-1;
    56.                     o.texcoord.xy = mul ( o.texcoord.xy, rotationMatrix );
    57.                     o.texcoord.xy += 0.5;
    58.                     return o;
    59.                 }
    60.  
    61.                 fixed4 frag (v2f i) : COLOR
    62.                 {
    63.                     fixed4 col = tex2D(_MainTex, i.texcoord);
    64.                     clip(col.a - _Cutoff);
    65.                     return col;
    66.                 }
    67.                
    68.                 void surf (Input IN, inout SurfaceOutput o) {
    69.                     half4 c = tex2D (_MainTex, IN.uv_MainTex);
    70.                     o.Albedo = c.rgb;
    71.                     o.Alpha = c.a;
    72.                 }
    73.  
    74.             ENDCG
    75.         }
    76.     }
    77.  
    78.     SubShader {
    79.         Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    80.         LOD 100
    81.  
    82.         Pass {
    83.             Lighting Off
    84.             Alphatest Greater [_Cutoff]
    85.             SetTexture [_MainTex] {
    86.                 combine texture
    87.             }
    88.         }
    89.     }
    90. }
    View attachment $Combined.shader


    But as it stands I have some errors which are:

    Shader error in 'Custom/Combined': Program 'frag_surf', declaration of "_MainTex_ST" conflicts with previous declaration at (25) at line 94

    and similarly

    Shader error in 'Custom/Combined': Program 'vert_surf', declaration of "_MainTex_ST" conflicts with previous declaration at (25) at line 94
     
  6. Duney

    Duney

    Joined:
    Aug 19, 2013
    Posts:
    170
    Not sure why the combined shader would be throwing that error exactly, but seen as you aren't actually using the _MainTex_ST, try to just comment it out and see if the shader will compile.
     
  7. WrexialMT

    WrexialMT

    Joined:
    Mar 28, 2013
    Posts:
    7
    I just tried that, and now its throwing undefined variable "_MainTex_ST" at line 46


    EDIT: Fixed most issues, except that now the shader Ignores completely the Tiling and Offsets

    Code (csharp):
    1.  
    2. Shader "Custom/Combined" {
    3.     Properties {
    4.         _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
    5.         _Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
    6.         _RotationSpeed ("Rotation Speed", Float) = 2.0
    7.         _RotationDegrees ("Rotation Degrees", Float) = 0.0
    8.     }
    9.  
    10.     SubShader {
    11.         Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    12.         LOD 100
    13.         Lighting Off
    14.         Pass {  
    15.             CGPROGRAM
    16.                 #pragma vertex vert
    17.                 #pragma fragment frag
    18.                 #include "UnityCG.cginc"
    19.                
    20.                 sampler2D _MainTex;
    21.                 float4 _MainTex_ST;
    22.                 fixed _Cutoff;
    23.                 float _RotationDegrees;
    24.                
    25.                 struct Input {
    26.                     float2 uv_MainTex;
    27.                 };
    28.                
    29.                 struct vertexOutput
    30.                 {
    31.                     float4 pos : SV_POSITION;
    32.                     float4 tex : TEXCOORD0;
    33.                 };
    34.                
    35.                 vertexOutput vert (appdata_full input)
    36.                 {
    37.                     vertexOutput output;
    38.  
    39.                     output.tex = input.texcoord;
    40.                     output.pos = mul(UNITY_MATRIX_MVP, input.vertex);
    41.                    
    42.                     output.tex.xy -= (0.5);
    43.                     float s = sin (_RotationDegrees);
    44.                     float c = cos (_RotationDegrees);
    45.  
    46.                     float2x2 rotationMatrix = float2x2( c, -s, s, c);
    47.                     rotationMatrix *=0.5;
    48.                     rotationMatrix +=0.5;
    49.                     rotationMatrix = rotationMatrix * 2-1;
    50.                     output.tex.xy = mul (output.tex.xy, rotationMatrix);
    51.                     output.tex.xy += (0.5);
    52.                     return output;
    53.                 }
    54.  
    55.                 float4 frag(vertexOutput input) : COLOR
    56.                 {
    57.                     float4 textureColor = tex2D(_MainTex, float2(input.tex));  
    58.                     if (textureColor.a < _Cutoff) // alpha value less than user-specified threshold?
    59.                     {
    60.                        discard; // yes: discard this fragment
    61.                     }
    62.                     return textureColor;
    63.                 }
    64.             ENDCG
    65.         }
    66.     }
    67.    
    68.     SubShader {
    69.         Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
    70.         LOD 100
    71.        
    72.         Pass {
    73.             Lighting Off
    74.             Alphatest Greater [_Cutoff]
    75.             SetTexture [_MainTex] {
    76.                 combine texture
    77.             }
    78.         }
    79.     }
    80. }
    81.  
     
    Last edited: Mar 6, 2014