NormalNode.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  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, vec3 } 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. const geometryAttribute = builder.hasGeometryAttribute( 'normal' );
  25. if ( geometryAttribute === false ) {
  26. outputNode = vec3( 0, 1, 0 );
  27. } else {
  28. outputNode = attribute( 'normal', 'vec3' );
  29. }
  30. } else if ( scope === NormalNode.LOCAL ) {
  31. outputNode = varying( normalGeometry );
  32. } else if ( scope === NormalNode.VIEW ) {
  33. const vertexNode = modelNormalMatrix.mul( normalLocal );
  34. outputNode = normalize( varying( vertexNode ) );
  35. } else if ( scope === NormalNode.WORLD ) {
  36. // To use inverseTransformDirection only inverse the param order like this: cameraViewMatrix.transformDirection( normalView )
  37. const vertexNode = normalView.transformDirection( cameraViewMatrix );
  38. outputNode = normalize( varying( vertexNode ) );
  39. }
  40. return outputNode.build( builder, this.getNodeType( builder ) );
  41. }
  42. serialize( data ) {
  43. super.serialize( data );
  44. data.scope = this.scope;
  45. }
  46. deserialize( data ) {
  47. super.deserialize( data );
  48. this.scope = data.scope;
  49. }
  50. }
  51. NormalNode.GEOMETRY = 'geometry';
  52. NormalNode.LOCAL = 'local';
  53. NormalNode.VIEW = 'view';
  54. NormalNode.WORLD = 'world';
  55. export default NormalNode;
  56. export const normalGeometry = nodeImmutable( NormalNode, NormalNode.GEOMETRY );
  57. export const normalLocal = nodeImmutable( NormalNode, NormalNode.LOCAL ).temp( 'Normal' );
  58. export const normalView = nodeImmutable( NormalNode, NormalNode.VIEW );
  59. export const normalWorld = nodeImmutable( NormalNode, NormalNode.WORLD );
  60. export const transformedNormalView = property( 'vec3', 'TransformedNormalView' );
  61. export const transformedNormalWorld = transformedNormalView.transformDirection( cameraViewMatrix ).normalize();
  62. export const transformedClearcoatNormalView = property( 'vec3', 'TransformedClearcoatNormalView' );
  63. addNodeClass( 'NormalNode', NormalNode );