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

i write a Transparent shader,but the other shader material could not display behind

Discussion in 'Shaders' started by mmitang, Jul 20, 2013.

  1. mmitang

    mmitang

    Joined:
    May 27, 2013
    Posts:
    11
    there is three shaders
    View attachment 60804

    this is the frist transparent shader
    Code (csharp):
    1.  
    2. Shader "Alpha/alphaTextureRim"{
    3.     Properties{
    4.         _Color ("MainColor",Color) = (1.0,1.0,1.0,1.0)
    5.         _RimColor("RimColor",Color) = (1.0,1.0,1.0,1.0)
    6.         _RimPower("RimPower",Range(-0.5,0.5)) = 0
    7.         _TextureBlend("textureBlebd",Range(0,1))  =0.2
    8.         _MainTex("MainTexture",2D) = "white"{}
    9.    
    10.     }
    11.     SubShader{
    12.         Pass{
    13.             Tags {"Queue" = "Transparent" }
    14.             Blend SrcAlpha OneMinusSrcAlpha
    15.             AlphaTest off
    16.             //Cull Off
    17.             //zWrite On
    18.             //ZTest LEqual
    19.             CGPROGRAM
    20.             #pragma vertex vert
    21.             #pragma fragment frag
    22.            
    23.             uniform float4 _Color;
    24.             uniform float4 _RimColor;
    25.             uniform float _RimPower;
    26.             uniform float _TextureBlend;
    27.             uniform sampler2D _MainTex;
    28.             uniform float4 _MainTex_ST;
    29.             //accept unity variable
    30.             //uniform float4 _LightColor0;
    31.            
    32.             struct vertexInput
    33.             {
    34.                 float4 texcoord:TEXCOORD0;
    35.                 float3 normal:NORMAL;
    36.                 float4 vertex:POSITION;
    37.            
    38.             };
    39.            
    40.            
    41.             struct vertexOutput
    42.             {
    43.                 float4 pos:SV_POSITION;
    44.                 float4 tex:TEXCOORD0;
    45.                 float4 posWorld:TEXCOORD1;
    46.                 float3 norDir:TEXCOORD2;
    47.                
    48.             };
    49.            
    50.            
    51.             vertexOutput vert(vertexInput i)
    52.             {
    53.                 vertexOutput o;
    54.                 o.pos = mul(UNITY_MATRIX_MVP,i.vertex);
    55.                 o.posWorld = mul(_Object2World,i.vertex);
    56.                 o.norDir = normalize(float3(mul(float4(i.normal,0.0),_World2Object).xyz));
    57.                 o.tex = i.texcoord;
    58.                 return o;
    59.             }
    60.            
    61.            
    62.             float4 frag(vertexOutput o):COLOR
    63.             {
    64.                 float3 normalDirection = o.norDir;
    65.                 float3 viewDirection = normalize(float3(_WorldSpaceCameraPos.xyz - o.posWorld .xyz));
    66.                
    67.                 float rim = 1- saturate(dot(viewDirection,normalDirection) + _RimPower);
    68.                  
    69.                 float3 mianColor =    lerp(_Color,_RimColor,rim) ;
    70.                  
    71.                 float4 tex = tex2D(_MainTex,_MainTex_ST.xy*o.tex.xy  + _MainTex_ST.zw*0.2);
    72.                  
    73.                 float3 finalColor = UNITY_LIGHTMODEL_AMBIENT.xyz  + tex + mianColor*_TextureBlend;
    74.                
    75.                
    76.                
    77.                 return float4(finalColor,rim)  ;
    78.             }
    79.            
    80.             ENDCG
    81.        
    82.        
    83.         }
    84.    
    85.    
    86.     }
    87.  
    88.  
    89.  
    90. }
    91.  
    this is the shader that can display behind the transparent
    Code (csharp):
    1.  
    2. Shader "testShader/vol3- SpecularFragment "{
    3.     Properties {
    4.         _Color("MainColor",Color) = (1.0,1.0,1.0,1.0)
    5.         _SpecColor("SpeColor",Color) = (1.0,1.0,1.0,1.0)
    6.         _Shininess("Shiness",Float) = 10.0
    7.        
    8.     }
    9.     SubShader{
    10.         Tags{"LightMode" = "ForwardBase"}
    11.             Pass{  
    12.             CGPROGRAM  
    13.             #pragma vertex vert
    14.             #pragma fragment frag
    15.             #include "UnityCG.cginc"
    16.            
    17.             //accept variable
    18.             uniform float4 _Color;
    19.             uniform float4 _SpecColor;
    20.             uniform float _Shininess;
    21.            
    22.             //define variable
    23.             uniform float4 _LightColor0;
    24.            
    25.             //struct
    26.            
    27.             struct vertexInput
    28.             {
    29.                 float4 vertex:POSITION;
    30.                 float3 normal:NORMAL;
    31.             };
    32.            
    33.             struct vertexOutput
    34.             {
    35.                 float4 pos:SV_POSITION;
    36.                 float4 posWorld:TEXCOORD0;
    37.                 float3 normalDir:TEXCOORD1;
    38.             };
    39.            
    40.            
    41.             vertexOutput vert (vertexInput i)
    42.             {
    43.                 vertexOutput o;
    44.                
    45.                 o.posWorld= mul(_Object2World,i.vertex);
    46.                
    47.                 o.normalDir = normalize(mul(float4(i.normal,0.0),_World2Object).xyz);
    48.                
    49.                 o.pos = mul(UNITY_MATRIX_MVP,i.vertex);
    50.                
    51.                 return o;
    52.             }
    53.            
    54.             float4 frag(vertexOutput o):COLOR{
    55.                  
    56.                
    57.                 float3 normalDirection  = o.normalDir;
    58.                 float3 LightDirection = normalize(_WorldSpaceLightPos0.xyz);
    59.                 float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - o.posWorld).xyz;
    60.                 float atten = 1.0;
    61.                
    62.                
    63.                 float3 diffectRle = atten*_LightColor0.xyz*max(0.0,dot(normalDirection,LightDirection))*_Color;
    64.                 float3 specRle = atten*_SpecColor.xyz*pow((max(0.0,dot(reflect(-LightDirection,normalDirection),viewDirection))),_Shininess)*max(0.0,dot(normalDirection,LightDirection));
    65.                 float3 finalCol = specRle+diffectRle + UNITY_LIGHTMODEL_AMBIENT;
    66.                
    67.                
    68.                 return float4(finalCol*_Color.rgb,1.0);
    69.             }
    70.            
    71.        
    72.             ENDCG
    73.         }
    74.     }
    75.  
    76. }
    77.  
    this is the shader that could not display behind the transparent ,why????

    Code (csharp):
    1.  
    2. Shader "testShader/vol4 - RimLight" {
    3.     Properties {
    4.         _Color("Color",Color) = (1.0,1.0,1.0,1.0)
    5.         _SpecColor("SpecColor",Color) = (1.0,1.0,1.0,1.0)
    6.         _Shininess("Shininess",float) = 10.0
    7.         _RimColor("RimColor",Color) = (1.0,1.0,1.0,1.0)
    8.         _RimPower("RimPower",Range(0.1,10.0)) = 5.0
    9.     }
    10.     SubShader {
    11.         Pass{
    12.             Tags { "LightMode" = "ForwardBase" }
    13.             CGPROGRAM
    14.             #pragma vertex vert
    15.             #pragma fragment frag
    16.             #include"UnityCG.cginc"
    17.            
    18.            
    19.             uniform float4 _Color;
    20.             uniform float4 _SpecColor;
    21.             uniform float4 _RimColor;
    22.             uniform float _Shininess;
    23.             uniform float _RimPower;
    24.            
    25.            
    26.             //translate the variable from unity to our CGRPOGRAM
    27.             uniform float4 _LightColor0;
    28.            
    29.            
    30.             struct vertexInput
    31.             {
    32.                 float4 vertex:POSITION;
    33.                 float3 normal:NORMAL;
    34.             };
    35.             struct vertexOutput
    36.             {
    37.                 float4 pos:SV_POSITION;
    38.                 float4 posWorld:TEXCOORD0;
    39.                 float3 normalDir:TEXCOORD1;
    40.             };
    41.            
    42.             vertexOutput vert(vertexInput i)
    43.             {
    44.                 vertexOutput o;
    45.                 o.normalDir = normalize(mul( float4(i.normal,0.0),_Object2World).xyz);
    46.                 o.posWorld = mul(_Object2World,i.vertex);
    47.                 o.pos = mul(UNITY_MATRIX_MVP,i.vertex);
    48.                 return o;
    49.                
    50.            
    51.             }
    52.            
    53.             float4 frag(vertexOutput o):COLOR  
    54.             {
    55.                 float3 normalDirection = o.normalDir;
    56.                 float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - o.posWorld.xyz);
    57.                 float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    58.                 float atten = 1.0;
    59.                
    60.                 float3 diffuceReflection = atten*_LightColor0.xyz*saturate(dot(normalize(normalDirection),lightDirection));
    61.                 float3 specReflection = atten*_LightColor0.xyz*pow(saturate(dot(reflect(-lightDirection,normalDirection),viewDirection)),_Shininess)*saturate(dot(normalize(normalDirection),lightDirection));
    62.            
    63.                 //float rim = dot(viewDirection,normalDirection);
    64.                 float rim = 1-saturate(dot(normalize(viewDirection),normalDirection));
    65.                 float3 rimLight = atten*_LightColor0.xyz*_RimColor*saturate(dot(normalDirection,lightDirection))*pow(rim,_RimPower);
    66.                
    67.                 float3 finalColor = rimLight + diffuceReflection +  specReflection + UNITY_LIGHTMODEL_AMBIENT;
    68.                
    69.                 return float4(finalColor  *_Color.xyz,1.0);
    70.            
    71.             }
    72.            
    73.            
    74.            
    75.             ENDCG
    76.        
    77.         }
    78.     }
    79.    
    80. }
    81.  
    82.  
     

    Attached Files:

  2. brianasu

    brianasu

    Joined:
    Mar 9, 2010
    Posts:
    369
    I think you need to set ZWrite Off and Tags {"Queue" = "Transparent" } for transparency.
     
  3. mmitang

    mmitang

    Joined:
    May 27, 2013
    Posts:
    11
    year,the key point is :"zWrite off"]
    thanks a lot
     
  4. mmitang

    mmitang

    Joined:
    May 27, 2013
    Posts:
    11
    see this :
    https://github.com/googlesamples/cardboard-unity/issues/113
    There was a bug related to a certain thread calling from C++ to C#. The thread watches for magnet and tilt events, but looked like it crashed only for tilt events. The workaround was to disable reporting tilt events, as a stopgap until we have a different architecture for event reporting. This is available in v0.6.

    And i fount that use il2cpp can avoid the crash!!