Search Unity

Question Geometry Shader URP

Discussion in 'Shaders' started by stychu, Jan 4, 2021.

  1. stychu

    stychu

    Joined:
    May 9, 2016
    Posts:
    62
    Hello there.
    At the start, I would say that I am a total newbie when it comes to shaders. Can't do much by myself. Pointing that out I recently found an amazing effect which I would like to try in my demo project in URP.
    Problem is it was created in Built-in RP and I need to translate it to the URP. I already fight with this for 4-5 days. Looking everywhere for help. Maybe someone will point out why its not working.

    I basically managed to get most things working from that shader but the main party (Geometry) is not working and I can't find out why.

    Some sharp and big minds out there would care to help??

    <--- Original tutorial of the effect --->



    And my tries to recreate it in URP. The main problem lies somewhere in the geometry part I think.

    Code (CSharp):
    1. Shader "Unlit/TestShader"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _Color("Color", Color) = (1, 1, 1, 1)
    7.         [HDR]_AmbientColor("Ambient Color", Color) = (0.4,0.4,0.4,1)
    8.         _BumpMap("Normal Map", 2D) = "bump" {}
    9.         _BumpStr("Normal Map Strenght", float) = 1
    10.  
    11.         _FlowMap("Flow (RG)", 2D) = "black" {}
    12.         _DissolveTexture("Dissolve Texutre", 2D) = "white" {}
    13.         _DissolveColor("Dissolve Color Border", Color) = (1, 1, 1, 1)
    14.         _DissolveBorder("Dissolve Border", float) = 0.05
    15.  
    16.  
    17.         _Exapnd("Expand", float) = 1
    18.         _Weight("Weight", Range(0,1)) = 0
    19.         _Direction("Direction", Vector) = (0, 0, 0, 0)
    20.         [HDR]_DisintegrationColor("Disintegration Color", Color) = (1, 1, 1, 1)
    21.         _Glow("Glow", float) = 1
    22.  
    23.         _Shape("Shape Texutre", 2D) = "white" {}
    24.         _R("Radius", float) = .1
    25.     }
    26.     SubShader
    27.     {
    28.         Tags
    29.         {
    30.             "RenderPipeline"="UniversalPipeline"
    31.             "RenderType"="Opaque"
    32.             "Queue"="Geometry+0"
    33.         }
    34.         LOD 100
    35.  
    36.         //Forward Lit Pass
    37.         Pass
    38.         {
    39.             Name "Universal Forward"
    40.             Tags
    41.             {
    42.                 "LightMode" = "UniversalForward"
    43.             }
    44.             // Render State
    45.             Blend One Zero, One Zero
    46.             Cull Back
    47.             ZTest LEqual
    48.             ZWrite On
    49.             // ColorMask: <None>
    50.            
    51.             HLSLPROGRAM
    52.             #pragma prefer_hlslcc gles
    53.             #pragma exclude_renderers d3d11_9x
    54.             #pragma target 2.0
    55.             #pragma multi_compile_instancing
    56.             #pragma require geometry
    57.  
    58.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    59.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    60.             #pragma multi_compile _ _ADDITIONAL_LIGHTS
    61.             #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
    62.             #pragma multi_compile _ _SHADOWS_SOFT
    63.  
    64.             #pragma vertex vert
    65.             #pragma geometry geom
    66.             #pragma fragment frag
    67.             #pragma require geometry
    68.  
    69.             #include "TestShader.hlsl"
    70.             ENDHLSL
    71.         }
    72.        
    73.        
    74.         Pass
    75.         {
    76.             Name "ShadowCaster"
    77.             Tags
    78.             {
    79.                 "LightMode" = "ShadowCaster"
    80.             }
    81.             Cull Back
    82.  
    83.             HLSLPROGRAM
    84.             #pragma prefer_hlslcc gles
    85.             #pragma exclude_renderers d3d11_9x
    86.             #pragma target 2.0
    87.             #pragma multi_compile_instancing
    88.             #pragma require geometry
    89.  
    90.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
    91.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
    92.             #pragma multi_compile _ _ADDITIONAL_LIGHTS
    93.             #pragma multi_compile _ _ADDITIONAL_LIGHT_SHADOWS
    94.             #pragma multi_compile _ _SHADOWS_SOFT
    95.  
    96.             #pragma vertex vert
    97.             #pragma geometry geom
    98.             #pragma fragment frag
    99.             #pragma require geometry
    100.            
    101.             #include "TestShader.hlsl"
    102.             ENDHLSL
    103.         }
    104.     }
    105. }
    106.  
    107.  
    108.  
    109. ---------------------------------------------------------
    110. ---------------------------------------------------------
    111. ---------------------------------------------------------
    112.  
    113. #ifndef TESTSHADER_INCLUDED
    114. #define TESTSHADER_INCLUDED
    115.  
    116. #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
    117. // #include "Lighting.cginc"
    118.  
    119. #define UNITY_MATRIX_TEXTURE0 float4x4(1,0,0,0, 0,1,0,0, 0,0,1,0, 0,0,0,1)
    120.  
    121.  
    122. struct appdata
    123. {
    124.     float4 vertex : POSITION;
    125.     float3 normal : NORMAL;
    126.     float2 uv : TEXCOORD0;
    127. };
    128.  
    129. struct v2g
    130. {
    131.     float4 objPos : SV_POSITION;
    132.     float2 uv : TEXCOORD0;
    133.     float3 normal : TEXCOORD3;
    134.     float3 worldPos : TEXCOORD2;
    135. };
    136.  
    137. struct g2f
    138. {
    139.     float4 worldPos : SV_POSITION;
    140.     float2 uv : TEXCOORD0;
    141.     half4 color : TEXCOORD4;
    142.     float3 normal : TEXCOORD3;
    143. };
    144.  
    145. sampler2D _MainTex;
    146. float4 _MainTex_ST;
    147. float4 _Color;
    148. float4 _AmbientColor;
    149. sampler2D _BumpMap;
    150. float _BumpStr;
    151. float _Metallic;
    152.  
    153. sampler2D _FlowMap;
    154. float4 _FlowMap_ST;
    155. sampler2D _DissolveTexture;
    156. float4 _DissolveColor;
    157. float _DissolveBorder;
    158.  
    159.  
    160. float _Exapnd;
    161. float _Weight;
    162. float4 _Direction;
    163. float4 _DisintegrationColor;
    164. float _Glow;
    165. sampler2D _Shape;
    166. float _R;
    167.  
    168.  
    169. float remap(float value, float from1, float to1, float from2, float to2)
    170. {
    171.     return (value - from1) / (to1 - from1) * (to2 - from2) + from2;
    172. }
    173.  
    174. float4 remapFlowTexture(float4 tex)
    175. {
    176.     return float4(
    177.         remap(tex.x, 0, 1, -1, 1),
    178.         remap(tex.y, 0, 1, -1, 1),
    179.         0,
    180.         remap(tex.w, 0, 1, -1, 1)
    181.     );
    182. }
    183.  
    184. float2 MultiplyUV (float4x4 mat, float2 inUV) {
    185.     float4 temp = float4 (inUV.x, inUV.y, 0, 0);
    186.     temp = mul (mat, temp);
    187.     return temp.xy;
    188. }
    189.  
    190.  
    191. v2g vert(appdata v)
    192. {
    193.     v2g o = (v2g)0;
    194.  
    195.     o.objPos = v.vertex;
    196.     o.uv = v.uv;
    197.     o.normal = TransformObjectToWorldNormal(v.normal);
    198.     o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
    199.     return o;
    200. }
    201.  
    202. [maxvertexcount(7)]
    203. void geom(triangle v2g inputs[3], inout TriangleStream<g2f> triStream)
    204. {
    205.     float2 avgUV = (inputs[0].uv + inputs[1].uv + inputs[2].uv) / 3;
    206.     float3 avgPos = (inputs[0].objPos + inputs[1].objPos + inputs[2].objPos) / 3;
    207.     float3 avgNormal = (inputs[0].normal + inputs[1].normal + inputs[2].normal) / 3;
    208.  
    209.     float dissolve_value = tex2Dlod(_DissolveTexture, float4(avgUV, 0, 0)).r;
    210.     float t = clamp(_Weight * 2 - dissolve_value, 0, 1);
    211.  
    212.     float2 flowUV = TRANSFORM_TEX(mul(unity_ObjectToWorld, avgPos).xz, _FlowMap);
    213.     float4 flowVector = remapFlowTexture(tex2Dlod(_FlowMap, float4(flowUV, 0, 0)));
    214.  
    215.     float3 pseudoRandomPos = (avgPos) + _Direction;
    216.     pseudoRandomPos += (flowVector.xyz * _Exapnd);
    217.  
    218.     float3 p = lerp(avgPos, pseudoRandomPos, t);
    219.     float radius = lerp(_R, 0, t);
    220.  
    221.  
    222.     if (t > 0)
    223.     {
    224.         float3 look = _WorldSpaceCameraPos - p;
    225.         look = normalize(look);
    226.  
    227.         float3 right = UNITY_MATRIX_IT_MV[0].xyz;
    228.         float3 up = UNITY_MATRIX_IT_MV[1].xyz;
    229.  
    230.         float halfS = 0.5f * radius;
    231.  
    232.         float4 v[4];
    233.         v[0] = float4(p + halfS * right - halfS * up, 1.0f);
    234.         v[1] = float4(p + halfS * right + halfS * up, 1.0f);
    235.         v[2] = float4(p - halfS * right - halfS * up, 1.0f);
    236.         v[3] = float4(p - halfS * right + halfS * up, 1.0f);
    237.  
    238.  
    239.         g2f vert;
    240.         vert.worldPos = TransformObjectToHClip(v[0]);
    241.         vert.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, float2(1.0f, 0.0f));
    242.         vert.color = float4(1, 1, 1, 1);
    243.         vert.normal = avgNormal;
    244.         triStream.Append(vert);
    245.  
    246.         vert.worldPos = TransformObjectToHClip(v[1]);
    247.         vert.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, float2(1.0f, 1.0f));
    248.         vert.color = float4(1, 1, 1, 1);
    249.         vert.normal = avgNormal;
    250.         triStream.Append(vert);
    251.  
    252.         vert.worldPos = TransformObjectToHClip(v[2]);
    253.         vert.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, float2(0.0f, 0.0f));
    254.         vert.color = float4(1, 1, 1, 1);
    255.         vert.normal = avgNormal;
    256.         triStream.Append(vert);
    257.  
    258.         vert.worldPos = TransformObjectToHClip(v[3]);
    259.         vert.uv = MultiplyUV(UNITY_MATRIX_TEXTURE0, float2(0.0f, 1.0f));
    260.         vert.color = float4(1, 1, 1, 1);
    261.         vert.normal = avgNormal;
    262.         triStream.Append(vert);
    263.  
    264.         triStream.RestartStrip();
    265.     }
    266.  
    267.     for (int j = 0; j < 3; j++)
    268.     {
    269.         g2f o;
    270.         o.worldPos = TransformObjectToHClip(inputs[j].objPos);
    271.         o.uv = TRANSFORM_TEX(inputs[j].uv, _MainTex);
    272.         o.color = half4(0, 0, 0, 0);
    273.         o.normal = inputs[j].normal;
    274.         triStream.Append(o);
    275.     }
    276.  
    277.     triStream.RestartStrip();
    278. }
    279.  
    280. half4 frag(g2f i) : SV_Target
    281. {
    282.     half4 col = tex2D(_MainTex, i.uv) * _Color;
    283.                
    284.     float3 normal = normalize(i.normal);
    285.     half3 tnormal = UnpackNormal(tex2D(_BumpMap, i.uv));
    286.     tnormal.xy *= _BumpStr;
    287.     tnormal = normalize(tnormal);
    288.  
    289.     float NdotL = dot((float4)0, normal * tnormal);
    290.     float4 light = NdotL * (half4)0;
    291.     col *= (_AmbientColor + light);
    292.              
    293.     float brightness = i.color.w  * _Glow;
    294.     col = lerp(col, _DisintegrationColor,  i.color.x);
    295.  
    296.     if(brightness > 0){
    297.         col *= brightness + _Weight;
    298.     }
    299.  
    300.  
    301.     float dissolve = tex2D(_DissolveTexture, i.uv).r;
    302.                
    303.     if(i.color.w == 0){
    304.         clip(dissolve - 2*_Weight);
    305.         if(_Weight > 0){
    306.             col +=  _DissolveColor * _Glow * step( dissolve - 2*_Weight, _DissolveBorder);
    307.         }
    308.     }else{
    309.         float s = tex2D(_Shape, i.uv).r;
    310.         if(s < .5) {
    311.             discard;
    312.         }
    313.  
    314.     }
    315.  
    316.     return col;
    317. }
    318.  
    319. #endif
    320.  
    321.  
    322.  
     
  2. z16331

    z16331

    Joined:
    Feb 10, 2023
    Posts:
    1
    What's the use of #include "TestShader.hlsl"
    Unity can't open it.