UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 19.1 kB
import{LAYERID_DEPTH as e}from"../../../scene/constants.js";import{Mesh as t}from"../../../scene/mesh.js";import{ParticleEmitter as s}from"../../../scene/particle-system/particle-emitter.js";import{Asset as a}from"../../asset/asset.js";import{Component as i}from"../component.js";const r=["emitterExtents","emitterRadius","emitterLength","emitterExtentsInner","emitterRadiusInner","emitterLengthInner","loop","initialVelocity","animSpeed","normalMap","particleNormal"],n=["numParticles","lifetime","rate","rate2","startAngle","startAngle2","lighting","halfLambert","intensity","wrap","wrapBounds","depthWrite","noFog","sort","stretch","alignToMotion","preWarm","emitterShape","animTilesX","animTilesY","animStartFrame","animNumFrames","animNumAnimations","animIndex","randomizeAnimIndex","animLoop","colorMap","localSpace","screenSpace","orientation"],h=["scaleGraph","scaleGraph2","colorGraph","colorGraph2","alphaGraph","alphaGraph2","velocityGraph","velocityGraph2","localVelocityGraph","localVelocityGraph2","rotationSpeedGraph","rotationSpeedGraph2","radialSpeedGraph","radialSpeedGraph2"],o=["colorMapAsset","normalMapAsset","meshAsset","renderAsset"];let l;class d extends i{constructor(e,t){super(e,t),this._requestedDepth=!1,this._drawOrder=0,this._evtLayersChanged=null,this._evtLayerAdded=null,this._evtLayerRemoved=null,this._evtSetMeshes=null,this.on("set_colorMapAsset",this.onSetColorMapAsset,this),this.on("set_normalMapAsset",this.onSetNormalMapAsset,this),this.on("set_meshAsset",this.onSetMeshAsset,this),this.on("set_mesh",this.onSetMesh,this),this.on("set_renderAsset",this.onSetRenderAsset,this),this.on("set_loop",this.onSetLoop,this),this.on("set_blendType",this.onSetBlendType,this),this.on("set_depthSoftening",this.onSetDepthSoftening,this),this.on("set_layers",this.onSetLayers,this),r.forEach((e=>{this.on(`set_${e}`,this.onSetSimpleProperty,this)})),n.forEach((e=>{this.on(`set_${e}`,this.onSetComplexProperty,this)})),h.forEach((e=>{this.on(`set_${e}`,this.onSetGraphProperty,this)}))}get data(){const e=this.system.store[this.entity.getGuid()];return e?e.data:null}set enabled(e){this._setValue("enabled",e)}get enabled(){return this.data.enabled}set autoPlay(e){this._setValue("autoPlay",e)}get autoPlay(){return this.data.autoPlay}set numParticles(e){this._setValue("numParticles",e)}get numParticles(){return this.data.numParticles}set lifetime(e){this._setValue("lifetime",e)}get lifetime(){return this.data.lifetime}set rate(e){this._setValue("rate",e)}get rate(){return this.data.rate}set rate2(e){this._setValue("rate2",e)}get rate2(){return this.data.rate2}set startAngle(e){this._setValue("startAngle",e)}get startAngle(){return this.data.startAngle}set startAngle2(e){this._setValue("startAngle2",e)}get startAngle2(){return this.data.startAngle2}set loop(e){this._setValue("loop",e)}get loop(){return this.data.loop}set preWarm(e){this._setValue("preWarm",e)}get preWarm(){return this.data.preWarm}set lighting(e){this._setValue("lighting",e)}get lighting(){return this.data.lighting}set halfLambert(e){this._setValue("halfLambert",e)}get halfLambert(){return this.data.halfLambert}set intensity(e){this._setValue("intensity",e)}get intensity(){return this.data.intensity}set depthWrite(e){this._setValue("depthWrite",e)}get depthWrite(){return this.data.depthWrite}set noFog(e){this._setValue("noFog",e)}get noFog(){return this.data.noFog}set depthSoftening(e){this._setValue("depthSoftening",e)}get depthSoftening(){return this.data.depthSoftening}set sort(e){this._setValue("sort",e)}get sort(){return this.data.sort}set blendType(e){this._setValue("blendType",e)}get blendType(){return this.data.blendType}set stretch(e){this._setValue("stretch",e)}get stretch(){return this.data.stretch}set alignToMotion(e){this._setValue("alignToMotion",e)}get alignToMotion(){return this.data.alignToMotion}set emitterShape(e){this._setValue("emitterShape",e)}get emitterShape(){return this.data.emitterShape}set emitterExtents(e){this._setValue("emitterExtents",e)}get emitterExtents(){return this.data.emitterExtents}set emitterExtentsInner(e){this._setValue("emitterExtentsInner",e)}get emitterExtentsInner(){return this.data.emitterExtentsInner}set emitterRadius(e){this._setValue("emitterRadius",e)}get emitterRadius(){return this.data.emitterRadius}set emitterRadiusInner(e){this._setValue("emitterRadiusInner",e)}get emitterRadiusInner(){return this.data.emitterRadiusInner}set emitterLength(e){this._setValue("emitterLength",e)}get emitterLength(){return this.data.emitterLength}set emitterLengthInner(e){this._setValue("emitterLengthInner",e)}get emitterLengthInner(){return this.data.emitterLengthInner}set initialVelocity(e){this._setValue("initialVelocity",e)}get initialVelocity(){return this.data.initialVelocity}set wrap(e){this._setValue("wrap",e)}get wrap(){return this.data.wrap}set wrapBounds(e){this._setValue("wrapBounds",e)}get wrapBounds(){return this.data.wrapBounds}set localSpace(e){this._setValue("localSpace",e)}get localSpace(){return this.data.localSpace}set screenSpace(e){this._setValue("screenSpace",e)}get screenSpace(){return this.data.screenSpace}set colorMapAsset(e){this._setValue("colorMapAsset",e)}get colorMapAsset(){return this.data.colorMapAsset}set normalMapAsset(e){this._setValue("normalMapAsset",e)}get normalMapAsset(){return this.data.normalMapAsset}set mesh(e){this._setValue("mesh",e)}get mesh(){return this.data.mesh}set meshAsset(e){this._setValue("meshAsset",e)}get meshAsset(){return this.data.meshAsset}set renderAsset(e){this._setValue("renderAsset",e)}get renderAsset(){return this.data.renderAsset}set orientation(e){this._setValue("orientation",e)}get orientation(){return this.data.orientation}set particleNormal(e){this._setValue("particleNormal",e)}get particleNormal(){return this.data.particleNormal}set localVelocityGraph(e){this._setValue("localVelocityGraph",e)}get localVelocityGraph(){return this.data.localVelocityGraph}set localVelocityGraph2(e){this._setValue("localVelocityGraph2",e)}get localVelocityGraph2(){return this.data.localVelocityGraph2}set velocityGraph(e){this._setValue("velocityGraph",e)}get velocityGraph(){return this.data.velocityGraph}set velocityGraph2(e){this._setValue("velocityGraph2",e)}get velocityGraph2(){return this.data.velocityGraph2}set rotationSpeedGraph(e){this._setValue("rotationSpeedGraph",e)}get rotationSpeedGraph(){return this.data.rotationSpeedGraph}set rotationSpeedGraph2(e){this._setValue("rotationSpeedGraph2",e)}get rotationSpeedGraph2(){return this.data.rotationSpeedGraph2}set radialSpeedGraph(e){this._setValue("radialSpeedGraph",e)}get radialSpeedGraph(){return this.data.radialSpeedGraph}set radialSpeedGraph2(e){this._setValue("radialSpeedGraph2",e)}get radialSpeedGraph2(){return this.data.radialSpeedGraph2}set scaleGraph(e){this._setValue("scaleGraph",e)}get scaleGraph(){return this.data.scaleGraph}set scaleGraph2(e){this._setValue("scaleGraph2",e)}get scaleGraph2(){return this.data.scaleGraph2}set colorGraph(e){this._setValue("colorGraph",e)}get colorGraph(){return this.data.colorGraph}set colorGraph2(e){this._setValue("colorGraph2",e)}get colorGraph2(){return this.data.colorGraph2}set alphaGraph(e){this._setValue("alphaGraph",e)}get alphaGraph(){return this.data.alphaGraph}set alphaGraph2(e){this._setValue("alphaGraph2",e)}get alphaGraph2(){return this.data.alphaGraph2}set colorMap(e){this._setValue("colorMap",e)}get colorMap(){return this.data.colorMap}set normalMap(e){this._setValue("normalMap",e)}get normalMap(){return this.data.normalMap}set animTilesX(e){this._setValue("animTilesX",e)}get animTilesX(){return this.data.animTilesX}set animTilesY(e){this._setValue("animTilesY",e)}get animTilesY(){return this.data.animTilesY}set animStartFrame(e){this._setValue("animStartFrame",e)}get animStartFrame(){return this.data.animStartFrame}set animNumFrames(e){this._setValue("animNumFrames",e)}get animNumFrames(){return this.data.animNumFrames}set animNumAnimations(e){this._setValue("animNumAnimations",e)}get animNumAnimations(){return this.data.animNumAnimations}set animIndex(e){this._setValue("animIndex",e)}get animIndex(){return this.data.animIndex}set randomizeAnimIndex(e){this._setValue("randomizeAnimIndex",e)}get randomizeAnimIndex(){return this.data.randomizeAnimIndex}set animSpeed(e){this._setValue("animSpeed",e)}get animSpeed(){return this.data.animSpeed}set animLoop(e){this._setValue("animLoop",e)}get animLoop(){return this.data.animLoop}set layers(e){this._setValue("layers",e)}get layers(){return this.data.layers}set drawOrder(e){this._drawOrder=e,this.emitter&&(this.emitter.drawOrder=e)}get drawOrder(){return this._drawOrder}_setValue(e,t){const s=this.data,a=s[e];s[e]=t,this.fire("set",e,a,t)}addMeshInstanceToLayers(){if(this.emitter)for(let e=0;e<this.layers.length;e++){const t=this.system.app.scene.layers.getLayerById(this.layers[e]);t&&(t.addMeshInstances([this.emitter.meshInstance]),this.emitter._layer=t)}}removeMeshInstanceFromLayers(){if(this.emitter)for(let e=0;e<this.layers.length;e++){const t=this.system.app.scene.layers.getLayerById(this.layers[e]);t&&t.removeMeshInstances([this.emitter.meshInstance])}}onSetLayers(e,t,s){if(this.emitter){for(let e=0;e<t.length;e++){const s=this.system.app.scene.layers.getLayerById(t[e]);s&&s.removeMeshInstances([this.emitter.meshInstance])}if(this.enabled&&this.entity.enabled)for(let e=0;e<s.length;e++){const t=this.system.app.scene.layers.getLayerById(s[e]);t&&t.addMeshInstances([this.emitter.meshInstance])}}}onLayersChanged(e,t){this.addMeshInstanceToLayers(),e.off("add",this.onLayerAdded,this),e.off("remove",this.onLayerRemoved,this),t.on("add",this.onLayerAdded,this),t.on("remove",this.onLayerRemoved,this)}onLayerAdded(e){if(!this.emitter)return;this.layers.indexOf(e.id)<0||e.addMeshInstances([this.emitter.meshInstance])}onLayerRemoved(e){if(!this.emitter)return;this.layers.indexOf(e.id)<0||e.removeMeshInstances([this.emitter.meshInstance])}_bindColorMapAsset(e){if(e.on("load",this._onColorMapAssetLoad,this),e.on("unload",this._onColorMapAssetUnload,this),e.on("remove",this._onColorMapAssetRemove,this),e.on("change",this._onColorMapAssetChange,this),e.resource)this._onColorMapAssetLoad(e);else{if(!this.enabled||!this.entity.enabled)return;this.system.app.assets.load(e)}}_unbindColorMapAsset(e){e.off("load",this._onColorMapAssetLoad,this),e.off("unload",this._onColorMapAssetUnload,this),e.off("remove",this._onColorMapAssetRemove,this),e.off("change",this._onColorMapAssetChange,this)}_onColorMapAssetLoad(e){this.colorMap=e.resource}_onColorMapAssetUnload(e){this.colorMap=null}_onColorMapAssetRemove(e){this._onColorMapAssetUnload(e)}_onColorMapAssetChange(e){}onSetColorMapAsset(e,t,s){const i=this.system.app.assets;if(t){const e=i.get(t);e&&this._unbindColorMapAsset(e)}if(s){s instanceof a&&(this.data.colorMapAsset=s.id,s=s.id);const e=i.get(s);e?this._bindColorMapAsset(e):i.once(`add:${s}`,(e=>{this._bindColorMapAsset(e)}))}else this.colorMap=null}_bindNormalMapAsset(e){if(e.on("load",this._onNormalMapAssetLoad,this),e.on("unload",this._onNormalMapAssetUnload,this),e.on("remove",this._onNormalMapAssetRemove,this),e.on("change",this._onNormalMapAssetChange,this),e.resource)this._onNormalMapAssetLoad(e);else{if(!this.enabled||!this.entity.enabled)return;this.system.app.assets.load(e)}}_unbindNormalMapAsset(e){e.off("load",this._onNormalMapAssetLoad,this),e.off("unload",this._onNormalMapAssetUnload,this),e.off("remove",this._onNormalMapAssetRemove,this),e.off("change",this._onNormalMapAssetChange,this)}_onNormalMapAssetLoad(e){this.normalMap=e.resource}_onNormalMapAssetUnload(e){this.normalMap=null}_onNormalMapAssetRemove(e){this._onNormalMapAssetUnload(e)}_onNormalMapAssetChange(e){}onSetNormalMapAsset(e,t,s){const i=this.system.app.assets;if(t){const e=i.get(t);e&&this._unbindNormalMapAsset(e)}if(s){s instanceof a&&(this.data.normalMapAsset=s.id,s=s.id);const e=i.get(s);e?this._bindNormalMapAsset(e):i.once(`add:${s}`,(e=>{this._bindNormalMapAsset(e)}))}else this.normalMap=null}_bindMeshAsset(e){if(e.on("load",this._onMeshAssetLoad,this),e.on("unload",this._onMeshAssetUnload,this),e.on("remove",this._onMeshAssetRemove,this),e.on("change",this._onMeshAssetChange,this),e.resource)this._onMeshAssetLoad(e);else{if(!this.enabled||!this.entity.enabled)return;this.system.app.assets.load(e)}}_unbindMeshAsset(e){e.off("load",this._onMeshAssetLoad,this),e.off("unload",this._onMeshAssetUnload,this),e.off("remove",this._onMeshAssetRemove,this),e.off("change",this._onMeshAssetChange,this)}_onMeshAssetLoad(e){this._onMeshChanged(e.resource)}_onMeshAssetUnload(e){this.mesh=null}_onMeshAssetRemove(e){this._onMeshAssetUnload(e)}_onMeshAssetChange(e){}onSetMeshAsset(e,t,s){const i=this.system.app.assets;if(t){const e=i.get(t);e&&this._unbindMeshAsset(e)}if(s){s instanceof a&&(this.data.meshAsset=s.id,s=s.id);const e=i.get(s);e&&this._bindMeshAsset(e)}else this._onMeshChanged(null)}onSetMesh(e,t,s){!s||s instanceof a||"number"==typeof s?this.meshAsset=s:this._onMeshChanged(s)}_onMeshChanged(e){!e||e instanceof t||(e=e.meshInstances[0]?e.meshInstances[0].mesh:null),this.data.mesh=e,this.emitter&&(this.emitter.mesh=e,this.emitter.resetMaterial(),this.rebuild())}onSetRenderAsset(e,t,s){const i=this.system.app.assets;if(t){const e=i.get(t);e&&this._unbindRenderAsset(e)}if(s){s instanceof a&&(this.data.renderAsset=s.id,s=s.id);const e=i.get(s);e&&this._bindRenderAsset(e)}else this._onRenderChanged(null)}_bindRenderAsset(e){if(e.on("load",this._onRenderAssetLoad,this),e.on("unload",this._onRenderAssetUnload,this),e.on("remove",this._onRenderAssetRemove,this),e.resource)this._onRenderAssetLoad(e);else{if(!this.enabled||!this.entity.enabled)return;this.system.app.assets.load(e)}}_unbindRenderAsset(e){var t;e.off("load",this._onRenderAssetLoad,this),e.off("unload",this._onRenderAssetUnload,this),e.off("remove",this._onRenderAssetRemove,this),null==(t=this._evtSetMeshes)||t.off(),this._evtSetMeshes=null}_onRenderAssetLoad(e){this._onRenderChanged(e.resource)}_onRenderAssetUnload(e){this._onRenderChanged(null)}_onRenderAssetRemove(e){this._onRenderAssetUnload(e)}_onRenderChanged(e){var t;e?(null==(t=this._evtSetMeshes)||t.off(),this._evtSetMeshes=e.on("set:meshes",this._onRenderSetMeshes,this),e.meshes&&this._onRenderSetMeshes(e.meshes)):this._onMeshChanged(null)}_onRenderSetMeshes(e){this._onMeshChanged(e&&e[0])}onSetLoop(e,t,s){this.emitter&&(this.emitter[e]=s,this.emitter.resetTime())}onSetBlendType(e,t,s){this.emitter&&(this.emitter[e]=s,this.emitter.material.blendType=s,this.emitter.resetMaterial(),this.rebuild())}_requestDepth(){this._requestedDepth||(l||(l=this.system.app.scene.layers.getLayerById(e)),l&&(l.incrementCounter(),this._requestedDepth=!0))}_releaseDepth(){this._requestedDepth&&l&&(l.decrementCounter(),this._requestedDepth=!1)}onSetDepthSoftening(e,t,s){t!==s&&(s?(this.enabled&&this.entity.enabled&&this._requestDepth(),this.emitter&&(this.emitter[e]=s)):(this.enabled&&this.entity.enabled&&this._releaseDepth(),this.emitter&&(this.emitter[e]=s)),this.emitter&&(this.reset(),this.emitter.resetMaterial(),this.rebuild()))}onSetSimpleProperty(e,t,s){this.emitter&&(this.emitter[e]=s,this.emitter.resetMaterial())}onSetComplexProperty(e,t,s){this.emitter&&(this.emitter[e]=s,this.emitter.resetMaterial(),this.rebuild(),this.reset())}onSetGraphProperty(e,t,s){this.emitter&&(this.emitter[e]=s,this.emitter.rebuildGraphs(),this.emitter.resetMaterial())}onEnable(){const e=this.system.app.scene,i=e.layers,r=this.data;for(let e=0,t=o.length;e<t;e++){let t=r[o[e]];if(t){if(!(t instanceof a)){if(!(parseInt(t,10)>=0))continue;t=this.system.app.assets.get(t)}t&&!t.resource&&this.system.app.assets.load(t)}}if(!this.system.app.graphicsDevice.disableParticleSystem){if(!this.emitter){let e=r.mesh;e instanceof t||(e=null),this.emitter=new s(this.system.app.graphicsDevice,{numParticles:r.numParticles,emitterExtents:r.emitterExtents,emitterExtentsInner:r.emitterExtentsInner,emitterRadius:r.emitterRadius,emitterRadiusInner:r.emitterRadiusInner,emitterLength:r.emitterLength,emitterLengthInner:r.emitterLengthInner,emitterShape:r.emitterShape,initialVelocity:r.initialVelocity,wrap:r.wrap,localSpace:r.localSpace,screenSpace:r.screenSpace,wrapBounds:r.wrapBounds,lifetime:r.lifetime,rate:r.rate,rate2:r.rate2,orientation:r.orientation,particleNormal:r.particleNormal,animTilesX:r.animTilesX,animTilesY:r.animTilesY,animStartFrame:r.animStartFrame,animNumFrames:r.animNumFrames,animNumAnimations:r.animNumAnimations,animIndex:r.animIndex,randomizeAnimIndex:r.randomizeAnimIndex,animSpeed:r.animSpeed,animLoop:r.animLoop,startAngle:r.startAngle,startAngle2:r.startAngle2,scaleGraph:r.scaleGraph,scaleGraph2:r.scaleGraph2,colorGraph:r.colorGraph,colorGraph2:r.colorGraph2,alphaGraph:r.alphaGraph,alphaGraph2:r.alphaGraph2,localVelocityGraph:r.localVelocityGraph,localVelocityGraph2:r.localVelocityGraph2,velocityGraph:r.velocityGraph,velocityGraph2:r.velocityGraph2,rotationSpeedGraph:r.rotationSpeedGraph,rotationSpeedGraph2:r.rotationSpeedGraph2,radialSpeedGraph:r.radialSpeedGraph,radialSpeedGraph2:r.radialSpeedGraph2,colorMap:r.colorMap,normalMap:r.normalMap,loop:r.loop,preWarm:r.preWarm,sort:r.sort,stretch:r.stretch,alignToMotion:r.alignToMotion,lighting:r.lighting,halfLambert:r.halfLambert,intensity:r.intensity,depthSoftening:r.depthSoftening,scene:this.system.app.scene,mesh:e,depthWrite:r.depthWrite,noFog:r.noFog,node:this.entity,blendType:r.blendType}),this.emitter.meshInstance.node=this.entity,this.emitter.drawOrder=this.drawOrder,r.autoPlay||(this.pause(),this.emitter.meshInstance.visible=!1)}this.emitter.colorMap&&this.addMeshInstanceToLayers(),this._evtLayersChanged=e.on("set:layers",this.onLayersChanged,this),i&&(this._evtLayerAdded=i.on("add",this.onLayerAdded,this),this._evtLayerRemoved=i.on("remove",this.onLayerRemoved,this)),this.enabled&&this.entity.enabled&&r.depthSoftening&&this._requestDepth()}}onDisable(){var e;const t=this.system.app.scene.layers;var s,a;(null==(e=this._evtLayersChanged)||e.off(),this._evtLayersChanged=null,t)&&(null==(s=this._evtLayerAdded)||s.off(),this._evtLayerAdded=null,null==(a=this._evtLayerRemoved)||a.off(),this._evtLayerRemoved=null);this.emitter&&(this.removeMeshInstanceFromLayers(),this.data.depthSoftening&&this._releaseDepth(),this.emitter.camera=null)}onBeforeRemove(){this.enabled&&(this.enabled=!1),this.emitter&&(this.emitter.destroy(),this.emitter=null);for(let e=0;e<o.length;e++){const t=o[e];this.data[t]&&(this[t]=null)}this.off()}reset(){this.emitter&&this.emitter.reset()}stop(){this.emitter&&(this.emitter.loop=!1,this.emitter.resetTime(),this.emitter.addTime(0,!0))}pause(){this.data.paused=!0}unpause(){this.data.paused=!1}play(){this.data.paused=!1,this.emitter&&(this.emitter.meshInstance.visible=!0,this.emitter.loop=this.data.loop,this.emitter.resetTime())}isPlaying(){return!this.data.paused&&(!(!this.emitter||!this.emitter.loop)||Date.now()<=this.emitter.endTime)}rebuild(){const e=this.enabled;this.enabled=!1,this.emitter&&(this.emitter.rebuild(),this.emitter.meshInstance.node=this.entity),this.enabled=e}}export{d as ParticleSystemComponent};