UNPKG

@animech-public/playcanvas

Version:
2 lines (1 loc) 7.98 kB
import{math as t}from"../../core/math/math.js";import{Mat3 as e}from"../../core/math/mat3.js";import{Mat4 as s}from"../../core/math/mat4.js";import{Vec3 as n}from"../../core/math/vec3.js";import{CULLFACE_NONE as o}from"../../platform/graphics/constants.js";import{BlendState as a}from"../../platform/graphics/blend-state.js";import{DepthState as i}from"../../platform/graphics/depth-state.js";import{drawQuadWithShader as r}from"../graphics/quad-render-utils.js";import{EMITTERSHAPE_BOX as l,EMITTERSHAPE_SPHERE as c,EMITTERSHAPE_HEMISPHERE as h,EMITTERSHAPE_CYLINDER as d,EMITTERSHAPE_CONE as m}from"../constants.js";const u=new e,p=new e,S=new e;class B{constructor(t,e){this._emitter=t,this.frameRandomUniform=new Float32Array(3),this.emitterPosUniform=new Float32Array(3),this.emitterScaleUniform=new Float32Array([1,1,1]),this.worldBoundsMulUniform=new Float32Array(3),this.worldBoundsAddUniform=new Float32Array(3),this.inBoundsSizeUniform=new Float32Array(3),this.inBoundsCenterUniform=new Float32Array(3),this.constantParticleTexIN=e.scope.resolve("particleTexIN"),this.constantParticleTexOUT=e.scope.resolve("particleTexOUT"),this.constantEmitterPos=e.scope.resolve("emitterPos"),this.constantEmitterScale=e.scope.resolve("emitterScale"),this.constantSpawnBounds=e.scope.resolve("spawnBounds"),this.constantSpawnPosInnerRatio=e.scope.resolve("spawnPosInnerRatio"),this.constantSpawnBoundsSphere=e.scope.resolve("spawnBoundsSphere"),this.constantSpawnBoundsSphereInnerRatio=e.scope.resolve("spawnBoundsSphereInnerRatio"),this.constantSpawnBoundsLength=e.scope.resolve("spawnBoundsLength"),this.constantSpawnBoundsLengthInnerRatio=e.scope.resolve("spawnBoundsLengthInnerRatio"),this.constantInitialVelocity=e.scope.resolve("initialVelocity"),this.constantFrameRandom=e.scope.resolve("frameRandom"),this.constantDelta=e.scope.resolve("delta"),this.constantRate=e.scope.resolve("rate"),this.constantRateDiv=e.scope.resolve("rateDiv"),this.constantLifetime=e.scope.resolve("lifetime"),this.constantGraphSampleSize=e.scope.resolve("graphSampleSize"),this.constantGraphNumSamples=e.scope.resolve("graphNumSamples"),this.constantInternalTex0=e.scope.resolve("internalTex0"),this.constantInternalTex1=e.scope.resolve("internalTex1"),this.constantInternalTex2=e.scope.resolve("internalTex2"),this.constantInternalTex3=e.scope.resolve("internalTex3"),this.constantEmitterMatrix=e.scope.resolve("emitterMatrix"),this.constantEmitterMatrixInv=e.scope.resolve("emitterMatrixInv"),this.constantNumParticles=e.scope.resolve("numParticles"),this.constantNumParticlesPot=e.scope.resolve("numParticlesPot"),this.constantLocalVelocityDivMult=e.scope.resolve("localVelocityDivMult"),this.constantVelocityDivMult=e.scope.resolve("velocityDivMult"),this.constantRotSpeedDivMult=e.scope.resolve("rotSpeedDivMult"),this.constantSeed=e.scope.resolve("seed"),this.constantStartAngle=e.scope.resolve("startAngle"),this.constantStartAngle2=e.scope.resolve("startAngle2"),this.constantOutBoundsMul=e.scope.resolve("outBoundsMul"),this.constantOutBoundsAdd=e.scope.resolve("outBoundsAdd"),this.constantInBoundsSize=e.scope.resolve("inBoundsSize"),this.constantInBoundsCenter=e.scope.resolve("inBoundsCenter"),this.constantMaxVel=e.scope.resolve("maxVel"),this.constantFaceTangent=e.scope.resolve("faceTangent"),this.constantFaceBinorm=e.scope.resolve("faceBinorm")}_setInputBounds(){this.inBoundsSizeUniform[0]=this._emitter.prevWorldBoundsSize.x,this.inBoundsSizeUniform[1]=this._emitter.prevWorldBoundsSize.y,this.inBoundsSizeUniform[2]=this._emitter.prevWorldBoundsSize.z,this.constantInBoundsSize.setValue(this.inBoundsSizeUniform),this.inBoundsCenterUniform[0]=this._emitter.prevWorldBoundsCenter.x,this.inBoundsCenterUniform[1]=this._emitter.prevWorldBoundsCenter.y,this.inBoundsCenterUniform[2]=this._emitter.prevWorldBoundsCenter.z,this.constantInBoundsCenter.setValue(this.inBoundsCenterUniform)}randomize(){this.frameRandomUniform[0]=Math.random(),this.frameRandomUniform[1]=Math.random(),this.frameRandomUniform[2]=Math.random()}update(e,B,v,f,w){const x=this._emitter;e.setBlendState(a.NOBLEND),e.setDepthState(i.NODEPTH),e.setCullMode(o),this.randomize(),this.constantGraphSampleSize.setValue(1/x.precision),this.constantGraphNumSamples.setValue(x.precision),this.constantNumParticles.setValue(x.numParticles),this.constantNumParticlesPot.setValue(x.numParticlesPot),this.constantInternalTex0.setValue(x.internalTex0),this.constantInternalTex1.setValue(x.internalTex1),this.constantInternalTex2.setValue(x.internalTex2),this.constantInternalTex3.setValue(x.internalTex3);const V=x.meshInstance.node,U=null===V?n.ONE:V.localScale;if(x.pack8){this.worldBoundsMulUniform[0]=x.worldBoundsMul.x,this.worldBoundsMulUniform[1]=x.worldBoundsMul.y,this.worldBoundsMulUniform[2]=x.worldBoundsMul.z,this.constantOutBoundsMul.setValue(this.worldBoundsMulUniform),this.worldBoundsAddUniform[0]=x.worldBoundsAdd.x,this.worldBoundsAddUniform[1]=x.worldBoundsAdd.y,this.worldBoundsAddUniform[2]=x.worldBoundsAdd.z,this.constantOutBoundsAdd.setValue(this.worldBoundsAddUniform),this._setInputBounds();let t=x.maxVel*Math.max(Math.max(U.x,U.y),U.z);t=Math.max(t,1),this.constantMaxVel.setValue(t)}const T=null===V||x.localSpace?n.ZERO:V.getPosition(),I=null===V?s.IDENTITY:V.getWorldTransform();x.emitterShape===l?(u.setFromMat4(B),this.constantSpawnBounds.setValue(u.data),this.constantSpawnPosInnerRatio.setValue(v)):x.emitterShape===c?(this.constantSpawnBoundsSphere.setValue(x.emitterRadius),this.constantSpawnBoundsSphereInnerRatio.setValue(0===x.emitterRadius?0:x.emitterRadiusInner/x.emitterRadius)):x.emitterShape===h?(u.setFromMat4(B),this.constantSpawnBounds.setValue(u.data),this.constantSpawnBoundsSphere.setValue(x.emitterRadius),this.constantSpawnBoundsSphereInnerRatio.setValue(0===x.emitterRadius?0:x.emitterRadiusInner/x.emitterRadius)):x.emitterShape===d||x.emitterShape===m?(u.setFromMat4(B),this.constantSpawnBounds.setValue(u.data),this.constantSpawnBoundsSphere.setValue(x.emitterRadius),this.constantSpawnBoundsSphereInnerRatio.setValue(0===x.emitterRadius?0:x.emitterRadiusInner/x.emitterRadius),this.constantSpawnBoundsLength.setValue(x.emitterLength),this.constantSpawnBoundsLengthInnerRatio.setValue(0===x.emitterLength?0:x.emitterLengthInner/x.emitterLength)):console.warn(`ParticleGPUUpdater: Unknown emitter shape ${x.emitterShape}`),this.constantInitialVelocity.setValue(x.initialVelocity),p.setFromMat4(I),S.invertMat4(I),this.emitterPosUniform[0]=T.x,this.emitterPosUniform[1]=T.y,this.emitterPosUniform[2]=T.z,this.constantEmitterPos.setValue(this.emitterPosUniform),this.constantFrameRandom.setValue(this.frameRandomUniform),this.constantDelta.setValue(f),this.constantRate.setValue(x.rate),this.constantRateDiv.setValue(x.rate2-x.rate),this.constantStartAngle.setValue(x.startAngle*t.DEG_TO_RAD),this.constantStartAngle2.setValue(x.startAngle2*t.DEG_TO_RAD),this.constantSeed.setValue(x.seed),this.constantLifetime.setValue(x.lifetime),this.emitterScaleUniform[0]=U.x,this.emitterScaleUniform[1]=U.y,this.emitterScaleUniform[2]=U.z,this.constantEmitterScale.setValue(this.emitterScaleUniform),this.constantEmitterMatrix.setValue(p.data),this.constantEmitterMatrixInv.setValue(S.data),this.constantLocalVelocityDivMult.setValue(x.localVelocityUMax),this.constantVelocityDivMult.setValue(x.velocityUMax),this.constantRotSpeedDivMult.setValue(x.rotSpeedUMax[0]);let R=x.swapTex?x.particleTexOUT:x.particleTexIN;R=x.beenReset?x.particleTexStart:R;const M=x.swapTex?x.particleTexIN:x.particleTexOUT;this.constantParticleTexIN.setValue(R),r(e,x.swapTex?x.rtParticleTexIN:x.rtParticleTexOUT,w?x.shaderParticleUpdateOnStop:x.loop?x.shaderParticleUpdateRespawn:x.shaderParticleUpdateNoRespawn),x.material.setParameter("particleTexOUT",R),x.material.setParameter("particleTexIN",M),x.beenReset=!1,x.swapTex=!x.swapTex,x.prevWorldBoundsSize.copy(x.worldBoundsSize),x.prevWorldBoundsCenter.copy(x.worldBounds.center),x.pack8&&this._setInputBounds()}}export{B as ParticleGPUUpdater};