Search Unity

How to implement custom post effects in 2017.4?

Discussion in 'Image Effects' started by SoftwareGeezers, Apr 16, 2018.

  1. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    I've a custom shadow effect that worked in 5.6. I've upgraded to 2017.4 and I get a shader error:

    Assertion failed: Failed to create DX11 vertex declaration; something wrong with vertex shader input data? (hr=80070057)

    I've traced the problem to this script of mine -
    Code (CSharp):
    1. Shader "SG/Stretch Shadows" {
    2.     Properties {
    3.         _MainTex ("Light texture", 2D) = "white" {}
    4. //        _SunPos ("Sun pos and scale", vector) = (0,0,0,0)
    5.     }
    6.     SubShader {
    7.     //    Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
    8.  
    9.         LOD 100
    10.         Blend One Zero
    11.         Cull Off
    12.         ZWrite Off
    13.         Lighting Off
    14.  
    15.         Pass {    // 0 Blacken source
    16.             CGPROGRAM
    17.                 #pragma target 3.0
    18.            
    19.                 #include "UnityCG.cginc"
    20.                 #pragma vertex processVerts
    21.                 #pragma fragment drawFrag
    22.  
    23.                 #pragma glsl_no_auto_normalization
    24.  
    25.                 struct vertdata {
    26.                     float2 uv : TEXCOORD0;
    27.                     float2 uvScaled : TEXCOORD1;
    28.                     float4 vertex : POSITION;
    29.                 };
    30.  
    31.                 struct frag_v2f {
    32.                     float4 vertex : SV_POSITION;
    33.                     half2 uv : TEXCOORD0;
    34.                 };
    35.  
    36.                 uniform sampler2D _MainTex;
    37.  
    38.                 frag_v2f processVerts (vertdata v){
    39.                     frag_v2f o;
    40.                     o.vertex = UnityObjectToClipPos(v.vertex);
    41.                      o.uv = v.uv;
    42.                     return o;
    43.                 }
    44.  
    45.                 half4 drawFrag (frag_v2f i) : SV_Target {
    46.                     fixed4 shadow = tex2D(_MainTex, i.uv);
    47.                     half intensity = max(shadow.r, max(shadow.g, shadow.b));
    48.                     intensity = step(intensity,0.1);
    49.                     return 1-intensity;
    50.                 }
    51.            
    52.             ENDCG
    53.         }
    54.  
    55.         Pass {    // 1 Stretch
    56.             CGPROGRAM
    57.                 #pragma target 3.0
    58.            
    59.                 #include "UnityCG.cginc"
    60.                 #pragma vertex processVerts
    61.                 #pragma fragment drawFrag
    62.  
    63.                 #pragma glsl_no_auto_normalization
    64.  
    65.                 struct vertdata {
    66.                     float2 uv : TEXCOORD0;
    67.                     float2 uvScaled : TEXCOORD1;
    68.                     float4 vertex : SV_POSITION;
    69.                 };
    70.  
    71.                 struct frag_v2f {
    72.                     float4 vertex : SV_POSITION;
    73.                     half2 basePos : TEXCOORD0;
    74.                     half2 zoomPos : TEXCOORD1;
    75.                 };
    76.            
    77.                 uniform sampler2D _ObstacleTex;
    78.                 uniform sampler2D _MainTex;
    79.                 half4 _SunPos;
    80.                 #ifdef UNITY_HALF_TEXEL_OFFSET
    81.                 #endif
    82.  
    83.                 frag_v2f processVerts (vertdata v){
    84.                     frag_v2f o;
    85.                     o.vertex = UnityObjectToClipPos(v.vertex);
    86.                      o.basePos = v.uv;
    87.                     o.zoomPos = v.uv * _SunPos.zw + (_SunPos.xy -_SunPos.zw*0.5);
    88.                     return o;
    89.                 }
    90.  
    91.                 half4 drawFrag (frag_v2f i) : SV_Target {
    92.                     half2 basePos = i.basePos;
    93.                     half2 zoomPos = i.zoomPos;
    94.                     half4 tex = tex2D(_MainTex, i.basePos);
    95.  
    96.                     half sub = 1.0/60;
    97.                     half len = length((basePos - zoomPos));
    98.                        
    99.                     half4 col = tex*tex.a;
    100.                     col = half4(0,0,0,0);
    101.                     half pos = 1;
    102.                     half power = 0.5;
    103.                
    104.                     for(int i = 0; i < 60; i++){
    105.                         pos -= sub;
    106.                         half4 obstacle = tex2D(_MainTex, lerp(zoomPos, basePos, pos));
    107.                         obstacle *= pow(pos, power);
    108.                         col = max(col, obstacle);
    109.                     }
    110.  
    111.                     return 1-col;//half4(half3((basePos - zoomPos).x*20), 1);//tex2D(_ObstacleTex, basePos);
    112.                 }
    113.            
    114.             ENDCG
    115.         }
    116.  
    117.         Pass {    // 2 Blacken alpha
    118.             CGPROGRAM
    119.                 #pragma target 3.0
    120.            
    121.                 #include "UnityCG.cginc"
    122.                 #pragma vertex processVerts
    123.                 #pragma fragment drawFrag
    124.  
    125.                 #pragma glsl_no_auto_normalization
    126.  
    127.                 struct vertdata {
    128.                     float2 uv : TEXCOORD0;
    129.                     float2 uvScaled : TEXCOORD1;
    130.                     float4 vertex : SV_POSITION;
    131.                 };
    132.  
    133.                 struct frag_v2f {
    134.                     float4 vertex : SV_POSITION;
    135.                     half2 uv : TEXCOORD0;
    136.                 };
    137.  
    138.                 uniform sampler2D _MainTex;
    139.  
    140.                 frag_v2f processVerts (vertdata v){
    141.                     frag_v2f o;
    142.                     o.vertex = UnityObjectToClipPos(v.vertex);
    143.                      o.uv = v.uv;
    144.                     return o;
    145.                 }
    146.  
    147.                 half4 drawFrag (frag_v2f i) : SV_Target {
    148.                     fixed4 shadow = tex2D(_MainTex, i.uv);
    149.                     half4 intensity = half4(0,0.2,0.2,shadow.a);
    150.                     intensity = lerp(shadow, intensity, shadow.a);
    151.  
    152.                     return intensity;
    153.                 }
    154.            
    155.             ENDCG
    156.         }
    157.  
    158.         Pass {    // 3 Draw fully zoomed shadow and half zoomed shadow
    159.             // Each pass duplicates previous pass, doubling drawn shadows
    160.             CGPROGRAM
    161.                 #pragma target 3.0
    162.            
    163.                 #include "UnityCG.cginc"
    164.                 #pragma vertex processVerts
    165.                 #pragma fragment drawFrag
    166.  
    167.                 #pragma glsl_no_auto_normalization
    168.  
    169.                 struct vertdata {
    170.                     float2 uv : TEXCOORD0;
    171.                     float2 uvScaled : TEXCOORD1;
    172.                     float4 vertex : SV_POSITION;
    173.                 };
    174.  
    175.                 struct frag_v2f {
    176.                     float4 vertex : SV_POSITION;
    177.                     half2 basePos : TEXCOORD0;
    178.                     half2 zoomPos : TEXCOORD1;
    179.                 };
    180.            
    181.                 uniform sampler2D _ObstacleTex;
    182.                 uniform sampler2D _MainTex;
    183.                 half4 _SunPos;
    184.                 half _Offset;                                            // frational lerp value. Decrease by powers of two each pass
    185.                 #ifdef UNITY_HALF_TEXEL_OFFSET
    186.                 #endif
    187.  
    188.                 frag_v2f processVerts (vertdata v){
    189.                     frag_v2f o;
    190.                     o.vertex = UnityObjectToClipPos(v.vertex);
    191.                      o.basePos = v.uv;
    192.                     o.zoomPos = v.uv * _SunPos.zw + (_SunPos.xy -_SunPos.zw*0.5);
    193.                     return o;
    194.                 }
    195.  
    196.                 half4 drawFrag (frag_v2f i) : SV_Target {
    197.                     half2 basePos = i.basePos;
    198.                     half2 zoomPos = i.zoomPos;
    199.                     half firstPass = tex2D(_MainTex, basePos).a;
    200.                     half secondPass = tex2D(_MainTex, zoomPos).a-0.85;
    201.                     half4 output = half4(0,0,0,max(firstPass, secondPass));
    202.                     secondPass = tex2D(_MainTex, lerp(zoomPos, basePos, 0.65)).a-0.3;
    203.                     output.a = max(output.a, secondPass);
    204.                     secondPass = tex2D(_MainTex, lerp(zoomPos, basePos, 0.4)).a-0.45;
    205.                     output.a = max(output.a, secondPass);
    206.                     secondPass = tex2D(_MainTex, lerp(zoomPos, basePos, 0.25)).a-0.6;
    207.                     output.a = max(output.a, secondPass);
    208.                     secondPass = tex2D(_MainTex, lerp(zoomPos, basePos, 0.1)).a-0.7;
    209.                     output.a = max(output.a, secondPass);
    210.  
    211.                     return output;//half4(half3((basePos - zoomPos).x*20), 1);//tex2D(_ObstacleTex, basePos);
    212.                 }
    213.            
    214.             ENDCG
    215.         }
    216.  
    217.     Pass {    // 4 Single pass. Ping-pong
    218.             // Each pass duplicates previous pass, doubling drawn shadows
    219.             CGPROGRAM
    220.                 #pragma target 3.0
    221.            
    222.                 #include "UnityCG.cginc"
    223.                 #pragma vertex processVerts
    224.                 #pragma fragment drawFrag
    225.  
    226.                 #pragma glsl_no_auto_normalization
    227.  
    228.                 struct vertdata {
    229.                     float2 uv : TEXCOORD0;
    230.                     float2 uvScaled : TEXCOORD1;
    231.                     float4 vertex : SV_POSITION;
    232.                 };
    233.  
    234.                 struct frag_v2f {
    235.                     float4 vertex : SV_POSITION;
    236.                     half2 basePos : TEXCOORD0;
    237.                     half2 zoomPos : TEXCOORD1;
    238.                 };
    239.            
    240.                 uniform sampler2D _ObstacleTex;
    241.                 uniform sampler2D _MainTex;
    242.                 half4 _SunPos;
    243.                 half _Offset;                                            // frational lerp value. Decrease by powers of two each pass
    244.                 #ifdef UNITY_HALF_TEXEL_OFFSET
    245.                 #endif
    246.  
    247.                 frag_v2f processVerts (vertdata v){
    248.                     frag_v2f o;
    249.                     o.vertex = UnityObjectToClipPos(v.vertex);
    250.                      o.basePos = v.uv;
    251.                     o.zoomPos = v.uv * _SunPos.zw + (_SunPos.xy -_SunPos.zw*0.5);
    252.                     return o;
    253.                 }
    254.  
    255.                 half4 drawFrag (frag_v2f i) : SV_Target {
    256.                     half firstPass = tex2D(_MainTex, i.basePos).a;
    257.                     half secondPass = tex2D(_MainTex, lerp(i.zoomPos, i.basePos, _Offset)).a;
    258.                     half4 output = half4(0,0,0,max(firstPass, secondPass));
    259.                     return output;
    260.                 }
    261.            
    262.             ENDCG
    263.         }
    264.     }
    265. }
    Can anyone explain what's different between 2017 and previous versions of Unity as to why this doesn't work any more? Documentation on post effects is non-existent.
     
  2. arshadameen

    arshadameen

    Joined:
    Mar 5, 2018
    Posts:
    8
    There are a lot of differences between current versions and 2017 versions. You need to download and then check the latest features.
     
  3. SoftwareGeezers

    SoftwareGeezers

    Joined:
    Jun 22, 2013
    Posts:
    902
    Found the problem is a change in being able to read and write to a destination rendertexture in a Graphics.Blit(). More details here. Of course, this change is undocumented...