中化加油站项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

116 lines
3.0 KiB

import { ParticleHelper, ParticleSystem, ParticleSystemSet, Scene, Vector3 } from "@babylonjs/core";
/**
*
*/
export class ParticleSystemTool {
/**
*
*/
psPool: Map<string, ParticleSystem>;
scene: Scene;
constructor(scene: Scene) {
this.scene = scene;
this.psPool = new Map();
}
static instance: ParticleSystemTool;
static Init(scene: Scene) {
ParticleSystemTool.instance = new ParticleSystemTool(scene);
}
static dispose() {
}
/**
*
* @param name
* @param path
* @param onSuccess
*/
loadPrefab(name: string, path: string, clone: boolean = true, onSuccess?: (ps: ParticleSystem) => void) {
let instance = this;
ParticleHelper.ParseFromFileAsync(null, path, this.scene, false).then((value: ParticleSystem) => {
value.stop();
value.emitter = Vector3.Zero();
value.name += "(prefab)";
instance.psPool.set(path, value);
if (clone) {
let result = instance.cloneParticle(value, name);
if (onSuccess) {
onSuccess(result)
}
}
else {
if (onSuccess) {
onSuccess(value)
}
}
});
}
/**
*
* @param path
* @param onSuccess
*/
importParticle(name: string, path: string, onSuccess: (ps: ParticleSystem) => void) {
let instance = this;
if (instance.psPool.has(path)) {
let set = instance.psPool.get(path);
let result = instance.cloneParticle(set, name);
console.log(set.uniqueId + "克隆特效id", result.uniqueId);
onSuccess(result);
}
else {
console.log("加载特效", name);
instance.loadPrefab(null, path, true, (value: ParticleSystem) => {
onSuccess(value);
})
// ParticleHelper.ParseFromFileAsync(null, path, this.scene, false).then((value: ParticleSystem) => {
// instance.psPool.set(path, value);
// let result = instance.cloneParticle(value, name);
// onSuccess(result)
// });
}
}
/**
*
* @param setPrefab
* @param name
*/
cloneParticleSet(setPrefab: ParticleSystemSet, name: string) {
let result = new ParticleSystemSet();
for (let i = 0; i < setPrefab.systems.length; i++) {
result.systems.push(this.cloneParticle(setPrefab.systems[i] as ParticleSystem, name));
}
return result;
}
/**
*
* @param particle
* @param name
*/
cloneParticle(particle: ParticleSystem, name: string) {
// let root = new AbstractMesh("root_" + name, this.scene);
let root = Vector3.Zero();
let result = particle.clone(name, root);
return result;
}
}