-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(model): add spherical billboard support for bones
- Loading branch information
Showing
12 changed files
with
257 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import * as THREE from 'three'; | ||
|
||
class ModelBone { | ||
flags: number; | ||
parent: ModelBone | null; | ||
pivot: THREE.Vector3; | ||
|
||
translation = new THREE.Vector3(); | ||
rotation = new THREE.Quaternion(); | ||
scale = new THREE.Vector3(1.0, 1.0, 1.0); | ||
matrix = new THREE.Matrix4(); | ||
|
||
constructor(flags: number, parent: ModelBone | null, pivot: THREE.Vector3) { | ||
this.flags = flags; | ||
this.parent = parent; | ||
this.pivot = pivot; | ||
} | ||
} | ||
|
||
export default ModelBone; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import * as THREE from 'three'; | ||
import ModelBone from './ModelBone.js'; | ||
import { updateBone } from './skeleton.js'; | ||
|
||
class ModelSkeleton { | ||
#root: THREE.Object3D; | ||
#bones: ModelBone[]; | ||
#boneTexture: THREE.DataTexture; | ||
#boneMatrices: Float32Array; | ||
|
||
constructor(root: THREE.Object3D, bones: ModelBone[] = []) { | ||
this.#root = root; | ||
this.#bones = bones; | ||
|
||
this.#createBoneData(); | ||
} | ||
|
||
get bones() { | ||
return this.#bones; | ||
} | ||
|
||
get boneTexture() { | ||
return this.#boneTexture; | ||
} | ||
|
||
dispose() { | ||
if (this.#boneTexture) { | ||
this.#boneTexture.dispose(); | ||
} | ||
} | ||
|
||
update() { | ||
// noop | ||
} | ||
|
||
updateBones(camera: THREE.Camera) { | ||
// Ensure model view matrix is synchronized | ||
this.#root.modelViewMatrix.multiplyMatrices(camera.matrixWorldInverse, this.#root.matrixWorld); | ||
|
||
for (const [index, bone] of this.#bones.entries()) { | ||
updateBone(this.#root, bone); | ||
bone.matrix.toArray(this.#boneMatrices, index * 16); | ||
} | ||
|
||
this.#boneTexture.needsUpdate = true; | ||
} | ||
|
||
/** | ||
* From Skeleton#computeBoneTexture in Three.js. Creates bone matrices array and bone texture. | ||
* | ||
* @private | ||
*/ | ||
#createBoneData() { | ||
let size = Math.sqrt(this.#bones.length * 4); | ||
size = Math.ceil(size / 4) * 4; | ||
size = Math.max(size, 4); | ||
|
||
const boneMatrices = new Float32Array(size * size * 4); | ||
|
||
const boneTexture = new THREE.DataTexture( | ||
boneMatrices, | ||
size, | ||
size, | ||
THREE.RGBAFormat, | ||
THREE.FloatType, | ||
); | ||
boneTexture.needsUpdate = true; | ||
|
||
this.#boneMatrices = boneMatrices; | ||
this.#boneTexture = boneTexture; | ||
} | ||
} | ||
|
||
export default ModelSkeleton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.