Skip to content

Instantly share code, notes, and snippets.

@phi-lira
Created April 30, 2020 08:28
Show Gist options
  • Save phi-lira/7b5a4a62080f52cc35dcba060d7749df to your computer and use it in GitHub Desktop.
Save phi-lira/7b5a4a62080f52cc35dcba060d7749df to your computer and use it in GitHub Desktop.
Unlit Texture + Realtime Shadows shader
Shader "Universal Render Pipeline/Custom/UnlitTextureShadows"
{
Properties
{
[MainColor] _BaseColor("BaseColor", Color) = (1,1,1,1)
[MainTexture] _BaseMap("BaseMap", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline"="UniversalRenderPipeline"}
Pass
{
Tags { "LightMode"="UniversalForward" }
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
// -------------------------------------
// Universal Render Pipeline keywords
#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/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float2 uv : TEXCOORD0;
float3 positionWS : TEXCOORD1;
float4 positionHCS : SV_POSITION;
};
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);
CBUFFER_START(UnityPerMaterial)
float4 _BaseMap_ST;
half4 _BaseColor;
CBUFFER_END
Varyings vert(Attributes IN)
{
Varyings OUT;
// GetVertexPositionInputs computes position in different spaces (ViewSpace, WorldSpace, Homogeneous Clip Space)
VertexPositionInputs positionInputs = GetVertexPositionInputs(IN.positionOS.xyz);
OUT.positionHCS = positionInputs.positionCS;
OUT.positionWS = positionInputs.positionWS;
OUT.uv = TRANSFORM_TEX(IN.uv, _BaseMap);
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
// shadowCoord is position in shadow light space
float4 shadowCoord = TransformWorldToShadowCoord(IN.positionWS);
Light mainLight = GetMainLight(shadowCoord);
half4 color = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv) * _BaseColor;
color *= mainLight.shadowAttenuation;
return color;
}
ENDHLSL
}
// Used for rendering shadowmaps
// TODO: there's one issue with adding this UsePass here, it won't make this shader compatible with SRP Batcher
// as the ShadowCaster pass from Lit shader is using a different UnityPerMaterial CBUFFER.
// Maybe we should add a DECLARE_PASS macro that allows to user to inform the UnityPerMaterial CBUFFER to use?
UsePass "Universal Render Pipeline/Lit/ShadowCaster"
}
}
@dzitsiuk
Copy link

dzitsiuk commented May 9, 2020

Is there a way to have two subshaders in the same shader - one for Built-in RP and one for Universal?

@phi-lira
Copy link
Author

Yes. You can add a Subshader that doesn't not have the "RenderPipeline" tag. The way it works is that Unity will look at the first subshader to see if it's compatible, then if not it looks on the second and so on. The "RenderPipeline" tag tells Unity which shader is compatible with each pipeline.

Alternatively, instead of adding a Subshader you can Fallback to another one for the built-in pipeline. It works in a similar way, except you have to be careful that the material properties should match.

@phi-lira
Copy link
Author

If anyone is interested I moved this piece of shader and I'm maintaining it in a examples project available here: https://github.com/phi-lira/UniversalShaderExamples

@segant08
Copy link

I'm looking forward to it after shadow pass compatible with srp.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment