Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example Shader for Universal RP
- // Written by @Cyanilux
- // https://cyangamedev.wordpress.com/urp-shader-code/
- Shader "Custom/LambertShaderExample" {
- Properties {
- _BaseMap ("Example Texture", 2D) = "white" {}
- _BaseColor ("Example Colour", Color) = (0, 0.66, 0.73, 1)
- _Cutoff ("Alpha Cutoff", Float) = 0.5
- }
- SubShader {
- Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline" }
- HLSLINCLUDE
- #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
- CBUFFER_START(UnityPerMaterial)
- float4 _BaseMap_ST;
- float4 _BaseColor;
- float _Cutoff;
- CBUFFER_END
- ENDHLSL
- Pass {
- Name "Example"
- Tags { "LightMode"="UniversalForward" }
- HLSLPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
- #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
- #pragma multi_compile _ _SHADOWS_SOFT
- #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
- struct Attributes {
- float4 positionOS : POSITION;
- float2 uv : TEXCOORD0;
- float4 color : COLOR;
- float4 normalOS : NORMAL;
- };
- struct Varyings {
- float4 positionCS : SV_POSITION;
- float2 uv : TEXCOORD0;
- float4 color : COLOR;
- float3 normalWS : NORMAL;
- float3 positionWS : TEXCOORD2;
- };
- TEXTURE2D(_BaseMap);
- SAMPLER(sampler_BaseMap);
- Varyings vert(Attributes IN) {
- Varyings OUT;
- VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.positionOS.xyz);
- OUT.positionCS = positionInputs.positionCS;
- // Or this :
- //OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
- OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap);
- OUT.color = IN.color;
- OUT.positionWS = positionInputs.positionWS;
- VertexNormalInputs normalInputs = GetVertexNormalInputs(IN.normalOS.xyz);
- OUT.normalWS = normalInputs.normalWS;
- return OUT;
- }
- half4 frag(Varyings IN) : SV_Target {
- half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv);
- half4 color = baseMap * _BaseColor * IN.color;
- float4 shadowCoord = TransformWorldToShadowCoord(IN.positionWS.xyz);
- Light light = GetMainLight(shadowCoord);
- half3 shading = LightingLambert(light.color, light.direction, IN.normalWS);
- return half4(color.rgb * shading * light.shadowAttenuation, color.a);
- }
- ENDHLSL
- }
- Pass {
- Name "ShadowCaster"
- Tags { "LightMode"="ShadowCaster" }
- ZWrite On
- ZTest LEqual
- HLSLPROGRAM
- // Required to compile gles 2.0 with standard srp library
- #pragma prefer_hlslcc gles
- #pragma exclude_renderers d3d11_9x gles
- //#pragma target 4.5
- // Material Keywords
- #pragma shader_feature _ALPHATEST_ON
- #pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
- // GPU Instancing
- #pragma multi_compile_instancing
- #pragma multi_compile _ DOTS_INSTANCING_ON
- #pragma vertex ShadowPassVertex
- #pragma fragment ShadowPassFragment
- //#include "Packages/com.unity.render-pipelines.universal/Shaders/LitInput.hlsl"
- // Note, the Lit shader that URP provides uses this, but it also handles the cbuffer which we already have.
- // We could change the shader to use their cbuffer, but we can also just do this :
- #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
- #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
- #include "Packages/com.unity.render-pipelines.universal/Shaders/ShadowCasterPass.hlsl"
- ENDHLSL
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement