PositionNode.js 1.7 KB

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