From a4d49b90d2a4861d6e387a01efeff400c3e48448 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 7 Feb 2018 18:09:45 +0800 Subject: [PATCH 1/3] Node use internal quaternion instead of vec3 rot --- cocos2d/core/CCNode.js | 71 ++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 38 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index b9e4135a60..c027cd9e81 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -52,8 +52,6 @@ var ActionManagerExist = !!cc.ActionManager; var emptyFunc = function () {}; var _mat4_temp = math.mat4.create(); var _vec3_temp = math.vec3.create(); -// quaternion for rotation -var _quat = math.quat.new(0, 0, 0, 1); var _globalOrderOfArrival = 1; /** @@ -379,15 +377,8 @@ var Node = cc.Class({ type: cc.Float }, _scale: cc.Vec3, - _rotationX: { - default: undefined, - type: cc.Float - }, - _rotationY: { - default: undefined, - type: cc.Float - }, - _rot: cc.Vec3, + _rotationX: 0.0, + _rotationY: 0.0, _skewX: 0.0, _skewY: 0.0, _localZOrder: 0, @@ -546,13 +537,15 @@ var Node = cc.Class({ */ rotation: { get () { - if (this._rot.x !== this._rot.y) + if (this._rotationX !== this._rotationY) cc.logID(1602); - return this._rot.z; + return this._rotationX; }, set (value) { - if (this._rot.z !== value) { - this._rot.z = value; + if (this._rotationX !== value || this._rotationY !== value) { + this._rotationX = this._rotationY = value; + // Update quaternion from rotation + math.quat.fromEuler(this._quat, 0, 0, this._rotationX); this._localMatDirty = true; var cache = this._hasListenerCache; @@ -574,11 +567,18 @@ var Node = cc.Class({ */ rotationX: { get () { - return this._rot.x; + return this._rotationX; }, set (value) { - if (this._rot.x !== value) { - this._rot.x = value; + if (this._rotationX !== value) { + this._rotationX = value; + // Update quaternion from rotation + if (this._rotationX === this._rotationY) { + math.quat.fromEuler(this._quat, 0, 0, this._rotationX); + } + else { + math.quat.fromEuler(this._quat, this._rotationX, this._rotationY, 0); + } this._localMatDirty = true; var cache = this._hasListenerCache; @@ -600,11 +600,18 @@ var Node = cc.Class({ */ rotationY: { get () { - return this._rot.y; + return this._rotationY; }, set (value) { - if (this._rot.y !== value) { - this._rot.y = value; + if (this._rotationY !== value) { + this._rotationY = value; + // Update quaternion from rotation + if (this._rotationX === this._rotationY) { + math.quat.fromEuler(this._quat, 0, 0, this._rotationX); + } + else { + math.quat.fromEuler(this._quat, this._rotationX, this._rotationY, 0); + } this._localMatDirty = true; var cache = this._hasListenerCache; @@ -913,6 +920,8 @@ var Node = cc.Class({ this._scale.x = 1; this._scale.y = 1; this._scale.z = 1; + // Quaternion for rotation + this._quat = mathPools.quat.get(); this._matrix = mathPools.mat4.get(); this._worldMatrix = mathPools.mat4.get(); @@ -967,6 +976,7 @@ var Node = cc.Class({ } // Recycle math objects + mathPools.quat.put(this._quat); mathPools.mat4.put(this._matrix); mathPools.mat4.put(this._worldMatrix); @@ -1031,20 +1041,7 @@ var Node = cc.Class({ } // Upgrade rotationX, rotationY from v1.x // TODO: remove in future version, 3.0 ? - if (this._rotationX !== undefined) { - if (this._rotationX === this._rotationY) { - this._rot.z = this._rotationX; - this._rotationX = undefined; - this._rotationY = undefined; - } - else { - this._rot.x = this._rotationX; - this._rotationX = undefined; - this._rot.y = this._rotationY || 0; - this._rotationY = undefined; - this._rot.z = 0; - } - } + var prefabInfo = this._prefab; @@ -1850,11 +1847,9 @@ var Node = cc.Class({ _updateLocalMatrix () { if (this._localMatDirty) { - // Update quaternion from rotation - math.quat.fromEuler(_quat, this._rot.x, this._rot.y, this._rot.z); // Update transform let t = this._matrix; - math.mat4.fromRTS(t, _quat, this._position, this._scale); + math.mat4.fromRTS(t, this._quat, this._position, this._scale); // skew if (this._skewX || this._skewY) { From e73a74c0ae6488d4de2b758dcbe0693738dfa286 Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 7 Feb 2018 18:10:01 +0800 Subject: [PATCH 2/3] Add Node z setter --- cocos2d/core/CCNode.js | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index c027cd9e81..66b4fa5cf8 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -523,7 +523,19 @@ var Node = cc.Class({ z: { get () { return this._position.z; - } + }, + set (value) { + var localPosition = this._position; + if (value !== localPosition.z) { + if (!CC_EDITOR || isFinite(value)) { + localPosition.z = value; + this._localMatDirty = true; + } + else { + cc.error(ERR_INVALID_NUMBER, 'new z'); + } + } + }, }, /** From b4c59387ed3b41e4a111fa2d19263e2fd3cdd16b Mon Sep 17 00:00:00 2001 From: pandamicro Date: Wed, 7 Feb 2018 18:11:21 +0800 Subject: [PATCH 3/3] Use 3d camera instead of old camera --- cocos2d/core/CCNode.js | 74 +++++++ cocos2d/core/renderer/index.js | 30 ++- cocos2d/core/renderer/render-engine.js | 278 ++++++++----------------- cocos2d/core/utils/math-pools.js | 55 +++-- 4 files changed, 210 insertions(+), 227 deletions(-) diff --git a/cocos2d/core/CCNode.js b/cocos2d/core/CCNode.js index 66b4fa5cf8..725e01794e 100644 --- a/cocos2d/core/CCNode.js +++ b/cocos2d/core/CCNode.js @@ -52,6 +52,7 @@ var ActionManagerExist = !!cc.ActionManager; var emptyFunc = function () {}; var _mat4_temp = math.mat4.create(); var _vec3_temp = math.vec3.create(); +var _quat_temp = math.quat.create(); var _globalOrderOfArrival = 1; /** @@ -1626,7 +1627,80 @@ var Node = cc.Class({ } } }, + + getWorldPos (out) { + math.vec3.copy(out, this._position); + let curr = this._parent; + while (curr) { + // out = parent_scale * pos + math.vec3.mul(out, out, curr._scale); + // out = parent_quat * out + math.vec3.transformQuat(out, out, curr._quat); + // out = out + pos + math.vec3.add(out, out, curr._position); + curr = curr._parent; + } + return out; + }, + getWorldRot (out) { + math.quat.copy(out, this._quat); + let curr = this._parent; + while (curr) { + math.quat.mul(out, curr._quat, out); + curr = curr._parent; + } + return out; + }, + + setWorldRot (quat) { + if (this._parent) { + this._parent.getWorldRot(this._quat); + math.quat.conjugate(this._quat, this._quat); + math.quat.mul(this._quat, this._quat, quat); + return; + } + math.quat.copy(this._quat, quat); + }, + + getWorldRT (out) { + let opos = _vec3_temp; + let orot = _quat_temp; + math.vec3.copy(opos, this._position); + math.quat.copy(orot, this._quat); + + let curr = this._parent; + while (curr) { + // opos = parent_lscale * lpos + math.vec3.mul(opos, opos, curr._scale); + // opos = parent_lrot * opos + math.vec3.transformQuat(opos, opos, curr._quat); + // opos = opos + lpos + math.vec3.add(opos, opos, curr._position); + // orot = lrot * orot + math.quat.mul(orot, curr._quat, orot); + curr = curr._parent; + } + math.mat4.fromRT(out, orot, opos); + return out; + }, + + /** + * !#en Set rotation by lookAt target point, normally used by Camera Node + * !#zh 通过观察目标来设置 rotation,一般用于 Camera Node 上 + * @method lookAt + * @param {vec3} pos + * @param {vec3} [up] - default is (0,1,0) + */ + lookAt (pos, up) { + this.getWorldPos(_vec3_temp); + math.vec3.sub(_vec3_temp, _vec3_temp, pos); // NOTE: we use -z for view-dir + math.vec3.normalize(_vec3_temp, _vec3_temp); + math.quat.fromViewUp(_quat_temp, _vec3_temp, up); + + this.setWorldRot(_quat_temp); + }, + /** * !#en * Returns a copy the untransformed size of the node.
diff --git a/cocos2d/core/renderer/index.js b/cocos2d/core/renderer/index.js index cf9996acc1..a43c91e6b5 100644 --- a/cocos2d/core/renderer/index.js +++ b/cocos2d/core/renderer/index.js @@ -25,7 +25,9 @@ const renderEngine = require('./render-engine'); const RenderComponentWalker = require('./render-component-walker'); -require('./RendererWebGL'); +const math = renderEngine.math; + +let _pos = math.vec3.create(); function _initBuiltins(device) { let canvas = document.createElement('canvas'); @@ -76,9 +78,13 @@ module.exports = { this._walker = new RenderComponentWalker(this.device, this.scene); this._cameraNode = new cc.Node(); - this._camera = new renderEngine.Camera({ - x: 0, y: 0, w: canvas.width, h: canvas.height - }); + this._camera = new renderEngine.Camera(); + this._camera.setFov(Math.PI * 60 / 180); + this._camera.setNear(0.1); + this._camera.setFar(1024); + this._camera.setNode(this._cameraNode); + this.updateCameraViewport(); + if (CC_EDITOR) { this._camera.setColor(0, 0, 0, 0); } @@ -96,12 +102,16 @@ module.exports = { var ecScene = cc.director.getScene(); ecScene.scaleX = ecScene.scaleY = 1; } - this._cameraNode.scaleX = 1 / cc.view.getScaleX(); - this._cameraNode.scaleY = 1 / cc.view.getScaleY(); - this._camera._rect.w = this.canvas.width; - this._camera._rect.h = this.canvas.height; - this._camera.setViewport(); - this._camera.setNode(this._cameraNode); + let node = this._cameraNode; + let canvas = this.canvas; + let zeye = canvas.height / 1.1566; + node.x = canvas.width / 2; + node.y = canvas.height / 2; + node.z = zeye; + _pos.x = canvas.width / 2; + _pos.y = canvas.height / 2; + _pos.z = 0; + node.lookAt(_pos); }, render () { diff --git a/cocos2d/core/renderer/render-engine.js b/cocos2d/core/renderer/render-engine.js index ae276334e8..5e648c9955 100644 --- a/cocos2d/core/renderer/render-engine.js +++ b/cocos2d/core/renderer/render-engine.js @@ -13509,8 +13509,11 @@ module.exports = (function () { render (scene) { this._reset(); + const canvas = this._device._gl.canvas; + for (let i = 0; i < scene._cameras.length; ++i) { - let view = scene._cameras.data[i].getView(); + let view = new renderer.View(); + scene._cameras.data[i].extractView(view, canvas.width, canvas.height); this._render(view, scene); } } @@ -13806,28 +13809,28 @@ module.exports = (function () { { name: 'vfx_emitter', vert: '#ifdef GL_ES\nprecision highp float;\n#endif\nattribute vec2 a_quad;\nvarying vec2 index;\nvoid main() {\n index = (a_quad + 1.0) / 2.0;\n gl_Position = vec4(a_quad, 0, 1);\n}\n', - frag: 'uniform sampler2D noise;\nuniform sampler2D state;\nuniform vec2 statesize;\nuniform vec2 noisesize;\nuniform bool stopped;\nuniform float dt;\nuniform float mode;\nuniform float noiseId;\nuniform float emitVar;\nuniform float life;\nuniform float lifeVar;\nuniform vec2 pos;\nuniform vec2 posVar;\nuniform vec4 color;\nuniform vec4 colorVar;\nuniform vec4 endColor;\nuniform vec4 endColorVar;\nuniform float size;\nuniform float sizeVar;\nuniform float endSize;\nuniform float endSizeVar;\nuniform float rot;\nuniform float rotVar;\nuniform float endRot;\nuniform float endRotVar;\nuniform float angle;\nuniform float angleVar;\nuniform float speed;\nuniform float speedVar;\nuniform float radial;\nuniform float radialVar;\nuniform float tangent;\nuniform float tangentVar;\nuniform float radius;\nuniform float radiusVar;\nuniform float endRadius;\nuniform float endRadiusVar;\nuniform float rotatePS;\nuniform float rotatePSVar;\nuniform float sizeScale;\nuniform float accelScale;\nuniform float radiusScale;\nvarying vec2 index;\nconst float BASE = 255.0;\nconst float OFFSET = BASE * BASE / 2.0;\nconst float NOISE_SCALE = 10000.0;\nconst float POSITION_SCALE = 1.0;\nconst float ROTATION_SCALE = 1.0;\nconst float COLOR_SCALE = 1.0;\nconst float LIFE_SCALE = 60.0;\nconst float START_SIZE_EQUAL_TO_END_SIZE = -1.0;\nconst float START_RADIUS_EQUAL_TO_END_RADIUS = -1.0;\nfloat decode(vec2 channels, float scale) {\n return (dot(channels, vec2(BASE, BASE * BASE)) - OFFSET) / scale;\n}\nvec2 encode(float value, float scale) {\n value = value * scale + OFFSET;\n float x = mod(value, BASE);\n float y = floor(value / BASE);\n return vec2(x, y) / BASE;\n}\nfloat randomMinus1To1(vec2 randomD) {\n float random = decode(randomD, NOISE_SCALE);\n return (random - 0.5) * 2.0;\n}\nbool doEmit (vec4 randomD) {\n float random1 = decode(randomD.rg, NOISE_SCALE);\n if (!stopped && (life + lifeVar) * random1 < life) {\n return true;\n }\n else {\n return false;\n }\n}\nvec4 initLife (vec4 data, vec4 randomD) {\n \n if (doEmit(randomD)) {\n float random2 = randomMinus1To1(randomD.ba);\n float plife = life + lifeVar * random2;\n vec2 l = encode(plife, LIFE_SCALE);\n return vec4(l, l);\n }\n else {\n return data;\n }\n}\nvec4 initColor (float randr, float randg, float randb, float randa) {\n vec4 random = vec4(randr, randg, randb, randa);\n vec4 result = clamp(color + colorVar * random, 0.0, 255.0);\n return result / 255.0;\n}\nvec4 initDeltaRG (vec2 startR, vec2 random) {\n vec2 start = clamp(color.rg + colorVar.rg * startR, 0.0, 255.0);\n vec2 end = clamp(endColor.rg + endColorVar.rg * random, 0.0, 255.0);\n vec2 delta = end - start;\n return vec4(encode(delta.x, COLOR_SCALE), encode(delta.y, COLOR_SCALE));\n}\nvec4 initDeltaBA (vec2 startR, vec2 random) {\n vec2 start = clamp(color.ba + colorVar.ba * startR, 0.0, 255.0);\n vec2 end = clamp(endColor.ba + endColorVar.ba * random, 0.0, 255.0);\n vec2 delta = end - start;\n return vec4(encode(delta.x, COLOR_SCALE), encode(delta.y, COLOR_SCALE));\n}\nvec4 initSize (float rand1, float rand2) {\n float start = max(0.0, size + sizeVar * rand1);\n if (endSize == START_SIZE_EQUAL_TO_END_SIZE) {\n float delta = 0.0;\n return vec4(encode(start, sizeScale), encode(delta, sizeScale));\n }\n else {\n float end = max(0.0, endSize + endSizeVar * rand2);\n float delta = end - start;\n return vec4(encode(start, sizeScale), encode(delta, sizeScale));\n }\n}\nvec4 initRotation (float rand1, float rand2) {\n float start = rot + rotVar * rand1;\n float end = endRot + endRotVar * rand2;\n float delta = end - start;\n return vec4(encode(start, ROTATION_SCALE), encode(delta, ROTATION_SCALE));\n}\nvec4 initControl1 (float rand1, float rand2) {\n \n if (mode == 0.0) {\n float pAngle = angle + angleVar * rand1;\n float dirX = cos(pAngle);\n float dirY = sin(pAngle);\n float pSpeed = speed + speedVar * rand2;\n return vec4(encode(dirX * pSpeed, POSITION_SCALE), encode(dirY * pSpeed, POSITION_SCALE));\n }\n \n else {\n float pAngle = angle + angleVar * rand1;\n float pRadius = radius + radiusVar * rand2;\n return vec4(encode(pAngle, ROTATION_SCALE), encode(pRadius, radiusScale));\n }\n}\nvec4 initControl2 (float startR1, float rand1, float rand2) {\n \n if (mode == 0.0) {\n float pRadial = radial + radialVar * rand1;\n float pTangent = tangent + tangentVar * rand2;\n return vec4(encode(pRadial, accelScale), encode(pTangent, accelScale));\n }\n \n else {\n float degreesPerSecond = rotatePS + rotatePSVar * rand1;\n float pDeltaRadius;\n if (endRadius == START_RADIUS_EQUAL_TO_END_RADIUS) {\n pDeltaRadius = 0.0;\n }\n else {\n float pRadius = radius + radiusVar * startR1;\n pDeltaRadius = (endRadius + endRadiusVar * rand2 - pRadius);\n }\n return vec4(encode(degreesPerSecond, ROTATION_SCALE), encode(pDeltaRadius, radiusScale));\n }\n}\nvec4 initPos (float rand1, float rand2) {\n vec2 result = pos + posVar * vec2(rand1, rand2);\n return vec4(encode(result.x, POSITION_SCALE), encode(result.y, POSITION_SCALE));\n}\nvoid main() {\n vec2 pixel = floor(index * statesize);\n vec2 pindex = floor(pixel / 3.0);\n vec2 temp = mod(pixel, vec2(3.0, 3.0));\n float id = floor(temp.y * 3.0 + temp.x);\n vec2 noffset = vec2(floor(noiseId / 4.0), mod(noiseId, 4.0));\n vec2 nid = pixel + noffset;\n vec4 randomD = texture2D(noise, nid / noisesize);\n \n vec4 lifeData = texture2D(state, pindex * 3.0 / statesize);\n float rest = decode(lifeData.rg, LIFE_SCALE);\n float life = decode(lifeData.ba, LIFE_SCALE);\n \n if (id == 0.0) {\n vec4 data = texture2D(state, index);\n if (rest <= 0.0) {\n gl_FragColor = initLife(data, randomD);\n }\n else {\n gl_FragColor = data;\n }\n return;\n }\n \n if (rest > 0.0) {\n vec4 data = texture2D(state, index);\n gl_FragColor = data;\n return;\n }\n vec2 lifeNid = pindex * 3.0 + noffset;\n vec4 lifeRandomD = texture2D(noise, lifeNid / noisesize);\n bool emitting = doEmit(lifeRandomD);\n if (!emitting) {\n vec4 data = texture2D(state, index);\n gl_FragColor = data;\n return;\n }\n \n float random1 = randomMinus1To1(randomD.rg);\n float random2 = randomMinus1To1(randomD.ba);\n \n if (id == 1.0) {\n vec4 randomD3 = texture2D(noise, vec2(nid.x - 1.0, nid.y + 1.0) / noisesize);\n float random3 = randomMinus1To1(randomD3.rg);\n float random4 = randomMinus1To1(randomD3.ba);\n gl_FragColor = initColor(random1, random2, random3, random4);\n return;\n }\n \n if (id == 2.0) {\n vec4 randomD1 = texture2D(noise, vec2(nid.x - 1.0, nid.y) / noisesize);\n float startR1 = randomMinus1To1(randomD1.rg);\n float startR2 = randomMinus1To1(randomD1.ba);\n vec2 startR = vec2(startR1, startR2);\n gl_FragColor = initDeltaRG(startR, vec2(random1, random2));\n return;\n }\n \n if (id == 3.0) {\n vec2 startR = vec2(random1, random2);\n gl_FragColor = initDeltaBA(startR, vec2(random1, random2));\n return;\n }\n \n if (id == 4.0) {\n gl_FragColor = initSize(random1, random2);\n return;\n }\n \n if (id == 5.0) {\n gl_FragColor = initRotation(random1, random2);\n return;\n }\n \n if (id == 6.0) {\n gl_FragColor = initControl1(random1, random2);\n return;\n }\n \n if (id == 7.0) {\n vec4 randomD6 = texture2D(noise, vec2(nid.x - 1.0, nid.y) / noisesize);\n float startR1 = randomMinus1To1(randomD6.rg);\n gl_FragColor = initControl2(startR1, random1, random2);\n return;\n }\n \n if (id == 8.0) {\n gl_FragColor = initPos(random1, random2);\n return;\n }\n}', + frag: '#ifdef GL_ES\nprecision highp float;\n#endif\nuniform sampler2D noise;\nuniform sampler2D state;\nuniform vec2 statesize;\nuniform vec2 noisesize;\nuniform bool stopped;\nuniform float dt;\nuniform float mode;\nuniform float noiseId;\nuniform float emitVar;\nuniform float life;\nuniform float lifeVar;\nuniform vec2 pos;\nuniform vec2 posVar;\nuniform vec4 color;\nuniform vec4 colorVar;\nuniform vec4 endColor;\nuniform vec4 endColorVar;\nuniform float size;\nuniform float sizeVar;\nuniform float endSize;\nuniform float endSizeVar;\nuniform float rot;\nuniform float rotVar;\nuniform float endRot;\nuniform float endRotVar;\nuniform float angle;\nuniform float angleVar;\nuniform float speed;\nuniform float speedVar;\nuniform float radial;\nuniform float radialVar;\nuniform float tangent;\nuniform float tangentVar;\nuniform float radius;\nuniform float radiusVar;\nuniform float endRadius;\nuniform float endRadiusVar;\nuniform float rotatePS;\nuniform float rotatePSVar;\nuniform float sizeScale;\nuniform float accelScale;\nuniform float radiusScale;\nvarying vec2 index;\nconst float BASE = 255.0;\nconst float OFFSET = BASE * BASE / 2.0;\nconst float NOISE_SCALE = 10000.0;\nconst float POSITION_SCALE = 1.0;\nconst float ROTATION_SCALE = 1.0;\nconst float COLOR_SCALE = 1.0;\nconst float LIFE_SCALE = 100.0;\nconst float START_SIZE_EQUAL_TO_END_SIZE = -1.0;\nconst float START_RADIUS_EQUAL_TO_END_RADIUS = -1.0;\nfloat decode(vec2 channels, float scale) {\n return (dot(channels, vec2(BASE, BASE * BASE)) - OFFSET) / scale;\n}\nvec2 encode(float value, float scale) {\n value = value * scale + OFFSET;\n float x = mod(value, BASE);\n float y = floor(value / BASE);\n return vec2(x, y) / BASE;\n}\nfloat randomMinus1To1(vec2 randomD) {\n float random = decode(randomD, NOISE_SCALE);\n return (random - 0.5) * 2.0;\n}\nbool doEmit (vec4 randomD) {\n float random1 = decode(randomD.rg, NOISE_SCALE);\n if (!stopped && (life + lifeVar) * random1 < life) {\n return true;\n }\n else {\n return false;\n }\n}\nvec4 initLife (vec4 data, vec4 randomD) {\n \n if (doEmit(randomD)) {\n float random2 = decode(randomD.ba, NOISE_SCALE);\n float plife = life + lifeVar * random2;\n vec2 l = encode(plife, LIFE_SCALE);\n return vec4(l, l);\n }\n else {\n return data;\n }\n}\nvec4 initColor (float randr, float randg, float randb, float randa) {\n vec4 random = vec4(randr, randg, randb, randa);\n vec4 result = clamp(color + colorVar * random, 0.0, 255.0);\n return result / 255.0;\n}\nvec4 initDeltaRG (vec2 startR, vec2 random) {\n vec2 start = clamp(color.rg + colorVar.rg * startR, 0.0, 255.0);\n vec2 end = clamp(endColor.rg + endColorVar.rg * random, 0.0, 255.0);\n vec2 delta = end - start;\n return vec4(encode(delta.x, COLOR_SCALE), encode(delta.y, COLOR_SCALE));\n}\nvec4 initDeltaBA (vec2 startR, vec2 random) {\n vec2 start = clamp(color.ba + colorVar.ba * startR, 0.0, 255.0);\n vec2 end = clamp(endColor.ba + endColorVar.ba * random, 0.0, 255.0);\n vec2 delta = end - start;\n return vec4(encode(delta.x, COLOR_SCALE), encode(delta.y, COLOR_SCALE));\n}\nvec4 initSize (float rand1, float rand2) {\n float start = max(0.0, size + sizeVar * rand1);\n if (endSize == START_SIZE_EQUAL_TO_END_SIZE) {\n float delta = 0.0;\n return vec4(encode(start, sizeScale), encode(delta, sizeScale));\n }\n else {\n float end = max(0.0, endSize + endSizeVar * rand2);\n float delta = end - start;\n return vec4(encode(start, sizeScale), encode(delta, sizeScale));\n }\n}\nvec4 initRotation (float rand1, float rand2) {\n float start = rot + rotVar * rand1;\n float end = endRot + endRotVar * rand2;\n float delta = end - start;\n return vec4(encode(start, ROTATION_SCALE), encode(delta, ROTATION_SCALE));\n}\nvec4 initControl1 (float rand1, float rand2) {\n \n if (mode == 0.0) {\n float pAngle = angle + angleVar * rand1;\n float dirX = cos(pAngle);\n float dirY = sin(pAngle);\n float pSpeed = speed + speedVar * rand2;\n return vec4(encode(dirX * pSpeed, POSITION_SCALE), encode(dirY * pSpeed, POSITION_SCALE));\n }\n \n else {\n float pAngle = angle + angleVar * rand1;\n float pRadius = radius + radiusVar * rand2;\n return vec4(encode(pAngle, ROTATION_SCALE), encode(pRadius, radiusScale));\n }\n}\nvec4 initControl2 (float startR1, float rand1, float rand2) {\n \n if (mode == 0.0) {\n float pRadial = radial + radialVar * rand1;\n float pTangent = tangent + tangentVar * rand2;\n return vec4(encode(pRadial, accelScale), encode(pTangent, accelScale));\n }\n \n else {\n float degreesPerSecond = rotatePS + rotatePSVar * rand1;\n float pDeltaRadius;\n if (endRadius == START_RADIUS_EQUAL_TO_END_RADIUS) {\n pDeltaRadius = 0.0;\n }\n else {\n float pRadius = radius + radiusVar * startR1;\n pDeltaRadius = (endRadius + endRadiusVar * rand2 - pRadius);\n }\n return vec4(encode(degreesPerSecond, ROTATION_SCALE), encode(pDeltaRadius, radiusScale));\n }\n}\nvec4 initPos (float rand1, float rand2) {\n vec2 result = pos + posVar * vec2(rand1, rand2);\n return vec4(encode(result.x, POSITION_SCALE), encode(result.y, POSITION_SCALE));\n}\nvoid main() {\n vec2 pixel = floor(index * statesize);\n vec2 pindex = floor(pixel / 3.0);\n vec2 temp = mod(pixel, vec2(3.0, 3.0));\n float id = floor(temp.y * 3.0 + temp.x);\n vec2 noffset = vec2(floor(noiseId / 4.0), mod(noiseId, 4.0));\n vec2 nid = pixel + noffset;\n vec4 randomD = texture2D(noise, nid / noisesize);\n \n vec4 lifeData = texture2D(state, pindex * 3.0 / statesize);\n float rest = decode(lifeData.rg, LIFE_SCALE);\n float life = decode(lifeData.ba, LIFE_SCALE);\n \n if (id == 0.0) {\n vec4 data = texture2D(state, index);\n if (rest <= 0.0) {\n gl_FragColor = initLife(data, randomD);\n }\n else {\n gl_FragColor = data;\n }\n return;\n }\n \n if (rest > 0.0) {\n vec4 data = texture2D(state, index);\n gl_FragColor = data;\n return;\n }\n vec2 lifeNid = pindex * 3.0 + noffset;\n vec4 lifeRandomD = texture2D(noise, lifeNid / noisesize);\n bool emitting = doEmit(lifeRandomD);\n if (!emitting) {\n vec4 data = texture2D(state, index);\n gl_FragColor = data;\n return;\n }\n \n float random1 = randomMinus1To1(randomD.rg);\n float random2 = randomMinus1To1(randomD.ba);\n \n if (id == 1.0) {\n vec4 randomD3 = texture2D(noise, vec2(nid.x - 1.0, nid.y + 1.0) / noisesize);\n float random3 = randomMinus1To1(randomD3.rg);\n float random4 = randomMinus1To1(randomD3.ba);\n gl_FragColor = initColor(random1, random2, random3, random4);\n return;\n }\n \n if (id == 2.0) {\n vec4 randomD1 = texture2D(noise, vec2(nid.x - 1.0, nid.y) / noisesize);\n float startR1 = randomMinus1To1(randomD1.rg);\n float startR2 = randomMinus1To1(randomD1.ba);\n vec2 startR = vec2(startR1, startR2);\n gl_FragColor = initDeltaRG(startR, vec2(random1, random2));\n return;\n }\n \n if (id == 3.0) {\n vec2 startR = vec2(random1, random2);\n gl_FragColor = initDeltaBA(startR, vec2(random1, random2));\n return;\n }\n \n if (id == 4.0) {\n gl_FragColor = initSize(random1, random2);\n return;\n }\n \n if (id == 5.0) {\n gl_FragColor = initRotation(random1, random2);\n return;\n }\n \n if (id == 6.0) {\n gl_FragColor = initControl1(random1, random2);\n return;\n }\n \n if (id == 7.0) {\n vec4 randomD6 = texture2D(noise, vec2(nid.x - 1.0, nid.y) / noisesize);\n float startR1 = randomMinus1To1(randomD6.rg);\n gl_FragColor = initControl2(startR1, random1, random2);\n return;\n }\n \n if (id == 8.0) {\n gl_FragColor = initPos(random1, random2);\n return;\n }\n}', defines: [ ], }, { name: 'vfx_particle', - vert: '#ifdef GL_ES\nprecision highp float;\n#endif\nattribute vec2 a_quad;\nuniform mat4 model;\nuniform mat4 viewProj;\nuniform sampler2D state;\nuniform sampler2D quad;\nuniform vec2 statesize;\nuniform vec2 quadsize;\nuniform float z;\nuniform vec2 lb;\nuniform vec2 rt;\nvarying lowp vec4 v_fragmentColor;\nvarying vec2 uv0;\nconst float BASE = 255.0;\nconst float OFFSET = BASE * BASE / 2.0;\nconst float LIFE_SCALE = 60.0;\nconst float POSITION_SCALE = 1.0;\nfloat decode(vec2 channels, float scale) {\n return (dot(channels, vec2(BASE, BASE * BASE)) - OFFSET) / scale;\n}\nvoid main() {\n vec2 sIndex = floor(a_quad / 2.0) * 3.0;\n vec4 lifeData = texture2D(state, sIndex / statesize);\n float life = decode(lifeData.rg, LIFE_SCALE);\n if (life <= 0.0) {\n v_fragmentColor = vec4(0.0, 0.0, 0.0, 0.0);\n uv0 = vec2(0.0, 0.0);\n gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\n }\n else {\n vec2 posIndex = a_quad / quadsize;\n vec4 posData = texture2D(quad, posIndex);\n vec2 pos = vec2(decode(posData.rg, POSITION_SCALE), decode(posData.ba, POSITION_SCALE));\n vec2 cIndex = vec2(sIndex.x + 1.0, sIndex.y) / statesize;\n vec4 color = texture2D(state, cIndex);\n v_fragmentColor = color;\n float u, v;\n vec2 uvId = mod(a_quad, vec2(2.0));\n if (uvId.x == 0.0) {\n u = lb.x;\n }\n else {\n u = rt.x;\n }\n if (uvId.y == 0.0) {\n v = lb.y;\n }\n else {\n v = rt.y;\n }\n uv0 = vec2(u, v);\n gl_Position = viewProj * model * vec4(pos, z, 1.0);\n } \n}\n', - frag: 'uniform sampler2D texture;\nvarying vec2 uv0;\nvarying vec4 v_fragmentColor;\nvoid main () {\n vec4 o = v_fragmentColor;\n o *= texture2D(texture, uv0);\n gl_FragColor = o;\n}', + vert: '#ifdef GL_ES\nprecision highp float;\n#endif\nattribute vec2 a_quad;\nuniform mat4 model;\nuniform mat4 viewProj;\nuniform sampler2D state;\nuniform sampler2D quad;\nuniform vec2 statesize;\nuniform vec2 quadsize;\nuniform float z;\nuniform vec2 lb;\nuniform vec2 rt;\nvarying lowp vec4 v_fragmentColor;\nvarying vec2 uv0;\nconst float BASE = 255.0;\nconst float OFFSET = BASE * BASE / 2.0;\nconst float LIFE_SCALE = 100.0;\nconst float POSITION_SCALE = 1.0;\nfloat decode(vec2 channels, float scale) {\n return (dot(channels, vec2(BASE, BASE * BASE)) - OFFSET) / scale;\n}\nvoid main() {\n vec2 sIndex = floor(a_quad / 2.0) * 3.0;\n vec4 lifeData = texture2D(state, sIndex / statesize);\n float life = decode(lifeData.rg, LIFE_SCALE);\n if (life <= 0.0) {\n v_fragmentColor = vec4(0.0, 0.0, 0.0, 0.0);\n uv0 = vec2(0.0, 0.0);\n gl_Position = vec4(0.0, 0.0, 0.0, 1.0);\n }\n else {\n vec2 posIndex = a_quad / quadsize;\n vec4 posData = texture2D(quad, posIndex);\n vec2 pos = vec2(decode(posData.rg, POSITION_SCALE), decode(posData.ba, POSITION_SCALE));\n vec2 cIndex = vec2(sIndex.x + 1.0, sIndex.y) / statesize;\n vec4 color = texture2D(state, cIndex);\n v_fragmentColor = color;\n float u, v;\n vec2 uvId = mod(a_quad, vec2(2.0));\n if (uvId.x == 0.0) {\n u = lb.x;\n }\n else {\n u = rt.x;\n }\n if (uvId.y == 0.0) {\n v = lb.y;\n }\n else {\n v = rt.y;\n }\n uv0 = vec2(u, v);\n gl_Position = viewProj * model * vec4(pos, z, 1.0);\n } \n}\n', + frag: '#ifdef GL_ES\nprecision highp float;\n#endif\nuniform sampler2D texture;\nvarying vec2 uv0;\nvarying vec4 v_fragmentColor;\nvoid main () {\n vec4 o = v_fragmentColor;\n o *= texture2D(texture, uv0);\n gl_FragColor = o;\n}', defines: [ ], }, { name: 'vfx_quad', vert: '#ifdef GL_ES\nprecision highp float;\n#endif\nattribute vec2 a_quad;\nvarying vec2 index;\nvoid main() {\n index = (a_quad + 1.0) / 2.0;\n gl_Position = vec4(a_quad, 0, 1);\n}\n', - frag: 'uniform sampler2D state;\nuniform vec2 quadsize;\nuniform vec2 statesize;\nuniform float sizeScale;\nvarying vec2 index;\nconst float BASE = 255.0;\nconst float OFFSET = BASE * BASE / 2.0;\nconst float POSITION_SCALE = 1.0;\nconst float ROTATION_SCALE = 1.0;\nfloat decode(vec2 channels, float scale) {\n return (dot(channels, vec2(BASE, BASE * BASE)) - OFFSET) / scale;\n}\nvec2 encode(float value, float scale) {\n value = value * scale + OFFSET;\n float x = mod(value, BASE);\n float y = floor(value / BASE);\n return vec2(x, y) / BASE;\n}\nvoid main() {\n vec2 pIndex = floor(index * quadsize / 2.0) * 3.0;\n vec2 dataIndex = (pIndex + 2.0) / statesize;\n vec4 posData = texture2D(state, dataIndex);\n vec2 pos = vec2(decode(posData.rg, POSITION_SCALE), decode(posData.ba, POSITION_SCALE));\n dataIndex = (pIndex + 1.0) / statesize;\n vec4 sizeData = texture2D(state, dataIndex);\n float size = decode(sizeData.rg, sizeScale);\n dataIndex.x = (pIndex.x + 2.0) / statesize.x;\n dataIndex.y = (pIndex.y + 1.0) / statesize.y;\n vec4 rotData = texture2D(state, dataIndex);\n float rot = radians(floor(decode(rotData.rg, ROTATION_SCALE)));\n float a = cos(rot);\n float b = -sin(rot);\n float c = -b;\n float d = a;\n vec2 vert = (mod(floor(index * quadsize), vec2(2.0)) - 0.5) * size;\n float x = vert.x * a + vert.y * c + pos.x;\n float y = vert.x * b + vert.y * d + pos.y;\n gl_FragColor = vec4(encode(x, POSITION_SCALE), encode(y, POSITION_SCALE));\n}\n', + frag: '#ifdef GL_ES\nprecision highp float;\n#endif\nuniform sampler2D state;\nuniform vec2 quadsize;\nuniform vec2 statesize;\nuniform float sizeScale;\nvarying vec2 index;\nconst float BASE = 255.0;\nconst float OFFSET = BASE * BASE / 2.0;\nconst float LIFE_SCALE = 100.0;\nconst float POSITION_SCALE = 1.0;\nconst float ROTATION_SCALE = 1.0;\nfloat decode(vec2 channels, float scale) {\n return (dot(channels, vec2(BASE, BASE * BASE)) - OFFSET) / scale;\n}\nvec2 encode(float value, float scale) {\n value = value * scale + OFFSET;\n float x = mod(value, BASE);\n float y = floor(value / BASE);\n return vec2(x, y) / BASE;\n}\nvoid main() {\n vec2 pixel = floor(index * quadsize);\n vec2 pIndex = floor(pixel / 2.0) * 3.0;\n vec4 lifeData = texture2D(state, pIndex / statesize);\n float rest = decode(lifeData.rg, LIFE_SCALE);\n if (rest <= 0.0) {\n gl_FragColor = vec4(encode(0.0, POSITION_SCALE), encode(0.0, POSITION_SCALE));\n return;\n }\n vec2 dataIndex = (pIndex + 2.0) / statesize;\n vec4 posData = texture2D(state, dataIndex);\n float x = decode(posData.rg, POSITION_SCALE);\n float y = decode(posData.ba, POSITION_SCALE);\n vec2 pos = vec2(x, y);\n dataIndex = (pIndex + 1.0) / statesize;\n vec4 sizeData = texture2D(state, dataIndex);\n float size = decode(sizeData.rg, sizeScale);\n dataIndex.x = (pIndex.x + 2.0) / statesize.x;\n dataIndex.y = (pIndex.y + 1.0) / statesize.y;\n vec4 rotData = texture2D(state, dataIndex);\n float rot = radians(mod(decode(rotData.rg, ROTATION_SCALE), 180.0));\n float a = cos(rot);\n float b = -sin(rot);\n float c = -b;\n float d = a;\n vec2 vert = (mod(pixel, vec2(2.0)) - 0.5) * size;\n x = vert.x * a + vert.y * c + pos.x;\n y = vert.x * b + vert.y * d + pos.y;\n gl_FragColor = vec4(encode(x, POSITION_SCALE), encode(y, POSITION_SCALE));\n}\n', defines: [ ], }, { name: 'vfx_update', vert: '#ifdef GL_ES\nprecision highp float;\n#endif\nattribute vec2 a_quad;\nvarying vec2 index;\nvoid main() {\n index = (a_quad + 1.0) / 2.0;\n gl_Position = vec4(a_quad, 0, 1);\n}\n', - frag: 'uniform sampler2D state;\nuniform vec2 statesize;\nuniform float dt;\nuniform float mode;\nuniform vec2 gravity;\nuniform float sizeScale;\nuniform float accelScale;\nuniform float radiusScale;\nvarying vec2 index;\nconst float BASE = 255.0;\nconst float OFFSET = BASE * BASE / 2.0;\nconst float MAX_VALUE = BASE * BASE;\nconst float LIFE_SCALE = 60.0;\nconst float POSITION_SCALE = 1.0;\nconst float ROTATION_SCALE = 1.0;\nconst float COLOR_SCALE = 1.0;\nfloat decode(vec2 channels, float scale) {\n return (dot(channels, vec2(BASE, BASE * BASE)) - OFFSET) / scale;\n}\nvec2 encode(float value, float scale) {\n value = value * scale + OFFSET;\n float x = mod(value, BASE);\n float y = floor(value / BASE);\n return vec2(x, y) / BASE;\n}\nvec4 updateLife (vec4 data) {\n float rest = decode(data.rg, LIFE_SCALE);\n rest -= dt;\n return vec4(encode(rest, LIFE_SCALE), data.ba);\n}\nvec4 updateColor (vec4 color, vec4 deltaRG, vec4 deltaBA, float life) {\n float r = decode(deltaRG.rg, COLOR_SCALE);\n float g = decode(deltaRG.ba, COLOR_SCALE);\n float b = decode(deltaBA.rg, COLOR_SCALE);\n float a = decode(deltaBA.ba, COLOR_SCALE);\n vec4 deltaColor = vec4(r, g, b, a) / 255.0;\n \n color = clamp(color + deltaColor * dt / life, 0.0, 1.0);\n return color;\n}\nvec4 updateSize (vec4 data, float life) {\n float size = decode(data.rg, sizeScale);\n float deltaSize = decode(data.ba, sizeScale);\n size = clamp(size + deltaSize * dt / life, 0.0, MAX_VALUE);\n return vec4(encode(size, sizeScale), data.ba);\n}\nvec4 updateRotation (vec4 data, float life) {\n float rotation = decode(data.rg, ROTATION_SCALE);\n float deltaRotation = decode(data.ba, ROTATION_SCALE);\n rotation += deltaRotation * dt / life;\n return vec4(encode(rotation, ROTATION_SCALE), data.ba);\n}\nvec4 updateControl (vec4 control1, vec4 control2, vec4 posData, float life) {\n \n if (mode == 0.0) {\n vec2 dir = vec2(decode(control1.rg, POSITION_SCALE), decode(control1.ba, POSITION_SCALE));\n float radialAccel = decode(control2.rg, accelScale);\n float tangentialAccel = decode(control2.ba, accelScale);\n vec2 pos = vec2(decode(posData.rg, POSITION_SCALE), decode(posData.ba, POSITION_SCALE));\n vec2 radial = normalize(pos);\n vec2 tangential = vec2(-radial.y, radial.x);\n radial = radial * radialAccel;\n tangential = tangential * tangentialAccel;\n vec2 result = dir + (radial + tangentialAccel + gravity) * dt;\n return vec4(encode(result.x, POSITION_SCALE), encode(result.y, POSITION_SCALE));\n }\n \n else {\n float angle = mod(decode(control1.rg, ROTATION_SCALE), 360.0);\n float radius = decode(control1.ba, radiusScale);\n float degreesPerSecond = decode(control2.rg, ROTATION_SCALE);\n float deltaRadius = decode(control2.ba, radiusScale);\n angle += degreesPerSecond * dt;\n radius += deltaRadius * dt / life;\n return vec4(encode(angle, ROTATION_SCALE), encode(radius, radiusScale));\n }\n}\nvec4 updatePos (vec4 posData, vec4 control) {\n vec2 result;\n \n if (mode == 0.0) {\n vec2 dir = vec2(decode(control.rg, POSITION_SCALE), decode(control.ba, POSITION_SCALE));\n vec2 pos = vec2(decode(posData.rg, POSITION_SCALE), decode(posData.ba, POSITION_SCALE));\n result = pos + dir * dt;\n }\n \n else {\n float angle = radians(decode(control.rg, ROTATION_SCALE));\n float radius = decode(control.ba, radiusScale);\n result.x = -cos(angle) * radius;\n result.y = -sin(angle) * radius;\n }\n return vec4(encode(result.x, POSITION_SCALE), encode(result.y, POSITION_SCALE));\n}\nvoid main() {\n vec2 pixel = floor(index * statesize);\n vec2 pindex = floor(pixel / 3.0);\n vec2 temp = mod(pixel, vec2(3.0));\n float id = floor(temp.y * 3.0 + temp.x);\n \n vec4 data = texture2D(state, index);\n vec4 lifeData = texture2D(state, pindex * 3.0 / statesize);\n float rest = decode(lifeData.rg, LIFE_SCALE);\n if (rest <= 0.0) {\n gl_FragColor = data;\n return;\n }\n \n if (id == 2.0 || id == 3.0 || id == 7.0) {\n gl_FragColor = data;\n return;\n }\n float life = decode(lifeData.ba, LIFE_SCALE);\n \n if (id == 0.0) {\n gl_FragColor = updateLife(data);\n return;\n }\n \n if (id == 1.0) {\n vec2 rgIndex = vec2(pixel.x + 1.0, pixel.y) / statesize;\n vec4 deltaRG = texture2D(state, rgIndex);\n vec2 baIndex = vec2(pixel.x - 1.0, pixel.y + 1.0) / statesize;\n vec4 deltaBA = texture2D(state, baIndex);\n gl_FragColor = updateColor(data, deltaRG, deltaBA, life);\n return;\n }\n \n if (id == 4.0) {\n gl_FragColor = updateSize(data, life);\n return;\n }\n \n if (id == 5.0) {\n gl_FragColor = updateRotation(data, life);\n return;\n }\n \n if (id == 6.0) {\n vec2 ctrlIndex = vec2(pixel.x + 1.0, pixel.y) / statesize;\n vec4 control2 = texture2D(state, ctrlIndex);\n vec2 posIndex = vec2(pixel.x + 2.0, pixel.y) / statesize;\n vec4 pos = texture2D(state, posIndex);\n gl_FragColor = updateControl(data, control2, pos, life);\n return;\n }\n \n if (id == 8.0) {\n vec2 ctrlIndex = vec2(pixel.x - 2.0, pixel.y) / statesize;\n vec4 control1 = texture2D(state, ctrlIndex);\n gl_FragColor = updatePos(data, control1);\n return;\n }\n}\n', + frag: '#ifdef GL_ES\nprecision highp float;\n#endif\nuniform sampler2D state;\nuniform vec2 statesize;\nuniform float dt;\nuniform float mode;\nuniform vec2 gravity;\nuniform float sizeScale;\nuniform float accelScale;\nuniform float radiusScale;\nvarying vec2 index;\nconst float BASE = 255.0;\nconst float OFFSET = BASE * BASE / 2.0;\nconst float MAX_VALUE = BASE * BASE;\nconst float LIFE_SCALE = 100.0;\nconst float POSITION_SCALE = 1.0;\nconst float ROTATION_SCALE = 1.0;\nconst float COLOR_SCALE = 1.0;\nfloat decode(vec2 channels, float scale) {\n return (dot(channels, vec2(BASE, BASE * BASE)) - OFFSET) / scale;\n}\nvec2 encode(float value, float scale) {\n value = value * scale + OFFSET;\n float x = mod(value, BASE);\n float y = floor(value / BASE);\n return vec2(x, y) / BASE;\n}\nvec4 updateLife (vec4 data) {\n float rest = decode(data.rg, LIFE_SCALE);\n rest -= dt;\n return vec4(encode(rest, LIFE_SCALE), data.ba);\n}\nvec4 updateColor (vec4 color, vec4 deltaRG, vec4 deltaBA, float life) {\n float r = decode(deltaRG.rg, COLOR_SCALE);\n float g = decode(deltaRG.ba, COLOR_SCALE);\n float b = decode(deltaBA.rg, COLOR_SCALE);\n float a = decode(deltaBA.ba, COLOR_SCALE);\n vec4 deltaColor = vec4(r, g, b, a) / 255.0;\n \n color = clamp(color + deltaColor * dt / life, 0.0, 1.0);\n return color;\n}\nvec4 updateSize (vec4 data, float life) {\n float size = decode(data.rg, sizeScale);\n float deltaSize = decode(data.ba, sizeScale);\n size = clamp(size + deltaSize * dt / life, 0.0, MAX_VALUE);\n return vec4(encode(size, sizeScale), data.ba);\n}\nvec4 updateRotation (vec4 data, float life) {\n float rotation = decode(data.rg, ROTATION_SCALE);\n float deltaRotation = decode(data.ba, ROTATION_SCALE);\n rotation = mod(rotation + deltaRotation * dt / life, 180.0);\n return vec4(encode(rotation, ROTATION_SCALE), data.ba);\n}\nvec4 updateControl (vec4 control1, vec4 control2, vec4 posData, float life) {\n \n if (mode == 0.0) {\n vec2 dir = vec2(decode(control1.rg, POSITION_SCALE), decode(control1.ba, POSITION_SCALE));\n float radialAccel = decode(control2.rg, accelScale);\n float tangentialAccel = decode(control2.ba, accelScale);\n vec2 pos = vec2(decode(posData.rg, POSITION_SCALE), decode(posData.ba, POSITION_SCALE));\n vec2 radial = normalize(pos);\n vec2 tangential = vec2(-radial.y, radial.x);\n radial = radial * radialAccel;\n tangential = tangential * tangentialAccel;\n vec2 result = dir + (radial + tangentialAccel + gravity) * dt;\n return vec4(encode(result.x, POSITION_SCALE), encode(result.y, POSITION_SCALE));\n }\n \n else {\n float angle = mod(decode(control1.rg, ROTATION_SCALE), 180.0);\n float radius = decode(control1.ba, radiusScale);\n float degreesPerSecond = decode(control2.rg, ROTATION_SCALE);\n float deltaRadius = decode(control2.ba, radiusScale);\n angle += degreesPerSecond * dt;\n radius += deltaRadius * dt / life;\n return vec4(encode(angle, ROTATION_SCALE), encode(radius, radiusScale));\n }\n}\nvec4 updatePos (vec4 posData, vec4 control) {\n vec2 result;\n \n if (mode == 0.0) {\n vec2 dir = vec2(decode(control.rg, POSITION_SCALE), decode(control.ba, POSITION_SCALE));\n vec2 pos = vec2(decode(posData.rg, POSITION_SCALE), decode(posData.ba, POSITION_SCALE));\n result = pos + dir * dt;\n }\n \n else {\n float angle = radians(mod(decode(control.rg, ROTATION_SCALE), 180.0));\n float radius = decode(control.ba, radiusScale);\n result.x = -cos(angle) * radius;\n result.y = -sin(angle) * radius;\n }\n return vec4(encode(result.x, POSITION_SCALE), encode(result.y, POSITION_SCALE));\n}\nvoid main() {\n vec2 pixel = floor(index * statesize);\n vec2 pindex = floor(pixel / 3.0);\n vec2 temp = mod(pixel, vec2(3.0));\n float id = floor(temp.y * 3.0 + temp.x);\n \n vec4 data = texture2D(state, index);\n vec4 lifeData = texture2D(state, pindex * 3.0 / statesize);\n float rest = decode(lifeData.rg, LIFE_SCALE);\n if (rest <= 0.0) {\n gl_FragColor = data;\n return;\n }\n \n if (id == 2.0 || id == 3.0 || id == 7.0) {\n gl_FragColor = data;\n return;\n }\n float life = decode(lifeData.ba, LIFE_SCALE);\n \n if (id == 0.0) {\n gl_FragColor = updateLife(data);\n return;\n }\n \n if (id == 1.0) {\n vec2 rgIndex = vec2(pixel.x + 1.0, pixel.y) / statesize;\n vec4 deltaRG = texture2D(state, rgIndex);\n vec2 baIndex = vec2(pixel.x - 1.0, pixel.y + 1.0) / statesize;\n vec4 deltaBA = texture2D(state, baIndex);\n gl_FragColor = updateColor(data, deltaRG, deltaBA, life);\n return;\n }\n \n if (id == 4.0) {\n gl_FragColor = updateSize(data, life);\n return;\n }\n \n if (id == 5.0) {\n gl_FragColor = updateRotation(data, life);\n return;\n }\n \n if (id == 6.0) {\n vec2 ctrlIndex = vec2(pixel.x + 1.0, pixel.y) / statesize;\n vec4 control2 = texture2D(state, ctrlIndex);\n vec2 posIndex = vec2(pixel.x + 2.0, pixel.y) / statesize;\n vec4 pos = texture2D(state, posIndex);\n gl_FragColor = updateControl(data, control2, pos, life);\n return;\n }\n \n if (id == 8.0) {\n vec2 ctrlIndex = vec2(pixel.x - 2.0, pixel.y) / statesize;\n vec4 control1 = texture2D(state, ctrlIndex);\n gl_FragColor = updatePos(data, control1);\n return;\n }\n}\n', defines: [ ], }, @@ -13838,193 +13841,6 @@ module.exports = (function () { templates }; - function create3DContext (canvas, opt_attribs) { - try { - return canvas.getContext('webgl', opt_attribs) || canvas.getContext('experimental-webgl', opt_attribs); - } catch (e) { - return null; - } - } - - let supportWebGL = false; - - // In editor main process - if (typeof window === 'undefined') { - supportWebGL = true; - } - else { - const sys = window.cc && window.cc.sys; - let canvas = document.createElement("canvas"); - - if (window.WebGLRenderingContext) { - // if (create3DContext(canvas)) { - supportWebGL = true; - // } - if (supportWebGL && sys && sys.os === sys.OS_ANDROID) { - var browserVer = parseFloat(sys.browserVersion); - switch (sys.browserType) { - case sys.BROWSER_TYPE_MOBILE_QQ: - case sys.BROWSER_TYPE_BAIDU: - case sys.BROWSER_TYPE_BAIDU_APP: - // QQ & Baidu Brwoser 6.2+ (using blink kernel) - if (browserVer >= 6.2) { - supportWebGL = true; - } - else { - supportWebGL = false; - } - break; - case sys.BROWSER_TYPE_ANDROID: - // Android 5+ default browser - if (sys.osMainVersion && sys.osMainVersion >= 5) { - supportWebGL = true; - } - break; - case sys.BROWSER_TYPE_CHROME: - // Chrome on android supports WebGL from v. 30 - if (browserVer >= 30.0) { - supportWebGL = true; - } else { - supportWebGL = false; - } - break; - case sys.BROWSER_TYPE_UC: - if (browserVer > 11.0) { - supportWebGL = true; - } else { - supportWebGL = false; - } - case sys.BROWSER_TYPE_360: - supportWebGL = false; - } - } - } - } - - var renderMode = { - supportWebGL, - create3DContext - }; - - class Camera$1 { - constructor(viewport, node) { - this.view = null; - - this._node = null; - this._poolID = -1; - this._projection = Camera$1.PROJECTION.PERSPECTIVE; - - // clear options - this._color = color4.new(0, 0, 0, 1); - this._depth = 1; - this._stencil = 1; - this._clearFlags = renderer.CLEAR_COLOR | renderer.CLEAR_DEPTH; - - // projection properties - this._near = 0.1; - this._far = 1024; - this._fov = Math.PI * 60 / 180; // vertical fov - - // view properties - this._rect = { - x: 0, y: 0, w: 1, h: 1 - }; - this._stages = []; - this._framebuffer = null; - - // matrix - this._matView = mat4.create(); - this._matProj = mat4.create(); - this._matViewProj = mat4.create(); - this._matInvViewProj = mat4.create(); - - this.setViewport(viewport); - if (node) { - this.setNode(node); - } - } - - setColor(r, g, b, a) { - color4.set(this._color, r, g, b, a); - } - - setDepth(depth) { - this._depth = depth; - } - - setStencil(stencil) { - this._stencil = stencil; - } - - setClearFlags(flags) { - this._clearFlags = flags; - } - - setStages(stages) { - this._stages = stages; - } - - setFramebuffer(framebuffer) { - this._framebuffer = framebuffer; - } - - setNode (node) { - this._node = node; - // view matrix - this._node.getWorldMatrix(this._matView); - mat4.invert(this._matView, this._matView); - - // view-projection - mat4.mul(this._matViewProj, this._matProj, this._matView); - } - - setViewport (viewport) { - if (viewport) { - this._rect = viewport; - } - - if (renderMode.supportWebGL) { - // projection matrix - // TODO: if this._projDirty - let aspect = this._rect.w / this._rect.h; - if (this._projection === Camera$1.PROJECTION.PERSPECTIVE) { - // Magic number - let zeye = this._rect.h / 1.1566; - let proj = mat4.create(); - mat4.perspective(proj, this._fov, aspect, this._near, zeye * 2); - let eye = vec3.new(-this._rect.x + this._rect.w / 2, -this._rect.y + this._rect.h / 2, zeye); - let center = vec3.new(-this._rect.x + this._rect.w / 2, -this._rect.y + this._rect.h / 2, 0.0); - let up = vec3.new(0.0, 1.0, 0.0); - let lookup = mat4.create(); - mat4.lookAt(lookup, eye, center, up); - mat4.mul(this._matProj, proj, lookup); - } else { - mat4.ortho(this._matProj, - 0, this._rect.w, 0, this._rect.h, this._near, this._far - ); - } - } - else { - mat4.identity(this._matProj); - this._matProj.m12 = this._rect.x; - this._matProj.m13 = this._rect.y + this._rect.h; - this._matProj.m05 = -1; - } - - // view-projection - mat4.mul(this._matViewProj, this._matProj, this._matView); - mat4.invert(this._matInvViewProj, this._matViewProj); - } - - getView() { - return this; - } - } - Camera$1.PROJECTION = { - PERSPECTIVE: 0, - ORTHO: 1 - }; - var _pool; var _dataPool = new Pool(() => { return { @@ -14114,7 +13930,7 @@ module.exports = (function () { const BASE = 255; const NOISE_SCALE = 10000; const POS_SCALE = 1; - const LIFE_SCALE = 60; + const LIFE_SCALE = 100; const COLOR_SCALE = 1; const ROTATION_SCALE = 1; const MAX_SCALE = 500; @@ -14995,9 +14811,79 @@ module.exports = (function () { } } + function create3DContext (canvas, opt_attribs) { + try { + return canvas.getContext('webgl', opt_attribs) || canvas.getContext('experimental-webgl', opt_attribs); + } catch (e) { + return null; + } + } + + let supportWebGL = false; + + // In editor main process + if (typeof window === 'undefined') { + supportWebGL = true; + } + else { + const sys = window.cc && window.cc.sys; + let canvas = document.createElement("canvas"); + + if (window.WebGLRenderingContext) { + // if (create3DContext(canvas)) { + supportWebGL = true; + // } + if (supportWebGL && sys && sys.os === sys.OS_ANDROID) { + var browserVer = parseFloat(sys.browserVersion); + switch (sys.browserType) { + case sys.BROWSER_TYPE_MOBILE_QQ: + case sys.BROWSER_TYPE_BAIDU: + case sys.BROWSER_TYPE_BAIDU_APP: + // QQ & Baidu Brwoser 6.2+ (using blink kernel) + if (browserVer >= 6.2) { + supportWebGL = true; + } + else { + supportWebGL = false; + } + break; + case sys.BROWSER_TYPE_ANDROID: + // Android 5+ default browser + if (sys.osMainVersion && sys.osMainVersion >= 5) { + supportWebGL = true; + } + break; + case sys.BROWSER_TYPE_CHROME: + // Chrome on android supports WebGL from v. 30 + if (browserVer >= 30.0) { + supportWebGL = true; + } else { + supportWebGL = false; + } + break; + case sys.BROWSER_TYPE_UC: + if (browserVer > 11.0) { + supportWebGL = true; + } else { + supportWebGL = false; + } + case sys.BROWSER_TYPE_360: + supportWebGL = false; + } + } + } + } + + var renderMode = { + supportWebGL, + create3DContext + }; + // intenral // deps const Scene$1 = renderer.Scene; + const Camera$1 = renderer.Camera; + const View$1 = renderer.View; const ForwardRenderer$2 = renderMode.supportWebGL ? ForwardRenderer : ForwardRenderer$1; const Texture2D$2 = renderMode.supportWebGL ? gfx.Texture2D : canvas.Texture2D; const Device$2 = renderMode.supportWebGL ? gfx.Device : canvas.Device; @@ -15013,6 +14899,7 @@ module.exports = (function () { // render scene Scene: Scene$1, Camera: Camera$1, + View: View$1, Model: Model$1, RenderData, InputAssembler: InputAssembler$1, @@ -15050,5 +14937,4 @@ module.exports = (function () { return renderEngine; - }()); - \ No newline at end of file + }()); \ No newline at end of file diff --git a/cocos2d/core/utils/math-pools.js b/cocos2d/core/utils/math-pools.js index b80831102d..367077f5ea 100644 --- a/cocos2d/core/utils/math-pools.js +++ b/cocos2d/core/utils/math-pools.js @@ -5,7 +5,7 @@ const Vec2 = require('../value-types/CCVec2'); const Vec3 = require('../value-types/CCVec3'); const math = renderEngine.math; -var mat4Pool = new JS.Pool(512); +var mat4Pool = new JS.Pool(128); mat4Pool.get = function () { var matrix = this._get(); if (matrix) { @@ -17,32 +17,45 @@ mat4Pool.get = function () { return matrix; }; -var vec2Pool = new JS.Pool(1024); -vec2Pool.get = function () { - var vec2 = this._get(); - if (vec2) { - vec2.x = vec2.y = 0; - } - else { - vec2 = new Vec2(); - } - return vec2; -}; +// var vec2Pool = new JS.Pool(128); +// vec2Pool.get = function () { +// var vec2 = this._get(); +// if (vec2) { +// vec2.x = vec2.y = 0; +// } +// else { +// vec2 = new Vec2(); +// } +// return vec2; +// }; -var vec3Pool = new JS.Pool(512); -vec3Pool.get = function () { - var vec3 = this._get(); - if (vec3) { - vec3.x = vec3.y = vec3.z = 0; +// var vec3Pool = new JS.Pool(128); +// vec3Pool.get = function () { +// var vec3 = this._get(); +// if (vec3) { +// vec3.x = vec3.y = vec3.z = 0; +// } +// else { +// vec3 = new Vec3(); +// } +// return vec3; +// }; + +var quatPool = new JS.Pool(64); +quatPool.get = function () { + var quat = this._get(); + if (quat) { + math.quat.identity(quat); } else { - vec3 = new Vec3(); + quat = math.quat.create(); } - return vec3; + return quat; }; module.exports = { mat4: mat4Pool, - vec2: vec2Pool, - vec3: vec3Pool + // vec2: vec2Pool, + // vec3: vec3Pool, + quat: quatPool }; \ No newline at end of file