Skip to content

Commit c5865f7

Browse files
authored
Merge pull request #15085 from sunag/dev-triplanar-node
NodeMaterial - Triplanar Mapping example
2 parents 05ee6d5 + 806102d commit c5865f7

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

examples/webgl_materials_nodes.html

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@
186186
'adv / soft-body': 'soft-body',
187187
'adv / wave': 'wave',
188188
'adv / triangle-blur': 'triangle-blur',
189+
'adv / triplanar-mapping': 'triplanar-mapping',
189190
'adv / render-to-texture': 'rtt',
190191
'adv / temporal-blur': 'temporal-blur',
191192
'adv / conditional': 'conditional',
@@ -2385,6 +2386,56 @@
23852386

23862387
break;
23872388

2389+
case 'triplanar-mapping':
2390+
2391+
// MATERIAL
2392+
2393+
mtl = new THREE.PhongNodeMaterial();
2394+
2395+
var scale = new THREE.FloatNode( .02 );
2396+
2397+
var triplanarMapping = new THREE.FunctionNode( [
2398+
// Reference: https://github.com/keijiro/StandardTriplanar
2399+
"vec4 triplanar_mapping( sampler2D texture, vec3 normal, vec3 position, float scale ) {",
2400+
2401+
// Blending factor of triplanar mapping
2402+
" vec3 bf = normalize( abs( normal ) );",
2403+
" bf /= dot( bf, vec3( 1.0 ) );",
2404+
2405+
// Triplanar mapping
2406+
" vec2 tx = position.yz * scale;",
2407+
" vec2 ty = position.zx * scale;",
2408+
" vec2 tz = position.xy * scale;",
2409+
2410+
// Base color
2411+
" vec4 cx = texture2D(texture, tx) * bf.x;",
2412+
" vec4 cy = texture2D(texture, ty) * bf.y;",
2413+
" vec4 cz = texture2D(texture, tz) * bf.z;",
2414+
2415+
" return cx + cy + cz;",
2416+
2417+
"}"
2418+
].join( "\n" ) );
2419+
2420+
var triplanarMappingTexture = new THREE.FunctionCallNode( triplanarMapping, {
2421+
texture: new THREE.TextureNode( getTexture( "brick" ) ),
2422+
normal: new THREE.NormalNode( THREE.NormalNode.WORLD ),
2423+
position: new THREE.PositionNode( THREE.PositionNode.WORLD ),
2424+
scale: scale,
2425+
} );
2426+
2427+
mtl.color = triplanarMappingTexture;
2428+
2429+
// GUI
2430+
2431+
addGui( 'scale', scale.value, function ( val ) {
2432+
2433+
scale.value = val;
2434+
2435+
}, false, 0.001, .1 );
2436+
2437+
break;
2438+
23882439
case 'firefly':
23892440

23902441
// MATERIAL

0 commit comments

Comments
 (0)