Search Unity

Shader and/or material disabled on bluid

Discussion in 'Shaders' started by LeRan, Apr 27, 2019.

  1. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    118
    Hi forum,

    So I had this little alpha mask shader that I liked very much to display a procedural map in-game. Here is the view from the editor.



    The problem is that, once the game is built, the exact same scene launched from the executable looks like this, as if the whole map image was disabled :



    I don't know what was lost upon build, and have no idea how to fix it... The shader was force-included in the graphic settings just in case, but to no avail.

    Just in case here is the script of the shader, it works perfectly fine from the editor.

    Code (CSharp):
    1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
    2.  
    3. // Unlit alpha-blended shader.
    4. // - no lighting
    5. // - no lightmap support
    6. // - no per-material color
    7.  
    8. Shader "Custom/AlphaMask (Ben Silvis perso)" {
    9. Properties {
    10.     _MainTex ("Base (RGB)", 2D) = "white" {}
    11.     _AlphaTex ("Alpha mask (R)", 2D) = "white" {}
    12.     _Ratio("Ratio", Range(0,1)) = 1
    13. }
    14.  
    15. SubShader {
    16.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    17.     LOD 100
    18.  
    19.     ZWrite Off
    20.     Blend SrcAlpha OneMinusSrcAlpha
    21.  
    22.     Pass {
    23.         CGPROGRAM
    24.             #pragma vertex vert
    25.             #pragma fragment frag
    26.        
    27.             #include "UnityCG.cginc"
    28.  
    29.             struct appdata_t {
    30.                 float4 vertex : POSITION;
    31.                 float2 texcoord : TEXCOORD0;
    32.             };
    33.  
    34.             struct v2f {
    35.                 float4 vertex : SV_POSITION;
    36.                 half2 texcoord : TEXCOORD0;
    37.             };
    38.  
    39.             sampler2D _MainTex;
    40.             sampler2D _AlphaTex;
    41.        
    42.             float4 _MainTex_ST;
    43.             float _Ratio;
    44.        
    45.             v2f vert (appdata_t v)
    46.             {
    47.                 v2f o;
    48.                 o.vertex = UnityObjectToClipPos(v.vertex);
    49.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    50.                 return o;
    51.             }
    52.        
    53.             fixed4 frag (v2f i) : SV_Target
    54.             {
    55.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    56.                 fixed4 col2 = tex2D(_AlphaTex, i.texcoord);
    57.            
    58.                 return fixed4(col.r, col.g, col.b, col.a*(1-_Ratio)+ col.a*col2.a*_Ratio);
    59.             }
    60.         ENDCG
    61.     }
    62. }
    63.  
    64. }
    65.  
    66. //https://bensilvis.com/unity3d-unlit-alpha-mask-shader/
     
    Last edited: Apr 27, 2019
  2. LeRan

    LeRan

    Joined:
    Nov 24, 2015
    Posts:
    118
    All right, I found what was missing, it was just :
     ZTest [unity_GUIZTestMode]

    And probably later on :
     #include "UnityUI.cginc"


    So, for the sake of science, here is the refined full shader, enjoy.

    Code (CSharp):
    1. Shader "Custom/AlphaMask (Ben Silvis et Ran)" {
    2. Properties {
    3.     [PerRendererData] _MainTex ("Base (RGB)", 2D) = "white" {}
    4.     _AlphaTex ("Alpha mask", 2D) = "white" {}
    5.     _Ratio("Ratio", Range(0,1)) = 1
    6. }
    7.  
    8. SubShader {
    9.     Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    10.     LOD 100
    11.  
    12.     ZWrite Off
    13.     ZTest [unity_GUIZTestMode] //imitation shader natif
    14.     Blend SrcAlpha OneMinusSrcAlpha
    15.  
    16.     Pass {
    17.         CGPROGRAM
    18.             #pragma vertex vert
    19.             #pragma fragment frag
    20.          
    21.             #include "UnityCG.cginc"
    22.             #include "UnityUI.cginc" //imitation shader natif
    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.             sampler2D _AlphaTex;
    36.          
    37.             float4 _MainTex_ST;
    38.             float _Ratio;
    39.          
    40.             v2f vert (appdata_t v)
    41.             {
    42.                 v2f o;
    43.                 o.vertex = UnityObjectToClipPos(v.vertex);
    44.                 o.texcoord = TRANSFORM_TEX(v.texcoord, _MainTex);
    45.                 return o;
    46.             }
    47.          
    48.             fixed4 frag (v2f i) : SV_Target
    49.             {
    50.                 fixed4 col = tex2D(_MainTex, i.texcoord);
    51.                 fixed4 col2 = tex2D(_AlphaTex, i.texcoord);
    52.              
    53.                 return fixed4(col.r, col.g, col.b, col.a*(1-_Ratio)+ col.a*col2.a*_Ratio);
    54.             }
    55.         ENDCG
    56.     }
    57. }
    58.  
    59. }
     
    justingarrigus likes this.