From 5eca100d60c03d8db38287352c50bb45c0e99f86 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Mon, 24 Jul 2023 20:34:58 +0200 Subject: [PATCH] [unity] URP shaders: Fixed DoF effect ignoring written depth when Decal feature is enabled. Closes #2283. --- .../Include/Spine-DepthNormalsPass-URP.hlsl | 74 ++++++++++++++++ .../Spine-DepthNormalsPass-URP.hlsl.meta | 7 ++ .../Spine-Sprite-DepthNormalsPass-URP.hlsl | 88 +++++++++++++++++++ ...pine-Sprite-DepthNormalsPass-URP.hlsl.meta | 7 ++ .../Shaders/Spine-SkeletonLit-URP.shader | 36 +++++++- .../Shaders/Spine-Sprite-URP.shader | 36 ++++++++ .../package.json | 2 +- 7 files changed, 248 insertions(+), 2 deletions(-) create mode 100644 spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-DepthNormalsPass-URP.hlsl create mode 100644 spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-DepthNormalsPass-URP.hlsl.meta create mode 100644 spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-Sprite-DepthNormalsPass-URP.hlsl create mode 100644 spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-Sprite-DepthNormalsPass-URP.hlsl.meta diff --git a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-DepthNormalsPass-URP.hlsl b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-DepthNormalsPass-URP.hlsl new file mode 100644 index 000000000..eee3a181d --- /dev/null +++ b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-DepthNormalsPass-URP.hlsl @@ -0,0 +1,74 @@ +#ifndef SPRITES_DEPTH_NORMALS_PASS_URP_INCLUDED +#define SPRITES_DEPTH_NORMALS_PASS_URP_INCLUDED + +#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl" +#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" + +struct AttributesSpine +{ + float4 positionOS : POSITION; + float3 normalOS : NORMAL; + float4 vertexColor : COLOR; + float2 texcoord : TEXCOORD0; + UNITY_VERTEX_INPUT_INSTANCE_ID +}; + +struct VaryingsSpine +{ + float3 normalWS : NORMAL; + float4 positionCS : SV_POSITION; + float4 texcoordAndAlpha: TEXCOORD0; + UNITY_VERTEX_OUTPUT_STEREO +}; + +VaryingsSpine DepthNormalsVertex(AttributesSpine input) +{ + VaryingsSpine output = (VaryingsSpine)0; + UNITY_SETUP_INSTANCE_ID(input); + UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output); + + half3 fixedNormal = half3(0, 0, -1); + half3 normalWS = normalize(mul((float3x3)unity_ObjectToWorld, fixedNormal)); + +#ifdef _DOUBLE_SIDED_LIGHTING + // 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.normalWS = normalWS; + + output.texcoordAndAlpha.xyz = float3(TRANSFORM_TEX(input.texcoord, _MainTex).xy, 0); + output.texcoordAndAlpha.a = input.vertexColor.a; + output.positionCS = TransformObjectToHClip(input.positionOS.xyz); + return output; +} + +void DepthNormalsFragment(VaryingsSpine input, + out half4 outNormalWS : SV_Target0 +#ifdef _WRITE_RENDERING_LAYERS + , out float4 outRenderingLayers : SV_Target1 +#endif + ) +{ + fixed4 texureColor = tex2D(_MainTex, input.texcoordAndAlpha.xy); + clip(texureColor.a * input.texcoordAndAlpha.a - _Cutoff); + + float3 normalWS = input.normalWS; +#if defined(_GBUFFER_NORMALS_OCT) + float2 octNormalWS = PackNormalOctQuadEncode(normalWS); // values between [-1, +1], must use fp32 on some platforms. + float2 remappedOctNormalWS = saturate(octNormalWS * 0.5 + 0.5); // values between [ 0, 1] + half3 packedNormalWS = PackFloat2To888(remappedOctNormalWS); // values between [ 0, 1] + outNormalWS = half4(packedNormalWS, 0.0); +#else + outNormalWS = half4(normalWS, 0.0); +#endif + +#ifdef USE_WRITE_RENDERING_LAYERS + uint renderingLayers = GetMeshRenderingLayerBackwardsCompatible(); + outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0); +#endif +} + +#endif diff --git a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-DepthNormalsPass-URP.hlsl.meta b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-DepthNormalsPass-URP.hlsl.meta new file mode 100644 index 000000000..5517cd01c --- /dev/null +++ b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-DepthNormalsPass-URP.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 803855a1999ecce4081f5e0fb18c6475 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-Sprite-DepthNormalsPass-URP.hlsl b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-Sprite-DepthNormalsPass-URP.hlsl new file mode 100644 index 000000000..70a817fa0 --- /dev/null +++ b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-Sprite-DepthNormalsPass-URP.hlsl @@ -0,0 +1,88 @@ +#ifndef SPRITES_DEPTH_NORMALS_PASS_URP_INCLUDED +#define SPRITES_DEPTH_NORMALS_PASS_URP_INCLUDED + +#include "Include/Spine-Sprite-Common-URP.hlsl" +#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl" +#include "SpineCoreShaders/SpriteLighting.cginc" +#include "SpineCoreShaders/Spine-Common.cginc" +#include "Spine-Common-URP.hlsl" + +//#include "Include/Spine-Sprite-Common-URP.hlsl" +//#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" + +struct VaryingsSprite +{ + float4 pos : SV_POSITION; + fixed4 vertexColor : COLOR; + float3 texcoord : TEXCOORD0; + +#if defined(_NORMALMAP) + half4 normalWorld : TEXCOORD4; + half4 tangentWorld : TEXCOORD5; + half4 binormalWorld : TEXCOORD6; +#else + half3 normalWorld : TEXCOORD4; +#endif + UNITY_VERTEX_INPUT_INSTANCE_ID + UNITY_VERTEX_OUTPUT_STEREO +}; + +VaryingsSprite DepthNormalsVertexSprite(VertexInput input) +{ + VaryingsSprite output = (VaryingsSprite)0; + UNITY_SETUP_INSTANCE_ID(input); + UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output); + + output.pos = calculateLocalPos(input.vertex); + output.vertexColor = calculateVertexColor(input.color); + output.texcoord = float3(calculateTextureCoord(input.texcoord), 0); + + float backFaceSign = 1; +#if defined(FIXED_NORMALS_BACKFACE_RENDERING) + backFaceSign = calculateBackfacingSign(positionWS.xyz); +#endif + + half3 normalWS = calculateSpriteWorldNormal(input, -backFaceSign); + output.normalWorld.xyz = normalWS; +#if defined(_NORMALMAP) + output.tangentWorld.xyz = calculateWorldTangent(input.tangent); + output.binormalWorld.xyz = calculateSpriteWorldBinormal(input, output.normalWorld.xyz, output.tangentWorld.xyz, backFaceSign); +#endif + + return output; +} + +void DepthNormalsFragmentSprite(VaryingsSprite input, + out half4 outNormalWS : SV_Target0 +#ifdef _WRITE_RENDERING_LAYERS + , out float4 outRenderingLayers : SV_Target1 +#endif + ) +{ + UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input); + + fixed4 texureColor = calculateTexturePixel(input.texcoord.xy); + ALPHA_CLIP(texureColor, input.vertexColor) + +#if defined(PER_PIXEL_LIGHTING) && defined(_NORMALMAP) + half3 normalWS = calculateNormalFromBumpMap(input.texcoord.xy, input.tangentWorld.xyz, input.binormalWorld.xyz, input.normalWorld.xyz); +#else + half3 normalWS = input.normalWorld.xyz; +#endif + +#if defined(_GBUFFER_NORMALS_OCT) + float2 octNormalWS = PackNormalOctQuadEncode(normalWS); // values between [-1, +1], must use fp32 on some platforms. + float2 remappedOctNormalWS = saturate(octNormalWS * 0.5 + 0.5); // values between [ 0, 1] + half3 packedNormalWS = PackFloat2To888(remappedOctNormalWS); // values between [ 0, 1] + outNormalWS = half4(packedNormalWS, 0.0); +#else + outNormalWS = half4(normalWS, 0.0); +#endif + +#ifdef USE_WRITE_RENDERING_LAYERS + uint renderingLayers = GetMeshRenderingLayerBackwardsCompatible(); + outRenderingLayers = float4(EncodeMeshRenderingLayer(renderingLayers), 0, 0, 0); +#endif +} + +#endif diff --git a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-Sprite-DepthNormalsPass-URP.hlsl.meta b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-Sprite-DepthNormalsPass-URP.hlsl.meta new file mode 100644 index 000000000..efe280cf7 --- /dev/null +++ b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Include/Spine-Sprite-DepthNormalsPass-URP.hlsl.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: 5070c54df4a943a438cfe0a199b55657 +ShaderIncludeImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Spine-SkeletonLit-URP.shader b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Spine-SkeletonLit-URP.shader index af98cf955..70fc7e7df 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Spine-SkeletonLit-URP.shader +++ b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Spine-SkeletonLit-URP.shader @@ -123,7 +123,7 @@ ENDHLSL } - Pass + Pass { Name "DepthOnly" Tags{"LightMode" = "DepthOnly"} @@ -157,6 +157,40 @@ #include "Include/Spine-DepthOnlyPass-URP.hlsl" ENDHLSL } + + // This pass is used when drawing to a _CameraNormalsTexture texture + Pass + { + Name "DepthNormals" + Tags{"LightMode" = "DepthNormals"} + + ZWrite On + + HLSLPROGRAM + #pragma vertex DepthNormalsVertex + #pragma fragment DepthNormalsFragment + + // ------------------------------------- + // Material Keywords + #pragma shader_feature _ALPHATEST_ON + #pragma shader_feature _ _DOUBLE_SIDED_LIGHTING + + // ------------------------------------- + // Universal Pipeline keywords + #pragma multi_compile_fragment _ _WRITE_RENDERING_LAYERS + + //-------------------------------------- + // GPU Instancing + #pragma multi_compile_instancing + + #define USE_URP + #define fixed4 half4 + #define fixed3 half3 + #define fixed half + #include "Include/Spine-Input-URP.hlsl" + #include "Include/Spine-DepthNormalsPass-URP.hlsl" + ENDHLSL + } } FallBack "Hidden/InternalErrorShader" diff --git a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Spine-Sprite-URP.shader b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Spine-Sprite-URP.shader index 01add5f75..d411f1e28 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Spine-Sprite-URP.shader +++ b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/Shaders/Spine-Sprite-URP.shader @@ -213,6 +213,42 @@ Shader "Universal Render Pipeline/Spine/Sprite" ENDHLSL } + // This pass is used when drawing to a _CameraNormalsTexture texture + Pass + { + Name "DepthNormals" + Tags{"LightMode" = "DepthNormals"} + + ZWrite On + Cull[_Cull] + + HLSLPROGRAM + #pragma vertex DepthNormalsVertexSprite + #pragma fragment DepthNormalsFragmentSprite + + // ------------------------------------- + // Material Keywords + #pragma shader_feature _ _FIXED_NORMALS_VIEWSPACE _FIXED_NORMALS_VIEWSPACE_BACKFACE _FIXED_NORMALS_MODELSPACE _FIXED_NORMALS_MODELSPACE_BACKFACE _FIXED_NORMALS_WORLDSPACE + #pragma shader_feature _NORMALMAP + #pragma shader_feature _ALPHA_CLIP + + // ------------------------------------- + // Universal Pipeline keywords + #pragma multi_compile_fragment _ _WRITE_RENDERING_LAYERS + + //-------------------------------------- + // GPU Instancing + #pragma multi_compile_instancing + + #define USE_URP + #define fixed4 half4 + #define fixed3 half3 + #define fixed half + #include "Include/Spine-Input-Sprite-URP.hlsl" + #include "Include/Spine-Sprite-DepthNormalsPass-URP.hlsl" + ENDHLSL + } + Pass { Name "Unlit" diff --git a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/package.json b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/package.json index 25e15624d..7d32f80a6 100644 --- a/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/package.json +++ b/spine-unity/Modules/com.esotericsoftware.spine.urp-shaders/package.json @@ -2,7 +2,7 @@ "name": "com.esotericsoftware.spine.urp-shaders", "displayName": "Spine Universal RP Shaders", "description": "This plugin provides universal render pipeline (URP) shaders for the spine-unity runtime.\n\nPrerequisites:\nIt requires a working installation of the spine-unity runtime, version 4.1.\n(See http://esotericsoftware.com/git/spine-runtimes/spine-unity)", - "version": "4.1.17", + "version": "4.1.18", "unity": "2019.3", "author": { "name": "Esoteric Software",