-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGLTransmissionFormat.js
49 lines (41 loc) · 1.68 KB
/
GLTransmissionFormat.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import { exportGltfFromTHREE } from "./utils"
// 临时解决方案,使用Three.js作为过渡,待理解glTF规范后重构
export class GLTransmissionFormat {
// glbBuffer = null
threeObject = null
fromTHREE(input) {
this.threeObject = input
// this.glbBuffer = await exportGltfFromTHREE(input, { binary: true })
return this
}
get batchLength() {
// if (!this.glbBuffer) return null
// const jsonChunkLength = new DataView(this.glbBuffer.slice(12, 16)).getUint32(0, true)
// const jsonChunkBuffer = this.glbBuffer.slice(20, 20 + jsonChunkLength)
// const glbHeader = JSON.parse(new TextDecoder("utf-8").decode(new Uint8Array(jsonChunkBuffer)))
// const batchIdIndexArray = glbHeader?.meshes?.flatMap(({ primitives }) => primitives.map(p => p.attributes._BATCHID)).filter(i => typeof (i) === "number")
// if (!batchIdIndexArray || batchIdIndexArray.length === 0) return 0
// else return Math.max(...batchIdIndexArray.map(index => glbHeader.accessors[index].max[0])) + 1
if (!this.threeObject) return null
let maxBatchId = 0
const stack = [this.threeObject]
while (stack.length > 0) {
const node = stack.pop()
if (node.isMesh || node.isLine || node.isPoints) {
const batchIdArray = node.geometry.attributes.batchId?.array
if (batchIdArray) {
for (const batchId of batchIdArray) {
maxBatchId = Math.max(maxBatchId, batchId)
}
}
}
if (node.children && node.children.length > 0) {
stack.push(...node.children)
}
}
return maxBatchId + 1
}
async export(options = {}) {
return await exportGltfFromTHREE(this.threeObject, options)
}
}