forked from chapuzzo/three-experiments
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbufferGeometryMerger.js
More file actions
63 lines (48 loc) · 1.78 KB
/
bufferGeometryMerger.js
File metadata and controls
63 lines (48 loc) · 1.78 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import * as THREE from 'three'
let sizes = {
position: 3,
normal: 3,
uv: 2
}
function bufferGeometryMerger (...geometries) {
let composedGeometry = new THREE.BufferGeometry()
let arrays = {}
;['position', 'normal', 'uv'].forEach(attribute => {
let length = geometries.reduce((total, geometry) => {
let currentAttribute = geometry.getAttribute(attribute)
// console.log(currentAttribute)
return total + currentAttribute.count * currentAttribute.itemSize
}, 0)
// console.log(length)
arrays[attribute] = new Float32Array(length)
geometries.reduce((mergedLength, geometry) => {
let currentAttribute = geometry.getAttribute(attribute).array
// console.log(currentAttribute)
currentAttribute.forEach((element, index) => {
arrays[attribute][mergedLength + index] = element
})
return mergedLength + currentAttribute.length
}, 0)
composedGeometry.addAttribute(attribute, new THREE.BufferAttribute(
arrays[attribute],
sizes[attribute]
))
// let composedArray = Float32Array.from(
// geometries.reduce((total, geometry) => {
// let currentAttribute = geometry.getAttribute(attribute)
// // console.log(currentAttribute)
// console.log(currentAttribute, currentAttribute.length)
// return total.concat(Array.from(currentAttribute.array))
// // return total + currentAttribute.count * currentAttribute.size
// }, [])
// )
// // console.log(composedArray, composedArray.length)
// composedGeometry.addAttribute(attribute, new THREE.BufferAttribute(
// composedArray,
// sizes[attribute]
// ))
// console.log(composedGeometry.getAttribute(attribute))
})
return composedGeometry
}
export default bufferGeometryMerger