Skip to content
Open
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 81 additions & 17 deletions renderer/viewer/lib/entity/EntityMesh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import entities from './entities.json'
import { externalModels } from './objModels'
import externalTexturesJson from './externalTextures.json'
import { sheep, sheepCoat } from './exportedModels'

interface ElemFace {
dir: [number, number, number]
Expand Down Expand Up @@ -143,7 +144,7 @@
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}

function addCube (

Check warning on line 147 in renderer/viewer/lib/entity/EntityMesh.ts

View workflow job for this annotation

GitHub Actions / build-and-deploy

Function 'addCube' has too many parameters (9). Maximum allowed is 4
attr: GeoData,
boneId: number,
bone: THREE.Bone,
Expand Down Expand Up @@ -222,7 +223,7 @@
}
}

export function getMesh (

Check warning on line 226 in renderer/viewer/lib/entity/EntityMesh.ts

View workflow job for this annotation

GitHub Actions / build-and-deploy

Function 'getMesh' has too many parameters (5). Maximum allowed is 4
worldRenderer: WorldRendererCommon | undefined,
texture: string,
jsonModel: JsonModel,
Expand Down Expand Up @@ -406,7 +407,8 @@
const scaleEntity: Record<string, number> = {
zombie: 1.85,
husk: 1.85,
arrow: 0.0025
arrow: 0.0025,
sheep: 2
}

const offsetEntity: Record<string, Vec3> = {
Expand Down Expand Up @@ -434,6 +436,76 @@
export class EntityMesh {
mesh: THREE.Object3D

private applyMaterialToMesh (obj: THREE.Object3D, material: THREE.Material): void {
obj.traverse((child) => {
if (child instanceof THREE.Mesh) {
child.material = material
}
})
}

private applyEntityScaleAndPosition (obj1: THREE.Object3D, obj2: THREE.Object3D, originalType: string): void {
const scale = scaleEntity[originalType]
const offset = offsetEntity[originalType]

if (scale) {
obj1.scale.set(scale, scale, scale)
obj2.scale.set(scale, scale, scale)
}
if (offset) {
obj1.position.set(offset.x, offset.y, offset.z)
obj2.position.set(offset.x, offset.y, offset.z)
}
}

private applyHeadRotation (mesh: THREE.Object3D, rotation: { x?: number; y?: number; z?: number }): void {
mesh.rotation.x -= (rotation.x ?? 0) * Math.PI / 180
mesh.rotation.y -= (rotation.y ?? 0) * Math.PI / 180
mesh.rotation.z -= (rotation.z ?? 0) * Math.PI / 180
}

private handleSheep (objLoader: OBJLoader, originalType: string, overrides: EntityOverrides): THREE.Object3D {
const sheepObj = objLoader.parse(sheep)
const sheepMaterial = new THREE.MeshLambertMaterial({ color: 0xff_ff_ff })
this.applyMaterialToMesh(sheepObj, sheepMaterial)

const sheepCoatObj = objLoader.parse(sheepCoat)
const sheepCoatMaterial = new THREE.MeshLambertMaterial({ color: 0xff_d7_00 })
this.applyMaterialToMesh(sheepCoatObj, sheepCoatMaterial)

this.applyEntityScaleAndPosition(sheepObj, sheepCoatObj, originalType)

if (overrides.rotation?.head) {
this.applyHeadRotation(sheepObj, overrides.rotation.head)
}

const mesh = new THREE.Object3D()
mesh.add(sheepObj)
mesh.add(sheepCoatObj)

return mesh
}

private applyMaterialAndTransform (obj: THREE.Object3D, originalType: string, currentType: string, material: THREE.Material, overrides: EntityOverrides): void {
const scale = scaleEntity[originalType] || scaleEntity[currentType]
const offset = offsetEntity[originalType]
obj.traverse((child) => {
if (child instanceof THREE.Mesh) {
child.material = material
// todo
if (child.name === 'Head layer') child.visible = false
if (child.name === 'Head' && overrides.rotation?.head) { // todo
child.rotation.x -= (overrides.rotation.head.x ?? 0) * Math.PI / 180
child.rotation.y -= (overrides.rotation.head.y ?? 0) * Math.PI / 180
child.rotation.z -= (overrides.rotation.head.z ?? 0) * Math.PI / 180
}
}
})

if (scale) obj.scale.set(scale, scale, scale)
if (offset) obj.position.set(offset.x, offset.y, offset.z)
}

constructor (
version: string,
type: string,
Expand All @@ -450,6 +522,13 @@

if (externalModels[type]) {
const objLoader = new OBJLoader()

if (type === 'sheep') {
this.mesh = this.handleSheep(objLoader, originalType, overrides)
debugFlags.type = 'obj'
return
}

const texturePathMap = {
'zombie_horse': `textures/${version}/entity/horse/horse_zombie.png`,
'husk': huskPng,
Expand Down Expand Up @@ -479,22 +558,7 @@
alphaTest: 0.1
})
const obj = objLoader.parse(externalModels[type])
const scale = scaleEntity[originalType] || scaleEntity[type]
if (scale) obj.scale.set(scale, scale, scale)
const offset = offsetEntity[originalType]
if (offset) obj.position.set(offset.x, offset.y, offset.z)
obj.traverse((child) => {
if (child instanceof THREE.Mesh) {
child.material = material
// todo
if (child.name === 'Head layer') child.visible = false
if (child.name === 'Head' && overrides.rotation?.head) { // todo
child.rotation.x -= (overrides.rotation.head.x ?? 0) * Math.PI / 180
child.rotation.y -= (overrides.rotation.head.y ?? 0) * Math.PI / 180
child.rotation.z -= (overrides.rotation.head.z ?? 0) * Math.PI / 180
}
}
})
this.applyMaterialAndTransform(obj, originalType, type, material, overrides)
this.mesh = obj
debugFlags.type = 'obj'
return
Expand Down
1 change: 1 addition & 0 deletions renderer/viewer/lib/entity/exportedModels.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export { default as piglin } from './models/piglin.obj'
export { default as pillager } from './models/pillager.obj'
export { default as rabbit } from './models/rabbit.obj'
export { default as sheep } from './models/sheep.obj'
export { default as sheepCoat } from './models/sheepCoat.obj'
export { default as arrow } from './models/arrow.obj'
export { default as shulker } from './models/shulker.obj'
export { default as sniffer } from './models/sniffer.obj'
Expand Down
Loading
Loading