Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Complexed entity for sheep #205

Open
wants to merge 3 commits into
base: next
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
178 changes: 121 additions & 57 deletions prismarine-viewer/viewer/lib/entity/EntityMesh.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import entities from './entities.json'
import { externalModels } from './objModels'
import externalTexturesJson from './externalTextures.json'
import { sheep, sheepCoat } from './exportedModels.js'
// import { loadTexture } from globalThis.isElectron ? '../utils.electron.js' : '../utils';
const { loadTexture } = globalThis.isElectron ? require('../utils.electron.js') : require('../utils')

Expand Down Expand Up @@ -94,7 +95,7 @@
return a[0] * b[0] + a[1] * b[1] + a[2] * b[2]
}

function addCube(attr, boneId, bone, cube, texWidth = 64, texHeight = 64) {

Check warning on line 98 in prismarine-viewer/viewer/lib/entity/EntityMesh.js

View workflow job for this annotation

GitHub Actions / build-and-deploy

Function 'addCube' has too many parameters (6). Maximum allowed is 4
const cubeRotation = new THREE.Euler(0, 0, 0)
if (cube.rotation) {
cubeRotation.x = -cube.rotation[0] * Math.PI / 180
Expand Down Expand Up @@ -294,15 +295,15 @@

const scaleEntity = {
zombie: 1.85,
husk: 1.85
husk: 1.85,
sheep: 2
}
const offsetEntity = {
zombie: new Vec3(0, 1, 0),
husk: new Vec3(0, 1, 0),
boat: new Vec3(0, -1, 0),
}

// eslint-disable-next-line @typescript-eslint/no-extraneous-class
export class EntityMesh {
constructor(version, type, scene, /** @type {{textures?, rotation?: Record<string, {x,y,z}>}} */overrides = {}) {
const originalType = type
Expand All @@ -312,50 +313,19 @@
if (externalModels[type]) {
const objLoader = new OBJLoader()
let texturePath = externalTexturesJson[type]
if (originalType === 'zombie_horse') {
texturePath = `textures/${version}/entity/horse/horse_zombie.png`
}
if (originalType === 'husk') {
texturePath = huskPng
}
if (originalType === 'skeleton_horse') {
texturePath = `textures/${version}/entity/horse/horse_skeleton.png`
}
if (originalType === 'donkey') {
texturePath = `textures/${version}/entity/horse/donkey.png`
}
if (originalType === 'mule') {
texturePath = `textures/${version}/entity/horse/mule.png`
}
if (originalType === 'ocelot') {
texturePath = `textures/${version}/entity/cat/ocelot.png`

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

texturePath = this.getTexturePath(originalType, version, texturePath)
if (!texturePath) throw new Error(`No texture for ${type}`)
const texture = new THREE.TextureLoader().load(texturePath)
texture.minFilter = THREE.NearestFilter
texture.magFilter = THREE.NearestFilter
const material = new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
alphaTest: 0.1
})

const material = this.loadMaterial(texturePath)
const obj = objLoader.parse(externalModels[type])
const scale = scaleEntity[originalType]
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, material, overrides)

this.mesh = obj
return
}
Expand All @@ -366,21 +336,115 @@
throw new Error(`Unknown entity ${type}`)
}

this.mesh = new THREE.Object3D()
for (const [name, jsonModel] of Object.entries(e.geometry)) {
const texture = overrides.textures?.[name] ?? e.textures[name]
this.mesh = this.loadDefaultEntity(e, overrides)
}

handleSheep(objLoader, originalType, overrides) {
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
}

getTexturePath(type, version, texturePath) {
switch (type) {
case 'zombie_horse':
return `textures/${version}/entity/horse/horse_zombie.png`
case 'husk':
return huskPng
case 'skeleton_horse':
return `textures/${version}/entity/horse/horse_skeleton.png`
case 'donkey':
return `textures/${version}/entity/horse/donkey.png`
case 'mule':
return `textures/${version}/entity/horse/mule.png`
case 'ocelot':
return `textures/${version}/entity/cat/ocelot.png`
default:
return texturePath
}
}

loadMaterial(texturePath) {
const texture = new THREE.TextureLoader().load(texturePath)
texture.minFilter = THREE.NearestFilter
texture.magFilter = THREE.NearestFilter
return new THREE.MeshBasicMaterial({
map: texture,
transparent: true,
alphaTest: 0.1
})
}

applyMaterialAndTransform(obj, originalType, material, overrides) {
const scale = scaleEntity[originalType]
const offset = offsetEntity[originalType]
obj.traverse((child) => {
if (child instanceof THREE.Mesh) {
child.material = material
if (child.name === 'Head' && overrides.rotation?.head) {
this.applyHeadRotation(child, overrides.rotation.head)
}
}
})

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

applyMaterialToMesh(obj, material) {
obj.traverse((child) => {
if (child instanceof THREE.Mesh) {
child.material = material
}
})
}

applyEntityScaleAndPosition(sheepObj, sheepCoatObj, originalType) {
const scale = scaleEntity[originalType]
const offset = offsetEntity[originalType]

if (scale) {
sheepObj.scale.set(scale, scale, scale)
sheepCoatObj.scale.set(scale, scale, scale)
}
if (offset) {
sheepObj.position.set(offset.x, offset.y, offset.z)
sheepCoatObj.position.set(offset.x, offset.y, offset.z)
}
}

applyHeadRotation(mesh, rotation) {
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
}

loadDefaultEntity(entity, overrides) {
const mesh = new THREE.Object3D()
for (const [name, jsonModel] of Object.entries(entity.geometry)) {
const texture = overrides.textures?.[name] ?? entity.textures[name]
if (!texture) continue
// console.log(JSON.stringify(jsonModel, null, 2))
const mesh = getMesh(texture + '.png', jsonModel, overrides)
mesh.name = `geometry_${name}`
this.mesh.add(mesh)

const skeletonHelper = new THREE.SkeletonHelper(mesh)
//@ts-expect-error
skeletonHelper.material.linewidth = 2
skeletonHelper.visible = false
this.mesh.add(skeletonHelper)
const entityMesh = getMesh(texture + '.png', jsonModel, overrides)
entityMesh.name = `geometry_${name}`
mesh.add(entityMesh)
}
return mesh
}

static getStaticData(name) {
Expand All @@ -393,7 +457,7 @@
const e = getEntity(name)
if (!e) throw new Error(`Unknown entity ${name}`)
return {
boneNames: Object.values(e.geometry).flatMap(x => x.name)
boneNames: Object.values(e.geometry).flatMap((x) => x.name)
}
}
}
3 changes: 2 additions & 1 deletion prismarine-viewer/viewer/lib/entity/exportedModels.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export { default as parrot } from './models/parrot.obj'
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 sheep } from './models/sheep.obj'
export { default as sheepCoat } from './models/sheepCoat.obj'
export { default as shulker } from './models/shulker.obj'
export { default as sniffer } from './models/sniffer.obj'
export { default as spider } from './models/spider.obj'
Expand Down
Loading
Loading