Files
cocos4/cocos/3d/framework/particle/animator/texture-animation.ts
T
2019-03-14 19:44:37 +08:00

181 lines
4.6 KiB
TypeScript

import { repeat, lerp, pseudoRandom } from "../../../../core/vmath";
import { Enum } from "../../../../core/value-types";
import CurveRange from "./curve-range";
import { CCClass } from "../../../../core/data";
import { property, ccclass } from "../../../../core/data/class-decorator";
import { ParticleSystemComponent } from "../particle-system-component";
import Particle from "../particle";
// tslint:disable: max-line-length
const TEXTURE_ANIMATION_RAND_OFFSET = 90794;
const Mode = Enum({
Grid: 0,
Sprites: 1,
});
const Animation = Enum({
WholeSheet: 0,
SingleRow: 1,
});
@ccclass('cc.TextureAnimationModule')
export default class TextureAnimationModule {
@property
private _enable = false;
@property({
displayOrder: 0,
})
get enable () {
return this._enable;
}
set enable (val) {
this._enable = val;
(this.ps!.getComponent(cc.ParticleSystemRenderer) as any)._updateMaterialParams();
}
@property({
type: Mode,
})
private _mode = Mode.Grid;
@property({
type: Mode,
displayOrder: 1,
})
get mode () {
return this._mode;
}
set mode (val) {
if (val === Mode.Sprites) {
console.error("particle texture animation's sprites is not supported!");
return;
}
}
@property({
displayOrder: 2,
})
public numTilesX = 0;
@property({
displayOrder: 3,
})
public numTilesY = 0;
@property({
type: Animation,
displayOrder: 4,
})
public animation = Animation.WholeSheet;
@property({
type: CurveRange,
displayOrder: 7,
})
public frameOverTime = new CurveRange();
@property({
type: CurveRange,
displayOrder: 8,
})
public startFrame = new CurveRange();
@property({
displayOrder: 9,
})
public cycleCount = 0;
@property
private _flipU = 0;
get flipU () {
return this._flipU;
}
set flipU (val) {
console.error("particle texture animation's flipU is not supported!");
}
@property
private _flipV = 0;
get flipV () {
return this._flipV;
}
set flipV (val) {
console.error("particle texture animation's flipV is not supported!");
}
@property
private _uvChannelMask = -1;
get uvChannelMask () {
return this._uvChannelMask;
}
set uvChannelMask (val) {
console.error("particle texture animation's uvChannelMask is not supported!");
}
@property({
displayOrder: 5,
})
public randomRow = false;
@property({
displayOrder: 6,
})
public rowIndex = 0;
private ps: ParticleSystemComponent | null = null;
public onInit (ps: ParticleSystemComponent) {
this.ps = ps;
}
public animate (p: Particle) {
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) {
p.frameIndex = repeat(this.cycleCount * (this.frameOverTime.evaluate(normalizedTime, pseudoRandom(p.randomSeed + TEXTURE_ANIMATION_RAND_OFFSET))! + startFrame), 1);
}
else if (this.animation === Animation.SingleRow) {
const rowLength = 1 / this.numTilesY;
if (this.randomRow) {
const f = repeat(this.cycleCount * (this.frameOverTime.evaluate(normalizedTime, pseudoRandom(p.randomSeed + TEXTURE_ANIMATION_RAND_OFFSET))! + startFrame), 1);
const startRow = Math.floor(Math.random() * this.numTilesY);
const from = startRow * rowLength;
const to = from + rowLength;
p.frameIndex = lerp(from, to, f);
}
else {
const from = this.rowIndex * rowLength;
const to = from + rowLength;
p.frameIndex = lerp(from, to, repeat(this.cycleCount * (this.frameOverTime.evaluate(normalizedTime, pseudoRandom(p.randomSeed + TEXTURE_ANIMATION_RAND_OFFSET))! + startFrame), 1));
}
}
}
}
// 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
// });