-
Notifications
You must be signed in to change notification settings - Fork 133
feat: triplanar projected materials #1950
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
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
ed02bb5
feat: triplanar projected materials
Dlizzio 363c75d
Merge branch 'dev' into triplanar-projection
Dlizzio 2c91bad
style: 🎨 apply pre-commit.ci formatting
pre-commit-ci[bot] 1b389e9
Merge branch 'dev' into triplanar-projection
Dlizzio 61e0dc9
Merge branch 'dev' into triplanar-projection
Dlizzio 57bbd40
remove blending
Dlizzio efa454e
Merge branch 'dev' into triplanar-projection
Dlizzio d060189
new
Dlizzio fc0edcf
style: 🎨 apply pre-commit.ci formatting
pre-commit-ci[bot] d3d0079
Merge branch 'dev' into triplanar-projection
Dlizzio 797a973
Merge branch 'dev' into triplanar-projection
Dlizzio File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,62 @@ | ||
| #ifndef TRIPLANAR_HLSLI | ||
| #define TRIPLANAR_HLSLI | ||
|
|
||
| namespace Triplanar | ||
| { | ||
| static const float BLEND_SHARPNESS = 6.0; // Power for weight computation; higher = sharper axis transitions | ||
| static const float STRETCH_CUTOFF = 0.4; // ~cos(66°) — per-axis alignment below this produces visible stretching | ||
|
|
||
| /// Compute triplanar blend weights using face normal mask and smooth vertex normal blend. | ||
| float3 GetWeights(float3 vertexNormal, float3 faceNormal, float sharpness = BLEND_SHARPNESS) | ||
| { | ||
| float3 mask = step(STRETCH_CUTOFF, abs(faceNormal)); | ||
| float3 w = pow(abs(vertexNormal), sharpness) * mask; | ||
| return w / (dot(w, 1.0) + EPSILON_DIVISION); | ||
| } | ||
|
|
||
| /// Weighted triplanar sample blending all 3 planes — stable for alpha/fade values. | ||
| float4 Sample(Texture2D<float4> tex, SamplerState samp, float3 worldPos, float3 weights, float scale) | ||
| { | ||
| return tex.Sample(samp, worldPos.yz * scale) * weights.x + | ||
| tex.Sample(samp, worldPos.xz * scale) * weights.y + | ||
| tex.Sample(samp, worldPos.xy * scale) * weights.z; | ||
| } | ||
|
|
||
| /// Compute gradients for stochastic triplanar sampling, pre-computed before branching. | ||
| void ComputeGradients(float3 worldPos, float scale, out float3 dPdx, out float3 dPdy) | ||
| { | ||
| dPdx = ddx(worldPos * scale); | ||
| dPdy = ddy(worldPos * scale); | ||
| } | ||
|
|
||
| /// Stochastic triplanar: select one projection plane via noise, reducing 3 texture reads to 1. | ||
| float4 SampleStochastic(Texture2D<float4> tex, SamplerState samp, float3 worldPos, float3 weights, float scale, float noise) | ||
| { | ||
| float3 dPdx, dPdy; | ||
| ComputeGradients(worldPos, scale, dPdx, dPdy); | ||
|
|
||
| if (noise < weights.x) | ||
| return tex.SampleGrad(samp, worldPos.yz * scale, dPdx.yz, dPdy.yz); | ||
| if (noise < weights.x + weights.y) | ||
| return tex.SampleGrad(samp, worldPos.xz * scale, dPdx.xz, dPdy.xz); | ||
| return tex.SampleGrad(samp, worldPos.xy * scale, dPdx.xy, dPdy.xy); | ||
| } | ||
|
|
||
| /// Stochastic triplanar with mip bias via gradient scaling. | ||
| float4 SampleStochasticBias(Texture2D<float4> tex, SamplerState samp, float3 worldPos, float3 weights, float scale, float bias, float noise) | ||
| { | ||
| float3 dPdx, dPdy; | ||
| ComputeGradients(worldPos, scale, dPdx, dPdy); | ||
| float biasScale = exp2(bias); | ||
| dPdx *= biasScale; | ||
| dPdy *= biasScale; | ||
|
|
||
| if (noise < weights.x) | ||
| return tex.SampleGrad(samp, worldPos.yz * scale, dPdx.yz, dPdy.yz); | ||
| if (noise < weights.x + weights.y) | ||
| return tex.SampleGrad(samp, worldPos.xz * scale, dPdx.xz, dPdy.xz); | ||
| return tex.SampleGrad(samp, worldPos.xy * scale, dPdx.xy, dPdy.xy); | ||
| } | ||
| } | ||
|
|
||
| #endif // TRIPLANAR_HLSLI |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.