NormalNode.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. import Node, { addNodeClass } from '../core/Node.js';
  2. import { attribute } from '../core/AttributeNode.js';
  3. import { varying } from '../core/VaryingNode.js';
  4. import { property } from '../core/PropertyNode.js';
  5. import { normalize } from '../math/MathNode.js';
  6. import { cameraViewMatrix } from './CameraNode.js';
  7. import { modelNormalMatrix } from './ModelNode.js';
  8. import { nodeImmutable } from '../shadernode/ShaderNode.js';
  9. class NormalNode extends Node {
  10. constructor( scope = NormalNode.LOCAL ) {
  11. super( 'vec3' );
  12. this.scope = scope;
  13. }
  14. isGlobal() {
  15. return true;
  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 = attribute( 'normal', 'vec3' );
  25. } else if ( scope === NormalNode.LOCAL ) {
  26. outputNode = varying( normalGeometry );
  27. } else if ( scope === NormalNode.VIEW ) {
  28. const vertexNode = modelNormalMatrix.mul( normalLocal );
  29. outputNode = normalize( varying( vertexNode ) );
  30. } else if ( scope === NormalNode.WORLD ) {
  31. // To use inverseTransformDirection only inverse the param order like this: cameraViewMatrix.transformDirection( normalView )
  32. const vertexNode = normalView.transformDirection( cameraViewMatrix );
  33. outputNode = normalize( varying( vertexNode ) );
  34. }
  35. return outputNode.build( builder, this.getNodeType( 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. NormalNode.GEOMETRY = 'geometry';
  47. NormalNode.LOCAL = 'local';
  48. NormalNode.VIEW = 'view';
  49. NormalNode.WORLD = 'world';
  50. export default NormalNode;
  51. export const normalGeometry = nodeImmutable( NormalNode, NormalNode.GEOMETRY );
  52. export const normalLocal = nodeImmutable( NormalNode, NormalNode.LOCAL ).temp( 'Normal' );
  53. export const normalView = nodeImmutable( NormalNode, NormalNode.VIEW );
  54. export const normalWorld = nodeImmutable( NormalNode, NormalNode.WORLD );
  55. export const transformedNormalView = property( 'vec3', 'TransformedNormalView' );
  56. export const transformedNormalWorld = transformedNormalView.transformDirection( cameraViewMatrix ).normalize();
  57. export const transformedClearcoatNormalView = property( 'vec3', 'TransformedClearcoatNormalView' );
  58. addNodeClass( 'NormalNode', NormalNode );