Improve particle sys performance by pre fill uv datas (#2733)

This commit is contained in:
pandamicro
2018-05-24 18:06:42 +08:00
committed by GitHub
parent ecf9a540d1
commit bbf047f574
3 changed files with 33 additions and 1031 deletions
-508
View File
@@ -13817,511 +13817,6 @@ IARenderData.type = 'IARenderData';
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
var BASE = 255;
var NOISE_SCALE = 10000;
var POS_SCALE = 1;
var LIFE_SCALE = 100;
var COLOR_SCALE = 1;
var ROTATION_SCALE = 1;
var MAX_SCALE = 500;
var _vertexFmt = new gfx.VertexFormat([
{ name: 'a_quad', type: gfx.ATTR_TYPE_FLOAT32, num: 2 }
]);
_vertexFmt.name = 'ParticlesVertexFormat';
var _dataOpts = {
width: 0,
height: 0,
minFilter: gfx.FILTER_NEAREST,
magFilter: gfx.FILTER_NEAREST,
wrapS: gfx.WRAP_CLAMP,
wrapT: gfx.WRAP_CLAMP,
format: gfx.TEXTURE_FMT_RGBA8,
images: []
};
function encode (value, scale, outArr, offset) {
value = value * scale + BASE * BASE / 2;
outArr[offset] = Math.floor((value % BASE) / BASE * 255);
outArr[offset+1] = Math.floor(Math.floor(value / BASE) / BASE * 255);
}
function decode (arr, offset, scale) {
return (((arr[offset] / 255) * BASE +
(arr[offset+1] / 255) * BASE * BASE) - BASE * BASE / 2) / scale;
}
function clampf (value, min_inclusive, max_inclusive) {
return value < min_inclusive ? min_inclusive : value < max_inclusive ? value : max_inclusive;
}
var Particles = function Particles (device, renderer$$1, config) {
this._device = device;
this._config = config;
this._sizeScale = 1;
this._accelScale = 1;
this._radiusScale = 1;
var opts = {};
var programLib = renderer$$1._programLib;
this.programs = {
emitter: programLib.getProgram('vfx_emitter', opts),
update: programLib.getProgram('vfx_update', opts),
quad: programLib.getProgram('vfx_quad', opts),
};
this.textures = {
// need swap
state0: new gfx.Texture2D(device, opts),
state1: new gfx.Texture2D(device, opts),
// no swap needed
noise: new gfx.Texture2D(device, opts),
quads: new gfx.Texture2D(device, opts),
};
this.framebuffers = {
state0: new gfx.FrameBuffer(device, 0, 0, {
colors: [this.textures.state0]
}),
state1: new gfx.FrameBuffer(device, 0, 0, {
colors: [this.textures.state1]
}),
quads: new gfx.FrameBuffer(device, 0, 0, {
colors: [this.textures.quads]
})
};
var verts = new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]);
var indices = new Uint8Array([ 0, 1, 2, 1, 3, 2]);
this.buffers = {
updateVB: new gfx.VertexBuffer(device, _vertexFmt, gfx.USAGE_STATIC, verts, 4),
updateIB: new gfx.IndexBuffer(device, gfx.INDEX_FMT_UINT8, gfx.USAGE_STATIC, indices, 6),
indexes: null,
particleCache: null
};
this._elapsed = 0;
this._stopped = false;
this.updateMaxParticle(config);
this.updateSizeScale(config);
this.updateRadiusScale(config);
this.updateAccelScale(config);
// Vector uniforms
this._pos = new Float32Array([config.sourcePos.x, config.sourcePos.y]);
this._posVar = new Float32Array([config.posVar.x, config.posVar.y]);
this._gravity = new Float32Array([config.gravity.x, config.gravity.y]);
this._color = new Float32Array([config.startColor.r, config.startColor.g, config.startColor.b, config.startColor.a]);
this._colorVar = new Float32Array([config.startColorVar.r, config.startColorVar.g, config.startColorVar.b, config.startColorVar.a]);
this._endColor = new Float32Array([config.endColor.r, config.endColor.g, config.endColor.b, config.endColor.a]);
this._endColorVar = new Float32Array([config.endColorVar.r, config.endColorVar.g, config.endColorVar.b, config.endColorVar.a]);
};
var prototypeAccessors$10 = { vertexFormat: { configurable: true },particleCount: { configurable: true },stopped: { configurable: true },pos: { configurable: true },posVar: { configurable: true },gravity: { configurable: true },startColor: { configurable: true },startColorVar: { configurable: true },endColor: { configurable: true },endColorVar: { configurable: true } };
prototypeAccessors$10.vertexFormat.get = function () {
return _vertexFmt;
};
// TODO: it's too slow, need other approach to retrieve the real particle count
prototypeAccessors$10.particleCount.get = function () {
var particleCount = 0;
var device = this._device;
device.setFrameBuffer(this.framebuffers.state0);
var w = this.statesize[0], h = this.statesize[1],
tw = this._tw, th = this._th,
rgba = this.buffers.particleCache;
device._gl.readPixels(0, 0, w, h, device._gl.RGBA, device._gl.UNSIGNED_BYTE, rgba);
for (var y = 0; y < th; y++) {
for (var x = 0; x < tw; x++) {
var offset = y * 3 * tw * 12 + x * 12;
var rest = decode(rgba, offset, LIFE_SCALE);
if (rest > 0) {
particleCount++;
}
}
}
return particleCount;
};
prototypeAccessors$10.stopped.get = function () {
return this._stopped;
};
Particles.prototype.updateMaxParticle = function updateMaxParticle (config) {
var emissionMax = Math.floor(config.emissionRate * (config.life + config.lifeVar));
var maxParticle;
if (config.totalParticles > emissionMax) {
maxParticle = emissionMax;
this._emitVar = config.lifeVar;
}
else {
maxParticle = config.totalParticles;
this._emitVar = 0;
}
if (maxParticle !== this._maxParticle) {
this._maxParticle = maxParticle;
this._tw = Math.ceil(Math.sqrt(maxParticle));
this._th = Math.floor(Math.sqrt(maxParticle));
this.initTextures();
this.initBuffers();
}
};
prototypeAccessors$10.pos.set = function (pos) {
this._pos[0] = pos.x;
this._pos[1] = pos.y;
};
prototypeAccessors$10.posVar.set = function (posVar) {
this._posVar[0] = posVar.x;
this._posVar[1] = posVar.y;
};
prototypeAccessors$10.gravity.set = function (gravity) {
this._gravity[0] = gravity.x;
this._gravity[1] = gravity.y;
};
prototypeAccessors$10.startColor.set = function (color) {
this._color[0] = color.r;
this._color[1] = color.g;
this._color[2] = color.b;
this._color[3] = color.a;
};
prototypeAccessors$10.startColorVar.set = function (color) {
this._colorVar[0] = color.r;
this._colorVar[1] = color.g;
this._colorVar[2] = color.b;
this._colorVar[3] = color.a;
};
prototypeAccessors$10.endColor.set = function (color) {
this._endColor[0] = color.r;
this._endColor[1] = color.g;
this._endColor[2] = color.b;
this._endColor[3] = color.a;
};
prototypeAccessors$10.endColorVar.set = function (color) {
this._endColorVar[0] = color.r;
this._endColorVar[1] = color.g;
this._endColorVar[2] = color.b;
this._endColorVar[3] = color.a;
};
Particles.prototype._updateTex = function _updateTex (tex, data, width, height) {
_dataOpts.images.length = 1;
_dataOpts.images[0] = data;
_dataOpts.width = width;
_dataOpts.height = height;
tex.update(_dataOpts);
};
Particles.prototype._initNoise = function _initNoise () {
var tw = this._tw, th = this._th;
var w = (tw + 1) * 3, h = (th + 1) * 3;
var data = new Uint8Array(w * h * 4);
var offset = 0;
for (var y = 0; y < h; y++) {
for (var x = 0; x < w; x++) {
encode(Math.random(), NOISE_SCALE, data, offset);
encode(Math.random(), NOISE_SCALE, data, offset + 2);
offset += 4;
}
}
this._noisesize = new Float32Array([w, h]);
this._updateTex(this.textures.noise, data, w, h);
};
Particles.prototype.initTextures = function initTextures () {
var tw = this._tw, th = this._th;
this.statesize = new Float32Array([tw * 3, th * 3]);
this.quadsize = new Float32Array([tw * 2, th * 2]);
var data = new Uint8Array(tw * 3 * th * 3 * 4);
// decode([97, 97], 0, LIFE_SCALE); equals to -128, just for initalize a negative value in life
data.fill(97);
this._updateTex(this.textures.state0, data, tw * 3, th * 3);
this._updateTex(this.textures.state1, data, tw * 3, th * 3);
this._updateTex(this.textures.quads, null, tw * 2, th * 2);
this._initNoise();
};
Particles.prototype.initBuffers = function initBuffers () {
var w = this.quadsize[0], h = this.quadsize[1],
indexes = new Float32Array(w * h * 2),
i = 0;
for (var y = 0; y < h; y++) {
for (var x = 0; x < w; x++) {
indexes[i + 0] = x;
indexes[i + 1] = y;
i += 2;
}
}
this.buffers.indexes = indexes;
this.buffers.particleCache = new Uint8Array(this.statesize[0] * this.statesize[1] * 4);
};
Particles.prototype.updateSizeScale = function updateSizeScale (config) {
var size = config.startSize,
sizeVar = config.startSizeVar,
endSize = config.endSize,
endSizeVar = config.endSizeVar;
var scale = Math.floor(Math.pow(BASE, 2) / Math.max(size + sizeVar, endSize + endSizeVar));
this._sizeScale = clampf(scale, 1, MAX_SCALE);
};
Particles.prototype.updateAccelScale = function updateAccelScale (config) {
var radial = config.radialAccel,
radialVar = config.radialAccelVar,
tangential = config.tangentialAccel,
tangentialVar = config.tangentialAccelVar;
var accelScale = Math.floor(Math.pow(BASE, 2) / Math.max(Math.abs(radial) + radialVar, Math.abs(tangential) + tangentialVar) / 2);
this._accelScale = clampf(accelScale, 1, MAX_SCALE);
};
Particles.prototype.updateRadiusScale = function updateRadiusScale (config) {
var radius = config.startRadius,
radiusVar = config.startRadiusVar,
endRadius = config.endRadius,
endRadiusVar = config.endRadiusVar;
var scale = Math.floor(Math.pow(BASE, 2) / Math.max(Math.abs(radius) + radiusVar, Math.abs(endRadius) + endRadiusVar));
this._radiusScale = clampf(scale, 1, MAX_SCALE);
};
Particles.prototype.resetSystem = function resetSystem () {
this._stopped = false;
};
Particles.prototype.stopSystem = function stopSystem () {
this._stopped = true;
};
Particles.prototype.getState = function getState (framebuffer) {
var this$1 = this;
var device = this._device;
device.setFrameBuffer(framebuffer);
var w = this.statesize[0], h = this.statesize[1],
tw = this._tw, th = this._th,
rgba = new Uint8Array(w * h * 4);
device._gl.readPixels(0, 0, w, h, device._gl.RGBA, device._gl.UNSIGNED_BYTE, rgba);
var particles = [];
for (var y = 0; y < th; y++) {
for (var x = 0; x < tw; x++) {
var offset = y * 3 * tw * 12 + x * 12;
var colorI = offset + 1 * 4;
var deltaRGI = offset + 2 * 4;
var deltaBAI = offset + tw * 12;
var sizeI = offset + tw * 12 + 1 * 4;
var rotI = offset + tw * 12 + 2 * 4;
var control1I = offset + 2 * tw * 12;
var control2I = offset + 2 * tw * 12 + 1 * 4;
var posI = offset + 2 * tw * 12 + 2 * 4;
var rest = decode(rgba, offset, LIFE_SCALE);
var life = decode(rgba, offset + 2, LIFE_SCALE);
var color = {
r: rgba[colorI],
g: rgba[colorI+1],
b: rgba[colorI+2],
a: rgba[colorI+3]
};
var deltaColor = {
r: decode(rgba, deltaRGI, COLOR_SCALE),
g: decode(rgba, deltaRGI + 2, COLOR_SCALE),
b: decode(rgba, deltaBAI, COLOR_SCALE),
a: decode(rgba, deltaBAI + 2, COLOR_SCALE)
};
var size = decode(rgba, sizeI, this$1._sizeScale);
var deltaSize = decode(rgba, sizeI + 2, this$1._sizeScale);
var rot = decode(rgba, rotI, ROTATION_SCALE);
var deltaRot = decode(rgba, rotI + 2, ROTATION_SCALE);
var control = (void 0);
if (this$1._config.emitterMode === 0) {
control = [
decode(rgba, control1I, POS_SCALE),
decode(rgba, control1I + 2, POS_SCALE),
decode(rgba, control2I, this$1._accelScale),
decode(rgba, control2I + 2, this$1._accelScale)
];
}
else {
control = [
decode(rgba, control1I, ROTATION_SCALE),
decode(rgba, control1I + 2, this$1._radiusScale),
decode(rgba, control2I, ROTATION_SCALE),
decode(rgba, control2I + 2, this$1._radiusScale)
];
}
var pos = {
x: decode(rgba, posI, POS_SCALE),
y: decode(rgba, posI + 2, POS_SCALE)
};
particles.push({
rest: rest,
life: life,
color: color,
deltaColor: deltaColor,
size: size,
deltaSize: deltaSize,
rot: rot,
deltaRot: deltaRot,
control: control,
pos: pos
});
}
}
return particles;
};
Particles.prototype.getNoise = function getNoise () {
var device = this._device;
var framebuffer = new gfx.FrameBuffer(device, 0, 0, {
colors: [this.textures.noise]
});
device.setFrameBuffer(framebuffer);
var w = this._noisesize[0], h = this._noisesize[1],
rgba = new Uint8Array(w * h * 4);
device._gl.readPixels(0, 0, w, h, device._gl.RGBA, device._gl.UNSIGNED_BYTE, rgba);
var noises = [];
var offset = 0;
for (var y = 0; y < h; y++) {
for (var x = 0; x < w; x++) {
noises.push(decode(rgba, offset, 10000));
noises.push(decode(rgba, offset+2, 10000));
offset += 4;
}
}
return noises;
};
Particles.prototype.getQuads = function getQuads () {
var device = this._device;
var framebuffer = new gfx.FrameBuffer(device, 0, 0, {
colors: [this.textures.quads]
});
device.setFrameBuffer(framebuffer);
var w = this.quadsize[0], h = this.quadsize[1],
rgba = new Uint8Array(w * h * 4);
device._gl.readPixels(0, 0, w, h, device._gl.RGBA, device._gl.UNSIGNED_BYTE, rgba);
var quads = [];
var off1 = 0, off2 = 0;
for (var y = 0; y < h; y += 2) {
for (var x = 0; x < w; x += 2) {
off1 = (y * w + x) * 4;
off2 = ((y + 1) * w + x) * 4;
quads.push([
decode(rgba, off1, POS_SCALE),
decode(rgba, off1+2, POS_SCALE),
decode(rgba, off1+4, POS_SCALE),
decode(rgba, off1+6, POS_SCALE),
decode(rgba, off2, POS_SCALE),
decode(rgba, off2+2, POS_SCALE),
decode(rgba, off2+4, POS_SCALE),
decode(rgba, off2+6, POS_SCALE)
]);
}
}
return quads;
};
Particles.prototype.step = function step (dt) {
var config = this._config;
var device = this._device;
// setup
device.setViewport(0, 0, this.statesize[0], this.statesize[1]);
// Emitter
device.setFrameBuffer(this.framebuffers.state1);
device.setTexture('noise', this.textures.noise, 0);
device.setTexture('state', this.textures.state0, 1);
device.setUniform('statesize', this.statesize);
device.setUniform('noisesize', this._noisesize);
device.setUniform('stopped', this._stopped);
device.setUniform('dt', dt);
device.setUniform('mode', config.emitterMode);
device.setUniform('noiseId', Math.floor(Math.random() * 16));
device.setUniform('emitVar', this._emitVar);
device.setUniform('life', config.life);
device.setUniform('lifeVar', config.lifeVar);
device.setUniform('pos', this._pos);
device.setUniform('posVar', this._posVar);
device.setUniform('color', this._color);
device.setUniform('colorVar', this._colorVar);
device.setUniform('endColor', this._endColor);
device.setUniform('endColorVar', this._endColorVar);
device.setUniform('size', config.startSize);
device.setUniform('sizeVar', config.startSizeVar);
device.setUniform('endSize', config.endSize);
device.setUniform('endSizeVar', config.endSizeVar);
device.setUniform('rot', config.startSpin);
device.setUniform('rotVar', config.startSpinVar);
device.setUniform('endRot', config.endSpin);
device.setUniform('endRotVar', config.endSpinVar);
device.setUniform('angle', config.angle);
device.setUniform('angleVar', config.angleVar);
device.setUniform('speed', config.speed);
device.setUniform('speedVar', config.speedVar);
device.setUniform('radial', config.radialAccel);
device.setUniform('radialVar', config.radialAccelVar);
device.setUniform('tangent', config.tangentialAccel);
device.setUniform('tangentVar', config.tangentialAccelVar);
device.setUniform('radius', config.startRadius);
device.setUniform('radiusVar', config.startRadiusVar);
device.setUniform('endRadius', config.endRadius);
device.setUniform('endRadiusVar', config.endRadiusVar);
device.setUniform('rotatePS', config.rotatePerS);
device.setUniform('rotatePSVar', config.rotatePerSVar);
device.setUniform('sizeScale', this._sizeScale);
device.setUniform('accelScale', this._accelScale);
device.setUniform('radiusScale', this._radiusScale);
device.setVertexBuffer(0, this.buffers.updateVB);
device.setIndexBuffer(this.buffers.updateIB);
device.setProgram(this.programs.emitter);
device.draw(0, this.buffers.updateIB.count);
// Update
device.setFrameBuffer(this.framebuffers.state0);
device.setTexture('state', this.textures.state1, 0);
device.setUniform('statesize', this.statesize);
device.setUniform('dt', dt);
device.setUniform('mode', config.emitterMode);
device.setUniform('gravity', this._gravity);
device.setUniform('sizeScale', this._sizeScale);
device.setUniform('accelScale', this._accelScale);
device.setUniform('radiusScale', this._radiusScale);
device.setVertexBuffer(0, this.buffers.updateVB);
device.setIndexBuffer(this.buffers.updateIB);
device.setProgram(this.programs.update);
device.draw(0, this.buffers.updateIB.count);
// Draw quad
device.setViewport(0, 0, this._tw * 2, this._th * 2);
device.setFrameBuffer(this.framebuffers.quads);
device.setTexture('state', this.textures.state0, 0);
device.setUniform('quadsize', this.quadsize);
device.setUniform('statesize', this.statesize);
device.setUniform('sizeScale', this._sizeScale);
device.setVertexBuffer(0, this.buffers.updateVB);
device.setIndexBuffer(this.buffers.updateIB);
device.setProgram(this.programs.quad);
device.draw(0, this.buffers.updateIB.count);
this._elapsed += dt;
if (config.duration !== -1 && config.duration < this._elapsed) {
this.stopSystem();
}
};
Object.defineProperties( Particles.prototype, prototypeAccessors$10 );
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
var Asset = function Asset(persist) {
if ( persist === void 0 ) persist = true;
@@ -15023,9 +14518,6 @@ var renderEngine = {
RenderData: RenderData,
IARenderData: IARenderData,
InputAssembler: InputAssembler$2,
// vfx
Particles: Particles,
// assets
Asset: Asset,
+4 -512
View File
@@ -1462,515 +1462,6 @@ var IARenderData = (function (BaseRenderData$$1) {
IARenderData.type = 'IARenderData';
var gfx = window.gfx;
var renderer = window.renderer;
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
var BASE = 255;
var NOISE_SCALE = 10000;
var POS_SCALE = 1;
var LIFE_SCALE = 100;
var COLOR_SCALE = 1;
var ROTATION_SCALE = 1;
var MAX_SCALE = 500;
var _vertexFmt = new gfx.VertexFormat([
{ name: 'a_quad', type: gfx.ATTR_TYPE_FLOAT32, num: 2 }
]);
_vertexFmt.name = 'ParticlesVertexFormat';
var _dataOpts = {
width: 0,
height: 0,
minFilter: gfx.FILTER_NEAREST,
magFilter: gfx.FILTER_NEAREST,
wrapS: gfx.WRAP_CLAMP,
wrapT: gfx.WRAP_CLAMP,
format: gfx.TEXTURE_FMT_RGBA8,
images: []
};
function encode (value, scale, outArr, offset) {
value = value * scale + BASE * BASE / 2;
outArr[offset] = Math.floor((value % BASE) / BASE * 255);
outArr[offset+1] = Math.floor(Math.floor(value / BASE) / BASE * 255);
}
function decode (arr, offset, scale) {
return (((arr[offset] / 255) * BASE +
(arr[offset+1] / 255) * BASE * BASE) - BASE * BASE / 2) / scale;
}
function clampf (value, min_inclusive, max_inclusive) {
return value < min_inclusive ? min_inclusive : value < max_inclusive ? value : max_inclusive;
}
var Particles = function Particles (device, renderer$$1, config) {
this._device = device;
this._config = config;
this._sizeScale = 1;
this._accelScale = 1;
this._radiusScale = 1;
var opts = {};
var programLib = renderer$$1._programLib;
this.programs = {
emitter: programLib.getProgram('vfx_emitter', opts),
update: programLib.getProgram('vfx_update', opts),
quad: programLib.getProgram('vfx_quad', opts),
};
this.textures = {
// need swap
state0: new gfx.Texture2D(device, opts),
state1: new gfx.Texture2D(device, opts),
// no swap needed
noise: new gfx.Texture2D(device, opts),
quads: new gfx.Texture2D(device, opts),
};
this.framebuffers = {
state0: new gfx.FrameBuffer(device, 0, 0, {
colors: [this.textures.state0]
}),
state1: new gfx.FrameBuffer(device, 0, 0, {
colors: [this.textures.state1]
}),
quads: new gfx.FrameBuffer(device, 0, 0, {
colors: [this.textures.quads]
})
};
var verts = new Float32Array([-1, -1, 1, -1, -1, 1, 1, 1]);
var indices = new Uint8Array([ 0, 1, 2, 1, 3, 2]);
this.buffers = {
updateVB: new gfx.VertexBuffer(device, _vertexFmt, gfx.USAGE_STATIC, verts, 4),
updateIB: new gfx.IndexBuffer(device, gfx.INDEX_FMT_UINT8, gfx.USAGE_STATIC, indices, 6),
indexes: null,
particleCache: null
};
this._elapsed = 0;
this._stopped = false;
this.updateMaxParticle(config);
this.updateSizeScale(config);
this.updateRadiusScale(config);
this.updateAccelScale(config);
// Vector uniforms
this._pos = new Float32Array([config.sourcePos.x, config.sourcePos.y]);
this._posVar = new Float32Array([config.posVar.x, config.posVar.y]);
this._gravity = new Float32Array([config.gravity.x, config.gravity.y]);
this._color = new Float32Array([config.startColor.r, config.startColor.g, config.startColor.b, config.startColor.a]);
this._colorVar = new Float32Array([config.startColorVar.r, config.startColorVar.g, config.startColorVar.b, config.startColorVar.a]);
this._endColor = new Float32Array([config.endColor.r, config.endColor.g, config.endColor.b, config.endColor.a]);
this._endColorVar = new Float32Array([config.endColorVar.r, config.endColorVar.g, config.endColorVar.b, config.endColorVar.a]);
};
var prototypeAccessors$3 = { vertexFormat: { configurable: true },particleCount: { configurable: true },stopped: { configurable: true },pos: { configurable: true },posVar: { configurable: true },gravity: { configurable: true },startColor: { configurable: true },startColorVar: { configurable: true },endColor: { configurable: true },endColorVar: { configurable: true } };
prototypeAccessors$3.vertexFormat.get = function () {
return _vertexFmt;
};
// TODO: it's too slow, need other approach to retrieve the real particle count
prototypeAccessors$3.particleCount.get = function () {
var particleCount = 0;
var device = this._device;
device.setFrameBuffer(this.framebuffers.state0);
var w = this.statesize[0], h = this.statesize[1],
tw = this._tw, th = this._th,
rgba = this.buffers.particleCache;
device._gl.readPixels(0, 0, w, h, device._gl.RGBA, device._gl.UNSIGNED_BYTE, rgba);
for (var y = 0; y < th; y++) {
for (var x = 0; x < tw; x++) {
var offset = y * 3 * tw * 12 + x * 12;
var rest = decode(rgba, offset, LIFE_SCALE);
if (rest > 0) {
particleCount++;
}
}
}
return particleCount;
};
prototypeAccessors$3.stopped.get = function () {
return this._stopped;
};
Particles.prototype.updateMaxParticle = function updateMaxParticle (config) {
var emissionMax = Math.floor(config.emissionRate * (config.life + config.lifeVar));
var maxParticle;
if (config.totalParticles > emissionMax) {
maxParticle = emissionMax;
this._emitVar = config.lifeVar;
}
else {
maxParticle = config.totalParticles;
this._emitVar = 0;
}
if (maxParticle !== this._maxParticle) {
this._maxParticle = maxParticle;
this._tw = Math.ceil(Math.sqrt(maxParticle));
this._th = Math.floor(Math.sqrt(maxParticle));
this.initTextures();
this.initBuffers();
}
};
prototypeAccessors$3.pos.set = function (pos) {
this._pos[0] = pos.x;
this._pos[1] = pos.y;
};
prototypeAccessors$3.posVar.set = function (posVar) {
this._posVar[0] = posVar.x;
this._posVar[1] = posVar.y;
};
prototypeAccessors$3.gravity.set = function (gravity) {
this._gravity[0] = gravity.x;
this._gravity[1] = gravity.y;
};
prototypeAccessors$3.startColor.set = function (color) {
this._color[0] = color.r;
this._color[1] = color.g;
this._color[2] = color.b;
this._color[3] = color.a;
};
prototypeAccessors$3.startColorVar.set = function (color) {
this._colorVar[0] = color.r;
this._colorVar[1] = color.g;
this._colorVar[2] = color.b;
this._colorVar[3] = color.a;
};
prototypeAccessors$3.endColor.set = function (color) {
this._endColor[0] = color.r;
this._endColor[1] = color.g;
this._endColor[2] = color.b;
this._endColor[3] = color.a;
};
prototypeAccessors$3.endColorVar.set = function (color) {
this._endColorVar[0] = color.r;
this._endColorVar[1] = color.g;
this._endColorVar[2] = color.b;
this._endColorVar[3] = color.a;
};
Particles.prototype._updateTex = function _updateTex (tex, data, width, height) {
_dataOpts.images.length = 1;
_dataOpts.images[0] = data;
_dataOpts.width = width;
_dataOpts.height = height;
tex.update(_dataOpts);
};
Particles.prototype._initNoise = function _initNoise () {
var tw = this._tw, th = this._th;
var w = (tw + 1) * 3, h = (th + 1) * 3;
var data = new Uint8Array(w * h * 4);
var offset = 0;
for (var y = 0; y < h; y++) {
for (var x = 0; x < w; x++) {
encode(Math.random(), NOISE_SCALE, data, offset);
encode(Math.random(), NOISE_SCALE, data, offset + 2);
offset += 4;
}
}
this._noisesize = new Float32Array([w, h]);
this._updateTex(this.textures.noise, data, w, h);
};
Particles.prototype.initTextures = function initTextures () {
var tw = this._tw, th = this._th;
this.statesize = new Float32Array([tw * 3, th * 3]);
this.quadsize = new Float32Array([tw * 2, th * 2]);
var data = new Uint8Array(tw * 3 * th * 3 * 4);
// decode([97, 97], 0, LIFE_SCALE); equals to -128, just for initalize a negative value in life
data.fill(97);
this._updateTex(this.textures.state0, data, tw * 3, th * 3);
this._updateTex(this.textures.state1, data, tw * 3, th * 3);
this._updateTex(this.textures.quads, null, tw * 2, th * 2);
this._initNoise();
};
Particles.prototype.initBuffers = function initBuffers () {
var w = this.quadsize[0], h = this.quadsize[1],
indexes = new Float32Array(w * h * 2),
i = 0;
for (var y = 0; y < h; y++) {
for (var x = 0; x < w; x++) {
indexes[i + 0] = x;
indexes[i + 1] = y;
i += 2;
}
}
this.buffers.indexes = indexes;
this.buffers.particleCache = new Uint8Array(this.statesize[0] * this.statesize[1] * 4);
};
Particles.prototype.updateSizeScale = function updateSizeScale (config) {
var size = config.startSize,
sizeVar = config.startSizeVar,
endSize = config.endSize,
endSizeVar = config.endSizeVar;
var scale = Math.floor(Math.pow(BASE, 2) / Math.max(size + sizeVar, endSize + endSizeVar));
this._sizeScale = clampf(scale, 1, MAX_SCALE);
};
Particles.prototype.updateAccelScale = function updateAccelScale (config) {
var radial = config.radialAccel,
radialVar = config.radialAccelVar,
tangential = config.tangentialAccel,
tangentialVar = config.tangentialAccelVar;
var accelScale = Math.floor(Math.pow(BASE, 2) / Math.max(Math.abs(radial) + radialVar, Math.abs(tangential) + tangentialVar) / 2);
this._accelScale = clampf(accelScale, 1, MAX_SCALE);
};
Particles.prototype.updateRadiusScale = function updateRadiusScale (config) {
var radius = config.startRadius,
radiusVar = config.startRadiusVar,
endRadius = config.endRadius,
endRadiusVar = config.endRadiusVar;
var scale = Math.floor(Math.pow(BASE, 2) / Math.max(Math.abs(radius) + radiusVar, Math.abs(endRadius) + endRadiusVar));
this._radiusScale = clampf(scale, 1, MAX_SCALE);
};
Particles.prototype.resetSystem = function resetSystem () {
this._stopped = false;
};
Particles.prototype.stopSystem = function stopSystem () {
this._stopped = true;
};
Particles.prototype.getState = function getState (framebuffer) {
var this$1 = this;
var device = this._device;
device.setFrameBuffer(framebuffer);
var w = this.statesize[0], h = this.statesize[1],
tw = this._tw, th = this._th,
rgba = new Uint8Array(w * h * 4);
device._gl.readPixels(0, 0, w, h, device._gl.RGBA, device._gl.UNSIGNED_BYTE, rgba);
var particles = [];
for (var y = 0; y < th; y++) {
for (var x = 0; x < tw; x++) {
var offset = y * 3 * tw * 12 + x * 12;
var colorI = offset + 1 * 4;
var deltaRGI = offset + 2 * 4;
var deltaBAI = offset + tw * 12;
var sizeI = offset + tw * 12 + 1 * 4;
var rotI = offset + tw * 12 + 2 * 4;
var control1I = offset + 2 * tw * 12;
var control2I = offset + 2 * tw * 12 + 1 * 4;
var posI = offset + 2 * tw * 12 + 2 * 4;
var rest = decode(rgba, offset, LIFE_SCALE);
var life = decode(rgba, offset + 2, LIFE_SCALE);
var color = {
r: rgba[colorI],
g: rgba[colorI+1],
b: rgba[colorI+2],
a: rgba[colorI+3]
};
var deltaColor = {
r: decode(rgba, deltaRGI, COLOR_SCALE),
g: decode(rgba, deltaRGI + 2, COLOR_SCALE),
b: decode(rgba, deltaBAI, COLOR_SCALE),
a: decode(rgba, deltaBAI + 2, COLOR_SCALE)
};
var size = decode(rgba, sizeI, this$1._sizeScale);
var deltaSize = decode(rgba, sizeI + 2, this$1._sizeScale);
var rot = decode(rgba, rotI, ROTATION_SCALE);
var deltaRot = decode(rgba, rotI + 2, ROTATION_SCALE);
var control = (void 0);
if (this$1._config.emitterMode === 0) {
control = [
decode(rgba, control1I, POS_SCALE),
decode(rgba, control1I + 2, POS_SCALE),
decode(rgba, control2I, this$1._accelScale),
decode(rgba, control2I + 2, this$1._accelScale)
];
}
else {
control = [
decode(rgba, control1I, ROTATION_SCALE),
decode(rgba, control1I + 2, this$1._radiusScale),
decode(rgba, control2I, ROTATION_SCALE),
decode(rgba, control2I + 2, this$1._radiusScale)
];
}
var pos = {
x: decode(rgba, posI, POS_SCALE),
y: decode(rgba, posI + 2, POS_SCALE)
};
particles.push({
rest: rest,
life: life,
color: color,
deltaColor: deltaColor,
size: size,
deltaSize: deltaSize,
rot: rot,
deltaRot: deltaRot,
control: control,
pos: pos
});
}
}
return particles;
};
Particles.prototype.getNoise = function getNoise () {
var device = this._device;
var framebuffer = new gfx.FrameBuffer(device, 0, 0, {
colors: [this.textures.noise]
});
device.setFrameBuffer(framebuffer);
var w = this._noisesize[0], h = this._noisesize[1],
rgba = new Uint8Array(w * h * 4);
device._gl.readPixels(0, 0, w, h, device._gl.RGBA, device._gl.UNSIGNED_BYTE, rgba);
var noises = [];
var offset = 0;
for (var y = 0; y < h; y++) {
for (var x = 0; x < w; x++) {
noises.push(decode(rgba, offset, 10000));
noises.push(decode(rgba, offset+2, 10000));
offset += 4;
}
}
return noises;
};
Particles.prototype.getQuads = function getQuads () {
var device = this._device;
var framebuffer = new gfx.FrameBuffer(device, 0, 0, {
colors: [this.textures.quads]
});
device.setFrameBuffer(framebuffer);
var w = this.quadsize[0], h = this.quadsize[1],
rgba = new Uint8Array(w * h * 4);
device._gl.readPixels(0, 0, w, h, device._gl.RGBA, device._gl.UNSIGNED_BYTE, rgba);
var quads = [];
var off1 = 0, off2 = 0;
for (var y = 0; y < h; y += 2) {
for (var x = 0; x < w; x += 2) {
off1 = (y * w + x) * 4;
off2 = ((y + 1) * w + x) * 4;
quads.push([
decode(rgba, off1, POS_SCALE),
decode(rgba, off1+2, POS_SCALE),
decode(rgba, off1+4, POS_SCALE),
decode(rgba, off1+6, POS_SCALE),
decode(rgba, off2, POS_SCALE),
decode(rgba, off2+2, POS_SCALE),
decode(rgba, off2+4, POS_SCALE),
decode(rgba, off2+6, POS_SCALE)
]);
}
}
return quads;
};
Particles.prototype.step = function step (dt) {
var config = this._config;
var device = this._device;
// setup
device.setViewport(0, 0, this.statesize[0], this.statesize[1]);
// Emitter
device.setFrameBuffer(this.framebuffers.state1);
device.setTexture('noise', this.textures.noise, 0);
device.setTexture('state', this.textures.state0, 1);
device.setUniform('statesize', this.statesize);
device.setUniform('noisesize', this._noisesize);
device.setUniform('stopped', this._stopped);
device.setUniform('dt', dt);
device.setUniform('mode', config.emitterMode);
device.setUniform('noiseId', Math.floor(Math.random() * 16));
device.setUniform('emitVar', this._emitVar);
device.setUniform('life', config.life);
device.setUniform('lifeVar', config.lifeVar);
device.setUniform('pos', this._pos);
device.setUniform('posVar', this._posVar);
device.setUniform('color', this._color);
device.setUniform('colorVar', this._colorVar);
device.setUniform('endColor', this._endColor);
device.setUniform('endColorVar', this._endColorVar);
device.setUniform('size', config.startSize);
device.setUniform('sizeVar', config.startSizeVar);
device.setUniform('endSize', config.endSize);
device.setUniform('endSizeVar', config.endSizeVar);
device.setUniform('rot', config.startSpin);
device.setUniform('rotVar', config.startSpinVar);
device.setUniform('endRot', config.endSpin);
device.setUniform('endRotVar', config.endSpinVar);
device.setUniform('angle', config.angle);
device.setUniform('angleVar', config.angleVar);
device.setUniform('speed', config.speed);
device.setUniform('speedVar', config.speedVar);
device.setUniform('radial', config.radialAccel);
device.setUniform('radialVar', config.radialAccelVar);
device.setUniform('tangent', config.tangentialAccel);
device.setUniform('tangentVar', config.tangentialAccelVar);
device.setUniform('radius', config.startRadius);
device.setUniform('radiusVar', config.startRadiusVar);
device.setUniform('endRadius', config.endRadius);
device.setUniform('endRadiusVar', config.endRadiusVar);
device.setUniform('rotatePS', config.rotatePerS);
device.setUniform('rotatePSVar', config.rotatePerSVar);
device.setUniform('sizeScale', this._sizeScale);
device.setUniform('accelScale', this._accelScale);
device.setUniform('radiusScale', this._radiusScale);
device.setVertexBuffer(0, this.buffers.updateVB);
device.setIndexBuffer(this.buffers.updateIB);
device.setProgram(this.programs.emitter);
device.draw(0, this.buffers.updateIB.count);
// Update
device.setFrameBuffer(this.framebuffers.state0);
device.setTexture('state', this.textures.state1, 0);
device.setUniform('statesize', this.statesize);
device.setUniform('dt', dt);
device.setUniform('mode', config.emitterMode);
device.setUniform('gravity', this._gravity);
device.setUniform('sizeScale', this._sizeScale);
device.setUniform('accelScale', this._accelScale);
device.setUniform('radiusScale', this._radiusScale);
device.setVertexBuffer(0, this.buffers.updateVB);
device.setIndexBuffer(this.buffers.updateIB);
device.setProgram(this.programs.update);
device.draw(0, this.buffers.updateIB.count);
// Draw quad
device.setViewport(0, 0, this._tw * 2, this._th * 2);
device.setFrameBuffer(this.framebuffers.quads);
device.setTexture('state', this.textures.state0, 0);
device.setUniform('quadsize', this.quadsize);
device.setUniform('statesize', this.statesize);
device.setUniform('sizeScale', this._sizeScale);
device.setVertexBuffer(0, this.buffers.updateVB);
device.setIndexBuffer(this.buffers.updateIB);
device.setProgram(this.programs.quad);
device.draw(0, this.buffers.updateIB.count);
this._elapsed += dt;
if (config.duration !== -1 && config.duration < this._elapsed) {
this.stopSystem();
}
};
Object.defineProperties( Particles.prototype, prototypeAccessors$3 );
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
var Asset = function Asset(persist) {
@@ -2067,6 +1558,8 @@ function murmurhash2_32_gc(str, seed) {
return h >>> 0;
}
var renderer = window.renderer;
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
// function genHashCode (str) {
@@ -2208,6 +1701,8 @@ var Material = (function (Asset$$1) {
return Material;
}(Asset));
var gfx = window.gfx;
// Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
var SpriteMaterial = (function (Material$$1) {
@@ -9950,9 +9445,6 @@ var renderEngine = {
RenderData: RenderData,
IARenderData: IARenderData,
InputAssembler: InputAssembler,
// vfx
Particles: Particles,
// assets
Asset: Asset,
+29 -11
View File
@@ -90,6 +90,7 @@ let Simulator = function (system) {
this.finished = false;
this.elapsed = 0;
this.emitCounter = 0;
this._uvFilled = 0;
}
Simulator.prototype.stop = function () {
@@ -190,10 +191,30 @@ Simulator.prototype.emitParticle = function (pos) {
}
};
Simulator.prototype.updateUVs = function (particleCount) {
if (this.sys._buffer && this.sys._spriteFrame) {
const FLOAT_PER_PARTICLE = 4 * this.sys._vertexFormat._bytes / 4;
let vbuf = this.sys._buffer._vData;
let uv = this.sys._spriteFrame.uv;
for (let i = this._uvFilled; i < particleCount; i++) {
let offset = i * FLOAT_PER_PARTICLE;
vbuf[offset+2] = uv[0];
vbuf[offset+3] = uv[1];
vbuf[offset+7] = uv[2];
vbuf[offset+8] = uv[3];
vbuf[offset+12] = uv[4];
vbuf[offset+13] = uv[5];
vbuf[offset+17] = uv[6];
vbuf[offset+18] = uv[7];
}
this._uvFilled = particleCount;
}
}
Simulator.prototype.updateParticleBuffer = function (particle, pos, buffer, offset) {
let vbuf = buffer._vData;
let uintbuf = buffer._uintVData;
let uv = this.sys._spriteFrame.uv;
let x = pos.x, y = pos.y;
let size_2 = particle.size / 2;
@@ -230,15 +251,6 @@ Simulator.prototype.updateParticleBuffer = function (particle, pos, buffer, offs
vbuf[offset+15] = x + size_2;
vbuf[offset+16] = y + size_2;
}
// uv
vbuf[offset+2] = uv[0];
vbuf[offset+3] = uv[1];
vbuf[offset+7] = uv[2];
vbuf[offset+8] = uv[3];
vbuf[offset+12] = uv[4];
vbuf[offset+13] = uv[5];
vbuf[offset+17] = uv[6];
vbuf[offset+18] = uv[7];
// color
uintbuf[offset+4] = particle.color._val;
uintbuf[offset+9] = particle.color._val;
@@ -285,8 +297,14 @@ Simulator.prototype.step = function (dt) {
// Request buffer for particles
let buffer = psys._buffer;
let particleCount = particles.length;
buffer.reset();
buffer.request(particles.length * 4, particles.length * 6);
buffer.request(particleCount * 4, particles.length * 6);
// Fill up uvs
if (particleCount > this._uvFilled) {
this.updateUVs(particleCount);
}
// Used to reduce memory allocation / creation within the loop
let particleIdx = 0;