BatchNode.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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, tslFn } from '../shadernode/ShaderNode.js';
  5. import { textureLoad } from './TextureNode.js';
  6. import { textureSize } from './TextureSizeNode.js';
  7. import { tangentLocal } from './TangentNode.js';
  8. import { instanceIndex, drawIndex } from '../core/IndexNode.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. if ( builder.getDrawIndex() === null ) {
  20. this.batchingIdNode = instanceIndex;
  21. } else {
  22. this.batchingIdNode = drawIndex;
  23. }
  24. }
  25. const getIndirectIndex = tslFn( ( [ id ] ) => {
  26. const size = textureSize( textureLoad( this.batchMesh._indirectTexture ), 0 );
  27. const x = int( id ).remainder( int( size ) );
  28. const y = int( id ).div( int( size ) );
  29. return textureLoad( this.batchMesh._indirectTexture, ivec2( x, y ), null, 'uvec4' ).x;
  30. } ).setLayout( {
  31. name: 'getIndirectIndex',
  32. type: 'uint',
  33. inputs: [
  34. { name: 'id', type: 'int' }
  35. ]
  36. } );
  37. const matriceTexture = this.batchMesh._matricesTexture;
  38. const size = textureSize( textureLoad( matriceTexture ), 0 );
  39. const j = float( getIndirectIndex( int( this.batchingIdNode ) ) ).mul( 4 ).toVar();
  40. const x = int( j.mod( size ) );
  41. const y = int( j ).div( int( size ) );
  42. const batchingMatrix = mat4(
  43. textureLoad( matriceTexture, ivec2( x, y ) ),
  44. textureLoad( matriceTexture, ivec2( x.add( 1 ), y ) ),
  45. textureLoad( matriceTexture, ivec2( x.add( 2 ), y ) ),
  46. textureLoad( matriceTexture, ivec2( x.add( 3 ), y ) )
  47. );
  48. const bm = mat3(
  49. batchingMatrix[ 0 ].xyz,
  50. batchingMatrix[ 1 ].xyz,
  51. batchingMatrix[ 2 ].xyz
  52. );
  53. positionLocal.assign( batchingMatrix.mul( positionLocal ) );
  54. const transformedNormal = normalLocal.div( vec3( bm[ 0 ].dot( bm[ 0 ] ), bm[ 1 ].dot( bm[ 1 ] ), bm[ 2 ].dot( bm[ 2 ] ) ) );
  55. const batchingNormal = bm.mul( transformedNormal ).xyz;
  56. normalLocal.assign( batchingNormal );
  57. if ( builder.hasGeometryAttribute( 'tangent' ) ) {
  58. tangentLocal.mulAssign( bm );
  59. }
  60. }
  61. }
  62. export default BatchNode;
  63. export const batch = nodeProxy( BatchNode );
  64. addNodeClass( 'batch', BatchNode );