Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,22 @@
// it return the offset to apply to the UVSet provide in PerPixelHeightDisplacementParam
// viewDirTS is view vector in texture space matching the UVSet
// ref: https://www.gamedev.net/resources/_/technical/graphics-programming-and-theory/a-closer-look-at-parallax-occlusion-mapping-r3262

#ifndef POM_USER_DATA_PARAMETERS
#define POM_USER_DATA_PARAMETERS
#endif

#ifndef POM_USER_DATA_ARGUMENTS
#define POM_USER_DATA_ARGUMENTS
#endif

real2
#ifdef POM_NAME_ID
MERGE_NAME(ParallaxOcclusionMapping,POM_NAME_ID)
#else
ParallaxOcclusionMapping
#endif
(real lod, real lodThreshold, int numSteps, real3 viewDirTS, PerPixelHeightDisplacementParam ppdParam, out real outHeight)
(real lod, real lodThreshold, int numSteps, real3 viewDirTS, PerPixelHeightDisplacementParam ppdParam, out real outHeight POM_USER_DATA_PARAMETERS)
{
// Convention: 1.0 is top, 0.0 is bottom - POM is always inward, no extrusion
real stepSize = 1.0 / (real)numSteps;
Expand All @@ -28,9 +37,9 @@ ParallaxOcclusionMapping

// Do a first step before the loop to init all value correctly
real2 texOffsetCurrent = real2(0.0, 0.0);
real prevHeight = ComputePerPixelHeightDisplacement(texOffsetCurrent, lod, ppdParam);
real prevHeight = ComputePerPixelHeightDisplacement(texOffsetCurrent, lod, ppdParam POM_USER_DATA_ARGUMENTS);
texOffsetCurrent += texOffsetPerStep;
real currHeight = ComputePerPixelHeightDisplacement(texOffsetCurrent, lod, ppdParam);
real currHeight = ComputePerPixelHeightDisplacement(texOffsetCurrent, lod, ppdParam POM_USER_DATA_ARGUMENTS);
real rayHeight = 1.0 - stepSize; // Start at top less one sample

// Linear search
Expand All @@ -45,7 +54,7 @@ ParallaxOcclusionMapping
texOffsetCurrent += texOffsetPerStep;

// Sample height map which in this case is stored in the alpha channel of the normal map:
currHeight = ComputePerPixelHeightDisplacement(texOffsetCurrent, lod, ppdParam);
currHeight = ComputePerPixelHeightDisplacement(texOffsetCurrent, lod, ppdParam POM_USER_DATA_ARGUMENTS);
}

// Found below and above points, now perform line interesection (ray) with piecewise linear heightfield approximation
Expand All @@ -71,7 +80,7 @@ ParallaxOcclusionMapping
// Retrieve offset require to find this intersectionHeight
offset = (1 - intersectionHeight) * texOffsetPerStep * numSteps;

currHeight = ComputePerPixelHeightDisplacement(offset, lod, ppdParam);
currHeight = ComputePerPixelHeightDisplacement(offset, lod, ppdParam POM_USER_DATA_ARGUMENTS);

delta = intersectionHeight - currHeight;

Expand Down Expand Up @@ -106,7 +115,7 @@ ParallaxOcclusionMapping
real ratio = delta0 / (delta0 + delta1);
real2 offset = texOffsetCurrent - ratio * texOffsetPerStep;

currHeight = ComputePerPixelHeightDisplacement(offset, lod, ppdParam);
currHeight = ComputePerPixelHeightDisplacement(offset, lod, ppdParam POM_USER_DATA_ARGUMENTS);

#endif

Expand Down
1 change: 1 addition & 0 deletions com.unity.shadergraph/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ The version number for this package has increased due to a version update of a r
- Fixed an issue where user setting a property to not Exposed, Hybrid-Instanced would result in a non-Hybrid Global property [1285700]
- Fixed an issue with Gradient when it is used as expose parameters. Generated code was failing [1285640 ]
- Fixed the subgraph slot sorting function [1286805]
- Fixed Parallax Occlusion Mapping not working in sub graphs. [1221317](https://issuetracker.unity3d.com/product/unity/issues/guid/1221317/)

## [10.1.0] - 2020-10-12

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,19 +111,23 @@ public void GenerateNodeFunction(FunctionRegistry registry, GenerationMode gener
registry.ProvideFunction(GetFunctionName(), s =>
{
s.AppendLine("// Required struct and function for the ParallaxOcclusionMapping function:");
s.AppendLine($"$precision ComputePerPixelHeightDisplacement_{GetVariableNameForNode()}($precision2 texOffsetCurrent, $precision lod, PerPixelHeightDisplacementParam param)");
s.AppendLine($"$precision ComputePerPixelHeightDisplacement_{GetVariableNameForNode()}($precision2 texOffsetCurrent, $precision lod, PerPixelHeightDisplacementParam param, TEXTURE2D_PARAM(heightTexture, heightSampler))");
using (s.BlockScope())
{
s.AppendLine("return SAMPLE_TEXTURE2D_LOD({0}, {1}, param.uv + texOffsetCurrent, lod).r;",
heightmap,
edgesSampler.Any() ? GetSlotValue(kHeightmapSamplerSlotId, generationMode) : "sampler" + heightmap);
s.AppendLine("return SAMPLE_TEXTURE2D_LOD(heightTexture, heightSampler, param.uv + texOffsetCurrent, lod).r;");
}
// heightmap,
// edgesSampler.Any() ? GetSlotValue(kHeightmapSamplerSlotId, generationMode) : "sampler" + heightmap);

s.AppendLine($"#define ComputePerPixelHeightDisplacement ComputePerPixelHeightDisplacement_{GetVariableNameForNode()}");
s.AppendLine($"#define POM_NAME_ID {GetVariableNameForNode()}");
s.AppendLine($"#define POM_USER_DATA_PARAMETERS , TEXTURE2D_PARAM(heightTexture, samplerState)");
s.AppendLine($"#define POM_USER_DATA_ARGUMENTS , TEXTURE2D_ARGS(heightTexture, samplerState)");
s.AppendLine(perPixelDisplacementInclude);
s.AppendLine($"#undef ComputePerPixelHeightDisplacement");
s.AppendLine($"#undef POM_NAME_ID");
s.AppendLine($"#undef POM_USER_DATA_PARAMETERS");
s.AppendLine($"#undef POM_USER_DATA_ARGUMENTS");
});
}

Expand All @@ -134,6 +138,8 @@ public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMo
string uvs = GetSlotValue(kUVsSlotId, generationMode);
string lod = GetSlotValue(kLodSlotId, generationMode);
string lodThreshold = GetSlotValue(kLodThresholdSlotId, generationMode);
string heightmap = GetSlotValue(kHeightmapSlotId, generationMode);
string sampler = GetSlotValue(kHeightmapSamplerSlotId, generationMode);

string tmpPOMParam = GetVariableNameForNode() + "_POM";
string tmpViewDir = GetVariableNameForNode() + "_ViewDir";
Expand All @@ -155,7 +161,7 @@ public void GenerateNodeCode(ShaderStringBuilder sb, GenerationMode generationMo

sb.AppendLines($@"
$precision {tmpOutHeight};
$precision2 {GetVariableNameForSlot(kParallaxUVsOutputSlotId)} = {uvs} + ParallaxOcclusionMapping{GetVariableNameForNode()}({lod}, {lodThreshold}, {steps}, {tmpViewDirUV}, {tmpPOMParam}, {tmpOutHeight});
$precision2 {GetVariableNameForSlot(kParallaxUVsOutputSlotId)} = {uvs} + ParallaxOcclusionMapping{GetVariableNameForNode()}({lod}, {lodThreshold}, {steps}, {tmpViewDirUV}, {tmpPOMParam}, {tmpOutHeight}, TEXTURE2D_ARGS({heightmap}, {sampler}));

$precision {GetVariableNameForSlot(kPixelDepthOffsetOutputSlotId)} = ({tmpMaxHeight} - {tmpOutHeight} * {tmpMaxHeight}) / max({tmpNdotV}, 0.0001);
");
Expand Down