BatchedMesh.js 26 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133
  1. import { BufferAttribute } from '../core/BufferAttribute.js';
  2. import { BufferGeometry } from '../core/BufferGeometry.js';
  3. import { DataTexture } from '../textures/DataTexture.js';
  4. import { FloatType } from '../constants.js';
  5. import { Matrix4 } from '../math/Matrix4.js';
  6. import { Mesh } from './Mesh.js';
  7. import { RGBAFormat } from '../constants.js';
  8. import { ColorManagement } from '../math/ColorManagement.js';
  9. import { Box3 } from '../math/Box3.js';
  10. import { Sphere } from '../math/Sphere.js';
  11. import { Frustum } from '../math/Frustum.js';
  12. import { Vector3 } from '../math/Vector3.js';
  13. import { Color } from '../math/Color.js';
  14. function sortOpaque( a, b ) {
  15. return a.z - b.z;
  16. }
  17. function sortTransparent( a, b ) {
  18. return b.z - a.z;
  19. }
  20. class MultiDrawRenderList {
  21. constructor() {
  22. this.index = 0;
  23. this.pool = [];
  24. this.list = [];
  25. }
  26. push( drawRange, z ) {
  27. const pool = this.pool;
  28. const list = this.list;
  29. if ( this.index >= pool.length ) {
  30. pool.push( {
  31. start: - 1,
  32. count: - 1,
  33. z: - 1,
  34. } );
  35. }
  36. const item = pool[ this.index ];
  37. list.push( item );
  38. this.index ++;
  39. item.start = drawRange.start;
  40. item.count = drawRange.count;
  41. item.z = z;
  42. }
  43. reset() {
  44. this.list.length = 0;
  45. this.index = 0;
  46. }
  47. }
  48. const ID_ATTR_NAME = 'batchId';
  49. const _matrix = /*@__PURE__*/ new Matrix4();
  50. const _invMatrixWorld = /*@__PURE__*/ new Matrix4();
  51. const _identityMatrix = /*@__PURE__*/ new Matrix4();
  52. const _whiteColor = /*@__PURE__*/ new Color( 1, 1, 1 );
  53. const _projScreenMatrix = /*@__PURE__*/ new Matrix4();
  54. const _frustum = /*@__PURE__*/ new Frustum();
  55. const _box = /*@__PURE__*/ new Box3();
  56. const _sphere = /*@__PURE__*/ new Sphere();
  57. const _vector = /*@__PURE__*/ new Vector3();
  58. const _forward = /*@__PURE__*/ new Vector3();
  59. const _temp = /*@__PURE__*/ new Vector3();
  60. const _renderList = /*@__PURE__*/ new MultiDrawRenderList();
  61. const _mesh = /*@__PURE__*/ new Mesh();
  62. const _batchIntersects = [];
  63. // @TODO: SkinnedMesh support?
  64. // @TODO: geometry.groups support?
  65. // @TODO: geometry.drawRange support?
  66. // @TODO: geometry.morphAttributes support?
  67. // @TODO: Support uniform parameter per geometry
  68. // @TODO: Add an "optimize" function to pack geometry and remove data gaps
  69. // copies data from attribute "src" into "target" starting at "targetOffset"
  70. function copyAttributeData( src, target, targetOffset = 0 ) {
  71. const itemSize = target.itemSize;
  72. if ( src.isInterleavedBufferAttribute || src.array.constructor !== target.array.constructor ) {
  73. // use the component getters and setters if the array data cannot
  74. // be copied directly
  75. const vertexCount = src.count;
  76. for ( let i = 0; i < vertexCount; i ++ ) {
  77. for ( let c = 0; c < itemSize; c ++ ) {
  78. target.setComponent( i + targetOffset, c, src.getComponent( i, c ) );
  79. }
  80. }
  81. } else {
  82. // faster copy approach using typed array set function
  83. target.array.set( src.array, targetOffset * itemSize );
  84. }
  85. target.needsUpdate = true;
  86. }
  87. class BatchedMesh extends Mesh {
  88. get maxGeometryCount() {
  89. return this._maxGeometryCount;
  90. }
  91. constructor( maxGeometryCount, maxVertexCount, maxIndexCount = maxVertexCount * 2, material ) {
  92. super( new BufferGeometry(), material );
  93. this.isBatchedMesh = true;
  94. this.perObjectFrustumCulled = true;
  95. this.sortObjects = true;
  96. this.boundingBox = null;
  97. this.boundingSphere = null;
  98. this.customSort = null;
  99. this._drawRanges = [];
  100. this._reservedRanges = [];
  101. this._visibility = [];
  102. this._active = [];
  103. this._bounds = [];
  104. this._maxGeometryCount = maxGeometryCount;
  105. this._maxVertexCount = maxVertexCount;
  106. this._maxIndexCount = maxIndexCount;
  107. this._geometryInitialized = false;
  108. this._geometryCount = 0;
  109. this._multiDrawCounts = new Int32Array( maxGeometryCount );
  110. this._multiDrawStarts = new Int32Array( maxGeometryCount );
  111. this._multiDrawCount = 0;
  112. this._multiDrawInstances = null;
  113. this._visibilityChanged = true;
  114. // Local matrix per geometry by using data texture
  115. this._matricesTexture = null;
  116. this._initMatricesTexture();
  117. // Local color per geometry by using data texture
  118. this._colorsTexture = null;
  119. }
  120. _initMatricesTexture() {
  121. // layout (1 matrix = 4 pixels)
  122. // RGBA RGBA RGBA RGBA (=> column1, column2, column3, column4)
  123. // with 8x8 pixel texture max 16 matrices * 4 pixels = (8 * 8)
  124. // 16x16 pixel texture max 64 matrices * 4 pixels = (16 * 16)
  125. // 32x32 pixel texture max 256 matrices * 4 pixels = (32 * 32)
  126. // 64x64 pixel texture max 1024 matrices * 4 pixels = (64 * 64)
  127. let size = Math.sqrt( this._maxGeometryCount * 4 ); // 4 pixels needed for 1 matrix
  128. size = Math.ceil( size / 4 ) * 4;
  129. size = Math.max( size, 4 );
  130. const matricesArray = new Float32Array( size * size * 4 ); // 4 floats per RGBA pixel
  131. const matricesTexture = new DataTexture( matricesArray, size, size, RGBAFormat, FloatType );
  132. this._matricesTexture = matricesTexture;
  133. }
  134. _initColorsTexture() {
  135. let size = Math.sqrt( this._maxGeometryCount );
  136. size = Math.ceil( size );
  137. // 4 floats per RGBA pixel initialized to white
  138. const colorsArray = new Float32Array( size * size * 4 ).fill( 1 );
  139. const colorsTexture = new DataTexture( colorsArray, size, size, RGBAFormat, FloatType );
  140. colorsTexture.colorSpace = ColorManagement.workingColorSpace;
  141. this._colorsTexture = colorsTexture;
  142. }
  143. _initializeGeometry( reference ) {
  144. const geometry = this.geometry;
  145. const maxVertexCount = this._maxVertexCount;
  146. const maxGeometryCount = this._maxGeometryCount;
  147. const maxIndexCount = this._maxIndexCount;
  148. if ( this._geometryInitialized === false ) {
  149. for ( const attributeName in reference.attributes ) {
  150. const srcAttribute = reference.getAttribute( attributeName );
  151. const { array, itemSize, normalized } = srcAttribute;
  152. const dstArray = new array.constructor( maxVertexCount * itemSize );
  153. const dstAttribute = new BufferAttribute( dstArray, itemSize, normalized );
  154. geometry.setAttribute( attributeName, dstAttribute );
  155. }
  156. if ( reference.getIndex() !== null ) {
  157. const indexArray = maxVertexCount > 65536
  158. ? new Uint32Array( maxIndexCount )
  159. : new Uint16Array( maxIndexCount );
  160. geometry.setIndex( new BufferAttribute( indexArray, 1 ) );
  161. }
  162. const idArray = maxGeometryCount > 65536
  163. ? new Uint32Array( maxVertexCount )
  164. : new Uint16Array( maxVertexCount );
  165. geometry.setAttribute( ID_ATTR_NAME, new BufferAttribute( idArray, 1 ) );
  166. this._geometryInitialized = true;
  167. }
  168. }
  169. // Make sure the geometry is compatible with the existing combined geometry attributes
  170. _validateGeometry( geometry ) {
  171. // check that the geometry doesn't have a version of our reserved id attribute
  172. if ( geometry.getAttribute( ID_ATTR_NAME ) ) {
  173. throw new Error( `BatchedMesh: Geometry cannot use attribute "${ ID_ATTR_NAME }"` );
  174. }
  175. // check to ensure the geometries are using consistent attributes and indices
  176. const batchGeometry = this.geometry;
  177. if ( Boolean( geometry.getIndex() ) !== Boolean( batchGeometry.getIndex() ) ) {
  178. throw new Error( 'BatchedMesh: All geometries must consistently have "index".' );
  179. }
  180. for ( const attributeName in batchGeometry.attributes ) {
  181. if ( attributeName === ID_ATTR_NAME ) {
  182. continue;
  183. }
  184. if ( ! geometry.hasAttribute( attributeName ) ) {
  185. throw new Error( `BatchedMesh: Added geometry missing "${ attributeName }". All geometries must have consistent attributes.` );
  186. }
  187. const srcAttribute = geometry.getAttribute( attributeName );
  188. const dstAttribute = batchGeometry.getAttribute( attributeName );
  189. if ( srcAttribute.itemSize !== dstAttribute.itemSize || srcAttribute.normalized !== dstAttribute.normalized ) {
  190. throw new Error( 'BatchedMesh: All attributes must have a consistent itemSize and normalized value.' );
  191. }
  192. }
  193. }
  194. setCustomSort( func ) {
  195. this.customSort = func;
  196. return this;
  197. }
  198. computeBoundingBox() {
  199. if ( this.boundingBox === null ) {
  200. this.boundingBox = new Box3();
  201. }
  202. const geometryCount = this._geometryCount;
  203. const boundingBox = this.boundingBox;
  204. const active = this._active;
  205. boundingBox.makeEmpty();
  206. for ( let i = 0; i < geometryCount; i ++ ) {
  207. if ( active[ i ] === false ) continue;
  208. this.getMatrixAt( i, _matrix );
  209. this.getBoundingBoxAt( i, _box ).applyMatrix4( _matrix );
  210. boundingBox.union( _box );
  211. }
  212. }
  213. computeBoundingSphere() {
  214. if ( this.boundingSphere === null ) {
  215. this.boundingSphere = new Sphere();
  216. }
  217. const geometryCount = this._geometryCount;
  218. const boundingSphere = this.boundingSphere;
  219. const active = this._active;
  220. boundingSphere.makeEmpty();
  221. for ( let i = 0; i < geometryCount; i ++ ) {
  222. if ( active[ i ] === false ) continue;
  223. this.getMatrixAt( i, _matrix );
  224. this.getBoundingSphereAt( i, _sphere ).applyMatrix4( _matrix );
  225. boundingSphere.union( _sphere );
  226. }
  227. }
  228. addGeometry( geometry, vertexCount = - 1, indexCount = - 1 ) {
  229. this._initializeGeometry( geometry );
  230. this._validateGeometry( geometry );
  231. // ensure we're not over geometry
  232. if ( this._geometryCount >= this._maxGeometryCount ) {
  233. throw new Error( 'BatchedMesh: Maximum geometry count reached.' );
  234. }
  235. // get the necessary range fo the geometry
  236. const reservedRange = {
  237. vertexStart: - 1,
  238. vertexCount: - 1,
  239. indexStart: - 1,
  240. indexCount: - 1,
  241. };
  242. let lastRange = null;
  243. const reservedRanges = this._reservedRanges;
  244. const drawRanges = this._drawRanges;
  245. const bounds = this._bounds;
  246. if ( this._geometryCount !== 0 ) {
  247. lastRange = reservedRanges[ reservedRanges.length - 1 ];
  248. }
  249. if ( vertexCount === - 1 ) {
  250. reservedRange.vertexCount = geometry.getAttribute( 'position' ).count;
  251. } else {
  252. reservedRange.vertexCount = vertexCount;
  253. }
  254. if ( lastRange === null ) {
  255. reservedRange.vertexStart = 0;
  256. } else {
  257. reservedRange.vertexStart = lastRange.vertexStart + lastRange.vertexCount;
  258. }
  259. const index = geometry.getIndex();
  260. const hasIndex = index !== null;
  261. if ( hasIndex ) {
  262. if ( indexCount === - 1 ) {
  263. reservedRange.indexCount = index.count;
  264. } else {
  265. reservedRange.indexCount = indexCount;
  266. }
  267. if ( lastRange === null ) {
  268. reservedRange.indexStart = 0;
  269. } else {
  270. reservedRange.indexStart = lastRange.indexStart + lastRange.indexCount;
  271. }
  272. }
  273. if (
  274. reservedRange.indexStart !== - 1 &&
  275. reservedRange.indexStart + reservedRange.indexCount > this._maxIndexCount ||
  276. reservedRange.vertexStart + reservedRange.vertexCount > this._maxVertexCount
  277. ) {
  278. throw new Error( 'BatchedMesh: Reserved space request exceeds the maximum buffer size.' );
  279. }
  280. const visibility = this._visibility;
  281. const active = this._active;
  282. const matricesTexture = this._matricesTexture;
  283. const matricesArray = this._matricesTexture.image.data;
  284. const colorsTexture = this._colorsTexture;
  285. // push new visibility states
  286. visibility.push( true );
  287. active.push( true );
  288. // update id
  289. const geometryId = this._geometryCount;
  290. this._geometryCount ++;
  291. // initialize matrix information
  292. _identityMatrix.toArray( matricesArray, geometryId * 16 );
  293. matricesTexture.needsUpdate = true;
  294. // initialize the color to white
  295. if ( colorsTexture !== null ) {
  296. _whiteColor.toArray( colorsTexture.image.data, geometryId * 4 );
  297. colorsTexture.needsUpdate = true;
  298. }
  299. // add the reserved range and draw range objects
  300. reservedRanges.push( reservedRange );
  301. drawRanges.push( {
  302. start: hasIndex ? reservedRange.indexStart : reservedRange.vertexStart,
  303. count: - 1
  304. } );
  305. bounds.push( {
  306. boxInitialized: false,
  307. box: new Box3(),
  308. sphereInitialized: false,
  309. sphere: new Sphere()
  310. } );
  311. // set the id for the geometry
  312. const idAttribute = this.geometry.getAttribute( ID_ATTR_NAME );
  313. for ( let i = 0; i < reservedRange.vertexCount; i ++ ) {
  314. idAttribute.setX( reservedRange.vertexStart + i, geometryId );
  315. }
  316. idAttribute.needsUpdate = true;
  317. // update the geometry
  318. this.setGeometryAt( geometryId, geometry );
  319. return geometryId;
  320. }
  321. setGeometryAt( id, geometry ) {
  322. if ( id >= this._geometryCount ) {
  323. throw new Error( 'BatchedMesh: Maximum geometry count reached.' );
  324. }
  325. this._validateGeometry( geometry );
  326. const batchGeometry = this.geometry;
  327. const hasIndex = batchGeometry.getIndex() !== null;
  328. const dstIndex = batchGeometry.getIndex();
  329. const srcIndex = geometry.getIndex();
  330. const reservedRange = this._reservedRanges[ id ];
  331. if (
  332. hasIndex &&
  333. srcIndex.count > reservedRange.indexCount ||
  334. geometry.attributes.position.count > reservedRange.vertexCount
  335. ) {
  336. throw new Error( 'BatchedMesh: Reserved space not large enough for provided geometry.' );
  337. }
  338. // copy geometry over
  339. const vertexStart = reservedRange.vertexStart;
  340. const vertexCount = reservedRange.vertexCount;
  341. for ( const attributeName in batchGeometry.attributes ) {
  342. if ( attributeName === ID_ATTR_NAME ) {
  343. continue;
  344. }
  345. // copy attribute data
  346. const srcAttribute = geometry.getAttribute( attributeName );
  347. const dstAttribute = batchGeometry.getAttribute( attributeName );
  348. copyAttributeData( srcAttribute, dstAttribute, vertexStart );
  349. // fill the rest in with zeroes
  350. const itemSize = srcAttribute.itemSize;
  351. for ( let i = srcAttribute.count, l = vertexCount; i < l; i ++ ) {
  352. const index = vertexStart + i;
  353. for ( let c = 0; c < itemSize; c ++ ) {
  354. dstAttribute.setComponent( index, c, 0 );
  355. }
  356. }
  357. dstAttribute.needsUpdate = true;
  358. dstAttribute.addUpdateRange( vertexStart * itemSize, vertexCount * itemSize );
  359. }
  360. // copy index
  361. if ( hasIndex ) {
  362. const indexStart = reservedRange.indexStart;
  363. // copy index data over
  364. for ( let i = 0; i < srcIndex.count; i ++ ) {
  365. dstIndex.setX( indexStart + i, vertexStart + srcIndex.getX( i ) );
  366. }
  367. // fill the rest in with zeroes
  368. for ( let i = srcIndex.count, l = reservedRange.indexCount; i < l; i ++ ) {
  369. dstIndex.setX( indexStart + i, vertexStart );
  370. }
  371. dstIndex.needsUpdate = true;
  372. dstIndex.addUpdateRange( indexStart, reservedRange.indexCount );
  373. }
  374. // store the bounding boxes
  375. const bound = this._bounds[ id ];
  376. if ( geometry.boundingBox !== null ) {
  377. bound.box.copy( geometry.boundingBox );
  378. bound.boxInitialized = true;
  379. } else {
  380. bound.boxInitialized = false;
  381. }
  382. if ( geometry.boundingSphere !== null ) {
  383. bound.sphere.copy( geometry.boundingSphere );
  384. bound.sphereInitialized = true;
  385. } else {
  386. bound.sphereInitialized = false;
  387. }
  388. // set drawRange count
  389. const drawRange = this._drawRanges[ id ];
  390. const posAttr = geometry.getAttribute( 'position' );
  391. drawRange.count = hasIndex ? srcIndex.count : posAttr.count;
  392. this._visibilityChanged = true;
  393. return id;
  394. }
  395. deleteGeometry( geometryId ) {
  396. // Note: User needs to call optimize() afterward to pack the data.
  397. const active = this._active;
  398. if ( geometryId >= active.length || active[ geometryId ] === false ) {
  399. return this;
  400. }
  401. active[ geometryId ] = false;
  402. this._visibilityChanged = true;
  403. return this;
  404. }
  405. getInstanceCountAt( id ) {
  406. if ( this._multiDrawInstances === null ) return null;
  407. return this._multiDrawInstances[ id ];
  408. }
  409. setInstanceCountAt( id, instanceCount ) {
  410. if ( this._multiDrawInstances === null ) {
  411. this._multiDrawInstances = new Int32Array( this._maxGeometryCount ).fill( 1 );
  412. }
  413. this._multiDrawInstances[ id ] = instanceCount;
  414. return id;
  415. }
  416. // get bounding box and compute it if it doesn't exist
  417. getBoundingBoxAt( id, target ) {
  418. const active = this._active;
  419. if ( active[ id ] === false ) {
  420. return null;
  421. }
  422. // compute bounding box
  423. const bound = this._bounds[ id ];
  424. const box = bound.box;
  425. const geometry = this.geometry;
  426. if ( bound.boxInitialized === false ) {
  427. box.makeEmpty();
  428. const index = geometry.index;
  429. const position = geometry.attributes.position;
  430. const drawRange = this._drawRanges[ id ];
  431. for ( let i = drawRange.start, l = drawRange.start + drawRange.count; i < l; i ++ ) {
  432. let iv = i;
  433. if ( index ) {
  434. iv = index.getX( iv );
  435. }
  436. box.expandByPoint( _vector.fromBufferAttribute( position, iv ) );
  437. }
  438. bound.boxInitialized = true;
  439. }
  440. target.copy( box );
  441. return target;
  442. }
  443. // get bounding sphere and compute it if it doesn't exist
  444. getBoundingSphereAt( id, target ) {
  445. const active = this._active;
  446. if ( active[ id ] === false ) {
  447. return null;
  448. }
  449. // compute bounding sphere
  450. const bound = this._bounds[ id ];
  451. const sphere = bound.sphere;
  452. const geometry = this.geometry;
  453. if ( bound.sphereInitialized === false ) {
  454. sphere.makeEmpty();
  455. this.getBoundingBoxAt( id, _box );
  456. _box.getCenter( sphere.center );
  457. const index = geometry.index;
  458. const position = geometry.attributes.position;
  459. const drawRange = this._drawRanges[ id ];
  460. let maxRadiusSq = 0;
  461. for ( let i = drawRange.start, l = drawRange.start + drawRange.count; i < l; i ++ ) {
  462. let iv = i;
  463. if ( index ) {
  464. iv = index.getX( iv );
  465. }
  466. _vector.fromBufferAttribute( position, iv );
  467. maxRadiusSq = Math.max( maxRadiusSq, sphere.center.distanceToSquared( _vector ) );
  468. }
  469. sphere.radius = Math.sqrt( maxRadiusSq );
  470. bound.sphereInitialized = true;
  471. }
  472. target.copy( sphere );
  473. return target;
  474. }
  475. setMatrixAt( geometryId, matrix ) {
  476. // @TODO: Map geometryId to index of the arrays because
  477. // optimize() can make geometryId mismatch the index
  478. const active = this._active;
  479. const matricesTexture = this._matricesTexture;
  480. const matricesArray = this._matricesTexture.image.data;
  481. const geometryCount = this._geometryCount;
  482. if ( geometryId >= geometryCount || active[ geometryId ] === false ) {
  483. return this;
  484. }
  485. matrix.toArray( matricesArray, geometryId * 16 );
  486. matricesTexture.needsUpdate = true;
  487. return this;
  488. }
  489. getMatrixAt( geometryId, matrix ) {
  490. const active = this._active;
  491. const matricesArray = this._matricesTexture.image.data;
  492. const geometryCount = this._geometryCount;
  493. if ( geometryId >= geometryCount || active[ geometryId ] === false ) {
  494. return null;
  495. }
  496. return matrix.fromArray( matricesArray, geometryId * 16 );
  497. }
  498. setColorAt( geometryId, color ) {
  499. if ( this._colorsTexture === null ) {
  500. this._initColorsTexture();
  501. }
  502. // @TODO: Map geometryId to index of the arrays because
  503. // optimize() can make geometryId mismatch the index
  504. const active = this._active;
  505. const colorsTexture = this._colorsTexture;
  506. const colorsArray = this._colorsTexture.image.data;
  507. const geometryCount = this._geometryCount;
  508. if ( geometryId >= geometryCount || active[ geometryId ] === false ) {
  509. return this;
  510. }
  511. color.toArray( colorsArray, geometryId * 4 );
  512. colorsTexture.needsUpdate = true;
  513. return this;
  514. }
  515. getColorAt( geometryId, color ) {
  516. const active = this._active;
  517. const colorsArray = this._colorsTexture.image.data;
  518. const geometryCount = this._geometryCount;
  519. if ( geometryId >= geometryCount || active[ geometryId ] === false ) {
  520. return null;
  521. }
  522. return color.fromArray( colorsArray, geometryId * 4 );
  523. }
  524. setVisibleAt( geometryId, value ) {
  525. const visibility = this._visibility;
  526. const active = this._active;
  527. const geometryCount = this._geometryCount;
  528. // if the geometry is out of range, not active, or visibility state
  529. // does not change then return early
  530. if (
  531. geometryId >= geometryCount ||
  532. active[ geometryId ] === false ||
  533. visibility[ geometryId ] === value
  534. ) {
  535. return this;
  536. }
  537. visibility[ geometryId ] = value;
  538. this._visibilityChanged = true;
  539. return this;
  540. }
  541. getVisibleAt( geometryId ) {
  542. const visibility = this._visibility;
  543. const active = this._active;
  544. const geometryCount = this._geometryCount;
  545. // return early if the geometry is out of range or not active
  546. if ( geometryId >= geometryCount || active[ geometryId ] === false ) {
  547. return false;
  548. }
  549. return visibility[ geometryId ];
  550. }
  551. raycast( raycaster, intersects ) {
  552. const visibility = this._visibility;
  553. const active = this._active;
  554. const drawRanges = this._drawRanges;
  555. const geometryCount = this._geometryCount;
  556. const matrixWorld = this.matrixWorld;
  557. const batchGeometry = this.geometry;
  558. // iterate over each geometry
  559. _mesh.material = this.material;
  560. _mesh.geometry.index = batchGeometry.index;
  561. _mesh.geometry.attributes = batchGeometry.attributes;
  562. if ( _mesh.geometry.boundingBox === null ) {
  563. _mesh.geometry.boundingBox = new Box3();
  564. }
  565. if ( _mesh.geometry.boundingSphere === null ) {
  566. _mesh.geometry.boundingSphere = new Sphere();
  567. }
  568. for ( let i = 0; i < geometryCount; i ++ ) {
  569. if ( ! visibility[ i ] || ! active[ i ] ) {
  570. continue;
  571. }
  572. const drawRange = drawRanges[ i ];
  573. _mesh.geometry.setDrawRange( drawRange.start, drawRange.count );
  574. // ge the intersects
  575. this.getMatrixAt( i, _mesh.matrixWorld ).premultiply( matrixWorld );
  576. this.getBoundingBoxAt( i, _mesh.geometry.boundingBox );
  577. this.getBoundingSphereAt( i, _mesh.geometry.boundingSphere );
  578. _mesh.raycast( raycaster, _batchIntersects );
  579. // add batch id to the intersects
  580. for ( let j = 0, l = _batchIntersects.length; j < l; j ++ ) {
  581. const intersect = _batchIntersects[ j ];
  582. intersect.object = this;
  583. intersect.batchId = i;
  584. intersects.push( intersect );
  585. }
  586. _batchIntersects.length = 0;
  587. }
  588. _mesh.material = null;
  589. _mesh.geometry.index = null;
  590. _mesh.geometry.attributes = {};
  591. _mesh.geometry.setDrawRange( 0, Infinity );
  592. }
  593. copy( source ) {
  594. super.copy( source );
  595. this.geometry = source.geometry.clone();
  596. this.perObjectFrustumCulled = source.perObjectFrustumCulled;
  597. this.sortObjects = source.sortObjects;
  598. this.boundingBox = source.boundingBox !== null ? source.boundingBox.clone() : null;
  599. this.boundingSphere = source.boundingSphere !== null ? source.boundingSphere.clone() : null;
  600. this._drawRanges = source._drawRanges.map( range => ( { ...range } ) );
  601. this._reservedRanges = source._reservedRanges.map( range => ( { ...range } ) );
  602. this._visibility = source._visibility.slice();
  603. this._active = source._active.slice();
  604. this._bounds = source._bounds.map( bound => ( {
  605. boxInitialized: bound.boxInitialized,
  606. box: bound.box.clone(),
  607. sphereInitialized: bound.sphereInitialized,
  608. sphere: bound.sphere.clone()
  609. } ) );
  610. this._maxGeometryCount = source._maxGeometryCount;
  611. this._maxVertexCount = source._maxVertexCount;
  612. this._maxIndexCount = source._maxIndexCount;
  613. this._geometryInitialized = source._geometryInitialized;
  614. this._geometryCount = source._geometryCount;
  615. this._multiDrawCounts = source._multiDrawCounts.slice();
  616. this._multiDrawStarts = source._multiDrawStarts.slice();
  617. this._matricesTexture = source._matricesTexture.clone();
  618. this._matricesTexture.image.data = this._matricesTexture.image.slice();
  619. if ( this._colorsTexture !== null ) {
  620. this._colorsTexture = source._colorsTexture.clone();
  621. this._colorsTexture.image.data = this._colorsTexture.image.slice();
  622. }
  623. return this;
  624. }
  625. dispose() {
  626. // Assuming the geometry is not shared with other meshes
  627. this.geometry.dispose();
  628. this._matricesTexture.dispose();
  629. this._matricesTexture = null;
  630. if ( this._colorsTexture !== null ) {
  631. this._colorsTexture.dispose();
  632. this._colorsTexture = null;
  633. }
  634. return this;
  635. }
  636. onBeforeRender( renderer, scene, camera, geometry, material/*, _group*/ ) {
  637. // if visibility has not changed and frustum culling and object sorting is not required
  638. // then skip iterating over all items
  639. if ( ! this._visibilityChanged && ! this.perObjectFrustumCulled && ! this.sortObjects ) {
  640. return;
  641. }
  642. // the indexed version of the multi draw function requires specifying the start
  643. // offset in bytes.
  644. const index = geometry.getIndex();
  645. const bytesPerElement = index === null ? 1 : index.array.BYTES_PER_ELEMENT;
  646. const active = this._active;
  647. const visibility = this._visibility;
  648. const multiDrawStarts = this._multiDrawStarts;
  649. const multiDrawCounts = this._multiDrawCounts;
  650. const drawRanges = this._drawRanges;
  651. const perObjectFrustumCulled = this.perObjectFrustumCulled;
  652. // prepare the frustum in the local frame
  653. if ( perObjectFrustumCulled ) {
  654. _projScreenMatrix
  655. .multiplyMatrices( camera.projectionMatrix, camera.matrixWorldInverse )
  656. .multiply( this.matrixWorld );
  657. _frustum.setFromProjectionMatrix(
  658. _projScreenMatrix,
  659. renderer.coordinateSystem
  660. );
  661. }
  662. let count = 0;
  663. if ( this.sortObjects ) {
  664. // get the camera position in the local frame
  665. _invMatrixWorld.copy( this.matrixWorld ).invert();
  666. _vector.setFromMatrixPosition( camera.matrixWorld ).applyMatrix4( _invMatrixWorld );
  667. _forward.set( 0, 0, - 1 ).transformDirection( camera.matrixWorld ).transformDirection( _invMatrixWorld );
  668. for ( let i = 0, l = visibility.length; i < l; i ++ ) {
  669. if ( visibility[ i ] && active[ i ] ) {
  670. // get the bounds in world space
  671. this.getMatrixAt( i, _matrix );
  672. this.getBoundingSphereAt( i, _sphere ).applyMatrix4( _matrix );
  673. // determine whether the batched geometry is within the frustum
  674. let culled = false;
  675. if ( perObjectFrustumCulled ) {
  676. culled = ! _frustum.intersectsSphere( _sphere );
  677. }
  678. if ( ! culled ) {
  679. // get the distance from camera used for sorting
  680. const z = _temp.subVectors( _sphere.center, _vector ).dot( _forward );
  681. _renderList.push( drawRanges[ i ], z );
  682. }
  683. }
  684. }
  685. // Sort the draw ranges and prep for rendering
  686. const list = _renderList.list;
  687. const customSort = this.customSort;
  688. if ( customSort === null ) {
  689. list.sort( material.transparent ? sortTransparent : sortOpaque );
  690. } else {
  691. customSort.call( this, list, camera );
  692. }
  693. for ( let i = 0, l = list.length; i < l; i ++ ) {
  694. const item = list[ i ];
  695. multiDrawStarts[ count ] = item.start * bytesPerElement;
  696. multiDrawCounts[ count ] = item.count;
  697. count ++;
  698. }
  699. _renderList.reset();
  700. } else {
  701. for ( let i = 0, l = visibility.length; i < l; i ++ ) {
  702. if ( visibility[ i ] && active[ i ] ) {
  703. // determine whether the batched geometry is within the frustum
  704. let culled = false;
  705. if ( perObjectFrustumCulled ) {
  706. // get the bounds in world space
  707. this.getMatrixAt( i, _matrix );
  708. this.getBoundingSphereAt( i, _sphere ).applyMatrix4( _matrix );
  709. culled = ! _frustum.intersectsSphere( _sphere );
  710. }
  711. if ( ! culled ) {
  712. const range = drawRanges[ i ];
  713. multiDrawStarts[ count ] = range.start * bytesPerElement;
  714. multiDrawCounts[ count ] = range.count;
  715. count ++;
  716. }
  717. }
  718. }
  719. }
  720. this._multiDrawCount = count;
  721. this._visibilityChanged = false;
  722. }
  723. onBeforeShadow( renderer, object, camera, shadowCamera, geometry, depthMaterial/* , group */ ) {
  724. this.onBeforeRender( renderer, null, shadowCamera, geometry, depthMaterial );
  725. }
  726. }
  727. export { BatchedMesh };
粤ICP备19079148号