Search Unity

  1. If you have experience with import & exporting custom (.unitypackage) packages, please help complete a survey (open until May 15, 2024).
    Dismiss Notice
  2. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice

Grab Pass Distortion & Emissive question

Discussion in 'Shaders' started by kornel-l-varga, Oct 7, 2015.

  1. kornel-l-varga

    kornel-l-varga

    Joined:
    Jan 18, 2013
    Posts:
    43
    Dear all,

    I am using in my Unity project a custom built shader ( not done by me, im really beginner in shader programming ) to distort the the view through a transparent object. It also has a Pass to TInt the object.
    The only thing I am missing is to make it Emissive or Additive so my Scion Bloom&Lensdirt plugin can pick it up.

    I have tried a lot of things but clearly I do not have the right understanding yet, and I would like to ask a pro what could be the best way.
    I am attaching the code that I got without my tryouts!

    Thanks in advance!

    Code (csharp):
    1.  
    2. // Per pixel bumped refraction.
    3. // Uses a normal map to distort the image behind, and
    4. // an additional texture to tint the color.
    5.  
    6. Shader "FX/Glass/Stained BumpDistort" {
    7. Properties {
    8.     _BumpAmt  ("Distortion", range (0,128)) = 10
    9.     _MainTex ("Tint Color (RGB)", 2D) = "white" {}
    10.     _BumpMap ("Normalmap", 2D) = "bump" {}
    11. }
    12.  
    13. Category {
    14.  
    15.     // We must be transparent, so other objects are drawn before this one.
    16.     Tags { "Queue"="Transparent" "RenderType"="Opaque" }
    17.  
    18.  
    19.     SubShader {
    20.  
    21.         // This pass grabs the screen behind the object into a texture.
    22.         // We can access the result in the next pass as _GrabTexture
    23.         GrabPass {
    24.             Name "BASE"
    25.             Tags { "LightMode" = "Always" }
    26.         }
    27.        
    28.         // Main pass: Take the texture grabbed above and use the bumpmap to perturb it
    29.         // on to the screen
    30.         Pass {
    31.         Ztest Always
    32.         //Blend SrcAlpha OneMinusSrcAlpha
    33.             Name "BASE"
    34.             Tags { "LightMode" = "Always" }
    35.            
    36.  
    37.  
    38. CGPROGRAM
    39. #pragma vertex vert
    40. #pragma fragment frag
    41. #pragma multi_compile_fog
    42. #include "UnityCG.cginc"
    43.          
    44.  
    45. struct appdata_t {
    46.     float4 vertex : POSITION;
    47.     float2 texcoord: TEXCOORD0;
    48. };
    49.  
    50. struct v2f {
    51.     float4 vertex : SV_POSITION;
    52.     float4 uvgrab : TEXCOORD0;
    53.     float2 uvbump : TEXCOORD1;
    54.     float2 uvmain : TEXCOORD2;
    55.     UNITY_FOG_COORDS(3)
    56. };
    57.  
    58. float _BumpAmt;
    59. float4 _BumpMap_ST;
    60. float4 _MainTex_ST;
    61.  
    62. v2f vert (appdata_t v)
    63. {
    64.     v2f o;
    65.     o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    66.     #if UNITY_UV_STARTS_AT_TOP
    67.     float scale = -1.0;
    68.     #else
    69.     float scale = 1.0;
    70.     #endif
    71.     o.uvgrab.xy = (float2(o.vertex.x, o.vertex.y*scale) + o.vertex.w) * 0.5;
    72.     o.uvgrab.zw = o.vertex.zw;
    73.     o.uvbump = TRANSFORM_TEX( v.texcoord, _BumpMap );
    74.     o.uvmain = TRANSFORM_TEX( v.texcoord, _MainTex );
    75.     UNITY_TRANSFER_FOG(o,o.vertex);
    76.     return o;
    77. }
    78.  
    79. sampler2D _GrabTexture;
    80. float4 _GrabTexture_TexelSize;
    81. sampler2D _BumpMap;
    82. sampler2D _MainTex;
    83.  
    84. half4 frag (v2f i) : SV_Target
    85. {
    86.     // calculate perturbed coordinates
    87.     half2 bump = UnpackNormal(tex2D( _BumpMap, i.uvbump )).rg; // we could optimize this by just reading the x & y without reconstructing the Z
    88.     float2 offset = bump * _BumpAmt * _GrabTexture_TexelSize.xy;
    89.     i.uvgrab.xy = offset * i.uvgrab.z + i.uvgrab.xy;
    90.    
    91.     half4 col = tex2Dproj( _GrabTexture, UNITY_PROJ_COORD(i.uvgrab));
    92.     half4 tint = tex2D(_MainTex, i.uvmain);
    93.     col *= tint;
    94.     UNITY_APPLY_FOG(i.fogCoord, col);
    95.     return col;
    96.    
    97. }
    98.  
    99. //void surf (Input IN, inout SurfaceOutput o) {
    100. //}
    101.  
    102. ENDCG
    103.         }
    104.     }
    105.  
    106.     // ------------------------------------------------------------------
    107.     // Fallback for older cards and Unity non-Pro
    108.  
    109.     SubShader {
    110.         Blend DstColor Zero
    111.         Ztest Always
    112.         Pass {
    113.             Name "BASE"
    114.             SetTexture [_MainTex] { combine texture }
    115.         }
    116.     }
    117. }
    118.  
    119. }
    120.  
    121.