BitangentNode.js 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import Node, { addNodeClass } from '../core/Node.js';
  2. import { varying } from '../core/VaryingNode.js';
  3. import { normalize } from '../math/MathNode.js';
  4. import { cameraViewMatrix } from './CameraNode.js';
  5. import { normalGeometry, normalLocal, normalView, normalWorld, transformedNormalView } from './NormalNode.js';
  6. import { tangentGeometry, tangentLocal, tangentView, tangentWorld, transformedTangentView } from './TangentNode.js';
  7. import { nodeImmutable } from '../shadernode/ShaderNode.js';
  8. class BitangentNode extends Node {
  9. constructor( scope = BitangentNode.LOCAL ) {
  10. super( 'vec3' );
  11. this.scope = scope;
  12. }
  13. getHash( /*builder*/ ) {
  14. return `bitangent-${this.scope}`;
  15. }
  16. generate( builder ) {
  17. const scope = this.scope;
  18. let crossNormalTangent;
  19. if ( scope === BitangentNode.GEOMETRY ) {
  20. crossNormalTangent = normalGeometry.cross( tangentGeometry );
  21. } else if ( scope === BitangentNode.LOCAL ) {
  22. crossNormalTangent = normalLocal.cross( tangentLocal );
  23. } else if ( scope === BitangentNode.VIEW ) {
  24. crossNormalTangent = normalView.cross( tangentView );
  25. } else if ( scope === BitangentNode.WORLD ) {
  26. crossNormalTangent = normalWorld.cross( tangentWorld );
  27. }
  28. const vertexNode = crossNormalTangent.mul( tangentGeometry.w ).xyz;
  29. const outputNode = normalize( varying( vertexNode ) );
  30. return outputNode.build( builder, this.getNodeType( builder ) );
  31. }
  32. serialize( data ) {
  33. super.serialize( data );
  34. data.scope = this.scope;
  35. }
  36. deserialize( data ) {
  37. super.deserialize( data );
  38. this.scope = data.scope;
  39. }
  40. }
  41. BitangentNode.GEOMETRY = 'geometry';
  42. BitangentNode.LOCAL = 'local';
  43. BitangentNode.VIEW = 'view';
  44. BitangentNode.WORLD = 'world';
  45. export default BitangentNode;
  46. export const bitangentGeometry = nodeImmutable( BitangentNode, BitangentNode.GEOMETRY );
  47. export const bitangentLocal = nodeImmutable( BitangentNode, BitangentNode.LOCAL );
  48. export const bitangentView = nodeImmutable( BitangentNode, BitangentNode.VIEW );
  49. export const bitangentWorld = nodeImmutable( BitangentNode, BitangentNode.WORLD );
  50. export const transformedBitangentView = normalize( transformedNormalView.cross( transformedTangentView ).mul( tangentGeometry.w ) );
  51. export const transformedBitangentWorld = normalize( transformedBitangentView.transformDirection( cameraViewMatrix ) );
  52. addNodeClass( BitangentNode );