[unity] Fixed URP and LWRP SkeletonLit shader not receiving shadows. Closes #1840. Now another known issue of the Sprite shader is surfacing, not handling shadows correctly when rotated 180 degrees (see #1842).

This commit is contained in:
Harald Csaszar 2021-02-17 18:13:07 +01:00
parent e9f900c333
commit 1163142ba8
4 changed files with 72 additions and 12 deletions

View File

@ -5,6 +5,10 @@
#include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Lighting.hlsl" #include "Packages/com.unity.render-pipelines.lightweight/ShaderLibrary/Lighting.hlsl"
#if (defined(_MAIN_LIGHT_SHADOWS) || defined(MAIN_LIGHT_CALCULATE_SHADOWS)) && !defined(_RECEIVE_SHADOWS_OFF)
#define SKELETONLIT_RECEIVE_SHADOWS
#endif
struct appdata { struct appdata {
float3 pos : POSITION; float3 pos : POSITION;
float3 normal : NORMAL; float3 normal : NORMAL;
@ -18,25 +22,32 @@ struct VertexOutput {
half4 color : COLOR0; half4 color : COLOR0;
float2 uv0 : TEXCOORD0; float2 uv0 : TEXCOORD0;
float4 pos : SV_POSITION; float4 pos : SV_POSITION;
#if defined(SKELETONLIT_RECEIVE_SHADOWS)
float4 shadowCoord : TEXCOORD1;
half3 shadowedColor : TEXCOORD2;
#endif
UNITY_VERTEX_OUTPUT_STEREO UNITY_VERTEX_OUTPUT_STEREO
}; };
half3 LightweightLightVertexSimplified(float3 positionWS, half3 normalWS) { half3 LightweightLightVertexSimplified(float3 positionWS, half3 normalWS, out half3 shadowedColor) {
Light mainLight = GetMainLight(); Light mainLight = GetMainLight();
half3 attenuatedLightColor = mainLight.color * (mainLight.distanceAttenuation * mainLight.shadowAttenuation); half3 attenuatedLightColor = mainLight.color * (mainLight.distanceAttenuation * mainLight.shadowAttenuation);
half3 diffuseLightColor = LightingLambert(attenuatedLightColor, mainLight.direction, normalWS); half3 mainLightColor = LightingLambert(attenuatedLightColor, mainLight.direction, normalWS);
half3 additionalLightColor = half3(0, 0, 0);
// Note: we don't add any lighting in the fragment shader, thus we include both variants below // Note: we don't add any lighting in the fragment shader, thus we include both variants below
#if defined(_ADDITIONAL_LIGHTS) || defined(_ADDITIONAL_LIGHTS_VERTEX) #if defined(_ADDITIONAL_LIGHTS) || defined(_ADDITIONAL_LIGHTS_VERTEX)
for (int i = 0; i < GetAdditionalLightsCount(); ++i) for (int i = 0; i < GetAdditionalLightsCount(); ++i)
{ {
Light light = GetAdditionalLight(i, positionWS); Light light = GetAdditionalLight(i, positionWS);
half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation); half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
diffuseLightColor += LightingLambert(attenuatedLightColor, light.direction, normalWS); additionalLightColor += LightingLambert(attenuatedLightColor, light.direction, normalWS);
} }
#endif #endif
return diffuseLightColor; shadowedColor = additionalLightColor;
return mainLightColor + additionalLightColor;
} }
VertexOutput vert(appdata v) { VertexOutput vert(appdata v) {
@ -57,7 +68,11 @@ VertexOutput vert(appdata v) {
normalWS *= faceSign; normalWS *= faceSign;
#endif #endif
color.rgb = LightweightLightVertexSimplified(positionWS, normalWS); half3 shadowedColor;
color.rgb = LightweightLightVertexSimplified(positionWS, normalWS, shadowedColor);
#if defined(SKELETONLIT_RECEIVE_SHADOWS)
o.shadowedColor = shadowedColor;
#endif
// Note: ambient light is also handled via SH. // Note: ambient light is also handled via SH.
half3 vertexSH; half3 vertexSH;
@ -67,6 +82,13 @@ VertexOutput vert(appdata v) {
o.color = color; o.color = color;
o.uv0 = v.uv0; o.uv0 = v.uv0;
o.pos = TransformWorldToHClip(positionWS); o.pos = TransformWorldToHClip(positionWS);
#if defined(SKELETONLIT_RECEIVE_SHADOWS)
VertexPositionInputs vertexInput;
vertexInput.positionWS = positionWS;
vertexInput.positionCS = o.pos;
o.shadowCoord = GetShadowCoord(vertexInput);
#endif
return o; return o;
} }
@ -74,12 +96,18 @@ half4 frag(VertexOutput i) : SV_Target{
half4 tex = tex2D(_MainTex, i.uv0); half4 tex = tex2D(_MainTex, i.uv0);
half4 col; half4 col;
#if defined(SKELETONLIT_RECEIVE_SHADOWS)
half shadowAttenuation = MainLightRealtimeShadow(i.shadowCoord);
i.color.rgb = lerp(i.shadowedColor, i.color.rgb, shadowAttenuation);
#endif
#if defined(_STRAIGHT_ALPHA_INPUT) #if defined(_STRAIGHT_ALPHA_INPUT)
col.rgb = tex.rgb * i.color.rgb * tex.a; col.rgb = tex.rgb * i.color.rgb * tex.a;
#else #else
col.rgb = tex.rgb * i.color.rgb; col.rgb = tex.rgb * i.color.rgb;
#endif #endif
col.a = tex.a * i.color.a; col.a = tex.a * i.color.a;
return col; return col;
} }

View File

@ -7,6 +7,7 @@ Shader "Lightweight Render Pipeline/Spine/Skeleton Lit" {
_Cutoff ("Shadow alpha cutoff", Range(0,1)) = 0.1 _Cutoff ("Shadow alpha cutoff", Range(0,1)) = 0.1
[NoScaleOffset] _MainTex ("Main Texture", 2D) = "black" {} [NoScaleOffset] _MainTex ("Main Texture", 2D) = "black" {}
[Toggle(_STRAIGHT_ALPHA_INPUT)] _StraightAlphaInput("Straight Alpha Texture", Int) = 0 [Toggle(_STRAIGHT_ALPHA_INPUT)] _StraightAlphaInput("Straight Alpha Texture", Int) = 0
[Toggle(_RECEIVE_SHADOWS)] _ReceiveShadows("Receive Shadows", Int) = 0
[Toggle(_DOUBLE_SIDED_LIGHTING)] _DoubleSidedLighting("Double-Sided Lighting", Int) = 0 [Toggle(_DOUBLE_SIDED_LIGHTING)] _DoubleSidedLighting("Double-Sided Lighting", Int) = 0
[HideInInspector] _StencilRef("Stencil Reference", Float) = 1.0 [HideInInspector] _StencilRef("Stencil Reference", Float) = 1.0
[Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp("Stencil Compare", Float) = 0.0 // Disabled stencil test by default [Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp("Stencil Compare", Float) = 0.0 // Disabled stencil test by default
@ -62,6 +63,7 @@ Shader "Lightweight Render Pipeline/Spine/Skeleton Lit" {
// Spine related keywords // Spine related keywords
#pragma shader_feature _ _STRAIGHT_ALPHA_INPUT #pragma shader_feature _ _STRAIGHT_ALPHA_INPUT
#pragma shader_feature _ _DOUBLE_SIDED_LIGHTING #pragma shader_feature _ _DOUBLE_SIDED_LIGHTING
#pragma shader_feature _RECEIVE_SHADOWS_OFF _RECEIVE_SHADOWS
#pragma vertex vert #pragma vertex vert
#pragma fragment frag #pragma fragment frag
#pragma target 2.0 #pragma target 2.0

View File

@ -5,6 +5,10 @@
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
#if (defined(_MAIN_LIGHT_SHADOWS) || defined(MAIN_LIGHT_CALCULATE_SHADOWS)) && !defined(_RECEIVE_SHADOWS_OFF)
#define SKELETONLIT_RECEIVE_SHADOWS
#endif
struct appdata { struct appdata {
float3 pos : POSITION; float3 pos : POSITION;
float3 normal : NORMAL; float3 normal : NORMAL;
@ -18,25 +22,32 @@ struct VertexOutput {
half4 color : COLOR0; half4 color : COLOR0;
float2 uv0 : TEXCOORD0; float2 uv0 : TEXCOORD0;
float4 pos : SV_POSITION; float4 pos : SV_POSITION;
#if defined(SKELETONLIT_RECEIVE_SHADOWS)
float4 shadowCoord : TEXCOORD1;
half3 shadowedColor : TEXCOORD2;
#endif
UNITY_VERTEX_OUTPUT_STEREO UNITY_VERTEX_OUTPUT_STEREO
}; };
half3 LightweightLightVertexSimplified(float3 positionWS, half3 normalWS) { half3 LightweightLightVertexSimplified(float3 positionWS, half3 normalWS, out half3 shadowedColor) {
Light mainLight = GetMainLight(); Light mainLight = GetMainLight();
half3 attenuatedLightColor = mainLight.color * (mainLight.distanceAttenuation * mainLight.shadowAttenuation); half3 attenuatedLightColor = mainLight.color * (mainLight.distanceAttenuation * mainLight.shadowAttenuation);
half3 diffuseLightColor = LightingLambert(attenuatedLightColor, mainLight.direction, normalWS); half3 mainLightColor = LightingLambert(attenuatedLightColor, mainLight.direction, normalWS);
half3 additionalLightColor = half3(0, 0, 0);
// Note: we don't add any lighting in the fragment shader, thus we include both variants below // Note: we don't add any lighting in the fragment shader, thus we include both variants below
#if defined(_ADDITIONAL_LIGHTS) || defined(_ADDITIONAL_LIGHTS_VERTEX) #if defined(_ADDITIONAL_LIGHTS) || defined(_ADDITIONAL_LIGHTS_VERTEX)
for (int i = 0; i < GetAdditionalLightsCount(); ++i) for (int i = 0; i < GetAdditionalLightsCount(); ++i)
{ {
Light light = GetAdditionalLight(i, positionWS); Light light = GetAdditionalLight(i, positionWS);
half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation); half3 attenuatedLightColor = light.color * (light.distanceAttenuation * light.shadowAttenuation);
diffuseLightColor += LightingLambert(attenuatedLightColor, light.direction, normalWS); additionalLightColor += LightingLambert(attenuatedLightColor, light.direction, normalWS);
} }
#endif #endif
return diffuseLightColor; shadowedColor = additionalLightColor;
return mainLightColor + additionalLightColor;
} }
VertexOutput vert(appdata v) { VertexOutput vert(appdata v) {
@ -57,7 +68,11 @@ VertexOutput vert(appdata v) {
normalWS *= faceSign; normalWS *= faceSign;
#endif #endif
color.rgb = LightweightLightVertexSimplified(positionWS, normalWS); half3 shadowedColor;
color.rgb = LightweightLightVertexSimplified(positionWS, normalWS, shadowedColor);
#if defined(SKELETONLIT_RECEIVE_SHADOWS)
o.shadowedColor = shadowedColor;
#endif
// Note: ambient light is also handled via SH. // Note: ambient light is also handled via SH.
half3 vertexSH; half3 vertexSH;
@ -67,6 +82,13 @@ VertexOutput vert(appdata v) {
o.color = color; o.color = color;
o.uv0 = v.uv0; o.uv0 = v.uv0;
o.pos = TransformWorldToHClip(positionWS); o.pos = TransformWorldToHClip(positionWS);
#if defined(SKELETONLIT_RECEIVE_SHADOWS)
VertexPositionInputs vertexInput;
vertexInput.positionWS = positionWS;
vertexInput.positionCS = o.pos;
o.shadowCoord = GetShadowCoord(vertexInput);
#endif
return o; return o;
} }
@ -74,12 +96,18 @@ half4 frag(VertexOutput i) : SV_Target{
half4 tex = tex2D(_MainTex, i.uv0); half4 tex = tex2D(_MainTex, i.uv0);
half4 col; half4 col;
#if defined(SKELETONLIT_RECEIVE_SHADOWS)
half shadowAttenuation = MainLightRealtimeShadow(i.shadowCoord);
i.color.rgb = lerp(i.shadowedColor, i.color.rgb, shadowAttenuation);
#endif
#if defined(_STRAIGHT_ALPHA_INPUT) #if defined(_STRAIGHT_ALPHA_INPUT)
col.rgb = tex.rgb * i.color.rgb * tex.a; col.rgb = tex.rgb * i.color.rgb * tex.a;
#else #else
col.rgb = tex.rgb * i.color.rgb; col.rgb = tex.rgb * i.color.rgb;
#endif #endif
col.a = tex.a * i.color.a; col.a = tex.a * i.color.a;
return col; return col;
} }

View File

@ -3,6 +3,7 @@
_Cutoff ("Shadow alpha cutoff", Range(0,1)) = 0.1 _Cutoff ("Shadow alpha cutoff", Range(0,1)) = 0.1
[NoScaleOffset] _MainTex ("Main Texture", 2D) = "black" {} [NoScaleOffset] _MainTex ("Main Texture", 2D) = "black" {}
[Toggle(_STRAIGHT_ALPHA_INPUT)] _StraightAlphaInput("Straight Alpha Texture", Int) = 0 [Toggle(_STRAIGHT_ALPHA_INPUT)] _StraightAlphaInput("Straight Alpha Texture", Int) = 0
[Toggle(_RECEIVE_SHADOWS)] _ReceiveShadows("Receive Shadows", Int) = 0
[Toggle(_DOUBLE_SIDED_LIGHTING)] _DoubleSidedLighting("Double-Sided Lighting", Int) = 0 [Toggle(_DOUBLE_SIDED_LIGHTING)] _DoubleSidedLighting("Double-Sided Lighting", Int) = 0
[HideInInspector] _StencilRef("Stencil Reference", Float) = 1.0 [HideInInspector] _StencilRef("Stencil Reference", Float) = 1.0
[Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp("Stencil Compare", Float) = 0.0 // Disabled stencil test by default [Enum(UnityEngine.Rendering.CompareFunction)] _StencilComp("Stencil Compare", Float) = 0.0 // Disabled stencil test by default
@ -57,6 +58,7 @@
// Spine related keywords // Spine related keywords
#pragma shader_feature _ _STRAIGHT_ALPHA_INPUT #pragma shader_feature _ _STRAIGHT_ALPHA_INPUT
#pragma shader_feature _ _DOUBLE_SIDED_LIGHTING #pragma shader_feature _ _DOUBLE_SIDED_LIGHTING
#pragma shader_feature _RECEIVE_SHADOWS_OFF _RECEIVE_SHADOWS
#pragma vertex vert #pragma vertex vert
#pragma fragment frag #pragma fragment frag