TriplanarTexturesNode.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import Node, { addNodeClass } from '../core/Node.js';
  2. import { add } from '../math/OperatorNode.js';
  3. import { normalLocal } from '../accessors/NormalNode.js';
  4. import { positionLocal } from '../accessors/PositionNode.js';
  5. import { texture } from '../accessors/TextureNode.js';
  6. import { addNodeElement, nodeProxy, float, vec3 } from '../shadernode/ShaderNode.js';
  7. class TriplanarTexturesNode extends Node {
  8. constructor( textureXNode, textureYNode = null, textureZNode = null, scaleNode = float( 1 ), positionNode = positionLocal, normalNode = normalLocal ) {
  9. super( 'vec4' );
  10. this.textureXNode = textureXNode;
  11. this.textureYNode = textureYNode;
  12. this.textureZNode = textureZNode;
  13. this.scaleNode = scaleNode;
  14. this.positionNode = positionNode;
  15. this.normalNode = normalNode;
  16. }
  17. setup() {
  18. const { textureXNode, textureYNode, textureZNode, scaleNode, positionNode, normalNode } = this;
  19. // Ref: https://github.com/keijiro/StandardTriplanar
  20. // Blending factor of triplanar mapping
  21. let bf = normalNode.abs().normalize();
  22. bf = bf.div( bf.dot( vec3( 1.0 ) ) );
  23. // Triplanar mapping
  24. const tx = positionNode.yz.mul( scaleNode );
  25. const ty = positionNode.zx.mul( scaleNode );
  26. const tz = positionNode.xy.mul( scaleNode );
  27. // Base color
  28. const textureX = textureXNode.value;
  29. const textureY = textureYNode !== null ? textureYNode.value : textureX;
  30. const textureZ = textureZNode !== null ? textureZNode.value : textureX;
  31. const cx = texture( textureX, tx ).mul( bf.x );
  32. const cy = texture( textureY, ty ).mul( bf.y );
  33. const cz = texture( textureZ, tz ).mul( bf.z );
  34. return add( cx, cy, cz );
  35. }
  36. }
  37. export default TriplanarTexturesNode;
  38. export const triplanarTextures = nodeProxy( TriplanarTexturesNode );
  39. export const triplanarTexture = ( ...params ) => triplanarTextures( ...params );
  40. addNodeElement( 'triplanarTexture', triplanarTexture );
  41. addNodeClass( 'TriplanarTexturesNode', TriplanarTexturesNode );