Created
October 1, 2022 13:05
-
-
Save arthurschiller/dc12c4ce596b21128ae8b816a298d94c to your computer and use it in GitHub Desktop.
A simple SceneKit shader modifier snippet for triplanar texture mapping
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
// Be sure to to set the WrapS and WrapT properties of your texture to .repeat | |
float4 worldPos = scn_frame.inverseViewTransform * float4(_surface.position, 1.0); | |
float4 worldNormal = scn_frame.inverseViewTransform * float4(_surface.normal, 0.0); | |
float3 blending = abs(worldNormal).xyz; | |
blending = normalize(max(blending, 0.00001)); // Force weights to sum to 1.0 | |
float b = (blending.x + blending.y + blending.z); | |
blending /= float3(b, b, b); | |
float scale = 2.0; | |
float4 xAxisTexture = u_diffuseTexture.sample(u_diffuseTextureSampler, worldPos.yz * scale); | |
float4 yAxisTexture = u_diffuseTexture.sample(u_diffuseTextureSampler, worldPos.xz * scale); | |
float4 zAxisTexture = u_diffuseTexture.sample(u_diffuseTextureSampler, worldPos.xy * scale); | |
float4 triplanarTexture = xAxisTexture * blending.x + yAxisTexture * blending.y + zAxisTexture * blending.z; | |
_surface.diffuse = triplanarTexture; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment