Skip to content

Commit

Permalink
lint the project and implement initial version of node based vfx
Browse files Browse the repository at this point in the history
  • Loading branch information
Alchemist0823 committed Sep 22, 2023
1 parent 0895ce0 commit ca32e87
Show file tree
Hide file tree
Showing 27 changed files with 930 additions and 299 deletions.
1 change: 1 addition & 0 deletions examples/trailDemo.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
Gradient,
} from './js/three.quarks.esm.js';
import {Demo} from './demo.js';

export class TrailDemo extends Demo {
name = 'Trail Renderer and Physics';

Expand Down
41 changes: 35 additions & 6 deletions src/BatchedRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,37 @@
import {VFXBatch, RenderMode, StoredBatchSettings} from './VFXBatch';
import {ParticleSystem, VFXBatchSettings} from './ParticleSystem';
import {Object3D} from 'three';
import {BufferGeometry, Layers, Material, Object3D} from 'three';
import {SpriteBatch} from './SpriteBatch';
import {TrailBatch} from './TrailBatch';
import {ParticleEmitter} from './ParticleEmitter';
import {Particle} from './Particle';

export interface VFXBatchSettings {
// 5 component x,y,z,u,v
instancingGeometry: BufferGeometry;
material: Material;
uTileCount: number;
vTileCount: number;
renderMode: RenderMode;
renderOrder: number;
layers: Layers;
}
export interface SerializationOptions {
useUrlForImage?: boolean;
}
export interface IParticleSystem {
speedFactor: number;
worldSpace: boolean;
particleNum: number;
particles: Array<Particle>;
emitter: ParticleEmitter<any>;
_renderer?: BatchedRenderer;

getRendererSettings(): VFXBatchSettings;

clone(): IParticleSystem;

toJSON(metaData: any, options: SerializationOptions): any;
}

/**
* the class represents the batch renderer. a three.js scene should only have one batchedRenderer
Expand All @@ -11,7 +40,7 @@ import {TrailBatch} from './TrailBatch';
*/
export class BatchedRenderer extends Object3D {
batches: Array<VFXBatch> = [];
systemToBatchIndex: Map<ParticleSystem, number> = new Map<ParticleSystem, number>();
systemToBatchIndex: Map<IParticleSystem, number> = new Map<IParticleSystem, number>();
type = 'BatchedRenderer';

constructor() {
Expand All @@ -35,7 +64,7 @@ export class BatchedRenderer extends Object3D {
);
}

addSystem(system: ParticleSystem) {
addSystem(system: IParticleSystem) {
system._renderer = this;
const settings = system.getRendererSettings();
for (let i = 0; i < this.batches.length; i++) {
Expand Down Expand Up @@ -64,7 +93,7 @@ export class BatchedRenderer extends Object3D {
this.add(batch);
}

deleteSystem(system: ParticleSystem) {
deleteSystem(system: IParticleSystem) {
const batchIndex = this.systemToBatchIndex.get(system);
if (batchIndex != undefined) {
this.batches[batchIndex].removeSystem(system);
Expand All @@ -79,7 +108,7 @@ export class BatchedRenderer extends Object3D {
}*/
}

updateSystem(system: ParticleSystem) {
updateSystem(system: IParticleSystem) {
this.deleteSystem(system);
this.addSystem(system);
}
Expand Down
108 changes: 49 additions & 59 deletions src/ParticleEmitter.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@

import {ParticleSystem, SerializationOptions} from './ParticleSystem';
import {
Object3D, BaseEvent
} from 'three';
import {Object3D, BaseEvent} from 'three';
import {IParticleSystem, SerializationOptions} from './BatchedRenderer';

export interface MetaData {
geometries: {[key: string]: any};
Expand All @@ -16,12 +13,11 @@ export interface MetaData {
}

export class ParticleEmitter<E extends BaseEvent> extends Object3D<E> {

type = "ParticleEmitter";
system: ParticleSystem;
type = 'ParticleEmitter';
system: IParticleSystem;
//interleavedBuffer: InterleavedBuffer;

constructor(system: ParticleSystem) {
constructor(system: IParticleSystem) {
super();
this.system = system;
// this.visible = false;
Expand All @@ -34,34 +30,29 @@ export class ParticleEmitter<E extends BaseEvent> extends Object3D<E> {
return system.emitter as any;
}

dispose() {
}

dispose() {}

// extract data from the cache hash
// remove metadata on each item
// and return as array
extractFromCache( cache: any ) {
extractFromCache(cache: any) {
const values = [];
for ( const key in cache ) {

const data = cache[ key ];
for (const key in cache) {
const data = cache[key];
delete data.metadata;
values.push( data );

values.push(data);
}
return values;
}

toJSON(meta?: MetaData, options: SerializationOptions = {}): any {
// meta is a string when called from JSON.stringify
const isRootObject = ( meta === undefined || typeof meta === 'string' );
// 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 ) {

// 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: {},
Expand All @@ -71,67 +62,66 @@ export class ParticleEmitter<E extends BaseEvent> extends Object3D<E> {
shapes: {},
skeletons: {},
animations: {},
nodes: {}
nodes: {},
};

output.metadata = {
version: 4.5,
type: 'Object',
generator: 'Object3D.toJSON'
generator: 'Object3D.toJSON',
};

}

// standard Object3D serialization
const object: any = {};
// 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;
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();
object.layers = this.layers.mask;
object.matrix = this.matrix.toArray();

if ( this.matrixAutoUpdate === false ) object.matrixAutoUpdate = false;
if (this.matrixAutoUpdate === false) object.matrixAutoUpdate = false;

// object specific properties
// 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.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") {
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 ) {
if (isRootObject) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const geometries = this.extractFromCache( meta!.geometries );
const geometries = this.extractFromCache(meta!.geometries);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const materials = this.extractFromCache( meta!.materials );
const materials = this.extractFromCache(meta!.materials);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const textures = this.extractFromCache( meta!.textures );
const textures = this.extractFromCache(meta!.textures);
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const images = this.extractFromCache( meta!.images );
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;
}
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;
output.object = object;
return output;
}
}
23 changes: 4 additions & 19 deletions src/ParticleSystem.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import {FunctionValueGenerator, ValueGenerator, ValueGeneratorFromJSON} from './functions/ValueGenerator';
import {Behavior, BehaviorFromJSON} from './behaviors/Behavior';
import {FunctionValueGenerator, ValueGenerator, ValueGeneratorFromJSON} from './functions';
import {Behavior, BehaviorFromJSON} from './behaviors';
import {Particle, SpriteParticle, TrailParticle} from './Particle';
import {MetaData, ParticleEmitter} from './ParticleEmitter';
import {EmitterFromJSON, EmitterShape, ShapeJSON} from './shape/EmitterShape';
import {EmitterFromJSON, EmitterShape, ShapeJSON} from './shape';
import {
AdditiveBlending,
BaseEvent,
Expand Down Expand Up @@ -34,19 +34,8 @@ import {
MemorizedFunctionColorGenerator,
} from './functions';
import {RenderMode} from './VFXBatch';
import {BatchedRenderer} from './BatchedRenderer';
import {BatchedRenderer, SerializationOptions, VFXBatchSettings} from './BatchedRenderer';
import {RotationGenerator} from './functions/RotationGenerator';

export interface VFXBatchSettings {
// 5 component x,y,z,u,v
instancingGeometry: BufferGeometry;
material: Material;
uTileCount: number;
vTileCount: number;
renderMode: RenderMode;
renderOrder: number;
layers: Layers;
}
export interface BurstParameters {
time: number;
count: number;
Expand Down Expand Up @@ -167,10 +156,6 @@ export interface EmissionState {
waitEmiting: number;
}

export interface SerializationOptions {
useUrlForImage?: boolean;
}

/**
* ParticleSystem represents a system that generates and controls particles with similar attributes.
*
Expand Down
Loading

0 comments on commit ca32e87

Please sign in to comment.