From a29197d32bcb42b0496385a7ad4d6f9f16bd01bf Mon Sep 17 00:00:00 2001 From: Paul Demeulenaere Date: Mon, 5 Jul 2021 14:05:45 +0200 Subject: [PATCH 1/3] [VFX] Fix soft particles (#4733) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Update changelog # Conflicts: # com.unity.visualeffectgraph/CHANGELOG.md * Different depth computation for orthographic camera # Conflicts: # com.unity.visualeffectgraph/CHANGELOG.md * Restore change from https://github.cds.internal.unity3d.com/unity/vfx-graphics/pull/211/commits/8169e083d9b68114bd7a248ebba1dfba38fca3f5 But using URP specific helper "LinearDepthToEyeDepth" * FIx issue 1330697 Correctly applying the flip-y when appropriate while sampling * Fix scaling using dedicated URP helper It's simpler than reimplement the space transform and equivalent * Update com.unity.render-pipelines.universal/Runtime/VFXGraph/Shaders/VFXCommon.hlsl Fix comment Co-authored-by: Elvar Örn Unnþórsson * Fix issue https://github.com/Unity-Technologies/Graphics/pull/4733#discussion_r643037035 Remove VFXLinearEyeDepth & VFXLinearEyeDepthOrthographic from the SRP specific implementation Synchronize implementation for VFXLinearEyeDepth in VFXCommonCompute.hlsl (not used anyway) TODO : Move LinearDepthToEyeDepth in core for 21.2 Co-authored-by: Ludovic Theobald Co-authored-by: Elvar Örn Unnþórsson # Conflicts: # com.unity.render-pipelines.universal/CHANGELOG.md # com.unity.render-pipelines.universal/Runtime/VFXGraph/Shaders/VFXCommon.hlsl # com.unity.visualeffectgraph/CHANGELOG.md --- .../Runtime/VFXGraph/Shaders/VFXCommon.hlsl | 5 - .../CHANGELOG.md | 2 + .../Runtime/VFXGraph/Shaders/VFXCommon.hlsl | 161 ++++++++++++++++++ com.unity.visualeffectgraph/CHANGELOG.md | 1 + .../Shaders/Common/VFXCommonCompute.hlsl | 7 +- .../Shaders/VFXCommonOutput.hlsl | 28 ++- 6 files changed, 196 insertions(+), 8 deletions(-) create mode 100644 com.unity.render-pipelines.universal/Runtime/VFXGraph/Shaders/VFXCommon.hlsl diff --git a/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXCommon.hlsl b/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXCommon.hlsl index 4d3c2f3a1f2..0311e85b8c0 100644 --- a/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXCommon.hlsl +++ b/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXCommon.hlsl @@ -118,11 +118,6 @@ float VFXSampleDepth(float4 posSS) return LoadCameraDepth(posSS.xy); } -float VFXLinearEyeDepth(float depth) -{ - return LinearEyeDepth(depth,_ZBufferParams); -} - void VFXApplyShadowBias(inout float4 posCS, inout float3 posWS, float3 normalWS) { } diff --git a/com.unity.render-pipelines.universal/CHANGELOG.md b/com.unity.render-pipelines.universal/CHANGELOG.md index 08e781e970c..20a8b0081d4 100644 --- a/com.unity.render-pipelines.universal/CHANGELOG.md +++ b/com.unity.render-pipelines.universal/CHANGELOG.md @@ -4,6 +4,8 @@ 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). +- VFX: Fixed soft particles when HDR or Opaque texture isn't enabled +- VFX: Fixed OpenGL soft particles fallback when depth texture isn't available ## [11.0.0] - 2020-10-21 ### Added - Added real-time Point Light Shadows. diff --git a/com.unity.render-pipelines.universal/Runtime/VFXGraph/Shaders/VFXCommon.hlsl b/com.unity.render-pipelines.universal/Runtime/VFXGraph/Shaders/VFXCommon.hlsl new file mode 100644 index 00000000000..d41a2ef65c0 --- /dev/null +++ b/com.unity.render-pipelines.universal/Runtime/VFXGraph/Shaders/VFXCommon.hlsl @@ -0,0 +1,161 @@ +#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" +#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl" +#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl" +#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" +#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl" +#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl" +#include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/ShaderPass.hlsl" + +float3 _LightDirection; + +#ifdef VFX_VARYING_PS_INPUTS +void VFXTransformPSInputs(inout VFX_VARYING_PS_INPUTS input) {} + +float4 VFXApplyPreExposure(float4 color, float exposureWeight) +{ + return color; +} + +float4 VFXApplyPreExposure(float4 color, VFX_VARYING_PS_INPUTS input) +{ + return color; +} +#endif + +float4 VFXTransformFinalColor(float4 color) +{ + return color; +} + +void VFXEncodeMotionVector(float2 velocity, out float4 outBuffer) +{ + //TODO : LWRP doesn't support motion vector & TAA yet + outBuffer = (float4)0.0f; +} + +float4 VFXTransformPositionWorldToClip(float3 posWS) +{ + return TransformWorldToHClip(posWS); +} + +float4 VFXTransformPositionWorldToNonJitteredClip(float3 posWS) +{ + //TODO : LWRP doesn't support motion vector & TAA yet + return VFXTransformPositionWorldToClip(posWS); +} + +float4 VFXTransformPositionWorldToPreviousClip(float3 posWS) +{ + //TODO : LWRP doesn't support motion vector & TAA yet + return VFXTransformPositionWorldToClip(posWS); +} + +float4 VFXTransformPositionObjectToClip(float3 posOS) +{ + float3 posWS = TransformObjectToWorld(posOS); + return VFXTransformPositionWorldToClip(posWS); +} + +float4 VFXTransformPositionObjectToNonJitteredClip(float3 posOS) +{ + //TODO : LWRP doesn't support motion vector & TAA yet + return VFXTransformPositionObjectToClip(posOS); +} + +float4 VFXTransformPositionObjectToPreviousClip(float3 posOS) +{ + //TODO : LWRP doesn't support motion vector & TAA yet + return VFXTransformPositionObjectToClip(posOS); +} + +float3 VFXTransformPositionWorldToView(float3 posWS) +{ + return TransformWorldToView(posWS); +} + +float3 VFXTransformPositionWorldToCameraRelative(float3 posWS) +{ +#if (VFX_WORLD_SPACE || SHADEROPTIONS_CAMERA_RELATIVE_RENDERING == 0) + return posWS - _WorldSpaceCameraPos.xyz; +#else + return posWS; +#endif +} + +float4x4 VFXGetObjectToWorldMatrix() +{ + return GetObjectToWorldMatrix(); +} + +float4x4 VFXGetWorldToObjectMatrix() +{ + return GetWorldToObjectMatrix(); +} + +float3x3 VFXGetWorldToViewRotMatrix() +{ + return (float3x3)GetWorldToViewMatrix(); +} + +float3 VFXGetViewWorldPosition() +{ + return GetCurrentViewPosition(); +} + +float4x4 VFXGetViewToWorldMatrix() +{ + return UNITY_MATRIX_I_V; +} + +#ifdef USING_STEREO_MATRICES +float3 GetWorldStereoOffset() +{ + return float3(0.0f, 0.0f, 0.0f); +} +#endif + +float VFXSampleDepth(float4 posSS) +{ + float2 screenUV = GetNormalizedScreenSpaceUV(posSS.xy); + + // In URP, the depth texture is optional and could be 4x4 white texture, Load isn't appropriate in that case. + //float depth = LoadSceneDepth(screenUV * _ScreenParams.xy); + float depth = SampleSceneDepth(screenUV); + + return depth; +} + +void VFXApplyShadowBias(inout float4 posCS, inout float3 posWS, float3 normalWS) +{ + posWS = ApplyShadowBias(posWS, normalWS, _LightDirection); + posCS = VFXTransformPositionWorldToClip(posWS); +} + +void VFXApplyShadowBias(inout float4 posCS, inout float3 posWS) +{ + posWS = ApplyShadowBias(posWS, _LightDirection, _LightDirection); + posCS = VFXTransformPositionWorldToClip(posWS); +} + +float4 VFXApplyFog(float4 color,float4 posCS,float3 posWS) +{ + float4 fog = (float4)0; + fog.rgb = unity_FogColor.rgb; + + float fogFactor = ComputeFogFactor(posCS.z * posCS.w); + fog.a = ComputeFogIntensity(fogFactor); + +#if VFX_BLENDMODE_ALPHA || IS_OPAQUE_PARTICLE + color.rgb = lerp(fog.rgb, color.rgb, fog.a); +#elif VFX_BLENDMODE_ADD + color.rgb *= fog.a; +#elif VFX_BLENDMODE_PREMULTIPLY + color.rgb = lerp(fog.rgb * color.a, color.rgb, fog.a); +#endif + return color; +} + +float3 VFXGetCameraWorldDirection() +{ + return unity_CameraToWorld._m02_m12_m22; +} diff --git a/com.unity.visualeffectgraph/CHANGELOG.md b/com.unity.visualeffectgraph/CHANGELOG.md index 675fe8f6256..512beb32500 100644 --- a/com.unity.visualeffectgraph/CHANGELOG.md +++ b/com.unity.visualeffectgraph/CHANGELOG.md @@ -61,6 +61,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Remove unexpected expression in spawn context evaluation [Case 1318412](https://issuetracker.unity3d.com/product/unity/issues/guid/1318412/) - Fix incorrect buffer type for strips - Compilation issue when normal is used in shadergraph for opacity with unlit output +- Fix Soft Particle depth computation when using an orthographic camera [Case 1309961](https://issuetracker.unity3d.com/product/unity/issues/guid/1309961) ## [10.2.0] - 2020-10-19 ### Added diff --git a/com.unity.visualeffectgraph/Shaders/Common/VFXCommonCompute.hlsl b/com.unity.visualeffectgraph/Shaders/Common/VFXCommonCompute.hlsl index ebf560bcd28..e9cd9f3c701 100644 --- a/com.unity.visualeffectgraph/Shaders/Common/VFXCommonCompute.hlsl +++ b/com.unity.visualeffectgraph/Shaders/Common/VFXCommonCompute.hlsl @@ -54,7 +54,12 @@ float3 VFXGetViewWorldPosition() return (float3)0.0f; } -float VFXLinearEyeDepth(float4 posSS) +float VFXLinearEyeDepth(float depth) +{ + return 0.0f; +} + +float VFXLinearEyeDepthOrthographic(float depth) { return 0.0f; } diff --git a/com.unity.visualeffectgraph/Shaders/VFXCommonOutput.hlsl b/com.unity.visualeffectgraph/Shaders/VFXCommonOutput.hlsl index 33151287782..e57a2989fe6 100644 --- a/com.unity.visualeffectgraph/Shaders/VFXCommonOutput.hlsl +++ b/com.unity.visualeffectgraph/Shaders/VFXCommonOutput.hlsl @@ -96,12 +96,36 @@ float4 VFXGetParticleColor(VFX_VARYING_PS_INPUTS i) return color; } +float VFXLinearEyeDepth(float depth) +{ + return LinearEyeDepth(depth, _ZBufferParams); +} + +float VFXLinearEyeDepthOrthographic(float depth) +{ +#if UNITY_REVERSED_Z + return float(_ProjectionParams.z - (_ProjectionParams.z - _ProjectionParams.y) * depth); +#else + return float(_ProjectionParams.y + (_ProjectionParams.z - _ProjectionParams.y) * depth); +#endif +} + float VFXGetSoftParticleFade(VFX_VARYING_PS_INPUTS i) { float fade = 1.0f; #if USE_SOFT_PARTICLE && defined(VFX_VARYING_INVSOFTPARTICLEFADEDISTANCE) - float sceneZ = VFXLinearEyeDepth(VFXSampleDepth(i.VFX_VARYING_POSCS)); - fade = saturate(i.VFX_VARYING_INVSOFTPARTICLEFADEDISTANCE * (sceneZ - i.VFX_VARYING_POSCS.w)); + float sceneZ, selfZ; + if(IsPerspectiveProjection()) + { + sceneZ = VFXLinearEyeDepth(VFXSampleDepth(i.VFX_VARYING_POSCS)); + selfZ = i.VFX_VARYING_POSCS.w; + } + else + { + sceneZ = VFXLinearEyeDepthOrthographic(VFXSampleDepth(i.VFX_VARYING_POSCS)); + selfZ = VFXLinearEyeDepthOrthographic(i.VFX_VARYING_POSCS.z); + } + fade = saturate(i.VFX_VARYING_INVSOFTPARTICLEFADEDISTANCE * (sceneZ - selfZ)); fade = fade * fade * (3.0 - (2.0 * fade)); // Smoothsteping the fade #endif return fade; From 129edd3ec7dc4ed2238269ba0d0ac6c2fecfc184 Mon Sep 17 00:00:00 2001 From: Paul Demeulenaere Date: Fri, 27 Aug 2021 09:50:59 +0200 Subject: [PATCH 2/3] Fix after merge Apply VFXSampleDepth for URP on the correct file --- .../Runtime/VFXGraph/Shaders/VFXCommon.hlsl | 161 ------------------ .../RenderPipeline/Universal/VFXCommon.hlsl | 11 +- 2 files changed, 6 insertions(+), 166 deletions(-) delete mode 100644 com.unity.render-pipelines.universal/Runtime/VFXGraph/Shaders/VFXCommon.hlsl diff --git a/com.unity.render-pipelines.universal/Runtime/VFXGraph/Shaders/VFXCommon.hlsl b/com.unity.render-pipelines.universal/Runtime/VFXGraph/Shaders/VFXCommon.hlsl deleted file mode 100644 index d41a2ef65c0..00000000000 --- a/com.unity.render-pipelines.universal/Runtime/VFXGraph/Shaders/VFXCommon.hlsl +++ /dev/null @@ -1,161 +0,0 @@ -#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl" -#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl" -#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Texture.hlsl" -#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" -#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl" -#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl" -#include "Packages/com.unity.render-pipelines.universal/Editor/ShaderGraph/Includes/ShaderPass.hlsl" - -float3 _LightDirection; - -#ifdef VFX_VARYING_PS_INPUTS -void VFXTransformPSInputs(inout VFX_VARYING_PS_INPUTS input) {} - -float4 VFXApplyPreExposure(float4 color, float exposureWeight) -{ - return color; -} - -float4 VFXApplyPreExposure(float4 color, VFX_VARYING_PS_INPUTS input) -{ - return color; -} -#endif - -float4 VFXTransformFinalColor(float4 color) -{ - return color; -} - -void VFXEncodeMotionVector(float2 velocity, out float4 outBuffer) -{ - //TODO : LWRP doesn't support motion vector & TAA yet - outBuffer = (float4)0.0f; -} - -float4 VFXTransformPositionWorldToClip(float3 posWS) -{ - return TransformWorldToHClip(posWS); -} - -float4 VFXTransformPositionWorldToNonJitteredClip(float3 posWS) -{ - //TODO : LWRP doesn't support motion vector & TAA yet - return VFXTransformPositionWorldToClip(posWS); -} - -float4 VFXTransformPositionWorldToPreviousClip(float3 posWS) -{ - //TODO : LWRP doesn't support motion vector & TAA yet - return VFXTransformPositionWorldToClip(posWS); -} - -float4 VFXTransformPositionObjectToClip(float3 posOS) -{ - float3 posWS = TransformObjectToWorld(posOS); - return VFXTransformPositionWorldToClip(posWS); -} - -float4 VFXTransformPositionObjectToNonJitteredClip(float3 posOS) -{ - //TODO : LWRP doesn't support motion vector & TAA yet - return VFXTransformPositionObjectToClip(posOS); -} - -float4 VFXTransformPositionObjectToPreviousClip(float3 posOS) -{ - //TODO : LWRP doesn't support motion vector & TAA yet - return VFXTransformPositionObjectToClip(posOS); -} - -float3 VFXTransformPositionWorldToView(float3 posWS) -{ - return TransformWorldToView(posWS); -} - -float3 VFXTransformPositionWorldToCameraRelative(float3 posWS) -{ -#if (VFX_WORLD_SPACE || SHADEROPTIONS_CAMERA_RELATIVE_RENDERING == 0) - return posWS - _WorldSpaceCameraPos.xyz; -#else - return posWS; -#endif -} - -float4x4 VFXGetObjectToWorldMatrix() -{ - return GetObjectToWorldMatrix(); -} - -float4x4 VFXGetWorldToObjectMatrix() -{ - return GetWorldToObjectMatrix(); -} - -float3x3 VFXGetWorldToViewRotMatrix() -{ - return (float3x3)GetWorldToViewMatrix(); -} - -float3 VFXGetViewWorldPosition() -{ - return GetCurrentViewPosition(); -} - -float4x4 VFXGetViewToWorldMatrix() -{ - return UNITY_MATRIX_I_V; -} - -#ifdef USING_STEREO_MATRICES -float3 GetWorldStereoOffset() -{ - return float3(0.0f, 0.0f, 0.0f); -} -#endif - -float VFXSampleDepth(float4 posSS) -{ - float2 screenUV = GetNormalizedScreenSpaceUV(posSS.xy); - - // In URP, the depth texture is optional and could be 4x4 white texture, Load isn't appropriate in that case. - //float depth = LoadSceneDepth(screenUV * _ScreenParams.xy); - float depth = SampleSceneDepth(screenUV); - - return depth; -} - -void VFXApplyShadowBias(inout float4 posCS, inout float3 posWS, float3 normalWS) -{ - posWS = ApplyShadowBias(posWS, normalWS, _LightDirection); - posCS = VFXTransformPositionWorldToClip(posWS); -} - -void VFXApplyShadowBias(inout float4 posCS, inout float3 posWS) -{ - posWS = ApplyShadowBias(posWS, _LightDirection, _LightDirection); - posCS = VFXTransformPositionWorldToClip(posWS); -} - -float4 VFXApplyFog(float4 color,float4 posCS,float3 posWS) -{ - float4 fog = (float4)0; - fog.rgb = unity_FogColor.rgb; - - float fogFactor = ComputeFogFactor(posCS.z * posCS.w); - fog.a = ComputeFogIntensity(fogFactor); - -#if VFX_BLENDMODE_ALPHA || IS_OPAQUE_PARTICLE - color.rgb = lerp(fog.rgb, color.rgb, fog.a); -#elif VFX_BLENDMODE_ADD - color.rgb *= fog.a; -#elif VFX_BLENDMODE_PREMULTIPLY - color.rgb = lerp(fog.rgb * color.a, color.rgb, fog.a); -#endif - return color; -} - -float3 VFXGetCameraWorldDirection() -{ - return unity_CameraToWorld._m02_m12_m22; -} diff --git a/com.unity.visualeffectgraph/Shaders/RenderPipeline/Universal/VFXCommon.hlsl b/com.unity.visualeffectgraph/Shaders/RenderPipeline/Universal/VFXCommon.hlsl index a26bf89fdc0..06e6f1a2300 100644 --- a/com.unity.visualeffectgraph/Shaders/RenderPipeline/Universal/VFXCommon.hlsl +++ b/com.unity.visualeffectgraph/Shaders/RenderPipeline/Universal/VFXCommon.hlsl @@ -105,12 +105,13 @@ float4x4 VFXGetViewToWorldMatrix() float VFXSampleDepth(float4 posSS) { - return LoadSceneDepth(uint2(posSS.xy)); -} + float2 screenUV = GetNormalizedScreenSpaceUV(posSS.xy); -float VFXLinearEyeDepth(float depth) -{ - return LinearEyeDepth(depth, _ZBufferParams); + // In URP, the depth texture is optional and could be 4x4 white texture, Load isn't appropriate in that case. + //float depth = LoadSceneDepth(screenUV * _ScreenParams.xy); + float depth = SampleSceneDepth(screenUV); + + return depth; } void VFXApplyShadowBias(inout float4 posCS, inout float3 posWS, float3 normalWS) From 73b6768f1c979555af919a93a02e4389aced8a3a Mon Sep 17 00:00:00 2001 From: Paul Demeulenaere Date: Fri, 27 Aug 2021 09:55:58 +0200 Subject: [PATCH 3/3] Fix changelog.md --- com.unity.render-pipelines.universal/CHANGELOG.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/com.unity.render-pipelines.universal/CHANGELOG.md b/com.unity.render-pipelines.universal/CHANGELOG.md index 20a8b0081d4..3c1719931ab 100644 --- a/com.unity.render-pipelines.universal/CHANGELOG.md +++ b/com.unity.render-pipelines.universal/CHANGELOG.md @@ -4,8 +4,6 @@ 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). -- VFX: Fixed soft particles when HDR or Opaque texture isn't enabled -- VFX: Fixed OpenGL soft particles fallback when depth texture isn't available ## [11.0.0] - 2020-10-21 ### Added - Added real-time Point Light Shadows. @@ -113,6 +111,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0. - Fixed terrain hole shadowing [case 1349305] - Fixed soft shadows shader variants not set to multi_compile_fragment on some shaders (gbuffer pass, speedtree shaders, WavingGrass shader). - Fixed artifacts in Speed Tree 8 billboard LODs due to SpeedTree LOD smoothing/crossfading [case 1348407] +- VFX: Fixed soft particles when HDR or Opaque texture isn't enabled +- VFX: Fixed OpenGL soft particles fallback when depth texture isn't available ## [10.2.0] - 2020-10-19