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