PositionNode.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 WORLD_DIRECTION = 'worldDirection';
  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 VaryingNode( 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 VaryingNode( vertexPositionNode );
  31. } else if ( scope === PositionNode.VIEW ) {
  32. const vertexPositionNode = new OperatorNode( '*', new ModelNode( ModelNode.VIEW_MATRIX ), new PositionNode( PositionNode.LOCAL ) );
  33. outputNode = new VaryingNode( 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 VaryingNode( vertexPositionNode ) );
  37. } else if ( scope === PositionNode.WORLD_DIRECTION ) {
  38. const vertexPositionNode = new MathNode( MathNode.NEGATE, new PositionNode( PositionNode.WORLD ) );
  39. outputNode = new MathNode( MathNode.NORMALIZE, new VaryingNode( vertexPositionNode ) );
  40. }
  41. return outputNode.build( builder, this.getNodeType( builder ) );
  42. }
  43. serialize( data ) {
  44. super.serialize( data );
  45. data.scope = this.scope;
  46. }
  47. deserialize( data ) {
  48. super.deserialize( data );
  49. this.scope = data.scope;
  50. }
  51. }
  52. export default PositionNode;