TriplanarTexturesNode.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import Node from '../core/Node.js';
  2. import { float, vec3, add, mul, div, dot, normalize, abs, texture, positionWorld, normalWorld } from '../shadernode/ShaderNodeBaseElements.js';
  3. class TriplanarTexturesNode extends Node {
  4. constructor( textureXNode, textureYNode = null, textureZNode = null, scaleNode = float( 1 ), positionNode = positionWorld, normalNode = normalWorld ) {
  5. super( 'vec4' );
  6. this.textureXNode = textureXNode;
  7. this.textureYNode = textureYNode;
  8. this.textureZNode = textureZNode;
  9. this.scaleNode = scaleNode;
  10. this.positionNode = positionNode;
  11. this.normalNode = normalNode;
  12. }
  13. construct() {
  14. const { textureXNode, textureYNode, textureZNode, scaleNode, positionNode, normalNode } = this;
  15. // Ref: https://github.com/keijiro/StandardTriplanar
  16. // Blending factor of triplanar mapping
  17. let bf = normalize( abs( normalNode ) );
  18. bf = div( bf, dot( bf, vec3( 1.0 ) ) );
  19. // Triplanar mapping
  20. const tx = mul( positionNode.yz, scaleNode );
  21. const ty = mul( positionNode.zx, scaleNode );
  22. const tz = mul( positionNode.xy, scaleNode );
  23. // Base color
  24. const textureX = textureXNode.value;
  25. const textureY = textureYNode !== null ? textureYNode.value : textureX;
  26. const textureZ = textureZNode !== null ? textureZNode.value : textureX;
  27. const cx = mul( texture( textureX, tx ), bf.x );
  28. const cy = mul( texture( textureY, ty ), bf.y );
  29. const cz = mul( texture( textureZ, tz ), bf.z );
  30. return add( cx, cy, cz );
  31. }
  32. }
  33. export default TriplanarTexturesNode;