Skip to content

Commit 1599917

Browse files
committed
feat: falling block & summoned tnt entities rendering support
1 parent e706f7d commit 1599917

File tree

3 files changed

+53
-9
lines changed

3 files changed

+53
-9
lines changed

renderer/viewer/lib/worldDataEmitter.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,9 +90,17 @@ export class WorldDataEmitter extends (EventEmitter as new () => TypedEmitter<Wo
9090
}
9191

9292
listenToBot (bot: typeof __type_bot) {
93+
const entitiesObjectData = new Map<string, number>()
94+
bot._client.prependListener('spawn_entity', (data) => {
95+
if (data.objectData && data.entityId !== undefined) {
96+
entitiesObjectData.set(data.entityId, data.objectData)
97+
}
98+
})
99+
93100
const emitEntity = (e, name = 'entity') => {
94101
if (!e || e === bot.entity) return
95102
if (!e.name) return // mineflayer received update for not spawned entity
103+
e.objectData = entitiesObjectData.get(e.id)
96104
this.emitter.emit(name as any, {
97105
...e,
98106
pos: e.position,

renderer/viewer/three/appShared.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,12 @@ export const getItemUv = (item: Record<string, any>, specificProps: ItemSpecific
1919
const resources = resourcesManager.currentResources
2020
if (!resources) throw new Error('Resources not loaded')
2121
const idOrName = item.itemId ?? item.blockId ?? item.name
22+
const { blockState } = item
2223
try {
23-
const name = typeof idOrName === 'number' ? loadedData.items[idOrName]?.name : idOrName
24+
const name =
25+
blockState
26+
? loadedData.blocksByStateId[blockState]?.name
27+
: typeof idOrName === 'number' ? loadedData.items[idOrName]?.name : idOrName
2428
if (!name) throw new Error(`Item not found: ${idOrName}`)
2529

2630
const model = getItemModelName({

renderer/viewer/three/entities.ts

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -620,24 +620,56 @@ export class Entities {
620620
let mesh
621621
if (e === undefined) {
622622
const group = new THREE.Group()
623-
if (entity.name === 'item') {
624-
const item = entity.metadata?.find((m: any) => typeof m === 'object' && m?.itemCount)
623+
if (entity.name === 'item' || entity.name === 'tnt' || entity.name === 'falling_block') {
624+
const item = entity.name === 'tnt'
625+
? { name: 'tnt' }
626+
: entity.name === 'falling_block'
627+
? { blockState: entity['objectData'] }
628+
: entity.metadata?.find((m: any) => typeof m === 'object' && m?.itemCount)
625629
if (item) {
626630
const object = this.getItemMesh(item, {
627631
'minecraft:display_context': 'ground',
628632
})
629633
if (object) {
630634
mesh = object.mesh
631-
mesh.scale.set(0.5, 0.5, 0.5)
632-
mesh.position.set(0, 0.2, 0)
635+
if (entity.name === 'item') {
636+
mesh.scale.set(0.5, 0.5, 0.5)
637+
mesh.position.set(0, 0.2, 0)
638+
} else {
639+
mesh.scale.set(2, 2, 2)
640+
mesh.position.set(0, 0.5, 0)
641+
}
633642
// set faces
634643
// mesh.position.set(targetPos.x + 0.5 + 2, targetPos.y + 0.5, targetPos.z + 0.5)
635644
// viewer.scene.add(mesh)
636645
const clock = new THREE.Clock()
637-
mesh.onBeforeRender = () => {
638-
const delta = clock.getDelta()
639-
mesh.rotation.y += delta
646+
if (entity.name === 'item') {
647+
mesh.onBeforeRender = () => {
648+
const delta = clock.getDelta()
649+
mesh.rotation.y += delta
650+
}
640651
}
652+
653+
// TNT blinking
654+
// if (entity.name === 'tnt') {
655+
// let lastBlink = 0
656+
// const blinkInterval = 500 // ms between blinks
657+
// mesh.onBeforeRender = () => {
658+
// const now = Date.now()
659+
// if (now - lastBlink > blinkInterval) {
660+
// lastBlink = now
661+
// mesh.traverse((child) => {
662+
// if (child instanceof THREE.Mesh) {
663+
// const material = child.material as THREE.MeshLambertMaterial
664+
// material.color.set(material.color?.equals(new THREE.Color(0xff_ff_ff))
665+
// ? new THREE.Color(0xff_00_00)
666+
// : new THREE.Color(0xff_ff_ff))
667+
// }
668+
// })
669+
// }
670+
// }
671+
// }
672+
641673
//@ts-expect-error
642674
group.additionalCleanup = () => {
643675
// important: avoid texture memory leak and gpu slowdown
@@ -1076,7 +1108,7 @@ export class Entities {
10761108
const entityMesh = this.entities[entityId]?.children.find(c => c.name === 'mesh')
10771109
if (entityMesh) {
10781110
entityMesh.traverse((child) => {
1079-
if (child instanceof THREE.Mesh) {
1111+
if (child instanceof THREE.Mesh && child.material.clone) {
10801112
const clonedMaterial = child.material.clone()
10811113
clonedMaterial.dispose()
10821114
child.material = child.material.clone()

0 commit comments

Comments
 (0)