Skip to content

Commit

Permalink
Shader Graph: shadow receive position offset [closes #205]
Browse files Browse the repository at this point in the history
  • Loading branch information
Delt06 committed Jan 3, 2024
1 parent aba3d58 commit 9be70a1
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public static void CreateLitGraph()
ToonBlockFields.SurfaceDescription.GlobalRampUV,
ToonBlockFields.SurfaceDescription.ShadowColor,
ToonBlockFields.SurfaceDescription.DiffuseOffset,
ToonBlockFields.SurfaceDescription.ShadowReceivePositionOffset,
ToonBlockFields.SurfaceDescription.SpecularColor,
ToonBlockFields.SurfaceDescription.SpecularSizeOffset,
ToonBlockFields.SurfaceDescription.RimColor,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ float4 PS(PackedVaryings packedInput) : SV_TARGET
lightComputationParameters.albedo = albedo;
lightComputationParameters.shadowColor = surfaceDescription.ShadowColor;
lightComputationParameters.diffuseOffset = surfaceDescription.DiffuseOffset;
lightComputationParameters.shadowReceivePositionOffset = surfaceDescription.ShadowReceivePositionOffset;

#if _TOON_LIGHTING_SPECULAR
lightComputationParameters.specularSizeOffset = surfaceDescription.SpecularSizeOffset;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public override void GetActiveBlocks(ref TargetActiveBlockContext context)
context.AddBlock(ToonBlockFields.SurfaceDescription.GlobalRampUV, !OverrideRamp);
context.AddBlock(ToonBlockFields.SurfaceDescription.ShadowColor);
context.AddBlock(ToonBlockFields.SurfaceDescription.DiffuseOffset);
context.AddBlock(ToonBlockFields.SurfaceDescription.ShadowReceivePositionOffset);
}

public override void CollectShaderProperties(PropertyCollector collector, GenerationMode generationMode)
Expand Down Expand Up @@ -412,6 +413,7 @@ private static class DefaultBlockMasks
ToonBlockFields.SurfaceDescription.GlobalRampUV,
ToonBlockFields.SurfaceDescription.ShadowColor,
ToonBlockFields.SurfaceDescription.DiffuseOffset,
ToonBlockFields.SurfaceDescription.ShadowReceivePositionOffset,

ToonBlockFields.SurfaceDescription.OverrideRampThreshold,
ToonBlockFields.SurfaceDescription.OverrideRampSmoothness,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ public struct VertexDescription
public static readonly BlockFieldDescriptor Tangent = new(Name, "Tangent", "VERTEXDESCRIPTION_TANGENT",
new TangentControl(CoordinateSpace.Object), ShaderStage.Vertex
);

public static readonly BlockFieldDescriptor OutlineThickness = new(Name, "OutlineThickness", "VERTEXDESCRIPTION_OUTLINE_THICKNESS",

public static readonly BlockFieldDescriptor OutlineThickness = new(Name, "OutlineThickness",
"VERTEXDESCRIPTION_OUTLINE_THICKNESS",
new FloatControl(1.0f), ShaderStage.Vertex
);
}
Expand Down Expand Up @@ -102,6 +103,12 @@ public struct SurfaceDescription
"SURFACEDESCRIPTION_DIFFUSEOFFSET",
new FloatControl(0.0f), ShaderStage.Fragment
);
public static readonly BlockFieldDescriptor ShadowReceivePositionOffset = new(Name,
"ShadowReceivePositionOffset",
"Shadow Receive Position Offset",
"SURFACEDESCRIPTION_SHADOWRECEIVEPOSITIONOFFSET",
new Vector3Control(Vector3.zero), ShaderStage.Fragment
);

public static readonly BlockFieldDescriptor RimColor = new(Name, "RimColor", "Rim Color",
"SURFACEDESCRIPTION_RIMCOLOR",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ struct LightComputationParameters
float4 albedo;
float4 shadowColor;
float diffuseOffset;
float3 shadowReceivePositionOffset;
float3 specularColor;
float specularSizeOffset;

Expand Down Expand Up @@ -109,17 +110,19 @@ float GetShadowAttenuation(const LightComputationParameters parameters, const Li

Light GetMainLight(const LightComputationParameters parameters)
{
const float3 shadowPositionWs = parameters.positionWs + parameters.shadowReceivePositionOffset;

#ifdef _TOON_RP_SHADOW_MAPS
const uint tileIndex = ComputeShadowTileIndex(parameters.positionWs);
const float3 shadowCoords = TransformWorldToShadowCoords(parameters.positionWs, tileIndex);
const uint tileIndex = ComputeShadowTileIndex(shadowPositionWs);
const float3 shadowCoords = TransformWorldToShadowCoords(shadowPositionWs, tileIndex);
Light light = GetMainLight(shadowCoords, parameters.positionWs);
#else // !_TOON_RP_SHADOW_MAPS
Light light = GetMainLight();
#endif // _TOON_RP_SHADOW_MAPS

#if defined(_TOON_RP_BLOB_SHADOWS) && defined(_RECEIVE_BLOB_SHADOWS)

const float blobShadowAttenuation = SampleBlobShadowAttenuation(parameters.positionWs);
const float blobShadowAttenuation = SampleBlobShadowAttenuation(shadowPositionWs);
light.shadowAttenuation = blobShadowAttenuation;

#endif // _TOON_RP_BLOB_SHADOWS && _RECEIVE_BLOB_SHADOWS
Expand Down

0 comments on commit 9be70a1

Please sign in to comment.