mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
Add GPU particle renderer. (#6462)
* Add GPU particle renderer. * refine code * fix ci * optimize the code * refine code * delete some if judgments * fix strength billboard * modify uniform update * update uniform in editor * fix api and some code specification * fix uniform set
This commit is contained in:
File diff suppressed because one or more lines are too long
@@ -174,6 +174,12 @@ class BuiltinResMgr {
|
||||
defaultParticleMtl.initialize({ effectName: 'builtin-particle' });
|
||||
resources[defaultParticleMtl._uuid] = defaultParticleMtl;
|
||||
|
||||
// default particle gpu material
|
||||
const defaultParticleGPUMtl = new cc.Material();
|
||||
defaultParticleGPUMtl._uuid = 'default-particle-gpu-material';
|
||||
defaultParticleGPUMtl.initialize({ effectName: 'builtin-particle-gpu' });
|
||||
resources[defaultParticleGPUMtl._uuid] = defaultParticleGPUMtl;
|
||||
|
||||
// default particle material
|
||||
const defaultTrailMtl = new cc.Material();
|
||||
defaultTrailMtl._uuid = 'default-trail-material';
|
||||
|
||||
@@ -28,7 +28,7 @@
|
||||
*/
|
||||
|
||||
import { GFXBindingType, GFXType } from '../../gfx/define';
|
||||
import { Color, Mat3, Mat4, Vec2, Vec3, Vec4 } from '../../math';
|
||||
import { Color, Mat3, Mat4, Vec2, Vec3, Vec4, Quat } from '../../math';
|
||||
|
||||
const btMask = 0xf0000000; // 4 bits => 16 binding types
|
||||
const typeMask = 0x0fc00000; // 6 bits => 64 types
|
||||
@@ -43,7 +43,7 @@ export const getBindingFromHandle = (handle: number) => (handle & bindingMask) >
|
||||
export const getOffsetFromHandle = (handle: number) => (handle & offsetMask);
|
||||
export const customizeType = (handle: number, type: GFXType) => (handle & ~typeMask) | ((type << 22) & typeMask);
|
||||
|
||||
export type MaterialProperty = number | Vec2 | Vec3 | Vec4 | Color | Mat3 | Mat4;
|
||||
export type MaterialProperty = number | Vec2 | Vec3 | Vec4 | Color | Mat3 | Mat4 | Quat;
|
||||
|
||||
export const type2reader = {
|
||||
[GFXType.UNKNOWN]: (a: Float32Array, v: any, idx: number = 0) => console.warn('illegal uniform handle'),
|
||||
|
||||
@@ -5,23 +5,35 @@
|
||||
|
||||
import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { pseudoRandom } from '../../core/math';
|
||||
import Particle from '../particle';
|
||||
import { Particle, PARTICLE_MODULE_NAME } from '../particle';
|
||||
import GradientRange from './gradient-range';
|
||||
import { ModuleRandSeed } from '../enum';
|
||||
import { ParticleModuleBase } from '../particle';
|
||||
|
||||
// tslint:disable: max-line-length
|
||||
|
||||
const COLOR_OVERTIME_RAND_OFFSET = 91041;
|
||||
const COLOR_OVERTIME_RAND_OFFSET = ModuleRandSeed.COLOR;
|
||||
|
||||
@ccclass('cc.ColorOvertimeModule')
|
||||
export default class ColorOvertimeModule {
|
||||
|
||||
export default class ColorOvertimeModule extends ParticleModuleBase {
|
||||
@property
|
||||
_enable = false;
|
||||
/**
|
||||
* @zh 是否启用。
|
||||
*/
|
||||
@property({
|
||||
displayOrder: 0,
|
||||
})
|
||||
public enable = false;
|
||||
public get enable () {
|
||||
return this._enable;
|
||||
}
|
||||
|
||||
public set enable (val) {
|
||||
if (this._enable === val) return;
|
||||
this._enable = val;
|
||||
if (!this.target) return;
|
||||
this.target.enableModule(this.name, val, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 颜色随时间变化的参数,各个 key 之间线性差值变化。
|
||||
@@ -31,12 +43,11 @@ export default class ColorOvertimeModule {
|
||||
displayOrder: 1,
|
||||
})
|
||||
public color = new GradientRange();
|
||||
public name = PARTICLE_MODULE_NAME.COLOR;
|
||||
|
||||
public animate (particle: Particle) {
|
||||
if (this.enable) {
|
||||
particle.color.set(particle.startColor);
|
||||
particle.color.multiply(this.color.evaluate(1.0 - particle.remainingLifetime / particle.startLifetime, pseudoRandom(particle.randomSeed + COLOR_OVERTIME_RAND_OFFSET)));
|
||||
}
|
||||
particle.color.set(particle.startColor);
|
||||
particle.color.multiply(this.color.evaluate(1.0 - particle.remainingLifetime / particle.startLifetime, pseudoRandom(particle.randomSeed + COLOR_OVERTIME_RAND_OFFSET)));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,13 +6,15 @@ import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { lerp } from '../../core/math';
|
||||
import { Enum } from '../../core/value-types';
|
||||
import { AnimationCurve } from '../../core/geometry';
|
||||
import { Texture2D } from '../../core';
|
||||
import { PixelFormat, Filter, WrapMode } from '../../core/assets/asset-enum';
|
||||
import { EDITOR } from 'internal:constants';
|
||||
|
||||
const SerializableTable = EDITOR && [
|
||||
[ "mode", "constant", "multiplier" ],
|
||||
[ "mode", "curve", "multiplier" ],
|
||||
[ "mode", "curveMin", "curveMax", "multiplier" ],
|
||||
[ "mode", "constantMin", "constantMax", "multiplier"]
|
||||
[ 'mode', 'constant', 'multiplier' ],
|
||||
[ 'mode', 'curve', 'multiplier' ],
|
||||
[ 'mode', 'curveMin', 'curveMax', 'multiplier' ],
|
||||
[ 'mode', 'constantMin', 'constantMax', 'multiplier']
|
||||
];
|
||||
|
||||
export const Mode = Enum({
|
||||
@@ -119,13 +121,146 @@ export default class CurveRange {
|
||||
}
|
||||
}
|
||||
|
||||
// CCClass.fastDefine('cc.CurveRange', CurveRange, {
|
||||
// mode: Mode.Constant,
|
||||
// constant: 0,
|
||||
// constantMin: 0,
|
||||
// constantMax: 0,
|
||||
// curve: new AnimationCurve(),
|
||||
// curveMin: new AnimationCurve(),
|
||||
// curveMax: new AnimationCurve(),
|
||||
// multiplier: 1
|
||||
// });
|
||||
function evaluateCurve (cr: CurveRange, time: number, index: number) {
|
||||
switch (cr.mode) {
|
||||
case Mode.Constant:
|
||||
return cr.constant;
|
||||
case Mode.Curve:
|
||||
return cr.curve.evaluate(time);
|
||||
case Mode.TwoCurves:
|
||||
return index === 0 ? cr.curveMin.evaluate(time) : cr.curveMax.evaluate(time);
|
||||
case Mode.TwoConstants:
|
||||
return index === 0 ? cr.constantMin : cr.constantMax;
|
||||
default:
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
function evaluateHeight (cr: CurveRange) {
|
||||
switch (cr.mode) {
|
||||
case Mode.TwoConstants:
|
||||
return 2;
|
||||
case Mode.TwoCurves:
|
||||
return 2;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
function packTexture (data, width, height) {
|
||||
const texture = new Texture2D();
|
||||
texture.create(width, height, PixelFormat.RGBA32F);
|
||||
texture.setFilters(Filter.NEAREST, Filter.NEAREST);
|
||||
texture.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE);
|
||||
texture.uploadData(data);
|
||||
|
||||
return texture;
|
||||
}
|
||||
|
||||
export function packCurveRangeZ (samples:number, cr: CurveRange) {
|
||||
const height = evaluateHeight(cr);
|
||||
const data = new Float32Array(samples * height * 4);
|
||||
const interval = 1.0 / (samples - 1);
|
||||
let sum = 0;
|
||||
let average = 0;
|
||||
let offset = 0;
|
||||
|
||||
for (let h = 0; h < height; h++) {
|
||||
sum = 0;
|
||||
for (let j = 0; j < samples; j++) {
|
||||
const value = evaluateCurve(cr, interval * j, h);
|
||||
sum += value;
|
||||
average = sum / (j + 1);
|
||||
data[offset + 2] = average;
|
||||
offset += 4;
|
||||
}
|
||||
}
|
||||
return packTexture(data, samples, height);
|
||||
}
|
||||
export function packCurveRangeN (samples:number, cr: CurveRange) {
|
||||
const height = evaluateHeight(cr);
|
||||
const data = new Float32Array(samples * height * 4);
|
||||
const interval = 1.0 / (samples - 1);
|
||||
let sum = 0;
|
||||
let average = 0;
|
||||
let offset = 0;
|
||||
for (let h = 0; h < height; h++) {
|
||||
sum = 0;
|
||||
for (let j = 0; j < samples; j++) {
|
||||
const value = evaluateCurve(cr, interval * j, h);
|
||||
sum += value;
|
||||
average = sum / (j + 1);
|
||||
data[offset] = average;
|
||||
data[offset + 1] = average;
|
||||
data[offset + 2] = average;
|
||||
offset += 4;
|
||||
}
|
||||
}
|
||||
return packTexture(data, samples, height);
|
||||
}
|
||||
|
||||
export function packCurveRangeXY (samples: number, x: CurveRange, y: CurveRange) {
|
||||
const height = Math.max(evaluateHeight(x), evaluateHeight(y));
|
||||
const data = new Float32Array(samples * height * 4);
|
||||
const curves: CurveRange[] = [x, y];
|
||||
const interval = 1.0 / (samples - 1);
|
||||
|
||||
for (let h = 0; h < height; h++) {
|
||||
for (let i = 0; i < 2; i++) {
|
||||
const cr = curves[i];
|
||||
let sum = 0;
|
||||
let average = 0;
|
||||
for (let j = 0; j < samples; j++) {
|
||||
const value = evaluateCurve(cr, interval * j, h);
|
||||
sum += value;
|
||||
average = sum / (j + 1);
|
||||
data[j * 4 + i] = average;
|
||||
}
|
||||
}
|
||||
}
|
||||
return packTexture(data, samples, height);
|
||||
}
|
||||
|
||||
export function packCurveRangeXYZ (samples: number, x: CurveRange, y: CurveRange, z: CurveRange) {
|
||||
const height = Math.max(evaluateHeight(x), evaluateHeight(y), evaluateHeight(z));
|
||||
const data = new Float32Array(samples * height * 4);
|
||||
const curves: CurveRange[] = [x, y, z];
|
||||
const interval = 1.0 / (samples - 1);
|
||||
|
||||
for (let h = 0; h < height; h++) {
|
||||
for (let i = 0; i < 3; i++) {
|
||||
const cr = curves[i];
|
||||
let sum = 0;
|
||||
let average = 0;
|
||||
for (let j = 0; j < samples; j++) {
|
||||
const value = evaluateCurve(cr, interval * j, h);
|
||||
sum += value;
|
||||
average = sum / (j + 1);
|
||||
data[j * 4 + i] = average;
|
||||
}
|
||||
}
|
||||
}
|
||||
return packTexture(data, samples, height);
|
||||
}
|
||||
|
||||
export function packCurveRangeXYZW (samples: number, x: CurveRange, y: CurveRange, z: CurveRange, w: CurveRange) {
|
||||
const height = Math.max(evaluateHeight(x), evaluateHeight(y), evaluateHeight(z), evaluateHeight(w));
|
||||
const data = new Float32Array(samples * height * 4);
|
||||
const curves: CurveRange[] = [x, y, z, w];
|
||||
const interval = 1.0 / (samples - 1);
|
||||
|
||||
for (let h = 0; h < height; h++) {
|
||||
for (let i = 0; i < 4; i++) {
|
||||
const cr = curves[i];
|
||||
let sum = 0;
|
||||
let average = 0;
|
||||
for (let j = 0; j < samples; j++) {
|
||||
const value = evaluateCurve(cr, interval * j, h);
|
||||
sum += value;
|
||||
average = sum / (j + 1);
|
||||
data[j * 4 + i] = average;
|
||||
}
|
||||
}
|
||||
}
|
||||
return packTexture(data, samples, height);
|
||||
}
|
||||
|
||||
@@ -8,22 +8,34 @@ import { pseudoRandom, Quat, Vec3 } from '../../core/math';
|
||||
import { Space } from '../enum';
|
||||
import { calculateTransform } from '../particle-general-function';
|
||||
import CurveRange from './curve-range';
|
||||
import { ModuleRandSeed } from '../enum';
|
||||
import { ParticleModuleBase, PARTICLE_MODULE_NAME} from '../particle';
|
||||
|
||||
// tslint:disable: max-line-length
|
||||
const FORCE_OVERTIME_RAND_OFFSET = 212165;
|
||||
const FORCE_OVERTIME_RAND_OFFSET = ModuleRandSeed.FORCE;
|
||||
|
||||
const _temp_v3 = new Vec3();
|
||||
|
||||
@ccclass('cc.ForceOvertimeModule')
|
||||
export default class ForceOvertimeModule {
|
||||
|
||||
export default class ForceOvertimeModule extends ParticleModuleBase {
|
||||
@property
|
||||
_enable: Boolean = false;
|
||||
/**
|
||||
* @zh 是否启用。
|
||||
*/
|
||||
@property({
|
||||
displayOrder: 0,
|
||||
})
|
||||
public enable = false;
|
||||
public get enable () {
|
||||
return this._enable;
|
||||
}
|
||||
|
||||
public set enable (val) {
|
||||
if (this._enable === val) return;
|
||||
this._enable = val;
|
||||
if (!this.target) return;
|
||||
this.target.enableModule(this.name, val, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh X 轴方向上的加速度分量。
|
||||
@@ -73,10 +85,13 @@ export default class ForceOvertimeModule {
|
||||
|
||||
private rotation: Quat;
|
||||
private needTransform: boolean;
|
||||
public name = PARTICLE_MODULE_NAME.FORCE;
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
this.rotation = new Quat();
|
||||
this.needTransform = false;
|
||||
this.needUpdate = true;
|
||||
}
|
||||
|
||||
public update (space, worldTransform) {
|
||||
@@ -90,6 +105,7 @@ export default class ForceOvertimeModule {
|
||||
Vec3.transformQuat(force, force, this.rotation);
|
||||
}
|
||||
Vec3.scaleAndAdd(p.velocity, p.velocity, force, dt);
|
||||
Vec3.copy(p.ultimateVelocity, p.velocity);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,24 +8,16 @@ import { Color } from '../../core/math';
|
||||
import { Enum } from '../../core/value-types';
|
||||
import Gradient, { AlphaKey, ColorKey } from './gradient';
|
||||
import { EDITOR } from 'internal:constants';
|
||||
import { Texture2D } from '../../core';
|
||||
import { PixelFormat, Filter, WrapMode } from '../../core/assets/asset-enum';
|
||||
|
||||
// tslint:disable: max-line-length
|
||||
|
||||
const GRADIENT_MODE_FIX = 0;
|
||||
const GRADIENT_MODE_BLEND = 1;
|
||||
|
||||
const GRADIENT_RANGE_MODE_COLOR = 0;
|
||||
const GRADIENT_RANGE_MODE_TWO_COLOR = 1;
|
||||
const GRADIENT_RANGE_MODE_RANDOM_COLOR = 2;
|
||||
const GRADIENT_RANGE_MODE_GRADIENT = 3;
|
||||
const GRADIENT_RANGE_MODE_TWO_GRADIENT = 4;
|
||||
|
||||
const SerializableTable = EDITOR && [
|
||||
[ "_mode", "color" ],
|
||||
[ "_mode", "gradient" ],
|
||||
[ "_mode", "colorMin", "colorMax" ],
|
||||
[ "_mode", "gradientMin", "gradientMax"],
|
||||
[ "_mode", "gradient" ]
|
||||
[ '_mode', 'color' ],
|
||||
[ '_mode', 'gradient' ],
|
||||
[ '_mode', 'colorMin', 'colorMax' ],
|
||||
[ '_mode', 'gradientMin', 'gradientMax'],
|
||||
[ '_mode', 'gradient' ]
|
||||
];
|
||||
|
||||
const Mode = Enum({
|
||||
@@ -147,3 +139,54 @@ export default class GradientRange {
|
||||
// gradientMin: null,
|
||||
// gradientMax: null
|
||||
// });
|
||||
function evaluateGradient (gr: GradientRange, time: number, index: number) {
|
||||
switch (gr.mode) {
|
||||
case Mode.Color:
|
||||
return gr.color;
|
||||
case Mode.TwoColors:
|
||||
return index === 0 ? gr.colorMin : gr.colorMax;
|
||||
case Mode.RandomColor:
|
||||
return gr.gradient.randomColor();
|
||||
case Mode.Gradient:
|
||||
return gr.gradient.evaluate(time);
|
||||
case Mode.TwoGradients:
|
||||
return index === 0 ? gr.gradientMin.evaluate(time) : gr.gradientMax.evaluate(time);
|
||||
default:
|
||||
return gr.color;
|
||||
}
|
||||
}
|
||||
function evaluateHeight (gr: GradientRange) {
|
||||
switch (gr.mode) {
|
||||
case Mode.TwoColors:
|
||||
return 2;
|
||||
case Mode.TwoGradients:
|
||||
return 2;
|
||||
default:
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
export function packGradientRange (samples: number, gr: GradientRange) {
|
||||
const height = evaluateHeight(gr);
|
||||
const data = new Uint8Array(samples * height * 4);
|
||||
const interval = 1.0 / (samples - 1);
|
||||
let offset = 0;
|
||||
|
||||
for (let h = 0; h < height; h++) {
|
||||
for (let j = 0; j < samples; j++) {
|
||||
const color = evaluateGradient(gr, interval * j, h);
|
||||
data[offset] = color.r;
|
||||
data[offset + 1] = color.g;
|
||||
data[offset + 2] = color.b;
|
||||
data[offset + 3] = color.a;
|
||||
offset += 4;
|
||||
}
|
||||
}
|
||||
|
||||
const texture = new Texture2D();
|
||||
texture.create(samples, height, PixelFormat.RGBA8888);
|
||||
texture.setFilters(Filter.LINEAR, Filter.LINEAR);
|
||||
texture.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE);
|
||||
texture.uploadData(data);
|
||||
|
||||
return texture;
|
||||
}
|
||||
@@ -5,27 +5,38 @@
|
||||
|
||||
import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { lerp, pseudoRandom, Vec3, Mat4, Quat } from '../../core/math';
|
||||
import { Space } from '../enum';
|
||||
import Particle from '../particle';
|
||||
import { Space, ModuleRandSeed } from '../enum';
|
||||
import { Particle, ParticleModuleBase, PARTICLE_MODULE_NAME } from '../particle';
|
||||
import CurveRange from './curve-range';
|
||||
import { calculateTransform } from '../particle-general-function';
|
||||
|
||||
// tslint:disable: max-line-length
|
||||
const LIMIT_VELOCITY_RAND_OFFSET = 23541;
|
||||
const LIMIT_VELOCITY_RAND_OFFSET = ModuleRandSeed.LIMIT;
|
||||
|
||||
const _temp_v3 = new Vec3();
|
||||
const _temp_v3_1 = new Vec3();
|
||||
|
||||
@ccclass('cc.LimitVelocityOvertimeModule')
|
||||
export default class LimitVelocityOvertimeModule {
|
||||
export default class LimitVelocityOvertimeModule extends ParticleModuleBase {
|
||||
|
||||
@property
|
||||
_enable: Boolean = false;
|
||||
/**
|
||||
* @zh 是否启用。
|
||||
*/
|
||||
@property({
|
||||
displayOrder: 0,
|
||||
})
|
||||
public enable = false;
|
||||
public get enable () {
|
||||
return this._enable;
|
||||
}
|
||||
|
||||
public set enable (val) {
|
||||
if (this._enable === val) return;
|
||||
this._enable = val;
|
||||
if (!this.target) return;
|
||||
this.target.enableModule(this.name, val, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh X 轴方向上的速度下限。
|
||||
@@ -101,24 +112,24 @@ export default class LimitVelocityOvertimeModule {
|
||||
|
||||
// TODO:functions related to drag are temporarily not supported
|
||||
public drag = null;
|
||||
|
||||
public multiplyDragByParticleSize = false;
|
||||
|
||||
public multiplyDragByParticleVelocity = false;
|
||||
|
||||
public name = PARTICLE_MODULE_NAME.LIMIT;
|
||||
private rotation: Quat;
|
||||
private needTransform: boolean;
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
this.rotation = new Quat();
|
||||
this.needTransform = false;
|
||||
this.needUpdate = true;
|
||||
}
|
||||
|
||||
public update (space: number, worldTransform: Mat4) {
|
||||
this.needTransform = calculateTransform(space, this.space, worldTransform, this.rotation);
|
||||
}
|
||||
|
||||
public animate (p: Particle) {
|
||||
public animate (p: Particle, dt: number) {
|
||||
const normalizedTime = 1 - p.remainingLifetime / p.startLifetime;
|
||||
const dampedVel = _temp_v3;
|
||||
if (this.separateAxes) {
|
||||
|
||||
@@ -5,22 +5,33 @@
|
||||
|
||||
import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { pseudoRandom } from '../../core/math';
|
||||
import Particle from '../particle';
|
||||
import { Particle, ParticleModuleBase, PARTICLE_MODULE_NAME } from '../particle';
|
||||
import CurveRange from './curve-range';
|
||||
import { ModuleRandSeed } from '../enum';
|
||||
|
||||
// tslint:disable: max-line-length
|
||||
const ROTATION_OVERTIME_RAND_OFFSET = 125292;
|
||||
const ROTATION_OVERTIME_RAND_OFFSET = ModuleRandSeed.ROTATION;
|
||||
|
||||
@ccclass('cc.RotationOvertimeModule')
|
||||
export default class RotationOvertimeModule {
|
||||
|
||||
export default class RotationOvertimeModule extends ParticleModuleBase {
|
||||
@property
|
||||
_enable: Boolean = false;
|
||||
/**
|
||||
* @zh 是否启用。
|
||||
*/
|
||||
@property({
|
||||
displayOrder: 0,
|
||||
})
|
||||
public enable = false;
|
||||
public get enable () {
|
||||
return this._enable;
|
||||
}
|
||||
|
||||
public set enable (val) {
|
||||
if (this._enable === val) return;
|
||||
this._enable = val;
|
||||
if (!this.target) return;
|
||||
this.target.enableModule(this.name, val, this);
|
||||
}
|
||||
|
||||
@property
|
||||
private _separateAxes = false;
|
||||
@@ -76,9 +87,7 @@ export default class RotationOvertimeModule {
|
||||
})
|
||||
public z = new CurveRange();
|
||||
|
||||
constructor () {
|
||||
|
||||
}
|
||||
public name = PARTICLE_MODULE_NAME.ROTATION;
|
||||
|
||||
public animate (p: Particle, dt: number) {
|
||||
const normalizedTime = 1 - p.remainingLifetime / p.startLifetime;
|
||||
|
||||
@@ -5,22 +5,33 @@
|
||||
|
||||
import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { pseudoRandom, Vec3 } from '../../core/math';
|
||||
import Particle from '../particle';
|
||||
import { Particle, ParticleModuleBase, PARTICLE_MODULE_NAME } from '../particle';
|
||||
import CurveRange from './curve-range';
|
||||
import { ModuleRandSeed } from '../enum';
|
||||
|
||||
// tslint:disable: max-line-length
|
||||
const SIZE_OVERTIME_RAND_OFFSET = 39825;
|
||||
const SIZE_OVERTIME_RAND_OFFSET = ModuleRandSeed.SIZE;
|
||||
|
||||
@ccclass('cc.SizeOvertimeModule')
|
||||
export default class SizeOvertimeModule {
|
||||
|
||||
export default class SizeOvertimeModule extends ParticleModuleBase {
|
||||
@property
|
||||
_enable: Boolean = false;
|
||||
/**
|
||||
* @zh 是否启用。
|
||||
*/
|
||||
@property({
|
||||
displayOrder: 0,
|
||||
})
|
||||
public enable = false;
|
||||
public get enable () {
|
||||
return this._enable;
|
||||
}
|
||||
|
||||
public set enable (val) {
|
||||
if (this._enable === val) return;
|
||||
this._enable = val;
|
||||
if (!this.target) return;
|
||||
this.target.enableModule(this.name, val, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 决定是否在每个轴上独立控制粒子大小。
|
||||
@@ -71,7 +82,9 @@ export default class SizeOvertimeModule {
|
||||
})
|
||||
public z = new CurveRange();
|
||||
|
||||
public animate (particle: Particle) {
|
||||
public name = PARTICLE_MODULE_NAME.SIZE;
|
||||
|
||||
public animate (particle: Particle, dt: number) {
|
||||
if (!this.separateAxes) {
|
||||
Vec3.multiplyScalar(particle.size, particle.startSize, this.size.evaluate(1 - particle.remainingLifetime / particle.startLifetime, pseudoRandom(particle.randomSeed + SIZE_OVERTIME_RAND_OFFSET))!);
|
||||
} else {
|
||||
|
||||
@@ -6,12 +6,13 @@
|
||||
import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { lerp, pseudoRandom, repeat } from '../../core/math';
|
||||
import { Enum } from '../../core/value-types';
|
||||
import Particle from '../particle';
|
||||
import { Particle, ParticleModuleBase, PARTICLE_MODULE_NAME } from '../particle';
|
||||
import { ParticleSystemComponent } from '../particle-system-component';
|
||||
import CurveRange from './curve-range';
|
||||
import { ModuleRandSeed } from '../enum';
|
||||
|
||||
// tslint:disable: max-line-length
|
||||
const TEXTURE_ANIMATION_RAND_OFFSET = 90794;
|
||||
const TEXTURE_ANIMATION_RAND_OFFSET = ModuleRandSeed.TEXTURE;
|
||||
|
||||
/**
|
||||
* 粒子贴图动画类型。
|
||||
@@ -46,7 +47,7 @@ const Animation = Enum({
|
||||
});
|
||||
|
||||
@ccclass('cc.TextureAnimationModule')
|
||||
export default class TextureAnimationModule {
|
||||
export default class TextureAnimationModule extends ParticleModuleBase {
|
||||
|
||||
@property
|
||||
private _enable = false;
|
||||
@@ -72,10 +73,11 @@ export default class TextureAnimationModule {
|
||||
}
|
||||
|
||||
set enable (val) {
|
||||
if (this._enable === val) return;
|
||||
this._enable = val;
|
||||
if (this.ps) {
|
||||
(this.ps.renderer as any)._updateMaterialParams();
|
||||
}
|
||||
if (!this.target) return;
|
||||
this.target.updateMaterialParams();
|
||||
this.target.enableModule(this.name, val, this);
|
||||
}
|
||||
|
||||
@property({
|
||||
@@ -116,9 +118,7 @@ export default class TextureAnimationModule {
|
||||
set numTilesX (val) {
|
||||
if (this._numTilesX !== val) {
|
||||
this._numTilesX = val;
|
||||
if (this.ps) {
|
||||
(this.ps.renderer as any)._updateMaterialParams();
|
||||
}
|
||||
this.target!.updateMaterialParams();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -136,9 +136,7 @@ export default class TextureAnimationModule {
|
||||
set numTilesY (val) {
|
||||
if (this._numTilesY !== val) {
|
||||
this._numTilesY = val;
|
||||
if (this.ps) {
|
||||
(this.ps.renderer as any)._updateMaterialParams();
|
||||
}
|
||||
this.target!.updateMaterialParams();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,18 +234,13 @@ export default class TextureAnimationModule {
|
||||
tooltip:'从动画贴图中选择特定行以生成动画。\n此选项仅在动画播放方式为 SingleRow 时且禁用 randomRow 时可用'
|
||||
})
|
||||
public rowIndex = 0;
|
||||
|
||||
private ps: ParticleSystemComponent | null = null;
|
||||
|
||||
public onInit (ps: ParticleSystemComponent) {
|
||||
this.ps = ps;
|
||||
}
|
||||
public name = PARTICLE_MODULE_NAME.TEXTURE;
|
||||
|
||||
public init (p: Particle) {
|
||||
p.startRow = Math.floor(Math.random() * this.numTilesY);
|
||||
}
|
||||
|
||||
public animate (p: Particle) {
|
||||
public animate (p: Particle, dt: number) {
|
||||
const normalizedTime = 1 - p.remainingLifetime / p.startLifetime;
|
||||
const startFrame = this.startFrame.evaluate(normalizedTime, pseudoRandom(p.randomSeed + TEXTURE_ANIMATION_RAND_OFFSET))! / (this.numTilesX * this.numTilesY);
|
||||
if (this.animation === Animation.WholeSheet) {
|
||||
@@ -269,19 +262,3 @@ export default class TextureAnimationModule {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// CCClass.fastDefine('cc.TextureAnimationModule', TextureAnimationModule, {
|
||||
// _enable: false,
|
||||
// _mode: Mode.Grid,
|
||||
// numTilesX: 0,
|
||||
// numTilesY: 0,
|
||||
// animation: Animation.WholeSheet,
|
||||
// frameOverTime: new CurveRange(),
|
||||
// startFrame: new CurveRange(),
|
||||
// cycleCount: 0,
|
||||
// _flipU: 0,
|
||||
// _flipV: 0,
|
||||
// _uvChannelMask: -1,
|
||||
// randomRow: false,
|
||||
// rowIndex: 0
|
||||
// });
|
||||
|
||||
@@ -6,27 +6,38 @@
|
||||
import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { Mat4, pseudoRandom, Quat, Vec3 } from '../../core/math';
|
||||
import { Space } from '../enum';
|
||||
import Particle from '../particle';
|
||||
import { Particle, ParticleModuleBase, PARTICLE_MODULE_NAME } from '../particle';
|
||||
import { calculateTransform } from '../particle-general-function';
|
||||
import CurveRange from './curve-range';
|
||||
import { ModuleRandSeed } from '../enum';
|
||||
|
||||
// tslint:disable: max-line-length
|
||||
const VELOCITY_X_OVERTIME_RAND_OFFSET = 197866;
|
||||
const VELOCITY_Y_OVERTIME_RAND_OFFSET = 156497;
|
||||
const VELOCITY_Z_OVERTIME_RAND_OFFSET = 984136;
|
||||
const VELOCITY_X_OVERTIME_RAND_OFFSET = ModuleRandSeed.VELOCITY_X;
|
||||
const VELOCITY_Y_OVERTIME_RAND_OFFSET = ModuleRandSeed.VELOCITY_Y;
|
||||
const VELOCITY_Z_OVERTIME_RAND_OFFSET = ModuleRandSeed.VELOCITY_Z;
|
||||
|
||||
const _temp_v3 = new Vec3();
|
||||
|
||||
@ccclass('cc.VelocityOvertimeModule')
|
||||
export default class VelocityOvertimeModule {
|
||||
|
||||
export default class VelocityOvertimeModule extends ParticleModuleBase {
|
||||
@property
|
||||
_enable: Boolean = false;
|
||||
/**
|
||||
* @zh 是否启用。
|
||||
*/
|
||||
@property({
|
||||
displayOrder: 0,
|
||||
})
|
||||
public enable = false;
|
||||
public get enable () {
|
||||
return this._enable;
|
||||
}
|
||||
|
||||
public set enable (val) {
|
||||
if (this._enable === val) return;
|
||||
this._enable = val;
|
||||
if (!this.target) return;
|
||||
this.target.enableModule(this.name, val, this);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh X 轴方向上的速度分量。
|
||||
@@ -84,18 +95,21 @@ export default class VelocityOvertimeModule {
|
||||
|
||||
private rotation: Quat;
|
||||
private needTransform: boolean;
|
||||
public name = PARTICLE_MODULE_NAME.VELOCITY;
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
this.rotation = new Quat();
|
||||
this.speedModifier.constant = 1;
|
||||
this.needTransform = false;
|
||||
this.needUpdate = true;
|
||||
}
|
||||
|
||||
public update (space: number, worldTransform: Mat4) {
|
||||
this.needTransform = calculateTransform(space, this.space, worldTransform, this.rotation);
|
||||
}
|
||||
|
||||
public animate (p: Particle) {
|
||||
public animate (p: Particle, dt: number) {
|
||||
const normalizedTime = 1 - p.remainingLifetime / p.startLifetime;
|
||||
const vel = Vec3.set(_temp_v3, this.x.evaluate(normalizedTime, pseudoRandom(p.randomSeed ^ VELOCITY_X_OVERTIME_RAND_OFFSET))!, this.y.evaluate(normalizedTime, pseudoRandom(p.randomSeed ^ VELOCITY_Y_OVERTIME_RAND_OFFSET))!, this.z.evaluate(normalizedTime, pseudoRandom(p.randomSeed ^ VELOCITY_Z_OVERTIME_RAND_OFFSET))!);
|
||||
if (this.needTransform) {
|
||||
@@ -105,7 +119,6 @@ export default class VelocityOvertimeModule {
|
||||
Vec3.add(p.ultimateVelocity, p.velocity, p.animatedVelocity);
|
||||
Vec3.multiplyScalar(p.ultimateVelocity, p.ultimateVelocity, this.speedModifier.evaluate(1 - p.remainingLifetime / p.startLifetime, pseudoRandom(p.randomSeed + VELOCITY_X_OVERTIME_RAND_OFFSET))!);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// CCClass.fastDefine('cc.VelocityOvertimeModule', VelocityOvertimeModule, {
|
||||
|
||||
@@ -95,13 +95,21 @@ export default class ShapeModule {
|
||||
this._angle = toRadian(val);
|
||||
}
|
||||
|
||||
@property
|
||||
private _enable = false;
|
||||
/**
|
||||
* @zh 是否启用。
|
||||
*/
|
||||
@property({
|
||||
displayOrder: 0,
|
||||
})
|
||||
public enable = false;
|
||||
public get enable () {
|
||||
return this._enable;
|
||||
}
|
||||
|
||||
public set enable (val) {
|
||||
this._enable = val;
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 粒子发射器类型 [[ShapeType]]。
|
||||
|
||||
@@ -166,3 +166,15 @@ export const TextureMode = Enum({
|
||||
*/
|
||||
Repeat: 1,
|
||||
});
|
||||
|
||||
export const ModuleRandSeed = {
|
||||
LIMIT: 23541,
|
||||
SIZE: 39825,
|
||||
TEXTURE: 90794,
|
||||
COLOR: 91041,
|
||||
FORCE: 212165,
|
||||
ROTATION: 125292,
|
||||
VELOCITY_X: 197866,
|
||||
VELOCITY_Y: 156497,
|
||||
VELOCITY_Z: 984136
|
||||
}
|
||||
|
||||
@@ -28,13 +28,21 @@
|
||||
*/
|
||||
|
||||
import { Material } from '../../core/assets/material';
|
||||
import { Mesh, RenderingSubMesh } from '../../core/assets/mesh';
|
||||
import { RenderingSubMesh, Mesh } from '../../core/assets/mesh';
|
||||
import { GFX_DRAW_INFO_SIZE, GFXBuffer, IGFXIndirectBuffer } from '../../core/gfx/buffer';
|
||||
import { GFXAttributeName, GFXBufferUsageBit, GFXFormatInfos,
|
||||
GFXMemoryUsageBit, GFXPrimitiveMode, GFXStatus } from '../../core/gfx/define';
|
||||
import { IGFXAttribute } from '../../core/gfx/input-assembler';
|
||||
import { Color } from '../../core/math/color';
|
||||
import { Model, ModelType } from '../../core/renderer/scene/model';
|
||||
import { Particle } from '../particle';
|
||||
|
||||
const _uvs = [
|
||||
0, 0, // bottom-left
|
||||
1, 0, // bottom-right
|
||||
0, 1, // top-left
|
||||
1, 1, // top-right
|
||||
];
|
||||
|
||||
export default class ParticleBatchModel extends Model {
|
||||
|
||||
@@ -51,6 +59,8 @@ export default class ParticleBatchModel extends Model {
|
||||
private _mesh: Mesh | null;
|
||||
private _vertCount: number = 0;
|
||||
private _indexCount: number = 0;
|
||||
private _startTimeOffset: number = 0;
|
||||
private _lifeTimeOffset: number = 0;
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
@@ -169,7 +179,7 @@ export default class ParticleBatchModel extends Model {
|
||||
}
|
||||
}
|
||||
|
||||
const indexBuffer = this._device.createBuffer({
|
||||
const indexBuffer: GFXBuffer = this._device.createBuffer({
|
||||
usage: GFXBufferUsageBit.INDEX | GFXBufferUsageBit.TRANSFER_DST,
|
||||
memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE,
|
||||
size: this._capacity * this._indexCount * Uint16Array.BYTES_PER_ELEMENT,
|
||||
@@ -193,10 +203,15 @@ export default class ParticleBatchModel extends Model {
|
||||
this._subMeshData = new RenderingSubMesh([vertexBuffer], this._vertAttrs!, GFXPrimitiveMode.TRIANGLE_LIST);
|
||||
this._subMeshData.indexBuffer = indexBuffer;
|
||||
this._subMeshData.indirectBuffer = this._iaInfoBuffer;
|
||||
this.setSubModelMesh(0, this._subMeshData);
|
||||
this.setSubModelMesh(0, this._subMeshData!);
|
||||
return vBuffer;
|
||||
}
|
||||
|
||||
public setSubModelMaterial (idx: number, mat: Material | null) {
|
||||
this.initLocalBindings(mat);
|
||||
super.setSubModelMaterial(idx, mat);
|
||||
}
|
||||
|
||||
public addParticleVertexData (index: number, pvdata: any[]) {
|
||||
if (!this._mesh) {
|
||||
let offset: number = index * this._vertAttrsFloatCount;
|
||||
@@ -239,6 +254,73 @@ export default class ParticleBatchModel extends Model {
|
||||
}
|
||||
}
|
||||
|
||||
public addGPUParticleVertexData (p: Particle, num: number, time:number) {
|
||||
let offset = num * this._vertAttrsFloatCount * this._vertCount;
|
||||
for (let i = 0; i < this._vertCount; i++) {
|
||||
let idx = offset;
|
||||
this._vdataF32![idx++] = p.position.x;
|
||||
this._vdataF32![idx++] = p.position.y;
|
||||
this._vdataF32![idx++] = p.position.z;
|
||||
this._vdataF32![idx++] = time;
|
||||
|
||||
this._vdataF32![idx++] = p.startSize.x;
|
||||
this._vdataF32![idx++] = p.startSize.y;
|
||||
this._vdataF32![idx++] = p.startSize.z;
|
||||
this._vdataF32![idx++] = _uvs[2 * i];
|
||||
|
||||
this._vdataF32![idx++] = p.rotation.x;
|
||||
this._vdataF32![idx++] = p.rotation.y;
|
||||
this._vdataF32![idx++] = p.rotation.z;
|
||||
this._vdataF32![idx++] = _uvs[2 * i + 1];
|
||||
|
||||
this._vdataF32![idx++] = p.startColor.r / 255.0;
|
||||
this._vdataF32![idx++] = p.startColor.g / 255.0;
|
||||
this._vdataF32![idx++] = p.startColor.b / 255.0;
|
||||
this._vdataF32![idx++] = p.startColor.a / 255.0;
|
||||
|
||||
this._vdataF32![idx++] = p.velocity.x;
|
||||
this._vdataF32![idx++] = p.velocity.y;
|
||||
this._vdataF32![idx++] = p.velocity.z;
|
||||
this._vdataF32![idx++] = p.startLifetime;
|
||||
|
||||
this._vdataF32![idx++] = p.randomSeed;
|
||||
|
||||
offset += this._vertAttrsFloatCount;
|
||||
}
|
||||
}
|
||||
|
||||
public updateGPUParticles (num: number, time: number) {
|
||||
const pSize = this._vertAttrsFloatCount * this._vertCount;
|
||||
let pBaseIndex = 0;
|
||||
let startTime = 0;
|
||||
let lifeTime = 0;
|
||||
let lastBaseIndex = 0;
|
||||
for (let i = 0; i < num; ++i) {
|
||||
pBaseIndex = i * pSize;
|
||||
startTime = this._vdataF32![pBaseIndex + this._startTimeOffset];
|
||||
lifeTime = this._vdataF32![pBaseIndex + this._lifeTimeOffset];
|
||||
if (time - startTime > lifeTime) {
|
||||
lastBaseIndex = -- num * pSize;
|
||||
this._vdataF32!.copyWithin(pBaseIndex, lastBaseIndex, lastBaseIndex + pSize);
|
||||
i--;
|
||||
}
|
||||
}
|
||||
|
||||
return num;
|
||||
}
|
||||
|
||||
public constructAttributeIndex () {
|
||||
if (!this._vertAttrs) {
|
||||
return;
|
||||
}
|
||||
let vIdx = this._vertAttrs!.findIndex((val) => val.name === 'a_position_starttime');
|
||||
let vOffset = (this._vertAttrs![vIdx] as any).offset;
|
||||
this._startTimeOffset = vOffset / 4 + 3;
|
||||
vIdx = this._vertAttrs!.findIndex((val) => val.name === 'a_dir_life');
|
||||
vOffset = (this._vertAttrs![vIdx] as any).offset;
|
||||
this._lifeTimeOffset = vOffset / 4 + 3;
|
||||
}
|
||||
|
||||
public updateIA (count: number) {
|
||||
const ia = this.getSubModel(0).inputAssembler!;
|
||||
ia.vertexBuffers[0].update(this._vdataF32!);
|
||||
|
||||
@@ -110,6 +110,8 @@ export function randomSortArray (arr: any[]) {
|
||||
|
||||
export function randomSign () {
|
||||
let sgn = randomRange(-1, 1);
|
||||
sgn === 0 ? sgn++ : sgn;
|
||||
if (sgn === 0) {
|
||||
sgn++;
|
||||
}
|
||||
return sign(sgn);
|
||||
}
|
||||
|
||||
@@ -25,22 +25,13 @@ import Burst from './burst';
|
||||
import ShapeModule from './emitter/shape-module';
|
||||
import { Space } from './enum';
|
||||
import { particleEmitZAxis } from './particle-general-function';
|
||||
import ParticleSystemRenderer from './renderer/particle-system-renderer';
|
||||
import ParticleSystemRenderer from './renderer/particle-system-renderer-data';
|
||||
import TrailModule from './renderer/trail';
|
||||
import { indexOf } from '../core/utils/array';
|
||||
import { IParticleSystemRenderer } from './renderer/particle-system-renderer-base';
|
||||
import { PARTICLE_MODULE_PROPERTY } from './particle';
|
||||
|
||||
const _world_mat = new Mat4();
|
||||
const _module_props = [
|
||||
"colorOverLifetimeModule",
|
||||
"shapeModule",
|
||||
"sizeOvertimeModule",
|
||||
"velocityOvertimeModule",
|
||||
"forceOvertimeModule",
|
||||
"limitVelocityOvertimeModule",
|
||||
"rotationOvertimeModule",
|
||||
"textureAnimationModule",
|
||||
"trailModule"
|
||||
];
|
||||
const _world_rol = new Quat();
|
||||
|
||||
@ccclass('cc.ParticleSystemComponent')
|
||||
@help('i18n:cc.ParticleSystemComponent')
|
||||
@@ -437,7 +428,7 @@ export class ParticleSystemComponent extends RenderableComponent {
|
||||
displayOrder: 27,
|
||||
tooltip:'是否剔除非 enable 的模块数据',
|
||||
})
|
||||
public isCulling: Boolean = true;
|
||||
public enableCulling: Boolean = false;
|
||||
|
||||
/**
|
||||
* @ignore
|
||||
@@ -467,6 +458,8 @@ export class ParticleSystemComponent extends RenderableComponent {
|
||||
@property
|
||||
private _simulationSpace = Space.Local;
|
||||
|
||||
public processor: IParticleSystemRenderer | null = null;
|
||||
|
||||
constructor () {
|
||||
super();
|
||||
|
||||
@@ -495,22 +488,21 @@ export class ParticleSystemComponent extends RenderableComponent {
|
||||
|
||||
public onLoad () {
|
||||
// HACK, TODO
|
||||
this.renderer!.onInit(this);
|
||||
this.renderer.onInit(this);
|
||||
this.shapeModule.onInit(this);
|
||||
this.trailModule.onInit(this);
|
||||
this.textureAnimationModule.onInit(this);
|
||||
|
||||
this.bindModule();
|
||||
this._resetPosition();
|
||||
|
||||
// this._system.add(this);
|
||||
}
|
||||
|
||||
public _onMaterialModified (index: number, material: Material) {
|
||||
this.renderer._onMaterialModified(index, material);
|
||||
this.processor!.onMaterialModified(index, material);
|
||||
}
|
||||
|
||||
public _onRebuildPSO (index: number, material: Material) {
|
||||
this.renderer._onRebuildPSO(index, material);
|
||||
this.processor!.onRebuildPSO(index, material);
|
||||
}
|
||||
|
||||
public _collectModels (): Model[] {
|
||||
@@ -522,20 +514,30 @@ export class ParticleSystemComponent extends RenderableComponent {
|
||||
return this._models;
|
||||
}
|
||||
|
||||
protected _attachToScene() {
|
||||
this.renderer._attachToScene();
|
||||
protected _attachToScene () {
|
||||
this.processor!.attachToScene();
|
||||
if (this.trailModule.enable) {
|
||||
this.trailModule._attachToScene();
|
||||
}
|
||||
}
|
||||
|
||||
protected _detachFromScene() {
|
||||
this.renderer._detachFromScene();
|
||||
protected _detachFromScene () {
|
||||
this.processor!.detachFromScene();
|
||||
if (this.trailModule.enable) {
|
||||
this.trailModule._detachFromScene();
|
||||
}
|
||||
}
|
||||
|
||||
public bindModule () {
|
||||
this.colorOverLifetimeModule.bindTarget(this.processor!);
|
||||
this.sizeOvertimeModule.bindTarget(this.processor!);
|
||||
this.rotationOvertimeModule.bindTarget(this.processor!);
|
||||
this.forceOvertimeModule.bindTarget(this.processor!);
|
||||
this.limitVelocityOvertimeModule.bindTarget(this.processor!);
|
||||
this.velocityOvertimeModule.bindTarget(this.processor!);
|
||||
this.textureAnimationModule.bindTarget(this.processor!);
|
||||
}
|
||||
|
||||
// TODO: fastforward current particle system by simulating particles over given period of time, then pause it.
|
||||
// simulate(time, withChildren, restart, fixedTimeStep) {
|
||||
|
||||
@@ -605,7 +607,7 @@ export class ParticleSystemComponent extends RenderableComponent {
|
||||
*/
|
||||
public clear () {
|
||||
if (this.enabledInHierarchy) {
|
||||
this.renderer!.clear();
|
||||
this.processor!.clear();
|
||||
this.trailModule.clear();
|
||||
}
|
||||
}
|
||||
@@ -614,7 +616,7 @@ export class ParticleSystemComponent extends RenderableComponent {
|
||||
* @zh 获取当前粒子数量
|
||||
*/
|
||||
public getParticleCount () {
|
||||
return this.renderer!.getParticleCount();
|
||||
return this.processor!.getParticleCount();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -630,7 +632,7 @@ export class ParticleSystemComponent extends RenderableComponent {
|
||||
|
||||
protected onDestroy () {
|
||||
// this._system.remove(this);
|
||||
this.renderer.onDestroy();
|
||||
this.processor!.onDestroy();
|
||||
this.trailModule.destroy();
|
||||
}
|
||||
|
||||
@@ -638,15 +640,13 @@ export class ParticleSystemComponent extends RenderableComponent {
|
||||
if (this.playOnAwake) {
|
||||
this.play();
|
||||
}
|
||||
this.renderer.onEnable();
|
||||
this.processor!.onEnable();
|
||||
this.trailModule.onEnable();
|
||||
}
|
||||
|
||||
protected onDisable () {
|
||||
this.renderer!.onDisable();
|
||||
this.processor!.onDisable();
|
||||
this.trailModule.onDisable();
|
||||
}
|
||||
|
||||
protected update (dt) {
|
||||
const scaledDeltaTime = dt * this.simulationSpeed;
|
||||
if (this._isPlaying) {
|
||||
@@ -656,12 +656,12 @@ export class ParticleSystemComponent extends RenderableComponent {
|
||||
this._emit(scaledDeltaTime);
|
||||
|
||||
// simulation, update particles.
|
||||
if (this.renderer!._updateParticles(scaledDeltaTime) === 0 && !this._isEmitting) {
|
||||
if (this.processor!.updateParticles(scaledDeltaTime) === 0 && !this._isEmitting) {
|
||||
this.stop();
|
||||
}
|
||||
|
||||
// update render data
|
||||
this.renderer!._updateRenderData();
|
||||
this.processor!.updateRenderData();
|
||||
|
||||
// update trail
|
||||
if (this.trailModule.enable) {
|
||||
@@ -679,9 +679,15 @@ export class ParticleSystemComponent extends RenderableComponent {
|
||||
}
|
||||
|
||||
private emit (count, dt) {
|
||||
const delta = this._time / this.duration;
|
||||
|
||||
if (this._simulationSpace === Space.World) {
|
||||
this.node.getWorldMatrix(_world_mat);
|
||||
this.node.getWorldRotation(_world_rol);
|
||||
}
|
||||
|
||||
for (let i = 0; i < count; ++i) {
|
||||
const particle = this.renderer!._getFreeParticle();
|
||||
const particle = this.processor!.getFreeParticle();
|
||||
if (particle === null) {
|
||||
return;
|
||||
}
|
||||
@@ -699,54 +705,45 @@ export class ParticleSystemComponent extends RenderableComponent {
|
||||
this.textureAnimationModule.init(particle);
|
||||
}
|
||||
|
||||
Vec3.multiplyScalar(particle.velocity, particle.velocity, this.startSpeed.evaluate(this._time / this.duration, rand)!);
|
||||
Vec3.multiplyScalar(particle.velocity, particle.velocity, this.startSpeed.evaluate(delta, rand)!);
|
||||
|
||||
switch (this._simulationSpace) {
|
||||
case Space.Local:
|
||||
break;
|
||||
case Space.World:
|
||||
this.node.getWorldMatrix(_world_mat);
|
||||
Vec3.transformMat4(particle.position, particle.position, _world_mat);
|
||||
const worldRot = new Quat();
|
||||
this.node.getWorldRotation(worldRot);
|
||||
Vec3.transformQuat(particle.velocity, particle.velocity, worldRot);
|
||||
break;
|
||||
case Space.Custom:
|
||||
// TODO:
|
||||
break;
|
||||
if (this._simulationSpace === Space.World) {
|
||||
Vec3.transformMat4(particle.position, particle.position, _world_mat);
|
||||
Vec3.transformQuat(particle.velocity, particle.velocity, _world_rol);
|
||||
}
|
||||
|
||||
Vec3.copy(particle.ultimateVelocity, particle.velocity);
|
||||
// apply startRotation.
|
||||
if (this.startRotation3D) {
|
||||
Vec3.set(particle.rotation, this.startRotationX.evaluate(this._time / this.duration, rand)!,
|
||||
this.startRotationY.evaluate(this._time / this.duration, rand)!,
|
||||
this.startRotationZ.evaluate(this._time / this.duration, rand)!);
|
||||
Vec3.set(particle.rotation, this.startRotationX.evaluate(delta, rand)!,
|
||||
this.startRotationY.evaluate(delta, rand)!,
|
||||
this.startRotationZ.evaluate(delta, rand)!);
|
||||
} else {
|
||||
Vec3.set(particle.rotation, 0, 0, this.startRotationZ.evaluate(this._time / this.duration, rand)!);
|
||||
Vec3.set(particle.rotation, 0, 0, this.startRotationZ.evaluate(delta, rand)!);
|
||||
}
|
||||
|
||||
// apply startSize.
|
||||
if (this.startSize3D) {
|
||||
Vec3.set(particle.startSize, this.startSizeX.evaluate(this._time / this.duration, rand)!,
|
||||
this.startSizeY.evaluate(this._time / this.duration, rand)!,
|
||||
this.startSizeZ.evaluate(this._time / this.duration, rand)!);
|
||||
Vec3.set(particle.startSize, this.startSizeX.evaluate(delta, rand)!,
|
||||
this.startSizeY.evaluate(delta, rand)!,
|
||||
this.startSizeZ.evaluate(delta, rand)!);
|
||||
} else {
|
||||
Vec3.set(particle.startSize, this.startSizeX.evaluate(this._time / this.duration, rand)!, 1, 1);
|
||||
Vec3.set(particle.startSize, this.startSizeX.evaluate(delta, rand)!, 1, 1);
|
||||
particle.startSize.y = particle.startSize.x;
|
||||
}
|
||||
Vec3.copy(particle.size, particle.startSize);
|
||||
|
||||
// apply startColor.
|
||||
particle.startColor.set(this.startColor.evaluate(this._time / this.duration, rand));
|
||||
particle.startColor.set(this.startColor.evaluate(delta, rand));
|
||||
particle.color.set(particle.startColor);
|
||||
|
||||
// apply startLifetime.
|
||||
particle.startLifetime = this.startLifetime.evaluate(this._time / this.duration, rand)! + dt;
|
||||
particle.startLifetime = this.startLifetime.evaluate(delta, rand)! + dt;
|
||||
particle.remainingLifetime = particle.startLifetime;
|
||||
|
||||
particle.randomSeed = randomRangeInt(0, 233280);
|
||||
|
||||
this.renderer!._setNewParticle(particle);
|
||||
this.processor!.setNewParticle(particle);
|
||||
|
||||
} // end of particles forLoop.
|
||||
}
|
||||
@@ -760,7 +757,7 @@ export class ParticleSystemComponent extends RenderableComponent {
|
||||
for (let i = 0; i < cnt; ++i) {
|
||||
this._time += dt;
|
||||
this._emit(dt);
|
||||
this.renderer!._updateParticles(dt);
|
||||
this.processor!.updateParticles(dt);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -849,6 +846,6 @@ export class ParticleSystemComponent extends RenderableComponent {
|
||||
}
|
||||
|
||||
public _onBeforeSerialize (props) {
|
||||
return this.isCulling ? props.filter(p => !_module_props.includes(p) || this[p].enable) : props;
|
||||
return this.enableCulling ? props.filter(p => !PARTICLE_MODULE_PROPERTY.includes(p) || this[p].enable) : props;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,10 +2,11 @@
|
||||
* @hidden
|
||||
*/
|
||||
|
||||
import { Color, Vec3 } from '../core/math';
|
||||
import { Color, Vec3, Mat4 } from '../core/math';
|
||||
import { ParticleSystemComponent } from './particle-system-component';
|
||||
import { IParticleSystemRenderer } from './renderer/particle-system-renderer-base';
|
||||
|
||||
export default class Particle {
|
||||
export class Particle {
|
||||
public particleSystem: ParticleSystemComponent;
|
||||
public position: Vec3;
|
||||
public velocity: Vec3;
|
||||
@@ -48,3 +49,59 @@ export default class Particle {
|
||||
this.startRow = 0;
|
||||
}
|
||||
}
|
||||
|
||||
export const PARTICLE_MODULE_NAME = {
|
||||
COLOR: 'colorModule',
|
||||
FORCE: 'forceModule',
|
||||
LIMIT: 'limitModule',
|
||||
ROTATION: 'rotationModule',
|
||||
SIZE: 'sizeModule',
|
||||
VELOCITY: 'velocityModule',
|
||||
TEXTURE: 'textureModule'
|
||||
};
|
||||
|
||||
export const PARTICLE_MODULE_ORDER = [
|
||||
'sizeModule',
|
||||
'colorModule',
|
||||
'forceModule',
|
||||
'velocityModule',
|
||||
'limitModule',
|
||||
'rotationModule',
|
||||
'textureModule'
|
||||
];
|
||||
|
||||
export const PARTICLE_MODULE_PROPERTY = [
|
||||
'colorOverLifetimeModule',
|
||||
'shapeModule',
|
||||
'sizeOvertimeModule',
|
||||
'velocityOvertimeModule',
|
||||
'forceOvertimeModule',
|
||||
'limitVelocityOvertimeModule',
|
||||
'rotationOvertimeModule',
|
||||
'textureAnimationModule',
|
||||
'trailModule'
|
||||
];
|
||||
|
||||
export interface IParticleModule {
|
||||
target: IParticleSystemRenderer | null;
|
||||
needUpdate: Boolean;
|
||||
needAnimate: Boolean;
|
||||
name: string;
|
||||
bindTarget (target: any): void;
|
||||
update (space: number, trans: Mat4): void;
|
||||
animate (p: Particle, dt?: number): void;
|
||||
}
|
||||
|
||||
export abstract class ParticleModuleBase implements IParticleModule{
|
||||
public target:IParticleSystemRenderer | null = null;
|
||||
public needUpdate: Boolean = false;
|
||||
public needAnimate: Boolean = true;
|
||||
|
||||
public bindTarget (target: IParticleSystemRenderer) {
|
||||
this.target = target;
|
||||
}
|
||||
|
||||
public update (space: number, trans: Mat4) {}
|
||||
public abstract name: string;
|
||||
public abstract animate (p: Particle, dt?: number): void;
|
||||
}
|
||||
|
||||
@@ -0,0 +1,109 @@
|
||||
import { Component, IGFXAttribute } from '../../core';
|
||||
import ParticleBatchModel from '../models/particle-batch-model';
|
||||
import ParticleSystemRenderer from './particle-system-renderer-data';
|
||||
import { Material } from '../../core/assets';
|
||||
import { Particle, IParticleModule } from '../particle';
|
||||
import { RenderMode } from '../enum';
|
||||
|
||||
export interface IParticleSystemRenderer {
|
||||
onInit (ps: Component): void;
|
||||
onEnable (): void;
|
||||
onDisable (): void;
|
||||
onDestroy (): void;
|
||||
clear (): void;
|
||||
attachToScene (): void;
|
||||
detachFromScene (): void;
|
||||
updateMaterialParams (): void;
|
||||
setVertexAttributes (): void;
|
||||
updateRenderMode (): void;
|
||||
onMaterialModified (index: number, material: Material): void;
|
||||
onRebuildPSO (index: number, material: Material) : void;
|
||||
getParticleCount (): number;
|
||||
getFreeParticle (): Particle | null;
|
||||
setNewParticle (p: Particle): void;
|
||||
updateParticles (dt: number): number;
|
||||
updateRenderData (): void;
|
||||
enableModule (name: string, val: Boolean, pm: IParticleModule): void;
|
||||
updateTrailMaterial (): void;
|
||||
getDefaultTrailMaterial (): any;
|
||||
}
|
||||
|
||||
export abstract class ParticleSystemRendererBase implements IParticleSystemRenderer {
|
||||
protected _particleSystem: any = null;
|
||||
protected _model: ParticleBatchModel | null = null;
|
||||
protected _renderInfo: ParticleSystemRenderer | null = null;
|
||||
protected _vertAttrs: IGFXAttribute[] = [];
|
||||
|
||||
constructor (info: ParticleSystemRenderer) {
|
||||
this._renderInfo = info;
|
||||
}
|
||||
|
||||
public onInit (ps: Component) {
|
||||
this._particleSystem = ps;
|
||||
}
|
||||
|
||||
public onEnable () {
|
||||
if (!this._particleSystem) {
|
||||
return;
|
||||
}
|
||||
this.attachToScene();
|
||||
this._model!.initialize(this._particleSystem.node);
|
||||
this._model!.enabled = this._particleSystem.enabledInHierarchy;
|
||||
}
|
||||
|
||||
public onDisable () {
|
||||
this.detachFromScene();
|
||||
}
|
||||
|
||||
public onDestroy () {
|
||||
if (this._model) {
|
||||
cc.director.root.destroyModel(this._model);
|
||||
this._model = null;
|
||||
}
|
||||
}
|
||||
|
||||
public attachToScene () {
|
||||
if (this._model) {
|
||||
if (this._model.scene) {
|
||||
this.detachFromScene();
|
||||
}
|
||||
this._particleSystem!._getRenderScene().addModel(this._model);
|
||||
}
|
||||
}
|
||||
|
||||
public detachFromScene () {
|
||||
if (this._model && this._model.scene) {
|
||||
this._model.scene.removeModel(this._model);
|
||||
}
|
||||
}
|
||||
|
||||
public setVertexAttributes () {
|
||||
if (this._model) {
|
||||
this._model.setVertexAttributes(this._renderInfo!.renderMode === RenderMode.Mesh ? this._renderInfo!.mesh : null, this._vertAttrs);
|
||||
}
|
||||
}
|
||||
|
||||
protected _updateModel () {
|
||||
if (!this._model) {
|
||||
this._model = cc.director.root.createModel(ParticleBatchModel);
|
||||
this._model!.setCapacity(this._particleSystem.capacity);
|
||||
this._model!.visFlags = this._particleSystem.visibility;
|
||||
}
|
||||
|
||||
this._model!.setVertexAttributes(this._renderInfo!.renderMode === RenderMode.Mesh ? this._renderInfo!.mesh : null, this._vertAttrs);
|
||||
}
|
||||
|
||||
public updateTrailMaterial () {}
|
||||
public getDefaultTrailMaterial () { return null; }
|
||||
public abstract getParticleCount () : number;
|
||||
public abstract getFreeParticle (): Particle | null;
|
||||
public abstract onMaterialModified (index: number, material: Material) : void;
|
||||
public abstract onRebuildPSO (index: number, material: Material) : void;
|
||||
public abstract updateRenderMode () : void;
|
||||
public abstract updateMaterialParams () : void;
|
||||
public abstract clear () : void;
|
||||
public abstract setNewParticle (p: Particle): void;
|
||||
public abstract updateParticles (dt: number): number;
|
||||
public abstract updateRenderData (): void;
|
||||
public abstract enableModule (name: string, val: Boolean, pm: IParticleModule): void;
|
||||
}
|
||||
@@ -0,0 +1,452 @@
|
||||
import { builtinResMgr } from '../../core/3d/builtin';
|
||||
import { Material } from '../../core/assets';
|
||||
import { GFXAttributeName, GFXFormat } from '../../core/gfx/define';
|
||||
import { Mat4, Vec2, Vec3, Vec4, pseudoRandom } from '../../core/math';
|
||||
import { RecyclePool } from '../../core/memop';
|
||||
import { MaterialInstance, IMaterialInstanceInfo } from '../../core/renderer/core/material-instance';
|
||||
import { IDefineMap } from '../../core/renderer/core/pass-utils';
|
||||
import { RenderMode, Space } from '../enum';
|
||||
import { Particle, IParticleModule, PARTICLE_MODULE_ORDER } from '../particle';
|
||||
import { ParticleSystemRendererBase } from './particle-system-renderer-base';
|
||||
import { Component } from '../../core';
|
||||
|
||||
const _tempAttribUV = new Vec3();
|
||||
const _tempWorldTrans = new Mat4();
|
||||
|
||||
const _anim_module = [
|
||||
'colorOverLifetimeModule',
|
||||
'sizeOvertimeModule',
|
||||
'velocityOvertimeModule',
|
||||
'forceOvertimeModule',
|
||||
'limitVelocityOvertimeModule',
|
||||
'rotationOvertimeModule',
|
||||
'textureAnimationModule'
|
||||
];
|
||||
|
||||
const _uvs = [
|
||||
0, 0, // bottom-left
|
||||
1, 0, // bottom-right
|
||||
0, 1, // top-left
|
||||
1, 1, // top-right
|
||||
];
|
||||
|
||||
const CC_USE_WORLD_SPACE = 'CC_USE_WORLD_SPACE';
|
||||
|
||||
const CC_RENDER_MODE = 'CC_RENDER_MODE';
|
||||
const RENDER_MODE_BILLBOARD = 0;
|
||||
const RENDER_MODE_STRETCHED_BILLBOARD = 1;
|
||||
const RENDER_MODE_HORIZONTAL_BILLBOARD = 2;
|
||||
const RENDER_MODE_VERTICAL_BILLBOARD = 3;
|
||||
const RENDER_MODE_MESH = 4;
|
||||
|
||||
const _vertex_attrs = [
|
||||
{ name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // position
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, // uv,frame idx
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RGB32F }, // size
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD2, format: GFXFormat.RGB32F }, // rotation
|
||||
{ name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true }, // color
|
||||
];
|
||||
|
||||
const _vertex_attrs_stretch = [
|
||||
{ name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // position
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, // uv,frame idx
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RGB32F }, // size
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD2, format: GFXFormat.RGB32F }, // rotation
|
||||
{ name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true }, // color
|
||||
{ name: GFXAttributeName.ATTR_COLOR1, format: GFXFormat.RGB32F }, // particle velocity
|
||||
];
|
||||
|
||||
const _vertex_attrs_mesh = [
|
||||
{ name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // particle position
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, // uv,frame idx
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RGB32F }, // size
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD2, format: GFXFormat.RGB32F }, // rotation
|
||||
{ name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true }, // particle color
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD3, format: GFXFormat.RGB32F }, // mesh position
|
||||
{ name: GFXAttributeName.ATTR_NORMAL, format: GFXFormat.RGB32F }, // mesh normal
|
||||
{ name: GFXAttributeName.ATTR_COLOR1, format: GFXFormat.RGBA8, isNormalized: true }, // mesh color
|
||||
];
|
||||
|
||||
const _matInsInfo: IMaterialInstanceInfo = {
|
||||
parent: null!,
|
||||
owner: null!,
|
||||
subModelIdx: 0,
|
||||
};
|
||||
|
||||
export default class ParticleSystemRendererCPU extends ParticleSystemRendererBase {
|
||||
private _defines: IDefineMap;
|
||||
private _trailDefines: IDefineMap;
|
||||
private _frameTile_velLenScale: Vec4;
|
||||
private _defaultMat: Material | null = null;
|
||||
private _node_scale: Vec4;
|
||||
private _attrs: any[];
|
||||
private _particles: RecyclePool | null = null;
|
||||
private _defaultTrailMat: Material | null = null;
|
||||
private _updateList: Map<string, IParticleModule> = new Map<string, IParticleModule>();
|
||||
private _animateList: Map<string, IParticleModule> = new Map<string, IParticleModule>();
|
||||
private _runAnimateList: IParticleModule[] = new Array<IParticleModule>();
|
||||
private _fillDataFunc: any = null;
|
||||
private _uScaleHandle: number = 0;
|
||||
private _uLenHandle: number = 0;
|
||||
|
||||
constructor (info: any) {
|
||||
super(info);
|
||||
|
||||
this._model = null;
|
||||
|
||||
this._frameTile_velLenScale = new Vec4(1, 1, 0, 0);
|
||||
this._node_scale = new Vec4();
|
||||
this._attrs = new Array(5);
|
||||
this._defines = {
|
||||
CC_USE_WORLD_SPACE: true,
|
||||
CC_USE_BILLBOARD: true,
|
||||
CC_USE_STRETCHED_BILLBOARD: false,
|
||||
CC_USE_HORIZONTAL_BILLBOARD: false,
|
||||
CC_USE_VERTICAL_BILLBOARD: false,
|
||||
};
|
||||
this._trailDefines = {
|
||||
CC_USE_WORLD_SPACE: true,
|
||||
// CC_DRAW_WIRE_FRAME: true, // <wireframe debug>
|
||||
};
|
||||
}
|
||||
|
||||
public onInit (ps: Component) {
|
||||
super.onInit(ps);
|
||||
|
||||
this._particles = new RecyclePool(() => {
|
||||
return new Particle(this);
|
||||
}, 16);
|
||||
this._setVertexAttrib();
|
||||
this._updateModel();
|
||||
this._setFillFunc();
|
||||
this._initModuleList();
|
||||
this.updateMaterialParams();
|
||||
this.updateTrailMaterial();
|
||||
}
|
||||
|
||||
public clear () {
|
||||
this._particles!.reset();
|
||||
this._particleSystem!.trailModule.clear();
|
||||
this.updateRenderData();
|
||||
}
|
||||
|
||||
public updateRenderMode () {
|
||||
this._setVertexAttrib();
|
||||
this._updateModel();
|
||||
this._setFillFunc();
|
||||
this.updateMaterialParams();
|
||||
}
|
||||
|
||||
public getFreeParticle (): Particle | null {
|
||||
if (this._particles!.length >= this._particleSystem!.capacity) {
|
||||
return null;
|
||||
}
|
||||
return this._particles!.add();
|
||||
}
|
||||
|
||||
public getDefaultTrailMaterial (): any {
|
||||
return this._defaultTrailMat;
|
||||
}
|
||||
|
||||
public setNewParticle (p: Particle) {
|
||||
}
|
||||
|
||||
private _initModuleList () {
|
||||
_anim_module.forEach(val => {
|
||||
const pm = this._particleSystem[val];
|
||||
if (pm.enable) {
|
||||
if (pm.needUpdate) {
|
||||
this._updateList[pm.name] = pm;
|
||||
}
|
||||
|
||||
if (pm.needAnimate) {
|
||||
this._animateList[pm.name] = pm;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// reorder
|
||||
this._runAnimateList.length = 0;
|
||||
for (let i = 0, len = PARTICLE_MODULE_ORDER.length; i < len; i++) {
|
||||
const p = this._animateList[PARTICLE_MODULE_ORDER[i]];
|
||||
if (p) {
|
||||
this._runAnimateList.push(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public enableModule (name: string, val: Boolean, pm: IParticleModule) {
|
||||
if (val) {
|
||||
if (pm.needUpdate) {
|
||||
this._updateList[pm.name] = pm;
|
||||
}
|
||||
|
||||
if (pm.needAnimate) {
|
||||
this._animateList[pm.name] = pm;
|
||||
}
|
||||
} else {
|
||||
delete this._animateList[name];
|
||||
delete this._updateList[name];
|
||||
}
|
||||
// reorder
|
||||
this._runAnimateList.length = 0;
|
||||
for (let i = 0, len = PARTICLE_MODULE_ORDER.length; i < len; i++) {
|
||||
const p = this._animateList[PARTICLE_MODULE_ORDER[i]];
|
||||
if (p) {
|
||||
this._runAnimateList.push(p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public updateParticles (dt: number) {
|
||||
const ps = this._particleSystem;
|
||||
if (!ps) {
|
||||
return this._particles!.length;
|
||||
}
|
||||
ps.node.getWorldMatrix(_tempWorldTrans);
|
||||
switch (ps.scaleSpace) {
|
||||
case Space.Local:
|
||||
ps.node.getScale(this._node_scale);
|
||||
break;
|
||||
case Space.World:
|
||||
ps.node.getWorldScale(this._node_scale);
|
||||
break;
|
||||
}
|
||||
const mat: Material | null = ps.getMaterialInstance(0) || this._defaultMat;
|
||||
const pass = mat!.passes[0];
|
||||
pass.setUniform(this._uScaleHandle, this._node_scale);
|
||||
|
||||
this._updateList.forEach((value: IParticleModule, key: string)=>{
|
||||
value.update(ps._simulationSpace, _tempWorldTrans);
|
||||
});
|
||||
|
||||
if (ps.trailModule.enable) {
|
||||
ps.trailModule.update();
|
||||
}
|
||||
|
||||
for (let i = 0; i < this._particles!.length; ++i) {
|
||||
const p = this._particles!.data[i];
|
||||
p.remainingLifetime -= dt;
|
||||
Vec3.set(p.animatedVelocity, 0, 0, 0);
|
||||
|
||||
if (p.remainingLifetime < 0.0) {
|
||||
if (ps.trailModule.enable) {
|
||||
ps.trailModule.removeParticle(p);
|
||||
}
|
||||
this._particles!.removeAt(i);
|
||||
--i;
|
||||
continue;
|
||||
}
|
||||
|
||||
// apply gravity.
|
||||
p.velocity.y -= ps.gravityModifier.evaluate(1 - p.remainingLifetime / p.startLifetime, pseudoRandom(p.randomSeed))! * 9.8 * dt;
|
||||
|
||||
Vec3.copy(p.ultimateVelocity, p.velocity);
|
||||
|
||||
this._runAnimateList.forEach(value =>{
|
||||
value.animate(p, dt);
|
||||
});
|
||||
|
||||
Vec3.scaleAndAdd(p.position, p.position, p.ultimateVelocity, dt); // apply velocity.
|
||||
if (ps.trailModule.enable) {
|
||||
ps.trailModule.animate(p, dt);
|
||||
}
|
||||
}
|
||||
return this._particles!.length;
|
||||
}
|
||||
|
||||
// internal function
|
||||
public updateRenderData () {
|
||||
// update vertex buffer
|
||||
let idx = 0;
|
||||
for (let i = 0; i < this._particles!.length; ++i) {
|
||||
const p = this._particles!.data[i];
|
||||
let fi = 0;
|
||||
if (this._particleSystem!.textureAnimationModule.enable) {
|
||||
fi = p.frameIndex;
|
||||
}
|
||||
idx = i * 4;
|
||||
this._fillDataFunc(p, idx, fi);
|
||||
}
|
||||
// because we use index buffer, per particle index count = 6.
|
||||
this._model!.updateIA(this._particles!.length);
|
||||
}
|
||||
|
||||
public getParticleCount (): number {
|
||||
return this._particles!.length;
|
||||
}
|
||||
|
||||
public onMaterialModified (index: number, material: Material) {
|
||||
if (index === 0) {
|
||||
this._updateModel();
|
||||
this.updateMaterialParams();
|
||||
} else {
|
||||
this.updateTrailMaterial();
|
||||
}
|
||||
}
|
||||
|
||||
public onRebuildPSO (index: number, material: Material) {
|
||||
if (this._model && index === 0) {
|
||||
this._model.setSubModelMaterial(0, material);
|
||||
}
|
||||
if (this._particleSystem!.trailModule._trailModel && index === 1) {
|
||||
this._particleSystem!.trailModule._trailModel.setSubModelMaterial(0, material);
|
||||
}
|
||||
}
|
||||
|
||||
private _setFillFunc () {
|
||||
if (this._renderInfo!.renderMode === RenderMode.Mesh) {
|
||||
this._fillDataFunc = this._fillMeshData;
|
||||
} else if (this._renderInfo!.renderMode === RenderMode.StrecthedBillboard) {
|
||||
this._fillDataFunc = this._fillStrecthedData;
|
||||
} else {
|
||||
this._fillDataFunc = this._fillNormalData;
|
||||
}
|
||||
}
|
||||
|
||||
private _fillMeshData (p: Particle, idx: number, fi: number) {
|
||||
const i = idx / 4;
|
||||
let attrNum = 0;
|
||||
this._attrs[attrNum++] = p.position;
|
||||
_tempAttribUV.z = fi;
|
||||
this._attrs[attrNum++] = _tempAttribUV;
|
||||
this._attrs[attrNum++] = p.size;
|
||||
this._attrs[attrNum++] = p.rotation;
|
||||
this._attrs[attrNum++] = p.color._val;
|
||||
this._model!.addParticleVertexData(i, this._attrs);
|
||||
}
|
||||
|
||||
private _fillStrecthedData (p: Particle, idx: number, fi: number) {
|
||||
let attrNum = 0;
|
||||
for (let j = 0; j < 4; ++j) { // four verts per particle.
|
||||
attrNum = 0;
|
||||
this._attrs[attrNum++] = p.position;
|
||||
_tempAttribUV.x = _uvs[2 * j];
|
||||
_tempAttribUV.y = _uvs[2 * j + 1];
|
||||
_tempAttribUV.z = fi;
|
||||
this._attrs[attrNum++] = _tempAttribUV;
|
||||
this._attrs[attrNum++] = p.size;
|
||||
this._attrs[attrNum++] = p.rotation;
|
||||
this._attrs[attrNum++] = p.color._val;
|
||||
this._attrs[attrNum++] = p.ultimateVelocity;
|
||||
this._attrs[attrNum++] = p.ultimateVelocity;
|
||||
this._model!.addParticleVertexData(idx++, this._attrs);
|
||||
}
|
||||
}
|
||||
|
||||
private _fillNormalData (p: Particle, idx: number, fi: number) {
|
||||
let attrNum = 0;
|
||||
for (let j = 0; j < 4; ++j) { // four verts per particle.
|
||||
attrNum = 0;
|
||||
this._attrs[attrNum++] = p.position;
|
||||
_tempAttribUV.x = _uvs[2 * j];
|
||||
_tempAttribUV.y = _uvs[2 * j + 1];
|
||||
_tempAttribUV.z = fi;
|
||||
this._attrs[attrNum++] = _tempAttribUV;
|
||||
this._attrs[attrNum++] = p.size;
|
||||
this._attrs[attrNum++] = p.rotation;
|
||||
this._attrs[attrNum++] = p.color._val;
|
||||
this._attrs[attrNum++] = null;
|
||||
this._model!.addParticleVertexData(idx++, this._attrs);
|
||||
}
|
||||
}
|
||||
|
||||
private _setVertexAttrib () {
|
||||
switch (this._renderInfo!.renderMode) {
|
||||
case RenderMode.StrecthedBillboard:
|
||||
this._vertAttrs = _vertex_attrs_stretch.slice();
|
||||
break;
|
||||
case RenderMode.Mesh:
|
||||
this._vertAttrs = _vertex_attrs_mesh.slice();
|
||||
break;
|
||||
default:
|
||||
this._vertAttrs = _vertex_attrs.slice();
|
||||
}
|
||||
}
|
||||
|
||||
public updateMaterialParams () {
|
||||
if (!this._particleSystem) {
|
||||
return;
|
||||
}
|
||||
|
||||
const ps = this._particleSystem;
|
||||
const shareMaterial = ps.sharedMaterial;
|
||||
if (shareMaterial != null) {
|
||||
const effectName = shareMaterial._effectAsset._name;
|
||||
// reset material
|
||||
if (effectName.indexOf('particle') === -1 || effectName.indexOf('particle-gpu') !== -1) {
|
||||
ps.setMaterial(null, 0);
|
||||
}
|
||||
}
|
||||
|
||||
if (ps.sharedMaterial == null && this._defaultMat == null) {
|
||||
_matInsInfo.parent = builtinResMgr.get<Material>('default-particle-material');
|
||||
_matInsInfo.owner = this._particleSystem;
|
||||
_matInsInfo.subModelIdx = 0;
|
||||
this._defaultMat = new MaterialInstance(_matInsInfo);
|
||||
}
|
||||
const mat: Material = ps.getMaterialInstance(0) || this._defaultMat;
|
||||
if (ps._simulationSpace === Space.World) {
|
||||
this._defines[CC_USE_WORLD_SPACE] = true;
|
||||
} else {
|
||||
this._defines[CC_USE_WORLD_SPACE] = false;
|
||||
}
|
||||
|
||||
const pass = mat.passes[0];
|
||||
this._uScaleHandle = pass.getHandle('scale')!;
|
||||
this._uLenHandle = pass.getHandle('frameTile_velLenScale')!;
|
||||
|
||||
const renderMode = this._renderInfo!.renderMode;
|
||||
const vlenScale = this._frameTile_velLenScale;
|
||||
if (renderMode === RenderMode.Billboard) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_BILLBOARD;
|
||||
} else if (renderMode === RenderMode.StrecthedBillboard) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_STRETCHED_BILLBOARD;
|
||||
vlenScale.z = this._renderInfo!.velocityScale;
|
||||
vlenScale.w = this._renderInfo!.lengthScale;
|
||||
} else if (renderMode === RenderMode.HorizontalBillboard) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_HORIZONTAL_BILLBOARD;
|
||||
} else if (renderMode === RenderMode.VerticalBillboard) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_VERTICAL_BILLBOARD;
|
||||
} else if (renderMode === RenderMode.Mesh) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_MESH;
|
||||
} else {
|
||||
console.warn(`particle system renderMode ${renderMode} not support.`);
|
||||
}
|
||||
|
||||
if (ps.textureAnimationModule.enable) {
|
||||
Vec2.set(vlenScale, ps.textureAnimationModule.numTilesX, ps.textureAnimationModule.numTilesY);
|
||||
pass.setUniform(this._uLenHandle, vlenScale);
|
||||
} else {
|
||||
pass.setUniform(this._uLenHandle, vlenScale);
|
||||
}
|
||||
mat.recompileShaders(this._defines);
|
||||
if (this._model) {
|
||||
this._model.setSubModelMaterial(0, mat);
|
||||
}
|
||||
}
|
||||
|
||||
public updateTrailMaterial () {
|
||||
if (!this._particleSystem) {
|
||||
return;
|
||||
}
|
||||
const ps = this._particleSystem;
|
||||
if (ps.trailModule.enable) {
|
||||
if (ps.simulationSpace === Space.World || ps.trailModule.space === Space.World) {
|
||||
this._trailDefines[CC_USE_WORLD_SPACE] = true;
|
||||
} else {
|
||||
this._trailDefines[CC_USE_WORLD_SPACE] = false;
|
||||
}
|
||||
let mat = ps.getMaterialInstance(1);
|
||||
if (mat === null && this._defaultTrailMat === null) {
|
||||
_matInsInfo.parent = builtinResMgr.get<Material>('default-trail-material');
|
||||
_matInsInfo.owner = this._particleSystem;
|
||||
_matInsInfo.subModelIdx = 1;
|
||||
this._defaultTrailMat = new MaterialInstance(_matInsInfo);
|
||||
}
|
||||
mat = mat || this._defaultTrailMat;
|
||||
mat!.recompileShaders(this._trailDefines);
|
||||
ps.trailModule._updateMaterial();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
import { Material, Mesh } from '../../core/assets';
|
||||
import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { RenderMode} from '../enum';
|
||||
import ParticleSystemRendererCPU from './particle-system-renderer-cpu';
|
||||
import ParticleSystemRendererGPU from './particle-system-renderer-gpu';
|
||||
@ccclass('cc.ParticleSystemRenderer')
|
||||
export default class ParticleSystemRenderer {
|
||||
|
||||
/**
|
||||
* @zh 设定粒子生成模式。
|
||||
*/
|
||||
@property({
|
||||
type: RenderMode,
|
||||
displayOrder: 0,
|
||||
tooltip: '设定粒子生成模式',
|
||||
})
|
||||
public get renderMode () {
|
||||
return this._renderMode;
|
||||
}
|
||||
|
||||
public set renderMode (val) {
|
||||
if (this._renderMode === val) {
|
||||
return;
|
||||
}
|
||||
this._renderMode = val;
|
||||
this._particleSystem.processor.updateRenderMode();
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按速度大小进行拉伸。
|
||||
*/
|
||||
@property({
|
||||
displayOrder: 1,
|
||||
tooltip: '在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按速度大小进行拉伸',
|
||||
})
|
||||
public get velocityScale () {
|
||||
return this._velocityScale;
|
||||
}
|
||||
|
||||
public set velocityScale (val) {
|
||||
this._velocityScale = val;
|
||||
this._particleSystem.processor.updateMaterialParams();
|
||||
// this._updateModel();
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按粒子大小进行拉伸。
|
||||
*/
|
||||
@property({
|
||||
displayOrder: 2,
|
||||
tooltip: '在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按粒子大小进行拉伸',
|
||||
})
|
||||
public get lengthScale () {
|
||||
return this._lengthScale;
|
||||
}
|
||||
|
||||
public set lengthScale (val) {
|
||||
this._lengthScale = val;
|
||||
this._particleSystem.processor.updateMaterialParams();
|
||||
// this._updateModel();
|
||||
}
|
||||
|
||||
@property({
|
||||
type: RenderMode,
|
||||
displayOrder: 3,
|
||||
})
|
||||
private _renderMode = RenderMode.Billboard;
|
||||
|
||||
@property({
|
||||
displayOrder: 4,
|
||||
})
|
||||
private _velocityScale = 1;
|
||||
|
||||
@property({
|
||||
displayOrder: 5,
|
||||
})
|
||||
private _lengthScale = 1;
|
||||
|
||||
@property({
|
||||
displayOrder: 6,
|
||||
})
|
||||
private _mesh: Mesh | null = null;
|
||||
|
||||
/**
|
||||
* @zh 粒子发射的模型。
|
||||
*/
|
||||
@property({
|
||||
type: Mesh,
|
||||
displayOrder: 7,
|
||||
tooltip: '粒子发射的模型',
|
||||
})
|
||||
public get mesh () {
|
||||
return this._mesh;
|
||||
}
|
||||
|
||||
public set mesh (val) {
|
||||
this._mesh = val;
|
||||
this._particleSystem.processor.setVertexAttributes();
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 粒子使用的材质。
|
||||
*/
|
||||
@property({
|
||||
type: Material,
|
||||
displayOrder: 8,
|
||||
tooltip: '粒子使用的材质',
|
||||
})
|
||||
public get particleMaterial () {
|
||||
if (!this._particleSystem) {
|
||||
return null;
|
||||
}
|
||||
return this._particleSystem.getMaterial(0);
|
||||
}
|
||||
|
||||
public set particleMaterial (val) {
|
||||
this._particleSystem.setMaterial(val, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 拖尾使用的材质。
|
||||
*/
|
||||
@property({
|
||||
type: Material,
|
||||
displayOrder: 9,
|
||||
tooltip: '拖尾使用的材质',
|
||||
})
|
||||
public get trailMaterial () {
|
||||
if (!this._particleSystem) {
|
||||
return null;
|
||||
}
|
||||
return this._particleSystem.getMaterial(1)!;
|
||||
}
|
||||
|
||||
public set trailMaterial (val) {
|
||||
this._particleSystem.setMaterial(val, 1);
|
||||
}
|
||||
|
||||
@property
|
||||
private _useGPU: Boolean = false;
|
||||
|
||||
@property({
|
||||
type: Boolean,
|
||||
displayOrder: 10,
|
||||
tooltip:'是否启用GPU粒子',
|
||||
})
|
||||
public get useGPU () {
|
||||
return this._useGPU;
|
||||
}
|
||||
|
||||
public set useGPU (val) {
|
||||
if (this._useGPU === val) {
|
||||
return;
|
||||
}
|
||||
|
||||
this._useGPU = val;
|
||||
this._switchProcessor();
|
||||
}
|
||||
|
||||
private _particleSystem: any = null;
|
||||
|
||||
onInit (ps: any) {
|
||||
this._particleSystem = ps;
|
||||
this._particleSystem.processor = this._useGPU ? new ParticleSystemRendererGPU(this) : new ParticleSystemRendererCPU(this);
|
||||
this._particleSystem.processor.onInit(ps);
|
||||
}
|
||||
|
||||
private _switchProcessor () {
|
||||
if (this._particleSystem.processor) {
|
||||
this._particleSystem.processor.detachFromScene();
|
||||
this._particleSystem.processor.clear();
|
||||
this._particleSystem.processor = null;
|
||||
}
|
||||
this._particleSystem.processor = this._useGPU ? new ParticleSystemRendererGPU(this) : new ParticleSystemRendererCPU(this);
|
||||
this._particleSystem.processor.onInit(this._particleSystem);
|
||||
this._particleSystem.processor.onEnable();
|
||||
this._particleSystem.bindModule();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,402 @@
|
||||
import { builtinResMgr } from '../../core/3d/builtin';
|
||||
import { Material } from '../../core/assets';
|
||||
import { Texture2D } from '../../core';
|
||||
import { Component } from '../../core/components';
|
||||
import { GFXAttributeName, GFXFormat } from '../../core/gfx/define';
|
||||
import { IGFXAttribute } from '../../core/gfx/input-assembler';
|
||||
import { Mat4, Vec2, Vec4, Quat} from '../../core/math';
|
||||
import { MaterialInstance, IMaterialInstanceInfo } from '../../core/renderer/core/material-instance';
|
||||
import { IDefineMap } from '../../core/renderer/core/pass-utils';
|
||||
import { RenderMode, Space } from '../enum';
|
||||
import { Particle, IParticleModule } from '../particle';
|
||||
import { packGradientRange } from '../animator/gradient-range';
|
||||
import { Pass } from '../../core/renderer';
|
||||
import { packCurveRangeXYZ, packCurveRangeZ, packCurveRangeXYZW, packCurveRangeN, packCurveRangeXY } from '../animator/curve-range';
|
||||
import { ParticleSystemRendererBase } from './particle-system-renderer-base';
|
||||
import { EDITOR } from 'internal:constants';
|
||||
|
||||
const _tempWorldTrans = new Mat4();
|
||||
const _tempVec4 = new Vec4();
|
||||
const _world_rot = new Quat();
|
||||
|
||||
const _sample_num = 32;
|
||||
const _sample_interval = 1.0 / _sample_num;
|
||||
|
||||
const CC_USE_WORLD_SPACE = 'CC_USE_WORLD_SPACE';
|
||||
|
||||
const CC_RENDER_MODE = 'CC_RENDER_MODE';
|
||||
const RENDER_MODE_BILLBOARD = 0;
|
||||
const RENDER_MODE_STRETCHED_BILLBOARD = 1;
|
||||
const RENDER_MODE_HORIZONTAL_BILLBOARD = 2;
|
||||
const RENDER_MODE_VERTICAL_BILLBOARD = 3;
|
||||
const RENDER_MODE_MESH = 4;
|
||||
|
||||
const COLOR_OVER_TIME_MODULE_ENABLE = 'COLOR_OVER_TIME_MODULE_ENABLE';
|
||||
const ROTATION_OVER_TIME_MODULE_ENABLE = 'ROTATION_OVER_TIME_MODULE_ENABLE';
|
||||
const SIZE_OVER_TIME_MODULE_ENABLE = 'SIZE_OVER_TIME_MODULE_ENABLE';
|
||||
const VELOCITY_OVER_TIME_MODULE_ENABLE = 'VELOCITY_OVER_TIME_MODULE_ENABLE';
|
||||
const FORCE_OVER_TIME_MODULE_ENABLE = 'FORCE_OVER_TIME_MODULE_ENABLE';
|
||||
const TEXTURE_ANIMATION_MODULE_ENABLE = 'TEXTURE_ANIMATION_MODULE_ENABLE';
|
||||
|
||||
const _vert_attr_name = {
|
||||
POSITION_STARTTIME: 'a_position_starttime',
|
||||
VERT_SIZE_UV: 'a_size_uv',
|
||||
VERT_ROTATION_UV: 'a_rotation_uv',
|
||||
COLOR: 'a_color',
|
||||
DIR_LIFE: 'a_dir_life',
|
||||
RANDOM_SEED: 'a_rndSeed'
|
||||
};
|
||||
|
||||
const _gpu_vert_attr = [
|
||||
{ name: _vert_attr_name.POSITION_STARTTIME, format: GFXFormat.RGBA32F},
|
||||
{ name: _vert_attr_name.VERT_SIZE_UV, format: GFXFormat.RGBA32F},
|
||||
{ name: _vert_attr_name.VERT_ROTATION_UV, format: GFXFormat.RGBA32F},
|
||||
{ name: _vert_attr_name.COLOR, format: GFXFormat.RGBA32F},
|
||||
{ name: _vert_attr_name.DIR_LIFE, format: GFXFormat.RGBA32F},
|
||||
{ name: _vert_attr_name.RANDOM_SEED, format: GFXFormat.R32F}
|
||||
];
|
||||
|
||||
const _gpu_vert_attr_mesh = [
|
||||
{ name: _vert_attr_name.POSITION_STARTTIME, format: GFXFormat.RGBA32F},
|
||||
{ name: _vert_attr_name.VERT_SIZE_UV, format: GFXFormat.RGBA32F},
|
||||
{ name: _vert_attr_name.VERT_ROTATION_UV, format: GFXFormat.RGBA32F},
|
||||
{ name: _vert_attr_name.COLOR, format: GFXFormat.RGBA32F},
|
||||
{ name: _vert_attr_name.DIR_LIFE, format: GFXFormat.RGBA32F},
|
||||
{ name: _vert_attr_name.RANDOM_SEED, format: GFXFormat.R32F},
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, // uv,frame idx
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD3, format: GFXFormat.RGB32F }, // mesh position
|
||||
{ name: GFXAttributeName.ATTR_NORMAL, format: GFXFormat.RGB32F }, // mesh normal
|
||||
{ name: GFXAttributeName.ATTR_COLOR1, format: GFXFormat.RGBA8, isNormalized: true }, // mesh color
|
||||
];
|
||||
|
||||
const _matInsInfo: IMaterialInstanceInfo = {
|
||||
parent: null!,
|
||||
owner: null!,
|
||||
subModelIdx: 0,
|
||||
};
|
||||
|
||||
export default class ParticleSystemRendererGPU extends ParticleSystemRendererBase {
|
||||
private _defines: IDefineMap;
|
||||
private _frameTile_velLenScale: Vec4;
|
||||
private _node_scale: Vec4;
|
||||
protected _vertAttrs: IGFXAttribute[] = [];
|
||||
protected _defaultMat: Material | null = null;
|
||||
private _particleNum: number = 0;
|
||||
private _tempParticle: any = null;
|
||||
private _colorTexture: Texture2D | null = null;
|
||||
private _forceTexture: Texture2D | null = null;
|
||||
private _velocityTexture: Texture2D | null = null;
|
||||
private _rotationTexture: Texture2D | null = null;
|
||||
private _sizeTexture: Texture2D | null = null;
|
||||
private _animTexture: Texture2D | null = null;
|
||||
private _uTimeHandle: number = 0;
|
||||
private _uRotHandle: number = 0;
|
||||
|
||||
constructor (info: any) {
|
||||
super(info);
|
||||
|
||||
this._frameTile_velLenScale = new Vec4(1, 1, 0, 0);
|
||||
this._node_scale = new Vec4();
|
||||
this._defines = {
|
||||
CC_USE_WORLD_SPACE: true,
|
||||
CC_USE_BILLBOARD: true,
|
||||
CC_USE_STRETCHED_BILLBOARD: false,
|
||||
CC_USE_HORIZONTAL_BILLBOARD: false,
|
||||
CC_USE_VERTICAL_BILLBOARD: false,
|
||||
COLOR_OVER_TIME_MODULE_ENABLE: false,
|
||||
};
|
||||
|
||||
this._tempParticle = new Particle(null);
|
||||
this._particleNum = 0;
|
||||
}
|
||||
|
||||
public onInit (ps: Component) {
|
||||
super.onInit(ps);
|
||||
this._setVertexAttrib();
|
||||
this._updateModel();
|
||||
this._model!.constructAttributeIndex();
|
||||
this.updateMaterialParams();
|
||||
}
|
||||
|
||||
public updateRenderMode () {
|
||||
this._setVertexAttrib();
|
||||
this._updateModel();
|
||||
this.updateMaterialParams();
|
||||
}
|
||||
|
||||
public clear () {
|
||||
this._particleNum = 0;
|
||||
this.updateRenderData();
|
||||
}
|
||||
|
||||
public onDestroy () {
|
||||
super.onDestroy();
|
||||
this._forceTexture && this._forceTexture.destroy();
|
||||
this._velocityTexture && this._velocityTexture.destroy();
|
||||
this._colorTexture && this._colorTexture.destroy();
|
||||
this._sizeTexture && this._sizeTexture.destroy();
|
||||
this._rotationTexture && this._rotationTexture.destroy();
|
||||
this._animTexture && this._animTexture.destroy();
|
||||
}
|
||||
|
||||
public enableModule (name: string, val: Boolean, pm: IParticleModule) {
|
||||
const mat: Material | null = this._particleSystem.getMaterialInstance(0) || this._defaultMat;
|
||||
if (!mat) {
|
||||
return;
|
||||
}
|
||||
this.initShaderUniform(mat);
|
||||
mat.recompileShaders(this._defines);
|
||||
if (this._model) {
|
||||
this._model.setSubModelMaterial(0, mat);
|
||||
}
|
||||
}
|
||||
|
||||
public getFreeParticle (): Particle | null {
|
||||
if (this._particleNum >= this._particleSystem._capacity) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return this._tempParticle;
|
||||
}
|
||||
|
||||
public setNewParticle (p: Particle) {
|
||||
this._model!.addGPUParticleVertexData(p, this._particleNum, this._particleSystem._time);
|
||||
this._particleNum++;
|
||||
}
|
||||
|
||||
public updateParticles (dt: number) {
|
||||
if (EDITOR) {
|
||||
const mat: Material | null = this._particleSystem.getMaterialInstance(0) || this._defaultMat;
|
||||
|
||||
this._particleSystem.node.getWorldMatrix(_tempWorldTrans);
|
||||
switch (this._particleSystem.scaleSpace) {
|
||||
case Space.Local:
|
||||
this._particleSystem.node.getScale(this._node_scale);
|
||||
break;
|
||||
case Space.World:
|
||||
this._particleSystem.node.getWorldScale(this._node_scale);
|
||||
break;
|
||||
}
|
||||
|
||||
this.initShaderUniform(mat!);
|
||||
}
|
||||
this._particleNum = this._model!.updateGPUParticles(this._particleNum, this._particleSystem._time);
|
||||
this.updateShaderUniform(dt);
|
||||
|
||||
return this._particleNum;
|
||||
}
|
||||
|
||||
// internal function
|
||||
public updateRenderData () {
|
||||
// update vertex buffer
|
||||
this._model!.updateIA(this._particleNum);
|
||||
}
|
||||
|
||||
public updateShaderUniform (dt: number) {
|
||||
const mat: Material | null = this._particleSystem.getMaterialInstance(0) || this._defaultMat;
|
||||
if (!mat) {
|
||||
return;
|
||||
}
|
||||
|
||||
const pass = mat.passes[0];
|
||||
_tempVec4.x = this._particleSystem._time;
|
||||
_tempVec4.y = dt;
|
||||
pass.setUniform(this._uTimeHandle, _tempVec4);
|
||||
|
||||
this._particleSystem.node.getWorldRotation(_world_rot);
|
||||
pass.setUniform(this._uRotHandle, _world_rot);
|
||||
}
|
||||
|
||||
public initShaderUniform (mat: Material) {
|
||||
const pass = mat.passes[0];
|
||||
|
||||
this._uTimeHandle = pass.getHandle('u_timeDelta')!;
|
||||
this._uRotHandle = pass.getHandle('u_worldRot')!;
|
||||
|
||||
pass.setUniform(pass.getHandle('scale')!, this._node_scale);
|
||||
pass.setUniform(pass.getHandle('frameTile_velLenScale')!, this._frameTile_velLenScale);
|
||||
|
||||
// force
|
||||
const forceModule = this._particleSystem.forceOvertimeModule;
|
||||
this._defines[FORCE_OVER_TIME_MODULE_ENABLE] = forceModule.enable;
|
||||
if (forceModule.enable) {
|
||||
this._forceTexture && this._forceTexture.destroy();
|
||||
this._forceTexture = packCurveRangeXYZ(_sample_num, forceModule.x, forceModule.y, forceModule.z);
|
||||
const handle = pass.getHandle('force_over_time_tex0');
|
||||
const binding = Pass.getBindingFromHandle(handle!);
|
||||
pass.bindTextureView(binding, this._forceTexture.getGFXTextureView()!);
|
||||
const spaceHandle = pass.getHandle('u_force_space');
|
||||
pass.setUniform(spaceHandle!, forceModule.space);
|
||||
const modeHandle = pass.getHandle('u_force_mode');
|
||||
pass.setUniform(modeHandle!, this._forceTexture.height);
|
||||
}
|
||||
|
||||
// velocity
|
||||
const velocityModule = this._particleSystem.velocityOvertimeModule;
|
||||
this._defines[VELOCITY_OVER_TIME_MODULE_ENABLE] = velocityModule.enable;
|
||||
if (velocityModule.enable) {
|
||||
this._velocityTexture && this._velocityTexture.destroy();
|
||||
this._velocityTexture = packCurveRangeXYZW(_sample_num, velocityModule.x, velocityModule.y, velocityModule.z, velocityModule.speedModifier);
|
||||
const handle = pass.getHandle('velocity_over_time_tex0');
|
||||
const binding = Pass.getBindingFromHandle(handle!);
|
||||
pass.bindTextureView(binding, this._velocityTexture.getGFXTextureView()!);
|
||||
const spaceHandle = pass.getHandle('u_velocity_space');
|
||||
pass.setUniform(spaceHandle!, velocityModule.space);
|
||||
const modeHandle = pass.getHandle('u_velocity_mode');
|
||||
pass.setUniform(modeHandle!, this._velocityTexture.height);
|
||||
}
|
||||
|
||||
// color module
|
||||
const colorModule = this._particleSystem.colorOverLifetimeModule;
|
||||
this._defines[COLOR_OVER_TIME_MODULE_ENABLE] = colorModule.enable;
|
||||
if (colorModule.enable) {
|
||||
this._colorTexture && this._colorTexture.destroy();
|
||||
this._colorTexture = packGradientRange(_sample_num, colorModule.color);
|
||||
const handle = pass.getHandle('color_over_time_tex0');
|
||||
const binding = Pass.getBindingFromHandle(handle!);
|
||||
pass.bindTextureView(binding, this._colorTexture.getGFXTextureView()!);
|
||||
const modeHandle = pass.getHandle('u_color_mode');
|
||||
pass.setUniform(modeHandle!, this._colorTexture.height);
|
||||
}
|
||||
|
||||
// rotation module
|
||||
const roationModule = this._particleSystem.rotationOvertimeModule;
|
||||
this._defines[ROTATION_OVER_TIME_MODULE_ENABLE] = roationModule.enable;
|
||||
if (roationModule.enable) {
|
||||
this._rotationTexture && this._rotationTexture.destroy();
|
||||
if (roationModule.separateAxes) {
|
||||
this._rotationTexture = packCurveRangeXYZ(_sample_num, roationModule.x, roationModule.y, roationModule.z);
|
||||
} else {
|
||||
this._rotationTexture = packCurveRangeZ(_sample_num, roationModule.z);
|
||||
}
|
||||
const handle = pass.getHandle('rotation_over_time_tex0');
|
||||
const binding = Pass.getBindingFromHandle(handle!);
|
||||
pass.bindTextureView(binding, this._rotationTexture.getGFXTextureView()!);
|
||||
const modeHandle = pass.getHandle('u_rotation_mode');
|
||||
pass.setUniform(modeHandle!, this._rotationTexture.height);
|
||||
}
|
||||
|
||||
// size module
|
||||
const sizeModule = this._particleSystem.sizeOvertimeModule;
|
||||
this._defines[SIZE_OVER_TIME_MODULE_ENABLE] = sizeModule.enable;
|
||||
if (sizeModule.enable) {
|
||||
this._sizeTexture && this._sizeTexture.destroy();
|
||||
if (sizeModule.separateAxes) {
|
||||
this._sizeTexture = packCurveRangeXYZ(_sample_num, sizeModule.x, sizeModule.y, sizeModule.z);
|
||||
} else {
|
||||
this._sizeTexture = packCurveRangeN(_sample_num, sizeModule.size);
|
||||
}
|
||||
const handle = pass.getHandle('size_over_time_tex0');
|
||||
const binding = Pass.getBindingFromHandle(handle!);
|
||||
pass.bindTextureView(binding, this._sizeTexture.getGFXTextureView()!);
|
||||
const modeHandle = pass.getHandle('u_size_mode');
|
||||
pass.setUniform(modeHandle!, this._sizeTexture.height);
|
||||
}
|
||||
|
||||
// texture module
|
||||
const textureModule = this._particleSystem.textureAnimationModule;
|
||||
this._defines[TEXTURE_ANIMATION_MODULE_ENABLE] = textureModule.enable;
|
||||
if (textureModule.enable) {
|
||||
this._animTexture && this._animTexture.destroy();
|
||||
this._animTexture = packCurveRangeXY(_sample_num, textureModule.startFrame, textureModule.frameOverTime);
|
||||
|
||||
const handle = pass.getHandle('texture_animation_tex0');
|
||||
const binding = Pass.getBindingFromHandle(handle!);
|
||||
pass.bindTextureView(binding, this._animTexture.getGFXTextureView()!);
|
||||
const infoHandle = pass.getHandle('u_anim_info');
|
||||
_tempVec4.x = this._animTexture.height;
|
||||
_tempVec4.y = textureModule.numTilesX * textureModule.numTilesY;
|
||||
_tempVec4.z = textureModule.cycleCount;
|
||||
pass.setUniform(infoHandle!, _tempVec4);
|
||||
}
|
||||
}
|
||||
|
||||
public getParticleCount (): number {
|
||||
return this._particleNum;
|
||||
}
|
||||
|
||||
public onMaterialModified (index: number, material: Material) {
|
||||
this._updateModel();
|
||||
this.updateMaterialParams();
|
||||
}
|
||||
|
||||
public onRebuildPSO (index: number, material: Material) {
|
||||
if (this._model && index === 0) {
|
||||
this._model.setSubModelMaterial(0, material);
|
||||
}
|
||||
}
|
||||
|
||||
private _setVertexAttrib () {
|
||||
switch (this._renderInfo!.renderMode) {
|
||||
case RenderMode.StrecthedBillboard:
|
||||
this._vertAttrs = _gpu_vert_attr.slice();
|
||||
break;
|
||||
case RenderMode.Mesh:
|
||||
this._vertAttrs = _gpu_vert_attr_mesh.slice();
|
||||
break;
|
||||
default:
|
||||
this._vertAttrs = _gpu_vert_attr.slice();
|
||||
}
|
||||
}
|
||||
|
||||
public updateMaterialParams () {
|
||||
if (!this._particleSystem) {
|
||||
return;
|
||||
}
|
||||
if (this._particleSystem.sharedMaterial != null && this._particleSystem.sharedMaterial._effectAsset._name.indexOf('particle-gpu') === -1) {
|
||||
// reset material
|
||||
this._particleSystem.setMaterial(null, 0);
|
||||
}
|
||||
if (this._particleSystem.sharedMaterial == null && this._defaultMat == null) {
|
||||
_matInsInfo.parent = builtinResMgr.get<Material>('default-particle-gpu-material');
|
||||
_matInsInfo.owner = this._particleSystem;
|
||||
_matInsInfo.subModelIdx = 0;
|
||||
this._defaultMat = new MaterialInstance(_matInsInfo);
|
||||
}
|
||||
const mat: Material | null = this._particleSystem.getMaterialInstance(0) || this._defaultMat;
|
||||
|
||||
this._particleSystem.node.getWorldMatrix(_tempWorldTrans);
|
||||
switch (this._particleSystem.scaleSpace) {
|
||||
case Space.Local:
|
||||
this._particleSystem.node.getScale(this._node_scale);
|
||||
break;
|
||||
case Space.World:
|
||||
this._particleSystem.node.getWorldScale(this._node_scale);
|
||||
break;
|
||||
}
|
||||
|
||||
if (this._particleSystem._simulationSpace === Space.World) {
|
||||
this._defines[CC_USE_WORLD_SPACE] = true;
|
||||
} else {
|
||||
this._defines[CC_USE_WORLD_SPACE] = false;
|
||||
}
|
||||
const renderMode = this._renderInfo!.renderMode;
|
||||
if (renderMode === RenderMode.Billboard) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_BILLBOARD;
|
||||
} else if (renderMode === RenderMode.StrecthedBillboard) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_STRETCHED_BILLBOARD;
|
||||
this._frameTile_velLenScale.z = this._renderInfo!.velocityScale;
|
||||
this._frameTile_velLenScale.w = this._renderInfo!.lengthScale;
|
||||
} else if (renderMode === RenderMode.HorizontalBillboard) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_HORIZONTAL_BILLBOARD;
|
||||
} else if (renderMode === RenderMode.VerticalBillboard) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_VERTICAL_BILLBOARD;
|
||||
} else if (renderMode === RenderMode.Mesh) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_MESH;
|
||||
} else {
|
||||
console.warn(`particle system renderMode ${renderMode} not support.`);
|
||||
}
|
||||
|
||||
if (this._particleSystem.textureAnimationModule.enable) {
|
||||
Vec2.set(this._frameTile_velLenScale, this._particleSystem.textureAnimationModule.numTilesX, this._particleSystem.textureAnimationModule.numTilesY);
|
||||
}
|
||||
|
||||
this.initShaderUniform(mat!);
|
||||
|
||||
mat!.recompileShaders(this._defines);
|
||||
|
||||
if (this._model) {
|
||||
this._model.setSubModelMaterial(0, mat);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,543 +0,0 @@
|
||||
import { builtinResMgr } from '../../core/3d/builtin';
|
||||
import { Material, Mesh } from '../../core/assets';
|
||||
import { Component } from '../../core/components';
|
||||
import { ccclass, property } from '../../core/data/class-decorator';
|
||||
import { GFXAttributeName, GFXFormat } from '../../core/gfx/define';
|
||||
import { IGFXAttribute } from '../../core/gfx/input-assembler';
|
||||
import { Mat4, Vec2, Vec3, Vec4 } from '../../core/math';
|
||||
import { RecyclePool } from '../../core/memop';
|
||||
import { MaterialInstance, IMaterialInstanceInfo } from '../../core/renderer/core/material-instance';
|
||||
import { IDefineMap } from '../../core/renderer/core/pass-utils';
|
||||
import { RenderMode, Space } from '../enum';
|
||||
import ParticleBatchModel from '../models/particle-batch-model';
|
||||
import Particle from '../particle';
|
||||
|
||||
const _tempAttribUV = new Vec3();
|
||||
const _tempAttribUV0 = new Vec2();
|
||||
const _tempWorldTrans = new Mat4();
|
||||
|
||||
const _uvs = [
|
||||
0, 0, // bottom-left
|
||||
1, 0, // bottom-right
|
||||
0, 1, // top-left
|
||||
1, 1, // top-right
|
||||
];
|
||||
|
||||
const CC_USE_WORLD_SPACE = 'CC_USE_WORLD_SPACE';
|
||||
|
||||
const CC_RENDER_MODE = 'CC_RENDER_MODE';
|
||||
const RENDER_MODE_BILLBOARD = 0;
|
||||
const RENDER_MODE_STRETCHED_BILLBOARD = 1;
|
||||
const RENDER_MODE_HORIZONTAL_BILLBOARD = 2;
|
||||
const RENDER_MODE_VERTICAL_BILLBOARD = 3;
|
||||
const RENDER_MODE_MESH = 4;
|
||||
|
||||
const _vertex_attrs = [
|
||||
{ name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // position
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, // uv,frame idx
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RGB32F }, // size
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD2, format: GFXFormat.RGB32F }, // rotation
|
||||
{ name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true }, // color
|
||||
];
|
||||
|
||||
const _vertex_attrs_stretch = [
|
||||
{ name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // position
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, // uv,frame idx
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RGB32F }, // size
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD2, format: GFXFormat.RGB32F }, // rotation
|
||||
{ name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true }, // color
|
||||
{ name: GFXAttributeName.ATTR_COLOR1, format: GFXFormat.RGB32F }, // particle velocity
|
||||
];
|
||||
|
||||
const _vertex_attrs_mesh = [
|
||||
{ name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // particle position
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, // uv,frame idx
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RGB32F }, // size
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD2, format: GFXFormat.RGB32F }, // rotation
|
||||
{ name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true }, // particle color
|
||||
{ name: GFXAttributeName.ATTR_TEX_COORD3, format: GFXFormat.RGB32F }, // mesh position
|
||||
{ name: GFXAttributeName.ATTR_NORMAL, format: GFXFormat.RGB32F }, // mesh normal
|
||||
{ name: GFXAttributeName.ATTR_COLOR1, format: GFXFormat.RGBA8, isNormalized: true }, // mesh color
|
||||
];
|
||||
|
||||
const _matInsInfo: IMaterialInstanceInfo = {
|
||||
parent: null!,
|
||||
owner: null!,
|
||||
subModelIdx: 0,
|
||||
};
|
||||
|
||||
@ccclass('cc.ParticleSystemRenderer')
|
||||
export default class ParticleSystemRenderer {
|
||||
|
||||
/**
|
||||
* @zh 设定粒子生成模式。
|
||||
*/
|
||||
@property({
|
||||
type: RenderMode,
|
||||
displayOrder: 0,
|
||||
tooltip: '设定粒子生成模式',
|
||||
})
|
||||
public get renderMode () {
|
||||
return this._renderMode;
|
||||
}
|
||||
|
||||
public set renderMode (val) {
|
||||
if (this._renderMode === val) {
|
||||
return;
|
||||
}
|
||||
this._renderMode = val;
|
||||
this._setVertexAttrib();
|
||||
this._updateModel();
|
||||
this._updateMaterialParams();
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按速度大小进行拉伸。
|
||||
*/
|
||||
@property({
|
||||
displayOrder: 1,
|
||||
tooltip: '在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按速度大小进行拉伸',
|
||||
})
|
||||
public get velocityScale () {
|
||||
return this._velocityScale;
|
||||
}
|
||||
|
||||
public set velocityScale (val) {
|
||||
this._velocityScale = val;
|
||||
this._updateMaterialParams();
|
||||
// this._updateModel();
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按粒子大小进行拉伸。
|
||||
*/
|
||||
@property({
|
||||
displayOrder: 2,
|
||||
tooltip: '在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按粒子大小进行拉伸',
|
||||
})
|
||||
public get lengthScale () {
|
||||
return this._lengthScale;
|
||||
}
|
||||
|
||||
public set lengthScale (val) {
|
||||
this._lengthScale = val;
|
||||
this._updateMaterialParams();
|
||||
// this._updateModel();
|
||||
}
|
||||
|
||||
@property({
|
||||
type: RenderMode,
|
||||
displayOrder: 3,
|
||||
})
|
||||
private _renderMode = RenderMode.Billboard;
|
||||
|
||||
@property({
|
||||
displayOrder: 4,
|
||||
})
|
||||
private _velocityScale = 1;
|
||||
|
||||
@property({
|
||||
displayOrder: 5,
|
||||
})
|
||||
private _lengthScale = 1;
|
||||
|
||||
@property({
|
||||
displayOrder: 6,
|
||||
})
|
||||
private _mesh: Mesh | null = null;
|
||||
|
||||
@property
|
||||
private _particleSystem: any = null;
|
||||
|
||||
/**
|
||||
* @zh 粒子发射的模型。
|
||||
*/
|
||||
@property({
|
||||
type: Mesh,
|
||||
displayOrder: 7,
|
||||
tooltip: '粒子发射的模型',
|
||||
})
|
||||
public get mesh () {
|
||||
return this._mesh;
|
||||
}
|
||||
|
||||
public set mesh (val) {
|
||||
this._mesh = val;
|
||||
if (this._model) {
|
||||
this._model.setVertexAttributes(this._renderMode === RenderMode.Mesh ? this._mesh : null, this._vertAttrs);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 粒子使用的材质。
|
||||
*/
|
||||
@property({
|
||||
type: Material,
|
||||
displayOrder: 8,
|
||||
tooltip: '粒子使用的材质',
|
||||
})
|
||||
public get particleMaterial () {
|
||||
if (!this._particleSystem) {
|
||||
return null;
|
||||
}
|
||||
return this._particleSystem.getMaterial(0);
|
||||
}
|
||||
|
||||
public set particleMaterial (val) {
|
||||
this._particleSystem.setMaterial(val, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @zh 拖尾使用的材质。
|
||||
*/
|
||||
@property({
|
||||
type: Material,
|
||||
displayOrder: 9,
|
||||
tooltip: '拖尾使用的材质',
|
||||
})
|
||||
public get trailMaterial () {
|
||||
if (!this._particleSystem) {
|
||||
return null;
|
||||
}
|
||||
return this._particleSystem.getMaterial(1)!;
|
||||
}
|
||||
|
||||
public set trailMaterial (val) {
|
||||
this._particleSystem.setMaterial(val, 1);
|
||||
}
|
||||
|
||||
private _defines: IDefineMap;
|
||||
private _trailDefines: IDefineMap;
|
||||
private _model: ParticleBatchModel | null;
|
||||
private frameTile_velLenScale: Vec4;
|
||||
private _node_scale: Vec4;
|
||||
private attrs: any[];
|
||||
private _vertAttrs: IGFXAttribute[] = [];
|
||||
private _particles: RecyclePool | null = null;
|
||||
private _defaultMat: Material | null = null;
|
||||
private _defaultTrailMat: Material | null = null;
|
||||
|
||||
constructor () {
|
||||
this._model = null;
|
||||
|
||||
this.frameTile_velLenScale = new Vec4(1, 1, 0, 0);
|
||||
this._node_scale = new Vec4();
|
||||
this.attrs = new Array(5);
|
||||
this._defines = {
|
||||
CC_USE_WORLD_SPACE: true,
|
||||
CC_USE_BILLBOARD: true,
|
||||
CC_USE_STRETCHED_BILLBOARD: false,
|
||||
CC_USE_HORIZONTAL_BILLBOARD: false,
|
||||
CC_USE_VERTICAL_BILLBOARD: false,
|
||||
};
|
||||
this._trailDefines = {
|
||||
CC_USE_WORLD_SPACE: true,
|
||||
// CC_DRAW_WIRE_FRAME: true, // <wireframe debug>
|
||||
};
|
||||
}
|
||||
|
||||
public onInit (ps: Component) {
|
||||
this._particleSystem = ps;
|
||||
this._particles = new RecyclePool(() => {
|
||||
return new Particle(this);
|
||||
}, 16);
|
||||
this._setVertexAttrib();
|
||||
this._updateModel();
|
||||
this._updateMaterialParams();
|
||||
this._updateTrailMaterial();
|
||||
}
|
||||
|
||||
public onEnable () {
|
||||
if (!this._particleSystem) {
|
||||
return;
|
||||
}
|
||||
this._attachToScene();
|
||||
this._model!.initialize(this._particleSystem.node);
|
||||
this._model!.enabled = this._particleSystem.enabledInHierarchy;
|
||||
}
|
||||
|
||||
public onDisable () {
|
||||
this._detachFromScene();
|
||||
}
|
||||
|
||||
public onDestroy () {
|
||||
if (this._model) {
|
||||
cc.director.root.destroyModel(this._model);
|
||||
this._model = null;
|
||||
}
|
||||
}
|
||||
|
||||
public _attachToScene () {
|
||||
if (this._model) {
|
||||
if (this._model.scene) {
|
||||
this._detachFromScene();
|
||||
}
|
||||
this._particleSystem._getRenderScene().addModel(this._model);
|
||||
}
|
||||
}
|
||||
|
||||
public _detachFromScene () {
|
||||
if (this._model && this._model.scene) {
|
||||
this._model.scene.removeModel(this._model);
|
||||
}
|
||||
}
|
||||
|
||||
public clear () {
|
||||
this._particles!.reset();
|
||||
this._updateRenderData();
|
||||
}
|
||||
|
||||
public _getFreeParticle (): Particle | null {
|
||||
if (this._particles!.length >= this._particleSystem.capacity) {
|
||||
return null;
|
||||
}
|
||||
return this._particles!.add();
|
||||
}
|
||||
|
||||
public _setNewParticle (p: Particle) {
|
||||
|
||||
}
|
||||
|
||||
public _updateParticles (dt: number) {
|
||||
this._particleSystem.node.getWorldMatrix(_tempWorldTrans);
|
||||
switch (this._particleSystem.scaleSpace) {
|
||||
case Space.Local:
|
||||
this._particleSystem.node.getScale(this._node_scale);
|
||||
break;
|
||||
case Space.World:
|
||||
this._particleSystem.node.getWorldScale(this._node_scale);
|
||||
break;
|
||||
}
|
||||
const mat: Material | null = this._particleSystem.getMaterialInstance(0) || this._defaultMat;
|
||||
mat!.setProperty('scale', this._node_scale);
|
||||
if (this._particleSystem.velocityOvertimeModule.enable) {
|
||||
this._particleSystem.velocityOvertimeModule.update(this._particleSystem._simulationSpace, _tempWorldTrans);
|
||||
}
|
||||
if (this._particleSystem.limitVelocityOvertimeModule.enable) {
|
||||
this._particleSystem.limitVelocityOvertimeModule.update(this._particleSystem._simulationSpace, _tempWorldTrans);
|
||||
}
|
||||
if (this._particleSystem.forceOvertimeModule.enable) {
|
||||
this._particleSystem.forceOvertimeModule.update(this._particleSystem._simulationSpace, _tempWorldTrans);
|
||||
}
|
||||
if (this._particleSystem.trailModule.enable) {
|
||||
this._particleSystem.trailModule.update();
|
||||
}
|
||||
for (let i = 0; i < this._particles!.length; ++i) {
|
||||
const p = this._particles!.data[i];
|
||||
p.remainingLifetime -= dt;
|
||||
Vec3.set(p.animatedVelocity, 0, 0, 0);
|
||||
|
||||
if (p.remainingLifetime < 0.0) {
|
||||
if (this._particleSystem.trailModule.enable) {
|
||||
this._particleSystem.trailModule.removeParticle(p);
|
||||
}
|
||||
this._particles!.removeAt(i);
|
||||
--i;
|
||||
continue;
|
||||
}
|
||||
|
||||
// apply gravity.
|
||||
p.velocity.y -= this._particleSystem.gravityModifier.evaluate(1 - p.remainingLifetime / p.startLifetime, p.randomSeed)! * 9.8 * dt;
|
||||
if (this._particleSystem.sizeOvertimeModule.enable) {
|
||||
this._particleSystem.sizeOvertimeModule.animate(p);
|
||||
}
|
||||
if (this._particleSystem.colorOverLifetimeModule.enable) {
|
||||
this._particleSystem.colorOverLifetimeModule.animate(p);
|
||||
}
|
||||
if (this._particleSystem.forceOvertimeModule.enable) {
|
||||
this._particleSystem.forceOvertimeModule.animate(p, dt);
|
||||
}
|
||||
if (this._particleSystem.velocityOvertimeModule.enable) {
|
||||
this._particleSystem.velocityOvertimeModule.animate(p);
|
||||
} else {
|
||||
Vec3.copy(p.ultimateVelocity, p.velocity);
|
||||
}
|
||||
if (this._particleSystem.limitVelocityOvertimeModule.enable) {
|
||||
this._particleSystem.limitVelocityOvertimeModule.animate(p);
|
||||
}
|
||||
if (this._particleSystem.rotationOvertimeModule.enable) {
|
||||
this._particleSystem.rotationOvertimeModule.animate(p, dt);
|
||||
}
|
||||
if (this._particleSystem.textureAnimationModule.enable) {
|
||||
this._particleSystem.textureAnimationModule.animate(p);
|
||||
}
|
||||
Vec3.scaleAndAdd(p.position, p.position, p.ultimateVelocity, dt); // apply velocity.
|
||||
if (this._particleSystem.trailModule.enable) {
|
||||
this._particleSystem.trailModule.animate(p, dt);
|
||||
}
|
||||
}
|
||||
return this._particles!.length;
|
||||
}
|
||||
|
||||
// internal function
|
||||
public _updateRenderData () {
|
||||
// update vertex buffer
|
||||
let idx = 0;
|
||||
const uploadVel = this._renderMode === RenderMode.StrecthedBillboard;
|
||||
for (let i = 0; i < this._particles!.length; ++i) {
|
||||
const p = this._particles!.data[i];
|
||||
let fi = 0;
|
||||
if (this._particleSystem.textureAnimationModule.enable) {
|
||||
fi = p.frameIndex;
|
||||
}
|
||||
idx = i * 4;
|
||||
let attrNum = 0;
|
||||
if (this._renderMode !== RenderMode.Mesh) {
|
||||
for (let j = 0; j < 4; ++j) { // four verts per particle.
|
||||
attrNum = 0;
|
||||
this.attrs[attrNum++] = p.position;
|
||||
_tempAttribUV.x = _uvs[2 * j];
|
||||
_tempAttribUV.y = _uvs[2 * j + 1];
|
||||
_tempAttribUV.z = fi;
|
||||
this.attrs[attrNum++] = _tempAttribUV;
|
||||
this.attrs[attrNum++] = p.size;
|
||||
this.attrs[attrNum++] = p.rotation;
|
||||
this.attrs[attrNum++] = p.color._val;
|
||||
|
||||
if (uploadVel) {
|
||||
this.attrs[attrNum++] = p.ultimateVelocity;
|
||||
} else {
|
||||
this.attrs[attrNum++] = null;
|
||||
}
|
||||
|
||||
this._model!.addParticleVertexData(idx++, this.attrs);
|
||||
}
|
||||
} else {
|
||||
attrNum = 0;
|
||||
this.attrs[attrNum++] = p.position;
|
||||
_tempAttribUV.z = fi;
|
||||
this.attrs[attrNum++] = _tempAttribUV;
|
||||
this.attrs[attrNum++] = p.size;
|
||||
this.attrs[attrNum++] = p.rotation;
|
||||
this.attrs[attrNum++] = p.color._val;
|
||||
this._model!.addParticleVertexData(i, this.attrs);
|
||||
}
|
||||
}
|
||||
|
||||
// because we use index buffer, per particle index count = 6.
|
||||
this._model!.updateIA(this._particles!.length);
|
||||
}
|
||||
|
||||
public updateShaderUniform () {
|
||||
|
||||
}
|
||||
|
||||
public getParticleCount (): number {
|
||||
return this._particles!.length;
|
||||
}
|
||||
|
||||
public _onMaterialModified (index: number, material: Material) {
|
||||
if (index === 0) {
|
||||
this._updateModel();
|
||||
this._updateMaterialParams();
|
||||
} else {
|
||||
this._updateTrailMaterial();
|
||||
}
|
||||
}
|
||||
|
||||
public _onRebuildPSO (index: number, material: Material) {
|
||||
if (this._model && index === 0) {
|
||||
this._model.setSubModelMaterial(0, material);
|
||||
}
|
||||
if (this._particleSystem.trailModule._trailModel && index === 1) {
|
||||
this._particleSystem.trailModule._trailModel.setSubModelMaterial(0, material);
|
||||
}
|
||||
}
|
||||
|
||||
private _setVertexAttrib () {
|
||||
switch (this._renderMode) {
|
||||
case RenderMode.StrecthedBillboard:
|
||||
this._vertAttrs = _vertex_attrs_stretch.slice();
|
||||
break;
|
||||
case RenderMode.Mesh:
|
||||
this._vertAttrs = _vertex_attrs_mesh.slice();
|
||||
break;
|
||||
default:
|
||||
this._vertAttrs = _vertex_attrs.slice();
|
||||
}
|
||||
}
|
||||
|
||||
private _updateMaterialParams () {
|
||||
if (!this._particleSystem) {
|
||||
return;
|
||||
}
|
||||
if (this._particleSystem.sharedMaterial != null && this._particleSystem.sharedMaterial._effectAsset._name.indexOf('particle') === -1) {
|
||||
this._particleSystem.setMaterial(null, 0);
|
||||
}
|
||||
if (this._particleSystem.sharedMaterial == null && this._defaultMat == null) {
|
||||
_matInsInfo.parent = builtinResMgr.get<Material>('default-particle-material');
|
||||
_matInsInfo.owner = this._particleSystem;
|
||||
_matInsInfo.subModelIdx = 0;
|
||||
this._defaultMat = new MaterialInstance(_matInsInfo);
|
||||
}
|
||||
const mat: Material | null = this._particleSystem.getMaterialInstance(0) || this._defaultMat;
|
||||
if (this._particleSystem._simulationSpace === Space.World) {
|
||||
this._defines[CC_USE_WORLD_SPACE] = true;
|
||||
} else {
|
||||
this._defines[CC_USE_WORLD_SPACE] = false;
|
||||
}
|
||||
|
||||
if (this._renderMode === RenderMode.Billboard) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_BILLBOARD;
|
||||
} else if (this._renderMode === RenderMode.StrecthedBillboard) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_STRETCHED_BILLBOARD;
|
||||
this.frameTile_velLenScale.z = this._velocityScale;
|
||||
this.frameTile_velLenScale.w = this._lengthScale;
|
||||
} else if (this._renderMode === RenderMode.HorizontalBillboard) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_HORIZONTAL_BILLBOARD;
|
||||
} else if (this._renderMode === RenderMode.VerticalBillboard) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_VERTICAL_BILLBOARD;
|
||||
} else if (this._renderMode === RenderMode.Mesh) {
|
||||
this._defines[CC_RENDER_MODE] = RENDER_MODE_MESH;
|
||||
} else {
|
||||
console.warn(`particle system renderMode ${this._renderMode} not support.`);
|
||||
}
|
||||
|
||||
if (this._particleSystem.textureAnimationModule.enable) {
|
||||
Vec2.set(this.frameTile_velLenScale, this._particleSystem.textureAnimationModule.numTilesX, this._particleSystem.textureAnimationModule.numTilesY);
|
||||
mat!.setProperty('frameTile_velLenScale', this.frameTile_velLenScale);
|
||||
} else {
|
||||
mat!.setProperty('frameTile_velLenScale', this.frameTile_velLenScale);
|
||||
}
|
||||
mat!.recompileShaders(this._defines);
|
||||
if (this._model) {
|
||||
this._model.setSubModelMaterial(0, mat);
|
||||
}
|
||||
}
|
||||
|
||||
private _updateTrailMaterial () {
|
||||
if (this._particleSystem.trailModule.enable) {
|
||||
if (this._particleSystem._simulationSpace === Space.World || this._particleSystem.trailModule.space === Space.World) {
|
||||
this._trailDefines[CC_USE_WORLD_SPACE] = true;
|
||||
} else {
|
||||
this._trailDefines[CC_USE_WORLD_SPACE] = false;
|
||||
}
|
||||
let mat = this._particleSystem.getMaterialInstance(1);
|
||||
if (mat === null && this._defaultTrailMat === null) {
|
||||
_matInsInfo.parent = builtinResMgr.get<Material>('default-trail-material');
|
||||
_matInsInfo.owner = this._particleSystem;
|
||||
_matInsInfo.subModelIdx = 1;
|
||||
this._defaultTrailMat = new MaterialInstance(_matInsInfo);
|
||||
}
|
||||
if (mat === null) {
|
||||
mat = this._defaultTrailMat;
|
||||
}
|
||||
mat!.recompileShaders(this._trailDefines);
|
||||
this._particleSystem.trailModule._updateMaterial();
|
||||
}
|
||||
}
|
||||
|
||||
private _updateModel () {
|
||||
if (!this._model) {
|
||||
this._model = cc.director.root.createModel(ParticleBatchModel);
|
||||
this._model!.setCapacity(this._particleSystem.capacity);
|
||||
this._model!.visFlags = this._particleSystem.visibility;
|
||||
}
|
||||
this._model!.setVertexAttributes(this._renderMode === RenderMode.Mesh ? this._mesh : null, this._vertAttrs);
|
||||
// if (Object.getPrototypeOf(this).constructor.name === 'ParticleSystemGpuRenderer') {
|
||||
// return;
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
||||
Object.assign(ParticleSystemRenderer, { uv: _uvs });
|
||||
@@ -17,7 +17,7 @@ import { Model } from '../../core/renderer';
|
||||
import CurveRange from '../animator/curve-range';
|
||||
import GradientRange from '../animator/gradient-range';
|
||||
import { Space, TextureMode, TrailMode } from '../enum';
|
||||
import Particle from '../particle';
|
||||
import { Particle } from '../particle';
|
||||
|
||||
// tslint:disable: max-line-length
|
||||
const PRE_TRIANGLE_INDEX = 1;
|
||||
@@ -162,7 +162,7 @@ export default class TrailModule {
|
||||
}
|
||||
if (val && !this._enable) {
|
||||
this._enable = val;
|
||||
this._particleSystem.renderer._updateTrailMaterial();
|
||||
this._particleSystem.processor.updateTrailMaterial();
|
||||
}
|
||||
this._enable = val;
|
||||
if (this._trailModel) {
|
||||
@@ -224,7 +224,7 @@ export default class TrailModule {
|
||||
public set space (val) {
|
||||
this._space = val;
|
||||
if (this._particleSystem) {
|
||||
this._particleSystem.renderer._updateTrailMaterial();
|
||||
this._particleSystem.processor.updateTrailMaterial();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -410,7 +410,7 @@ export default class TrailModule {
|
||||
if (mat) {
|
||||
this._trailModel.setSubModelMaterial(0, mat);
|
||||
} else {
|
||||
this._trailModel.setSubModelMaterial(0, this._particleSystem.renderer._defaultTrailMat);
|
||||
this._trailModel.setSubModelMaterial(0, this._particleSystem.processor._defaultTrailMat);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user