-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add missing (shader) code to finalize support for enabling/disabling …
…triplanar mapping on HLMS Terra detail maps
- Loading branch information
Showing
5 changed files
with
215 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
175 changes: 175 additions & 0 deletions
175
Samples/Media/Hlms/Terra/Any/DetailTriplanar_piece_ps.any
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |