|
| 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 ); |
0 commit comments