PositionNode.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  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. import { transformDirection } from '../functions/MathFunctions.js';
  8. class PositionNode extends Node {
  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. generate( builder ) {
  18. const scope = this.scope;
  19. let outputNode = null;
  20. if ( scope === PositionNode.LOCAL ) {
  21. outputNode = new AttributeNode( 'position', 'vec3' );
  22. } else if ( scope === PositionNode.WORLD ) {
  23. const vertexPositionNode = transformDirection.call( { dir: new PositionNode( PositionNode.LOCAL ), matrix: new ModelNode( ModelNode.WORLD_MATRIX ) } );
  24. outputNode = new VaryNode( vertexPositionNode );
  25. } else if ( scope === PositionNode.VIEW ) {
  26. const vertexPositionNode = new OperatorNode( '*', new ModelNode( ModelNode.VIEW_MATRIX ), new PositionNode( PositionNode.LOCAL ) );
  27. outputNode = new VaryNode( vertexPositionNode );
  28. } else if ( scope === PositionNode.VIEW_DIRECTION ) {
  29. const vertexPositionNode = new MathNode( MathNode.NEGATE, new PositionNode( PositionNode.VIEW ) );
  30. outputNode = new MathNode( MathNode.NORMALIZE, new VaryNode( vertexPositionNode ) );
  31. }
  32. return outputNode.build( builder, this.getNodeType( builder ) );
  33. }
  34. }
  35. export default PositionNode;