BatchNode.js 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import Node, { addNodeClass } from '../core/Node.js';
  2. import { normalLocal } from './NormalNode.js';
  3. import { positionLocal } from './PositionNode.js';
  4. import { nodeProxy, vec3, mat3, mat4, int, ivec2, float } from '../shadernode/ShaderNode.js';
  5. import { textureLoad } from './TextureNode.js';
  6. import { textureSize } from './TextureSizeNode.js';
  7. import { attribute } from '../core/AttributeNode.js';
  8. import { tangentLocal } from './TangentNode.js';
  9. class BatchNode extends Node {
  10. constructor( batchMesh ) {
  11. super( 'void' );
  12. this.batchMesh = batchMesh;
  13. this.instanceColorNode = null;
  14. this.batchingIdNode = null;
  15. }
  16. setup( builder ) {
  17. // POSITION
  18. if ( this.batchingIdNode === null ) {
  19. this.batchingIdNode = attribute( 'batchId' );
  20. }
  21. const matriceTexture = this.batchMesh._matricesTexture;
  22. const size = textureSize( textureLoad( matriceTexture ), 0 );
  23. const j = float( int( this.batchingIdNode ) ).mul( 4 ).toVar();
  24. const x = int( j.mod( size ) );
  25. const y = int( j ).div( int( size ) );
  26. const batchingMatrix = mat4(
  27. textureLoad( matriceTexture, ivec2( x, y ) ),
  28. textureLoad( matriceTexture, ivec2( x.add( 1 ), y ) ),
  29. textureLoad( matriceTexture, ivec2( x.add( 2 ), y ) ),
  30. textureLoad( matriceTexture, ivec2( x.add( 3 ), y ) )
  31. );
  32. const bm = mat3(
  33. batchingMatrix[ 0 ].xyz,
  34. batchingMatrix[ 1 ].xyz,
  35. batchingMatrix[ 2 ].xyz
  36. );
  37. positionLocal.assign( batchingMatrix.mul( positionLocal ) );
  38. const transformedNormal = normalLocal.div( vec3( bm[ 0 ].dot( bm[ 0 ] ), bm[ 1 ].dot( bm[ 1 ] ), bm[ 2 ].dot( bm[ 2 ] ) ) );
  39. const batchingNormal = bm.mul( transformedNormal ).xyz;
  40. normalLocal.assign( batchingNormal );
  41. if ( builder.hasGeometryAttribute( 'tangent' ) ) {
  42. tangentLocal.mulAssign( bm );
  43. }
  44. }
  45. }
  46. export default BatchNode;
  47. export const batch = nodeProxy( BatchNode );
  48. addNodeClass( 'batch', BatchNode );