Skip to content

Commit a36bc1b

Browse files
authored
TSL: Move TriplanarTexturesNode to triplanarTextures() Fn (#31285)
1 parent 4ec743d commit a36bc1b

File tree

4 files changed

+66
-150
lines changed

4 files changed

+66
-150
lines changed

src/nodes/Nodes.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ export { default as SetNode } from './utils/SetNode.js';
5353
export { default as SplitNode } from './utils/SplitNode.js';
5454
export { default as SpriteSheetUVNode } from './utils/SpriteSheetUVNode.js';
5555
export { default as StorageArrayElementNode } from './utils/StorageArrayElementNode.js';
56-
export { default as TriplanarTexturesNode } from './utils/TriplanarTexturesNode.js';
5756
export { default as ReflectorNode } from './utils/ReflectorNode.js';
5857
export { default as RTTNode } from './utils/RTTNode.js';
5958
export { default as MemberNode } from './utils/MemberNode.js';

src/nodes/TSL.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ export * from './utils/ViewportUtils.js';
3838
export * from './utils/RotateNode.js';
3939
export * from './utils/SpriteSheetUVNode.js';
4040
export * from './utils/Timer.js';
41-
export * from './utils/TriplanarTexturesNode.js';
41+
export * from './utils/TriplanarTextures.js';
4242
export * from './utils/ReflectorNode.js';
4343
export * from './utils/RTTNode.js';
4444
export * from './utils/PostProcessingUtils.js';

src/nodes/utils/TriplanarTextures.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import { add } from '../math/OperatorNode.js';
2+
import { normalLocal } from '../accessors/Normal.js';
3+
import { positionLocal } from '../accessors/Position.js';
4+
import { texture } from '../accessors/TextureNode.js';
5+
import { float, vec3, Fn } from '../tsl/TSLBase.js';
6+
7+
/**
8+
* TSL function for creating a triplanar textures node.
9+
*
10+
* Can be used for triplanar texture mapping.
11+
*
12+
* ```js
13+
* material.colorNode = triplanarTexture( texture( diffuseMap ) );
14+
* ```
15+
*
16+
* @tsl
17+
* @function
18+
* @param {Node} textureXNode - First texture node.
19+
* @param {?Node} [textureYNode=null] - Second texture node. When not set, the shader will sample from `textureXNode` instead.
20+
* @param {?Node} [textureZNode=null] - Third texture node. When not set, the shader will sample from `textureXNode` instead.
21+
* @param {?Node<float>} [scaleNode=float(1)] - The scale node.
22+
* @param {?Node<vec3>} [positionNode=positionLocal] - Vertex positions in local space.
23+
* @param {?Node<vec3>} [normalNode=normalLocal] - Normals in local space.
24+
* @returns {Node<vec4>}
25+
*/
26+
export const triplanarTextures = /*@__PURE__*/ Fn( ( [ textureXNode, textureYNode = null, textureZNode = null, scaleNode = float( 1 ), positionNode = positionLocal, normalNode = normalLocal ] ) => {
27+
28+
// Reference: https://github.com/keijiro/StandardTriplanar
29+
30+
// Blending factor of triplanar mapping
31+
let bf = normalNode.abs().normalize();
32+
bf = bf.div( bf.dot( vec3( 1.0 ) ) );
33+
34+
// Triplanar mapping
35+
const tx = positionNode.yz.mul( scaleNode );
36+
const ty = positionNode.zx.mul( scaleNode );
37+
const tz = positionNode.xy.mul( scaleNode );
38+
39+
// Base color
40+
const textureX = textureXNode.value;
41+
const textureY = textureYNode !== null ? textureYNode.value : textureX;
42+
const textureZ = textureZNode !== null ? textureZNode.value : textureX;
43+
44+
const cx = texture( textureX, tx ).mul( bf.x );
45+
const cy = texture( textureY, ty ).mul( bf.y );
46+
const cz = texture( textureZ, tz ).mul( bf.z );
47+
48+
return add( cx, cy, cz );
49+
50+
} );
51+
52+
/**
53+
* TSL function for creating a triplanar textures node.
54+
*
55+
* @tsl
56+
* @function
57+
* @param {Node} textureXNode - First texture node.
58+
* @param {?Node} [textureYNode=null] - Second texture node. When not set, the shader will sample from `textureXNode` instead.
59+
* @param {?Node} [textureZNode=null] - Third texture node. When not set, the shader will sample from `textureXNode` instead.
60+
* @param {?Node<float>} [scaleNode=float(1)] - The scale node.
61+
* @param {?Node<vec3>} [positionNode=positionLocal] - Vertex positions in local space.
62+
* @param {?Node<vec3>} [normalNode=normalLocal] - Normals in local space.
63+
* @returns {Node<vec4>}
64+
*/
65+
export const triplanarTexture = ( ...params ) => triplanarTextures( ...params );

src/nodes/utils/TriplanarTexturesNode.js

Lines changed: 0 additions & 148 deletions
This file was deleted.

0 commit comments

Comments
 (0)