NormalNode.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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 CameraNode from '../accessors/CameraNode.js';
  6. import OperatorNode from '../math/OperatorNode.js';
  7. import MathNode from '../math/MathNode.js';
  8. class NormalNode extends Node {
  9. static GEOMETRY = 'geometry';
  10. static LOCAL = 'local';
  11. static WORLD = 'world';
  12. static VIEW = 'view';
  13. constructor( scope = NormalNode.LOCAL ) {
  14. super( 'vec3' );
  15. this.scope = scope;
  16. }
  17. getHash( /*builder*/ ) {
  18. return `normal-${this.scope}`;
  19. }
  20. generate( builder ) {
  21. const scope = this.scope;
  22. let outputNode = null;
  23. if ( scope === NormalNode.GEOMETRY ) {
  24. outputNode = new AttributeNode( 'normal', 'vec3' );
  25. } else if ( scope === NormalNode.LOCAL ) {
  26. outputNode = new VaryNode( new NormalNode( NormalNode.GEOMETRY ) );
  27. } else if ( scope === NormalNode.VIEW ) {
  28. const vertexNormalNode = new OperatorNode( '*', new ModelNode( ModelNode.NORMAL_MATRIX ), new NormalNode( NormalNode.LOCAL ) );
  29. outputNode = new MathNode( MathNode.NORMALIZE, new VaryNode( vertexNormalNode ) );
  30. } else if ( scope === NormalNode.WORLD ) {
  31. // To use INVERSE_TRANSFORM_DIRECTION only inverse the param order like this: MathNode( ..., Vector, Matrix );
  32. const vertexNormalNode = new MathNode( MathNode.TRANSFORM_DIRECTION, new NormalNode( NormalNode.VIEW ), new CameraNode( CameraNode.VIEW_MATRIX ) );
  33. outputNode = new MathNode( MathNode.NORMALIZE, new VaryNode( vertexNormalNode ) );
  34. }
  35. return outputNode.build( builder );
  36. }
  37. }
  38. export default NormalNode;