Advertisement
Guest User

DecalHLSL.shader

a guest
Apr 11th, 2021
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // This shader fills the mesh shape with a color predefined in the code.
  2. Shader "Custom/DecalShader"
  3. {
  4.     // The properties block of the Unity shader. In this example this block is empty
  5.     // because the output color is predefined in the fragment shader code.
  6.     Properties
  7.     {
  8.       _LightStrength("Point/Spot Light Strength", Range(0,1)) = 0.5
  9.     }
  10.  
  11.     // The SubShader block containing the Shader code.
  12.     SubShader
  13.     {
  14.         // SubShader Tags define when and under which conditions a SubShader block or
  15.         // a pass is executed.
  16.         Tags { "RenderType" = "Transparent" "RenderPipeline" = "UniversalRenderPipeline" }
  17.     ZWrite On
  18.         Blend SrcAlpha OneMinusSrcAlpha
  19.  
  20.         // uncomment to have selective decals
  21.         // Stencil {
  22.         // Ref 5
  23.         // Comp Equal
  24.         // Fail zero
  25.         // }
  26.  
  27. // Then add the following to the shader you want the decals to show up on, not this one!:
  28.     //  Stencil {
  29.     //       Ref 5
  30.     //          Comp always
  31.     //      Pass Replace
  32.     //      }
  33.         Pass
  34.         {
  35.             // The HLSL code block. Unity SRP uses the HLSL language.
  36.             HLSLPROGRAM
  37.             // This line defines the name of the vertex shader.
  38.             #pragma vertex vert
  39.             // This line defines the name of the fragment shader.
  40.             #pragma fragment frag
  41.  
  42.             // The Core.hlsl file contains definitions of frequently used HLSL
  43.             // macros and functions, and also contains #include references to other
  44.             // HLSL files (for example, Common.hlsl, SpaceTransforms.hlsl, etc.).
  45.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
  46.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
  47.             #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl"
  48.             #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
  49.  
  50.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
  51.             #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
  52.  
  53.             // The structure definition defines which variables it contains.
  54.             // This example uses the Attributes structure as an input structure in
  55.             // the vertex shader.
  56.             struct Attributes
  57.             {
  58.                 // The positionOS variable contains the vertex positions in object
  59.                 // space.
  60.                 float4 positionOS   : POSITION;  
  61.                 float2 texcoord : TEXCOORD;              
  62.             };
  63.  
  64.             struct Varyings
  65.             {
  66.                 // The positions in this struct must have the SV_POSITION semantic.
  67.                 float4 positionHCS  : SV_POSITION;
  68.                 float2 uv : TEXCOORD;
  69.                 float4 screenUV : TEXCOORD1;
  70.                 float3 ray : TEXCOORD2;
  71.             };            
  72.  
  73.             // The vertex shader definition with properties defined in the Varyings
  74.             // structure. The type of the vert function must match the type (struct)
  75.             // that it returns.
  76.             Varyings vert(Attributes IN)
  77.             {
  78.                 // Declaring the output object (OUT) with the Varyings struct.
  79.                 Varyings OUT;
  80.  
  81.                    
  82.                 // The TransformObjectToHClip function transforms vertex positions
  83.                 // from object space to homogenous space
  84.                 OUT.positionHCS = TransformObjectToHClip(IN.positionOS.xyz);
  85.  
  86.                 OUT.uv = IN.texcoord;
  87.                 OUT.screenUV = ComputeScreenPos(OUT.positionHCS);
  88.                 OUT.ray = TransformWorldToView(TransformObjectToWorld(IN.positionOS)) * float3(1, 1, -1);
  89.                 // Returning the output.
  90.                 return OUT;
  91.             }
  92.  
  93.            
  94.         UNITY_INSTANCING_BUFFER_START(Props)
  95.         UNITY_DEFINE_INSTANCED_PROP(sampler2D, _MainTex)
  96.         UNITY_DEFINE_INSTANCED_PROP(float4, _Tint)
  97.         UNITY_INSTANCING_BUFFER_END(Props)
  98.  
  99.         float _LightStrength;
  100.  
  101.          // The fragment shader definition.            
  102.         half4 frag(Varyings i) : SV_Target
  103.         {
  104.  
  105.             i.ray = i.ray * (_ProjectionParams.z / i.ray.z);
  106.  
  107.             //Screenspace UV
  108.             float2 uv = i.screenUV.xy / i.screenUV.w;
  109.             // read depth
  110.             float depth = SampleSceneDepth(uv);
  111.             depth = Linear01Depth(depth, _ZBufferParams);
  112.  
  113.        
  114.             // reconstruct world space
  115.             float4 vpos = float4(i.ray * depth, 1);
  116.             float4 wpos = mul(unity_CameraToWorld, vpos);
  117.             float3 opos = TransformWorldToObject(float4(wpos)).xyz;
  118.             clip(float3(0.5, 0.5, 0.5) - abs(opos.xyz));
  119.  
  120.             // offset uvs
  121.             i.uv = opos.xz + 0.5;
  122.  
  123.             // add texture from decal script
  124.             float4 col = tex2D(UNITY_ACCESS_INSTANCED_PROP(Props, _MainTex), i.uv);
  125.  
  126.             clip(col.a - 0.1);
  127.             col *= col.a;
  128.  
  129.             // get directional shadows
  130.             float4 shadowCoord = TransformWorldToShadowCoord(wpos);
  131.             ShadowSamplingData shadowSamplingData = GetMainLightShadowSamplingData();
  132.             float shadowStrength = GetMainLightShadowStrength();
  133.             float ShadowAtten = SampleShadowmap(shadowCoord, TEXTURE2D_ARGS(_MainLightShadowmapTexture,sampler_MainLightShadowmapTexture),shadowSamplingData, shadowStrength, false);
  134.        
  135.             Light light = GetMainLight();
  136.             // multiply with light color and shadows, ambient
  137.             col.rgb *= (light.color * ShadowAtten) + unity_AmbientSky;
  138.  
  139.             // extra lights (point/spot)
  140.             float3 extraLights;
  141.             int pixelLightCount = GetAdditionalLightsCount();
  142.             for (int j = 0; j < pixelLightCount; ++j) {
  143.                 Light lightA = GetAdditionalLight(j, wpos);
  144.                 float3 attenuatedLightColor = lightA.color * (lightA.distanceAttenuation * lightA.shadowAttenuation);
  145.                 extraLights += attenuatedLightColor;
  146.             }
  147.             extraLights *= _LightStrength;
  148.             col.rgb+= extraLights;
  149.             // add tinting and transparency
  150.             col.rgb *= _Tint.rgb;
  151.             col *= _Tint.a;    
  152.             return col;
  153.             }
  154.             ENDHLSL
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement