瀏覽代碼

[unity] Fixed URP and LWRP shaders `Sprite` and `SkeletonLit` not handling shadows correctly after 180 degree rotation (normal needs to be flipped for bias direction). Now URP shaders no longer require `Advanced - Add Normals` enabled to receive shadows. Closes #1842.

Harald Csaszar 4 年之前
父節點
當前提交
4a0b30dd2f

+ 1 - 0
CHANGELOG.md

@@ -252,6 +252,7 @@
   * Sprite shaders now provide four `Diffuse Ramp` modes as an Inspector Material parameter: `Hard`, `Soft`, `Old Hard` and `Old Soft`. In spine-unity 3.8 it defaults to `Old Hard` to keep the behaviour of existing projects unchanged. Note that `Old Hard` and `Old Soft` ramp versions were using only the right half of the ramp texture, and additionally multiplying the light intensity by 2, both leading to brighter lighting than without a ramp texture active. The new ramp modes `Hard` and `Soft` use the full ramp texture and do not modify light intensity, being consistent with lighting without a ramp texture active.
   * Added **native support for slot blend modes** `Additive`, `Multiply` and `Screen` with automatic assignment at newly imported skeleton assets. `BlendModeMaterialAssets` are now obsolete and replaced by the native properties at `SkeletonDataAsset`. The `SkeletonDataAsset` Inspector provides a new `Blend Modes - Upgrade` button to upgrade an obsolete `BlendModeMaterialAsset` to the native blend modes properties. This upgrade will be performed automatically on imported and re-imported assets in Unity 2020.1 and newer to prevent reported `BlendModeMaterialAsset` issues in these Unity versions. spine-unity 4.0 and newer will automatically perform this upgrade regardless of the Unity version.
   * `BoneFollower` and `BoneFollowerGraphic` components now provide better support for following bones when the skeleton's Transform is not the parent of the follower's Transform. Previously e.g. rotating a common parent Transform did not lead to the desired result, as well as negatively scaling a skeleton's Transform when it is not a parent of the follower's Transform.
+  * URP and LWRP `Sprite` and `SkeletonLit` shaders no longer require `Advanced - Add Normals` enabled to properly cast and receive shadows. It is recommended to disable `Add Normals` if normals are otherwise not needed.
 
 * **Changes of default values**
   * `SkeletonMecanim`'s `Layer Mix Mode` now defaults to `MixMode.MixNext` instead of `MixMode.MixAlways`.

+ 0 - 8
spine-unity/Assets/Spine/Runtime/spine-unity/Utility/MaterialChecks.cs

@@ -66,10 +66,6 @@ namespace Spine.Unity {
 			"\nWarning: 'Add Normals' required when not using 'Fixed Normals'!\n\nPlease\n"
 			+ "a) enable 'Add Normals' at the SkeletonRenderer/SkeletonAnimation component under 'Advanced' or\n"
 			+ "b) enable 'Fixed Normals' at the Material.\n";
-		public static readonly string kAddNormalsRequiredForURPShadowsMessage =
-			"\nWarning: 'Add Normals' required on URP shader to receive shadows!\n\nPlease\n"
-			+ "a) enable 'Add Normals' at the SkeletonRenderer/SkeletonAnimation component under 'Advanced' or\n"
-			+ "b) disable 'Receive Shadows' at the Material.\n";
 		public static readonly string kSolveTangentsMessage =
 			"\nWarning: 'Solve Tangents' required when using a Normal Map!\n\nPlease\n"
 			+ "a) enable 'Solve Tangents' at the SkeletonRenderer/SkeletonAnimation component under 'Advanced' or\n"
@@ -119,10 +115,6 @@ namespace Spine.Unity {
 					isProblematic = true;
 					errorMessage += kTintBlackMessage;
 				}
-				if (IsURP3DMaterial(material) && !AreShadowsDisabled(material) && renderer.addNormals == false) {
-					isProblematic = true;
-					errorMessage += kAddNormalsRequiredForURPShadowsMessage;
-				}
 			}
 			return isProblematic;
 		}

+ 15 - 5
spine-unity/Modules/com.esotericsoftware.spine.lwrp-shaders/Shaders/CGIncludes/Spine-SkeletonLit-ShadowCasterPass-LW.hlsl

@@ -22,11 +22,9 @@ struct VaryingsSpine
 	float4 texcoordAndAlpha: TEXCOORD0;
 };
 
-float4 GetShadowPositionHClip(AttributesSpine input)
+float4 GetShadowPositionHClip(float3 positionOS, half3 normalWS)
 {
-	float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
-	float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
-
+	float3 positionWS = TransformObjectToWorld(positionOS);
 	float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
 
 #if UNITY_REVERSED_Z
@@ -44,7 +42,19 @@ VaryingsSpine ShadowPassVertexSkeletonLit(AttributesSpine input)
 	UNITY_SETUP_INSTANCE_ID(input);
 
 	output.texcoordAndAlpha.xyz = float3(TRANSFORM_TEX(input.texcoord, _MainTex).xy, 0);
-	output.positionCS = GetShadowPositionHClip(input);
+
+	half3 fixedNormalOS = half3(0, 0, -1);
+	half3 normalWS = normalize(TransformObjectToWorldNormal(fixedNormalOS));
+#ifdef _DOUBLE_SIDED_LIGHTING
+	// flip normal for shadow bias if necessary
+	// unfortunately we have to compute the sign here in the vertex shader
+	// instead of using VFACE in fragment shader stage.
+	half3 viewDirWS = UNITY_MATRIX_V[2].xyz;
+	half faceSign = sign(dot(viewDirWS, normalWS));
+	normalWS *= faceSign;
+#endif
+
+	output.positionCS = GetShadowPositionHClip(input.positionOS.xyz, normalWS);
 	output.texcoordAndAlpha.a = input.vertexColor.a;
 	return output;
 }

+ 13 - 5
spine-unity/Modules/com.esotericsoftware.spine.lwrp-shaders/Shaders/CGIncludes/Spine-Sprite-ShadowCasterPass-LW.hlsl

@@ -21,11 +21,9 @@ struct VaryingsSpine
 	float4 texcoordAndAlpha: TEXCOORD0;
 };
 
-float4 GetShadowPositionHClip(AttributesSpine input)
+float4 GetShadowPositionHClip(float3 positionOS, half3 normalWS)
 {
-	float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
-	float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
-
+	float3 positionWS = TransformObjectToWorld(positionOS);
 	float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
 
 #if UNITY_REVERSED_Z
@@ -43,7 +41,17 @@ VaryingsSpine ShadowPassVertexSprite(AttributesSpine input)
 	UNITY_SETUP_INSTANCE_ID(input);
 
 	output.texcoordAndAlpha.xyz = float3(TRANSFORM_TEX(input.texcoord, _MainTex).xy, 0);
-	output.positionCS = GetShadowPositionHClip(input);
+
+	half3 fixedNormalOS = half3(0, 0, -1);
+	half3 normalWS = normalize(TransformObjectToWorldNormal(fixedNormalOS));
+	// flip normal for shadow bias if necessary
+	// unfortunately we have to compute the sign here in the vertex shader
+	// instead of using VFACE in fragment shader stage.
+	half3 viewDirWS = UNITY_MATRIX_V[2].xyz;
+	half faceSign = sign(dot(viewDirWS, normalWS));
+	normalWS *= faceSign;
+
+	output.positionCS = GetShadowPositionHClip(input.positionOS.xyz, normalWS);
 	output.texcoordAndAlpha.a = input.vertexColor.a * _Color.a;
 	return output;
 }

+ 1 - 0
spine-unity/Modules/com.esotericsoftware.spine.lwrp-shaders/Shaders/Spine-SkeletonLit-LW.shader

@@ -102,6 +102,7 @@ Shader "Lightweight Render Pipeline/Spine/Skeleton Lit" {
 			// GPU Instancing
 			#pragma multi_compile_instancing
 			#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
+			#pragma shader_feature _ _DOUBLE_SIDED_LIGHTING
 
 			#pragma vertex ShadowPassVertexSkeletonLit
 			#pragma fragment ShadowPassFragmentSkeletonLit

+ 15 - 5
spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-SkeletonLit-ShadowCasterPass-URP.hlsl

@@ -22,11 +22,9 @@ struct VaryingsSpine
 	float4 texcoordAndAlpha: TEXCOORD0;
 };
 
-float4 GetShadowPositionHClip(AttributesSpine input)
+float4 GetShadowPositionHClip(float3 positionOS, half3 normalWS)
 {
-	float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
-	float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
-
+	float3 positionWS = TransformObjectToWorld(positionOS);
 	float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
 
 #if UNITY_REVERSED_Z
@@ -44,7 +42,19 @@ VaryingsSpine ShadowPassVertexSkeletonLit(AttributesSpine input)
 	UNITY_SETUP_INSTANCE_ID(input);
 
 	output.texcoordAndAlpha.xyz = float3(TRANSFORM_TEX(input.texcoord, _MainTex).xy, 0);
-	output.positionCS = GetShadowPositionHClip(input);
+
+	half3 fixedNormalOS = half3(0, 0, -1);
+	half3 normalWS = normalize(TransformObjectToWorldNormal(fixedNormalOS));
+#ifdef _DOUBLE_SIDED_LIGHTING
+	// flip normal for shadow bias if necessary
+	// unfortunately we have to compute the sign here in the vertex shader
+	// instead of using VFACE in fragment shader stage.
+	half3 viewDirWS = UNITY_MATRIX_V[2].xyz;
+	half faceSign = sign(dot(viewDirWS, normalWS));
+	normalWS *= faceSign;
+#endif
+
+	output.positionCS = GetShadowPositionHClip(input.positionOS.xyz, normalWS);
 	output.texcoordAndAlpha.a = input.vertexColor.a;
 	return output;
 }

+ 13 - 5
spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-Sprite-ShadowCasterPass-URP.hlsl

@@ -21,11 +21,9 @@ struct VaryingsSpine
 	float4 texcoordAndAlpha: TEXCOORD0;
 };
 
-float4 GetShadowPositionHClip(AttributesSpine input)
+float4 GetShadowPositionHClip(float3 positionOS, half3 normalWS)
 {
-	float3 positionWS = TransformObjectToWorld(input.positionOS.xyz);
-	float3 normalWS = TransformObjectToWorldNormal(input.normalOS);
-
+	float3 positionWS = TransformObjectToWorld(positionOS);
 	float4 positionCS = TransformWorldToHClip(ApplyShadowBias(positionWS, normalWS, _LightDirection));
 
 #if UNITY_REVERSED_Z
@@ -43,7 +41,17 @@ VaryingsSpine ShadowPassVertexSprite(AttributesSpine input)
 	UNITY_SETUP_INSTANCE_ID(input);
 
 	output.texcoordAndAlpha.xyz = float3(TRANSFORM_TEX(input.texcoord, _MainTex).xy, 0);
-	output.positionCS = GetShadowPositionHClip(input);
+
+	half3 fixedNormalOS = half3(0, 0, -1);
+	half3 normalWS = normalize(TransformObjectToWorldNormal(fixedNormalOS));
+	// flip normal for shadow bias if necessary
+	// unfortunately we have to compute the sign here in the vertex shader
+	// instead of using VFACE in fragment shader stage.
+	half3 viewDirWS = UNITY_MATRIX_V[2].xyz;
+	half faceSign = sign(dot(viewDirWS, normalWS));
+	normalWS *= faceSign;
+
+	output.positionCS = GetShadowPositionHClip(input.positionOS.xyz, normalWS);
 	output.texcoordAndAlpha.a = input.vertexColor.a * _Color.a;
 	return output;
 }

+ 1 - 0
spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Spine-SkeletonLit-URP.shader

@@ -96,6 +96,7 @@
 			// GPU Instancing
 			#pragma multi_compile_instancing
 			#pragma shader_feature _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A
+			#pragma shader_feature _ _DOUBLE_SIDED_LIGHTING
 
 			#pragma vertex ShadowPassVertexSkeletonLit
 			#pragma fragment ShadowPassFragmentSkeletonLit