Search Unity

Unity 5 standard shader with fresnel transparency

Discussion in 'Shaders' started by jhm, Jul 20, 2016.

  1. jhm

    jhm

    Joined:
    Aug 5, 2015
    Posts:
    3
    Hi,
    i´m new to all the shader stuff in unity. I usually work with non-realtime-renderers and their material systems - so i know the look that i want, but not really how to get there whith unity.
    It´s a ghost/xray shader, but not only with a simple color in the visible areas, but with a full material.
    Here´s an example of a fresnel-transparency-effect:



    Thanks for any helps, tips & suggestions!
     
    Last edited: Jul 20, 2016
  2. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,735
    You'd have to write your own shader for this. It's fairly easy and I think there are a couple of shaders that do this that are already available (on the forum or wikis). Did you search for it?
     
  3. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    simple

    Code (CSharp):
    1.  
    2. Shader "CustomShader/Rim Light" {
    3.  
    4.     Properties {
    5.         _Color ("Main Color", Color) = (1,1,1,1)
    6.         _RimColor ("Rim Color", Color) = (1, 1, 1, 1)
    7.         _MainTex ("Base (RGB)", 2D) = "white" {}
    8.         _RimWidth ("RimWidth", Range(0,20.0)) = 0.7
    9.     }
    10.    
    11.  
    12.  
    13.     SubShader {
    14.    
    15.         Tags { "RenderType" = "Overlay" "IgnoreProjector"="True"  "Queue" = "Transparent"}
    16.         Cull back
    17.         Lighting Off
    18.         ZWrite Off
    19.         Fog { Mode Off }
    20.         Blend one One
    21.      
    22.         Pass {      
    23.          
    24.             CGPROGRAM
    25.              
    26.                 #pragma vertex vert
    27.                 #pragma fragment frag
    28.                 #include "UnityCG.cginc"
    29.                 struct appdata {
    30.                     float4 vertex : POSITION;
    31.                     float3 normal : NORMAL;
    32.                     float2 texcoord : TEXCOORD0;
    33.                 };
    34.              
    35.                 struct v2f {
    36.                     float4 pos : SV_POSITION;
    37.                     float2 uv : TEXCOORD0;
    38.                     float3 color : COLOR;
    39.                 };
    40.              
    41.                 uniform float4 _MainTex_ST;
    42.                 uniform float4 _RimColor;
    43.                 half _RimWidth;
    44.              
    45.                 v2f vert (appdata_base v) {
    46.                     v2f o;
    47.                     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    48.                  
    49.                     float3 viewDir = normalize(ObjSpaceViewDir(v.vertex));
    50.                     float dotProduct = 1 - dot(v.normal, viewDir);
    51.                     float rimWidth = _RimWidth;
    52.                     o.color = smoothstep(1 - rimWidth, 1.0, dotProduct);
    53.                  
    54.                     o.color *= _RimColor;
    55.                  
    56.                     o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    57.                  
    58.                     return o;
    59.                 }
    60.              
    61.                 uniform sampler2D _MainTex;
    62.                 uniform float4 _Color;
    63.              
    64.                 float4 frag(v2f i) : COLOR {
    65.                     float4 texcol = tex2D(_MainTex, i.uv);
    66.                     texcol *= _Color;
    67.                     texcol.rgb += i.color;
    68.                     return texcol;
    69.                 }
    70.              
    71.             ENDCG
    72.         }
    73.     }
    74. }
    75.  
     
    LaurieAnnis likes this.
  4. jhm

    jhm

    Joined:
    Aug 5, 2015
    Posts:
    3
    Thank You!

    @AcidArrow - till now found only xray/ghost shaders without lighting (asset store, froum), maybe i didn´t use the matching keywords...

    @Ironmax - that´s a huge step in the right direction, thank you! - i´m just digging into shader-programming, so i hope i can extend your code for my needs
     
  5. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    your welcome. I am sure you can, its very simple and clean :) good luck

    ps. You can add a surface render to this shader if you want light to reflect the surface. To work with transparent with surface you need this :


    Code (CSharp):
    1.                 CGPROGRAM
    2.                 #pragma surface surf SimpleSpecular alpha:blend
    3.  
    4. void surf(Input IN, inout SurfaceOutput o)
    5.  
    Then in your output you will be able to do this o.Alpha = 0.5; // 50% transparent

    You can do the rim effect in the surface output to instead of in a vertex pass.

    Note! Vertex pass does not receive light and is cheap on the GPU because it works similar to
    how the GPU handles instructions.

    Here is example:

    Code (CSharp):
    1.                 half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
    2.                 float4 Tex2D1 = tex2D(_EmissionTex, (IN.uv_EmissionTex.xyxy).xy);
    3.                 float4 Multiply0 = Tex2D1 * _EmissionColor;
    4.                 float4 Multiply2 = Multiply0 * _EmissionColor.a;
    5.  
    6.                 if (_RimPower > 0)
    7.                 {
    8.                     o.Emission = _EmissionColor.a * _EmissionColor.rgb * pow(rim, _RimPower);
    9.                 }
     
    Last edited: Jul 20, 2016
    LaurieAnnis likes this.
  6. jhm

    jhm

    Joined:
    Aug 5, 2015
    Posts:
    3
    Thank you again!
    I got it working somehow, (at least i got no more error messages and a non-pink output) but it´s acting strange:
    it´s flickering on some viewing angles, fresnel transparency is not working while some objects with other materiels are visible through the shader (even when alpha is set to opaque) and the sliding the RimPower over 0 results in fancy colors.

    Code (CSharp):
    1. Shader "Custom/Standard-FresnelTransparency" {
    2.     Properties {
    3.         _Color ("Color", Color) = (1,1,1,1)
    4.         _MainTex ("Albedo (RGB)", 2D) = "white" {}
    5.         _Glossiness ("Smoothness", Range(0,1)) = 0.5
    6.         _Metallic ("Metallic", Range(0,1)) = 0.0
    7.         _RimPower ("RimPower", Range(0,1)) = 0.0
    8.     }
    9.     SubShader {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 200
    12.    
    13.         CGPROGRAM
    14.         // Physically based Standard lighting model, and enable shadows on all light types
    15.         #pragma surface surf Standard fullforwardshadows alpha:blend
    16.  
    17.         // Use shader model 3.0 target, to get nicer looking lighting
    18.         #pragma target 3.0
    19.  
    20.         sampler2D _MainTex;
    21.  
    22.         struct Input {
    23.             float2 uv_MainTex;
    24.             float3 viewDir;
    25.         };
    26.  
    27.         half _Glossiness;
    28.         half _Metallic;
    29.         fixed4 _Color;
    30.         half _RimPower;
    31.  
    32.         void surf(Input IN, inout SurfaceOutputStandard o) {
    33.  
    34.             // Albedo comes from a texture tinted by color
    35.         fixed4 c = tex2D (_MainTex, IN.uv_MainTex) * _Color;
    36.         o.Albedo = c.rgb;
    37.         // Metallic and smoothness come from slider variables
    38.         o.Metallic = _Metallic;
    39.         o.Smoothness = _Glossiness;
    40.  
    41.  
    42.  
    43.         half rim = 1.0 - saturate(dot(normalize(IN.viewDir), o.Normal));
    44.                 float4 Tex2D1 = tex2D(_MainTex, (IN.uv_MainTex.xyxy).xy);
    45.                 float4 Multiply0 = Tex2D1 * _Color;
    46.                 float4 Multiply2 = Multiply0 * _Color.a;
    47.                 if (_RimPower > 0)
    48.                 {
    49.                     o.Emission = _Color.a * _Color.rgb * pow(rim, _RimPower);
    50.                 }
    51.  
    52.          o.Alpha = 0.5; // 50% transparent
    53.  
    54.      
    55.          }
    56.    
    57.         ENDCG
    58.     }
    59.     FallBack "Diffuse"
    60. }
    61.  
     
    Last edited: Jul 22, 2016
    Pn-io likes this.
  7. Ironmax

    Ironmax

    Joined:
    May 12, 2015
    Posts:
    890
    Good its working for you :) It shouldn't flicker, that must be some other reason. Try with this adjustments:
    Code (CSharp):
    1. Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
    2.  
    3. // and
    4.  
    5. #pragma surface surf Standard alpha:blend



    You can control the transparent by the color:
    Code (CSharp):
    1.  o.Alpha = _Color.a; // The transparent value of the color slidder a