Skip to content

Commit 40fc1a9

Browse files
ludovic-theobaldEvergreen
authored andcommitted
[VFX][Fix] Fix Screen Space Size (and other) block in ShaderGraph outputs
This PR fixes the blocks that are implicitly using TransformObjectToWorld (e.g. Screen Space Size) in a ShaderGraph output. Prior to this PR, the block was trying to use `UNITY_MATRIX_M `which is redefined as `elementToWorld` ("particle -> world matrix), instead of the "object -> world" matrix. It now uses the correct matrix. Additionally, the selection & picking passes were broken when using blocks like Screen Space Size so it required change.
1 parent 5b00509 commit 40fc1a9

File tree

11 files changed

+8871
-80
lines changed

11 files changed

+8871
-80
lines changed

Packages/com.unity.render-pipelines.high-definition/Runtime/ShaderLibrary/PickingSpaceTransforms.hlsl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,11 @@ float4x4 ScenePickingGetCameraViewProjMatrix()
115115
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"
116116

117117
#if defined(HAVE_VFX_MODIFICATION)
118-
#define VFX_APPLY_CAMERA_POSITION_IN_ELEMENT_MATRIX 1
118+
#define VFX_HAS_PICKING_MATRIX_CORRECTION 1
119+
#define GetSGVFXUnityObjectToWorld GetSGVFXUnityObjectToWorld_Picking
120+
#define GetSGVFXUnityWorldToObject GetSGVFXUnityWorldToObject_Picking
121+
float4x4 GetSGVFXUnityObjectToWorld() { return RevertCameraTranslationFromMatrix(GetSGVFXUnityObjectToWorldBackup()); }
122+
float4x4 GetSGVFXUnityWorldToObject() { return RevertCameraTranslationFromInverseMatrix(GetSGVFXUnityWorldToObjectBackup()); }
119123
#endif
120124

121125
#endif

Packages/com.unity.render-pipelines.high-definition/Runtime/VFXGraph/Shaders/VFXCommon.hlsl

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,46 @@ float2 VFXGetNormalizedScreenSpaceUV(float4 clipPos)
5454
return clipPos.xy * frac(_ScreenParams.zw);
5555
}
5656

57+
float4x4 VFXGetObjectToWorldMatrix()
58+
{
59+
#if defined(SHADER_STAGE_RAY_TRACING)
60+
float3x4 objToWorld3x4 = ObjectToWorld3x4();
61+
float4x4 objToWorld = float4x4(
62+
objToWorld3x4._m00, objToWorld3x4._m01, objToWorld3x4._m02, objToWorld3x4._m03,
63+
objToWorld3x4._m10, objToWorld3x4._m11, objToWorld3x4._m12, objToWorld3x4._m13,
64+
objToWorld3x4._m20, objToWorld3x4._m21, objToWorld3x4._m22, objToWorld3x4._m23,
65+
0,0,0,1);
66+
return objToWorld;
67+
#else
68+
// NOTE: If using the new generation path, explicitly call the object matrix (since the particle matrix is now baked into UNITY_MATRIX_M)
69+
#if defined(HAVE_VFX_MODIFICATION) && !defined(SHADER_STAGE_COMPUTE)
70+
return GetSGVFXUnityObjectToWorld();
71+
#else
72+
return GetObjectToWorldMatrix();
73+
#endif
74+
#endif
75+
}
76+
77+
float4x4 VFXGetWorldToObjectMatrix()
78+
{
79+
#if defined(SHADER_STAGE_RAY_TRACING)
80+
float3x4 worldToObj3x4 = WorldToObject3x4();
81+
float4x4 worldToObj = float4x4(
82+
worldToObj3x4._m00, worldToObj3x4._m01, worldToObj3x4._m02, worldToObj3x4._m03,
83+
worldToObj3x4._m10, worldToObj3x4._m11, worldToObj3x4._m12, worldToObj3x4._m13,
84+
worldToObj3x4._m20, worldToObj3x4._m21, worldToObj3x4._m22, worldToObj3x4._m23,
85+
0,0,0,1);
86+
return worldToObj;
87+
#else
88+
// NOTE: If using the new generation path, explicitly call the object matrix (since the particle matrix is now baked into UNITY_MATRIX_I_M)
89+
#if defined(HAVE_VFX_MODIFICATION) && !defined(SHADER_STAGE_COMPUTE)
90+
return GetSGVFXUnityWorldToObject();
91+
#else
92+
return GetWorldToObjectMatrix();
93+
#endif
94+
#endif
95+
}
96+
5797
float4 VFXTransformPositionWorldToClip(float3 posWS)
5898
{
5999
#if VFX_WORLD_SPACE
@@ -64,7 +104,7 @@ float4 VFXTransformPositionWorldToClip(float3 posWS)
64104

65105
float4 VFXTransformPositionObjectToNonJitteredClip(float3 posOS)
66106
{
67-
float3 posWS = TransformObjectToWorld(posOS);
107+
float3 posWS = mul(VFXGetObjectToWorldMatrix(), float4(posOS,1)).xyz;
68108
return mul(UNITY_MATRIX_UNJITTERED_VP, float4(posWS, 1.0f));
69109
}
70110

@@ -81,7 +121,7 @@ float3 VFXTransformPreviousObjectToWorld(float3 posOS)
81121

82122
float4 VFXTransformPositionObjectToClip(float3 posOS)
83123
{
84-
float3 posWS = TransformObjectToWorld(posOS);
124+
float3 posWS = mul(VFXGetObjectToWorldMatrix(), float4(posOS,1)).xyz;
85125
return VFXTransformPositionWorldToClip(posWS);
86126
}
87127

@@ -102,46 +142,6 @@ float3 VFXTransformPositionWorldToCameraRelative(float3 posWS)
102142
#endif
103143
}
104144

105-
float4x4 VFXGetObjectToWorldMatrix()
106-
{
107-
#if defined(SHADER_STAGE_RAY_TRACING)
108-
float3x4 objToWorld3x4 = ObjectToWorld3x4();
109-
float4x4 objToWorld = float4x4(
110-
objToWorld3x4._m00, objToWorld3x4._m01, objToWorld3x4._m02, objToWorld3x4._m03,
111-
objToWorld3x4._m10, objToWorld3x4._m11, objToWorld3x4._m12, objToWorld3x4._m13,
112-
objToWorld3x4._m20, objToWorld3x4._m21, objToWorld3x4._m22, objToWorld3x4._m23,
113-
0,0,0,1);
114-
return objToWorld;
115-
#else
116-
// NOTE: If using the new generation path, explicitly call the object matrix (since the particle matrix is now baked into UNITY_MATRIX_M)
117-
#if defined(HAVE_VFX_MODIFICATION) && !defined(SHADER_STAGE_COMPUTE)
118-
return GetSGVFXUnityObjectToWorld();
119-
#else
120-
return GetObjectToWorldMatrix();
121-
#endif
122-
#endif
123-
}
124-
125-
float4x4 VFXGetWorldToObjectMatrix()
126-
{
127-
#if defined(SHADER_STAGE_RAY_TRACING)
128-
float3x4 worldToObj3x4 = WorldToObject3x4();
129-
float4x4 worldToObj = float4x4(
130-
worldToObj3x4._m00, worldToObj3x4._m01, worldToObj3x4._m02, worldToObj3x4._m03,
131-
worldToObj3x4._m10, worldToObj3x4._m11, worldToObj3x4._m12, worldToObj3x4._m13,
132-
worldToObj3x4._m20, worldToObj3x4._m21, worldToObj3x4._m22, worldToObj3x4._m23,
133-
0,0,0,1);
134-
return worldToObj;
135-
#else
136-
// NOTE: If using the new generation path, explicitly call the object matrix (since the particle matrix is now baked into UNITY_MATRIX_I_M)
137-
#if defined(HAVE_VFX_MODIFICATION) && !defined(SHADER_STAGE_COMPUTE)
138-
return GetSGVFXUnityWorldToObject();
139-
#else
140-
return GetWorldToObjectMatrix();
141-
#endif
142-
#endif
143-
}
144-
145145
float3x3 VFXGetWorldToViewRotMatrix()
146146
{
147147
return (float3x3)GetWorldToViewMatrix();

Packages/com.unity.render-pipelines.universal/Runtime/VFXGraph/Shaders/VFXCommon.hlsl

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,26 @@ void VFXEncodeMotionVector(float2 velocity, out float4 outBuffer)
3333
outBuffer = float4(velocity.xy, 0, 0);
3434
}
3535

36+
float4x4 VFXGetObjectToWorldMatrix()
37+
{
38+
// NOTE: If using the new generation path, explicitly call the object matrix (since the particle matrix is now baked into UNITY_MATRIX_M)
39+
#if defined(HAVE_VFX_MODIFICATION) && !defined(SHADER_STAGE_COMPUTE)
40+
return GetSGVFXUnityObjectToWorld();
41+
#else
42+
return GetObjectToWorldMatrix();
43+
#endif
44+
}
45+
46+
float4x4 VFXGetWorldToObjectMatrix()
47+
{
48+
// NOTE: If using the new generation path, explicitly call the object matrix (since the particle matrix is now baked into UNITY_MATRIX_I_M)
49+
#if defined(HAVE_VFX_MODIFICATION) && !defined(SHADER_STAGE_COMPUTE)
50+
return GetSGVFXUnityWorldToObject();
51+
#else
52+
return GetWorldToObjectMatrix();
53+
#endif
54+
}
55+
3656
float4 VFXTransformPositionWorldToClip(float3 posWS)
3757
{
3858
return TransformWorldToHClip(posWS);
@@ -50,13 +70,13 @@ float4 VFXTransformPositionWorldToPreviousClip(float3 posWS)
5070

5171
float4 VFXTransformPositionObjectToClip(float3 posOS)
5272
{
53-
float3 posWS = TransformObjectToWorld(posOS);
73+
float3 posWS = mul(VFXGetObjectToWorldMatrix(), float4(posOS,1)).xyz;
5474
return VFXTransformPositionWorldToClip(posWS);
5575
}
5676

5777
float4 VFXTransformPositionObjectToNonJitteredClip(float3 posOS)
5878
{
59-
float3 posWS = TransformObjectToWorld(posOS);
79+
float3 posWS = mul(VFXGetObjectToWorldMatrix(), float4(posOS,1)).xyz;
6080
return VFXTransformPositionWorldToNonJitteredClip(posWS);
6181
}
6282

@@ -95,26 +115,6 @@ float4x4 ApplyCameraTranslationToInverseMatrix(float4x4 inverseModelMatrix)
95115
}
96116
//End of compatibility functions
97117

98-
float4x4 VFXGetObjectToWorldMatrix()
99-
{
100-
// NOTE: If using the new generation path, explicitly call the object matrix (since the particle matrix is now baked into UNITY_MATRIX_M)
101-
#if defined(HAVE_VFX_MODIFICATION) && !defined(SHADER_STAGE_COMPUTE)
102-
return GetSGVFXUnityObjectToWorld();
103-
#else
104-
return GetObjectToWorldMatrix();
105-
#endif
106-
}
107-
108-
float4x4 VFXGetWorldToObjectMatrix()
109-
{
110-
// NOTE: If using the new generation path, explicitly call the object matrix (since the particle matrix is now baked into UNITY_MATRIX_I_M)
111-
#if defined(HAVE_VFX_MODIFICATION) && !defined(SHADER_STAGE_COMPUTE)
112-
return GetSGVFXUnityWorldToObject();
113-
#else
114-
return GetWorldToObjectMatrix();
115-
#endif
116-
}
117-
118118
float3x3 VFXGetWorldToViewRotMatrix()
119119
{
120120
return (float3x3)GetWorldToViewMatrix();

Packages/com.unity.visualeffectgraph/Editor/ShaderGraph/Templates/VFXConfig.template.hlsl

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ void SetupVFXMatrices(AttributesElement element, inout VFX_SRP_VARYINGS output)
284284

285285
#if VFX_LOCAL_SPACE
286286
elementToWorld = mul(GetSGVFXUnityObjectToWorld(), elementToWorld);
287-
#else
287+
#elif !defined(VFX_HAS_PICKING_MATRIX_CORRECTION)
288288
elementToWorld = ApplyCameraTranslationToMatrix(elementToWorld);
289289
#endif
290290

@@ -301,18 +301,10 @@ void SetupVFXMatrices(AttributesElement element, inout VFX_SRP_VARYINGS output)
301301

302302
#if VFX_LOCAL_SPACE
303303
worldToElement = mul(worldToElement,GetSGVFXUnityWorldToObject());
304-
#else
304+
#elif !defined(VFX_HAS_PICKING_MATRIX_CORRECTION)
305305
worldToElement = ApplyCameraTranslationToInverseMatrix(worldToElement);
306306
#endif
307307

308-
#if VFX_APPLY_CAMERA_POSITION_IN_ELEMENT_MATRIX
309-
//Specific to PickingSpaceTransforms.hlsl (in HDRP so far)
310-
//SHADEROPTIONS_CAMERA_RELATIVE_RENDERING has been undef at this stage
311-
//Avoid removing twice _WorldSpaceCameraPos
312-
elementToWorld = RevertCameraTranslationFromMatrix(elementToWorld);
313-
worldToElement = RevertCameraTranslationFromInverseMatrix(worldToElement);
314-
#endif
315-
316308
// Pack matrices into interpolator if requested by any node.
317309
#ifdef VARYINGS_NEED_ELEMENT_TO_WORLD
318310
output.elementToWorld0 = elementToWorld[0];

Packages/com.unity.visualeffectgraph/Shaders/VFXMatricesOverride.hlsl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,11 @@ static float4x4 vfxWorldToLocal;
3030
#else //SHADER_STAGE_COMPUTE
3131

3232
//Store the previous definition of UNITY_MATRIX_M/I_M
33-
float4x4 GetSGVFXUnityObjectToWorld() { return UNITY_MATRIX_M; }
34-
float4x4 GetSGVFXUnityWorldToObject() { return UNITY_MATRIX_I_M; }
33+
float4x4 GetSGVFXUnityObjectToWorldBackup() { return UNITY_MATRIX_M; }
34+
float4x4 GetSGVFXUnityWorldToObjectBackup() { return UNITY_MATRIX_I_M; }
35+
36+
float4x4 GetSGVFXUnityObjectToWorld() { return GetSGVFXUnityObjectToWorldBackup(); }
37+
float4x4 GetSGVFXUnityWorldToObject() { return GetSGVFXUnityWorldToObjectBackup(); }
3538

3639
// Abstraction of Unity matrices for VFX element/particles.
3740
#undef UNITY_MATRIX_M

0 commit comments

Comments
 (0)