NormalNode.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  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 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 VaryingNode( 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 VaryingNode( 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 VaryingNode( vertexNormalNode ) );
  34. }
  35. return outputNode.build( builder );
  36. }
  37. serialize( data ) {
  38. super.serialize( data );
  39. data.scope = this.scope;
  40. }
  41. deserialize( data ) {
  42. super.deserialize( data );
  43. this.scope = data.scope;
  44. }
  45. }
  46. export default NormalNode;