PositionNode.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import Node from '../core/Node.js';
  2. import AttributeNode from '../core/AttributeNode.js';
  3. import NodeKeywords from '../core/NodeKeywords.js';
  4. import VaryNode from '../core/VaryNode.js';
  5. import ModelNode from '../accessors/ModelNode.js';
  6. import MathNode from '../math/MathNode.js';
  7. import OperatorNode from '../math/OperatorNode.js';
  8. class PositionNode extends Node {
  9. static GEOMETRY = 'geometry';
  10. static LOCAL = 'local';
  11. static WORLD = 'world';
  12. static VIEW = 'view';
  13. static VIEW_DIRECTION = 'viewDirection';
  14. constructor( scope = PositionNode.LOCAL ) {
  15. super( 'vec3' );
  16. this.scope = scope;
  17. }
  18. getHash( /*builder*/ ) {
  19. return `position-${this.scope}`;
  20. }
  21. generate( builder ) {
  22. const scope = this.scope;
  23. let outputNode = null;
  24. if ( scope === PositionNode.GEOMETRY ) {
  25. outputNode = new AttributeNode( 'position', 'vec3' );
  26. } else if ( scope === PositionNode.LOCAL ) {
  27. outputNode = new VaryNode( new PositionNode( PositionNode.GEOMETRY ) );
  28. } else if ( scope === PositionNode.WORLD ) {
  29. const vertexPositionNode = new MathNode( MathNode.TRANSFORM_DIRECTION, new ModelNode( ModelNode.WORLD_MATRIX ), new PositionNode( PositionNode.LOCAL ) );
  30. outputNode = new VaryNode( vertexPositionNode );
  31. } else if ( scope === PositionNode.VIEW ) {
  32. const vertexPositionNode = new OperatorNode( '*', new ModelNode( ModelNode.VIEW_MATRIX ), new PositionNode( PositionNode.LOCAL ) );
  33. outputNode = new VaryNode( vertexPositionNode );
  34. } else if ( scope === PositionNode.VIEW_DIRECTION ) {
  35. const vertexPositionNode = new MathNode( MathNode.NEGATE, new PositionNode( PositionNode.VIEW ) );
  36. outputNode = new MathNode( MathNode.NORMALIZE, new VaryNode( vertexPositionNode ) );
  37. }
  38. return outputNode.build( builder, this.getNodeType( builder ) );
  39. }
  40. }
  41. export default PositionNode;