Skip to content

Commit

Permalink
Add missing (shader) code to finalize support for enabling/disabling …
Browse files Browse the repository at this point in the history
…triplanar mapping on HLMS Terra detail maps
  • Loading branch information
TaaTT4 committed Jan 12, 2023
1 parent a769567 commit d41139a
Show file tree
Hide file tree
Showing 5 changed files with 215 additions and 13 deletions.
6 changes: 3 additions & 3 deletions Samples/Media/Hlms/Pbs/Any/Main/800.PixelShader_piece_ps.any
Original file line number Diff line number Diff line change
Expand Up @@ -260,9 +260,9 @@
/// Sample detail maps and weight them against the weight map in the next foreach loop.
@foreach( detail_maps_diffuse, n )
@property( detail_map@n )
midf4 detailCol@n = SampleDetailCol@n( textureMaps@value(detail_map@n_idx),
midf4 detailCol@n = SampleDetailCol@n@property( detail_triplanar_diffuse )Triplanar@end ( textureMaps@value(detail_map@n_idx),
samplerState@value(detail_map@n_sampler),
UV_DETAIL@n( inPs.uv@value(uv_detail@n).xy@insertpiece( offsetDetail@n ) ),
UV_DETAIL@n( @property( detail_triplanar )GetTriplanarUVTp( inPs.worldPos, pixelData.normal )@else inPs.uv@value(uv_detail@n).xy@end @insertpiece( offsetDetail@n ) ),
texIndex_detailMapIdx@n );
detailWeights.@insertpiece(detail_swizzle@n) *= detailCol@n.w;
detailCol@n.w = detailWeights.@insertpiece(detail_swizzle@n);
Expand Down Expand Up @@ -422,7 +422,7 @@
@foreach( detail_maps_normal, n )
@piece( SampleDetailMapNm@n )getTSNormal( textureMaps@value(detail_map_nm@n_idx),
samplerState@value(detail_map_nm@n_sampler),
UV_DETAIL_NM@n( inPs.uv@value(uv_detail_nm@n).xy@insertpiece( offsetDetail@n ) ),
UV_DETAIL_NM@n( @property( detail_triplanar )GetTriplanarUVTp( inPs.worldPos, pixelData.normal )@else inPs.uv@value(uv_detail_nm@n).xy@end @insertpiece( offsetDetail@n ) ),
texIndex_detailNormMapIdx@n ) * detailWeights.@insertpiece(detail_swizzle@n) @insertpiece( detail@n_nm_weight_mul )@end
@end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,11 @@ struct CellData
@piece( Terra_VStoPS_block )
INTERPOLANT( float3 pos, @counter(texcoord) );
INTERPOLANT( float2 uv0, @counter(texcoord) );

@property( detail_triplanar )
INTERPOLANT( float3 worldPos, @counter(texcoord) );
@end

@insertpiece( VStoPS_block )
@end

Expand Down
38 changes: 28 additions & 10 deletions Samples/Media/Hlms/Terra/Any/800.PixelShader_piece_ps.any
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
@insertpiece( DeclareBRDF_AreaLightApprox )
@end

@insertpiece( DeclDetailTriplanarFuncs )

#define SampleMetalness0( tex, sampler, uv, arrayIdx ) OGRE_SampleArray2DF16( tex, sampler, uv, arrayIdx )
#define SampleMetalness1( tex, sampler, uv, arrayIdx ) OGRE_SampleArray2DF16( tex, sampler, uv, arrayIdx )
#define SampleMetalness2( tex, sampler, uv, arrayIdx ) OGRE_SampleArray2DF16( tex, sampler, uv, arrayIdx )
Expand Down Expand Up @@ -127,9 +129,9 @@
/// SPECUlAR MAP
@foreach( 4, n )
@property( metalness_map@n )
midf metalness@n = SampleMetalness@n( textureMaps@value( metalness_map@n_idx ),
midf metalness@n = SampleMetalness@n@property( detail_triplanar_metalness )Triplanar@end ( textureMaps@value( metalness_map@n_idx ),
samplerState@value(metalness_map@n_sampler),
inPs.uv0.xy * material.detailOffsetScale[@n].zw +
@property( detail_triplanar )GetTriplanarUVTp( inPs.worldPos, pixelData.normal )@else inPs.uv0.xy@end * material.detailOffsetScale[@n].zw +
material.detailOffsetScale[@n].xy,
texIndex_detailMetalnessIdx@n ).x;
@else
Expand All @@ -152,9 +154,9 @@
/// ROUGHNESS MAP
@foreach( 4, n )
@property( roughness_map@n )
midf roughness@n = SampleRoughness@n( textureMaps@value( roughness_map@n_idx ),
midf roughness@n = SampleRoughness@n@property( detail_triplanar_roughness )Triplanar@end ( textureMaps@value( roughness_map@n_idx ),
samplerState@value(roughness_map@n_sampler),
inPs.uv0.xy * material.detailOffsetScale[@n].zw +
@property( detail_triplanar )GetTriplanarUVTp( inPs.worldPos, pixelData.normal )@else inPs.uv0.xy@end * material.detailOffsetScale[@n].zw +
material.detailOffsetScale[@n].xy,
texIndex_detailRoughnessIdx@n ).x;
@else
Expand All @@ -180,8 +182,18 @@
@property( z_up )
pixelData.geomNormal.yz = midf2_c( -pixelData.geomNormal.z, pixelData.geomNormal.y );
@end
pixelData.geomNormal = mul( pixelData.geomNormal, toMidf3x3( passBuf.view ) );
@property( normal_map )

@property( normal_map && detail_triplanar_normal )
pixelData.normal = pixelData.geomNormal;
@end

@property( detail_triplanar )
// https://bgolus.medium.com/normal-mapping-for-a-triplanar-shader-10bf39dca05a
midf3 triplanarBlend = pow( pixelData.geomNormal, _h( 4.0 ) );
triplanarBlend /= dot( triplanarBlend, midf3_c( 1.0, 1.0, 1.0 ) );
@end

@property( normal_map && !detail_triplanar_normal )
//Get the TBN matrix
midf3 viewSpaceUnitX = mul( midf3_c( 1, 0, 0 ), toMidf3x3( passBuf.view ) );
midf3 vTangent = normalize( cross( pixelData.geomNormal, viewSpaceUnitX ) );
Expand All @@ -201,6 +213,11 @@

@insertpiece( LoadDetailWeights )

@property( !hlms_use_prepass )
@insertpiece( LoadNormalData )
@insertpiece( SampleAndApplyDetailNormalMaps@property( detail_triplanar_normal )Triplanar@end )
@end

@insertpiece( SampleDetailMaps )

@property( !hlms_prepass )
Expand All @@ -213,15 +230,16 @@
@insertpiece( forwardPlusDoDecals )

@property( !hlms_use_prepass )
@insertpiece( LoadNormalData )
@insertpiece( SampleAndApplyDetailNormalMaps )

@insertpiece( custom_ps_posSampleNormal )

@insertpiece( forwardPlusApplyDecalsNormal )

@property( normal_map )
pixelData.normal = normalize( mul( TBN, pixelData.normal ) );
@property( detail_triplanar_normal )
pixelData.normal = mul( pixelData.normal, toMidf3x3( passBuf.view ) );
@else
pixelData.normal = normalize( mul( TBN, pixelData.normal ) );
@end
@end

@insertpiece( DoDirectionalShadowMaps )
Expand Down
4 changes: 4 additions & 0 deletions Samples/Media/Hlms/Terra/Any/800.VertexShader_piece_vs.any
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,10 @@
worldPos.yz = float2( -worldPos.z, worldPos.y );
@end

@property( detail_triplanar )
outVs.worldPos = worldPos.xyz;
@end

@insertpiece( VertexTerraTransform )

outVs.uv0.xy = float2( uVertexPos.xy ) * float2( cellData.pos.w, cellData.scale.w );
Expand Down
175 changes: 175 additions & 0 deletions Samples/Media/Hlms/Terra/Any/DetailTriplanar_piece_ps.any
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
@piece( DeclDetailTriplanarFuncs )
@property( detail_triplanar )
// https://catlikecoding.com/unity/tutorials/advanced-rendering/triplanar-mapping/

// Side view projection
midf2 GetTriplanarUVSd( midf3 position, midf3 normal )
{
midf2 uv = -position.zy;

if( normal.x < _h( 0.0 ) )
{
uv.x = -uv.x;
}

return uv;
}

// Top view projection
midf2 GetTriplanarUVTp( midf3 position, midf3 normal )
{
return position.xz;
}

// Front view projection
midf2 GetTriplanarUVFr( midf3 position, midf3 normal )
{
midf2 uv = -position.xy;

if( normal.z >= _h( 0.0 ) )
{
uv.x = -uv.x;
}

return uv;
}
@end

@property( detail_triplanar_diffuse )
@foreach( detail_maps_diffuse, n )@property( detail_map@n )
#define SampleDetailCol@nTriplanar( tex, sampler, uv, arrayIdx ) SampleDetailCol@n( tex, \
sampler, \
UV_DETAIL@n( GetTriplanarUVSd( inPs.worldPos, pixelData.normal )@insertpiece( offsetDetail@n ) ), \
arrayIdx ) * triplanarBlend.x + \
\
SampleDetailCol@n( tex, \
sampler, \
UV_DETAIL@n( GetTriplanarUVTp( inPs.worldPos, pixelData.normal )@insertpiece( offsetDetail@n ) ), \
arrayIdx ) * triplanarBlend.y + \
\
SampleDetailCol@n( tex, \
sampler, \
UV_DETAIL@n( GetTriplanarUVFr( inPs.worldPos, pixelData.normal )@insertpiece( offsetDetail@n ) ), \
arrayIdx ) * triplanarBlend.z
@end @end
@end

@property( detail_triplanar_normal )
// https://catlikecoding.com/unity/tutorials/advanced-rendering/triplanar-mapping/
midf3 BlendTriplanarNormal( midf3 mappedNormal, midf3 surfaceNormal )
{
midf3 n;
n.xy = mappedNormal.xy + surfaceNormal.xy;
n.z = mappedNormal.z * surfaceNormal.z;
return n;
}

@foreach( detail_maps_normal, n )@property( detail_map_nm@n )
#define SampleDetailMapNm@nSd( tex, sampler, uv, arrayIdx ) SampleDetailMapNm@nSdFn( tex, sampler, uv, arrayIdx, detailWeights, pixelData.normal )
#define SampleDetailMapNm@nTp( tex, sampler, uv, arrayIdx ) SampleDetailMapNm@nTpFn( tex, sampler, uv, arrayIdx, detailWeights, pixelData.normal )
#define SampleDetailMapNm@nFr( tex, sampler, uv, arrayIdx ) SampleDetailMapNm@nFrFn( tex, sampler, uv, arrayIdx, detailWeights, pixelData.normal )

// Side view projection
midf3 SampleDetailMapNm@nSdFn( Texture2DArray tex, SamplerState smp, midf2 uv, uint arrayIdx, midf4 weights, midf3 normal )
{
midf3 tangentNormal = getTSNormal( tex, smp, uv, arrayIdx ) * weights.@insertpiece( detail_swizzle@n );

if( normal.x >= _h( 0.0 ) )
{
tangentNormal.x = -tangentNormal.x;
}

midf3 worldNormal = BlendTriplanarNormal( tangentNormal, normal.zyx ).zyx;
worldNormal.x += _h( 1.0 ) - weights.@insertpiece( detail_swizzle@n );

return worldNormal;
}

// Top view projection
midf3 SampleDetailMapNm@nTpFn( Texture2DArray tex, SamplerState smp, midf2 uv, uint arrayIdx, midf4 weights, midf3 normal )
{
midf3 tangentNormal = getTSNormal( tex, smp, uv, arrayIdx ) * weights.@insertpiece( detail_swizzle@n );
tangentNormal.y = -tangentNormal.y;

midf3 worldNormal = BlendTriplanarNormal( tangentNormal, normal.xzy ).xzy;
worldNormal.y += _h( 1.0 ) - weights.@insertpiece( detail_swizzle@n );

return worldNormal;
}

// Front view projection
midf3 SampleDetailMapNm@nFrFn( Texture2DArray tex, SamplerState smp, midf2 uv, uint arrayIdx, midf4 weights, midf3 normal )
{
midf3 tangentNormal = getTSNormal( tex, smp, uv, arrayIdx ) * weights.@insertpiece( detail_swizzle@n );

if( normal.z < _h( 0.0 ) )
{
tangentNormal.x = -tangentNormal.x;
}

midf3 worldNormal = BlendTriplanarNormal( tangentNormal, normal );
worldNormal.z += _h( 1.0 ) - weights.@insertpiece( detail_swizzle@n );

return worldNormal;
}
@end @end
@end

@property( detail_triplanar_roughness )
@foreach( 4, n )@property( roughness_map@n )
#define SampleRoughness@nTriplanar( tex, sampler, uv, arrayIdx ) SampleRoughness@n( tex, \
sampler, \
GetTriplanarUVSd( inPs.worldPos, pixelData.normal )@insertpiece( offsetDetail@n ), \
arrayIdx ) * triplanarBlend.x + \
\
SampleRoughness@n( tex, \
sampler, \
GetTriplanarUVTp( inPs.worldPos, pixelData.normal )@insertpiece( offsetDetail@n ), \
arrayIdx ) * triplanarBlend.y + \
\
SampleRoughness@n( tex, \
sampler, \
GetTriplanarUVFr( inPs.worldPos, pixelData.normal )@insertpiece( offsetDetail@n ), \
arrayIdx ) * triplanarBlend.z
@end @end
@end

@property( detail_triplanar_metalness )
@foreach( 4, n )@property( metalness_map@n )
#define SampleMetalness@nTriplanar( tex, sampler, uv, arrayIdx ) SampleMetalness@n( tex, \
sampler, \
GetTriplanarUVSd( inPs.worldPos, pixelData.normal )@insertpiece( offsetDetail@n ), \
arrayIdx ) * triplanarBlend.x + \
\
SampleMetalness@n( tex, \
sampler, \
GetTriplanarUVTp( inPs.worldPos, pixelData.normal )@insertpiece( offsetDetail@n ), \
arrayIdx ) * triplanarBlend.y + \
\
SampleMetalness@n( tex, \
sampler, \
GetTriplanarUVFr( inPs.worldPos, pixelData.normal )@insertpiece( offsetDetail@n ), \
arrayIdx ) * triplanarBlend.z
@end @end
@end
@end


@piece( SampleAndApplyDetailNormalMapsTriplanar )
@foreach( detail_maps_normal, n )@property( detail_map_nm@n )
pixelData.normal = SampleDetailMapNm@nSd( textureMaps@value( detail_map_nm@n_idx ),
samplerState@value( detail_map_nm@n_sampler ),
UV_DETAIL_NM@n( GetTriplanarUVSd( inPs.worldPos, pixelData.normal )@insertpiece( offsetDetail@n ) ),
texIndex_detailNormMapIdx@n ) * triplanarBlend.x +

SampleDetailMapNm@nTp( textureMaps@value( detail_map_nm@n_idx ),
samplerState@value( detail_map_nm@n_sampler ),
UV_DETAIL_NM@n( GetTriplanarUVTp( inPs.worldPos, pixelData.normal )@insertpiece( offsetDetail@n ) ),
texIndex_detailNormMapIdx@n ) * triplanarBlend.y +

SampleDetailMapNm@nFr( textureMaps@value( detail_map_nm@n_idx ),
samplerState@value( detail_map_nm@n_sampler ),
UV_DETAIL_NM@n( GetTriplanarUVFr( inPs.worldPos, pixelData.normal )@insertpiece( offsetDetail@n ) ),
texIndex_detailNormMapIdx@n ) * triplanarBlend.z;
@end @end
@end

0 comments on commit d41139a

Please sign in to comment.