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

Triplanar Mapping for Projected Textures #132

Merged
merged 2 commits into from
Oct 25, 2023
Merged
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
25 changes: 24 additions & 1 deletion package/Shaders/Lighting.hlsl
Original file line number Diff line number Diff line change
Expand Up @@ -947,6 +947,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 @@ -1140,8 +1150,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 @@ -1157,7 +1172,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 @@ -1431,7 +1450,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