From 2449dbcbfa5ec49172bef64abc950ce6bf62700b Mon Sep 17 00:00:00 2001 From: zxg0622 <247274249@qq.com> Date: Tue, 2 Jul 2019 11:16:27 +0800 Subject: [PATCH] fix particle rateoverdistance bug & some tslint errors (#4783) --- .../particle/animator/gradient-range.ts | 90 ------ .../particle/animator/optimized-curve.ts | 130 -------- .../particle/particle-system-component.ts | 16 +- .../renderer/particle-system-gpu-renderer.js | 288 ------------------ .../renderer/particle-system-renderer.ts | 4 +- cocos/3d/framework/particle/renderer/trail.ts | 164 +++++----- cocos/renderer/models/particle-batch-model.ts | 14 +- 7 files changed, 103 insertions(+), 603 deletions(-) delete mode 100644 cocos/3d/framework/particle/renderer/particle-system-gpu-renderer.js diff --git a/cocos/3d/framework/particle/animator/gradient-range.ts b/cocos/3d/framework/particle/animator/gradient-range.ts index a0aecb5acf..8a91b18202 100644 --- a/cocos/3d/framework/particle/animator/gradient-range.ts +++ b/cocos/3d/framework/particle/animator/gradient-range.ts @@ -133,93 +133,3 @@ export default class GradientRange { // gradientMin: null, // gradientMax: null // }); - -export class GradientUniform { - public gr: GradientRange | null; - public minColor: Float32Array | null = null; - public maxColor: Float32Array | null = null; - public minColorKeyTime: Float32Array | null = null; - public minColorKeyValue: Float32Array | null = null; - public minAlphaKeyTime: Float32Array | null = null; - public minAlphaKeyValue: Float32Array | null = null; - public maxColorKeyTime: Float32Array | null = null; - public maxColorKeyValue: Float32Array | null = null; - public maxAlphaKeyTime: Float32Array | null = null; - public maxAlphaKeyValue: Float32Array | null = null; - - constructor (gr: GradientRange) { - this.gr = gr; - if (gr.mode === 'color') { - this.minColor = new Float32Array(4); - color4.array(this.minColor, gr.color); - } else if (gr.mode === 'twoColors') { - this.minColor = new Float32Array(4); - this.maxColor = new Float32Array(4); - color4.array(this.minColor, gr.colorMin); - color4.array(this.maxColor, gr.colorMax); - } else if (gr.mode === 'gradient') { - this.minColorKeyTime = new Float32Array(gr.gradient.colorKeys.length); - this.minColorKeyValue = new Float32Array(3 * this.minColorKeyTime.length); - this.minAlphaKeyTime = new Float32Array(gr.gradient.alphaKeys.length); - this.minAlphaKeyValue = new Float32Array(this.minAlphaKeyTime.length); - (this.constructor as any).generateGradientUniform(gr.gradient, this.minColorKeyTime, this.minColorKeyValue, this.minAlphaKeyTime, this.minAlphaKeyValue); - } else if (gr.mode === 'twoGradients') { - this.minColorKeyTime = new Float32Array(gr.gradientMin.colorKeys.length); - this.minColorKeyValue = new Float32Array(3 * this.minColorKeyTime.length); - this.minAlphaKeyTime = new Float32Array(gr.gradientMin.alphaKeys.length); - this.minAlphaKeyValue = new Float32Array(this.minAlphaKeyTime.length); - (this.constructor as any).generateGradientUniform(gr.gradientMin, this.minColorKeyTime, this.minColorKeyValue, this.minAlphaKeyTime, this.minAlphaKeyValue); - this.maxColorKeyTime = new Float32Array(gr.gradientMax.colorKeys.length); - this.maxColorKeyValue = new Float32Array(3 * this.maxColorKeyTime.length); - this.maxAlphaKeyTime = new Float32Array(gr.gradientMax.alphaKeys.length); - this.maxAlphaKeyValue = new Float32Array(this.maxAlphaKeyTime.length); - (this.constructor as any).generateGradientUniform(gr.gradientMax, this.maxColorKeyTime, this.maxColorKeyValue, this.maxAlphaKeyTime, this.maxAlphaKeyValue); - } - } - - public uploadUniform (device: any, name: string) { - if (this.gr != null) { - if (this.gr.mode === 'color') { - device.setUniform('u_' + name + '_rangeMode', GRADIENT_RANGE_MODE_COLOR); - device.setUniform('u_' + name + '_minColor', this.minColor); - } else if (this.gr.mode === 'twoColors') { - device.setUniform('u_' + name + '_rangeMode', GRADIENT_RANGE_MODE_TWO_COLOR); - device.setUniform('u_' + name + '_minColor', this.minColor); - device.setUniform('u_' + name + '_maxColor', this.maxColor); - } else if (this.gr.mode === 'gradient') { - device.setUniform('u_' + name + '_rangeMode', GRADIENT_RANGE_MODE_GRADIENT); - device.setUniform('u_' + name + '_minGradMode', this.gr.gradient.mode === 'blend' ? GRADIENT_MODE_BLEND : GRADIENT_MODE_FIX); - device.setUniform('u_' + name + '_minColorKeyValue', this.minColorKeyValue); - device.setUniform('u_' + name + '_minColorKeyTime', this.minColorKeyTime); - device.setUniform('u_' + name + '_minAlphaKeyValue', this.minAlphaKeyValue); - device.setUniform('u_' + name + '_minAlphaKeyTime', this.minAlphaKeyTime); - } else if (this.gr.mode === 'twoGradients') { - device.setUniform('u_' + name + '_rangeMode', GRADIENT_RANGE_MODE_TWO_GRADIENT); - device.setUniform('u_' + name + '_minGradMode', this.gr.gradientMin.mode === 'blend' ? GRADIENT_MODE_BLEND : GRADIENT_MODE_FIX); - device.setUniform('u_' + name + '_minColorKeyValue', this.minColorKeyValue); - device.setUniform('u_' + name + '_minColorKeyTime', this.minColorKeyTime); - device.setUniform('u_' + name + '_minAlphaKeyValue', this.minAlphaKeyValue); - device.setUniform('u_' + name + '_minAlphaKeyTime', this.minAlphaKeyTime); - - device.setUniform('u_' + name + '_maxGradMode', this.gr.gradientMax.mode === 'blend' ? GRADIENT_MODE_BLEND : GRADIENT_MODE_FIX); - device.setUniform('u_' + name + '_maxColorKeyValue', this.maxColorKeyValue); - device.setUniform('u_' + name + '_maxColorKeyTime', this.maxColorKeyTime); - device.setUniform('u_' + name + '_maxAlphaKeyValue', this.maxAlphaKeyValue); - device.setUniform('u_' + name + '_maxAlphaKeyTime', this.maxAlphaKeyTime); - } - } - } - - public static generateGradientUniform (gradient: Gradient, colorKeyTime: number, colorKeyValue: Float32Array, alphaKeyTime: number, alphaKeyValue: Float32Array) { - for (let i = 0; i < gradient.colorKeys.length; i++) { - colorKeyTime[i] = gradient.colorKeys[i].time; - colorKeyValue[i * 3] = gradient.colorKeys[i].color.r; - colorKeyValue[i * 3 + 1] = gradient.colorKeys[i].color.g; - colorKeyValue[i * 3 + 2] = gradient.colorKeys[i].color.b; - } - for (let i = 0; i < gradient.alphaKeys.length; i++) { - alphaKeyTime[i] = gradient.alphaKeys[i].time; - alphaKeyValue[i] = gradient.alphaKeys[i].alpha; - } - } -} diff --git a/cocos/3d/framework/particle/animator/optimized-curve.ts b/cocos/3d/framework/particle/animator/optimized-curve.ts index 54c14fc493..3476cdb3ed 100644 --- a/cocos/3d/framework/particle/animator/optimized-curve.ts +++ b/cocos/3d/framework/particle/animator/optimized-curve.ts @@ -203,133 +203,3 @@ export class OptimizedCurve { } } } - -export class CurveUniform { - private mode: 0 | 'constant' | 'twoConstants' | 'curve' | 'twoCurves'; - private minConstant: number; - private maxConstant: number; - private minCurve: OptimizedCurve | null; - private maxCurve: OptimizedCurve | null; - - constructor () { - this.mode = 0; - this.minConstant = 0; - this.maxConstant = 0; - this.minCurve = null; - this.maxCurve = null; - } - - public constructCurve (cr: CurveRange) { - this.mode = cr.mode; - switch (cr.mode) { - case 'constant': - this.minConstant = cr.constant; - break; - case 'twoConstants': - this.minConstant = cr.constantMin; - this.maxConstant = cr.constantMax; - break; - case 'curve': - this.minCurve = new OptimizedCurve(true); - this.minCurve.buildCurve(cr.curve, cr.multiplier); - break; - case 'twoCurves': - this.minCurve = new OptimizedCurve(true); - this.minCurve.buildCurve(cr.curveMin, cr.multiplier); - this.maxCurve = new OptimizedCurve(true); - this.maxCurve.buildCurve(cr.curveMax, cr.multiplier); - break; - } - } - - public constructCurveIntegral (cr: CurveRange) { - this.mode = cr.mode; - switch (cr.mode) { - case 'constant': - this.minConstant = cr.constant; - break; - case 'twoConstants': - this.minConstant = cr.constantMin; - this.maxConstant = cr.constantMax; - break; - case 'curve': - this.minCurve = new OptimizedCurve(true); - this.minCurve.buildCurve(cr.curve, cr.multiplier); - this.minCurve.integrateOnce(); - break; - case 'twoCurves': - this.minCurve = new OptimizedCurve(true); - this.minCurve.buildCurve(cr.curveMin, cr.multiplier); - this.minCurve.integrateOnce(); - this.maxCurve = new OptimizedCurve(true); - this.maxCurve.buildCurve(cr.curveMax, cr.multiplier); - this.maxCurve.integrateOnce(); - break; - } - } - - public constructCurveIntegralTwice (cr: CurveRange) { - this.mode = cr.mode; - switch (cr.mode) { - case 'constant': - this.minConstant = cr.constant; - break; - case 'twoConstants': - this.minConstant = cr.constantMin; - this.maxConstant = cr.constantMax; - break; - case 'curve': - this.minCurve = new OptimizedCurve(true); - this.minCurve.buildCurve(cr.curve, cr.multiplier); - this.minCurve.integrateTwice(); - break; - case 'twoCurves': - this.minCurve = new OptimizedCurve(true); - this.minCurve.buildCurve(cr.curveMin, cr.multiplier); - this.minCurve.integrateTwice(); - this.maxCurve = new OptimizedCurve(true); - this.maxCurve.buildCurve(cr.curveMax, cr.multiplier); - this.maxCurve.integrateTwice(); - break; - } - } - - public uploadUniform (device: any, name: number) { - switch (this.mode) { - case Mode.Constant: - device.setUniform('u_' + name + '_curveMode', CURVE_MODE_CONSTANT); - device.setUniform('u_' + name + '_minConstant', this.minConstant); - break; - case Mode.TwoConstants: - device.setUniform('u_' + name + '_curveMode', CURVE_MODE_RANDOM_CONSTANT); - device.setUniform('u_' + name + '_minConstant', this.minConstant); - device.setUniform('u_' + name + '_maxConstant', this.maxConstant); - break; - case Mode.Curve: - if (this.minCurve != null) { - device.setUniform('u_' + name + '_curveMode', CURVE_MODE_CURVE); - device.setUniform('u_' + name + '_minKeyTime', this.minCurve.timeUniform); - device.setUniform('u_' + name + '_minKeyCoef', this.minCurve.coefUniform); - if (this.minCurve.integralUniform !== undefined) { - device.setUniform('u_' + name + '_minIntegral', this.minCurve.integralUniform); - } - } - break; - case Mode.TwoCurves: - if (this.minCurve != null && this.maxCurve != null) { - device.setUniform('u_' + name + '_curveMode', CURVE_MODE_RANDOM_CURVE); - device.setUniform('u_' + name + '_minKeyTime', this.minCurve.timeUniform); - device.setUniform('u_' + name + '_minKeyCoef', this.minCurve.coefUniform); - if (this.minCurve.integralUniform !== undefined) { - device.setUniform('u_' + name + '_minIntegral', this.minCurve.integralUniform); - } - device.setUniform('u_' + name + '_maxKeyTime', this.maxCurve.timeUniform); - device.setUniform('u_' + name + '_maxKeyCoef', this.maxCurve.coefUniform); - if (this.minCurve.integralUniform !== undefined) { - device.setUniform('u_' + name + '_maxIntegral', this.maxCurve.integralUniform); - } - } - break; - } - } -} diff --git a/cocos/3d/framework/particle/particle-system-component.ts b/cocos/3d/framework/particle/particle-system-component.ts index b1c31a2427..f6feb89feb 100644 --- a/cocos/3d/framework/particle/particle-system-component.ts +++ b/cocos/3d/framework/particle/particle-system-component.ts @@ -390,8 +390,7 @@ export class ParticleSystemComponent extends RenderableComponent { this.trailModule.init(this); this.textureAnimationModule.onInit(this); - this.node.getWorldPosition(this._oldWPos); - vec3.copy(this._curWPos, this._oldWPos); + this._resetPosition(); // this._system.add(this); } @@ -436,6 +435,8 @@ export class ParticleSystemComponent extends RenderableComponent { this._isPlaying = true; this._isEmitting = true; + this._resetPosition(); + // prewarm if (this._prewarm) { this._prewarmSystem(); @@ -483,8 +484,10 @@ export class ParticleSystemComponent extends RenderableComponent { * 将所有粒子从粒子系统中清除。 */ public clear () { - this.renderer!.clear(); - this.trailModule.clear(); + if (this.enabledInHierarchy) { + this.renderer!.clear(); + this.trailModule.clear(); + } } /** @@ -662,6 +665,11 @@ export class ParticleSystemComponent extends RenderableComponent { } } + private _resetPosition () { + this.node.getWorldPosition(this._oldWPos); + vec3.copy(this._curWPos, this._oldWPos); + } + private addSubEmitter (subEmitter) { this._subEmitters.push(subEmitter); } diff --git a/cocos/3d/framework/particle/renderer/particle-system-gpu-renderer.js b/cocos/3d/framework/particle/renderer/particle-system-gpu-renderer.js deleted file mode 100644 index 1ae876aa4f..0000000000 --- a/cocos/3d/framework/particle/renderer/particle-system-gpu-renderer.js +++ /dev/null @@ -1,288 +0,0 @@ -import ParticleSystemRenderer from "./particle-system-renderer"; -import { Material } from "../../../assets/material"; -import Particle from "../particle"; -import gfx from "../../../../renderer/gfx"; -import { CurveUniform } from "../animator/optimized-curve"; -import { quat } from "../../../../core/vmath"; -import { GradientUniform } from "../animator/gradient-range"; - -let _temp_rot = quat.create(); -let _world_rot_uniform = new Float32Array(4); - -let _vert_attr_name = { - POSITION_STARTTIME: 'a_position_starttime', - VERT_IDX_SIZE_ANGLE: 'a_vertIdx_size_angle', - COLOR: 'a_color', - DIR_LIFE: 'a_dir_life', - RANDOM_SEED: 'a_rndSeed' -}; - -let _gpu_vert_attr = [ - { name: _vert_attr_name.POSITION_STARTTIME, type: gfx.ATTR_TYPE_FLOAT32, num: 4 }, - { name: _vert_attr_name.VERT_IDX_SIZE_ANGLE, type: gfx.ATTR_TYPE_FLOAT32, num: 4 }, - { name: _vert_attr_name.COLOR, type: gfx.ATTR_TYPE_FLOAT32, num: 4 }, - { name: _vert_attr_name.DIR_LIFE, type: gfx.ATTR_TYPE_FLOAT32, num: 4 }, - { name: _vert_attr_name.RANDOM_SEED, type: gfx.ATTR_TYPE_FLOAT32, num: 1 } -]; - -let _space_map = { - 'local': 0, - 'world': 1 -}; - -let _animationMode = { - 'wholeSheet': 0, - 'singleRow': 1 -}; - -let ATTR_INDEX = { - POS: 0, - VERT_IDX: 1, - SIZE: 2, - ANGLE: 3, - COLOR: 4, - DIR: 5, - LIFE_TIME: 6, - START_TIME: 7, - RANDOM_SEED: 8 -}; - -export default class ParticleSystemGpuRenderer extends ParticleSystemRenderer { - onInit(ps) { - this.particleSystem = ps; - if (this._material === null || this._material === undefined) { - this._material = new Material(); - this._material.effect = cc.EffectAsset.get('builtin-particle'); - } - //the following code is just for test use,it will be deleted on release - let m = new Material(); - m.copy(this._material); - m.effect = cc.EffectAsset.get('builtin-particle-gpu'); - Object.keys(this._material._props).forEach(pn => { - m.setProperty(pn, this._material._props[pn]); - }); - this._material = m; - //end test code - this._vertAttrs = _gpu_vert_attr; - this._updateMaterialParams(); - this._updateModel(); - this._tempParticle = new Particle(); - this._particleNum = 0; - this._vertIndexMap = new Int8Array(Object.keys(ATTR_INDEX).length); - this._constructAttributeIndex(); - } - - _constructAttributeIndex() { - let offset = 0; - for (let i = 0; i < _gpu_vert_attr.length; i++) { - switch (_gpu_vert_attr[i].name) { - case _vert_attr_name.POSITION_STARTTIME: - this._vertIndexMap[ATTR_INDEX.POS] = offset; - this._vertIndexMap[ATTR_INDEX.START_TIME] = offset + 3; - break; - case _vert_attr_name.VERT_IDX_SIZE_ANGLE: - this._vertIndexMap[ATTR_INDEX.VERT_IDX] = offset; - this._vertIndexMap[ATTR_INDEX.SIZE] = offset + 2; - this._vertIndexMap[ATTR_INDEX.ANGLE] = offset + 3; - break; - case _vert_attr_name.COLOR: - this._vertIndexMap[ATTR_INDEX.COLOR] = offset; - break; - case _vert_attr_name.DIR_LIFE: - this._vertIndexMap[ATTR_INDEX.DIR] = offset; - this._vertIndexMap[ATTR_INDEX.LIFE_TIME] = offset + 3; - break; - case _vert_attr_name.RANDOM_SEED: - this._vertIndexMap[ATTR_INDEX.RANDOM_SEED] = offset; - break; - } - offset += _gpu_vert_attr[i].num; - } - } - - _getFreeParticle() { - if (this._particleNum >= this.particleSystem._capacity) { - return null; - } - return this._tempParticle; - } - - _setNewParticle(p) { - let offset = this._particleNum * this._model._vertAttrsSize * 4; - for (let i = 0; i < 4; i++) { - this._model._vdataF32[offset++] = p.position.x; - this._model._vdataF32[offset++] = p.position.y; - this._model._vdataF32[offset++] = p.position.z; - this._model._vdataF32[offset++] = this.particleSystem._time; - - this._model._vdataF32[offset++] = this.constructor.uv[2 * i]; - this._model._vdataF32[offset++] = this.constructor.uv[2 * i + 1]; - this._model._vdataF32[offset++] = p.startSize.x; - this._model._vdataF32[offset++] = p.rotation.x; - - this._model._vdataF32[offset++] = p.startColor.r; - this._model._vdataF32[offset++] = p.startColor.g; - this._model._vdataF32[offset++] = p.startColor.b; - this._model._vdataF32[offset++] = p.startColor.a; - - this._model._vdataF32[offset++] = p.velocity.x; - this._model._vdataF32[offset++] = p.velocity.y; - this._model._vdataF32[offset++] = p.velocity.z; - this._model._vdataF32[offset++] = p.startLifetime; - - this._model._vdataF32[offset++] = p.randomSeed; - } - this._particleNum++; - } - - _updateParticles() { - for (let i = 0; i < this._particleNum; ++i) { - let pBaseIndex = i * this._model._vertAttrsSize * 4; - if (this.particleSystem._time - this._model._vdataF32[pBaseIndex + this._vertIndexMap[ATTR_INDEX.START_TIME]] > this._model._vdataF32[pBaseIndex + this._vertIndexMap[ATTR_INDEX.LIFE_TIME]]) { - let lastParticleBaseIndex = (this._particleNum - 1) * this._model._vertAttrsSize * 4; - this._model._vdataF32.copyWithin(pBaseIndex, lastParticleBaseIndex, lastParticleBaseIndex + this._model._vertAttrsSize * 4); - i--; - this._particleNum--; - } - } - } - - _updateRenderData() { - this._model.updateIA(this._particleNum * 6); - } - - updateShaderUniform() { - this._model._device.setUniform('u_psTime', this.particleSystem._time); - let needUploadRot = false; - if (this.particleSystem._velocityOvertimeModule.enable) { - this._material.define('VELOCITY_OVERTIME_MODULE_ENABLE', true); - if (this.velocity_pos_x_uniform === undefined) { - this.velocity_pos_x_uniform = new CurveUniform(); - this.velocity_pos_x_uniform.constructCurveIntegral(this.particleSystem._velocityOvertimeModule._x); - } - if (this.velocity_pos_y_uniform === undefined) { - this.velocity_pos_y_uniform = new CurveUniform(); - this.velocity_pos_y_uniform.constructCurveIntegral(this.particleSystem._velocityOvertimeModule._y); - } - if (this.velocity_pos_z_uniform === undefined) { - this.velocity_pos_z_uniform = new CurveUniform(); - this.velocity_pos_z_uniform.constructCurveIntegral(this.particleSystem._velocityOvertimeModule._z); - } - if (this._renderMode === 'stretchedBillboard') { - if (this.velocity_x_uniform === undefined) { - this.velocity_x_uniform = new CurveUniform(); - this.velocity_x_uniform.constructCurve(this.particleSystem._velocityOvertimeModule._x); - } - if (this.velocity_y_uniform === undefined) { - this.velocity_y_uniform = new CurveUniform(); - this.velocity_y_uniform.constructCurve(this.particleSystem._velocityOvertimeModule._y); - } - if (this.velocity_z_uniform === undefined) { - this.velocity_z_uniform = new CurveUniform(); - this.velocity_z_uniform.constructCurve(this.particleSystem._velocityOvertimeModule._z); - } - this.velocity_x_uniform.uploadUniform(this._model._device, 'velocity_x'); - this.velocity_y_uniform.uploadUniform(this._model._device, 'velocity_y'); - this.velocity_z_uniform.uploadUniform(this._model._device, 'velocity_z'); - } - this.velocity_pos_x_uniform.uploadUniform(this._model._device, 'velocity_pos_x'); - this.velocity_pos_y_uniform.uploadUniform(this._model._device, 'velocity_pos_y'); - this.velocity_pos_z_uniform.uploadUniform(this._model._device, 'velocity_pos_z'); - this._model._device.setUniform('u_speedModifier', this.particleSystem._velocityOvertimeModule._speedModifier.evaluate()); - this._model._device.setUniform('u_velocity_space', _space_map[this.particleSystem._velocityOvertimeModule._space]); - if (this.particleSystem._velocityOvertimeModule._space === 'local') { - needUploadRot = true; - } - } else { - this._material.define('VELOCITY_OVERTIME_MODULE_ENABLE', false); - } - if (this.particleSystem._forceOvertimeModule.enable) { - this._material.define('FORCE_OVERTIME_MODULE_ENABLE', true); - if (this.force_pos_x_uniform === undefined) { - this.force_pos_x_uniform = new CurveUniform(); - this.force_pos_x_uniform.constructCurveIntegralTwice(this.particleSystem._forceOvertimeModule._x); - } - if (this.force_pos_y_uniform === undefined) { - this.force_pos_y_uniform = new CurveUniform(); - this.force_pos_y_uniform.constructCurveIntegralTwice(this.particleSystem._forceOvertimeModule._y); - } - if (this.force_pos_z_uniform === undefined) { - this.force_pos_z_uniform = new CurveUniform(); - this.force_pos_z_uniform.constructCurveIntegralTwice(this.particleSystem._forceOvertimeModule._z); - } - if (this._renderMode === 'stretchedBillboard') { - if (this.force_vel_x_uniform === undefined) { - this.force_vel_x_uniform = new CurveUniform(); - this.force_vel_x_uniform.constructCurveIntegral(this.particleSystem._forceOvertimeModule._x); - } - if (this.force_vel_y_uniform === undefined) { - this.force_vel_y_uniform = new CurveUniform(); - this.force_vel_y_uniform.constructCurveIntegral(this.particleSystem._forceOvertimeModule._y); - } - if (this.force_vel_z_uniform === undefined) { - this.force_vel_z_uniform = new CurveUniform(); - this.force_vel_z_uniform.constructCurveIntegral(this.particleSystem._forceOvertimeModule._z); - } - this.force_vel_x_uniform.uploadUniform(this._model._device, 'force_vel_x'); - this.force_vel_y_uniform.uploadUniform(this._model._device, 'force_vel_y'); - this.force_vel_z_uniform.uploadUniform(this._model._device, 'force_vel_z'); - } - this.force_pos_x_uniform.uploadUniform(this._model._device, 'force_pos_x'); - this.force_pos_y_uniform.uploadUniform(this._model._device, 'force_pos_y'); - this.force_pos_z_uniform.uploadUniform(this._model._device, 'force_pos_z'); - this._model._device.setUniform('u_force_space', _space_map[this.particleSystem._forceOvertimeModule._space]); - if (this.particleSystem._forceOvertimeModule._space === 'local') { - needUploadRot = true; - } - } else { - this._material.define('FORCE_OVERTIME_MODULE_ENABLE', false); - } - if (needUploadRot || this.particleSystem._simulationSpace) { - quat.array(_world_rot_uniform, this.particleSystem._entity.getWorldRot(_temp_rot)); - this._model._device.setUniform('u_worldRot', _world_rot_uniform); - } - if (this.particleSystem._sizeOvertimeModule.enable) { - this._material.define('SIZE_OVERTIME_MODULE_ENABLE', true); - if (this.size_uniform === undefined) { - this.size_uniform = new CurveUniform(); - this.size_uniform.constructCurve(this.particleSystem._sizeOvertimeModule._size); - } - this.size_uniform.uploadUniform(this._model._device, 'size'); - } else { - this._material.define('SIZE_OVERTIME_MODULE_ENABLE', false); - } - if (this.particleSystem._colorOverLifetimeModule.enable) { - this._material.define('COLOR_OVERTIME_MODULE_ENABLE', true); - if (this.color_uniform === undefined) { - this.color_uniform = new GradientUniform(this.particleSystem._colorOverLifetimeModule._color); - } - this.color_uniform.uploadUniform(this._model._device, 'color'); - } else { - this._material.define('COLOR_OVERTIME_MODULE_ENABLE', false); - } - if (this.particleSystem._textureAnimationModule.enable) { - this._material.define('TEXTURE_ANIMATION_ENABLE', true); - if (this.frame_uniform === undefined) { - this.frame_uniform = new CurveUniform(); - this.frame_uniform.constructCurve(this.particleSystem._textureAnimationModule._frameOverTime); - } - this.frame_uniform.uploadUniform(this._model._device, 'frameOverTime'); - this._model._device.setUniform('u_cycles', this.particleSystem._textureAnimationModule._cycleCount); - this._model._device.setUniform('u_animation_mode', _animationMode[this.particleSystem._textureAnimationModule._animation]); - this._model._device.setUniform('u_random_row', this.particleSystem._textureAnimationModule._randomRow); - this._model._device.setUniform('u_row_index', this.particleSystem._textureAnimationModule._rowIndex); - } else { - this._material.define('TEXTURE_ANIMATION_ENABLE', false); - } - if (this.particleSystem._rotationOvertimeModule.enable) { - this._material.define('ROTATE_OVERTIME_MODULE_ENABLE', true); - if (this.size_uniform === undefined) { - this.size_uniform = new CurveUniform(); - this.size_uniform.constructCurveIntegral(this.particleSystem._rotationOvertimeModule._z); - } - this.size_uniform.uploadUniform(this._model._device, 'rotate'); - } else { - this._material.define('ROTATE_OVERTIME_MODULE_ENABLE', false); - } - } -} diff --git a/cocos/3d/framework/particle/renderer/particle-system-renderer.ts b/cocos/3d/framework/particle/renderer/particle-system-renderer.ts index dc3f7ea9be..1dbab53139 100644 --- a/cocos/3d/framework/particle/renderer/particle-system-renderer.ts +++ b/cocos/3d/framework/particle/renderer/particle-system-renderer.ts @@ -393,8 +393,8 @@ export default class ParticleSystemRenderer { public _onMaterialModified (index: number, material: Material) { if (index === 0) { - this._updateMaterialParams(); this._updateModel(); + this._updateMaterialParams(); } else { this._updateTrailMaterial(); } @@ -474,7 +474,6 @@ export default class ParticleSystemRenderer { } else { console.warn(`particle system renderMode ${this._renderMode} not support.`); } - mat!.recompileShaders(this._defines); if (this.particleSystem.textureAnimationModule.enable) { vec2.set(this.frameTile_velLenScale, this.particleSystem.textureAnimationModule.numTilesX, this.particleSystem.textureAnimationModule.numTilesY); @@ -482,6 +481,7 @@ export default class ParticleSystemRenderer { } else { mat!.setProperty('frameTile_velLenScale', this.frameTile_velLenScale); } + mat!.recompileShaders(this._defines); if (this._model) { this._model.setSubModelMaterial(0, this.particleSystem.sharedMaterial || this._defaultMat); } diff --git a/cocos/3d/framework/particle/renderer/trail.ts b/cocos/3d/framework/particle/renderer/trail.ts index 35214c1f16..fd75c85b33 100644 --- a/cocos/3d/framework/particle/renderer/trail.ts +++ b/cocos/3d/framework/particle/renderer/trail.ts @@ -168,6 +168,9 @@ export default class TrailModule { }) public lifeTime = new CurveRange(); + @property + public _minParticleDistance = 0.1; + /** * 每个轨迹粒子之间的最小间距。 */ @@ -183,17 +186,6 @@ export default class TrailModule { this._minSquaredDistance = val * val; } - @property - public _minParticleDistance = 0.1; - - /** - * 轨迹设定时的坐标系。 - */ - @property({ - type: Space, - }) - private _space = Space.World; - @property({ type: Space, displayOrder: 6, @@ -255,6 +247,14 @@ export default class TrailModule { }) public colorOvertime = new GradientRange(); + /** + * 轨迹设定时的坐标系。 + */ + @property({ + type: Space, + }) + private _space = Space.World; + private _particleSystem: any; private _minSquaredDistance: number = 0; private _vertSize: number; @@ -348,54 +348,6 @@ export default class TrailModule { } } - private _createModel () { - if (this._trailModel) { - return; - } - const device: GFXDevice = cc.director.root.device; - const vertexBuffer = device.createBuffer({ - usage: GFXBufferUsageBit.VERTEX | GFXBufferUsageBit.TRANSFER_DST, - memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, - size: this._vertSize * (this._trailNum + 1) * 2, - stride: this._vertSize, - }); - const vBuffer: ArrayBuffer = new ArrayBuffer(this._vertSize * (this._trailNum + 1) * 2); - this._vbF32 = new Float32Array(vBuffer); - this._vbUint32 = new Uint32Array(vBuffer); - vertexBuffer.update(vBuffer); - - const indexBuffer = device.createBuffer({ - usage: GFXBufferUsageBit.INDEX | GFXBufferUsageBit.TRANSFER_DST, - memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, - size: this._trailNum * 6 * Uint16Array.BYTES_PER_ELEMENT, - stride: Uint16Array.BYTES_PER_ELEMENT, - }); - this._iBuffer = new Uint16Array(this._trailNum * 6); - indexBuffer.update(this._iBuffer); - - this._iaInfoBuffer = device.createBuffer({ - usage: GFXBufferUsageBit.INDIRECT, - memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, - size: GFX_DRAW_INFO_SIZE, - stride: 1, - }); - this._iaInfo.drawInfos[0].vertexCount = (this._trailNum + 1) * 2; - this._iaInfo.drawInfos[0].indexCount = this._trailNum * 6; - this._iaInfoBuffer.update(this._iaInfo); - - this._subMeshData = { - vertexBuffers: [vertexBuffer], - indexBuffer, - indirectBuffer: this._iaInfoBuffer, - attributes: this._vertAttrs!, - primitiveMode: GFXPrimitiveMode.TRIANGLE_LIST, - }; - - this._trailModel = this._particleSystem._getRenderScene().createModel(Model, this._particleSystem.node); - this._trailModel!.setSubModelMesh(0, this._subMeshData); - this._trailModel!.enabled = true; - } - public _updateMaterial () { if (this._particleSystem) { const mat = this._particleSystem.renderer.trailMaterial; @@ -465,21 +417,6 @@ export default class TrailModule { } } - private _updateTrailElement (module: any, trailEle: ITrailElement, p: Particle, dt: number): boolean { - trailEle.lifetime += dt; - if (module.colorFromParticle) { - p.color.mul(module.colorOvertime.evaluate(1.0 - p.remainingLifetime / p.startLifetime, 1), trailEle.color); - } else { - trailEle.color.set(module.colorOvertime.evaluate(1.0 - p.remainingLifetime / p.startLifetime, 1)); - } - if (module.widthFromParticle) { - trailEle.width = p.size.x * module.widthRatio.evaluate(trailEle.lifetime / module._trailLifetime, 1)!; - } else { - trailEle.width = module.widthRatio.evaluate(trailEle.lifetime / module._trailLifetime, 1)!; - } - return trailEle.lifetime > module._trailLifetime; - } - public removeParticle (p: Particle) { const trail = this._particleTrail.get(p); if (trail) { @@ -545,6 +482,77 @@ export default class TrailModule { this.updateIA(this.ibOffset); } + public updateIA (count: number) { + this._trailModel!.getSubModel(0).inputAssembler!.vertexBuffers[0].update(this._vbF32!); + this._trailModel!.getSubModel(0).inputAssembler!.indexBuffer!.update(this._iBuffer!); + this._trailModel!.getSubModel(0).inputAssembler!.indexCount = count; + this._trailModel!.getSubModel(0).inputAssembler!.extractDrawInfo(this._iaInfo.drawInfos[0]); + this._iaInfoBuffer.update(this._iaInfo); + } + + private _createModel () { + if (this._trailModel) { + return; + } + const device: GFXDevice = cc.director.root.device; + const vertexBuffer = device.createBuffer({ + usage: GFXBufferUsageBit.VERTEX | GFXBufferUsageBit.TRANSFER_DST, + memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, + size: this._vertSize * (this._trailNum + 1) * 2, + stride: this._vertSize, + }); + const vBuffer: ArrayBuffer = new ArrayBuffer(this._vertSize * (this._trailNum + 1) * 2); + this._vbF32 = new Float32Array(vBuffer); + this._vbUint32 = new Uint32Array(vBuffer); + vertexBuffer.update(vBuffer); + + const indexBuffer = device.createBuffer({ + usage: GFXBufferUsageBit.INDEX | GFXBufferUsageBit.TRANSFER_DST, + memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, + size: this._trailNum * 6 * Uint16Array.BYTES_PER_ELEMENT, + stride: Uint16Array.BYTES_PER_ELEMENT, + }); + this._iBuffer = new Uint16Array(this._trailNum * 6); + indexBuffer.update(this._iBuffer); + + this._iaInfoBuffer = device.createBuffer({ + usage: GFXBufferUsageBit.INDIRECT, + memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, + size: GFX_DRAW_INFO_SIZE, + stride: 1, + }); + this._iaInfo.drawInfos[0].vertexCount = (this._trailNum + 1) * 2; + this._iaInfo.drawInfos[0].indexCount = this._trailNum * 6; + this._iaInfoBuffer.update(this._iaInfo); + + this._subMeshData = { + vertexBuffers: [vertexBuffer], + indexBuffer, + indirectBuffer: this._iaInfoBuffer, + attributes: this._vertAttrs!, + primitiveMode: GFXPrimitiveMode.TRIANGLE_LIST, + }; + + this._trailModel = this._particleSystem._getRenderScene().createModel(Model, this._particleSystem.node); + this._trailModel!.setSubModelMesh(0, this._subMeshData); + this._trailModel!.enabled = true; + } + + private _updateTrailElement (module: any, trailEle: ITrailElement, p: Particle, dt: number): boolean { + trailEle.lifetime += dt; + if (module.colorFromParticle) { + p.color.mul(module.colorOvertime.evaluate(1.0 - p.remainingLifetime / p.startLifetime, 1), trailEle.color); + } else { + trailEle.color.set(module.colorOvertime.evaluate(1.0 - p.remainingLifetime / p.startLifetime, 1)); + } + if (module.widthFromParticle) { + trailEle.width = p.size.x * module.widthRatio.evaluate(trailEle.lifetime / module._trailLifetime, 1)!; + } else { + trailEle.width = module.widthRatio.evaluate(trailEle.lifetime / module._trailLifetime, 1)!; + } + return trailEle.lifetime > module._trailLifetime; + } + private _fillVertexBuffer (trailSeg: ITrailElement, colorModifer: Color, indexOffset: number, xTexCoord: number, trailEleIdx: number, indexSet: number) { this._vbF32[this.vbOffset++] = trailSeg.position.x; this._vbF32[this.vbOffset++] = trailSeg.position.y; @@ -580,12 +588,4 @@ export default class TrailModule { this._iBuffer[this.ibOffset++] = indexOffset + 2 * trailEleIdx + 2; } } - - public updateIA (count: number) { - this._trailModel!.getSubModel(0).inputAssembler!.vertexBuffers[0].update(this._vbF32!); - this._trailModel!.getSubModel(0).inputAssembler!.indexBuffer!.update(this._iBuffer!); - this._trailModel!.getSubModel(0).inputAssembler!.indexCount = count; - this._trailModel!.getSubModel(0).inputAssembler!.extractDrawInfo(this._iaInfo.drawInfos[0]); - this._iaInfoBuffer.update(this._iaInfo); - } } diff --git a/cocos/renderer/models/particle-batch-model.ts b/cocos/renderer/models/particle-batch-model.ts index 26082cc0b6..92b3f98853 100644 --- a/cocos/renderer/models/particle-batch-model.ts +++ b/cocos/renderer/models/particle-batch-model.ts @@ -112,13 +112,6 @@ export default class ParticleBatchModel extends Model { this._inited = true; } - private _recreateBuffer () { - this._vBuffer = this._createSubMeshData(); - this.getSubModel(0).updateCommandBuffer(); - this._vdataF32 = new Float32Array(this._vBuffer); - this._vdataUint32 = new Uint32Array(this._vBuffer); - } - public _createSubMeshData (): ArrayBuffer { if (this._subMeshData) { this.destroySubMeshData(); @@ -264,6 +257,13 @@ export default class ParticleBatchModel extends Model { this._subMeshData = null; } + private _recreateBuffer () { + this._vBuffer = this._createSubMeshData(); + this.getSubModel(0).updateCommandBuffer(); + this._vdataF32 = new Float32Array(this._vBuffer); + this._vdataUint32 = new Uint32Array(this._vBuffer); + } + private destroySubMeshData () { for (const vb of this._subMeshData!.vertexBuffers) { vb.destroy();