Skip to content

Commit

Permalink
Merge pull request #132 from CiMaWi/dynamic-shader-triplanar-mapping
Browse files Browse the repository at this point in the history
Triplanar Mapping for Projected Textures
  • Loading branch information
doodlum authored Oct 25, 2023
2 parents 0206ab8 + 7362abc commit 22a2e5c
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -952,6 +952,16 @@ float GetSnowParameterY(float texProjTmp, float alpha)
# endif
}

float2 ComputeTriplanarUV(float3 InputPosition)
{
float3 posDDX = ddx(InputPosition.xyz);
float3 posDDY = ddy(InputPosition.xyz);
return (((0.1 * (abs(posDDX.z) + abs(posDDY.z)) < abs(posDDX.x) + abs(posDDY.x)) &&
(0.1 * (abs(posDDX.z) + abs(posDDY.z)) < abs(posDDX.y) + abs(posDDY.y))) ?
InputPosition.xy :
((0.5 * (abs(posDDX.x) + abs(posDDY.x)) < abs(posDDX.y) + abs(posDDY.y)) ? InputPosition.yz : InputPosition.xz));
}

# if defined(LOD)
# undef COMPLEX_PARALLAX_MATERIALS
# undef WATER_BLENDING
Expand Down Expand Up @@ -1149,8 +1159,13 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace

float2 diffuseUv = uv;
# if defined(SPARKLE)
# if defined(VR)
diffuseUv = ProjectedUVParams2.yy * input.TexCoord0.zw;
# endif // SPARKLE
# else
float2 triplanarUv = ComputeTriplanarUV(input.InputPosition.xyz);
diffuseUv = ProjectedUVParams2.yy * triplanarUv;
# endif // VR
# endif // SPARKLE

# if defined(CPM_AVAILABLE)

Expand All @@ -1166,7 +1181,11 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# endif // LANDSCAPE

# if defined(SPARKLE)
# if defined(VR)
diffuseUv = ProjectedUVParams2.yy * (input.TexCoord0.zw + (uv - uvOriginal));
# else
diffuseUv = ProjectedUVParams2.yy * (triplanarUv + (uv - uvOriginal));
# endif // VR
# else
diffuseUv = uv;
# endif // SPARKLE
Expand Down Expand Up @@ -1440,7 +1459,11 @@ PS_OUTPUT main(PS_INPUT input, bool frontFace
# endif // SNOW
# else
if (ProjectedUVParams3.w > 0.5) {
# if defined(VR)
float2 projNormalUv = ProjectedUVParams3.x * ProjectedUVParams.zz * ComputeTriplanarUV(input.InputPosition.xyz);
# else
float2 projNormalUv = ProjectedUVParams3.x * projNoiseUv;
# endif // VR
float3 projNormal = TransformNormal(TexProjNormalSampler.Sample(SampProjNormalSampler, projNormalUv).xyz);
float2 projDetailUv = ProjectedUVParams3.y * projNoiseUv;
float3 projDetail = TexProjDetail.Sample(SampProjDetailSampler, projDetailUv).xyz;
Expand Down

0 comments on commit 22a2e5c

Please sign in to comment.