-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGeometryManager.js
56 lines (46 loc) · 1.58 KB
/
GeometryManager.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
50
51
52
53
54
55
import isEqual from 'lodash/isEqual';
import {BufferAttribute} from 'three/src/core/BufferAttribute';
import {BufferGeometry} from 'three/src/core/BufferGeometry';
const GeometryCache = {};
const PropsCache = {};
const ActualGeometry = {};
export function getOrCreateGeometry(props){
if(!props.id) console.warn('Cannot rely on props id');
let cached = PropsCache[props.id] || {};
if(props.type == cached.type && isEqual(props.arguments, cached.arguments)){
return ActualGeometry[props.id];
}
delete PropsCache[props.id];
delete ActualGeometry[props.id];
return createGeometry(props);
}
export function createGeometry(props){
let geometry = new props.type(...props.arguments);
GeometryCache[geometry.uuid] = props; // old way
PropsCache[props.id] = props;
ActualGeometry[props.id] = geometry;
return geometry;
}
export function replaceGeometryIfNeeded(props, oldGeometry){
// console.warn("TODO: replace this function call");
let cached = GeometryCache[oldGeometry.uuid] || {};
if(props.type == cached.type && isEqual(props.arguments, cached.arguments)){
return oldGeometry;
}else{
// console.log("not ok", props.arguments, cached.arguments);
}
delete GeometryCache[oldGeometry.uuid];
return createGeometry(props);
}
export function createBufferGeometry(props){
let geometry = new BufferGeometry();
for(let key in props){
let attr = props[key];
let attribute = new BufferAttribute(attr.array, attr.size)
if(key == 'index')
geometry.setIndex(attribute);
else
geometry.addAttribute(key, attribute);
}
return geometry;
}