File size: 24,271 Bytes
b797337 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 | import { BufferAttribute } from '../core/BufferAttribute.js';
import { BufferGeometry } from '../core/BufferGeometry.js';
import { DataTexture } from '../textures/DataTexture.js';
import { FloatType } from '../constants.js';
import { Matrix4 } from '../math/Matrix4.js';
import { Mesh } from './Mesh.js';
import { RGBAFormat } from '../constants.js';
import { Box3 } from '../math/Box3.js';
import { Sphere } from '../math/Sphere.js';
import { Frustum } from '../math/Frustum.js';
import { WebGLCoordinateSystem } from '../constants.js';
import { WebGPUCoordinateSystem } from '../constants.js';
import { Vector3 } from '../math/Vector3.js';
function sortOpaque( a, b ) {
return a.z - b.z;
}
function sortTransparent( a, b ) {
return b.z - a.z;
}
class MultiDrawRenderList {
constructor() {
this.index = 0;
this.pool = [];
this.list = [];
}
push( drawRange, z ) {
const pool = this.pool;
const list = this.list;
if ( this.index >= pool.length ) {
pool.push( {
start: - 1,
count: - 1,
z: - 1,
} );
}
const item = pool[ this.index ];
list.push( item );
this.index ++;
item.start = drawRange.start;
item.count = drawRange.count;
item.z = z;
}
reset() {
this.list.length = 0;
this.index = 0;
}
}
const ID_ATTR_NAME = 'batchId';
const _matrix = /*@__PURE__*/ new Matrix4();
const _invMatrixWorld = /*@__PURE__*/ new Matrix4();
const _identityMatrix = /*@__PURE__*/ new Matrix4();
const _projScreenMatrix = /*@__PURE__*/ new Matrix4();
const _frustum = /*@__PURE__*/ new Frustum();
const _box = /*@__PURE__*/ new Box3();
const _sphere = /*@__PURE__*/ new Sphere();
const _vector = /*@__PURE__*/ new Vector3();
const _renderList = /*@__PURE__*/ new MultiDrawRenderList();
const _mesh = /*@__PURE__*/ new Mesh();
const _batchIntersects = [];
// @TODO: SkinnedMesh support?
// @TODO: geometry.groups support?
// @TODO: geometry.drawRange support?
// @TODO: geometry.morphAttributes support?
// @TODO: Support uniform parameter per geometry
// @TODO: Add an "optimize" function to pack geometry and remove data gaps
// copies data from attribute "src" into "target" starting at "targetOffset"
function copyAttributeData( src, target, targetOffset = 0 ) {
const itemSize = target.itemSize;
if ( src.isInterleavedBufferAttribute || src.array.constructor !== target.array.constructor ) {
// use the component getters and setters if the array data cannot
// be copied directly
const vertexCount = src.count;
for ( let i = 0; i < vertexCount; i ++ ) {
for ( let c = 0; c < itemSize; c ++ ) {
target.setComponent( i + targetOffset, c, src.getComponent( i, c ) );
}
}
} else {
// faster copy approach using typed array set function
target.array.set( src.array, targetOffset * itemSize );
}
target.needsUpdate = true;
}
class BatchedMesh extends Mesh {
get maxGeometryCount() {
return this._maxGeometryCount;
}
constructor( maxGeometryCount, maxVertexCount, maxIndexCount = maxVertexCount * 2, material ) {
super( new BufferGeometry(), material );
this.isBatchedMesh = true;
this.perObjectFrustumCulled = true;
this.sortObjects = true;
this.boundingBox = null;
this.boundingSphere = null;
this.customSort = null;
this._drawRanges = [];
this._reservedRanges = [];
this._visibility = [];
this._active = [];
this._bounds = [];
this._maxGeometryCount = maxGeometryCount;
this._maxVertexCount = maxVertexCount;
this._maxIndexCount = maxIndexCount;
this._geometryInitialized = false;
this._geometryCount = 0;
this._multiDrawCounts = new Int32Array( maxGeometryCount );
this._multiDrawStarts = new Int32Array( maxGeometryCount );
this._multiDrawCount = 0;
this._visibilityChanged = true;
// Local matrix per geometry by using data texture
this._matricesTexture = null;
this._initMatricesTexture();
}
_initMatricesTexture() {
// layout (1 matrix = 4 pixels)
// RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
// with 8x8 pixel texture max 16 matrices * 4 pixels = (8 * 8)
// 16x16 pixel texture max 64 matrices * 4 pixels = (16 * 16)
// 32x32 pixel texture max 256 matrices * 4 pixels = (32 * 32)
// 64x64 pixel texture max 1024 matrices * 4 pixels = (64 * 64)
let size = Math.sqrt( this._maxGeometryCount * 4 ); // 4 pixels needed for 1 matrix
size = Math.ceil( size / 4 ) * 4;
size = Math.max( size, 4 );
const matricesArray = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel
const matricesTexture = new DataTexture( matricesArray, size, size, RGBAFormat, FloatType );
this._matricesTexture = matricesTexture;
}
_initializeGeometry( reference ) {
const geometry = this.geometry;
const maxVertexCount = this._maxVertexCount;
const maxGeometryCount = this._maxGeometryCount;
const maxIndexCount = this._maxIndexCount;
if ( this._geometryInitialized === false ) {
for ( const attributeName in reference.attributes ) {
const srcAttribute = reference.getAttribute( attributeName );
const { array, itemSize, normalized } = srcAttribute;
const dstArray = new array.constructor( maxVertexCount * itemSize );
const dstAttribute = new srcAttribute.constructor( dstArray, itemSize, normalized );
dstAttribute.setUsage( srcAttribute.usage );
geometry.setAttribute( attributeName, dstAttribute );
}
if ( reference.getIndex() !== null ) {
const indexArray = maxVertexCount > 65536
? new Uint32Array( maxIndexCount )
: new Uint16Array( maxIndexCount );
geometry.setIndex( new BufferAttribute( indexArray, 1 ) );
}
const idArray = maxGeometryCount > 65536
? new Uint32Array( maxVertexCount )
: new Uint16Array( maxVertexCount );
geometry.setAttribute( ID_ATTR_NAME, new BufferAttribute( idArray, 1 ) );
this._geometryInitialized = true;
}
}
// Make sure the geometry is compatible with the existing combined geometry atributes
_validateGeometry( geometry ) {
// check that the geometry doesn't have a version of our reserved id attribute
if ( geometry.getAttribute( ID_ATTR_NAME ) ) {
throw new Error( `BatchedMesh: Geometry cannot use attribute "${ ID_ATTR_NAME }"` );
}
// check to ensure the geometries are using consistent attributes and indices
const batchGeometry = this.geometry;
if ( Boolean( geometry.getIndex() ) !== Boolean( batchGeometry.getIndex() ) ) {
throw new Error( 'BatchedMesh: All geometries must consistently have "index".' );
}
for ( const attributeName in batchGeometry.attributes ) {
if ( attributeName === ID_ATTR_NAME ) {
continue;
}
if ( ! geometry.hasAttribute( attributeName ) ) {
throw new Error( `BatchedMesh: Added geometry missing "${ attributeName }". All geometries must have consistent attributes.` );
}
const srcAttribute = geometry.getAttribute( attributeName );
const dstAttribute = batchGeometry.getAttribute( attributeName );
if ( srcAttribute.itemSize !== dstAttribute.itemSize || srcAttribute.normalized !== dstAttribute.normalized ) {
throw new Error( 'BatchedMesh: All attributes must have a consistent itemSize and normalized value.' );
}
}
}
setCustomSort( func ) {
this.customSort = func;
return this;
}
computeBoundingBox() {
if ( this.boundingBox === null ) {
this.boundingBox = new Box3();
}
const geometryCount = this._geometryCount;
const boundingBox = this.boundingBox;
const active = this._active;
boundingBox.makeEmpty();
for ( let i = 0; i < geometryCount; i ++ ) {
if ( active[ i ] === false ) continue;
this.getMatrixAt( i, _matrix );
this.getBoundingBoxAt( i, _box ).applyMatrix4( _matrix );
boundingBox.union( _box );
}
}
computeBoundingSphere() {
if ( this.boundingSphere === null ) {
this.boundingSphere = new Sphere();
}
const geometryCount = this._geometryCount;
const boundingSphere = this.boundingSphere;
const active = this._active;
boundingSphere.makeEmpty();
for ( let i = 0; i < geometryCount; i ++ ) {
if ( active[ i ] === false ) continue;
this.getMatrixAt( i, _matrix );
this.getBoundingSphereAt( i, _sphere ).applyMatrix4( _matrix );
boundingSphere.union( _sphere );
}
}
addGeometry( geometry, vertexCount = - 1, indexCount = - 1 ) {
this._initializeGeometry( geometry );
this._validateGeometry( geometry );
// ensure we're not over geometry
if ( this._geometryCount >= this._maxGeometryCount ) {
throw new Error( 'BatchedMesh: Maximum geometry count reached.' );
}
// get the necessary range fo the geometry
const reservedRange = {
vertexStart: - 1,
vertexCount: - 1,
indexStart: - 1,
indexCount: - 1,
};
let lastRange = null;
const reservedRanges = this._reservedRanges;
const drawRanges = this._drawRanges;
const bounds = this._bounds;
if ( this._geometryCount !== 0 ) {
lastRange = reservedRanges[ reservedRanges.length - 1 ];
}
if ( vertexCount === - 1 ) {
reservedRange.vertexCount = geometry.getAttribute( 'position' ).count;
} else {
reservedRange.vertexCount = vertexCount;
}
if ( lastRange === null ) {
reservedRange.vertexStart = 0;
} else {
reservedRange.vertexStart = lastRange.vertexStart + lastRange.vertexCount;
}
const index = geometry.getIndex();
const hasIndex = index !== null;
if ( hasIndex ) {
if ( indexCount === - 1 ) {
reservedRange.indexCount = index.count;
} else {
reservedRange.indexCount = indexCount;
}
if ( lastRange === null ) {
reservedRange.indexStart = 0;
} else {
reservedRange.indexStart = lastRange.indexStart + lastRange.indexCount;
}
}
if (
reservedRange.indexStart !== - 1 &&
reservedRange.indexStart + reservedRange.indexCount > this._maxIndexCount ||
reservedRange.vertexStart + reservedRange.vertexCount > this._maxVertexCount
) {
throw new Error( 'BatchedMesh: Reserved space request exceeds the maximum buffer size.' );
}
const visibility = this._visibility;
const active = this._active;
const matricesTexture = this._matricesTexture;
const matricesArray = this._matricesTexture.image.data;
// push new visibility states
visibility.push( true );
active.push( true );
// update id
const geometryId = this._geometryCount;
this._geometryCount ++;
// initialize matrix information
_identityMatrix.toArray( matricesArray, geometryId * 16 );
matricesTexture.needsUpdate = true;
// add the reserved range and draw range objects
reservedRanges.push( reservedRange );
drawRanges.push( {
start: hasIndex ? reservedRange.indexStart : reservedRange.vertexStart,
count: - 1
} );
bounds.push( {
boxInitialized: false,
box: new Box3(),
sphereInitialized: false,
sphere: new Sphere()
} );
// set the id for the geometry
const idAttribute = this.geometry.getAttribute( ID_ATTR_NAME );
for ( let i = 0; i < reservedRange.vertexCount; i ++ ) {
idAttribute.setX( reservedRange.vertexStart + i, geometryId );
}
idAttribute.needsUpdate = true;
// update the geometry
this.setGeometryAt( geometryId, geometry );
return geometryId;
}
setGeometryAt( id, geometry ) {
if ( id >= this._geometryCount ) {
throw new Error( 'BatchedMesh: Maximum geometry count reached.' );
}
this._validateGeometry( geometry );
const batchGeometry = this.geometry;
const hasIndex = batchGeometry.getIndex() !== null;
const dstIndex = batchGeometry.getIndex();
const srcIndex = geometry.getIndex();
const reservedRange = this._reservedRanges[ id ];
if (
hasIndex &&
srcIndex.count > reservedRange.indexCount ||
geometry.attributes.position.count > reservedRange.vertexCount
) {
throw new Error( 'BatchedMesh: Reserved space not large enough for provided geometry.' );
}
// copy geometry over
const vertexStart = reservedRange.vertexStart;
const vertexCount = reservedRange.vertexCount;
for ( const attributeName in batchGeometry.attributes ) {
if ( attributeName === ID_ATTR_NAME ) {
continue;
}
// copy attribute data
const srcAttribute = geometry.getAttribute( attributeName );
const dstAttribute = batchGeometry.getAttribute( attributeName );
copyAttributeData( srcAttribute, dstAttribute, vertexStart );
// fill the rest in with zeroes
const itemSize = srcAttribute.itemSize;
for ( let i = srcAttribute.count, l = vertexCount; i < l; i ++ ) {
const index = vertexStart + i;
for ( let c = 0; c < itemSize; c ++ ) {
dstAttribute.setComponent( index, c, 0 );
}
}
dstAttribute.needsUpdate = true;
}
// copy index
if ( hasIndex ) {
const indexStart = reservedRange.indexStart;
// copy index data over
for ( let i = 0; i < srcIndex.count; i ++ ) {
dstIndex.setX( indexStart + i, vertexStart + srcIndex.getX( i ) );
}
// fill the rest in with zeroes
for ( let i = srcIndex.count, l = reservedRange.indexCount; i < l; i ++ ) {
dstIndex.setX( indexStart + i, vertexStart );
}
dstIndex.needsUpdate = true;
}
// store the bounding boxes
const bound = this._bounds[ id ];
if ( geometry.boundingBox !== null ) {
bound.box.copy( geometry.boundingBox );
bound.boxInitialized = true;
} else {
bound.boxInitialized = false;
}
if ( geometry.boundingSphere !== null ) {
bound.sphere.copy( geometry.boundingSphere );
bound.sphereInitialized = true;
} else {
bound.sphereInitialized = false;
}
// set drawRange count
const drawRange = this._drawRanges[ id ];
const posAttr = geometry.getAttribute( 'position' );
drawRange.count = hasIndex ? srcIndex.count : posAttr.count;
this._visibilityChanged = true;
return id;
}
deleteGeometry( geometryId ) {
// Note: User needs to call optimize() afterward to pack the data.
const active = this._active;
if ( geometryId >= active.length || active[ geometryId ] === false ) {
return this;
}
active[ geometryId ] = false;
this._visibilityChanged = true;
return this;
}
// get bounding box and compute it if it doesn't exist
getBoundingBoxAt( id, target ) {
const active = this._active;
if ( active[ id ] === false ) {
return this;
}
// compute bounding box
const bound = this._bounds[ id ];
const box = bound.box;
const geometry = this.geometry;
if ( bound.boxInitialized === false ) {
box.makeEmpty();
const index = geometry.index;
const position = geometry.attributes.position;
const drawRange = this._drawRanges[ id ];
for ( let i = drawRange.start, l = drawRange.start + drawRange.count; i < l; i ++ ) {
let iv = i;
if ( index ) {
iv = index.getX( iv );
}
box.expandByPoint( _vector.fromBufferAttribute( position, iv ) );
}
bound.boxInitialized = true;
}
target.copy( box );
return target;
}
// get bounding sphere and compute it if it doesn't exist
getBoundingSphereAt( id, target ) {
const active = this._active;
if ( active[ id ] === false ) {
return this;
}
// compute bounding sphere
const bound = this._bounds[ id ];
const sphere = bound.sphere;
const geometry = this.geometry;
if ( bound.sphereInitialized === false ) {
sphere.makeEmpty();
this.getBoundingBoxAt( id, _box );
_box.getCenter( sphere.center );
const index = geometry.index;
const position = geometry.attributes.position;
const drawRange = this._drawRanges[ id ];
let maxRadiusSq = 0;
for ( let i = drawRange.start, l = drawRange.start + drawRange.count; i < l; i ++ ) {
let iv = i;
if ( index ) {
iv = index.getX( iv );
}
_vector.fromBufferAttribute( position, iv );
maxRadiusSq = Math.max( maxRadiusSq, sphere.center.distanceToSquared( _vector ) );
}
sphere.radius = Math.sqrt( maxRadiusSq );
bound.sphereInitialized = true;
}
target.copy( sphere );
return target;
}
setMatrixAt( geometryId, matrix ) {
// @TODO: Map geometryId to index of the arrays because
// optimize() can make geometryId mismatch the index
const active = this._active;
const matricesTexture = this._matricesTexture;
const matricesArray = this._matricesTexture.image.data;
const geometryCount = this._geometryCount;
if ( geometryId >= geometryCount || active[ geometryId ] === false ) {
return this;
}
matrix.toArray( matricesArray, geometryId * 16 );
matricesTexture.needsUpdate = true;
return this;
}
getMatrixAt( geometryId, matrix ) {
const active = this._active;
const matricesArray = this._matricesTexture.image.data;
const geometryCount = this._geometryCount;
if ( geometryId >= geometryCount || active[ geometryId ] === false ) {
return null;
}
return matrix.fromArray( matricesArray, geometryId * 16 );
}
setVisibleAt( geometryId, value ) {
const visibility = this._visibility;
const active = this._active;
const geometryCount = this._geometryCount;
// if the geometry is out of range, not active, or visibility state
// does not change then return early
if (
geometryId >= geometryCount ||
active[ geometryId ] === false ||
visibility[ geometryId ] === value
) {
return this;
}
visibility[ geometryId ] = value;
this._visibilityChanged = true;
return this;
}
getVisibleAt( geometryId ) {
const visibility = this._visibility;
const active = this._active;
const geometryCount = this._geometryCount;
// return early if the geometry is out of range or not active
if ( geometryId >= geometryCount || active[ geometryId ] === false ) {
return false;
}
return visibility[ geometryId ];
}
raycast( raycaster, intersects ) {
const visibility = this._visibility;
const active = this._active;
const drawRanges = this._drawRanges;
const geometryCount = this._geometryCount;
const matrixWorld = this.matrixWorld;
const batchGeometry = this.geometry;
// iterate over each geometry
_mesh.material = this.material;
_mesh.geometry.index = batchGeometry.index;
_mesh.geometry.attributes = batchGeometry.attributes;
if ( _mesh.geometry.boundingBox === null ) {
_mesh.geometry.boundingBox = new Box3();
}
if ( _mesh.geometry.boundingSphere === null ) {
_mesh.geometry.boundingSphere = new Sphere();
}
for ( let i = 0; i < geometryCount; i ++ ) {
if ( ! visibility[ i ] || ! active[ i ] ) {
continue;
}
const drawRange = drawRanges[ i ];
_mesh.geometry.setDrawRange( drawRange.start, drawRange.count );
// ge the intersects
this.getMatrixAt( i, _mesh.matrixWorld ).premultiply( matrixWorld );
this.getBoundingBoxAt( i, _mesh.geometry.boundingBox );
this.getBoundingSphereAt( i, _mesh.geometry.boundingSphere );
_mesh.raycast( raycaster, _batchIntersects );
// add batch id to the intersects
for ( let j = 0, l = _batchIntersects.length; j < l; j ++ ) {
const intersect = _batchIntersects[ j ];
intersect.object = this;
intersect.batchId = i;
intersects.push( intersect );
}
_batchIntersects.length = 0;
}
_mesh.material = null;
_mesh.geometry.index = null;
_mesh.geometry.attributes = {};
_mesh.geometry.setDrawRange( 0, Infinity );
}
copy( source ) {
super.copy( source );
this.geometry = source.geometry.clone();
this.perObjectFrustumCulled = source.perObjectFrustumCulled;
this.sortObjects = source.sortObjects;
this.boundingBox = source.boundingBox !== null ? source.boundingBox.clone() : null;
this.boundingSphere = source.boundingSphere !== null ? source.boundingSphere.clone() : null;
this._drawRanges = source._drawRanges.map( range => ( { ...range } ) );
this._reservedRanges = source._reservedRanges.map( range => ( { ...range } ) );
this._visibility = source._visibility.slice();
this._active = source._active.slice();
this._bounds = source._bounds.map( bound => ( {
boxInitialized: bound.boxInitialized,
box: bound.box.clone(),
sphereInitialized: bound.sphereInitialized,
sphere: bound.sphere.clone()
} ) );
this._maxGeometryCount = source._maxGeometryCount;
this._maxVertexCount = source._maxVertexCount;
this._maxIndexCount = source._maxIndexCount;
this._geometryInitialized = source._geometryInitialized;
this._geometryCount = source._geometryCount;
this._multiDrawCounts = source._multiDrawCounts.slice();
this._multiDrawStarts = source._multiDrawStarts.slice();
this._matricesTexture = source._matricesTexture.clone();
this._matricesTexture.image.data = this._matricesTexture.image.slice();
return this;
}
dispose() {
// Assuming the geometry is not shared with other meshes
this.geometry.dispose();
this._matricesTexture.dispose();
this._matricesTexture = null;
return this;
}
onBeforeRender( renderer, scene, camera, geometry, material/*, _group*/ ) {
// if visibility has not changed and frustum culling and object sorting is not required
// then skip iterating over all items
if ( ! this._visibilityChanged && ! this.perObjectFrustumCulled && ! this.sortObjects ) {
return;
}
// the indexed version of the multi draw function requires specifying the start
// offset in bytes.
const index = geometry.getIndex();
const bytesPerElement = index === null ? 1 : index.array.BYTES_PER_ELEMENT;
const visibility = this._visibility;
const multiDrawStarts = this._multiDrawStarts;
const multiDrawCounts = this._multiDrawCounts;
const drawRanges = this._drawRanges;
const perObjectFrustumCulled = this.perObjectFrustumCulled;
// prepare the frustum in the local frame
if ( perObjectFrustumCulled ) {
_projScreenMatrix
.multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse )
.multiply( this.matrixWorld );
_frustum.setFromProjectionMatrix(
_projScreenMatrix,
renderer.isWebGPURenderer ? WebGPUCoordinateSystem : WebGLCoordinateSystem
);
}
let count = 0;
if ( this.sortObjects ) {
// get the camera position in the local frame
_invMatrixWorld.copy( this.matrixWorld ).invert();
_vector.setFromMatrixPosition( camera.matrixWorld ).applyMatrix4( _invMatrixWorld );
for ( let i = 0, l = visibility.length; i < l; i ++ ) {
if ( visibility[ i ] ) {
// get the bounds in world space
this.getMatrixAt( i, _matrix );
this.getBoundingSphereAt( i, _sphere ).applyMatrix4( _matrix );
// determine whether the batched geometry is within the frustum
let culled = false;
if ( perObjectFrustumCulled ) {
culled = ! _frustum.intersectsSphere( _sphere );
}
if ( ! culled ) {
// get the distance from camera used for sorting
const z = _vector.distanceTo( _sphere.center );
_renderList.push( drawRanges[ i ], z );
}
}
}
// Sort the draw ranges and prep for rendering
const list = _renderList.list;
const customSort = this.customSort;
if ( customSort === null ) {
list.sort( material.transparent ? sortTransparent : sortOpaque );
} else {
customSort.call( this, list, camera );
}
for ( let i = 0, l = list.length; i < l; i ++ ) {
const item = list[ i ];
multiDrawStarts[ count ] = item.start * bytesPerElement;
multiDrawCounts[ count ] = item.count;
count ++;
}
_renderList.reset();
} else {
for ( let i = 0, l = visibility.length; i < l; i ++ ) {
if ( visibility[ i ] ) {
// determine whether the batched geometry is within the frustum
let culled = false;
if ( perObjectFrustumCulled ) {
// get the bounds in world space
this.getMatrixAt( i, _matrix );
this.getBoundingSphereAt( i, _sphere ).applyMatrix4( _matrix );
culled = ! _frustum.intersectsSphere( _sphere );
}
if ( ! culled ) {
const range = drawRanges[ i ];
multiDrawStarts[ count ] = range.start * bytesPerElement;
multiDrawCounts[ count ] = range.count;
count ++;
}
}
}
}
this._multiDrawCount = count;
this._visibilityChanged = false;
}
onBeforeShadow( renderer, object, camera, shadowCamera, geometry, depthMaterial/* , group */ ) {
this.onBeforeRender( renderer, null, shadowCamera, geometry, depthMaterial );
}
}
export { BatchedMesh };
|