From 84a9ce430cb6174d1841fccba298c3edd54f95d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Elvar=20O=CC=88rn=20Unn=C3=BEo=CC=81rsson?= Date: Tue, 21 Apr 2020 14:34:47 +0200 Subject: [PATCH] Backport of Unity-Technologies/ScriptableRenderPipeline#6376 --- .../CHANGELOG.md | 5 +++++ .../ShaderGraph/Includes/PBRForwardPass.hlsl | 16 +++++++++------- .../Editor/ShaderGraph/Includes/Varyings.hlsl | 10 +++++----- .../Nature/SpeedTree7BillboardPasses.hlsl | 6 +++--- .../Nature/SpeedTree7CommonPasses.hlsl | 2 +- .../Shaders/Nature/SpeedTree8Passes.hlsl | 19 +++++++++++-------- .../Shaders/Terrain/WavingGrassPasses.hlsl | 11 +++++++---- 7 files changed, 41 insertions(+), 28 deletions(-) diff --git a/com.unity.render-pipelines.universal/CHANGELOG.md b/com.unity.render-pipelines.universal/CHANGELOG.md index 8bcece0c7de..33796d6e3e0 100644 --- a/com.unity.render-pipelines.universal/CHANGELOG.md +++ b/com.unity.render-pipelines.universal/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this package will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [Unreleased] + +### Fixed +- Fixed an issue with shadows not being correctly calculated in some shaders. + ## [8.1.0] - 2020-04-21 ### Added diff --git a/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/PBRForwardPass.hlsl b/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/PBRForwardPass.hlsl index 5581ee4c221..9ac3b77b6fb 100644 --- a/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/PBRForwardPass.hlsl +++ b/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/PBRForwardPass.hlsl @@ -13,14 +13,16 @@ #elif _NORMAL_DROPOFF_WS inputData.normalWS = normal; #endif - + #else inputData.normalWS = input.normalWS; #endif inputData.normalWS = NormalizeNormalPerPixel(inputData.normalWS); inputData.viewDirectionWS = SafeNormalize(input.viewDirectionWS); -#if defined(MAIN_LIGHT_CALCULATE_SHADOWS) +#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) + inputData.shadowCoord = input.shadowCoord; +#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS) inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS); #else inputData.shadowCoord = float4(0, 0, 0, 0); @@ -40,8 +42,8 @@ PackedVaryings vert(Attributes input) return packedOutput; } -half4 frag(PackedVaryings packedInput) : SV_TARGET -{ +half4 frag(PackedVaryings packedInput) : SV_TARGET +{ Varyings unpacked = UnpackVaryings(packedInput); UNITY_SETUP_INSTANCE_ID(unpacked); UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(unpacked); @@ -59,7 +61,7 @@ half4 frag(PackedVaryings packedInput) : SV_TARGET #ifdef _SPECULAR_SETUP float3 specular = surfaceDescription.Specular; float metallic = 1; - #else + #else float3 specular = 0; float metallic = surfaceDescription.Metallic; #endif @@ -72,8 +74,8 @@ half4 frag(PackedVaryings packedInput) : SV_TARGET surfaceDescription.Smoothness, surfaceDescription.Occlusion, surfaceDescription.Emission, - surfaceDescription.Alpha); + surfaceDescription.Alpha); - color.rgb = MixFog(color.rgb, inputData.fogCoord); + color.rgb = MixFog(color.rgb, inputData.fogCoord); return color; } diff --git a/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/Varyings.hlsl b/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/Varyings.hlsl index a20c0b95edb..801d1cbe473 100644 --- a/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/Varyings.hlsl +++ b/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/Varyings.hlsl @@ -14,12 +14,12 @@ Varyings BuildVaryings(Attributes input) // Evaluate Vertex Graph VertexDescriptionInputs vertexDescriptionInputs = BuildVertexDescriptionInputs(input); VertexDescription vertexDescription = VertexDescriptionFunction(vertexDescriptionInputs); - + // Assign modified vertex attributes input.positionOS = vertexDescription.VertexPosition; #if defined(VARYINGS_NEED_NORMAL_WS) input.normalOS = vertexDescription.VertexNormal; - #endif //FEATURES_GRAPH_NORMAL + #endif //FEATURES_GRAPH_NORMAL #if defined(VARYINGS_NEED_TANGENT_WS) input.tangentOS.xyz = vertexDescription.VertexTangent.xyz; #endif //FEATURES GRAPH TANGENT @@ -51,7 +51,7 @@ Varyings BuildVaryings(Attributes input) #ifdef VARYINGS_NEED_POSITION_WS output.positionWS = positionWS; #endif - + #ifdef VARYINGS_NEED_NORMAL_WS output.normalWS = normalWS; // normalized in TransformObjectToWorldNormal() #endif @@ -110,9 +110,9 @@ Varyings BuildVaryings(Attributes input) output.fogFactorAndVertexLight = half4(fogFactor, vertexLight); #endif -#ifdef _MAIN_LIGHT_SHADOWS +#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) output.shadowCoord = GetShadowCoord(vertexInput); #endif return output; -} \ No newline at end of file +} diff --git a/com.unity.render-pipelines.universal/Shaders/Nature/SpeedTree7BillboardPasses.hlsl b/com.unity.render-pipelines.universal/Shaders/Nature/SpeedTree7BillboardPasses.hlsl index 80d30b4ae84..a350919db68 100644 --- a/com.unity.render-pipelines.universal/Shaders/Nature/SpeedTree7BillboardPasses.hlsl +++ b/com.unity.render-pipelines.universal/Shaders/Nature/SpeedTree7BillboardPasses.hlsl @@ -100,9 +100,9 @@ SpeedTreeVertexOutput SpeedTree7Vert(SpeedTreeVertexInput input) output.clipPos = vertexInput.positionCS; -#ifdef _MAIN_LIGHT_SHADOWS - output.shadowCoord = GetShadowCoord(vertexInput); -#endif + #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) + output.shadowCoord = GetShadowCoord(vertexInput); + #endif return output; } diff --git a/com.unity.render-pipelines.universal/Shaders/Nature/SpeedTree7CommonPasses.hlsl b/com.unity.render-pipelines.universal/Shaders/Nature/SpeedTree7CommonPasses.hlsl index bb8f69e7be1..6642e934711 100644 --- a/com.unity.render-pipelines.universal/Shaders/Nature/SpeedTree7CommonPasses.hlsl +++ b/com.unity.render-pipelines.universal/Shaders/Nature/SpeedTree7CommonPasses.hlsl @@ -44,7 +44,7 @@ struct SpeedTreeVertexOutput half3 viewDirWS : TEXCOORD4; #endif - #ifdef _MAIN_LIGHT_SHADOWS + #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) float4 shadowCoord : TEXCOORD6; #endif diff --git a/com.unity.render-pipelines.universal/Shaders/Nature/SpeedTree8Passes.hlsl b/com.unity.render-pipelines.universal/Shaders/Nature/SpeedTree8Passes.hlsl index 9c17d4b183e..e0e85b26236 100644 --- a/com.unity.render-pipelines.universal/Shaders/Nature/SpeedTree8Passes.hlsl +++ b/com.unity.render-pipelines.universal/Shaders/Nature/SpeedTree8Passes.hlsl @@ -33,7 +33,7 @@ struct SpeedTreeVertexOutput half3 viewDirWS : TEXCOORD4; #endif - #ifdef _MAIN_LIGHT_SHADOWS + #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) float4 shadowCoord : TEXCOORD6; #endif @@ -230,11 +230,12 @@ SpeedTreeVertexOutput SpeedTree8Vert(SpeedTreeVertexInput input) output.viewDirWS = viewDirWS; #endif - #ifdef _MAIN_LIGHT_SHADOWS + output.positionWS = vertexInput.positionWS; + + #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) output.shadowCoord = GetShadowCoord(vertexInput); #endif - output.positionWS = vertexInput.positionWS; output.clipPos = vertexInput.positionCS; return output; @@ -282,11 +283,13 @@ void InitializeInputData(SpeedTreeFragmentInput input, half3 normalTS, out Input inputData.viewDirectionWS = SafeNormalize(inputData.viewDirectionWS); #endif -#ifdef _MAIN_LIGHT_SHADOWS - inputData.shadowCoord = input.interpolated.shadowCoord; -#else - inputData.shadowCoord = float4(0, 0, 0, 0); -#endif + #if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) + inputData.shadowCoord = input.interpolated.shadowCoord; + #elif defined(MAIN_LIGHT_CALCULATE_SHADOWS) + inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS); + #else + inputData.shadowCoord = float4(0, 0, 0, 0); + #endif inputData.fogCoord = input.interpolated.fogFactorAndVertexLight.x; inputData.vertexLighting = input.interpolated.fogFactorAndVertexLight.yzw; diff --git a/com.unity.render-pipelines.universal/Shaders/Terrain/WavingGrassPasses.hlsl b/com.unity.render-pipelines.universal/Shaders/Terrain/WavingGrassPasses.hlsl index bdbe53a1a63..a701a7d0efe 100644 --- a/com.unity.render-pipelines.universal/Shaders/Terrain/WavingGrassPasses.hlsl +++ b/com.unity.render-pipelines.universal/Shaders/Terrain/WavingGrassPasses.hlsl @@ -26,7 +26,7 @@ struct GrassVertexOutput half4 fogFactorAndVertexLight : TEXCOORD5; // x: fogFactor, yzw: vertex light -#ifdef _MAIN_LIGHT_SHADOWS +#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) float4 shadowCoord : TEXCOORD6; #endif half4 color : TEXCOORD7; @@ -46,13 +46,16 @@ void InitializeInputData(GrassVertexOutput input, out InputData inputData) #endif inputData.normalWS = NormalizeNormalPerPixel(input.normal); - inputData.viewDirectionWS = viewDirWS; -#ifdef _MAIN_LIGHT_SHADOWS + +#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) inputData.shadowCoord = input.shadowCoord; +#elif defined(MAIN_LIGHT_CALCULATE_SHADOWS) + inputData.shadowCoord = TransformWorldToShadowCoord(inputData.positionWS); #else inputData.shadowCoord = float4(0, 0, 0, 0); #endif + inputData.fogCoord = input.fogFactorAndVertexLight.x; inputData.vertexLighting = input.fogFactorAndVertexLight.yzw; inputData.bakedGI = SAMPLE_GI(input.lightmapUV, input.vertexSH, inputData.normalWS); @@ -86,7 +89,7 @@ void InitializeVertData(GrassVertexInput input, inout GrassVertexOutput vertData half fogFactor = ComputeFogFactor(vertexInput.positionCS.z); vertData.fogFactorAndVertexLight = half4(fogFactor, vertexLight); -#ifdef _MAIN_LIGHT_SHADOWS +#if defined(REQUIRES_VERTEX_SHADOW_COORD_INTERPOLATOR) vertData.shadowCoord = GetShadowCoord(vertexInput); #endif }