PositionNode.js 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import Node from '../core/Node.js';
  2. import AttributeNode from '../core/AttributeNode.js';
  3. import VaryingNode from '../core/VaryingNode.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 VaryingNode( 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 VaryingNode( vertexPositionNode );
  30. } else if ( scope === PositionNode.VIEW ) {
  31. const vertexPositionNode = new OperatorNode( '*', new ModelNode( ModelNode.VIEW_MATRIX ), new PositionNode( PositionNode.LOCAL ) );
  32. outputNode = new VaryingNode( 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 VaryingNode( vertexPositionNode ) );
  36. }
  37. return outputNode.build( builder, this.getNodeType( builder ) );
  38. }
  39. serialize( data ) {
  40. super.serialize( data );
  41. data.scope = this.scope;
  42. }
  43. deserialize( data ) {
  44. super.deserialize( data );
  45. this.scope = data.scope;
  46. }
  47. }
  48. export default PositionNode;