@@ -17,7 +17,12 @@ import {
1717} from './model'
1818import { TEXTURE_FRAG_SHADER , TEXTURE_VERT_SHADER } from './textureShaders'
1919
20- type BlockModelMesh = { mesh : THREE . Mesh ; outline : THREE . LineSegments ; isBlock : true }
20+ type BlockModelMesh = {
21+ mesh : THREE . Mesh
22+ outline : THREE . LineSegments
23+ boundingBox : THREE . BufferGeometry
24+ isBlock : true
25+ }
2126
2227const LOADER = new THREE . TextureLoader ( )
2328const BLOCK_MODEL_CACHE = new Map < string , BlockModelMesh > ( )
@@ -59,6 +64,7 @@ export async function getBlockModel(block: string): Promise<BlockModelMesh | und
5964 result = {
6065 mesh : result . mesh . clone ( true ) ,
6166 outline : result . outline . clone ( true ) ,
67+ boundingBox : result . boundingBox . clone ( ) ,
6268 isBlock : true ,
6369 }
6470 for ( const child of result . mesh . children as THREE . Mesh [ ] ) {
@@ -112,6 +118,7 @@ async function generateModelMesh(
112118 }
113119
114120 const mesh : THREE . Mesh = new THREE . Mesh ( )
121+ const boundingBoxes : THREE . BufferGeometry [ ] = [ ]
115122 const outlineGeos : THREE . BufferGeometry [ ] = [ ]
116123
117124 for ( const element of model . elements ) {
@@ -334,6 +341,7 @@ async function generateModelMesh(
334341 geometry . setAttribute ( 'uv' , new THREE . Float32BufferAttribute ( uvs , 2 ) )
335342 geometry . attributes . uv . needsUpdate = true
336343
344+ boundingBoxes . push ( geometry . clone ( ) )
337345 const outlineGeo = new THREE . EdgesGeometry ( geometry )
338346 outlineGeos . push ( outlineGeo )
339347 const elementMesh = new THREE . Mesh ( geometry , materials )
@@ -342,12 +350,13 @@ async function generateModelMesh(
342350
343351 const outlineGeo = mergeGeometries ( outlineGeos ) !
344352 const outline = new THREE . LineSegments ( outlineGeo , Canvas . outlineMaterial )
353+ const boundingBox = mergeGeometries ( boundingBoxes ) !
345354
346355 outline . no_export = true
347356 outline . renderOrder = 2
348357 outline . frustumCulled = false
349358
350- return { mesh, outline, isBlock : true }
359+ return { mesh, outline, boundingBox , isBlock : true }
351360}
352361
353362const TEXTURE_CACHE = new Map < string , THREE . Texture > ( )
@@ -423,6 +432,7 @@ export async function parseBlockState(block: IParsedBlock): Promise<BlockModelMe
423432 } else if ( blockstate . multipart ) {
424433 // throw new Error(`Multipart block states are not supported yet`)
425434 const mesh = new THREE . Mesh ( )
435+ const boundingBoxes : THREE . BufferGeometry [ ] = [ ]
426436 const outlines : THREE . BufferGeometry [ ] = [ ]
427437 for ( const c of blockstate . multipart ) {
428438 const result = await parseMultipartCase ( block , c )
@@ -433,6 +443,10 @@ export async function parseBlockState(block: IParsedBlock): Promise<BlockModelMe
433443 newChild . rotateY ( result . mesh . rotation . y )
434444 newChild . rotateX ( result . mesh . rotation . x )
435445 mesh . add ( newChild )
446+ const boundingBox = result . boundingBox . clone ( )
447+ boundingBox . rotateY ( result . mesh . rotation . y )
448+ boundingBox . rotateX ( result . mesh . rotation . x )
449+ boundingBoxes . push ( boundingBox )
436450 }
437451 const outlineGeo = result . outline . geometry . clone ( )
438452 outlineGeo . rotateY ( result . mesh . rotation . y )
@@ -448,12 +462,13 @@ export async function parseBlockState(block: IParsedBlock): Promise<BlockModelMe
448462
449463 const outlineGeo = mergeGeometries ( outlines ) !
450464 const outline = new THREE . LineSegments ( outlineGeo , Canvas . outlineMaterial )
465+ const boundingBox = mergeGeometries ( boundingBoxes ) !
451466
452467 outline . no_export = true
453468 outline . renderOrder = 2
454469 outline . frustumCulled = false
455470
456- return { mesh, outline, isBlock : true }
471+ return { mesh, outline, boundingBox , isBlock : true }
457472 }
458473
459474 throw new Error ( `Unsupported block state '${ block . resourceLocation } '` )
0 commit comments