-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSaveLoad.js
More file actions
163 lines (102 loc) · 4.7 KB
/
SaveLoad.js
File metadata and controls
163 lines (102 loc) · 4.7 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import * as THREE from 'three';
import './GLTFLoader';
import './GLTFExporter';
import { COLORS } from './Colors';
import { GroupWrapper, loadGroupFromExistingGroupObject } from './Group'
import { serializeConstraintWrapper ,deserializeConstraintWrapper } from './Physics/ConstraintSystem/Constraints/ConstraintWrappers/ConstraintWrapperSerialization';
const jetpack = require('fs-jetpack');
const { dialog } = require('electron').remote
export class SaveLoad {
constructor(){
}
save(world, filename) {
const data = {};
let groups = world.getGroups();
let constraints = world.physics.constraintStore.getConstraints();
data.groupMap = {};
data.groups = [];
groups.filter(x => x._alive).forEach(group => {
data.groupMap[group.id] = {
id: group.id,
name: group.name,
collisionGroup: group.collisionGroup,
collidesWith: group.collidesWith,
mass: group._mass,
fixed: group._fixed,
position: group.obj.position,
rotation: group.obj.rotation
};
data.groups.push(group.id);
});
data.constraints = constraints.map(c => serializeConstraintWrapper(c));
var exporter = new THREE.GLTFExporter();
var container = new THREE.Object3D();
groups.forEach( group => {
container.add(group.obj);
});
// Parse the input and generate the glTF output
exporter.parse( container, function ( gltf ) {
jetpack.write(filename, data);
jetpack.write(filename + '.gltf', gltf);
groups.forEach( group => {
container.remove(group.obj);
world.scene.add(group.obj);
});
}, {} );
}
load(world, filename) {
world.deleteAllGroups();
// Instantiate a loader
var loader = new THREE.GLTFLoader();
const gltfData = jetpack.read(filename + '.gltf');
// Load a glTF resource
loader.parse(
gltfData,
'',
function success( gltf ) {
world.scene.add( gltf.scene );
gltf.animations; // Array<THREE.AnimationClip>
gltf.scene; // THREE.Scene
gltf.scenes; // Array<THREE.Scene>
gltf.cameras; // Array<THREE.Camera>
const data = jetpack.read(filename, 'json');
// Find the group scene objects
const groupObjects = gltf.scene.children[0].children;
groupObjects.forEach(obj => {
// We need to convert the buffer geometry to normal geometry.
// supporting buffer geometry is a big job.
const newChildren = [];
obj.children.forEach(child => {
// The conversion from buffer geometry will stop us changing face colors
// if the geometry has vertex colours set.
child.geometry.attributes.color = undefined;
const newGeom = new THREE.Geometry().fromBufferGeometry(child.geometry);
const mat = new THREE.MeshPhongMaterial({color: COLORS.OBJECT_BASE_COLOR });
mat.vertexColors = THREE.FaceColors;
const newChild = new THREE.Mesh(newGeom, mat);
// Stuff like groupId is stored here.
newChild.userData = child.userData;
newChild.position.copy(child.position);
newChild.quaternion.copy(child.quaternion);
newChildren.push(newChild);
});
// Clone the array so we can modify it while iterating.
// remove all children
obj.children.splice(0).forEach(child => obj.remove(child));
// Add in the Direct Geometry replacements.
newChildren.forEach( child => obj.add(child) );
loadGroupFromExistingGroupObject(obj, data.groupMap[obj.userData.groupId], world.physics, world.scene );
});
for(var i = 0; i < data.constraints.length; i++) {
deserializeConstraintWrapper(data.constraints[i], world.physics);
}
},
// called when parsing has errors
function ( error ) {
console.trace();
console.log(error);
throw error;
}
);
}
}