From a3e16d37797bc05d89e88c6810d4c7bca3f58b48 Mon Sep 17 00:00:00 2001 From: Forrest Sun Date: Sun, 9 Jun 2024 19:03:43 -0700 Subject: [PATCH] use Object3D's toJson on Particle System --- src/ParticleEmitter.ts | 83 +++--------------------------------- test/ParticleEmitter.test.ts | 19 ++++++++- 2 files changed, 24 insertions(+), 78 deletions(-) diff --git a/src/ParticleEmitter.ts b/src/ParticleEmitter.ts index 1b55c0a..1b343f1 100644 --- a/src/ParticleEmitter.ts +++ b/src/ParticleEmitter.ts @@ -46,82 +46,11 @@ export class ParticleEmitter exte } toJSON(meta?: MetaData, options: SerializationOptions = {}): any { - // meta is a string when called from JSON.stringify - const isRootObject = meta === undefined || typeof meta === 'string'; - const output: any = {}; - // meta is a hash used to collect geometries, materials. - // not providing it implies that this is the root object - // being serialized. - if (isRootObject) { - // initialize meta obj - meta = { - geometries: {}, - materials: {}, - textures: {}, - images: {}, - shapes: {}, - skeletons: {}, - animations: {}, - nodes: {}, - }; - - output.metadata = { - version: 4.5, - type: 'Object', - generator: 'Object3D.toJSON', - }; - } - - // standard Object3D serialization - const object: any = {}; - - object.uuid = this.uuid; - object.type = this.type; - - if (this.name !== '') object.name = this.name; - if (this.castShadow === true) object.castShadow = true; - if (this.receiveShadow === true) object.receiveShadow = true; - if (this.visible === false) object.visible = false; - if (this.frustumCulled === false) object.frustumCulled = false; - if (this.renderOrder !== 0) object.renderOrder = this.renderOrder; - if (JSON.stringify(this.userData) !== '{}') object.userData = this.userData; - - object.layers = this.layers.mask; - object.matrix = this.matrix.toArray(); - - if (this.matrixAutoUpdate === false) object.matrixAutoUpdate = false; - - // object specific properties - - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - if (this.system !== null) object.ps = this.system.toJSON(meta!, options); - - if (this.children.length > 0) { - object.children = []; - for (let i = 0; i < this.children.length; i++) { - if (this.children[i].type !== 'ParticleSystemPreview') { - object.children.push(this.children[i].toJSON(meta).object); - } - } - } - - if (isRootObject) { - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const geometries = this.extractFromCache(meta!.geometries); - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const materials = this.extractFromCache(meta!.materials); - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const textures = this.extractFromCache(meta!.textures); - // eslint-disable-next-line @typescript-eslint/no-non-null-assertion - const images = this.extractFromCache(meta!.images); - - if (geometries.length > 0) output.geometries = geometries; - if (materials.length > 0) output.materials = materials; - if (textures.length > 0) output.textures = textures; - if (images.length > 0) output.images = images; - } - - output.object = object; - return output; + const children = this.children; + this.children = this.children.filter((child) => child.type !== 'ParticleSystemPreview'); + const data = super.toJSON(meta); + this.children = children; + if (this.system !== null) data.object.ps = this.system.toJSON(meta!, options); + return data; } } diff --git a/test/ParticleEmitter.test.ts b/test/ParticleEmitter.test.ts index 95ff7fc..06722e3 100644 --- a/test/ParticleEmitter.test.ts +++ b/test/ParticleEmitter.test.ts @@ -15,7 +15,7 @@ import { SphereEmitter, TrailSettings, } from '../src'; -import {Layers, MeshBasicMaterial, NormalBlending, Texture, Vector3, Vector4} from 'three'; +import {Layers, MeshBasicMaterial, NormalBlending, Object3D, Texture, Vector3, Vector4} from 'three'; import {QuarksLoader} from '../src'; import {BatchedRenderer} from '../src'; @@ -124,4 +124,21 @@ describe('ParticleEmitter', () => { expect((system.rendererEmitterSettings as TrailSettings).startLength!.type).toBe('value'); expect(system.behaviors.length).toBe(2); }); + + test('.fromJSON parent', () => { + // const meta = { geometries: {}, materials: {}, textures: {}, images: {} }; + const parent = new Object3D(); + parent.add(glowBeam.emitter); + const json = parent.toJSON(); + // console.log(json); + const loader = new QuarksLoader(); + const emitter = loader.parse(json, () => {}).children[0] as ParticleEmitter; + const system = emitter.system as ParticleSystem; + expect(system.rendererSettings.layers.mask).toBe(3); + expect(system.startTileIndex.type).toBe('value'); + expect(system.rendererSettings.instancingGeometry).toBeDefined(); + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + expect((system.rendererEmitterSettings as TrailSettings).startLength!.type).toBe('value'); + expect(system.behaviors.length).toBe(2); + }); });