Skip to content

Commit

Permalink
Added renderer bounds access in ShaderGraph
Browse files Browse the repository at this point in the history
  • Loading branch information
alelievr committed May 17, 2022
1 parent 8fd575f commit 6603f3c
Show file tree
Hide file tree
Showing 17 changed files with 2,182 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,10 @@
#endif
#endif

#if defined(UNITY_INSTANCED_RENDERER_BOUNDS)
#define UNITY_USE_RENDERER_BOUNDS
#endif

#if defined(UNITY_INSTANCED_SH) && !defined(LIGHTMAP_ON)
#if !defined(DYNAMICLIGHTMAP_ON)
#define UNITY_USE_SHCOEFFS_ARRAYS
Expand Down Expand Up @@ -345,6 +349,12 @@
UNITY_DEFINE_INSTANCED_PROP(float, unity_RenderingLayerArray)
#define unity_RenderingLayer UNITY_ACCESS_INSTANCED_PROP(unity_Builtins1, unity_RenderingLayerArray).xxxx
#endif
#if defined(UNITY_USE_RENDERER_BOUNDS)
UNITY_DEFINE_INSTANCED_PROP(float4, unity_RendererBounds_MinArray)
UNITY_DEFINE_INSTANCED_PROP(float4, unity_RendererBounds_MaxArray)
#define unity_RendererBounds_Min UNITY_ACCESS_INSTANCED_PROP(unity_Builtins1, unity_RendererBounds_MinArray)
#define unity_RendererBounds_Max UNITY_ACCESS_INSTANCED_PROP(unity_Builtins1, unity_RendererBounds_MaxArray)
#endif
UNITY_INSTANCING_BUFFER_END(unity_Builtins1)

UNITY_INSTANCING_BUFFER_START(PerDraw2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,3 +57,16 @@ Improvements have also been made to the raymarching algorithm to improve scatter
In the UI, **thickness** and **distortion** fields have been renamed to **density** and **wind**.

![](Images/cl-whats-new.png)

### Fullscreen Shader Graph

![](Images/HDRP-Fullscreen-Frost-Effect.png)

HDRP 14.0 introduces a new material type in ShaderGraph to create fullscreen effects.
Shaders of the fullscreen type can be used in fullscreen custom passes, custom post processes and C# scripting.

For more details on how to use fulscreen shaders, see [FullScreen Shader Graph](Fullscreen-Shader-Graph.md).

### Renderer bounds access in ShaderGraph

The [Object Node](https://docs.unity3d.com/Packages/com.unity.shadergraph@13.1/manual/Object-Node.html) in Shader Graph has been updated to give access to the bounds of the current object being rendered. This information can be useful to compute refraction effect and such. Note that these bounds are available in world space.
Original file line number Diff line number Diff line change
Expand Up @@ -93,5 +93,28 @@ float3 shadergraph_HDMainLightDirection()
#endif
#define SHADERGRAPH_MAIN_LIGHT_DIRECTION shadergraph_HDMainLightDirection

#ifdef SHADERGRAPH_RENDERER_BOUNDS_MIN
#undef SHADERGRAPH_RENDERER_BOUNDS_MIN
#endif
#define SHADERGRAPH_RENDERER_BOUNDS_MIN shadergraph_RendererBoundsWS_Min()

float3 shadergraph_RendererBoundsWS_Min()
{
float3 minBounds, maxBounds;
GetRendererBounds(minBounds, maxBounds);
return minBounds;
}

#ifdef SHADERGRAPH_RENDERER_BOUNDS_MAX
#undef SHADERGRAPH_RENDERER_BOUNDS_MAX
#endif
#define SHADERGRAPH_RENDERER_BOUNDS_MAX shadergraph_RendererBoundsWS_Max()

float3 shadergraph_RendererBoundsWS_Max()
{
float3 minBounds, maxBounds;
GetRendererBounds(minBounds, maxBounds);
return maxBounds;
}

#endif // UNITY_GRAPHFUNCTIONS_HD_INCLUDED
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,30 @@ void GetAbsoluteWorldRendererBounds(out float3 minBounds, out float3 maxBounds)
maxBounds = unity_RendererBounds_Max.xyz;
}

// Define Model Matrix Macro
// Note: In order to be able to define our macro to forbid usage of unity_ObjectToWorld/unity_WorldToObject/unity_MatrixPreviousM/unity_MatrixPreviousMI
// We need to declare inline function. Using uniform directly mean they are expand with the macro
float4x4 GetRawUnityObjectToWorld() { return unity_ObjectToWorld; }
float4x4 GetRawUnityWorldToObject() { return unity_WorldToObject; }
float4x4 GetRawUnityPrevObjectToWorld() { return unity_MatrixPreviousM; }
float4x4 GetRawUnityPrevWorldToObject() { return unity_MatrixPreviousMI; }

#define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(GetRawUnityObjectToWorld())
#define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(GetRawUnityWorldToObject())
#define UNITY_PREV_MATRIX_M ApplyCameraTranslationToMatrix(GetRawUnityPrevObjectToWorld())
#define UNITY_PREV_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(GetRawUnityPrevWorldToObject())

#else

// Not yet supported by BRG
void GetAbsoluteWorldRendererBounds(out float3 minBounds, out float3 maxBounds)
{
minBounds = 0;
maxBounds = 0;
}

#endif

void GetRendererBounds(out float3 minBounds, out float3 maxBounds)
{
GetAbsoluteWorldRendererBounds(minBounds, maxBounds);
Expand All @@ -500,21 +524,6 @@ float3 GetRendererExtents()
return (maxBounds - minBounds) * 0.5;
}

// Define Model Matrix Macro
// Note: In order to be able to define our macro to forbid usage of unity_ObjectToWorld/unity_WorldToObject/unity_MatrixPreviousM/unity_MatrixPreviousMI
// We need to declare inline function. Using uniform directly mean they are expand with the macro
float4x4 GetRawUnityObjectToWorld() { return unity_ObjectToWorld; }
float4x4 GetRawUnityWorldToObject() { return unity_WorldToObject; }
float4x4 GetRawUnityPrevObjectToWorld() { return unity_MatrixPreviousM; }
float4x4 GetRawUnityPrevWorldToObject() { return unity_MatrixPreviousMI; }

#define UNITY_MATRIX_M ApplyCameraTranslationToMatrix(GetRawUnityObjectToWorld())
#define UNITY_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(GetRawUnityWorldToObject())
#define UNITY_PREV_MATRIX_M ApplyCameraTranslationToMatrix(GetRawUnityPrevObjectToWorld())
#define UNITY_PREV_MATRIX_I_M ApplyCameraTranslationToInverseMatrix(GetRawUnityPrevWorldToObject())

#endif

// To get instancing working, we must use UNITY_MATRIX_M/UNITY_MATRIX_I_M/UNITY_PREV_MATRIX_M/UNITY_PREV_MATRIX_I_M as UnityInstancing.hlsl redefine them
#define unity_ObjectToWorld Use_Macro_UNITY_MATRIX_M_instead_of_unity_ObjectToWorld
#define unity_WorldToObject Use_Macro_UNITY_MATRIX_I_M_instead_of_unity_WorldToObject
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@
#define SHADERGRAPH_AMBIENT_EQUATOR unity_AmbientEquator
#define SHADERGRAPH_AMBIENT_GROUND unity_AmbientGround
#define SHADERGRAPH_MAIN_LIGHT_DIRECTION shadergraph_URPMainLightDirection

#define SHADERGRAPH_RENDERER_BOUNDS_MIN shadergraph_RendererBoundsWS_Min()
#define SHADERGRAPH_RENDERER_BOUNDS_MAX shadergraph_RendererBoundsWS_Max()

#if defined(REQUIRE_DEPTH_TEXTURE)
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
Expand Down Expand Up @@ -116,6 +117,16 @@ float3 shadergraph_URPMainLightDirection()
return -GetMainLight().direction;
}

float3 shadergraph_RendererBoundsWS_Min()
{
return GetCameraRelativePositionWS(unity_RendererBounds_Min.xyz);
}

float3 shadergraph_RendererBoundsWS_Max()
{
return GetCameraRelativePositionWS(unity_RendererBounds_Max.xyz);
}

// Always include Shader Graph version
// Always include last to avoid double macros
#include "Packages/com.unity.shadergraph/ShaderGraphLibrary/Functions.hlsl"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,10 @@ real4 unity_SHBg;
real4 unity_SHBb;
real4 unity_SHC;

// Renderer bounding box.
float4 unity_RendererBounds_Min;
float4 unity_RendererBounds_Max;

// Velocity
float4x4 unity_MatrixPreviousM;
float4x4 unity_MatrixPreviousMI;
Expand Down
8 changes: 8 additions & 0 deletions Packages/com.unity.shadergraph/Documentation~/Object-Node.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ Note: The behaviour of the Position [Port](Port.md) can be defined per Render Pi
|:------------ |:-------------|:-----|:---|:---|
| Position | Output | Vector 3 | None | Object position in world space |
| Scale | Output | Vector 3 | None | Object scale in world space |
| World Bounds Min | Output | Vector 3 | None | Minimum value of the renderer bounds in world space |
| World Bounds Max | Output | Vector 3 | None | Maximum value of the renderer bounds in world space |
| Bounds Size | Output | Vector 3 | None | Size of the renderer bounds |

Note: the bounds values are the equivalent of [the bounds in the renderer component](https://docs.unity3d.com/ScriptReference/Renderer-bounds.html). This means that vertex deformation done in ShaderGraph doesn't affect these values.

## Generated Code Example

Expand All @@ -26,4 +31,7 @@ float3 _Object_Position = SHADERGRAPH_OBJECT_POSITION;
float3 _Object_Scale = float3(length(float3(UNITY_MATRIX_M[0].x, UNITY_MATRIX_M[1].x, UNITY_MATRIX_M[2].x)),
length(float3(UNITY_MATRIX_M[0].y, UNITY_MATRIX_M[1].y, UNITY_MATRIX_M[2].y)),
length(float3(UNITY_MATRIX_M[0].z, UNITY_MATRIX_M[1].z, UNITY_MATRIX_M[2].z)));
float3 _Object_WorldBoundsMin = SHADERGRAPH_RENDERER_BOUNDS_MIN;
float3 _Object_WorldBoundsMax = SHADERGRAPH_RENDERER_BOUNDS_MAX;
float3 _Object_BoundsSize = (SHADERGRAPH_RENDERER_BOUNDS_MAX - SHADERGRAPH_RENDERER_BOUNDS_MIN);
```
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,31 @@ sealed class ObjectNode : AbstractMaterialNode, IMayRequireTransform
{
const string kOutputSlotName = "Position";
const string kOutputSlot1Name = "Scale";
const string kOutputSlot2Name = "World Bounds Min";
const string kOutputSlot3Name = "World Bounds Max";
const string kOutputSlot4Name = "Bounds Size";

public const int OutputSlotId = 0;
public const int OutputSlot1Id = 1;
public const int OutputSlot2Id = 2;
public const int OutputSlot3Id = 3;
public const int OutputSlot4Id = 4;

public ObjectNode()
{
name = "Object";
synonyms = new string[] { "position", "scale" };
synonyms = new string[] { "position", "scale", "bounds", "size" };
UpdateNodeAfterDeserialization();
}

public override void UpdateNodeAfterDeserialization()
{
AddSlot(new Vector3MaterialSlot(OutputSlotId, kOutputSlotName, kOutputSlotName, SlotType.Output, Vector3.zero));
AddSlot(new Vector3MaterialSlot(OutputSlot1Id, kOutputSlot1Name, kOutputSlot1Name, SlotType.Output, Vector3.zero));
RemoveSlotsNameNotMatching(new[] { OutputSlotId, OutputSlot1Id });
AddSlot(new Vector3MaterialSlot(OutputSlot2Id, kOutputSlot2Name, kOutputSlot2Name, SlotType.Output, Vector3.zero));
AddSlot(new Vector3MaterialSlot(OutputSlot3Id, kOutputSlot3Name, kOutputSlot3Name, SlotType.Output, Vector3.zero));
AddSlot(new Vector3MaterialSlot(OutputSlot4Id, kOutputSlot4Name, kOutputSlot4Name, SlotType.Output, Vector3.zero));
RemoveSlotsNameNotMatching(new[] { OutputSlotId, OutputSlot1Id, OutputSlot2Id, OutputSlot3Id, OutputSlot4Id });
}

public override string GetVariableNameForSlot(int slotId)
Expand All @@ -34,6 +43,12 @@ public override string GetVariableNameForSlot(int slotId)
return @"$precision3(length($precision3(UNITY_MATRIX_M[0].x, UNITY_MATRIX_M[1].x, UNITY_MATRIX_M[2].x)),
length($precision3(UNITY_MATRIX_M[0].y, UNITY_MATRIX_M[1].y, UNITY_MATRIX_M[2].y)),
length($precision3(UNITY_MATRIX_M[0].z, UNITY_MATRIX_M[1].z, UNITY_MATRIX_M[2].z)))";
case OutputSlot2Id:
return "SHADERGRAPH_RENDERER_BOUNDS_MIN";
case OutputSlot3Id:
return "SHADERGRAPH_RENDERER_BOUNDS_MAX";
case OutputSlot4Id:
return "(SHADERGRAPH_RENDERER_BOUNDS_MAX - SHADERGRAPH_RENDERER_BOUNDS_MIN)";
default:
return "SHADERGRAPH_OBJECT_POSITION";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#define SHADERGRAPH_AMBIENT_EQUATOR unity_AmbientEquator
#define SHADERGRAPH_AMBIENT_GROUND unity_AmbientGround
#define SHADERGRAPH_MAIN_LIGHT_DIRECTION shadergraph_MainLightDirection
#define SHADERGRAPH_RENDERER_BOUNDS_MIN shadergraph_RendererBoundsWS_Min()
#define SHADERGRAPH_RENDERER_BOUNDS_MAX shadergraph_RendererBoundsWS_Max()

#if defined(REQUIRE_DEPTH_TEXTURE)
#include "Packages/com.unity.shadergraph/Editor/Generation/Targets/BuiltIn/ShaderLibrary/DeclareDepthTexture.hlsl"
Expand Down Expand Up @@ -93,6 +95,16 @@ float3 shadergraph_MainLightDirection()
return 0.0f;
}

float3 shadergraph_RendererBoundsWS_Min()
{
return 0.0f;
}

float3 shadergraph_RendererBoundsWS_Max()
{
return 0.0f;
}

// Always include Shader Graph version
// Always include last to avoid double macros
#include "Packages/com.unity.shadergraph/ShaderGraphLibrary/Functions.hlsl"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ Gradient NewGradient(int type, int colorsLength, int alphasLength,
#define SHADERGRAPH_OBJECT_POSITION UNITY_MATRIX_M._m03_m13_m23
#endif

#ifndef SHADERGRAPH_RENDERER_BOUNDS_MIN
#define SHADERGRAPH_RENDERER_BOUNDS_MIN float3(0, 0, 0)
#endif
#ifndef SHADERGRAPH_RENDERER_BOUNDS_MAX
#define SHADERGRAPH_RENDERER_BOUNDS_MAX float3(0, 0, 0)
#endif

float shadergraph_SampleSceneDepth(float2 uv)
{
return 1;
Expand Down
Loading

0 comments on commit 6603f3c

Please sign in to comment.