PositionNode.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. isGlobal() {
  19. return true;
  20. }
  21. getHash( /*builder*/ ) {
  22. return `position-${this.scope}`;
  23. }
  24. generate( builder ) {
  25. const scope = this.scope;
  26. let outputNode = null;
  27. if ( scope === PositionNode.GEOMETRY ) {
  28. outputNode = new AttributeNode( 'position', 'vec3' );
  29. } else if ( scope === PositionNode.LOCAL ) {
  30. outputNode = new VaryingNode( new PositionNode( PositionNode.GEOMETRY ) );
  31. } else if ( scope === PositionNode.WORLD ) {
  32. const vertexPositionNode = new MathNode( MathNode.TRANSFORM_DIRECTION, new ModelNode( ModelNode.WORLD_MATRIX ), new PositionNode( PositionNode.LOCAL ) );
  33. outputNode = new VaryingNode( vertexPositionNode );
  34. } else if ( scope === PositionNode.VIEW ) {
  35. const vertexPositionNode = new OperatorNode( '*', new ModelNode( ModelNode.VIEW_MATRIX ), new PositionNode( PositionNode.LOCAL ) );
  36. outputNode = new VaryingNode( vertexPositionNode );
  37. } else if ( scope === PositionNode.VIEW_DIRECTION ) {
  38. const vertexPositionNode = new MathNode( MathNode.NEGATE, new PositionNode( PositionNode.VIEW ) );
  39. outputNode = new MathNode( MathNode.NORMALIZE, new VaryingNode( vertexPositionNode ) );
  40. } else if ( scope === PositionNode.WORLD_DIRECTION ) {
  41. const vertexPositionNode = new MathNode( MathNode.NEGATE, new PositionNode( PositionNode.WORLD ) );
  42. outputNode = new MathNode( MathNode.NORMALIZE, new VaryingNode( vertexPositionNode ) );
  43. }
  44. return outputNode.build( builder, this.getNodeType( builder ) );
  45. }
  46. serialize( data ) {
  47. super.serialize( data );
  48. data.scope = this.scope;
  49. }
  50. deserialize( data ) {
  51. super.deserialize( data );
  52. this.scope = data.scope;
  53. }
  54. }
  55. export default PositionNode;