Skip to content

Commit

Permalink
[Regression] Fix lighting in Terra
Browse files Browse the repository at this point in the history
Since the merge of triplanar mapping, on most shader generation paths
the normal would not end up in view space as it was supposed to.

Triplanar mapping requires the normal to be in world space, but the
lighting calculations required it in view space.
  • Loading branch information
darksylinc committed Mar 3, 2023
1 parent 4488f13 commit d851f3f
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 55 deletions.
7 changes: 5 additions & 2 deletions Samples/Media/Hlms/Pbs/Any/Main/800.PixelShader_piece_ps.any
Expand Up @@ -50,6 +50,9 @@
@else
#define geomNormal normal
@end
@property( detail_triplanar )
midf3 worldSpaceNormal;
@end
midf4 diffuse;
midf3 specular;

Expand Down Expand Up @@ -262,7 +265,7 @@
@property( detail_map@n )
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( @property( detail_triplanar )GetTriplanarUVTp( inPs.worldPos, pixelData.normal )@else inPs.uv@value(uv_detail@n).xy@end @insertpiece( offsetDetail@n ) ),
UV_DETAIL@n( @property( detail_triplanar )GetTriplanarUVTp( inPs.worldPos, pixelData.worldSpaceNormal )@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 +425,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( @property( detail_triplanar )GetTriplanarUVTp( inPs.worldPos, pixelData.normal )@else inPs.uv@value(uv_detail_nm@n).xy@end @insertpiece( offsetDetail@n ) ),
UV_DETAIL_NM@n( @property( detail_triplanar )GetTriplanarUVTp( inPs.worldPos, pixelData.worldSpaceNormal )@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
90 changes: 45 additions & 45 deletions Samples/Media/Hlms/Terra/Any/550.DetailTriplanar_piece_ps.any
Expand Up @@ -37,19 +37,19 @@

@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 ) ), \
#define SampleDetailCol@nTriplanar( tex, sampler, uv, arrayIdx ) (SampleDetailCol@n( tex, \
sampler, \
UV_DETAIL@n( GetTriplanarUVSd( inPs.worldPos, pixelData.worldSpaceNormal )@insertpiece( offsetDetail@n ) ), \
arrayIdx ) * triplanarBlend.x + \
\
SampleDetailCol@n( tex, \
sampler, \
UV_DETAIL@n( GetTriplanarUVTp( inPs.worldPos, pixelData.worldSpaceNormal )@insertpiece( offsetDetail@n ) ), \
arrayIdx ) * triplanarBlend.y + \
\
SampleDetailCol@n( tex, \
sampler, \
UV_DETAIL@n( GetTriplanarUVFr( inPs.worldPos, pixelData.worldSpaceNormal )@insertpiece( offsetDetail@n ) ), \
arrayIdx ) * triplanarBlend.z)
@end @end
@end
Expand All @@ -65,9 +65,9 @@
}

@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 )
#define SampleDetailMapNm@nSd( tex, sampler, uv, arrayIdx ) SampleDetailMapNm@nSdFn( tex, sampler, uv, arrayIdx, detailWeights, pixelData.worldSpaceNormal )
#define SampleDetailMapNm@nTp( tex, sampler, uv, arrayIdx ) SampleDetailMapNm@nTpFn( tex, sampler, uv, arrayIdx, detailWeights, pixelData.worldSpaceNormal )
#define SampleDetailMapNm@nFr( tex, sampler, uv, arrayIdx ) SampleDetailMapNm@nFrFn( tex, sampler, uv, arrayIdx, detailWeights, pixelData.worldSpaceNormal )

// Side view projection
midf3 SampleDetailMapNm@nSdFn( Texture2DArray tex, SamplerState smp, midf2 uv, uint arrayIdx, midf4 weights, midf3 normal )
Expand Down Expand Up @@ -117,38 +117,38 @@

@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 ), \
#define SampleRoughness@nTriplanar( tex, sampler, uv, arrayIdx ) (SampleRoughness@n( tex, \
sampler, \
GetTriplanarUVSd( inPs.worldPos, pixelData.worldSpaceNormal )@insertpiece( offsetDetail@n ), \
arrayIdx ) * triplanarBlend.x + \
\
SampleRoughness@n( tex, \
sampler, \
GetTriplanarUVTp( inPs.worldPos, pixelData.worldSpaceNormal )@insertpiece( offsetDetail@n ), \
arrayIdx ) * triplanarBlend.y + \
\
SampleRoughness@n( tex, \
sampler, \
GetTriplanarUVFr( inPs.worldPos, pixelData.worldSpaceNormal )@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 ), \
#define SampleMetalness@nTriplanar( tex, sampler, uv, arrayIdx ) (SampleMetalness@n( tex, \
sampler, \
GetTriplanarUVSd( inPs.worldPos, pixelData.worldSpaceNormal )@insertpiece( offsetDetail@n ), \
arrayIdx ) * triplanarBlend.x + \
\
SampleMetalness@n( tex, \
sampler, \
GetTriplanarUVTp( inPs.worldPos, pixelData.worldSpaceNormal )@insertpiece( offsetDetail@n ), \
arrayIdx ) * triplanarBlend.y + \
\
SampleMetalness@n( tex, \
sampler, \
GetTriplanarUVFr( inPs.worldPos, pixelData.worldSpaceNormal )@insertpiece( offsetDetail@n ), \
arrayIdx ) * triplanarBlend.z)
@end @end
@end
Expand All @@ -159,17 +159,17 @@
@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 ) ),
UV_DETAIL_NM@n( GetTriplanarUVSd( inPs.worldPos, pixelData.worldSpaceNormal )@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 ) ),
UV_DETAIL_NM@n( GetTriplanarUVTp( inPs.worldPos, pixelData.worldSpaceNormal )@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 ) ),
UV_DETAIL_NM@n( GetTriplanarUVFr( inPs.worldPos, pixelData.worldSpaceNormal )@insertpiece( offsetDetail@n ) ),
texIndex_detailNormMapIdx@n ) * triplanarBlend.z;
@end @end
@end
15 changes: 7 additions & 8 deletions Samples/Media/Hlms/Terra/Any/800.PixelShader_piece_ps.any
Expand Up @@ -138,7 +138,7 @@
@property( metalness_map@n )
midf metalness@n = SampleMetalness@n@property( detail_triplanar_metalness )Triplanar@end ( textureMaps@value( metalness_map@n_idx ),
samplerState@value(metalness_map@n_sampler),
@property( detail_triplanar )GetTriplanarUVTp( inPs.worldPos, pixelData.normal )@else inPs.uv0.xy@end * material.detailOffsetScale[@n].zw +
@property( detail_triplanar )GetTriplanarUVTp( inPs.worldPos, pixelData.worldSpaceNormal )@else inPs.uv0.xy@end * material.detailOffsetScale[@n].zw +
material.detailOffsetScale[@n].xy,
texIndex_detailMetalnessIdx@n ).x;
@else
Expand All @@ -163,7 +163,7 @@
@property( roughness_map@n )
midf roughness@n = SampleRoughness@n@property( detail_triplanar_roughness )Triplanar@end ( textureMaps@value( roughness_map@n_idx ),
samplerState@value(roughness_map@n_sampler),
@property( detail_triplanar )GetTriplanarUVTp( inPs.worldPos, pixelData.normal )@else inPs.uv0.xy@end * material.detailOffsetScale[@n].zw +
@property( detail_triplanar )GetTriplanarUVTp( inPs.worldPos, pixelData.worldSpaceNormal )@else inPs.uv0.xy@end * material.detailOffsetScale[@n].zw +
material.detailOffsetScale[@n].xy,
texIndex_detailRoughnessIdx@n ).x;
@else
Expand All @@ -190,17 +190,16 @@
pixelData.geomNormal.yz = midf2_c( -pixelData.geomNormal.z, pixelData.geomNormal.y );
@end

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

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

@property( normal_map && !detail_triplanar_normal )
pixelData.geomNormal = mul( pixelData.geomNormal, toMidf3x3( passBuf.view ) );

@property( normal_map )
//Get the TBN matrix
midf3 viewSpaceUnitX = mul( midf3_c( 1, 0, 0 ), toMidf3x3( passBuf.view ) );
midf3 vTangent = normalize( cross( pixelData.geomNormal, viewSpaceUnitX ) );
Expand Down

0 comments on commit d851f3f

Please sign in to comment.