Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inverted Hull Outline: Material Override #196

Merged
merged 8 commits into from
Dec 28, 2023
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
namespace DELTation.ToonRP.Editor.ShaderGUI.ShaderGraph
{
public class ToonRpShaderGraphInvertedHullOutlineShaderGui : ToonRpShaderGraphShaderGuiBase { }
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
using System;
using DELTation.ToonRP.Editor.ShaderGraph.Targets;
using UnityEditor;
using UnityEditor.ShaderGraph;
using UnityEngine.Rendering;

namespace DELTation.ToonRP.Editor.ShaderGraph.AssetCallbacks
{
internal static class CreateInvertedHullOutlineShaderGraph
{
[MenuItem("Assets/Create/Shader Graph/Toon RP/Inverted Hull Outline Shader Graph",
priority = CoreUtils.Priorities.assetsCreateShaderMenuPriority + 3
)]
public static void CreateInvertedHullOutlineGraph()
{
var target = (ToonTarget) Activator.CreateInstance(typeof(ToonTarget));
target.TrySetActiveSubTarget(typeof(ToonInvertedHullOutlineSubTarget));

BlockFieldDescriptor[] blockDescriptors =
{
ToonBlockFields.VertexDescription.Position,
ToonBlockFields.VertexDescription.Normal,
ToonBlockFields.VertexDescription.OutlineThickness,

ToonBlockFields.SurfaceDescription.Albedo,
ToonBlockFields.SurfaceDescription.Emission,
};

GraphUtil.CreateNewGraphWithOutputs(new Target[] { target }, blockDescriptors);
}
}
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ PackedVaryings VS(Attributes input)
{
// ReSharper disable once CppRedundantCastExpression
Varyings output = (Varyings) 0;
output = BuildVaryings(input);
VertexDescription vertexDescription;
float3 positionWs, normalWs;
output = BuildVaryings(input, vertexDescription, positionWs, normalWs);
PackedVaryings packedOutput = PackVaryings(output);
return packedOutput;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ PackedVaryings VS(Attributes input)
{
// ReSharper disable once CppRedundantCastExpression
Varyings output = (Varyings) 0;
output = BuildVaryings(input);
VertexDescription vertexDescription;
float3 positionWs, normalWs;
output = BuildVaryings(input, vertexDescription, positionWs, normalWs);
PackedVaryings packedOutput = PackVaryings(output);
return packedOutput;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ PackedVaryings VS(Attributes input)
{
// ReSharper disable once CppRedundantCastExpression
Varyings output = (Varyings) 0;
output = BuildVaryings(input);
VertexDescription vertexDescription;
float3 positionWs, normalWs;
output = BuildVaryings(input, vertexDescription, positionWs, normalWs);
PackedVaryings packedOutput = PackVaryings(output);
return packedOutput;
}
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "Packages/com.deltation.toon-rp/ShaderLibrary/Common.hlsl"
#include "Packages/com.deltation.toon-rp/ShaderLibrary/Fog.hlsl"
#include "Packages/com.deltation.toon-rp/Shaders/Extensions/ToonRPInvertedHullOutlineCommon.hlsl"

#include "Packages/com.deltation.toon-rp/ShaderLibrary/ShaderGraphForwardDeclarations.hlsl"

PackedVaryings VS(Attributes input)
{
// ReSharper disable once CppRedundantCastExpression
Varyings output = (Varyings) 0;
VertexDescription vertexDescription;
float3 positionWs, normalWs;
output = BuildVaryings(input, vertexDescription, positionWs, normalWs);

PackedVaryings packedOutput = PackVaryings(output);
return packedOutput;
}

float4 PS(PackedVaryings packedInput) : SV_TARGET
{
Varyings unpacked = UnpackVaryings(packedInput);
UNITY_SETUP_INSTANCE_ID(unpacked);

const SurfaceDescription surfaceDescription = BuildSurfaceDescription(unpacked);

#ifdef SHADERGRAPH_PREVIEW
float3 passColor = 1;
#else // !SHADERGRAPH_PREVIEW
float3 passColor = _ToonRpInvertedHullOutline_Color;
#endif // !SHADERGRAPH_PREVIEW

float3 albedo = 1.0f;
albedo.rgb = surfaceDescription.Albedo * passColor;

const float3 emission = surfaceDescription.Emission;
float3 outputColor = albedo + emission;

#if !_FORCE_DISABLE_FOG
const float fogFactor = unpacked.fogFactorAndVertexLight.x;
outputColor = MixFog(outputColor.rgb, fogFactor);
#endif // !_FORCE_DISABLE_FOG

ApplyCustomFog(outputColor, surfaceDescription);

return float4(outputColor, 1);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "Packages/com.deltation.toon-rp/ShaderLibrary/Common.hlsl"
#include "Packages/com.deltation.toon-rp/ShaderLibrary/MotionVectors.hlsl"
#include "Packages/com.deltation.toon-rp/Shaders/Extensions/ToonRPInvertedHullOutlineCommon.hlsl"

#include "Packages/com.deltation.toon-rp/ShaderLibrary/ShaderGraphForwardDeclarations.hlsl"

PackedVaryings VS(Attributes input)
{
// ReSharper disable once CppRedundantCastExpression
Varyings output = (Varyings) 0;
VertexDescription vertexDescription;
float3 positionWs, normalWs;
output = BuildVaryings(input, vertexDescription, positionWs, normalWs);

const float2 uv = input.uv0.xy;

{
const float thickness = ComputeThickness(uv, positionWs, normalWs);
output.positionCsNoJitter = ApplyThicknessAndTransformToHClip(_NonJitteredViewProjMatrix, positionWs, normalWs, thickness);
}

{
const float3 previousPositionOs = UseLastFramePositions() ? input.positionOld : input.positionOS;
// ReSharper disable once CppLocalVariableMayBeConst
float3 previousPositionWs = mul(UNITY_PREV_MATRIX_M, float4(previousPositionOs, 1)).xyz;
const float thickness = ComputeThickness(uv, positionWs, normalWs);
output.previousPositionCsNoJitter = ApplyThicknessAndTransformToHClip(_PrevViewProjMatrix, previousPositionWs, normalWs, thickness);
}

ApplyMotionVectorZBias(output.positionCS);

PackedVaryings packedOutput = PackVaryings(output);
return packedOutput;
}

float4 PS(PackedVaryings packedInput) : SV_TARGET
{
Varyings unpacked = UnpackVaryings(packedInput);
UNITY_SETUP_INSTANCE_ID(unpacked);

const SurfaceDescription surfaceDescription = BuildSurfaceDescription(unpacked);

return float4(CalcNdcMotionVectorFromCsPositions(unpacked.positionCsNoJitter, unpacked.previousPositionCsNoJitter),
0, 0);
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef TOON_RP_INVERTED_HULL_OUTLINE_PRE_GRAPH
#define TOON_RP_INVERTED_HULL_OUTLINE_PRE_GRAPH

#include "Packages/com.deltation.toon-rp/Shaders/Extensions/ToonRPInvertedHullOutlineCommon.hlsl"

float4 InvertedHullOutline_TransformWorldToHClip(const float3 positionWs, const float3 normalWs, const float2 uv, const float thicknessMultiplier)
{
const float thickness = ComputeThickness(uv, positionWs, normalWs) * thicknessMultiplier;
const float4 positionCs = ApplyThicknessAndTransformToHClip(positionWs, normalWs, thickness);
return positionCs;
}

#define TRANSFORM_WORLD_TO_HCLIP(positionWs, normalWs, appdata, vertexDescription) (InvertedHullOutline_TransformWorldToHClip(positionWs, normalWs, appdata.uv0.xy, vertexDescription.OutlineThickness))

#ifdef _NORMAL_SEMANTIC_TANGENT
#define SHADER_GRAPH_NORMAL_SOURCE_TANGENT
#endif // _NORMAL_SEMANTIC_TANGENT

#ifdef _NORMAL_SEMANTIC_UV2
#define SHADER_GRAPH_NORMAL_SOURCE_UV2
#endif // _NORMAL_SEMANTIC_UV2

#endif // TOON_RP_INVERTED_HULL_OUTLINE_PRE_GRAPH

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@ PackedVaryings VS(Attributes input)
{
// ReSharper disable once CppRedundantCastExpression
Varyings output = (Varyings) 0;
output = BuildVaryings(input);
VertexDescription vertexDescription;
float3 positionWs, normalWs;
output = BuildVaryings(input, vertexDescription, positionWs, normalWs);

const float3 positionWs = TransformObjectToWorld(input.positionOS);
output.positionCsNoJitter = mul(_NonJitteredViewProjMatrix, float4(positionWs, 1));

const float3 previousPosition = UseLastFramePositions() ? input.positionOld : input.positionOS;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ PackedVaryings VS(Attributes input)
{
// ReSharper disable once CppRedundantCastExpression
Varyings output = (Varyings) 0;
output = BuildVaryings(input);
VertexDescription vertexDescription;
float3 positionWs, normalWs;
output = BuildVaryings(input, vertexDescription, positionWs, normalWs);
PackedVaryings packedOutput = PackVaryings(output);
return packedOutput;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ PackedVaryings VS(Attributes input)
{
// ReSharper disable once CppRedundantCastExpression
Varyings output = (Varyings) 0;
output = BuildVaryings(input);
VertexDescription vertexDescription;
float3 positionWs, normalWs;
output = BuildVaryings(input, vertexDescription, positionWs, normalWs);
PackedVaryings packedOutput = PackVaryings(output);
return packedOutput;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ PackedVaryings VS(Attributes input)
{
// ReSharper disable once CppRedundantCastExpression
Varyings output = (Varyings) 0;
output = BuildVaryings(input);
VertexDescription vertexDescription;
float3 positionWs, normalWs;
output = BuildVaryings(input, vertexDescription, positionWs, normalWs);
PackedVaryings packedOutput = PackVaryings(output);
return packedOutput;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,11 @@ VertexDescription BuildVertexDescription(Attributes input)
#endif
#endif

Varyings BuildVaryings(Attributes input)
#ifndef TRANSFORM_WORLD_TO_HCLIP
#define TRANSFORM_WORLD_TO_HCLIP(positionWS, normalWS, appdata, vertexDescription) TransformWorldToHClip(positionWS)
#endif // TRANSFORM_WORLD_TO_HCLIP

Varyings BuildVaryings(Attributes input, out VertexDescription vertexDescription, out float3 positionWS, out float3 normalWS)
{
// ReSharper disable once CppRedundantCastExpression
Varyings output = (Varyings) 0;
Expand All @@ -35,7 +39,7 @@ Varyings BuildVaryings(Attributes input)

#if defined(FEATURES_GRAPH_VERTEX)

VertexDescription vertexDescription = BuildVertexDescription(input);
vertexDescription = BuildVertexDescription(input);

#if defined(CUSTOMINTERPOLATOR_VARYPASSTHROUGH_FUNC)
CustomInterpolatorPassThroughFunc(output, vertexDescription);
Expand All @@ -50,19 +54,33 @@ Varyings BuildVaryings(Attributes input)
input.tangentOS.xyz = vertexDescription.Tangent.xyz;
#endif //FEATURES GRAPH TANGENT

#else // !FEATURES_GRAPH_VERTEX

vertexDescription = (VertexDescription) 0;

#endif //FEATURES_GRAPH_VERTEX

// TODO: Avoid path via VertexPositionInputs (Universal)
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);

// Returns the camera relative position (if enabled)
float3 positionWS = TransformObjectToWorld(input.positionOS);
positionWS = TransformObjectToWorld(input.positionOS);

#ifdef ATTRIBUTES_NEED_NORMAL
float3 normalWS = TransformObjectToWorldNormal(input.normalOS);

float3 normalOS;
#if defined(SHADER_GRAPH_NORMAL_SOURCE_TANGENT)
normalOS = input.tangentOS.xyz;
#elif defined (SHADER_GRAPH_NORMAL_SOURCE_UV2)
normalOS = input.uv2.xyz;
#else
normalOS = input.normalOS.xyz;
#endif

normalWS = TransformObjectToWorldNormal(normalOS);
#else
// Required to compile ApplyVertexModification that doesn't use normal.
float3 normalWS = float3(0.0, 0.0, 0.0);
normalWS = float3(0.0, 0.0, 0.0);
#endif

#ifdef ATTRIBUTES_NEED_TANGENT
Expand Down Expand Up @@ -90,7 +108,7 @@ Varyings BuildVaryings(Attributes input)
#if (SHADERPASS == SHADERPASS_SHADOWCASTER)

positionWS = ApplyShadowBias(positionWS, normalWS, _DirectionalLightDirection);
output.positionCS = TransformWorldToHClip(positionWS);
output.positionCS = TRANSFORM_WORLD_TO_HCLIP(positionWS, normalWS, input, vertexDescription);
#if UNITY_REVERSED_Z
output.positionCS.z = min(output.positionCS.z, UNITY_NEAR_CLIP_VALUE);
#else
Expand All @@ -102,7 +120,7 @@ Varyings BuildVaryings(Attributes input)
#endif // _TOON_RP_VSM

#else
output.positionCS = TransformWorldToHClip(positionWS);
output.positionCS = TRANSFORM_WORLD_TO_HCLIP(positionWS, normalWS, input, vertexDescription);
#endif

#if defined(VARYINGS_NEED_TEXCOORD0) || defined(VARYINGS_DS_NEED_TEXCOORD0)
Expand Down
Loading