Skip to content

Commit 14c40e7

Browse files
committed
added toJSON()
1 parent f9874da commit 14c40e7

File tree

4 files changed

+154
-0
lines changed

4 files changed

+154
-0
lines changed

examples/index.html

+2
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@
198198
entity.position.set(0, 5, 10);
199199

200200
app.start();
201+
202+
app.toJSON();
201203
</script>
202204
</body>
203205
</html>

src/core/App.js

+37
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,43 @@ export class App {
190190

191191
}
192192

193+
toJSON() {
194+
195+
const output = {
196+
metadata: {
197+
'version': 1,
198+
'type': 'App',
199+
'generator': 'TaroExporter'
200+
},
201+
scenes: [],
202+
parameters: Object.assign( {}, this.parameters )
203+
};
204+
205+
const op = output.parameters;
206+
207+
// can't jsonify an HTMLelement, so remove it
208+
delete op.canvas;
209+
210+
// update physics
211+
if ( this.physics.allowSleep === false ) op.allowSleep = false;
212+
if ( this.physics.epsilon !== 0.001 ) op.epsilon = this.parameters.epsilon;
213+
if ( this.physics.gravity.equals( { x: 0, y: - 9.80665, z: 0 } ) === false ) op.gravity = this.parameters.gravity;
214+
if ( this.physics.maxSubSteps !== 10 ) op.maxSubSteps = this.parameters.maxSubSteps;
215+
216+
// update time
217+
if ( this.time.fixedTimestep !== 0.02 ) op.fixedTimestep = this.parameters.fixedTimestep;
218+
if ( this.time.maxDeltaTime !== 0.1 ) op.maxDeltaTime = this.parameters.maxDeltaTime;
219+
if ( this.time.timeScale !== 1 ) op.timeScale = this.parameters.timeScale;
220+
221+
for ( let i = 0, len = this.scenes.length; i < len; i ++ )
222+
output.scenes[ i ] = this.scenes[ i ].toJSON();
223+
224+
output.currentScene = ( this.currentScene !== undefined ) ? this.currentScene.uuid : this.scenes[0].uuid;
225+
226+
return output;
227+
228+
}
229+
193230
}
194231

195232
App.prototype.isApp = true;

src/core/Entity.js

+93
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,99 @@ export class Entity extends Group {
300300

301301
}
302302

303+
toJSON() {
304+
305+
const output = {};
306+
307+
output.uuid = this.uuid;
308+
output.matrix = this.matrix.toArray();
309+
310+
if ( this.name !== '' ) output.name = this.name;
311+
if ( this.castShadow === true ) output.castShadow = true;
312+
if ( this.receiveShadow === true ) output.receiveShadow = true;
313+
if ( this.visible === false ) this.visible = false;
314+
if ( this._enabled === false ) output.enabled = false;
315+
316+
if ( this.componentData !== undefined ) {
317+
318+
const array = JSON.parse( JSON.stringify( this.componentData ) );
319+
320+
output.components = [];
321+
322+
// sorting array to place non-dependency components last
323+
array.sort( ( a, b ) => {
324+
325+
const dependenciesA = ComponentManager.components[ a.type ].config.dependencies;
326+
const dependenciesB = ComponentManager.components[ b.type ].config.dependencies;
327+
328+
if ( dependenciesA === undefined && dependenciesB !== undefined )
329+
return 1;
330+
return - 1;
331+
332+
} );
333+
334+
while ( array.length > 0 ) {
335+
336+
let i = array.length;
337+
338+
while ( i -- ) {
339+
340+
const dependencies = ComponentManager.components[ array[ i ].type ].config.dependencies;
341+
342+
if ( dependencies !== undefined ) {
343+
344+
let wait = false;
345+
for ( let j = 0, len = dependencies.length; j < len; j ++ ) {
346+
347+
if ( array.includes( dependencies[ j ] ) ) {
348+
349+
wait = true;
350+
break;
351+
352+
}
353+
354+
}
355+
356+
if ( wait === true ) {
357+
358+
continue;
359+
360+
}
361+
362+
}
363+
364+
output.components.push( array[ i ] );
365+
array.splice( i, 1 );
366+
367+
}
368+
369+
}
370+
371+
} else if ( this.components.length > 0 ) {
372+
373+
output.components = [];
374+
375+
for ( let i = 0; i < this.components.length; i++ ) {
376+
if ( typeof this.components[i].toJSON === 'function' )
377+
output.components.push( this.components.toJSON() )
378+
379+
}
380+
381+
}
382+
383+
const children = this.getEntities();
384+
if ( children.length > 0 ) {
385+
386+
output.children = [];
387+
for ( let i = 0, len = children.length; i < len; i ++ )
388+
output.children.push( this.parseEntity( children[ i ] ) );
389+
390+
}
391+
392+
return output;
393+
394+
}
395+
303396
get app() {
304397

305398
return this.scene.app;

src/core/Scene.js

+22
Original file line numberDiff line numberDiff line change
@@ -230,4 +230,26 @@ export class Scene extends ThreeScene {
230230

231231
}
232232

233+
toJSON() {
234+
235+
const output = {};
236+
237+
output.uuid = this.uuid;
238+
if ( this.background !== null ) output.background = this.background.toJSON();
239+
if ( this.environment !== null ) output.environment = this.environment.toJSON();
240+
if ( this.fog !== null ) output.fog = this.fog.toJSON();
241+
242+
const children = this.getEntities();
243+
if ( children.length > 0 ) {
244+
245+
output.children = [];
246+
for ( let i = 0, len = children.length; i < len; i ++ )
247+
output.children.push( children[ i ].toJSON() );
248+
249+
}
250+
251+
return output;
252+
253+
}
254+
233255
}

0 commit comments

Comments
 (0)