add default trail material & world space trail (#4389)

This commit is contained in:
zxg0622
2019-05-09 20:34:05 +08:00
committed by pandamicro
parent d7b678f777
commit 3bdd342961
15 changed files with 319 additions and 225 deletions
+54 -15
View File
File diff suppressed because one or more lines are too long
+6
View File
@@ -174,6 +174,12 @@ class BuiltinResMgr {
defaultParticleMtl.initialize({ effectName: 'builtin-particle' });
resources[defaultParticleMtl._uuid] = defaultParticleMtl;
// default particle material
const defaultTrailMtl = new cc.Material();
defaultTrailMtl._uuid = 'default-trail-material';
defaultTrailMtl.initialize({ effectName: 'builtin-particle-trail' });
resources[defaultTrailMtl._uuid] = defaultTrailMtl;
// customization for special effects
const tmp = aabb.create();
customizationManager.register('bounds-merge-shadow', {
@@ -14,6 +14,8 @@ export const Mode = Enum({
@ccclass('cc.CurveRange')
export default class CurveRange {
public static Mode = Mode;
@property({
type: Mode,
})
@@ -1,7 +1,8 @@
import { CCClass } from '../../../../core/data';
import { ccclass, property } from '../../../../core/data/class-decorator';
import { pseudoRandom, quat, vec3 } from '../../../../core/vmath';
import { calculateTransform, Space } from '../particle-general-function';
import { Space } from '../enum';
import { calculateTransform } from '../particle-general-function';
import CurveRange from './curve-range';
// tslint:disable: max-line-length
@@ -26,6 +26,8 @@ const Mode = Enum({
@ccclass('cc.GradientRange')
export default class GradientRange {
public static Mode = Mode;
@property({
type: Mode,
})
@@ -43,6 +43,8 @@ export class AlphaKey {
@ccclass('cc.Gradient')
export default class Gradient {
public static Mode = Mode;
@property
public colorKeys = new Array<ColorKey>();
@@ -1,8 +1,8 @@
import { CCClass } from '../../../../core/data';
import { ccclass, property } from '../../../../core/data/class-decorator';
import { lerp, pseudoRandom, vec3 } from '../../../../core/vmath';
import { Space } from '../enum';
import Particle from '../particle';
import { Space } from '../particle-general-function';
import CurveRange from './curve-range';
// tslint:disable: max-line-length
@@ -1,8 +1,9 @@
import { CCClass } from '../../../../core/data';
import { ccclass, property } from '../../../../core/data/class-decorator';
import { mat4, pseudoRandom, quat, vec3 } from '../../../../core/vmath';
import { Space } from '../enum';
import Particle from '../particle';
import { calculateTransform, Space } from '../particle-general-function';
import { calculateTransform } from '../particle-general-function';
import CurveRange from './curve-range';
// tslint:disable: max-line-length
+4
View File
@@ -88,6 +88,10 @@ export default class Burst {
}
}
}
public getMaxCount (psys) {
return this.count.getMax() * Math.min(Math.floor(psys.duration / this.repeatInterval), this.repeatCount);
}
}
// CCClass.fastDefine('cc.Burst', Burst, {
@@ -1,106 +1,17 @@
import { randomUnitVector, particleEmitZAxis, randomSortArray, randomSign, randomPointBetweenSphere, randomPointInCube, randomPointBetweenCircleAtFixedAngle, fixedAngleUnitVector2 } from '../particle-general-function';
import { vec3, vec2, random, randomRange, mat4, quat, clamp, repeat, pingPong, toRadian, toDegree } from '../../../../core/vmath';
import { Enum, Vec3 } from '../../../../core/value-types';
import CurveRange from '../animator/curve-range';
import { CCClass } from '../../../../core/data';
import { property, ccclass } from '../../../../core/data/class-decorator';
import { ccclass, property } from '../../../../core/data/class-decorator';
import { Enum, Vec3 } from '../../../../core/value-types';
import { clamp, mat4, pingPong, quat, random, randomRange, repeat, toDegree, toRadian, vec2, vec3 } from '../../../../core/vmath';
import CurveRange from '../animator/curve-range';
import { fixedAngleUnitVector2, particleEmitZAxis, randomPointBetweenCircleAtFixedAngle, randomPointBetweenSphere, randomPointInCube, randomSign, randomSortArray, randomUnitVector } from '../particle-general-function';
import { ParticleSystemComponent } from '../particle-system-component';
import { ShapeType, EmitLocation, ArcMode } from '../enum';
// tslint:disable: max-line-length
const _intermediVec = vec3.create(0, 0, 0);
const _intermediArr = new Array();
const _unitBoxExtent = vec3.create(0.5, 0.5, 0.5);
/**
* 粒子发射器类型
* @enum shapeModule.ShapeType
*/
const ShapeType = Enum({
/**
* 立方体类型粒子发射器
* @property {Number} Box
*/
Box: 0,
/**
* 圆形粒子发射器
* @property {Number} Circle
*/
Circle: 1,
/**
* 圆锥体粒子发射器
* @property {Number} Cone
*/
Cone: 2,
/**
* 球体粒子发射器
* @property {Number} Sphere
*/
Sphere: 3,
/**
* 半球体粒子发射器
* @property {Number} Hemisphere
*/
Hemisphere: 4,
});
/**
* 粒子从发射器的哪个部位发射
* @enum shapeModule.EmitLocation
*/
const EmitLocation = Enum({
/**
* 基础位置发射(仅对 Circle 类型及 Cone 类型的粒子发射器适用)
* @property {Number} Base
*/
Base: 0,
/**
* 边框位置发射(仅对 Box 类型及 Circle 类型的粒子发射器适用)
* @property {Number} Edge
*/
Edge: 1,
/**
* 表面位置发射(对所有类型的粒子发射器都适用)
* @property {Number} Shell
*/
Shell: 2,
/**
* 内部位置发射(对所有类型的粒子发射器都适用)
* @property {Number} Volume
*/
Volume: 3,
});
/**
* 粒子在扇形区域的发射方式
* @enum shapeModule.ArcMode
*/
const ArcMode = Enum({
/**
* 随机位置发射
* @property {Number} Random
*/
Random: 0,
/**
* 沿某一方向循环发射,每次循环方向相同
* @property {Number} Loop
*/
Loop: 1,
/**
* 循环发射,每次循环方向相反
* @property {Number} PingPong
*/
PingPong: 2,
});
@ccclass('cc.ShapeModule')
export default class ShapeModule {
+163
View File
@@ -0,0 +1,163 @@
import { Enum } from '../../../core';
export const Space = Enum({
World: 0,
Local: 1,
Custom: 2,
});
/**
* 粒子的生成模式
* @enum ParticleSystemRenderer.RenderMode
*/
export const RenderMode = Enum({
/**
* 粒子始终面向摄像机
*/
Billboard: 0,
/**
* 粒子始终面向摄像机但会根据参数进行拉伸
*/
StrecthedBillboard: 1,
/**
* 粒子始终与 XZ 平面平行
*/
HorizontalBillboard: 2,
/**
* 粒子始终与 Y 轴平行且朝向摄像机
*/
VerticalBillboard: 3,
/**
* 粒子保持模型本身状态
*/
Mesh: 4,
});
/**
* 粒子发射器类型
* @enum shapeModule.ShapeType
*/
export const ShapeType = Enum({
/**
* 立方体类型粒子发射器
* @property {Number} Box
*/
Box: 0,
/**
* 圆形粒子发射器
* @property {Number} Circle
*/
Circle: 1,
/**
* 圆锥体粒子发射器
* @property {Number} Cone
*/
Cone: 2,
/**
* 球体粒子发射器
* @property {Number} Sphere
*/
Sphere: 3,
/**
* 半球体粒子发射器
* @property {Number} Hemisphere
*/
Hemisphere: 4,
});
/**
* 粒子从发射器的哪个部位发射
* @enum shapeModule.EmitLocation
*/
export const EmitLocation = Enum({
/**
* 基础位置发射(仅对 Circle 类型及 Cone 类型的粒子发射器适用)
* @property {Number} Base
*/
Base: 0,
/**
* 边框位置发射(仅对 Box 类型及 Circle 类型的粒子发射器适用)
* @property {Number} Edge
*/
Edge: 1,
/**
* 表面位置发射(对所有类型的粒子发射器都适用)
* @property {Number} Shell
*/
Shell: 2,
/**
* 内部位置发射(对所有类型的粒子发射器都适用)
* @property {Number} Volume
*/
Volume: 3,
});
/**
* 粒子在扇形区域的发射方式
* @enum shapeModule.ArcMode
*/
export const ArcMode = Enum({
/**
* 随机位置发射
* @property {Number} Random
*/
Random: 0,
/**
* 沿某一方向循环发射,每次循环方向相同
* @property {Number} Loop
*/
Loop: 1,
/**
* 循环发射,每次循环方向相反
* @property {Number} PingPong
*/
PingPong: 2,
});
/**
* 选择如何为粒子系统生成轨迹
* @enum trailModule.TrailMode
*/
export const TrailMode = Enum({
/**
* 粒子模式<bg>
* 创建一种效果,其中每个粒子在其路径中留下固定的轨迹
*/
Particles: 0,
/**
* 带模式<bg>
* 根据其生命周期创建连接每个粒子的轨迹带
*/
Ribbon: 1,
});
/**
* 纹理填充模式
* @enum trailModule.TextureMode
*/
export const TextureMode = Enum({
/**
* 拉伸填充纹理
*/
Stretch: 0,
/**
* 重复填充纹理
*/
Repeat: 1,
});
@@ -1,15 +1,10 @@
import { mat4, quat, randomRange, vec3, random, vec2, randomRangeInt } from '../../../core/vmath';
import { sign } from '../../../core/vmath/bits';
import { Enum } from '../../../core/value-types';
import { mat4, quat, random, randomRange, randomRangeInt, vec2, vec3 } from '../../../core/vmath';
import { sign } from '../../../core/vmath/bits';
import { Space } from './enum';
export const particleEmitZAxis = vec3.create(0, 0, -1);
export const Space = Enum({
World: 0,
Local: 1,
Custom: 2,
});
export function calculateTransform (systemSpace: number, moduleSpace: number, worldTransform: mat4, outQuat: quat) {
if (moduleSpace !== systemSpace) {
if (systemSpace === Space.World) {
@@ -3,25 +3,26 @@
// tslint:disable: max-line-length
import { Component } from '../../../components/component';
import { vec3, vec2, mat4, quat, randomRangeInt, pseudoRandom } from '../../../core/vmath';
import CurveRange from './animator/curve-range';
import GradientRange from './animator/gradient-range';
import ParticleSystemRenderer from './renderer/particle-system-renderer';
import SizeOvertimeModule from './animator/size-overtime';
import { ccclass, executeInEditMode, executionOrder, menu, property, requireComponent } from '../../../core/data/class-decorator';
import { mat4, pseudoRandom, quat, randomRangeInt, vec2, vec3 } from '../../../core/vmath';
import { INT_MAX } from '../../../core/vmath/bits';
import { Material } from '../../assets';
import { RenderableComponent } from '../renderable-component';
import ColorOverLifetimeModule from './animator/color-overtime';
import VelocityOvertimeModule from './animator/velocity-overtime';
import CurveRange from './animator/curve-range';
import ForceOvertimeModule from './animator/force-overtime';
import GradientRange from './animator/gradient-range';
import LimitVelocityOvertimeModule from './animator/limit-velocity-overtime';
import RotationOvertimeModule from './animator/rotation-overtime';
import SizeOvertimeModule from './animator/size-overtime';
import TextureAnimationModule from './animator/texture-animation';
import ShapeModule from './emitter/shape-module';
import VelocityOvertimeModule from './animator/velocity-overtime';
import Burst from './burst';
import { particleEmitZAxis, Space } from './particle-general-function';
import { INT_MAX } from '../../../core/vmath/bits';
import { ccclass, executionOrder, executeInEditMode, property, requireComponent, menu } from '../../../core/data/class-decorator';
import ShapeModule from './emitter/shape-module';
import { Space } from './enum';
import { particleEmitZAxis } from './particle-general-function';
import ParticleSystemRenderer from './renderer/particle-system-renderer';
import TrailModule from './renderer/trail';
import { RenderableComponent } from '../renderable-component';
import { Material } from '../../assets';
const _world_mat = mat4.create();
@@ -5,13 +5,12 @@ import { GFXAttributeName, GFXFormat } from '../../../../gfx/define';
import { IGFXAttribute } from '../../../../gfx/input-assembler';
import * as renderer from '../../../../renderer';
import ParticleBatchModel from '../../../../renderer/models/particle-batch-model';
import { Material } from '../../../assets/material';
import RecyclePool from '../../../memop/recycle-pool';
import Particle from '../particle';
import { RenderableComponent } from '../../renderable-component';
import { Space } from '../particle-general-function';
import { builtinResMgr } from '../../../builtin';
import { Mesh } from '../../../assets';
import { Material } from '../../../assets/material';
import { builtinResMgr } from '../../../builtin';
import RecyclePool from '../../../memop/recycle-pool';
import { RenderMode, Space } from '../enum';
import Particle from '../particle';
// import ParticleSystemComponent from '../particle-system-component';
// tslint:disable: max-line-length
@@ -27,38 +26,6 @@ const _uvs = [
1, 1, // top-right
];
/**
* 粒子的生成模式
* @enum ParticleSystemRenderer.RenderMode
*/
const RenderMode = Enum({
/**
* 粒子始终面向摄像机
*/
Billboard: 0,
/**
* 粒子始终面向摄像机但会根据参数进行拉伸
*/
StrecthedBillboard: 1,
/**
* 粒子始终与 XZ 平面平行
*/
HorizontalBillboard: 2,
/**
* 粒子始终与 Y 轴平行且朝向摄像机
*/
VerticalBillboard: 3,
/**
* 粒子保持模型本身状态
*/
Mesh: 4,
});
const CC_USE_WORLD_SPACE = 'CC_USE_WORLD_SPACE';
const CC_USE_BILLBOARD = 'CC_USE_BILLBOARD';
const CC_USE_STRETCHED_BILLBOARD = 'CC_USE_STRETCHED_BILLBOARD';
@@ -220,6 +187,7 @@ export default class ParticleSystemRenderer {
private particleSystem: any;
private _particles: RecyclePool | null = null;
private _defaultMat: Material | null = null;
private _defaultTrailMat: Material | null = null;
constructor () {
this._model = null;
@@ -252,7 +220,7 @@ export default class ParticleSystemRenderer {
return;
}
if (this._model == null) {
this._model = this.particleSystem._getRenderScene().createModel(ParticleBatchModel, this.node) as ParticleBatchModel;
this._model = this.particleSystem._getRenderScene().createModel(ParticleBatchModel, this.particleSystem.node) as ParticleBatchModel;
}
if (!this._model.inited) {
this._model.setCapacity(this.particleSystem.capacity);
@@ -444,10 +412,8 @@ export default class ParticleSystemRenderer {
const mat: Material | null = this.particleSystem.sharedMaterial ? this.particleMaterial : this._defaultMat;
if (this.particleSystem._simulationSpace === Space.World) {
this._defines[CC_USE_WORLD_SPACE] = true;
this._trailDefines[CC_USE_WORLD_SPACE] = true;
} else {
this._defines[CC_USE_WORLD_SPACE] = false;
this._trailDefines[CC_USE_WORLD_SPACE] = false;
}
if (this._renderMode === RenderMode.Billboard) {
@@ -499,12 +465,18 @@ export default class ParticleSystemRenderer {
private _updateTrailMaterial () {
if (this.particleSystem.trailModule.enable) {
if (this.particleSystem._simulationSpace === Space.World) {
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;
}
const mat = this.trailMaterial;
let mat = this.trailMaterial;
if (mat === null && this._defaultTrailMat === null) {
this._defaultTrailMat = Material.getInstantiatedMaterial(builtinResMgr.get<Material>('default-trail-material'), this.particleSystem, true);
}
if (mat === null) {
mat = this._defaultTrailMat;
}
mat!.recompileShaders(this._trailDefines);
this.particleSystem.trailModule._updateMaterial();
}
+44 -49
View File
@@ -1,5 +1,5 @@
import { Color, Vec3 } from '../../../../core';
import { property, ccclass } from '../../../../core/data/class-decorator';
import { ccclass, property } from '../../../../core/data/class-decorator';
import Enum from '../../../../core/value-types/enum';
import { vec3 } from '../../../../core/vmath';
import { GFX_DRAW_INFO_SIZE, GFXBuffer, IGFXIndirectBuffer } from '../../../../gfx/buffer';
@@ -10,47 +10,18 @@ import { Model } from '../../../../renderer';
import { IRenderingSubmesh } from '../../../assets/mesh';
import { Pool } from '../../../memop';
import CurveRange from '../animator/curve-range';
import { Space, TrailMode, TextureMode } from '../enum';
import Particle from '../particle';
import { Space } from '../particle-general-function';
import { Material } from '../../../assets';
import { builtinResMgr } from '../../../builtin';
const PRE_TRIANGLE_INDEX = 1;
const NEXT_TRIANGLE_INDEX = 1 << 2;
const _temp_trailEle = {} as ITrailElement;
/**
* 选择如何为粒子系统生成轨迹
* @enum trailModule.TrailMode
*/
const TrailMode = Enum({
/**
* 粒子模式<bg>
* 创建一种效果,其中每个粒子在其路径中留下固定的轨迹
*/
Particles: 0,
/**
* 带模式<bg>
* 根据其生命周期创建连接每个粒子的轨迹带
*/
Ribbon: 1,
});
/**
* 纹理填充模式
* @enum trailModule.TextureMode
*/
const TextureMode = Enum({
/**
* 拉伸填充纹理
*/
Stretch: 0,
/**
* 重复填充纹理
*/
Repeat: 1,
});
const _temp_quat = cc.quat();
const _temp_xform = cc.mat4();
const _temp_vec3 = cc.v3();
interface ITrailElement {
position: Vec3;
@@ -141,7 +112,7 @@ export default class TrailModule {
this._createModel();
}
if (val && !this._enable) {
this._updateMaterial();
this._particleSystem.renderer._updateTrailMaterial();
}
this._enable = val;
if (this._trailModel) {
@@ -196,14 +167,14 @@ export default class TrailModule {
@property
public _minParticleDistance = 0.1;
// /**
// * 轨迹设定时的坐标系
// */
// @property({
// type: Space,
// displayOrder: 6,
// })
// public space = Space.World;
/**
* 轨迹设定时的坐标系
*/
@property({
type: Space,
displayOrder: 6,
})
public space = Space.World;
/**
* 粒子本身是否存在
@@ -248,6 +219,8 @@ export default class TrailModule {
private _vbF32: Float32Array;
private _vbUint32: Uint32Array;
private _iBuffer: Uint16Array;
private _needTransform: boolean;
private _defaultMat: Material | null = null;
constructor () {
this._iaInfo = {
@@ -279,7 +252,11 @@ export default class TrailModule {
public init (ps) {
this._particleSystem = ps;
this.lifeTime.constant = 1;
this._trailNum = ps.startLifetime.getMax() * this.lifeTime.getMax() * 60 * ps.rateOverTime.getMax() * ps.duration;
let burstCount = 0;
for (const b of ps.bursts) {
burstCount += b.getMaxCount(ps);
}
this._trailNum = ps.startLifetime.getMax() * this.lifeTime.getMax() * 60 * (ps.rateOverTime.getMax() * ps.duration + burstCount);
this._trailSegments = new Pool(() => new TrailSegment(ps.startLifetime.getMax() * this.lifeTime.getMax() * 60), Math.ceil(ps.rateOverTime.getMax() * ps.duration));
if (this._enable) {
this.enable = this._enable;
@@ -359,12 +336,21 @@ export default class TrailModule {
const mat = this._particleSystem.renderer.trailMaterial;
if (mat) {
this._trailModel!.setSubModelMaterial(0, mat);
} else {
this._trailModel!.setSubModelMaterial(0, this._particleSystem.renderer._defaultTrailMat);
}
}
}
public update () {
this._trailLifetime = this.lifeTime.evaluate(this._particleSystem._time, 1)!;
if (this.space === Space.World && this._particleSystem._simulationSpace === Space.Local) {
this._needTransform = true;
this._particleSystem.node.getWorldMatrix(_temp_xform);
this._particleSystem.node.getWorldRotation(_temp_quat);
} else {
this._needTransform = false;
}
}
public animate (p: Particle, scaledDt: number) {
@@ -374,17 +360,26 @@ export default class TrailModule {
this._particleTrail.set(p, trail);
}
let lastSeg = trail.getTailElement();
if (this._needTransform) {
vec3.transformMat4(_temp_vec3, p.position, _temp_xform);
} else {
vec3.copy(_temp_vec3, p.position);
}
if (lastSeg) {
trail.iterateElement(this, this._updateTrailElement, scaledDt);
if (vec3.squaredDistance(lastSeg.position, p.position) < this._minSquaredDistance) {
if (vec3.squaredDistance(lastSeg.position, _temp_vec3) < this._minSquaredDistance) {
return;
}
}
lastSeg = trail.addElement();
vec3.copy(lastSeg.position, p.position);
vec3.copy(lastSeg.position, _temp_vec3);
lastSeg.lifetime = 0;
lastSeg.width = p.size.x;
vec3.copy(lastSeg.velocity, p.ultimateVelocity);
if (this._needTransform) {
vec3.transformQuat(lastSeg.velocity, p.ultimateVelocity, _temp_quat);
} else {
vec3.copy(lastSeg.velocity, p.ultimateVelocity);
}
lastSeg.color.set(p.color);
}