From cc015e746e0f2683cd48a511c7cbca54d73c2259 Mon Sep 17 00:00:00 2001 From: cary <39078800+caryliu1999@users.noreply.github.com> Date: Wed, 8 Apr 2020 10:44:08 +0800 Subject: [PATCH] Add GPU particle renderer. (#6462) * Add GPU particle renderer. * refine code * fix ci * optimize the code * refine code * delete some if judgments * fix strength billboard * modify uniform update * update uniform in editor * fix api and some code specification * fix uniform set --- cocos/core/3d/builtin/effects.ts | 397 +++++++++++++ cocos/core/3d/builtin/init.ts | 6 + cocos/core/renderer/core/pass-utils.ts | 4 +- cocos/particle/animator/color-overtime.ts | 29 +- cocos/particle/animator/curve-range.ts | 163 +++++- cocos/particle/animator/force-overtime.ts | 24 +- cocos/particle/animator/gradient-range.ts | 73 ++- .../animator/limit-velocity-overtime.ts | 29 +- cocos/particle/animator/rotation-overtime.ts | 25 +- cocos/particle/animator/size-overtime.ts | 25 +- cocos/particle/animator/texture-animation.ts | 47 +- cocos/particle/animator/velocity-overtime.ts | 31 +- cocos/particle/emitter/shape-module.ts | 10 +- cocos/particle/enum.ts | 12 + cocos/particle/models/particle-batch-model.ts | 88 ++- cocos/particle/particle-general-function.ts | 4 +- cocos/particle/particle-system-component.ts | 117 ++-- cocos/particle/particle.ts | 61 +- .../renderer/particle-system-renderer-base.ts | 109 ++++ .../renderer/particle-system-renderer-cpu.ts | 452 +++++++++++++++ .../renderer/particle-system-renderer-data.ts | 179 ++++++ .../renderer/particle-system-renderer-gpu.ts | 402 +++++++++++++ .../renderer/particle-system-renderer.ts | 543 ------------------ cocos/particle/renderer/trail.ts | 8 +- 24 files changed, 2113 insertions(+), 725 deletions(-) create mode 100644 cocos/particle/renderer/particle-system-renderer-base.ts create mode 100644 cocos/particle/renderer/particle-system-renderer-cpu.ts create mode 100644 cocos/particle/renderer/particle-system-renderer-data.ts create mode 100644 cocos/particle/renderer/particle-system-renderer-gpu.ts delete mode 100644 cocos/particle/renderer/particle-system-renderer.ts diff --git a/cocos/core/3d/builtin/effects.ts b/cocos/core/3d/builtin/effects.ts index c5e3768ed0..016d3f8255 100644 --- a/cocos/core/3d/builtin/effects.ts +++ b/cocos/core/3d/builtin/effects.ts @@ -50,6 +50,403 @@ export default [ } ] }, + { + "name": "builtin-particle-gpu", + "techniques": [ + { "name": "add", "passes": [{ "rasterizerState": { "cullMode": 0 }, "blendState": { "targets": [{ "blend": true, "blendSrc": 2, "blendDst": 1, "blendSrcAlpha": 2, "blendDstAlpha": 1 }] }, "program": "builtin-particle-gpu|particle-vs-gpu:gpvs_main|tinted-fs:add", "depthStencilState": { "depthTest": true, "depthWrite": false }, "properties": { "mainTexture": { "value": "grey", "type": 28 }, "mainTiling_Offset": { "value": [1, 1, 0, 0], "type": 16 }, "tintColor": { "value": [0.5, 0.5, 0.5, 0.5], "editor": { "type": "color" }, "type": 16 } } }] }, + { "name": "alpha-blend", "passes": [{ "rasterizerState": { "cullMode": 0 }, "blendState": { "targets": [{ "blend": true, "blendSrc": 2, "blendDst": 4, "blendSrcAlpha": 2, "blendDstAlpha": 4 }] }, "program": "builtin-particle-gpu|particle-vs-gpu:gpvs_main|tinted-fs:add", "depthStencilState": { "depthTest": true, "depthWrite": false }, "properties": { "mainTexture": { "value": "grey", "type": 28 }, "mainTiling_Offset": { "value": [1, 1, 0, 0], "type": 16 }, "tintColor": { "value": [0.5, 0.5, 0.5, 0.5], "editor": { "type": "color" }, "type": 16 } } }] }, + { "name": "add-multiply", "passes": [{ "rasterizerState": { "cullMode": 0 }, "blendState": { "targets": [{ "blend": true, "blendSrc": 2, "blendDst": 4, "blendSrcAlpha": 2, "blendDstAlpha": 4 }] }, "program": "builtin-particle-gpu|particle-vs-gpu:gpvs_main|tinted-fs:multiply", "depthStencilState": { "depthTest": true, "depthWrite": false }, "properties": { "mainTexture": { "value": "grey", "type": 28 }, "mainTiling_Offset": { "value": [1, 1, 0, 0], "type": 16 }, "tintColor": { "value": [0.5, 0.5, 0.5, 0.5], "editor": { "type": "color" }, "type": 16 } } }] }, + { "name": "add-smooth", "passes": [{ "rasterizerState": { "cullMode": 0 }, "blendState": { "targets": [{ "blend": true, "blendSrc": 2, "blendDst": 4, "blendSrcAlpha": 2, "blendDstAlpha": 4 }] }, "program": "builtin-particle-gpu|particle-vs-gpu:gpvs_main|no-tint-fs:addSmooth", "depthStencilState": { "depthTest": true, "depthWrite": false }, "properties": { "mainTexture": { "value": "grey", "type": 28 }, "mainTiling_Offset": { "value": [1, 1, 0, 0], "type": 16 } } }] }, + { "name": "premultiply-blend", "passes": [{ "rasterizerState": { "cullMode": 0 }, "blendState": { "targets": [{ "blend": true, "blendSrc": 2, "blendDst": 4, "blendSrcAlpha": 2, "blendDstAlpha": 4 }] }, "program": "builtin-particle-gpu|particle-vs-gpu:gpvs_main|no-tint-fs:premultiplied", "depthStencilState": { "depthTest": true, "depthWrite": false }, "properties": { "mainTexture": { "value": "grey", "type": 28 }, "mainTiling_Offset": { "value": [1, 1, 0, 0], "type": 16 } } }] } + ], + "dependencies": [ + "particle-vs-gpu", + "transform", + "particle-common", + "cc-global", + "cc-local", + "tinted-fs", + "output", + "gamma", + "no-tint-fs" + ], + "shaders": [ + { + "name": "builtin-particle-gpu|particle-vs-gpu:gpvs_main|tinted-fs:add", + "hash": 1416742325, + "glsl3": { + "vert": `\nprecision mediump float;\nvec4 quaternionFromAxis (vec3 xAxis,vec3 yAxis,vec3 zAxis){\n mat3 m = mat3(xAxis,yAxis,zAxis);\n float trace = m[0][0] + m[1][1] + m[2][2];\n vec4 quat;\n if (trace > 0.) {\n float s = 0.5 / sqrt(trace + 1.0);\n quat.w = 0.25 / s;\n quat.x = (m[2][1] - m[1][2]) * s;\n quat.y = (m[0][2] - m[2][0]) * s;\n quat.z = (m[1][0] - m[0][1]) * s;\n } else if ((m[0][0] > m[1][1]) && (m[0][0] > m[2][2])) {\n float s = 2.0 * sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]);\n quat.w = (m[2][1] - m[1][2]) / s;\n quat.x = 0.25 * s;\n quat.y = (m[0][1] + m[1][0]) / s;\n quat.z = (m[0][2] + m[2][0]) / s;\n } else if (m[1][1] > m[2][2]) {\n float s = 2.0 * sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]);\n quat.w = (m[0][2] - m[2][0]) / s;\n quat.x = (m[0][1] + m[1][0]) / s;\n quat.y = 0.25 * s;\n quat.z = (m[1][2] + m[2][1]) / s;\n } else {\n float s = 2.0 * sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]);\n quat.w = (m[1][0] - m[0][1]) / s;\n quat.x = (m[0][2] + m[2][0]) / s;\n quat.y = (m[1][2] + m[2][1]) / s;\n quat.z = 0.25 * s;\n }\n float len = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;\n if (len > 0.) {\n len = 1. / sqrt(len);\n quat.x = quat.x * len;\n quat.y = quat.y * len;\n quat.z = quat.z * len;\n quat.w = quat.w * len;\n }\n return quat;\n}\nvec4 quaternionFromEuler (vec3 angle){\n float x = angle.x / 2.;\n float y = angle.y / 2.;\n float z = angle.z / 2.;\n float sx = sin(x);\n float cx = cos(x);\n float sy = sin(y);\n float cy = cos(y);\n float sz = sin(z);\n float cz = cos(z);\n vec4 quat = vec4(0);\n quat.x = sx * cy * cz + cx * sy * sz;\n quat.y = cx * sy * cz + sx * cy * sz;\n quat.z = cx * cy * sz - sx * sy * cz;\n quat.w = cx * cy * cz - sx * sy * sz;\n return quat;\n}\nmat4 matrixFromRT (vec4 q, vec3 p){\n float x2 = q.x + q.x;\n float y2 = q.y + q.y;\n float z2 = q.z + q.z;\n float xx = q.x * x2;\n float xy = q.x * y2;\n float xz = q.x * z2;\n float yy = q.y * y2;\n float yz = q.y * z2;\n float zz = q.z * z2;\n float wx = q.w * x2;\n float wy = q.w * y2;\n float wz = q.w * z2;\n return mat4(\n 1. - (yy + zz), xy + wz, xz - wy, 0,\n xy - wz, 1. - (xx + zz), yz + wx, 0,\n xz + wy, yz - wx, 1. - (xx + yy), 0,\n p.x, p.y, p.z, 1\n );\n}\nmat4 matFromRTS (vec4 q, vec3 t, vec3 s){\n float x = q.x, y = q.y, z = q.z, w = q.w;\n float x2 = x + x;\n float y2 = y + y;\n float z2 = z + z;\n float xx = x * x2;\n float xy = x * y2;\n float xz = x * z2;\n float yy = y * y2;\n float yz = y * z2;\n float zz = z * z2;\n float wx = w * x2;\n float wy = w * y2;\n float wz = w * z2;\n float sx = s.x;\n float sy = s.y;\n float sz = s.z;\n return mat4((1. - (yy + zz)) * sx, (xy + wz) * sx, (xz - wy) * sx, 0,\n (xy - wz) * sy, (1. - (xx + zz)) * sy, (yz + wx) * sy, 0,\n (xz + wy) * sz, (yz - wx) * sz, (1. - (xx + yy)) * sz, 0,\n t.x, t.y, t.z, 1);\n}\nvec4 quatMultiply (vec4 a, vec4 b){\n vec4 quat;\n quat.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;\n quat.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;\n quat.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;\n quat.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;\n return quat;\n}\nvoid rotateVecFromQuat (inout vec3 v, vec4 q){\n float ix = q.w * v.x + q.y * v.z - q.z * v.y;\n float iy = q.w * v.y + q.z * v.x - q.x * v.z;\n float iz = q.w * v.z + q.x * v.y - q.y * v.x;\n float iw = -q.x * v.x - q.y * v.y - q.z * v.z;\n v.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;\n v.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;\n v.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;\n}\nvec3 rotateInLocalSpace (vec3 pos, vec3 xAxis, vec3 yAxis, vec3 zAxis, vec4 q){\n vec4 viewQuat = quaternionFromAxis(xAxis, yAxis, zAxis);\n vec4 rotQuat = quatMultiply(viewQuat, q);\n rotateVecFromQuat(pos, rotQuat);\n return pos;\n}\nvoid rotateCorner (inout vec2 corner, float angle){\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nuniform Constants {\n vec4 mainTiling_Offset;\n vec4 frameTile_velLenScale;\n vec4 scale;\n};\nuniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nuniform CCLocal {\n highp mat4 cc_matWorld;\n highp mat4 cc_matWorldIT;\n};\nout vec2 uv;\nout vec4 color;\nvoid computeVertPos (inout vec4 pos, vec2 vertOffset, vec4 q, vec3 s\n#if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , mat4 viewInv\n#endif\n#if CC_RENDER_MODE == 1\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float xIndex\n#endif\n) {\n#if CC_RENDER_MODE == 0\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = normalize(vec3(viewInv[0][0], viewInv[1][0], viewInv[2][0]));\n vec3 camY = normalize(vec3(viewInv[0][1], viewInv[1][1], viewInv[2][1]));\n vec3 camZ = normalize(vec3(viewInv[0][2], viewInv[1][2], viewInv[2][2]));\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, camZ, q);\n#elif CC_RENDER_MODE == 1\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz)) * s.x;\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * s.y;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif CC_RENDER_MODE == 2\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = vec3(1, 0, 0);\n vec3 camY = vec3(0, 0, -1);\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, cross(camX, camY), q);\n#elif CC_RENDER_MODE == 3\n vec2 viewSpaceVert = vec2(vertOffset.x * s.x, vertOffset.y * s.y);\n rotateCorner(viewSpaceVert, q.z);\n vec3 camX = normalize(vec3(cc_matView[0][0], cc_matView[1][0], cc_matView[2][0]));\n vec3 camY = vec3(0, 1, 0);\n vec3 offset = camX * viewSpaceVert.x + camY * viewSpaceVert.y;\n pos.xyz += offset;\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV (float frameIndex, vec2 vertIndex, vec2 frameTile){\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n#if CC_RENDER_MODE != 4\n vertIndex.y = 1. - vertIndex.y;\n#endif\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nuniform SampleConstants {\n float sampleInterval;\n float sampleNum;\n};\nuniform TickConstants {\n vec4 u_worldRot;\n vec4 u_timeDelta;\n};\nin vec4 a_position_starttime;\nin vec4 a_size_uv;\nin vec4 a_rotation_uv;\nin vec4 a_color;\nin vec4 a_dir_life;\nin float a_rndSeed;\n#if CC_RENDER_MODE == 4\n in vec3 a_texCoord;\n in vec3 a_texCoord3;\n in vec3 a_normal;\n in vec4 a_color1;\n#endif\nvec3 unpackCurveData (sampler2D tex, vec2 coord) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n return mix(a.xyz, b.xyz, c);\n}\nvec3 unpackCurveData (sampler2D tex, vec2 coord, out float w) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n w = mix(a.w, b.w, c);\n return mix(a.xyz, b.xyz, c);\n}\nfloat pseudoRandom (float seed) {\n seed = mod(seed, 233280.);\n float q = (seed * 9301. + 49297.) / 233280.;\n return fract(q);\n}\n#if COLOR_OVER_TIME_MODULE_ENABLE\n uniform sampler2D color_over_time_tex0;\n uniform ColorConstant {\n int u_color_mode;\n };\n#endif\n#if ROTATION_OVER_TIME_MODULE_ENABLE\n uniform sampler2D rotation_over_time_tex0;\n uniform RotationConstant {\n int u_rotation_mode;\n };\n#endif\n#if SIZE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D size_over_time_tex0;\n uniform SizeConstant {\n int u_size_mode;\n };\n#endif\n#if FORCE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D force_over_time_tex0;\n uniform ForceConstant {\n int u_force_mode;\n int u_force_space;\n };\n#endif\n#if VELOCITY_OVER_TIME_MODULE_ENABLE\n uniform sampler2D velocity_over_time_tex0;\n uniform VelocityConstant {\n int u_velocity_mode;\n int u_velocity_space;\n };\n#endif\n#if TEXTURE_ANIMATION_MODULE_ENABLE\n uniform sampler2D texture_animation_tex0;\n uniform AnimationConstant {\n vec4 u_anim_info;\n };\n#endif\nfloat repeat (float t, float length) {\n return t - floor(t / length) * length;\n}\nvec4 rotateQuat (vec4 p, vec4 q) {\n vec3 iv = cross(q.xyz, p.xyz) + q.w * p.xyz;\n vec3 res = p.xyz + 2.0 * cross(q.xyz, iv);\n return vec4(res.xyz, p.w);\n}\nvec4 gpvs_main () {\n float activeTime = u_timeDelta.x - a_position_starttime.w;\n float normalizedTime = clamp(activeTime / a_dir_life.w, 0.0, 1.0);\n vec2 timeCoord0 = vec2(normalizedTime, 0.);\n vec2 timeCoord1 = vec2(normalizedTime, 1.);\n #if CC_RENDER_MODE == 4\n vec2 vertIdx = vec2(a_texCoord.x, a_texCoord.y);\n #else\n vec2 vertIdx = vec2(a_size_uv.w, a_rotation_uv.w);\n #endif\n vec4 velocity = vec4(a_dir_life.xyz, 0.);\n vec4 pos = vec4(a_position_starttime.xyz, 1.);\n vec3 size = a_size_uv.xyz;\n #if SIZE_OVER_TIME_MODULE_ENABLE\n if (u_size_mode == 1) {\n size *= unpackCurveData(size_over_time_tex0, timeCoord0);\n } else {\n vec3 size_0 = unpackCurveData(size_over_time_tex0, timeCoord0);\n vec3 size_1 = unpackCurveData(size_over_time_tex0, timeCoord1);\n float factor_s = pseudoRandom(a_rndSeed + 39825.);\n size *= mix(size_0, size_1, factor_s);\n }\n #endif\n vec3 compScale = scale.xyz * size;\n #if FORCE_OVER_TIME_MODULE_ENABLE\n vec3 forceAnim = vec3(0.);\n if (u_force_mode == 1) {\n forceAnim = unpackCurveData(force_over_time_tex0, timeCoord0);\n } else {\n vec3 force_0 = unpackCurveData(force_over_time_tex0, timeCoord0);\n vec3 force_1 = unpackCurveData(force_over_time_tex0, timeCoord1);\n float factor_f = pseudoRandom(a_rndSeed + 212165.);\n forceAnim = mix(force_0, force_1, factor_f);\n }\n vec4 forceTrack = vec4(forceAnim, 0.);\n if (u_force_space == 0) {\n forceTrack = rotateQuat(forceTrack, u_worldRot);\n }\n velocity.xyz += forceTrack.xyz * normalizedTime * a_dir_life.w;\n #endif\n #if VELOCITY_OVER_TIME_MODULE_ENABLE\n float speedModifier0 = 1.;\n float speedModifier1 = 1.;\n vec3 velocityAnim = vec3(0.);\n if (u_velocity_mode == 1) {\n velocityAnim = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n } else {\n vec3 vectory_0 = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n vec3 vectory_1 = unpackCurveData(velocity_over_time_tex0, timeCoord1, speedModifier1);\n float factor_v = pseudoRandom(a_rndSeed + 197866.);\n velocityAnim = mix(vectory_0, vectory_1, factor_v);\n speedModifier0 = mix(speedModifier0, speedModifier1, factor_v);\n }\n vec4 velocityTrack = vec4(velocityAnim, 0.);\n if (u_velocity_space == 0) {\n velocityTrack = rotateQuat(velocityTrack, u_worldRot);\n }\n velocity.xyz += velocityTrack.xyz;\n velocity.xyz *= speedModifier0;\n #endif\n pos.xyz += velocity.xyz * normalizedTime * a_dir_life.w;\n #if !CC_USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if CC_RENDER_MODE == 1\n velocity = rotateQuat(velocity, u_worldRot);\n #endif\n #endif\n vec3 rotation = a_rotation_uv.xyz;\n #if ROTATION_OVER_TIME_MODULE_ENABLE\n if (u_rotation_mode == 1) {\n rotation += unpackCurveData(rotation_over_time_tex0, timeCoord0) * normalizedTime * a_dir_life.w;\n } else {\n vec3 rotation_0 = unpackCurveData(rotation_over_time_tex0, timeCoord0);\n vec3 rotation_1 = unpackCurveData(rotation_over_time_tex0, timeCoord1);\n float factor_r = pseudoRandom(a_rndSeed + 125292.);\n rotation += mix(rotation_0, rotation_1, factor_r);\n }\n #endif\n #if COLOR_OVER_TIME_MODULE_ENABLE\n if (u_color_mode == 1) {\n color = a_color * texture(color_over_time_tex0, timeCoord0);\n } else {\n vec4 color_0 = texture(color_over_time_tex0, timeCoord0);\n vec4 color_1 = texture(color_over_time_tex0, timeCoord1);\n float factor_c = pseudoRandom(a_rndSeed + 91041.);\n color = a_color * mix(color_0, color_1, factor_c);\n }\n #else\n color = a_color;\n #endif\n #if CC_RENDER_MODE != 4\n vec2 cornerOffset = vec2((vertIdx - 0.5));\n #if CC_RENDER_MODE == 0\n vec3 rotEuler = rotation.xyz;\n #elif CC_RENDER_MODE == 1\n vec3 rotEuler = vec3(0.);\n #else\n vec3 rotEuler = vec3(0., 0., rotation.z);\n #endif\n computeVertPos(pos, cornerOffset, quaternionFromEuler(rotEuler), compScale\n #if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , cc_matViewInv\n #endif\n #if CC_RENDER_MODE == 1\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_size_uv.w\n #endif\n );\n #else\n mat4 xformNoScale = matrixFromRT(quaternionFromEuler(rotation), pos.xyz);\n mat4 xform = matFromRTS(quaternionFromEuler(rotation), pos.xyz, compScale);\n pos = xform * vec4(a_texCoord3, 1);\n vec4 normal = xformNoScale * vec4(a_normal, 0);\n color *= a_color1;\n #endif\n pos = cc_matViewProj * pos;\n float frameIndex = 0.;\n #if TEXTURE_ANIMATION_MODULE_ENABLE\n float startFrame = 0.;\n vec3 frameInfo = vec3(0.);\n if (int(u_anim_info.x) == 1) {\n frameInfo = unpackCurveData(texture_animation_tex0, timeCoord0);\n } else {\n vec3 frameInfo0 = unpackCurveData(texture_animation_tex0, timeCoord0);\n vec3 frameInfo1 = unpackCurveData(texture_animation_tex0, timeCoord1);\n float factor_t = pseudoRandom(a_rndSeed + 90794.);\n frameInfo = mix(frameInfo0, frameInfo1, factor_t);\n }\n startFrame = frameInfo.x / u_anim_info.y;\n frameIndex = repeat(u_anim_info.z * (frameInfo.y + startFrame), 1.);\n #endif\n uv = computeUV(frameIndex, vertIdx, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n return pos;\n}\nvoid main() { gl_Position = gpvs_main(); }`, + "frag": `\nprecision mediump float;\nuniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nvec3 SRGBToLinear (vec3 gamma) {\n return gamma * gamma;\n}\nvec4 CCFragOutput (vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nin vec2 uv;\nin vec4 color;\nuniform sampler2D mainTexture;\nuniform FragConstants {\n vec4 tintColor;\n};\nvec4 add () {\n vec4 col = 2.0 * color * tintColor * texture(mainTexture, uv);\n return CCFragOutput(col);\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = add(); }` + }, + "glsl1": { + "vert": `\nprecision mediump float;\nvec4 quaternionFromAxis (vec3 xAxis,vec3 yAxis,vec3 zAxis){\n mat3 m = mat3(xAxis,yAxis,zAxis);\n float trace = m[0][0] + m[1][1] + m[2][2];\n vec4 quat;\n if (trace > 0.) {\n float s = 0.5 / sqrt(trace + 1.0);\n quat.w = 0.25 / s;\n quat.x = (m[2][1] - m[1][2]) * s;\n quat.y = (m[0][2] - m[2][0]) * s;\n quat.z = (m[1][0] - m[0][1]) * s;\n } else if ((m[0][0] > m[1][1]) && (m[0][0] > m[2][2])) {\n float s = 2.0 * sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]);\n quat.w = (m[2][1] - m[1][2]) / s;\n quat.x = 0.25 * s;\n quat.y = (m[0][1] + m[1][0]) / s;\n quat.z = (m[0][2] + m[2][0]) / s;\n } else if (m[1][1] > m[2][2]) {\n float s = 2.0 * sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]);\n quat.w = (m[0][2] - m[2][0]) / s;\n quat.x = (m[0][1] + m[1][0]) / s;\n quat.y = 0.25 * s;\n quat.z = (m[1][2] + m[2][1]) / s;\n } else {\n float s = 2.0 * sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]);\n quat.w = (m[1][0] - m[0][1]) / s;\n quat.x = (m[0][2] + m[2][0]) / s;\n quat.y = (m[1][2] + m[2][1]) / s;\n quat.z = 0.25 * s;\n }\n float len = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;\n if (len > 0.) {\n len = 1. / sqrt(len);\n quat.x = quat.x * len;\n quat.y = quat.y * len;\n quat.z = quat.z * len;\n quat.w = quat.w * len;\n }\n return quat;\n}\nvec4 quaternionFromEuler (vec3 angle){\n float x = angle.x / 2.;\n float y = angle.y / 2.;\n float z = angle.z / 2.;\n float sx = sin(x);\n float cx = cos(x);\n float sy = sin(y);\n float cy = cos(y);\n float sz = sin(z);\n float cz = cos(z);\n vec4 quat = vec4(0);\n quat.x = sx * cy * cz + cx * sy * sz;\n quat.y = cx * sy * cz + sx * cy * sz;\n quat.z = cx * cy * sz - sx * sy * cz;\n quat.w = cx * cy * cz - sx * sy * sz;\n return quat;\n}\nmat4 matrixFromRT (vec4 q, vec3 p){\n float x2 = q.x + q.x;\n float y2 = q.y + q.y;\n float z2 = q.z + q.z;\n float xx = q.x * x2;\n float xy = q.x * y2;\n float xz = q.x * z2;\n float yy = q.y * y2;\n float yz = q.y * z2;\n float zz = q.z * z2;\n float wx = q.w * x2;\n float wy = q.w * y2;\n float wz = q.w * z2;\n return mat4(\n 1. - (yy + zz), xy + wz, xz - wy, 0,\n xy - wz, 1. - (xx + zz), yz + wx, 0,\n xz + wy, yz - wx, 1. - (xx + yy), 0,\n p.x, p.y, p.z, 1\n );\n}\nmat4 matFromRTS (vec4 q, vec3 t, vec3 s){\n float x = q.x, y = q.y, z = q.z, w = q.w;\n float x2 = x + x;\n float y2 = y + y;\n float z2 = z + z;\n float xx = x * x2;\n float xy = x * y2;\n float xz = x * z2;\n float yy = y * y2;\n float yz = y * z2;\n float zz = z * z2;\n float wx = w * x2;\n float wy = w * y2;\n float wz = w * z2;\n float sx = s.x;\n float sy = s.y;\n float sz = s.z;\n return mat4((1. - (yy + zz)) * sx, (xy + wz) * sx, (xz - wy) * sx, 0,\n (xy - wz) * sy, (1. - (xx + zz)) * sy, (yz + wx) * sy, 0,\n (xz + wy) * sz, (yz - wx) * sz, (1. - (xx + yy)) * sz, 0,\n t.x, t.y, t.z, 1);\n}\nvec4 quatMultiply (vec4 a, vec4 b){\n vec4 quat;\n quat.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;\n quat.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;\n quat.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;\n quat.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;\n return quat;\n}\nvoid rotateVecFromQuat (inout vec3 v, vec4 q){\n float ix = q.w * v.x + q.y * v.z - q.z * v.y;\n float iy = q.w * v.y + q.z * v.x - q.x * v.z;\n float iz = q.w * v.z + q.x * v.y - q.y * v.x;\n float iw = -q.x * v.x - q.y * v.y - q.z * v.z;\n v.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;\n v.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;\n v.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;\n}\nvec3 rotateInLocalSpace (vec3 pos, vec3 xAxis, vec3 yAxis, vec3 zAxis, vec4 q){\n vec4 viewQuat = quaternionFromAxis(xAxis, yAxis, zAxis);\n vec4 rotQuat = quatMultiply(viewQuat, q);\n rotateVecFromQuat(pos, rotQuat);\n return pos;\n}\nvoid rotateCorner (inout vec2 corner, float angle){\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nuniform vec4 mainTiling_Offset;\nuniform vec4 frameTile_velLenScale;\nuniform vec4 scale;\nuniform highp mat4 cc_matView;\nuniform highp mat4 cc_matViewInv;\nuniform highp mat4 cc_matViewProj;\nuniform highp vec4 cc_cameraPos;\nuniform highp mat4 cc_matWorld;\nvarying vec2 uv;\nvarying vec4 color;\nvoid computeVertPos (inout vec4 pos, vec2 vertOffset, vec4 q, vec3 s\n#if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , mat4 viewInv\n#endif\n#if CC_RENDER_MODE == 1\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float xIndex\n#endif\n) {\n#if CC_RENDER_MODE == 0\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = normalize(vec3(viewInv[0][0], viewInv[1][0], viewInv[2][0]));\n vec3 camY = normalize(vec3(viewInv[0][1], viewInv[1][1], viewInv[2][1]));\n vec3 camZ = normalize(vec3(viewInv[0][2], viewInv[1][2], viewInv[2][2]));\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, camZ, q);\n#elif CC_RENDER_MODE == 1\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz)) * s.x;\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * s.y;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif CC_RENDER_MODE == 2\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = vec3(1, 0, 0);\n vec3 camY = vec3(0, 0, -1);\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, cross(camX, camY), q);\n#elif CC_RENDER_MODE == 3\n vec2 viewSpaceVert = vec2(vertOffset.x * s.x, vertOffset.y * s.y);\n rotateCorner(viewSpaceVert, q.z);\n vec3 camX = normalize(vec3(cc_matView[0][0], cc_matView[1][0], cc_matView[2][0]));\n vec3 camY = vec3(0, 1, 0);\n vec3 offset = camX * viewSpaceVert.x + camY * viewSpaceVert.y;\n pos.xyz += offset;\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV (float frameIndex, vec2 vertIndex, vec2 frameTile){\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n#if CC_RENDER_MODE != 4\n vertIndex.y = 1. - vertIndex.y;\n#endif\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nuniform float sampleInterval;\nuniform float sampleNum;\nuniform vec4 u_worldRot;\nuniform vec4 u_timeDelta;\nattribute vec4 a_position_starttime;\nattribute vec4 a_size_uv;\nattribute vec4 a_rotation_uv;\nattribute vec4 a_color;\nattribute vec4 a_dir_life;\nattribute float a_rndSeed;\n#if CC_RENDER_MODE == 4\n attribute vec3 a_texCoord;\n attribute vec3 a_texCoord3;\n attribute vec3 a_normal;\n attribute vec4 a_color1;\n#endif\nvec3 unpackCurveData (sampler2D tex, vec2 coord) {\n vec4 a = texture2D(tex, coord);\n vec4 b = texture2D(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n return mix(a.xyz, b.xyz, c);\n}\nvec3 unpackCurveData (sampler2D tex, vec2 coord, out float w) {\n vec4 a = texture2D(tex, coord);\n vec4 b = texture2D(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n w = mix(a.w, b.w, c);\n return mix(a.xyz, b.xyz, c);\n}\nfloat pseudoRandom (float seed) {\n seed = mod(seed, 233280.);\n float q = (seed * 9301. + 49297.) / 233280.;\n return fract(q);\n}\n#if COLOR_OVER_TIME_MODULE_ENABLE\n uniform sampler2D color_over_time_tex0;\n uniform int u_color_mode;\n#endif\n#if ROTATION_OVER_TIME_MODULE_ENABLE\n uniform sampler2D rotation_over_time_tex0;\n uniform int u_rotation_mode;\n#endif\n#if SIZE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D size_over_time_tex0;\n uniform int u_size_mode;\n#endif\n#if FORCE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D force_over_time_tex0;\n uniform int u_force_mode;\nuniform int u_force_space;\n#endif\n#if VELOCITY_OVER_TIME_MODULE_ENABLE\n uniform sampler2D velocity_over_time_tex0;\n uniform int u_velocity_mode;\nuniform int u_velocity_space;\n#endif\n#if TEXTURE_ANIMATION_MODULE_ENABLE\n uniform sampler2D texture_animation_tex0;\n uniform vec4 u_anim_info;\n#endif\nfloat repeat (float t, float length) {\n return t - floor(t / length) * length;\n}\nvec4 rotateQuat (vec4 p, vec4 q) {\n vec3 iv = cross(q.xyz, p.xyz) + q.w * p.xyz;\n vec3 res = p.xyz + 2.0 * cross(q.xyz, iv);\n return vec4(res.xyz, p.w);\n}\nvec4 gpvs_main () {\n float activeTime = u_timeDelta.x - a_position_starttime.w;\n float normalizedTime = clamp(activeTime / a_dir_life.w, 0.0, 1.0);\n vec2 timeCoord0 = vec2(normalizedTime, 0.);\n vec2 timeCoord1 = vec2(normalizedTime, 1.);\n #if CC_RENDER_MODE == 4\n vec2 vertIdx = vec2(a_texCoord.x, a_texCoord.y);\n #else\n vec2 vertIdx = vec2(a_size_uv.w, a_rotation_uv.w);\n #endif\n vec4 velocity = vec4(a_dir_life.xyz, 0.);\n vec4 pos = vec4(a_position_starttime.xyz, 1.);\n vec3 size = a_size_uv.xyz;\n #if SIZE_OVER_TIME_MODULE_ENABLE\n if (u_size_mode == 1) {\n size *= unpackCurveData(size_over_time_tex0, timeCoord0);\n } else {\n vec3 size_0 = unpackCurveData(size_over_time_tex0, timeCoord0);\n vec3 size_1 = unpackCurveData(size_over_time_tex0, timeCoord1);\n float factor_s = pseudoRandom(a_rndSeed + 39825.);\n size *= mix(size_0, size_1, factor_s);\n }\n #endif\n vec3 compScale = scale.xyz * size;\n #if FORCE_OVER_TIME_MODULE_ENABLE\n vec3 forceAnim = vec3(0.);\n if (u_force_mode == 1) {\n forceAnim = unpackCurveData(force_over_time_tex0, timeCoord0);\n } else {\n vec3 force_0 = unpackCurveData(force_over_time_tex0, timeCoord0);\n vec3 force_1 = unpackCurveData(force_over_time_tex0, timeCoord1);\n float factor_f = pseudoRandom(a_rndSeed + 212165.);\n forceAnim = mix(force_0, force_1, factor_f);\n }\n vec4 forceTrack = vec4(forceAnim, 0.);\n if (u_force_space == 0) {\n forceTrack = rotateQuat(forceTrack, u_worldRot);\n }\n velocity.xyz += forceTrack.xyz * normalizedTime * a_dir_life.w;\n #endif\n #if VELOCITY_OVER_TIME_MODULE_ENABLE\n float speedModifier0 = 1.;\n float speedModifier1 = 1.;\n vec3 velocityAnim = vec3(0.);\n if (u_velocity_mode == 1) {\n velocityAnim = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n } else {\n vec3 vectory_0 = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n vec3 vectory_1 = unpackCurveData(velocity_over_time_tex0, timeCoord1, speedModifier1);\n float factor_v = pseudoRandom(a_rndSeed + 197866.);\n velocityAnim = mix(vectory_0, vectory_1, factor_v);\n speedModifier0 = mix(speedModifier0, speedModifier1, factor_v);\n }\n vec4 velocityTrack = vec4(velocityAnim, 0.);\n if (u_velocity_space == 0) {\n velocityTrack = rotateQuat(velocityTrack, u_worldRot);\n }\n velocity.xyz += velocityTrack.xyz;\n velocity.xyz *= speedModifier0;\n #endif\n pos.xyz += velocity.xyz * normalizedTime * a_dir_life.w;\n #if !CC_USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if CC_RENDER_MODE == 1\n velocity = rotateQuat(velocity, u_worldRot);\n #endif\n #endif\n vec3 rotation = a_rotation_uv.xyz;\n #if ROTATION_OVER_TIME_MODULE_ENABLE\n if (u_rotation_mode == 1) {\n rotation += unpackCurveData(rotation_over_time_tex0, timeCoord0) * normalizedTime * a_dir_life.w;\n } else {\n vec3 rotation_0 = unpackCurveData(rotation_over_time_tex0, timeCoord0);\n vec3 rotation_1 = unpackCurveData(rotation_over_time_tex0, timeCoord1);\n float factor_r = pseudoRandom(a_rndSeed + 125292.);\n rotation += mix(rotation_0, rotation_1, factor_r);\n }\n #endif\n #if COLOR_OVER_TIME_MODULE_ENABLE\n if (u_color_mode == 1) {\n color = a_color * texture2D(color_over_time_tex0, timeCoord0);\n } else {\n vec4 color_0 = texture2D(color_over_time_tex0, timeCoord0);\n vec4 color_1 = texture2D(color_over_time_tex0, timeCoord1);\n float factor_c = pseudoRandom(a_rndSeed + 91041.);\n color = a_color * mix(color_0, color_1, factor_c);\n }\n #else\n color = a_color;\n #endif\n #if CC_RENDER_MODE != 4\n vec2 cornerOffset = vec2((vertIdx - 0.5));\n #if CC_RENDER_MODE == 0\n vec3 rotEuler = rotation.xyz;\n #elif CC_RENDER_MODE == 1\n vec3 rotEuler = vec3(0.);\n #else\n vec3 rotEuler = vec3(0., 0., rotation.z);\n #endif\n computeVertPos(pos, cornerOffset, quaternionFromEuler(rotEuler), compScale\n #if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , cc_matViewInv\n #endif\n #if CC_RENDER_MODE == 1\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_size_uv.w\n #endif\n );\n #else\n mat4 xformNoScale = matrixFromRT(quaternionFromEuler(rotation), pos.xyz);\n mat4 xform = matFromRTS(quaternionFromEuler(rotation), pos.xyz, compScale);\n pos = xform * vec4(a_texCoord3, 1);\n vec4 normal = xformNoScale * vec4(a_normal, 0);\n color *= a_color1;\n #endif\n pos = cc_matViewProj * pos;\n float frameIndex = 0.;\n #if TEXTURE_ANIMATION_MODULE_ENABLE\n float startFrame = 0.;\n vec3 frameInfo = vec3(0.);\n if (int(u_anim_info.x) == 1) {\n frameInfo = unpackCurveData(texture_animation_tex0, timeCoord0);\n } else {\n vec3 frameInfo0 = unpackCurveData(texture_animation_tex0, timeCoord0);\n vec3 frameInfo1 = unpackCurveData(texture_animation_tex0, timeCoord1);\n float factor_t = pseudoRandom(a_rndSeed + 90794.);\n frameInfo = mix(frameInfo0, frameInfo1, factor_t);\n }\n startFrame = frameInfo.x / u_anim_info.y;\n frameIndex = repeat(u_anim_info.z * (frameInfo.y + startFrame), 1.);\n #endif\n uv = computeUV(frameIndex, vertIdx, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n return pos;\n}\nvoid main() { gl_Position = gpvs_main(); }`, + "frag": `\nprecision mediump float;\nuniform mediump vec4 cc_exposure;\nvec3 SRGBToLinear (vec3 gamma) {\n return gamma * gamma;\n}\nvec4 CCFragOutput (vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nvarying vec2 uv;\nvarying vec4 color;\nuniform sampler2D mainTexture;\nuniform vec4 tintColor;\nvec4 add () {\n vec4 col = 2.0 * color * tintColor * texture2D(mainTexture, uv);\n return CCFragOutput(col);\n}\nvoid main() { gl_FragColor = add(); }` + }, + "glsl4": { + "vert": `\nprecision mediump float;\nvec4 quaternionFromAxis (vec3 xAxis,vec3 yAxis,vec3 zAxis){\n mat3 m = mat3(xAxis,yAxis,zAxis);\n float trace = m[0][0] + m[1][1] + m[2][2];\n vec4 quat;\n if (trace > 0.) {\n float s = 0.5 / sqrt(trace + 1.0);\n quat.w = 0.25 / s;\n quat.x = (m[2][1] - m[1][2]) * s;\n quat.y = (m[0][2] - m[2][0]) * s;\n quat.z = (m[1][0] - m[0][1]) * s;\n } else if ((m[0][0] > m[1][1]) && (m[0][0] > m[2][2])) {\n float s = 2.0 * sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]);\n quat.w = (m[2][1] - m[1][2]) / s;\n quat.x = 0.25 * s;\n quat.y = (m[0][1] + m[1][0]) / s;\n quat.z = (m[0][2] + m[2][0]) / s;\n } else if (m[1][1] > m[2][2]) {\n float s = 2.0 * sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]);\n quat.w = (m[0][2] - m[2][0]) / s;\n quat.x = (m[0][1] + m[1][0]) / s;\n quat.y = 0.25 * s;\n quat.z = (m[1][2] + m[2][1]) / s;\n } else {\n float s = 2.0 * sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]);\n quat.w = (m[1][0] - m[0][1]) / s;\n quat.x = (m[0][2] + m[2][0]) / s;\n quat.y = (m[1][2] + m[2][1]) / s;\n quat.z = 0.25 * s;\n }\n float len = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;\n if (len > 0.) {\n len = 1. / sqrt(len);\n quat.x = quat.x * len;\n quat.y = quat.y * len;\n quat.z = quat.z * len;\n quat.w = quat.w * len;\n }\n return quat;\n}\nvec4 quaternionFromEuler (vec3 angle){\n float x = angle.x / 2.;\n float y = angle.y / 2.;\n float z = angle.z / 2.;\n float sx = sin(x);\n float cx = cos(x);\n float sy = sin(y);\n float cy = cos(y);\n float sz = sin(z);\n float cz = cos(z);\n vec4 quat = vec4(0);\n quat.x = sx * cy * cz + cx * sy * sz;\n quat.y = cx * sy * cz + sx * cy * sz;\n quat.z = cx * cy * sz - sx * sy * cz;\n quat.w = cx * cy * cz - sx * sy * sz;\n return quat;\n}\nmat4 matrixFromRT (vec4 q, vec3 p){\n float x2 = q.x + q.x;\n float y2 = q.y + q.y;\n float z2 = q.z + q.z;\n float xx = q.x * x2;\n float xy = q.x * y2;\n float xz = q.x * z2;\n float yy = q.y * y2;\n float yz = q.y * z2;\n float zz = q.z * z2;\n float wx = q.w * x2;\n float wy = q.w * y2;\n float wz = q.w * z2;\n return mat4(\n 1. - (yy + zz), xy + wz, xz - wy, 0,\n xy - wz, 1. - (xx + zz), yz + wx, 0,\n xz + wy, yz - wx, 1. - (xx + yy), 0,\n p.x, p.y, p.z, 1\n );\n}\nmat4 matFromRTS (vec4 q, vec3 t, vec3 s){\n float x = q.x, y = q.y, z = q.z, w = q.w;\n float x2 = x + x;\n float y2 = y + y;\n float z2 = z + z;\n float xx = x * x2;\n float xy = x * y2;\n float xz = x * z2;\n float yy = y * y2;\n float yz = y * z2;\n float zz = z * z2;\n float wx = w * x2;\n float wy = w * y2;\n float wz = w * z2;\n float sx = s.x;\n float sy = s.y;\n float sz = s.z;\n return mat4((1. - (yy + zz)) * sx, (xy + wz) * sx, (xz - wy) * sx, 0,\n (xy - wz) * sy, (1. - (xx + zz)) * sy, (yz + wx) * sy, 0,\n (xz + wy) * sz, (yz - wx) * sz, (1. - (xx + yy)) * sz, 0,\n t.x, t.y, t.z, 1);\n}\nvec4 quatMultiply (vec4 a, vec4 b){\n vec4 quat;\n quat.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;\n quat.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;\n quat.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;\n quat.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;\n return quat;\n}\nvoid rotateVecFromQuat (inout vec3 v, vec4 q){\n float ix = q.w * v.x + q.y * v.z - q.z * v.y;\n float iy = q.w * v.y + q.z * v.x - q.x * v.z;\n float iz = q.w * v.z + q.x * v.y - q.y * v.x;\n float iw = -q.x * v.x - q.y * v.y - q.z * v.z;\n v.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;\n v.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;\n v.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;\n}\nvec3 rotateInLocalSpace (vec3 pos, vec3 xAxis, vec3 yAxis, vec3 zAxis, vec4 q){\n vec4 viewQuat = quaternionFromAxis(xAxis, yAxis, zAxis);\n vec4 rotQuat = quatMultiply(viewQuat, q);\n rotateVecFromQuat(pos, rotQuat);\n return pos;\n}\nvoid rotateCorner (inout vec2 corner, float angle){\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nlayout(binding = 0) uniform Constants {\n vec4 mainTiling_Offset;\n vec4 frameTile_velLenScale;\n vec4 scale;\n};\nlayout(binding = 23) uniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nlayout(binding = 21) uniform CCLocal {\n highp mat4 cc_matWorld;\n highp mat4 cc_matWorldIT;\n};\nlayout(location = 0) out vec2 uv;\nlayout(location = 1) out vec4 color;\nvoid computeVertPos (inout vec4 pos, vec2 vertOffset, vec4 q, vec3 s\n#if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , mat4 viewInv\n#endif\n#if CC_RENDER_MODE == 1\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float xIndex\n#endif\n) {\n#if CC_RENDER_MODE == 0\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = normalize(vec3(viewInv[0][0], viewInv[1][0], viewInv[2][0]));\n vec3 camY = normalize(vec3(viewInv[0][1], viewInv[1][1], viewInv[2][1]));\n vec3 camZ = normalize(vec3(viewInv[0][2], viewInv[1][2], viewInv[2][2]));\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, camZ, q);\n#elif CC_RENDER_MODE == 1\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz)) * s.x;\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * s.y;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif CC_RENDER_MODE == 2\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = vec3(1, 0, 0);\n vec3 camY = vec3(0, 0, -1);\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, cross(camX, camY), q);\n#elif CC_RENDER_MODE == 3\n vec2 viewSpaceVert = vec2(vertOffset.x * s.x, vertOffset.y * s.y);\n rotateCorner(viewSpaceVert, q.z);\n vec3 camX = normalize(vec3(cc_matView[0][0], cc_matView[1][0], cc_matView[2][0]));\n vec3 camY = vec3(0, 1, 0);\n vec3 offset = camX * viewSpaceVert.x + camY * viewSpaceVert.y;\n pos.xyz += offset;\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV (float frameIndex, vec2 vertIndex, vec2 frameTile){\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n#if CC_RENDER_MODE != 4\n vertIndex.y = 1. - vertIndex.y;\n#endif\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nlayout(binding = 1) uniform SampleConstants {\n float sampleInterval;\n float sampleNum;\n};\nlayout(binding = 2) uniform TickConstants {\n vec4 u_worldRot;\n vec4 u_timeDelta;\n};\nlayout(location = 0) in vec4 a_position_starttime;\nlayout(location = 1) in vec4 a_size_uv;\nlayout(location = 2) in vec4 a_rotation_uv;\nlayout(location = 3) in vec4 a_color;\nlayout(location = 4) in vec4 a_dir_life;\nlayout(location = 5) in float a_rndSeed;\n#if CC_RENDER_MODE == 4\n layout(location = 6) in vec3 a_texCoord;\n layout(location = 7) in vec3 a_texCoord3;\n layout(location = 8) in vec3 a_normal;\n layout(location = 9) in vec4 a_color1;\n#endif\nvec3 unpackCurveData (sampler2D tex, vec2 coord) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n return mix(a.xyz, b.xyz, c);\n}\nvec3 unpackCurveData (sampler2D tex, vec2 coord, layout(location = 2) out float w) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n w = mix(a.w, b.w, c);\n return mix(a.xyz, b.xyz, c);\n}\nfloat pseudoRandom (float seed) {\n seed = mod(seed, 233280.);\n float q = (seed * 9301. + 49297.) / 233280.;\n return fract(q);\n}\n#if COLOR_OVER_TIME_MODULE_ENABLE\n layout(binding = 30) uniform sampler2D color_over_time_tex0;\n layout(binding = 3) uniform ColorConstant {\n int u_color_mode;\n };\n#endif\n#if ROTATION_OVER_TIME_MODULE_ENABLE\n layout(binding = 31) uniform sampler2D rotation_over_time_tex0;\n layout(binding = 4) uniform RotationConstant {\n int u_rotation_mode;\n };\n#endif\n#if SIZE_OVER_TIME_MODULE_ENABLE\n layout(binding = 32) uniform sampler2D size_over_time_tex0;\n layout(binding = 5) uniform SizeConstant {\n int u_size_mode;\n };\n#endif\n#if FORCE_OVER_TIME_MODULE_ENABLE\n layout(binding = 33) uniform sampler2D force_over_time_tex0;\n layout(binding = 6) uniform ForceConstant {\n int u_force_mode;\n int u_force_space;\n };\n#endif\n#if VELOCITY_OVER_TIME_MODULE_ENABLE\n layout(binding = 34) uniform sampler2D velocity_over_time_tex0;\n layout(binding = 7) uniform VelocityConstant {\n int u_velocity_mode;\n int u_velocity_space;\n };\n#endif\n#if TEXTURE_ANIMATION_MODULE_ENABLE\n layout(binding = 35) uniform sampler2D texture_animation_tex0;\n layout(binding = 8) uniform AnimationConstant {\n vec4 u_anim_info;\n };\n#endif\nfloat repeat (float t, float length) {\n return t - floor(t / length) * length;\n}\nvec4 rotateQuat (vec4 p, vec4 q) {\n vec3 iv = cross(q.xyz, p.xyz) + q.w * p.xyz;\n vec3 res = p.xyz + 2.0 * cross(q.xyz, iv);\n return vec4(res.xyz, p.w);\n}\nvec4 gpvs_main () {\n float activeTime = u_timeDelta.x - a_position_starttime.w;\n float normalizedTime = clamp(activeTime / a_dir_life.w, 0.0, 1.0);\n vec2 timeCoord0 = vec2(normalizedTime, 0.);\n vec2 timeCoord1 = vec2(normalizedTime, 1.);\n #if CC_RENDER_MODE == 4\n vec2 vertIdx = vec2(a_texCoord.x, a_texCoord.y);\n #else\n vec2 vertIdx = vec2(a_size_uv.w, a_rotation_uv.w);\n #endif\n vec4 velocity = vec4(a_dir_life.xyz, 0.);\n vec4 pos = vec4(a_position_starttime.xyz, 1.);\n vec3 size = a_size_uv.xyz;\n #if SIZE_OVER_TIME_MODULE_ENABLE\n if (u_size_mode == 1) {\n size *= unpackCurveData(size_over_time_tex0, timeCoord0);\n } else {\n vec3 size_0 = unpackCurveData(size_over_time_tex0, timeCoord0);\n vec3 size_1 = unpackCurveData(size_over_time_tex0, timeCoord1);\n float factor_s = pseudoRandom(a_rndSeed + 39825.);\n size *= mix(size_0, size_1, factor_s);\n }\n #endif\n vec3 compScale = scale.xyz * size;\n #if FORCE_OVER_TIME_MODULE_ENABLE\n vec3 forceAnim = vec3(0.);\n if (u_force_mode == 1) {\n forceAnim = unpackCurveData(force_over_time_tex0, timeCoord0);\n } else {\n vec3 force_0 = unpackCurveData(force_over_time_tex0, timeCoord0);\n vec3 force_1 = unpackCurveData(force_over_time_tex0, timeCoord1);\n float factor_f = pseudoRandom(a_rndSeed + 212165.);\n forceAnim = mix(force_0, force_1, factor_f);\n }\n vec4 forceTrack = vec4(forceAnim, 0.);\n if (u_force_space == 0) {\n forceTrack = rotateQuat(forceTrack, u_worldRot);\n }\n velocity.xyz += forceTrack.xyz * normalizedTime * a_dir_life.w;\n #endif\n #if VELOCITY_OVER_TIME_MODULE_ENABLE\n float speedModifier0 = 1.;\n float speedModifier1 = 1.;\n vec3 velocityAnim = vec3(0.);\n if (u_velocity_mode == 1) {\n velocityAnim = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n } else {\n vec3 vectory_0 = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n vec3 vectory_1 = unpackCurveData(velocity_over_time_tex0, timeCoord1, speedModifier1);\n float factor_v = pseudoRandom(a_rndSeed + 197866.);\n velocityAnim = mix(vectory_0, vectory_1, factor_v);\n speedModifier0 = mix(speedModifier0, speedModifier1, factor_v);\n }\n vec4 velocityTrack = vec4(velocityAnim, 0.);\n if (u_velocity_space == 0) {\n velocityTrack = rotateQuat(velocityTrack, u_worldRot);\n }\n velocity.xyz += velocityTrack.xyz;\n velocity.xyz *= speedModifier0;\n #endif\n pos.xyz += velocity.xyz * normalizedTime * a_dir_life.w;\n #if !CC_USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if CC_RENDER_MODE == 1\n velocity = rotateQuat(velocity, u_worldRot);\n #endif\n #endif\n vec3 rotation = a_rotation_uv.xyz;\n #if ROTATION_OVER_TIME_MODULE_ENABLE\n if (u_rotation_mode == 1) {\n rotation += unpackCurveData(rotation_over_time_tex0, timeCoord0) * normalizedTime * a_dir_life.w;\n } else {\n vec3 rotation_0 = unpackCurveData(rotation_over_time_tex0, timeCoord0);\n vec3 rotation_1 = unpackCurveData(rotation_over_time_tex0, timeCoord1);\n float factor_r = pseudoRandom(a_rndSeed + 125292.);\n rotation += mix(rotation_0, rotation_1, factor_r);\n }\n #endif\n #if COLOR_OVER_TIME_MODULE_ENABLE\n if (u_color_mode == 1) {\n color = a_color * texture(color_over_time_tex0, timeCoord0);\n } else {\n vec4 color_0 = texture(color_over_time_tex0, timeCoord0);\n vec4 color_1 = texture(color_over_time_tex0, timeCoord1);\n float factor_c = pseudoRandom(a_rndSeed + 91041.);\n color = a_color * mix(color_0, color_1, factor_c);\n }\n #else\n color = a_color;\n #endif\n #if CC_RENDER_MODE != 4\n vec2 cornerOffset = vec2((vertIdx - 0.5));\n #if CC_RENDER_MODE == 0\n vec3 rotEuler = rotation.xyz;\n #elif CC_RENDER_MODE == 1\n vec3 rotEuler = vec3(0.);\n #else\n vec3 rotEuler = vec3(0., 0., rotation.z);\n #endif\n computeVertPos(pos, cornerOffset, quaternionFromEuler(rotEuler), compScale\n #if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , cc_matViewInv\n #endif\n #if CC_RENDER_MODE == 1\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_size_uv.w\n #endif\n );\n #else\n mat4 xformNoScale = matrixFromRT(quaternionFromEuler(rotation), pos.xyz);\n mat4 xform = matFromRTS(quaternionFromEuler(rotation), pos.xyz, compScale);\n pos = xform * vec4(a_texCoord3, 1);\n vec4 normal = xformNoScale * vec4(a_normal, 0);\n color *= a_color1;\n #endif\n pos = cc_matViewProj * pos;\n float frameIndex = 0.;\n #if TEXTURE_ANIMATION_MODULE_ENABLE\n float startFrame = 0.;\n vec3 frameInfo = vec3(0.);\n if (int(u_anim_info.x) == 1) {\n frameInfo = unpackCurveData(texture_animation_tex0, timeCoord0);\n } else {\n vec3 frameInfo0 = unpackCurveData(texture_animation_tex0, timeCoord0);\n vec3 frameInfo1 = unpackCurveData(texture_animation_tex0, timeCoord1);\n float factor_t = pseudoRandom(a_rndSeed + 90794.);\n frameInfo = mix(frameInfo0, frameInfo1, factor_t);\n }\n startFrame = frameInfo.x / u_anim_info.y;\n frameIndex = repeat(u_anim_info.z * (frameInfo.y + startFrame), 1.);\n #endif\n uv = computeUV(frameIndex, vertIdx, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n return pos;\n}\nvoid main() { gl_Position = gpvs_main(); }`, + "frag": `\nprecision mediump float;\nlayout(binding = 23) uniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nvec3 SRGBToLinear (vec3 gamma) {\n return gamma * gamma;\n}\nvec4 CCFragOutput (vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nlayout(location = 0) in vec2 uv;\nlayout(location = 1) in vec4 color;\nlayout(binding = 36) uniform sampler2D mainTexture;\nlayout(binding = 9) uniform FragConstants {\n vec4 tintColor;\n};\nvec4 add () {\n vec4 col = 2.0 * color * tintColor * texture(mainTexture, uv);\n return CCFragOutput(col);\n}\nlayout(location = 0) out vec4 cc_FragColor;\nvoid main() { cc_FragColor = add(); }` + }, + "varyings": [ + { "name": "uv", "type": 14, "count": 1, "defines": [], "location": 0 }, + { "name": "color", "type": 16, "count": 1, "defines": [], "location": 1 }, + { "name": "w", "type": 13, "count": 1, "defines": [], "location": 2 } + ], + "builtins": { + "globals": { "blocks": [{ "name": "CCGlobal", "defines": [] }], "samplers": [] }, + "locals": { "blocks": [{ "name": "CCLocal", "defines": [] }], "samplers": [] } + }, + "defines": [ + { "name": "CC_RENDER_MODE", "type": "number", "defines": [], "range": [0, 4] }, + { "name": "COLOR_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "ROTATION_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "SIZE_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "FORCE_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "VELOCITY_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "TEXTURE_ANIMATION_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "CC_USE_WORLD_SPACE", "type": "boolean", "defines": [] }, + { "name": "CC_USE_HDR", "type": "boolean", "defines": [] } + ], + "blocks": [ + {"name": "Constants", "defines": [], "binding": 0, "members": [ + { "name": "mainTiling_Offset", "type": 16, "count": 1 }, + { "name": "frameTile_velLenScale", "type": 16, "count": 1 }, + { "name": "scale", "type": 16, "count": 1 } + ]}, + {"name": "SampleConstants", "defines": [], "binding": 1, "members": [ + { "name": "sampleInterval", "type": 13, "count": 1 }, + { "name": "sampleNum", "type": 13, "count": 1 } + ]}, + {"name": "TickConstants", "defines": [], "binding": 2, "members": [ + { "name": "u_worldRot", "type": 16, "count": 1 }, + { "name": "u_timeDelta", "type": 16, "count": 1 } + ]}, + {"name": "ColorConstant", "defines": ["COLOR_OVER_TIME_MODULE_ENABLE"], "binding": 3, "members": [ + { "name": "u_color_mode", "type": 5, "count": 1 } + ]}, + {"name": "RotationConstant", "defines": ["ROTATION_OVER_TIME_MODULE_ENABLE"], "binding": 4, "members": [ + { "name": "u_rotation_mode", "type": 5, "count": 1 } + ]}, + {"name": "SizeConstant", "defines": ["SIZE_OVER_TIME_MODULE_ENABLE"], "binding": 5, "members": [ + { "name": "u_size_mode", "type": 5, "count": 1 } + ]}, + {"name": "ForceConstant", "defines": ["FORCE_OVER_TIME_MODULE_ENABLE"], "binding": 6, "members": [ + { "name": "u_force_mode", "type": 5, "count": 1 }, + { "name": "u_force_space", "type": 5, "count": 1 } + ]}, + {"name": "VelocityConstant", "defines": ["VELOCITY_OVER_TIME_MODULE_ENABLE"], "binding": 7, "members": [ + { "name": "u_velocity_mode", "type": 5, "count": 1 }, + { "name": "u_velocity_space", "type": 5, "count": 1 } + ]}, + {"name": "AnimationConstant", "defines": ["TEXTURE_ANIMATION_MODULE_ENABLE"], "binding": 8, "members": [ + { "name": "u_anim_info", "type": 16, "count": 1 } + ]}, + {"name": "FragConstants", "defines": [], "binding": 9, "members": [ + { "name": "tintColor", "type": 16, "count": 1 } + ]} + ], + "samplers": [ + { "name": "color_over_time_tex0", "type": 28, "count": 1, "defines": ["COLOR_OVER_TIME_MODULE_ENABLE"], "binding": 30 }, + { "name": "rotation_over_time_tex0", "type": 28, "count": 1, "defines": ["ROTATION_OVER_TIME_MODULE_ENABLE"], "binding": 31 }, + { "name": "size_over_time_tex0", "type": 28, "count": 1, "defines": ["SIZE_OVER_TIME_MODULE_ENABLE"], "binding": 32 }, + { "name": "force_over_time_tex0", "type": 28, "count": 1, "defines": ["FORCE_OVER_TIME_MODULE_ENABLE"], "binding": 33 }, + { "name": "velocity_over_time_tex0", "type": 28, "count": 1, "defines": ["VELOCITY_OVER_TIME_MODULE_ENABLE"], "binding": 34 }, + { "name": "texture_animation_tex0", "type": 28, "count": 1, "defines": ["TEXTURE_ANIMATION_MODULE_ENABLE"], "binding": 35 }, + { "name": "mainTexture", "type": 28, "count": 1, "defines": [], "binding": 36 } + ], + "attributes": [ + { "name": "a_position_starttime", "type": 16, "count": 1, "defines": [], "format": 43, "location": 0 }, + { "name": "a_size_uv", "type": 16, "count": 1, "defines": [], "format": 43, "location": 1 }, + { "name": "a_rotation_uv", "type": 16, "count": 1, "defines": [], "format": 43, "location": 2 }, + { "name": "a_color", "type": 16, "count": 1, "defines": [], "format": 43, "location": 3 }, + { "name": "a_dir_life", "type": 16, "count": 1, "defines": [], "format": 43, "location": 4 }, + { "name": "a_rndSeed", "type": 13, "count": 1, "defines": [], "format": 11, "location": 5 }, + { "name": "a_texCoord", "type": 15, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 32, "location": 6 }, + { "name": "a_texCoord3", "type": 15, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 32, "location": 7 }, + { "name": "a_normal", "type": 15, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 32, "location": 8 }, + { "name": "a_color1", "type": 16, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 43, "location": 9 } + ] + }, + { + "name": "builtin-particle-gpu|particle-vs-gpu:gpvs_main|tinted-fs:multiply", + "hash": 2389851033, + "glsl3": { + "vert": `\nprecision mediump float;\nvec4 quaternionFromAxis (vec3 xAxis,vec3 yAxis,vec3 zAxis){\n mat3 m = mat3(xAxis,yAxis,zAxis);\n float trace = m[0][0] + m[1][1] + m[2][2];\n vec4 quat;\n if (trace > 0.) {\n float s = 0.5 / sqrt(trace + 1.0);\n quat.w = 0.25 / s;\n quat.x = (m[2][1] - m[1][2]) * s;\n quat.y = (m[0][2] - m[2][0]) * s;\n quat.z = (m[1][0] - m[0][1]) * s;\n } else if ((m[0][0] > m[1][1]) && (m[0][0] > m[2][2])) {\n float s = 2.0 * sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]);\n quat.w = (m[2][1] - m[1][2]) / s;\n quat.x = 0.25 * s;\n quat.y = (m[0][1] + m[1][0]) / s;\n quat.z = (m[0][2] + m[2][0]) / s;\n } else if (m[1][1] > m[2][2]) {\n float s = 2.0 * sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]);\n quat.w = (m[0][2] - m[2][0]) / s;\n quat.x = (m[0][1] + m[1][0]) / s;\n quat.y = 0.25 * s;\n quat.z = (m[1][2] + m[2][1]) / s;\n } else {\n float s = 2.0 * sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]);\n quat.w = (m[1][0] - m[0][1]) / s;\n quat.x = (m[0][2] + m[2][0]) / s;\n quat.y = (m[1][2] + m[2][1]) / s;\n quat.z = 0.25 * s;\n }\n float len = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;\n if (len > 0.) {\n len = 1. / sqrt(len);\n quat.x = quat.x * len;\n quat.y = quat.y * len;\n quat.z = quat.z * len;\n quat.w = quat.w * len;\n }\n return quat;\n}\nvec4 quaternionFromEuler (vec3 angle){\n float x = angle.x / 2.;\n float y = angle.y / 2.;\n float z = angle.z / 2.;\n float sx = sin(x);\n float cx = cos(x);\n float sy = sin(y);\n float cy = cos(y);\n float sz = sin(z);\n float cz = cos(z);\n vec4 quat = vec4(0);\n quat.x = sx * cy * cz + cx * sy * sz;\n quat.y = cx * sy * cz + sx * cy * sz;\n quat.z = cx * cy * sz - sx * sy * cz;\n quat.w = cx * cy * cz - sx * sy * sz;\n return quat;\n}\nmat4 matrixFromRT (vec4 q, vec3 p){\n float x2 = q.x + q.x;\n float y2 = q.y + q.y;\n float z2 = q.z + q.z;\n float xx = q.x * x2;\n float xy = q.x * y2;\n float xz = q.x * z2;\n float yy = q.y * y2;\n float yz = q.y * z2;\n float zz = q.z * z2;\n float wx = q.w * x2;\n float wy = q.w * y2;\n float wz = q.w * z2;\n return mat4(\n 1. - (yy + zz), xy + wz, xz - wy, 0,\n xy - wz, 1. - (xx + zz), yz + wx, 0,\n xz + wy, yz - wx, 1. - (xx + yy), 0,\n p.x, p.y, p.z, 1\n );\n}\nmat4 matFromRTS (vec4 q, vec3 t, vec3 s){\n float x = q.x, y = q.y, z = q.z, w = q.w;\n float x2 = x + x;\n float y2 = y + y;\n float z2 = z + z;\n float xx = x * x2;\n float xy = x * y2;\n float xz = x * z2;\n float yy = y * y2;\n float yz = y * z2;\n float zz = z * z2;\n float wx = w * x2;\n float wy = w * y2;\n float wz = w * z2;\n float sx = s.x;\n float sy = s.y;\n float sz = s.z;\n return mat4((1. - (yy + zz)) * sx, (xy + wz) * sx, (xz - wy) * sx, 0,\n (xy - wz) * sy, (1. - (xx + zz)) * sy, (yz + wx) * sy, 0,\n (xz + wy) * sz, (yz - wx) * sz, (1. - (xx + yy)) * sz, 0,\n t.x, t.y, t.z, 1);\n}\nvec4 quatMultiply (vec4 a, vec4 b){\n vec4 quat;\n quat.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;\n quat.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;\n quat.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;\n quat.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;\n return quat;\n}\nvoid rotateVecFromQuat (inout vec3 v, vec4 q){\n float ix = q.w * v.x + q.y * v.z - q.z * v.y;\n float iy = q.w * v.y + q.z * v.x - q.x * v.z;\n float iz = q.w * v.z + q.x * v.y - q.y * v.x;\n float iw = -q.x * v.x - q.y * v.y - q.z * v.z;\n v.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;\n v.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;\n v.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;\n}\nvec3 rotateInLocalSpace (vec3 pos, vec3 xAxis, vec3 yAxis, vec3 zAxis, vec4 q){\n vec4 viewQuat = quaternionFromAxis(xAxis, yAxis, zAxis);\n vec4 rotQuat = quatMultiply(viewQuat, q);\n rotateVecFromQuat(pos, rotQuat);\n return pos;\n}\nvoid rotateCorner (inout vec2 corner, float angle){\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nuniform Constants {\n vec4 mainTiling_Offset;\n vec4 frameTile_velLenScale;\n vec4 scale;\n};\nuniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nuniform CCLocal {\n highp mat4 cc_matWorld;\n highp mat4 cc_matWorldIT;\n};\nout vec2 uv;\nout vec4 color;\nvoid computeVertPos (inout vec4 pos, vec2 vertOffset, vec4 q, vec3 s\n#if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , mat4 viewInv\n#endif\n#if CC_RENDER_MODE == 1\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float xIndex\n#endif\n) {\n#if CC_RENDER_MODE == 0\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = normalize(vec3(viewInv[0][0], viewInv[1][0], viewInv[2][0]));\n vec3 camY = normalize(vec3(viewInv[0][1], viewInv[1][1], viewInv[2][1]));\n vec3 camZ = normalize(vec3(viewInv[0][2], viewInv[1][2], viewInv[2][2]));\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, camZ, q);\n#elif CC_RENDER_MODE == 1\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz)) * s.x;\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * s.y;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif CC_RENDER_MODE == 2\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = vec3(1, 0, 0);\n vec3 camY = vec3(0, 0, -1);\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, cross(camX, camY), q);\n#elif CC_RENDER_MODE == 3\n vec2 viewSpaceVert = vec2(vertOffset.x * s.x, vertOffset.y * s.y);\n rotateCorner(viewSpaceVert, q.z);\n vec3 camX = normalize(vec3(cc_matView[0][0], cc_matView[1][0], cc_matView[2][0]));\n vec3 camY = vec3(0, 1, 0);\n vec3 offset = camX * viewSpaceVert.x + camY * viewSpaceVert.y;\n pos.xyz += offset;\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV (float frameIndex, vec2 vertIndex, vec2 frameTile){\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n#if CC_RENDER_MODE != 4\n vertIndex.y = 1. - vertIndex.y;\n#endif\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nuniform SampleConstants {\n float sampleInterval;\n float sampleNum;\n};\nuniform TickConstants {\n vec4 u_worldRot;\n vec4 u_timeDelta;\n};\nin vec4 a_position_starttime;\nin vec4 a_size_uv;\nin vec4 a_rotation_uv;\nin vec4 a_color;\nin vec4 a_dir_life;\nin float a_rndSeed;\n#if CC_RENDER_MODE == 4\n in vec3 a_texCoord;\n in vec3 a_texCoord3;\n in vec3 a_normal;\n in vec4 a_color1;\n#endif\nvec3 unpackCurveData (sampler2D tex, vec2 coord) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n return mix(a.xyz, b.xyz, c);\n}\nvec3 unpackCurveData (sampler2D tex, vec2 coord, out float w) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n w = mix(a.w, b.w, c);\n return mix(a.xyz, b.xyz, c);\n}\nfloat pseudoRandom (float seed) {\n seed = mod(seed, 233280.);\n float q = (seed * 9301. + 49297.) / 233280.;\n return fract(q);\n}\n#if COLOR_OVER_TIME_MODULE_ENABLE\n uniform sampler2D color_over_time_tex0;\n uniform ColorConstant {\n int u_color_mode;\n };\n#endif\n#if ROTATION_OVER_TIME_MODULE_ENABLE\n uniform sampler2D rotation_over_time_tex0;\n uniform RotationConstant {\n int u_rotation_mode;\n };\n#endif\n#if SIZE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D size_over_time_tex0;\n uniform SizeConstant {\n int u_size_mode;\n };\n#endif\n#if FORCE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D force_over_time_tex0;\n uniform ForceConstant {\n int u_force_mode;\n int u_force_space;\n };\n#endif\n#if VELOCITY_OVER_TIME_MODULE_ENABLE\n uniform sampler2D velocity_over_time_tex0;\n uniform VelocityConstant {\n int u_velocity_mode;\n int u_velocity_space;\n };\n#endif\n#if TEXTURE_ANIMATION_MODULE_ENABLE\n uniform sampler2D texture_animation_tex0;\n uniform AnimationConstant {\n vec4 u_anim_info;\n };\n#endif\nfloat repeat (float t, float length) {\n return t - floor(t / length) * length;\n}\nvec4 rotateQuat (vec4 p, vec4 q) {\n vec3 iv = cross(q.xyz, p.xyz) + q.w * p.xyz;\n vec3 res = p.xyz + 2.0 * cross(q.xyz, iv);\n return vec4(res.xyz, p.w);\n}\nvec4 gpvs_main () {\n float activeTime = u_timeDelta.x - a_position_starttime.w;\n float normalizedTime = clamp(activeTime / a_dir_life.w, 0.0, 1.0);\n vec2 timeCoord0 = vec2(normalizedTime, 0.);\n vec2 timeCoord1 = vec2(normalizedTime, 1.);\n #if CC_RENDER_MODE == 4\n vec2 vertIdx = vec2(a_texCoord.x, a_texCoord.y);\n #else\n vec2 vertIdx = vec2(a_size_uv.w, a_rotation_uv.w);\n #endif\n vec4 velocity = vec4(a_dir_life.xyz, 0.);\n vec4 pos = vec4(a_position_starttime.xyz, 1.);\n vec3 size = a_size_uv.xyz;\n #if SIZE_OVER_TIME_MODULE_ENABLE\n if (u_size_mode == 1) {\n size *= unpackCurveData(size_over_time_tex0, timeCoord0);\n } else {\n vec3 size_0 = unpackCurveData(size_over_time_tex0, timeCoord0);\n vec3 size_1 = unpackCurveData(size_over_time_tex0, timeCoord1);\n float factor_s = pseudoRandom(a_rndSeed + 39825.);\n size *= mix(size_0, size_1, factor_s);\n }\n #endif\n vec3 compScale = scale.xyz * size;\n #if FORCE_OVER_TIME_MODULE_ENABLE\n vec3 forceAnim = vec3(0.);\n if (u_force_mode == 1) {\n forceAnim = unpackCurveData(force_over_time_tex0, timeCoord0);\n } else {\n vec3 force_0 = unpackCurveData(force_over_time_tex0, timeCoord0);\n vec3 force_1 = unpackCurveData(force_over_time_tex0, timeCoord1);\n float factor_f = pseudoRandom(a_rndSeed + 212165.);\n forceAnim = mix(force_0, force_1, factor_f);\n }\n vec4 forceTrack = vec4(forceAnim, 0.);\n if (u_force_space == 0) {\n forceTrack = rotateQuat(forceTrack, u_worldRot);\n }\n velocity.xyz += forceTrack.xyz * normalizedTime * a_dir_life.w;\n #endif\n #if VELOCITY_OVER_TIME_MODULE_ENABLE\n float speedModifier0 = 1.;\n float speedModifier1 = 1.;\n vec3 velocityAnim = vec3(0.);\n if (u_velocity_mode == 1) {\n velocityAnim = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n } else {\n vec3 vectory_0 = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n vec3 vectory_1 = unpackCurveData(velocity_over_time_tex0, timeCoord1, speedModifier1);\n float factor_v = pseudoRandom(a_rndSeed + 197866.);\n velocityAnim = mix(vectory_0, vectory_1, factor_v);\n speedModifier0 = mix(speedModifier0, speedModifier1, factor_v);\n }\n vec4 velocityTrack = vec4(velocityAnim, 0.);\n if (u_velocity_space == 0) {\n velocityTrack = rotateQuat(velocityTrack, u_worldRot);\n }\n velocity.xyz += velocityTrack.xyz;\n velocity.xyz *= speedModifier0;\n #endif\n pos.xyz += velocity.xyz * normalizedTime * a_dir_life.w;\n #if !CC_USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if CC_RENDER_MODE == 1\n velocity = rotateQuat(velocity, u_worldRot);\n #endif\n #endif\n vec3 rotation = a_rotation_uv.xyz;\n #if ROTATION_OVER_TIME_MODULE_ENABLE\n if (u_rotation_mode == 1) {\n rotation += unpackCurveData(rotation_over_time_tex0, timeCoord0) * normalizedTime * a_dir_life.w;\n } else {\n vec3 rotation_0 = unpackCurveData(rotation_over_time_tex0, timeCoord0);\n vec3 rotation_1 = unpackCurveData(rotation_over_time_tex0, timeCoord1);\n float factor_r = pseudoRandom(a_rndSeed + 125292.);\n rotation += mix(rotation_0, rotation_1, factor_r);\n }\n #endif\n #if COLOR_OVER_TIME_MODULE_ENABLE\n if (u_color_mode == 1) {\n color = a_color * texture(color_over_time_tex0, timeCoord0);\n } else {\n vec4 color_0 = texture(color_over_time_tex0, timeCoord0);\n vec4 color_1 = texture(color_over_time_tex0, timeCoord1);\n float factor_c = pseudoRandom(a_rndSeed + 91041.);\n color = a_color * mix(color_0, color_1, factor_c);\n }\n #else\n color = a_color;\n #endif\n #if CC_RENDER_MODE != 4\n vec2 cornerOffset = vec2((vertIdx - 0.5));\n #if CC_RENDER_MODE == 0\n vec3 rotEuler = rotation.xyz;\n #elif CC_RENDER_MODE == 1\n vec3 rotEuler = vec3(0.);\n #else\n vec3 rotEuler = vec3(0., 0., rotation.z);\n #endif\n computeVertPos(pos, cornerOffset, quaternionFromEuler(rotEuler), compScale\n #if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , cc_matViewInv\n #endif\n #if CC_RENDER_MODE == 1\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_size_uv.w\n #endif\n );\n #else\n mat4 xformNoScale = matrixFromRT(quaternionFromEuler(rotation), pos.xyz);\n mat4 xform = matFromRTS(quaternionFromEuler(rotation), pos.xyz, compScale);\n pos = xform * vec4(a_texCoord3, 1);\n vec4 normal = xformNoScale * vec4(a_normal, 0);\n color *= a_color1;\n #endif\n pos = cc_matViewProj * pos;\n float frameIndex = 0.;\n #if TEXTURE_ANIMATION_MODULE_ENABLE\n float startFrame = 0.;\n vec3 frameInfo = vec3(0.);\n if (int(u_anim_info.x) == 1) {\n frameInfo = unpackCurveData(texture_animation_tex0, timeCoord0);\n } else {\n vec3 frameInfo0 = unpackCurveData(texture_animation_tex0, timeCoord0);\n vec3 frameInfo1 = unpackCurveData(texture_animation_tex0, timeCoord1);\n float factor_t = pseudoRandom(a_rndSeed + 90794.);\n frameInfo = mix(frameInfo0, frameInfo1, factor_t);\n }\n startFrame = frameInfo.x / u_anim_info.y;\n frameIndex = repeat(u_anim_info.z * (frameInfo.y + startFrame), 1.);\n #endif\n uv = computeUV(frameIndex, vertIdx, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n return pos;\n}\nvoid main() { gl_Position = gpvs_main(); }`, + "frag": `\nprecision mediump float;\nuniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nvec3 SRGBToLinear (vec3 gamma) {\n return gamma * gamma;\n}\nvec4 CCFragOutput (vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nin vec2 uv;\nin vec4 color;\nuniform sampler2D mainTexture;\nuniform FragConstants {\n vec4 tintColor;\n};\nvec4 multiply () {\n vec4 col;\n vec4 texColor = texture(mainTexture, uv);\n col.rgb = tintColor.rgb * texColor.rgb * color.rgb * vec3(2.0);\n col.a = (1.0 - texColor.a) * (tintColor.a * color.a * 2.0);\n return CCFragOutput(col);\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = multiply(); }` + }, + "glsl1": { + "vert": `\nprecision mediump float;\nvec4 quaternionFromAxis (vec3 xAxis,vec3 yAxis,vec3 zAxis){\n mat3 m = mat3(xAxis,yAxis,zAxis);\n float trace = m[0][0] + m[1][1] + m[2][2];\n vec4 quat;\n if (trace > 0.) {\n float s = 0.5 / sqrt(trace + 1.0);\n quat.w = 0.25 / s;\n quat.x = (m[2][1] - m[1][2]) * s;\n quat.y = (m[0][2] - m[2][0]) * s;\n quat.z = (m[1][0] - m[0][1]) * s;\n } else if ((m[0][0] > m[1][1]) && (m[0][0] > m[2][2])) {\n float s = 2.0 * sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]);\n quat.w = (m[2][1] - m[1][2]) / s;\n quat.x = 0.25 * s;\n quat.y = (m[0][1] + m[1][0]) / s;\n quat.z = (m[0][2] + m[2][0]) / s;\n } else if (m[1][1] > m[2][2]) {\n float s = 2.0 * sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]);\n quat.w = (m[0][2] - m[2][0]) / s;\n quat.x = (m[0][1] + m[1][0]) / s;\n quat.y = 0.25 * s;\n quat.z = (m[1][2] + m[2][1]) / s;\n } else {\n float s = 2.0 * sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]);\n quat.w = (m[1][0] - m[0][1]) / s;\n quat.x = (m[0][2] + m[2][0]) / s;\n quat.y = (m[1][2] + m[2][1]) / s;\n quat.z = 0.25 * s;\n }\n float len = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;\n if (len > 0.) {\n len = 1. / sqrt(len);\n quat.x = quat.x * len;\n quat.y = quat.y * len;\n quat.z = quat.z * len;\n quat.w = quat.w * len;\n }\n return quat;\n}\nvec4 quaternionFromEuler (vec3 angle){\n float x = angle.x / 2.;\n float y = angle.y / 2.;\n float z = angle.z / 2.;\n float sx = sin(x);\n float cx = cos(x);\n float sy = sin(y);\n float cy = cos(y);\n float sz = sin(z);\n float cz = cos(z);\n vec4 quat = vec4(0);\n quat.x = sx * cy * cz + cx * sy * sz;\n quat.y = cx * sy * cz + sx * cy * sz;\n quat.z = cx * cy * sz - sx * sy * cz;\n quat.w = cx * cy * cz - sx * sy * sz;\n return quat;\n}\nmat4 matrixFromRT (vec4 q, vec3 p){\n float x2 = q.x + q.x;\n float y2 = q.y + q.y;\n float z2 = q.z + q.z;\n float xx = q.x * x2;\n float xy = q.x * y2;\n float xz = q.x * z2;\n float yy = q.y * y2;\n float yz = q.y * z2;\n float zz = q.z * z2;\n float wx = q.w * x2;\n float wy = q.w * y2;\n float wz = q.w * z2;\n return mat4(\n 1. - (yy + zz), xy + wz, xz - wy, 0,\n xy - wz, 1. - (xx + zz), yz + wx, 0,\n xz + wy, yz - wx, 1. - (xx + yy), 0,\n p.x, p.y, p.z, 1\n );\n}\nmat4 matFromRTS (vec4 q, vec3 t, vec3 s){\n float x = q.x, y = q.y, z = q.z, w = q.w;\n float x2 = x + x;\n float y2 = y + y;\n float z2 = z + z;\n float xx = x * x2;\n float xy = x * y2;\n float xz = x * z2;\n float yy = y * y2;\n float yz = y * z2;\n float zz = z * z2;\n float wx = w * x2;\n float wy = w * y2;\n float wz = w * z2;\n float sx = s.x;\n float sy = s.y;\n float sz = s.z;\n return mat4((1. - (yy + zz)) * sx, (xy + wz) * sx, (xz - wy) * sx, 0,\n (xy - wz) * sy, (1. - (xx + zz)) * sy, (yz + wx) * sy, 0,\n (xz + wy) * sz, (yz - wx) * sz, (1. - (xx + yy)) * sz, 0,\n t.x, t.y, t.z, 1);\n}\nvec4 quatMultiply (vec4 a, vec4 b){\n vec4 quat;\n quat.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;\n quat.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;\n quat.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;\n quat.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;\n return quat;\n}\nvoid rotateVecFromQuat (inout vec3 v, vec4 q){\n float ix = q.w * v.x + q.y * v.z - q.z * v.y;\n float iy = q.w * v.y + q.z * v.x - q.x * v.z;\n float iz = q.w * v.z + q.x * v.y - q.y * v.x;\n float iw = -q.x * v.x - q.y * v.y - q.z * v.z;\n v.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;\n v.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;\n v.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;\n}\nvec3 rotateInLocalSpace (vec3 pos, vec3 xAxis, vec3 yAxis, vec3 zAxis, vec4 q){\n vec4 viewQuat = quaternionFromAxis(xAxis, yAxis, zAxis);\n vec4 rotQuat = quatMultiply(viewQuat, q);\n rotateVecFromQuat(pos, rotQuat);\n return pos;\n}\nvoid rotateCorner (inout vec2 corner, float angle){\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nuniform vec4 mainTiling_Offset;\nuniform vec4 frameTile_velLenScale;\nuniform vec4 scale;\nuniform highp mat4 cc_matView;\nuniform highp mat4 cc_matViewInv;\nuniform highp mat4 cc_matViewProj;\nuniform highp vec4 cc_cameraPos;\nuniform highp mat4 cc_matWorld;\nvarying vec2 uv;\nvarying vec4 color;\nvoid computeVertPos (inout vec4 pos, vec2 vertOffset, vec4 q, vec3 s\n#if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , mat4 viewInv\n#endif\n#if CC_RENDER_MODE == 1\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float xIndex\n#endif\n) {\n#if CC_RENDER_MODE == 0\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = normalize(vec3(viewInv[0][0], viewInv[1][0], viewInv[2][0]));\n vec3 camY = normalize(vec3(viewInv[0][1], viewInv[1][1], viewInv[2][1]));\n vec3 camZ = normalize(vec3(viewInv[0][2], viewInv[1][2], viewInv[2][2]));\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, camZ, q);\n#elif CC_RENDER_MODE == 1\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz)) * s.x;\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * s.y;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif CC_RENDER_MODE == 2\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = vec3(1, 0, 0);\n vec3 camY = vec3(0, 0, -1);\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, cross(camX, camY), q);\n#elif CC_RENDER_MODE == 3\n vec2 viewSpaceVert = vec2(vertOffset.x * s.x, vertOffset.y * s.y);\n rotateCorner(viewSpaceVert, q.z);\n vec3 camX = normalize(vec3(cc_matView[0][0], cc_matView[1][0], cc_matView[2][0]));\n vec3 camY = vec3(0, 1, 0);\n vec3 offset = camX * viewSpaceVert.x + camY * viewSpaceVert.y;\n pos.xyz += offset;\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV (float frameIndex, vec2 vertIndex, vec2 frameTile){\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n#if CC_RENDER_MODE != 4\n vertIndex.y = 1. - vertIndex.y;\n#endif\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nuniform float sampleInterval;\nuniform float sampleNum;\nuniform vec4 u_worldRot;\nuniform vec4 u_timeDelta;\nattribute vec4 a_position_starttime;\nattribute vec4 a_size_uv;\nattribute vec4 a_rotation_uv;\nattribute vec4 a_color;\nattribute vec4 a_dir_life;\nattribute float a_rndSeed;\n#if CC_RENDER_MODE == 4\n attribute vec3 a_texCoord;\n attribute vec3 a_texCoord3;\n attribute vec3 a_normal;\n attribute vec4 a_color1;\n#endif\nvec3 unpackCurveData (sampler2D tex, vec2 coord) {\n vec4 a = texture2D(tex, coord);\n vec4 b = texture2D(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n return mix(a.xyz, b.xyz, c);\n}\nvec3 unpackCurveData (sampler2D tex, vec2 coord, out float w) {\n vec4 a = texture2D(tex, coord);\n vec4 b = texture2D(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n w = mix(a.w, b.w, c);\n return mix(a.xyz, b.xyz, c);\n}\nfloat pseudoRandom (float seed) {\n seed = mod(seed, 233280.);\n float q = (seed * 9301. + 49297.) / 233280.;\n return fract(q);\n}\n#if COLOR_OVER_TIME_MODULE_ENABLE\n uniform sampler2D color_over_time_tex0;\n uniform int u_color_mode;\n#endif\n#if ROTATION_OVER_TIME_MODULE_ENABLE\n uniform sampler2D rotation_over_time_tex0;\n uniform int u_rotation_mode;\n#endif\n#if SIZE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D size_over_time_tex0;\n uniform int u_size_mode;\n#endif\n#if FORCE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D force_over_time_tex0;\n uniform int u_force_mode;\nuniform int u_force_space;\n#endif\n#if VELOCITY_OVER_TIME_MODULE_ENABLE\n uniform sampler2D velocity_over_time_tex0;\n uniform int u_velocity_mode;\nuniform int u_velocity_space;\n#endif\n#if TEXTURE_ANIMATION_MODULE_ENABLE\n uniform sampler2D texture_animation_tex0;\n uniform vec4 u_anim_info;\n#endif\nfloat repeat (float t, float length) {\n return t - floor(t / length) * length;\n}\nvec4 rotateQuat (vec4 p, vec4 q) {\n vec3 iv = cross(q.xyz, p.xyz) + q.w * p.xyz;\n vec3 res = p.xyz + 2.0 * cross(q.xyz, iv);\n return vec4(res.xyz, p.w);\n}\nvec4 gpvs_main () {\n float activeTime = u_timeDelta.x - a_position_starttime.w;\n float normalizedTime = clamp(activeTime / a_dir_life.w, 0.0, 1.0);\n vec2 timeCoord0 = vec2(normalizedTime, 0.);\n vec2 timeCoord1 = vec2(normalizedTime, 1.);\n #if CC_RENDER_MODE == 4\n vec2 vertIdx = vec2(a_texCoord.x, a_texCoord.y);\n #else\n vec2 vertIdx = vec2(a_size_uv.w, a_rotation_uv.w);\n #endif\n vec4 velocity = vec4(a_dir_life.xyz, 0.);\n vec4 pos = vec4(a_position_starttime.xyz, 1.);\n vec3 size = a_size_uv.xyz;\n #if SIZE_OVER_TIME_MODULE_ENABLE\n if (u_size_mode == 1) {\n size *= unpackCurveData(size_over_time_tex0, timeCoord0);\n } else {\n vec3 size_0 = unpackCurveData(size_over_time_tex0, timeCoord0);\n vec3 size_1 = unpackCurveData(size_over_time_tex0, timeCoord1);\n float factor_s = pseudoRandom(a_rndSeed + 39825.);\n size *= mix(size_0, size_1, factor_s);\n }\n #endif\n vec3 compScale = scale.xyz * size;\n #if FORCE_OVER_TIME_MODULE_ENABLE\n vec3 forceAnim = vec3(0.);\n if (u_force_mode == 1) {\n forceAnim = unpackCurveData(force_over_time_tex0, timeCoord0);\n } else {\n vec3 force_0 = unpackCurveData(force_over_time_tex0, timeCoord0);\n vec3 force_1 = unpackCurveData(force_over_time_tex0, timeCoord1);\n float factor_f = pseudoRandom(a_rndSeed + 212165.);\n forceAnim = mix(force_0, force_1, factor_f);\n }\n vec4 forceTrack = vec4(forceAnim, 0.);\n if (u_force_space == 0) {\n forceTrack = rotateQuat(forceTrack, u_worldRot);\n }\n velocity.xyz += forceTrack.xyz * normalizedTime * a_dir_life.w;\n #endif\n #if VELOCITY_OVER_TIME_MODULE_ENABLE\n float speedModifier0 = 1.;\n float speedModifier1 = 1.;\n vec3 velocityAnim = vec3(0.);\n if (u_velocity_mode == 1) {\n velocityAnim = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n } else {\n vec3 vectory_0 = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n vec3 vectory_1 = unpackCurveData(velocity_over_time_tex0, timeCoord1, speedModifier1);\n float factor_v = pseudoRandom(a_rndSeed + 197866.);\n velocityAnim = mix(vectory_0, vectory_1, factor_v);\n speedModifier0 = mix(speedModifier0, speedModifier1, factor_v);\n }\n vec4 velocityTrack = vec4(velocityAnim, 0.);\n if (u_velocity_space == 0) {\n velocityTrack = rotateQuat(velocityTrack, u_worldRot);\n }\n velocity.xyz += velocityTrack.xyz;\n velocity.xyz *= speedModifier0;\n #endif\n pos.xyz += velocity.xyz * normalizedTime * a_dir_life.w;\n #if !CC_USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if CC_RENDER_MODE == 1\n velocity = rotateQuat(velocity, u_worldRot);\n #endif\n #endif\n vec3 rotation = a_rotation_uv.xyz;\n #if ROTATION_OVER_TIME_MODULE_ENABLE\n if (u_rotation_mode == 1) {\n rotation += unpackCurveData(rotation_over_time_tex0, timeCoord0) * normalizedTime * a_dir_life.w;\n } else {\n vec3 rotation_0 = unpackCurveData(rotation_over_time_tex0, timeCoord0);\n vec3 rotation_1 = unpackCurveData(rotation_over_time_tex0, timeCoord1);\n float factor_r = pseudoRandom(a_rndSeed + 125292.);\n rotation += mix(rotation_0, rotation_1, factor_r);\n }\n #endif\n #if COLOR_OVER_TIME_MODULE_ENABLE\n if (u_color_mode == 1) {\n color = a_color * texture2D(color_over_time_tex0, timeCoord0);\n } else {\n vec4 color_0 = texture2D(color_over_time_tex0, timeCoord0);\n vec4 color_1 = texture2D(color_over_time_tex0, timeCoord1);\n float factor_c = pseudoRandom(a_rndSeed + 91041.);\n color = a_color * mix(color_0, color_1, factor_c);\n }\n #else\n color = a_color;\n #endif\n #if CC_RENDER_MODE != 4\n vec2 cornerOffset = vec2((vertIdx - 0.5));\n #if CC_RENDER_MODE == 0\n vec3 rotEuler = rotation.xyz;\n #elif CC_RENDER_MODE == 1\n vec3 rotEuler = vec3(0.);\n #else\n vec3 rotEuler = vec3(0., 0., rotation.z);\n #endif\n computeVertPos(pos, cornerOffset, quaternionFromEuler(rotEuler), compScale\n #if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , cc_matViewInv\n #endif\n #if CC_RENDER_MODE == 1\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_size_uv.w\n #endif\n );\n #else\n mat4 xformNoScale = matrixFromRT(quaternionFromEuler(rotation), pos.xyz);\n mat4 xform = matFromRTS(quaternionFromEuler(rotation), pos.xyz, compScale);\n pos = xform * vec4(a_texCoord3, 1);\n vec4 normal = xformNoScale * vec4(a_normal, 0);\n color *= a_color1;\n #endif\n pos = cc_matViewProj * pos;\n float frameIndex = 0.;\n #if TEXTURE_ANIMATION_MODULE_ENABLE\n float startFrame = 0.;\n vec3 frameInfo = vec3(0.);\n if (int(u_anim_info.x) == 1) {\n frameInfo = unpackCurveData(texture_animation_tex0, timeCoord0);\n } else {\n vec3 frameInfo0 = unpackCurveData(texture_animation_tex0, timeCoord0);\n vec3 frameInfo1 = unpackCurveData(texture_animation_tex0, timeCoord1);\n float factor_t = pseudoRandom(a_rndSeed + 90794.);\n frameInfo = mix(frameInfo0, frameInfo1, factor_t);\n }\n startFrame = frameInfo.x / u_anim_info.y;\n frameIndex = repeat(u_anim_info.z * (frameInfo.y + startFrame), 1.);\n #endif\n uv = computeUV(frameIndex, vertIdx, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n return pos;\n}\nvoid main() { gl_Position = gpvs_main(); }`, + "frag": `\nprecision mediump float;\nuniform mediump vec4 cc_exposure;\nvec3 SRGBToLinear (vec3 gamma) {\n return gamma * gamma;\n}\nvec4 CCFragOutput (vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nvarying vec2 uv;\nvarying vec4 color;\nuniform sampler2D mainTexture;\nuniform vec4 tintColor;\nvec4 multiply () {\n vec4 col;\n vec4 texColor = texture2D(mainTexture, uv);\n col.rgb = tintColor.rgb * texColor.rgb * color.rgb * vec3(2.0);\n col.a = (1.0 - texColor.a) * (tintColor.a * color.a * 2.0);\n return CCFragOutput(col);\n}\nvoid main() { gl_FragColor = multiply(); }` + }, + "glsl4": { + "vert": `\nprecision mediump float;\nvec4 quaternionFromAxis (vec3 xAxis,vec3 yAxis,vec3 zAxis){\n mat3 m = mat3(xAxis,yAxis,zAxis);\n float trace = m[0][0] + m[1][1] + m[2][2];\n vec4 quat;\n if (trace > 0.) {\n float s = 0.5 / sqrt(trace + 1.0);\n quat.w = 0.25 / s;\n quat.x = (m[2][1] - m[1][2]) * s;\n quat.y = (m[0][2] - m[2][0]) * s;\n quat.z = (m[1][0] - m[0][1]) * s;\n } else if ((m[0][0] > m[1][1]) && (m[0][0] > m[2][2])) {\n float s = 2.0 * sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]);\n quat.w = (m[2][1] - m[1][2]) / s;\n quat.x = 0.25 * s;\n quat.y = (m[0][1] + m[1][0]) / s;\n quat.z = (m[0][2] + m[2][0]) / s;\n } else if (m[1][1] > m[2][2]) {\n float s = 2.0 * sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]);\n quat.w = (m[0][2] - m[2][0]) / s;\n quat.x = (m[0][1] + m[1][0]) / s;\n quat.y = 0.25 * s;\n quat.z = (m[1][2] + m[2][1]) / s;\n } else {\n float s = 2.0 * sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]);\n quat.w = (m[1][0] - m[0][1]) / s;\n quat.x = (m[0][2] + m[2][0]) / s;\n quat.y = (m[1][2] + m[2][1]) / s;\n quat.z = 0.25 * s;\n }\n float len = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;\n if (len > 0.) {\n len = 1. / sqrt(len);\n quat.x = quat.x * len;\n quat.y = quat.y * len;\n quat.z = quat.z * len;\n quat.w = quat.w * len;\n }\n return quat;\n}\nvec4 quaternionFromEuler (vec3 angle){\n float x = angle.x / 2.;\n float y = angle.y / 2.;\n float z = angle.z / 2.;\n float sx = sin(x);\n float cx = cos(x);\n float sy = sin(y);\n float cy = cos(y);\n float sz = sin(z);\n float cz = cos(z);\n vec4 quat = vec4(0);\n quat.x = sx * cy * cz + cx * sy * sz;\n quat.y = cx * sy * cz + sx * cy * sz;\n quat.z = cx * cy * sz - sx * sy * cz;\n quat.w = cx * cy * cz - sx * sy * sz;\n return quat;\n}\nmat4 matrixFromRT (vec4 q, vec3 p){\n float x2 = q.x + q.x;\n float y2 = q.y + q.y;\n float z2 = q.z + q.z;\n float xx = q.x * x2;\n float xy = q.x * y2;\n float xz = q.x * z2;\n float yy = q.y * y2;\n float yz = q.y * z2;\n float zz = q.z * z2;\n float wx = q.w * x2;\n float wy = q.w * y2;\n float wz = q.w * z2;\n return mat4(\n 1. - (yy + zz), xy + wz, xz - wy, 0,\n xy - wz, 1. - (xx + zz), yz + wx, 0,\n xz + wy, yz - wx, 1. - (xx + yy), 0,\n p.x, p.y, p.z, 1\n );\n}\nmat4 matFromRTS (vec4 q, vec3 t, vec3 s){\n float x = q.x, y = q.y, z = q.z, w = q.w;\n float x2 = x + x;\n float y2 = y + y;\n float z2 = z + z;\n float xx = x * x2;\n float xy = x * y2;\n float xz = x * z2;\n float yy = y * y2;\n float yz = y * z2;\n float zz = z * z2;\n float wx = w * x2;\n float wy = w * y2;\n float wz = w * z2;\n float sx = s.x;\n float sy = s.y;\n float sz = s.z;\n return mat4((1. - (yy + zz)) * sx, (xy + wz) * sx, (xz - wy) * sx, 0,\n (xy - wz) * sy, (1. - (xx + zz)) * sy, (yz + wx) * sy, 0,\n (xz + wy) * sz, (yz - wx) * sz, (1. - (xx + yy)) * sz, 0,\n t.x, t.y, t.z, 1);\n}\nvec4 quatMultiply (vec4 a, vec4 b){\n vec4 quat;\n quat.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;\n quat.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;\n quat.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;\n quat.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;\n return quat;\n}\nvoid rotateVecFromQuat (inout vec3 v, vec4 q){\n float ix = q.w * v.x + q.y * v.z - q.z * v.y;\n float iy = q.w * v.y + q.z * v.x - q.x * v.z;\n float iz = q.w * v.z + q.x * v.y - q.y * v.x;\n float iw = -q.x * v.x - q.y * v.y - q.z * v.z;\n v.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;\n v.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;\n v.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;\n}\nvec3 rotateInLocalSpace (vec3 pos, vec3 xAxis, vec3 yAxis, vec3 zAxis, vec4 q){\n vec4 viewQuat = quaternionFromAxis(xAxis, yAxis, zAxis);\n vec4 rotQuat = quatMultiply(viewQuat, q);\n rotateVecFromQuat(pos, rotQuat);\n return pos;\n}\nvoid rotateCorner (inout vec2 corner, float angle){\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nlayout(binding = 0) uniform Constants {\n vec4 mainTiling_Offset;\n vec4 frameTile_velLenScale;\n vec4 scale;\n};\nlayout(binding = 23) uniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nlayout(binding = 21) uniform CCLocal {\n highp mat4 cc_matWorld;\n highp mat4 cc_matWorldIT;\n};\nlayout(location = 0) out vec2 uv;\nlayout(location = 1) out vec4 color;\nvoid computeVertPos (inout vec4 pos, vec2 vertOffset, vec4 q, vec3 s\n#if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , mat4 viewInv\n#endif\n#if CC_RENDER_MODE == 1\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float xIndex\n#endif\n) {\n#if CC_RENDER_MODE == 0\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = normalize(vec3(viewInv[0][0], viewInv[1][0], viewInv[2][0]));\n vec3 camY = normalize(vec3(viewInv[0][1], viewInv[1][1], viewInv[2][1]));\n vec3 camZ = normalize(vec3(viewInv[0][2], viewInv[1][2], viewInv[2][2]));\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, camZ, q);\n#elif CC_RENDER_MODE == 1\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz)) * s.x;\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * s.y;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif CC_RENDER_MODE == 2\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = vec3(1, 0, 0);\n vec3 camY = vec3(0, 0, -1);\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, cross(camX, camY), q);\n#elif CC_RENDER_MODE == 3\n vec2 viewSpaceVert = vec2(vertOffset.x * s.x, vertOffset.y * s.y);\n rotateCorner(viewSpaceVert, q.z);\n vec3 camX = normalize(vec3(cc_matView[0][0], cc_matView[1][0], cc_matView[2][0]));\n vec3 camY = vec3(0, 1, 0);\n vec3 offset = camX * viewSpaceVert.x + camY * viewSpaceVert.y;\n pos.xyz += offset;\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV (float frameIndex, vec2 vertIndex, vec2 frameTile){\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n#if CC_RENDER_MODE != 4\n vertIndex.y = 1. - vertIndex.y;\n#endif\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nlayout(binding = 1) uniform SampleConstants {\n float sampleInterval;\n float sampleNum;\n};\nlayout(binding = 2) uniform TickConstants {\n vec4 u_worldRot;\n vec4 u_timeDelta;\n};\nlayout(location = 0) in vec4 a_position_starttime;\nlayout(location = 1) in vec4 a_size_uv;\nlayout(location = 2) in vec4 a_rotation_uv;\nlayout(location = 3) in vec4 a_color;\nlayout(location = 4) in vec4 a_dir_life;\nlayout(location = 5) in float a_rndSeed;\n#if CC_RENDER_MODE == 4\n layout(location = 6) in vec3 a_texCoord;\n layout(location = 7) in vec3 a_texCoord3;\n layout(location = 8) in vec3 a_normal;\n layout(location = 9) in vec4 a_color1;\n#endif\nvec3 unpackCurveData (sampler2D tex, vec2 coord) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n return mix(a.xyz, b.xyz, c);\n}\nvec3 unpackCurveData (sampler2D tex, vec2 coord, layout(location = 2) out float w) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n w = mix(a.w, b.w, c);\n return mix(a.xyz, b.xyz, c);\n}\nfloat pseudoRandom (float seed) {\n seed = mod(seed, 233280.);\n float q = (seed * 9301. + 49297.) / 233280.;\n return fract(q);\n}\n#if COLOR_OVER_TIME_MODULE_ENABLE\n layout(binding = 30) uniform sampler2D color_over_time_tex0;\n layout(binding = 3) uniform ColorConstant {\n int u_color_mode;\n };\n#endif\n#if ROTATION_OVER_TIME_MODULE_ENABLE\n layout(binding = 31) uniform sampler2D rotation_over_time_tex0;\n layout(binding = 4) uniform RotationConstant {\n int u_rotation_mode;\n };\n#endif\n#if SIZE_OVER_TIME_MODULE_ENABLE\n layout(binding = 32) uniform sampler2D size_over_time_tex0;\n layout(binding = 5) uniform SizeConstant {\n int u_size_mode;\n };\n#endif\n#if FORCE_OVER_TIME_MODULE_ENABLE\n layout(binding = 33) uniform sampler2D force_over_time_tex0;\n layout(binding = 6) uniform ForceConstant {\n int u_force_mode;\n int u_force_space;\n };\n#endif\n#if VELOCITY_OVER_TIME_MODULE_ENABLE\n layout(binding = 34) uniform sampler2D velocity_over_time_tex0;\n layout(binding = 7) uniform VelocityConstant {\n int u_velocity_mode;\n int u_velocity_space;\n };\n#endif\n#if TEXTURE_ANIMATION_MODULE_ENABLE\n layout(binding = 35) uniform sampler2D texture_animation_tex0;\n layout(binding = 8) uniform AnimationConstant {\n vec4 u_anim_info;\n };\n#endif\nfloat repeat (float t, float length) {\n return t - floor(t / length) * length;\n}\nvec4 rotateQuat (vec4 p, vec4 q) {\n vec3 iv = cross(q.xyz, p.xyz) + q.w * p.xyz;\n vec3 res = p.xyz + 2.0 * cross(q.xyz, iv);\n return vec4(res.xyz, p.w);\n}\nvec4 gpvs_main () {\n float activeTime = u_timeDelta.x - a_position_starttime.w;\n float normalizedTime = clamp(activeTime / a_dir_life.w, 0.0, 1.0);\n vec2 timeCoord0 = vec2(normalizedTime, 0.);\n vec2 timeCoord1 = vec2(normalizedTime, 1.);\n #if CC_RENDER_MODE == 4\n vec2 vertIdx = vec2(a_texCoord.x, a_texCoord.y);\n #else\n vec2 vertIdx = vec2(a_size_uv.w, a_rotation_uv.w);\n #endif\n vec4 velocity = vec4(a_dir_life.xyz, 0.);\n vec4 pos = vec4(a_position_starttime.xyz, 1.);\n vec3 size = a_size_uv.xyz;\n #if SIZE_OVER_TIME_MODULE_ENABLE\n if (u_size_mode == 1) {\n size *= unpackCurveData(size_over_time_tex0, timeCoord0);\n } else {\n vec3 size_0 = unpackCurveData(size_over_time_tex0, timeCoord0);\n vec3 size_1 = unpackCurveData(size_over_time_tex0, timeCoord1);\n float factor_s = pseudoRandom(a_rndSeed + 39825.);\n size *= mix(size_0, size_1, factor_s);\n }\n #endif\n vec3 compScale = scale.xyz * size;\n #if FORCE_OVER_TIME_MODULE_ENABLE\n vec3 forceAnim = vec3(0.);\n if (u_force_mode == 1) {\n forceAnim = unpackCurveData(force_over_time_tex0, timeCoord0);\n } else {\n vec3 force_0 = unpackCurveData(force_over_time_tex0, timeCoord0);\n vec3 force_1 = unpackCurveData(force_over_time_tex0, timeCoord1);\n float factor_f = pseudoRandom(a_rndSeed + 212165.);\n forceAnim = mix(force_0, force_1, factor_f);\n }\n vec4 forceTrack = vec4(forceAnim, 0.);\n if (u_force_space == 0) {\n forceTrack = rotateQuat(forceTrack, u_worldRot);\n }\n velocity.xyz += forceTrack.xyz * normalizedTime * a_dir_life.w;\n #endif\n #if VELOCITY_OVER_TIME_MODULE_ENABLE\n float speedModifier0 = 1.;\n float speedModifier1 = 1.;\n vec3 velocityAnim = vec3(0.);\n if (u_velocity_mode == 1) {\n velocityAnim = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n } else {\n vec3 vectory_0 = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n vec3 vectory_1 = unpackCurveData(velocity_over_time_tex0, timeCoord1, speedModifier1);\n float factor_v = pseudoRandom(a_rndSeed + 197866.);\n velocityAnim = mix(vectory_0, vectory_1, factor_v);\n speedModifier0 = mix(speedModifier0, speedModifier1, factor_v);\n }\n vec4 velocityTrack = vec4(velocityAnim, 0.);\n if (u_velocity_space == 0) {\n velocityTrack = rotateQuat(velocityTrack, u_worldRot);\n }\n velocity.xyz += velocityTrack.xyz;\n velocity.xyz *= speedModifier0;\n #endif\n pos.xyz += velocity.xyz * normalizedTime * a_dir_life.w;\n #if !CC_USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if CC_RENDER_MODE == 1\n velocity = rotateQuat(velocity, u_worldRot);\n #endif\n #endif\n vec3 rotation = a_rotation_uv.xyz;\n #if ROTATION_OVER_TIME_MODULE_ENABLE\n if (u_rotation_mode == 1) {\n rotation += unpackCurveData(rotation_over_time_tex0, timeCoord0) * normalizedTime * a_dir_life.w;\n } else {\n vec3 rotation_0 = unpackCurveData(rotation_over_time_tex0, timeCoord0);\n vec3 rotation_1 = unpackCurveData(rotation_over_time_tex0, timeCoord1);\n float factor_r = pseudoRandom(a_rndSeed + 125292.);\n rotation += mix(rotation_0, rotation_1, factor_r);\n }\n #endif\n #if COLOR_OVER_TIME_MODULE_ENABLE\n if (u_color_mode == 1) {\n color = a_color * texture(color_over_time_tex0, timeCoord0);\n } else {\n vec4 color_0 = texture(color_over_time_tex0, timeCoord0);\n vec4 color_1 = texture(color_over_time_tex0, timeCoord1);\n float factor_c = pseudoRandom(a_rndSeed + 91041.);\n color = a_color * mix(color_0, color_1, factor_c);\n }\n #else\n color = a_color;\n #endif\n #if CC_RENDER_MODE != 4\n vec2 cornerOffset = vec2((vertIdx - 0.5));\n #if CC_RENDER_MODE == 0\n vec3 rotEuler = rotation.xyz;\n #elif CC_RENDER_MODE == 1\n vec3 rotEuler = vec3(0.);\n #else\n vec3 rotEuler = vec3(0., 0., rotation.z);\n #endif\n computeVertPos(pos, cornerOffset, quaternionFromEuler(rotEuler), compScale\n #if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , cc_matViewInv\n #endif\n #if CC_RENDER_MODE == 1\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_size_uv.w\n #endif\n );\n #else\n mat4 xformNoScale = matrixFromRT(quaternionFromEuler(rotation), pos.xyz);\n mat4 xform = matFromRTS(quaternionFromEuler(rotation), pos.xyz, compScale);\n pos = xform * vec4(a_texCoord3, 1);\n vec4 normal = xformNoScale * vec4(a_normal, 0);\n color *= a_color1;\n #endif\n pos = cc_matViewProj * pos;\n float frameIndex = 0.;\n #if TEXTURE_ANIMATION_MODULE_ENABLE\n float startFrame = 0.;\n vec3 frameInfo = vec3(0.);\n if (int(u_anim_info.x) == 1) {\n frameInfo = unpackCurveData(texture_animation_tex0, timeCoord0);\n } else {\n vec3 frameInfo0 = unpackCurveData(texture_animation_tex0, timeCoord0);\n vec3 frameInfo1 = unpackCurveData(texture_animation_tex0, timeCoord1);\n float factor_t = pseudoRandom(a_rndSeed + 90794.);\n frameInfo = mix(frameInfo0, frameInfo1, factor_t);\n }\n startFrame = frameInfo.x / u_anim_info.y;\n frameIndex = repeat(u_anim_info.z * (frameInfo.y + startFrame), 1.);\n #endif\n uv = computeUV(frameIndex, vertIdx, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n return pos;\n}\nvoid main() { gl_Position = gpvs_main(); }`, + "frag": `\nprecision mediump float;\nlayout(binding = 23) uniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nvec3 SRGBToLinear (vec3 gamma) {\n return gamma * gamma;\n}\nvec4 CCFragOutput (vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nlayout(location = 0) in vec2 uv;\nlayout(location = 1) in vec4 color;\nlayout(binding = 36) uniform sampler2D mainTexture;\nlayout(binding = 9) uniform FragConstants {\n vec4 tintColor;\n};\nvec4 multiply () {\n vec4 col;\n vec4 texColor = texture(mainTexture, uv);\n col.rgb = tintColor.rgb * texColor.rgb * color.rgb * vec3(2.0);\n col.a = (1.0 - texColor.a) * (tintColor.a * color.a * 2.0);\n return CCFragOutput(col);\n}\nlayout(location = 0) out vec4 cc_FragColor;\nvoid main() { cc_FragColor = multiply(); }` + }, + "varyings": [ + { "name": "uv", "type": 14, "count": 1, "defines": [], "location": 0 }, + { "name": "color", "type": 16, "count": 1, "defines": [], "location": 1 }, + { "name": "w", "type": 13, "count": 1, "defines": [], "location": 2 } + ], + "builtins": { + "globals": { "blocks": [{ "name": "CCGlobal", "defines": [] }], "samplers": [] }, + "locals": { "blocks": [{ "name": "CCLocal", "defines": [] }], "samplers": [] } + }, + "defines": [ + { "name": "CC_RENDER_MODE", "type": "number", "defines": [], "range": [0, 4] }, + { "name": "COLOR_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "ROTATION_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "SIZE_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "FORCE_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "VELOCITY_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "TEXTURE_ANIMATION_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "CC_USE_WORLD_SPACE", "type": "boolean", "defines": [] }, + { "name": "CC_USE_HDR", "type": "boolean", "defines": [] } + ], + "blocks": [ + {"name": "Constants", "defines": [], "binding": 0, "members": [ + { "name": "mainTiling_Offset", "type": 16, "count": 1 }, + { "name": "frameTile_velLenScale", "type": 16, "count": 1 }, + { "name": "scale", "type": 16, "count": 1 } + ]}, + {"name": "SampleConstants", "defines": [], "binding": 1, "members": [ + { "name": "sampleInterval", "type": 13, "count": 1 }, + { "name": "sampleNum", "type": 13, "count": 1 } + ]}, + {"name": "TickConstants", "defines": [], "binding": 2, "members": [ + { "name": "u_worldRot", "type": 16, "count": 1 }, + { "name": "u_timeDelta", "type": 16, "count": 1 } + ]}, + {"name": "ColorConstant", "defines": ["COLOR_OVER_TIME_MODULE_ENABLE"], "binding": 3, "members": [ + { "name": "u_color_mode", "type": 5, "count": 1 } + ]}, + {"name": "RotationConstant", "defines": ["ROTATION_OVER_TIME_MODULE_ENABLE"], "binding": 4, "members": [ + { "name": "u_rotation_mode", "type": 5, "count": 1 } + ]}, + {"name": "SizeConstant", "defines": ["SIZE_OVER_TIME_MODULE_ENABLE"], "binding": 5, "members": [ + { "name": "u_size_mode", "type": 5, "count": 1 } + ]}, + {"name": "ForceConstant", "defines": ["FORCE_OVER_TIME_MODULE_ENABLE"], "binding": 6, "members": [ + { "name": "u_force_mode", "type": 5, "count": 1 }, + { "name": "u_force_space", "type": 5, "count": 1 } + ]}, + {"name": "VelocityConstant", "defines": ["VELOCITY_OVER_TIME_MODULE_ENABLE"], "binding": 7, "members": [ + { "name": "u_velocity_mode", "type": 5, "count": 1 }, + { "name": "u_velocity_space", "type": 5, "count": 1 } + ]}, + {"name": "AnimationConstant", "defines": ["TEXTURE_ANIMATION_MODULE_ENABLE"], "binding": 8, "members": [ + { "name": "u_anim_info", "type": 16, "count": 1 } + ]}, + {"name": "FragConstants", "defines": [], "binding": 9, "members": [ + { "name": "tintColor", "type": 16, "count": 1 } + ]} + ], + "samplers": [ + { "name": "color_over_time_tex0", "type": 28, "count": 1, "defines": ["COLOR_OVER_TIME_MODULE_ENABLE"], "binding": 30 }, + { "name": "rotation_over_time_tex0", "type": 28, "count": 1, "defines": ["ROTATION_OVER_TIME_MODULE_ENABLE"], "binding": 31 }, + { "name": "size_over_time_tex0", "type": 28, "count": 1, "defines": ["SIZE_OVER_TIME_MODULE_ENABLE"], "binding": 32 }, + { "name": "force_over_time_tex0", "type": 28, "count": 1, "defines": ["FORCE_OVER_TIME_MODULE_ENABLE"], "binding": 33 }, + { "name": "velocity_over_time_tex0", "type": 28, "count": 1, "defines": ["VELOCITY_OVER_TIME_MODULE_ENABLE"], "binding": 34 }, + { "name": "texture_animation_tex0", "type": 28, "count": 1, "defines": ["TEXTURE_ANIMATION_MODULE_ENABLE"], "binding": 35 }, + { "name": "mainTexture", "type": 28, "count": 1, "defines": [], "binding": 36 } + ], + "attributes": [ + { "name": "a_position_starttime", "type": 16, "count": 1, "defines": [], "format": 43, "location": 0 }, + { "name": "a_size_uv", "type": 16, "count": 1, "defines": [], "format": 43, "location": 1 }, + { "name": "a_rotation_uv", "type": 16, "count": 1, "defines": [], "format": 43, "location": 2 }, + { "name": "a_color", "type": 16, "count": 1, "defines": [], "format": 43, "location": 3 }, + { "name": "a_dir_life", "type": 16, "count": 1, "defines": [], "format": 43, "location": 4 }, + { "name": "a_rndSeed", "type": 13, "count": 1, "defines": [], "format": 11, "location": 5 }, + { "name": "a_texCoord", "type": 15, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 32, "location": 6 }, + { "name": "a_texCoord3", "type": 15, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 32, "location": 7 }, + { "name": "a_normal", "type": 15, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 32, "location": 8 }, + { "name": "a_color1", "type": 16, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 43, "location": 9 } + ] + }, + { + "name": "builtin-particle-gpu|particle-vs-gpu:gpvs_main|no-tint-fs:addSmooth", + "hash": 1259891897, + "glsl3": { + "vert": `\nprecision mediump float;\nvec4 quaternionFromAxis (vec3 xAxis,vec3 yAxis,vec3 zAxis){\n mat3 m = mat3(xAxis,yAxis,zAxis);\n float trace = m[0][0] + m[1][1] + m[2][2];\n vec4 quat;\n if (trace > 0.) {\n float s = 0.5 / sqrt(trace + 1.0);\n quat.w = 0.25 / s;\n quat.x = (m[2][1] - m[1][2]) * s;\n quat.y = (m[0][2] - m[2][0]) * s;\n quat.z = (m[1][0] - m[0][1]) * s;\n } else if ((m[0][0] > m[1][1]) && (m[0][0] > m[2][2])) {\n float s = 2.0 * sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]);\n quat.w = (m[2][1] - m[1][2]) / s;\n quat.x = 0.25 * s;\n quat.y = (m[0][1] + m[1][0]) / s;\n quat.z = (m[0][2] + m[2][0]) / s;\n } else if (m[1][1] > m[2][2]) {\n float s = 2.0 * sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]);\n quat.w = (m[0][2] - m[2][0]) / s;\n quat.x = (m[0][1] + m[1][0]) / s;\n quat.y = 0.25 * s;\n quat.z = (m[1][2] + m[2][1]) / s;\n } else {\n float s = 2.0 * sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]);\n quat.w = (m[1][0] - m[0][1]) / s;\n quat.x = (m[0][2] + m[2][0]) / s;\n quat.y = (m[1][2] + m[2][1]) / s;\n quat.z = 0.25 * s;\n }\n float len = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;\n if (len > 0.) {\n len = 1. / sqrt(len);\n quat.x = quat.x * len;\n quat.y = quat.y * len;\n quat.z = quat.z * len;\n quat.w = quat.w * len;\n }\n return quat;\n}\nvec4 quaternionFromEuler (vec3 angle){\n float x = angle.x / 2.;\n float y = angle.y / 2.;\n float z = angle.z / 2.;\n float sx = sin(x);\n float cx = cos(x);\n float sy = sin(y);\n float cy = cos(y);\n float sz = sin(z);\n float cz = cos(z);\n vec4 quat = vec4(0);\n quat.x = sx * cy * cz + cx * sy * sz;\n quat.y = cx * sy * cz + sx * cy * sz;\n quat.z = cx * cy * sz - sx * sy * cz;\n quat.w = cx * cy * cz - sx * sy * sz;\n return quat;\n}\nmat4 matrixFromRT (vec4 q, vec3 p){\n float x2 = q.x + q.x;\n float y2 = q.y + q.y;\n float z2 = q.z + q.z;\n float xx = q.x * x2;\n float xy = q.x * y2;\n float xz = q.x * z2;\n float yy = q.y * y2;\n float yz = q.y * z2;\n float zz = q.z * z2;\n float wx = q.w * x2;\n float wy = q.w * y2;\n float wz = q.w * z2;\n return mat4(\n 1. - (yy + zz), xy + wz, xz - wy, 0,\n xy - wz, 1. - (xx + zz), yz + wx, 0,\n xz + wy, yz - wx, 1. - (xx + yy), 0,\n p.x, p.y, p.z, 1\n );\n}\nmat4 matFromRTS (vec4 q, vec3 t, vec3 s){\n float x = q.x, y = q.y, z = q.z, w = q.w;\n float x2 = x + x;\n float y2 = y + y;\n float z2 = z + z;\n float xx = x * x2;\n float xy = x * y2;\n float xz = x * z2;\n float yy = y * y2;\n float yz = y * z2;\n float zz = z * z2;\n float wx = w * x2;\n float wy = w * y2;\n float wz = w * z2;\n float sx = s.x;\n float sy = s.y;\n float sz = s.z;\n return mat4((1. - (yy + zz)) * sx, (xy + wz) * sx, (xz - wy) * sx, 0,\n (xy - wz) * sy, (1. - (xx + zz)) * sy, (yz + wx) * sy, 0,\n (xz + wy) * sz, (yz - wx) * sz, (1. - (xx + yy)) * sz, 0,\n t.x, t.y, t.z, 1);\n}\nvec4 quatMultiply (vec4 a, vec4 b){\n vec4 quat;\n quat.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;\n quat.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;\n quat.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;\n quat.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;\n return quat;\n}\nvoid rotateVecFromQuat (inout vec3 v, vec4 q){\n float ix = q.w * v.x + q.y * v.z - q.z * v.y;\n float iy = q.w * v.y + q.z * v.x - q.x * v.z;\n float iz = q.w * v.z + q.x * v.y - q.y * v.x;\n float iw = -q.x * v.x - q.y * v.y - q.z * v.z;\n v.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;\n v.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;\n v.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;\n}\nvec3 rotateInLocalSpace (vec3 pos, vec3 xAxis, vec3 yAxis, vec3 zAxis, vec4 q){\n vec4 viewQuat = quaternionFromAxis(xAxis, yAxis, zAxis);\n vec4 rotQuat = quatMultiply(viewQuat, q);\n rotateVecFromQuat(pos, rotQuat);\n return pos;\n}\nvoid rotateCorner (inout vec2 corner, float angle){\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nuniform Constants {\n vec4 mainTiling_Offset;\n vec4 frameTile_velLenScale;\n vec4 scale;\n};\nuniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nuniform CCLocal {\n highp mat4 cc_matWorld;\n highp mat4 cc_matWorldIT;\n};\nout vec2 uv;\nout vec4 color;\nvoid computeVertPos (inout vec4 pos, vec2 vertOffset, vec4 q, vec3 s\n#if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , mat4 viewInv\n#endif\n#if CC_RENDER_MODE == 1\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float xIndex\n#endif\n) {\n#if CC_RENDER_MODE == 0\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = normalize(vec3(viewInv[0][0], viewInv[1][0], viewInv[2][0]));\n vec3 camY = normalize(vec3(viewInv[0][1], viewInv[1][1], viewInv[2][1]));\n vec3 camZ = normalize(vec3(viewInv[0][2], viewInv[1][2], viewInv[2][2]));\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, camZ, q);\n#elif CC_RENDER_MODE == 1\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz)) * s.x;\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * s.y;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif CC_RENDER_MODE == 2\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = vec3(1, 0, 0);\n vec3 camY = vec3(0, 0, -1);\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, cross(camX, camY), q);\n#elif CC_RENDER_MODE == 3\n vec2 viewSpaceVert = vec2(vertOffset.x * s.x, vertOffset.y * s.y);\n rotateCorner(viewSpaceVert, q.z);\n vec3 camX = normalize(vec3(cc_matView[0][0], cc_matView[1][0], cc_matView[2][0]));\n vec3 camY = vec3(0, 1, 0);\n vec3 offset = camX * viewSpaceVert.x + camY * viewSpaceVert.y;\n pos.xyz += offset;\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV (float frameIndex, vec2 vertIndex, vec2 frameTile){\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n#if CC_RENDER_MODE != 4\n vertIndex.y = 1. - vertIndex.y;\n#endif\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nuniform SampleConstants {\n float sampleInterval;\n float sampleNum;\n};\nuniform TickConstants {\n vec4 u_worldRot;\n vec4 u_timeDelta;\n};\nin vec4 a_position_starttime;\nin vec4 a_size_uv;\nin vec4 a_rotation_uv;\nin vec4 a_color;\nin vec4 a_dir_life;\nin float a_rndSeed;\n#if CC_RENDER_MODE == 4\n in vec3 a_texCoord;\n in vec3 a_texCoord3;\n in vec3 a_normal;\n in vec4 a_color1;\n#endif\nvec3 unpackCurveData (sampler2D tex, vec2 coord) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n return mix(a.xyz, b.xyz, c);\n}\nvec3 unpackCurveData (sampler2D tex, vec2 coord, out float w) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n w = mix(a.w, b.w, c);\n return mix(a.xyz, b.xyz, c);\n}\nfloat pseudoRandom (float seed) {\n seed = mod(seed, 233280.);\n float q = (seed * 9301. + 49297.) / 233280.;\n return fract(q);\n}\n#if COLOR_OVER_TIME_MODULE_ENABLE\n uniform sampler2D color_over_time_tex0;\n uniform ColorConstant {\n int u_color_mode;\n };\n#endif\n#if ROTATION_OVER_TIME_MODULE_ENABLE\n uniform sampler2D rotation_over_time_tex0;\n uniform RotationConstant {\n int u_rotation_mode;\n };\n#endif\n#if SIZE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D size_over_time_tex0;\n uniform SizeConstant {\n int u_size_mode;\n };\n#endif\n#if FORCE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D force_over_time_tex0;\n uniform ForceConstant {\n int u_force_mode;\n int u_force_space;\n };\n#endif\n#if VELOCITY_OVER_TIME_MODULE_ENABLE\n uniform sampler2D velocity_over_time_tex0;\n uniform VelocityConstant {\n int u_velocity_mode;\n int u_velocity_space;\n };\n#endif\n#if TEXTURE_ANIMATION_MODULE_ENABLE\n uniform sampler2D texture_animation_tex0;\n uniform AnimationConstant {\n vec4 u_anim_info;\n };\n#endif\nfloat repeat (float t, float length) {\n return t - floor(t / length) * length;\n}\nvec4 rotateQuat (vec4 p, vec4 q) {\n vec3 iv = cross(q.xyz, p.xyz) + q.w * p.xyz;\n vec3 res = p.xyz + 2.0 * cross(q.xyz, iv);\n return vec4(res.xyz, p.w);\n}\nvec4 gpvs_main () {\n float activeTime = u_timeDelta.x - a_position_starttime.w;\n float normalizedTime = clamp(activeTime / a_dir_life.w, 0.0, 1.0);\n vec2 timeCoord0 = vec2(normalizedTime, 0.);\n vec2 timeCoord1 = vec2(normalizedTime, 1.);\n #if CC_RENDER_MODE == 4\n vec2 vertIdx = vec2(a_texCoord.x, a_texCoord.y);\n #else\n vec2 vertIdx = vec2(a_size_uv.w, a_rotation_uv.w);\n #endif\n vec4 velocity = vec4(a_dir_life.xyz, 0.);\n vec4 pos = vec4(a_position_starttime.xyz, 1.);\n vec3 size = a_size_uv.xyz;\n #if SIZE_OVER_TIME_MODULE_ENABLE\n if (u_size_mode == 1) {\n size *= unpackCurveData(size_over_time_tex0, timeCoord0);\n } else {\n vec3 size_0 = unpackCurveData(size_over_time_tex0, timeCoord0);\n vec3 size_1 = unpackCurveData(size_over_time_tex0, timeCoord1);\n float factor_s = pseudoRandom(a_rndSeed + 39825.);\n size *= mix(size_0, size_1, factor_s);\n }\n #endif\n vec3 compScale = scale.xyz * size;\n #if FORCE_OVER_TIME_MODULE_ENABLE\n vec3 forceAnim = vec3(0.);\n if (u_force_mode == 1) {\n forceAnim = unpackCurveData(force_over_time_tex0, timeCoord0);\n } else {\n vec3 force_0 = unpackCurveData(force_over_time_tex0, timeCoord0);\n vec3 force_1 = unpackCurveData(force_over_time_tex0, timeCoord1);\n float factor_f = pseudoRandom(a_rndSeed + 212165.);\n forceAnim = mix(force_0, force_1, factor_f);\n }\n vec4 forceTrack = vec4(forceAnim, 0.);\n if (u_force_space == 0) {\n forceTrack = rotateQuat(forceTrack, u_worldRot);\n }\n velocity.xyz += forceTrack.xyz * normalizedTime * a_dir_life.w;\n #endif\n #if VELOCITY_OVER_TIME_MODULE_ENABLE\n float speedModifier0 = 1.;\n float speedModifier1 = 1.;\n vec3 velocityAnim = vec3(0.);\n if (u_velocity_mode == 1) {\n velocityAnim = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n } else {\n vec3 vectory_0 = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n vec3 vectory_1 = unpackCurveData(velocity_over_time_tex0, timeCoord1, speedModifier1);\n float factor_v = pseudoRandom(a_rndSeed + 197866.);\n velocityAnim = mix(vectory_0, vectory_1, factor_v);\n speedModifier0 = mix(speedModifier0, speedModifier1, factor_v);\n }\n vec4 velocityTrack = vec4(velocityAnim, 0.);\n if (u_velocity_space == 0) {\n velocityTrack = rotateQuat(velocityTrack, u_worldRot);\n }\n velocity.xyz += velocityTrack.xyz;\n velocity.xyz *= speedModifier0;\n #endif\n pos.xyz += velocity.xyz * normalizedTime * a_dir_life.w;\n #if !CC_USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if CC_RENDER_MODE == 1\n velocity = rotateQuat(velocity, u_worldRot);\n #endif\n #endif\n vec3 rotation = a_rotation_uv.xyz;\n #if ROTATION_OVER_TIME_MODULE_ENABLE\n if (u_rotation_mode == 1) {\n rotation += unpackCurveData(rotation_over_time_tex0, timeCoord0) * normalizedTime * a_dir_life.w;\n } else {\n vec3 rotation_0 = unpackCurveData(rotation_over_time_tex0, timeCoord0);\n vec3 rotation_1 = unpackCurveData(rotation_over_time_tex0, timeCoord1);\n float factor_r = pseudoRandom(a_rndSeed + 125292.);\n rotation += mix(rotation_0, rotation_1, factor_r);\n }\n #endif\n #if COLOR_OVER_TIME_MODULE_ENABLE\n if (u_color_mode == 1) {\n color = a_color * texture(color_over_time_tex0, timeCoord0);\n } else {\n vec4 color_0 = texture(color_over_time_tex0, timeCoord0);\n vec4 color_1 = texture(color_over_time_tex0, timeCoord1);\n float factor_c = pseudoRandom(a_rndSeed + 91041.);\n color = a_color * mix(color_0, color_1, factor_c);\n }\n #else\n color = a_color;\n #endif\n #if CC_RENDER_MODE != 4\n vec2 cornerOffset = vec2((vertIdx - 0.5));\n #if CC_RENDER_MODE == 0\n vec3 rotEuler = rotation.xyz;\n #elif CC_RENDER_MODE == 1\n vec3 rotEuler = vec3(0.);\n #else\n vec3 rotEuler = vec3(0., 0., rotation.z);\n #endif\n computeVertPos(pos, cornerOffset, quaternionFromEuler(rotEuler), compScale\n #if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , cc_matViewInv\n #endif\n #if CC_RENDER_MODE == 1\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_size_uv.w\n #endif\n );\n #else\n mat4 xformNoScale = matrixFromRT(quaternionFromEuler(rotation), pos.xyz);\n mat4 xform = matFromRTS(quaternionFromEuler(rotation), pos.xyz, compScale);\n pos = xform * vec4(a_texCoord3, 1);\n vec4 normal = xformNoScale * vec4(a_normal, 0);\n color *= a_color1;\n #endif\n pos = cc_matViewProj * pos;\n float frameIndex = 0.;\n #if TEXTURE_ANIMATION_MODULE_ENABLE\n float startFrame = 0.;\n vec3 frameInfo = vec3(0.);\n if (int(u_anim_info.x) == 1) {\n frameInfo = unpackCurveData(texture_animation_tex0, timeCoord0);\n } else {\n vec3 frameInfo0 = unpackCurveData(texture_animation_tex0, timeCoord0);\n vec3 frameInfo1 = unpackCurveData(texture_animation_tex0, timeCoord1);\n float factor_t = pseudoRandom(a_rndSeed + 90794.);\n frameInfo = mix(frameInfo0, frameInfo1, factor_t);\n }\n startFrame = frameInfo.x / u_anim_info.y;\n frameIndex = repeat(u_anim_info.z * (frameInfo.y + startFrame), 1.);\n #endif\n uv = computeUV(frameIndex, vertIdx, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n return pos;\n}\nvoid main() { gl_Position = gpvs_main(); }`, + "frag": `\nprecision mediump float;\nuniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nvec3 SRGBToLinear (vec3 gamma) {\n return gamma * gamma;\n}\nvec4 CCFragOutput (vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nin vec2 uv;\nin vec4 color;\nuniform sampler2D mainTexture;\nvec4 addSmooth () {\n vec4 col = color * texture(mainTexture, uv);\n col.rgb *= col.a;\n return CCFragOutput(col);\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = addSmooth(); }` + }, + "glsl1": { + "vert": `\nprecision mediump float;\nvec4 quaternionFromAxis (vec3 xAxis,vec3 yAxis,vec3 zAxis){\n mat3 m = mat3(xAxis,yAxis,zAxis);\n float trace = m[0][0] + m[1][1] + m[2][2];\n vec4 quat;\n if (trace > 0.) {\n float s = 0.5 / sqrt(trace + 1.0);\n quat.w = 0.25 / s;\n quat.x = (m[2][1] - m[1][2]) * s;\n quat.y = (m[0][2] - m[2][0]) * s;\n quat.z = (m[1][0] - m[0][1]) * s;\n } else if ((m[0][0] > m[1][1]) && (m[0][0] > m[2][2])) {\n float s = 2.0 * sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]);\n quat.w = (m[2][1] - m[1][2]) / s;\n quat.x = 0.25 * s;\n quat.y = (m[0][1] + m[1][0]) / s;\n quat.z = (m[0][2] + m[2][0]) / s;\n } else if (m[1][1] > m[2][2]) {\n float s = 2.0 * sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]);\n quat.w = (m[0][2] - m[2][0]) / s;\n quat.x = (m[0][1] + m[1][0]) / s;\n quat.y = 0.25 * s;\n quat.z = (m[1][2] + m[2][1]) / s;\n } else {\n float s = 2.0 * sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]);\n quat.w = (m[1][0] - m[0][1]) / s;\n quat.x = (m[0][2] + m[2][0]) / s;\n quat.y = (m[1][2] + m[2][1]) / s;\n quat.z = 0.25 * s;\n }\n float len = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;\n if (len > 0.) {\n len = 1. / sqrt(len);\n quat.x = quat.x * len;\n quat.y = quat.y * len;\n quat.z = quat.z * len;\n quat.w = quat.w * len;\n }\n return quat;\n}\nvec4 quaternionFromEuler (vec3 angle){\n float x = angle.x / 2.;\n float y = angle.y / 2.;\n float z = angle.z / 2.;\n float sx = sin(x);\n float cx = cos(x);\n float sy = sin(y);\n float cy = cos(y);\n float sz = sin(z);\n float cz = cos(z);\n vec4 quat = vec4(0);\n quat.x = sx * cy * cz + cx * sy * sz;\n quat.y = cx * sy * cz + sx * cy * sz;\n quat.z = cx * cy * sz - sx * sy * cz;\n quat.w = cx * cy * cz - sx * sy * sz;\n return quat;\n}\nmat4 matrixFromRT (vec4 q, vec3 p){\n float x2 = q.x + q.x;\n float y2 = q.y + q.y;\n float z2 = q.z + q.z;\n float xx = q.x * x2;\n float xy = q.x * y2;\n float xz = q.x * z2;\n float yy = q.y * y2;\n float yz = q.y * z2;\n float zz = q.z * z2;\n float wx = q.w * x2;\n float wy = q.w * y2;\n float wz = q.w * z2;\n return mat4(\n 1. - (yy + zz), xy + wz, xz - wy, 0,\n xy - wz, 1. - (xx + zz), yz + wx, 0,\n xz + wy, yz - wx, 1. - (xx + yy), 0,\n p.x, p.y, p.z, 1\n );\n}\nmat4 matFromRTS (vec4 q, vec3 t, vec3 s){\n float x = q.x, y = q.y, z = q.z, w = q.w;\n float x2 = x + x;\n float y2 = y + y;\n float z2 = z + z;\n float xx = x * x2;\n float xy = x * y2;\n float xz = x * z2;\n float yy = y * y2;\n float yz = y * z2;\n float zz = z * z2;\n float wx = w * x2;\n float wy = w * y2;\n float wz = w * z2;\n float sx = s.x;\n float sy = s.y;\n float sz = s.z;\n return mat4((1. - (yy + zz)) * sx, (xy + wz) * sx, (xz - wy) * sx, 0,\n (xy - wz) * sy, (1. - (xx + zz)) * sy, (yz + wx) * sy, 0,\n (xz + wy) * sz, (yz - wx) * sz, (1. - (xx + yy)) * sz, 0,\n t.x, t.y, t.z, 1);\n}\nvec4 quatMultiply (vec4 a, vec4 b){\n vec4 quat;\n quat.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;\n quat.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;\n quat.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;\n quat.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;\n return quat;\n}\nvoid rotateVecFromQuat (inout vec3 v, vec4 q){\n float ix = q.w * v.x + q.y * v.z - q.z * v.y;\n float iy = q.w * v.y + q.z * v.x - q.x * v.z;\n float iz = q.w * v.z + q.x * v.y - q.y * v.x;\n float iw = -q.x * v.x - q.y * v.y - q.z * v.z;\n v.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;\n v.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;\n v.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;\n}\nvec3 rotateInLocalSpace (vec3 pos, vec3 xAxis, vec3 yAxis, vec3 zAxis, vec4 q){\n vec4 viewQuat = quaternionFromAxis(xAxis, yAxis, zAxis);\n vec4 rotQuat = quatMultiply(viewQuat, q);\n rotateVecFromQuat(pos, rotQuat);\n return pos;\n}\nvoid rotateCorner (inout vec2 corner, float angle){\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nuniform vec4 mainTiling_Offset;\nuniform vec4 frameTile_velLenScale;\nuniform vec4 scale;\nuniform highp mat4 cc_matView;\nuniform highp mat4 cc_matViewInv;\nuniform highp mat4 cc_matViewProj;\nuniform highp vec4 cc_cameraPos;\nuniform highp mat4 cc_matWorld;\nvarying vec2 uv;\nvarying vec4 color;\nvoid computeVertPos (inout vec4 pos, vec2 vertOffset, vec4 q, vec3 s\n#if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , mat4 viewInv\n#endif\n#if CC_RENDER_MODE == 1\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float xIndex\n#endif\n) {\n#if CC_RENDER_MODE == 0\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = normalize(vec3(viewInv[0][0], viewInv[1][0], viewInv[2][0]));\n vec3 camY = normalize(vec3(viewInv[0][1], viewInv[1][1], viewInv[2][1]));\n vec3 camZ = normalize(vec3(viewInv[0][2], viewInv[1][2], viewInv[2][2]));\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, camZ, q);\n#elif CC_RENDER_MODE == 1\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz)) * s.x;\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * s.y;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif CC_RENDER_MODE == 2\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = vec3(1, 0, 0);\n vec3 camY = vec3(0, 0, -1);\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, cross(camX, camY), q);\n#elif CC_RENDER_MODE == 3\n vec2 viewSpaceVert = vec2(vertOffset.x * s.x, vertOffset.y * s.y);\n rotateCorner(viewSpaceVert, q.z);\n vec3 camX = normalize(vec3(cc_matView[0][0], cc_matView[1][0], cc_matView[2][0]));\n vec3 camY = vec3(0, 1, 0);\n vec3 offset = camX * viewSpaceVert.x + camY * viewSpaceVert.y;\n pos.xyz += offset;\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV (float frameIndex, vec2 vertIndex, vec2 frameTile){\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n#if CC_RENDER_MODE != 4\n vertIndex.y = 1. - vertIndex.y;\n#endif\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nuniform float sampleInterval;\nuniform float sampleNum;\nuniform vec4 u_worldRot;\nuniform vec4 u_timeDelta;\nattribute vec4 a_position_starttime;\nattribute vec4 a_size_uv;\nattribute vec4 a_rotation_uv;\nattribute vec4 a_color;\nattribute vec4 a_dir_life;\nattribute float a_rndSeed;\n#if CC_RENDER_MODE == 4\n attribute vec3 a_texCoord;\n attribute vec3 a_texCoord3;\n attribute vec3 a_normal;\n attribute vec4 a_color1;\n#endif\nvec3 unpackCurveData (sampler2D tex, vec2 coord) {\n vec4 a = texture2D(tex, coord);\n vec4 b = texture2D(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n return mix(a.xyz, b.xyz, c);\n}\nvec3 unpackCurveData (sampler2D tex, vec2 coord, out float w) {\n vec4 a = texture2D(tex, coord);\n vec4 b = texture2D(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n w = mix(a.w, b.w, c);\n return mix(a.xyz, b.xyz, c);\n}\nfloat pseudoRandom (float seed) {\n seed = mod(seed, 233280.);\n float q = (seed * 9301. + 49297.) / 233280.;\n return fract(q);\n}\n#if COLOR_OVER_TIME_MODULE_ENABLE\n uniform sampler2D color_over_time_tex0;\n uniform int u_color_mode;\n#endif\n#if ROTATION_OVER_TIME_MODULE_ENABLE\n uniform sampler2D rotation_over_time_tex0;\n uniform int u_rotation_mode;\n#endif\n#if SIZE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D size_over_time_tex0;\n uniform int u_size_mode;\n#endif\n#if FORCE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D force_over_time_tex0;\n uniform int u_force_mode;\nuniform int u_force_space;\n#endif\n#if VELOCITY_OVER_TIME_MODULE_ENABLE\n uniform sampler2D velocity_over_time_tex0;\n uniform int u_velocity_mode;\nuniform int u_velocity_space;\n#endif\n#if TEXTURE_ANIMATION_MODULE_ENABLE\n uniform sampler2D texture_animation_tex0;\n uniform vec4 u_anim_info;\n#endif\nfloat repeat (float t, float length) {\n return t - floor(t / length) * length;\n}\nvec4 rotateQuat (vec4 p, vec4 q) {\n vec3 iv = cross(q.xyz, p.xyz) + q.w * p.xyz;\n vec3 res = p.xyz + 2.0 * cross(q.xyz, iv);\n return vec4(res.xyz, p.w);\n}\nvec4 gpvs_main () {\n float activeTime = u_timeDelta.x - a_position_starttime.w;\n float normalizedTime = clamp(activeTime / a_dir_life.w, 0.0, 1.0);\n vec2 timeCoord0 = vec2(normalizedTime, 0.);\n vec2 timeCoord1 = vec2(normalizedTime, 1.);\n #if CC_RENDER_MODE == 4\n vec2 vertIdx = vec2(a_texCoord.x, a_texCoord.y);\n #else\n vec2 vertIdx = vec2(a_size_uv.w, a_rotation_uv.w);\n #endif\n vec4 velocity = vec4(a_dir_life.xyz, 0.);\n vec4 pos = vec4(a_position_starttime.xyz, 1.);\n vec3 size = a_size_uv.xyz;\n #if SIZE_OVER_TIME_MODULE_ENABLE\n if (u_size_mode == 1) {\n size *= unpackCurveData(size_over_time_tex0, timeCoord0);\n } else {\n vec3 size_0 = unpackCurveData(size_over_time_tex0, timeCoord0);\n vec3 size_1 = unpackCurveData(size_over_time_tex0, timeCoord1);\n float factor_s = pseudoRandom(a_rndSeed + 39825.);\n size *= mix(size_0, size_1, factor_s);\n }\n #endif\n vec3 compScale = scale.xyz * size;\n #if FORCE_OVER_TIME_MODULE_ENABLE\n vec3 forceAnim = vec3(0.);\n if (u_force_mode == 1) {\n forceAnim = unpackCurveData(force_over_time_tex0, timeCoord0);\n } else {\n vec3 force_0 = unpackCurveData(force_over_time_tex0, timeCoord0);\n vec3 force_1 = unpackCurveData(force_over_time_tex0, timeCoord1);\n float factor_f = pseudoRandom(a_rndSeed + 212165.);\n forceAnim = mix(force_0, force_1, factor_f);\n }\n vec4 forceTrack = vec4(forceAnim, 0.);\n if (u_force_space == 0) {\n forceTrack = rotateQuat(forceTrack, u_worldRot);\n }\n velocity.xyz += forceTrack.xyz * normalizedTime * a_dir_life.w;\n #endif\n #if VELOCITY_OVER_TIME_MODULE_ENABLE\n float speedModifier0 = 1.;\n float speedModifier1 = 1.;\n vec3 velocityAnim = vec3(0.);\n if (u_velocity_mode == 1) {\n velocityAnim = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n } else {\n vec3 vectory_0 = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n vec3 vectory_1 = unpackCurveData(velocity_over_time_tex0, timeCoord1, speedModifier1);\n float factor_v = pseudoRandom(a_rndSeed + 197866.);\n velocityAnim = mix(vectory_0, vectory_1, factor_v);\n speedModifier0 = mix(speedModifier0, speedModifier1, factor_v);\n }\n vec4 velocityTrack = vec4(velocityAnim, 0.);\n if (u_velocity_space == 0) {\n velocityTrack = rotateQuat(velocityTrack, u_worldRot);\n }\n velocity.xyz += velocityTrack.xyz;\n velocity.xyz *= speedModifier0;\n #endif\n pos.xyz += velocity.xyz * normalizedTime * a_dir_life.w;\n #if !CC_USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if CC_RENDER_MODE == 1\n velocity = rotateQuat(velocity, u_worldRot);\n #endif\n #endif\n vec3 rotation = a_rotation_uv.xyz;\n #if ROTATION_OVER_TIME_MODULE_ENABLE\n if (u_rotation_mode == 1) {\n rotation += unpackCurveData(rotation_over_time_tex0, timeCoord0) * normalizedTime * a_dir_life.w;\n } else {\n vec3 rotation_0 = unpackCurveData(rotation_over_time_tex0, timeCoord0);\n vec3 rotation_1 = unpackCurveData(rotation_over_time_tex0, timeCoord1);\n float factor_r = pseudoRandom(a_rndSeed + 125292.);\n rotation += mix(rotation_0, rotation_1, factor_r);\n }\n #endif\n #if COLOR_OVER_TIME_MODULE_ENABLE\n if (u_color_mode == 1) {\n color = a_color * texture2D(color_over_time_tex0, timeCoord0);\n } else {\n vec4 color_0 = texture2D(color_over_time_tex0, timeCoord0);\n vec4 color_1 = texture2D(color_over_time_tex0, timeCoord1);\n float factor_c = pseudoRandom(a_rndSeed + 91041.);\n color = a_color * mix(color_0, color_1, factor_c);\n }\n #else\n color = a_color;\n #endif\n #if CC_RENDER_MODE != 4\n vec2 cornerOffset = vec2((vertIdx - 0.5));\n #if CC_RENDER_MODE == 0\n vec3 rotEuler = rotation.xyz;\n #elif CC_RENDER_MODE == 1\n vec3 rotEuler = vec3(0.);\n #else\n vec3 rotEuler = vec3(0., 0., rotation.z);\n #endif\n computeVertPos(pos, cornerOffset, quaternionFromEuler(rotEuler), compScale\n #if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , cc_matViewInv\n #endif\n #if CC_RENDER_MODE == 1\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_size_uv.w\n #endif\n );\n #else\n mat4 xformNoScale = matrixFromRT(quaternionFromEuler(rotation), pos.xyz);\n mat4 xform = matFromRTS(quaternionFromEuler(rotation), pos.xyz, compScale);\n pos = xform * vec4(a_texCoord3, 1);\n vec4 normal = xformNoScale * vec4(a_normal, 0);\n color *= a_color1;\n #endif\n pos = cc_matViewProj * pos;\n float frameIndex = 0.;\n #if TEXTURE_ANIMATION_MODULE_ENABLE\n float startFrame = 0.;\n vec3 frameInfo = vec3(0.);\n if (int(u_anim_info.x) == 1) {\n frameInfo = unpackCurveData(texture_animation_tex0, timeCoord0);\n } else {\n vec3 frameInfo0 = unpackCurveData(texture_animation_tex0, timeCoord0);\n vec3 frameInfo1 = unpackCurveData(texture_animation_tex0, timeCoord1);\n float factor_t = pseudoRandom(a_rndSeed + 90794.);\n frameInfo = mix(frameInfo0, frameInfo1, factor_t);\n }\n startFrame = frameInfo.x / u_anim_info.y;\n frameIndex = repeat(u_anim_info.z * (frameInfo.y + startFrame), 1.);\n #endif\n uv = computeUV(frameIndex, vertIdx, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n return pos;\n}\nvoid main() { gl_Position = gpvs_main(); }`, + "frag": `\nprecision mediump float;\nuniform mediump vec4 cc_exposure;\nvec3 SRGBToLinear (vec3 gamma) {\n return gamma * gamma;\n}\nvec4 CCFragOutput (vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nvarying vec2 uv;\nvarying vec4 color;\nuniform sampler2D mainTexture;\nvec4 addSmooth () {\n vec4 col = color * texture2D(mainTexture, uv);\n col.rgb *= col.a;\n return CCFragOutput(col);\n}\nvoid main() { gl_FragColor = addSmooth(); }` + }, + "glsl4": { + "vert": `\nprecision mediump float;\nvec4 quaternionFromAxis (vec3 xAxis,vec3 yAxis,vec3 zAxis){\n mat3 m = mat3(xAxis,yAxis,zAxis);\n float trace = m[0][0] + m[1][1] + m[2][2];\n vec4 quat;\n if (trace > 0.) {\n float s = 0.5 / sqrt(trace + 1.0);\n quat.w = 0.25 / s;\n quat.x = (m[2][1] - m[1][2]) * s;\n quat.y = (m[0][2] - m[2][0]) * s;\n quat.z = (m[1][0] - m[0][1]) * s;\n } else if ((m[0][0] > m[1][1]) && (m[0][0] > m[2][2])) {\n float s = 2.0 * sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]);\n quat.w = (m[2][1] - m[1][2]) / s;\n quat.x = 0.25 * s;\n quat.y = (m[0][1] + m[1][0]) / s;\n quat.z = (m[0][2] + m[2][0]) / s;\n } else if (m[1][1] > m[2][2]) {\n float s = 2.0 * sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]);\n quat.w = (m[0][2] - m[2][0]) / s;\n quat.x = (m[0][1] + m[1][0]) / s;\n quat.y = 0.25 * s;\n quat.z = (m[1][2] + m[2][1]) / s;\n } else {\n float s = 2.0 * sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]);\n quat.w = (m[1][0] - m[0][1]) / s;\n quat.x = (m[0][2] + m[2][0]) / s;\n quat.y = (m[1][2] + m[2][1]) / s;\n quat.z = 0.25 * s;\n }\n float len = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;\n if (len > 0.) {\n len = 1. / sqrt(len);\n quat.x = quat.x * len;\n quat.y = quat.y * len;\n quat.z = quat.z * len;\n quat.w = quat.w * len;\n }\n return quat;\n}\nvec4 quaternionFromEuler (vec3 angle){\n float x = angle.x / 2.;\n float y = angle.y / 2.;\n float z = angle.z / 2.;\n float sx = sin(x);\n float cx = cos(x);\n float sy = sin(y);\n float cy = cos(y);\n float sz = sin(z);\n float cz = cos(z);\n vec4 quat = vec4(0);\n quat.x = sx * cy * cz + cx * sy * sz;\n quat.y = cx * sy * cz + sx * cy * sz;\n quat.z = cx * cy * sz - sx * sy * cz;\n quat.w = cx * cy * cz - sx * sy * sz;\n return quat;\n}\nmat4 matrixFromRT (vec4 q, vec3 p){\n float x2 = q.x + q.x;\n float y2 = q.y + q.y;\n float z2 = q.z + q.z;\n float xx = q.x * x2;\n float xy = q.x * y2;\n float xz = q.x * z2;\n float yy = q.y * y2;\n float yz = q.y * z2;\n float zz = q.z * z2;\n float wx = q.w * x2;\n float wy = q.w * y2;\n float wz = q.w * z2;\n return mat4(\n 1. - (yy + zz), xy + wz, xz - wy, 0,\n xy - wz, 1. - (xx + zz), yz + wx, 0,\n xz + wy, yz - wx, 1. - (xx + yy), 0,\n p.x, p.y, p.z, 1\n );\n}\nmat4 matFromRTS (vec4 q, vec3 t, vec3 s){\n float x = q.x, y = q.y, z = q.z, w = q.w;\n float x2 = x + x;\n float y2 = y + y;\n float z2 = z + z;\n float xx = x * x2;\n float xy = x * y2;\n float xz = x * z2;\n float yy = y * y2;\n float yz = y * z2;\n float zz = z * z2;\n float wx = w * x2;\n float wy = w * y2;\n float wz = w * z2;\n float sx = s.x;\n float sy = s.y;\n float sz = s.z;\n return mat4((1. - (yy + zz)) * sx, (xy + wz) * sx, (xz - wy) * sx, 0,\n (xy - wz) * sy, (1. - (xx + zz)) * sy, (yz + wx) * sy, 0,\n (xz + wy) * sz, (yz - wx) * sz, (1. - (xx + yy)) * sz, 0,\n t.x, t.y, t.z, 1);\n}\nvec4 quatMultiply (vec4 a, vec4 b){\n vec4 quat;\n quat.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;\n quat.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;\n quat.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;\n quat.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;\n return quat;\n}\nvoid rotateVecFromQuat (inout vec3 v, vec4 q){\n float ix = q.w * v.x + q.y * v.z - q.z * v.y;\n float iy = q.w * v.y + q.z * v.x - q.x * v.z;\n float iz = q.w * v.z + q.x * v.y - q.y * v.x;\n float iw = -q.x * v.x - q.y * v.y - q.z * v.z;\n v.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;\n v.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;\n v.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;\n}\nvec3 rotateInLocalSpace (vec3 pos, vec3 xAxis, vec3 yAxis, vec3 zAxis, vec4 q){\n vec4 viewQuat = quaternionFromAxis(xAxis, yAxis, zAxis);\n vec4 rotQuat = quatMultiply(viewQuat, q);\n rotateVecFromQuat(pos, rotQuat);\n return pos;\n}\nvoid rotateCorner (inout vec2 corner, float angle){\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nlayout(binding = 0) uniform Constants {\n vec4 mainTiling_Offset;\n vec4 frameTile_velLenScale;\n vec4 scale;\n};\nlayout(binding = 23) uniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nlayout(binding = 21) uniform CCLocal {\n highp mat4 cc_matWorld;\n highp mat4 cc_matWorldIT;\n};\nlayout(location = 0) out vec2 uv;\nlayout(location = 1) out vec4 color;\nvoid computeVertPos (inout vec4 pos, vec2 vertOffset, vec4 q, vec3 s\n#if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , mat4 viewInv\n#endif\n#if CC_RENDER_MODE == 1\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float xIndex\n#endif\n) {\n#if CC_RENDER_MODE == 0\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = normalize(vec3(viewInv[0][0], viewInv[1][0], viewInv[2][0]));\n vec3 camY = normalize(vec3(viewInv[0][1], viewInv[1][1], viewInv[2][1]));\n vec3 camZ = normalize(vec3(viewInv[0][2], viewInv[1][2], viewInv[2][2]));\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, camZ, q);\n#elif CC_RENDER_MODE == 1\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz)) * s.x;\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * s.y;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif CC_RENDER_MODE == 2\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = vec3(1, 0, 0);\n vec3 camY = vec3(0, 0, -1);\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, cross(camX, camY), q);\n#elif CC_RENDER_MODE == 3\n vec2 viewSpaceVert = vec2(vertOffset.x * s.x, vertOffset.y * s.y);\n rotateCorner(viewSpaceVert, q.z);\n vec3 camX = normalize(vec3(cc_matView[0][0], cc_matView[1][0], cc_matView[2][0]));\n vec3 camY = vec3(0, 1, 0);\n vec3 offset = camX * viewSpaceVert.x + camY * viewSpaceVert.y;\n pos.xyz += offset;\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV (float frameIndex, vec2 vertIndex, vec2 frameTile){\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n#if CC_RENDER_MODE != 4\n vertIndex.y = 1. - vertIndex.y;\n#endif\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nlayout(binding = 1) uniform SampleConstants {\n float sampleInterval;\n float sampleNum;\n};\nlayout(binding = 2) uniform TickConstants {\n vec4 u_worldRot;\n vec4 u_timeDelta;\n};\nlayout(location = 0) in vec4 a_position_starttime;\nlayout(location = 1) in vec4 a_size_uv;\nlayout(location = 2) in vec4 a_rotation_uv;\nlayout(location = 3) in vec4 a_color;\nlayout(location = 4) in vec4 a_dir_life;\nlayout(location = 5) in float a_rndSeed;\n#if CC_RENDER_MODE == 4\n layout(location = 6) in vec3 a_texCoord;\n layout(location = 7) in vec3 a_texCoord3;\n layout(location = 8) in vec3 a_normal;\n layout(location = 9) in vec4 a_color1;\n#endif\nvec3 unpackCurveData (sampler2D tex, vec2 coord) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n return mix(a.xyz, b.xyz, c);\n}\nvec3 unpackCurveData (sampler2D tex, vec2 coord, layout(location = 2) out float w) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n w = mix(a.w, b.w, c);\n return mix(a.xyz, b.xyz, c);\n}\nfloat pseudoRandom (float seed) {\n seed = mod(seed, 233280.);\n float q = (seed * 9301. + 49297.) / 233280.;\n return fract(q);\n}\n#if COLOR_OVER_TIME_MODULE_ENABLE\n layout(binding = 30) uniform sampler2D color_over_time_tex0;\n layout(binding = 3) uniform ColorConstant {\n int u_color_mode;\n };\n#endif\n#if ROTATION_OVER_TIME_MODULE_ENABLE\n layout(binding = 31) uniform sampler2D rotation_over_time_tex0;\n layout(binding = 4) uniform RotationConstant {\n int u_rotation_mode;\n };\n#endif\n#if SIZE_OVER_TIME_MODULE_ENABLE\n layout(binding = 32) uniform sampler2D size_over_time_tex0;\n layout(binding = 5) uniform SizeConstant {\n int u_size_mode;\n };\n#endif\n#if FORCE_OVER_TIME_MODULE_ENABLE\n layout(binding = 33) uniform sampler2D force_over_time_tex0;\n layout(binding = 6) uniform ForceConstant {\n int u_force_mode;\n int u_force_space;\n };\n#endif\n#if VELOCITY_OVER_TIME_MODULE_ENABLE\n layout(binding = 34) uniform sampler2D velocity_over_time_tex0;\n layout(binding = 7) uniform VelocityConstant {\n int u_velocity_mode;\n int u_velocity_space;\n };\n#endif\n#if TEXTURE_ANIMATION_MODULE_ENABLE\n layout(binding = 35) uniform sampler2D texture_animation_tex0;\n layout(binding = 8) uniform AnimationConstant {\n vec4 u_anim_info;\n };\n#endif\nfloat repeat (float t, float length) {\n return t - floor(t / length) * length;\n}\nvec4 rotateQuat (vec4 p, vec4 q) {\n vec3 iv = cross(q.xyz, p.xyz) + q.w * p.xyz;\n vec3 res = p.xyz + 2.0 * cross(q.xyz, iv);\n return vec4(res.xyz, p.w);\n}\nvec4 gpvs_main () {\n float activeTime = u_timeDelta.x - a_position_starttime.w;\n float normalizedTime = clamp(activeTime / a_dir_life.w, 0.0, 1.0);\n vec2 timeCoord0 = vec2(normalizedTime, 0.);\n vec2 timeCoord1 = vec2(normalizedTime, 1.);\n #if CC_RENDER_MODE == 4\n vec2 vertIdx = vec2(a_texCoord.x, a_texCoord.y);\n #else\n vec2 vertIdx = vec2(a_size_uv.w, a_rotation_uv.w);\n #endif\n vec4 velocity = vec4(a_dir_life.xyz, 0.);\n vec4 pos = vec4(a_position_starttime.xyz, 1.);\n vec3 size = a_size_uv.xyz;\n #if SIZE_OVER_TIME_MODULE_ENABLE\n if (u_size_mode == 1) {\n size *= unpackCurveData(size_over_time_tex0, timeCoord0);\n } else {\n vec3 size_0 = unpackCurveData(size_over_time_tex0, timeCoord0);\n vec3 size_1 = unpackCurveData(size_over_time_tex0, timeCoord1);\n float factor_s = pseudoRandom(a_rndSeed + 39825.);\n size *= mix(size_0, size_1, factor_s);\n }\n #endif\n vec3 compScale = scale.xyz * size;\n #if FORCE_OVER_TIME_MODULE_ENABLE\n vec3 forceAnim = vec3(0.);\n if (u_force_mode == 1) {\n forceAnim = unpackCurveData(force_over_time_tex0, timeCoord0);\n } else {\n vec3 force_0 = unpackCurveData(force_over_time_tex0, timeCoord0);\n vec3 force_1 = unpackCurveData(force_over_time_tex0, timeCoord1);\n float factor_f = pseudoRandom(a_rndSeed + 212165.);\n forceAnim = mix(force_0, force_1, factor_f);\n }\n vec4 forceTrack = vec4(forceAnim, 0.);\n if (u_force_space == 0) {\n forceTrack = rotateQuat(forceTrack, u_worldRot);\n }\n velocity.xyz += forceTrack.xyz * normalizedTime * a_dir_life.w;\n #endif\n #if VELOCITY_OVER_TIME_MODULE_ENABLE\n float speedModifier0 = 1.;\n float speedModifier1 = 1.;\n vec3 velocityAnim = vec3(0.);\n if (u_velocity_mode == 1) {\n velocityAnim = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n } else {\n vec3 vectory_0 = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n vec3 vectory_1 = unpackCurveData(velocity_over_time_tex0, timeCoord1, speedModifier1);\n float factor_v = pseudoRandom(a_rndSeed + 197866.);\n velocityAnim = mix(vectory_0, vectory_1, factor_v);\n speedModifier0 = mix(speedModifier0, speedModifier1, factor_v);\n }\n vec4 velocityTrack = vec4(velocityAnim, 0.);\n if (u_velocity_space == 0) {\n velocityTrack = rotateQuat(velocityTrack, u_worldRot);\n }\n velocity.xyz += velocityTrack.xyz;\n velocity.xyz *= speedModifier0;\n #endif\n pos.xyz += velocity.xyz * normalizedTime * a_dir_life.w;\n #if !CC_USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if CC_RENDER_MODE == 1\n velocity = rotateQuat(velocity, u_worldRot);\n #endif\n #endif\n vec3 rotation = a_rotation_uv.xyz;\n #if ROTATION_OVER_TIME_MODULE_ENABLE\n if (u_rotation_mode == 1) {\n rotation += unpackCurveData(rotation_over_time_tex0, timeCoord0) * normalizedTime * a_dir_life.w;\n } else {\n vec3 rotation_0 = unpackCurveData(rotation_over_time_tex0, timeCoord0);\n vec3 rotation_1 = unpackCurveData(rotation_over_time_tex0, timeCoord1);\n float factor_r = pseudoRandom(a_rndSeed + 125292.);\n rotation += mix(rotation_0, rotation_1, factor_r);\n }\n #endif\n #if COLOR_OVER_TIME_MODULE_ENABLE\n if (u_color_mode == 1) {\n color = a_color * texture(color_over_time_tex0, timeCoord0);\n } else {\n vec4 color_0 = texture(color_over_time_tex0, timeCoord0);\n vec4 color_1 = texture(color_over_time_tex0, timeCoord1);\n float factor_c = pseudoRandom(a_rndSeed + 91041.);\n color = a_color * mix(color_0, color_1, factor_c);\n }\n #else\n color = a_color;\n #endif\n #if CC_RENDER_MODE != 4\n vec2 cornerOffset = vec2((vertIdx - 0.5));\n #if CC_RENDER_MODE == 0\n vec3 rotEuler = rotation.xyz;\n #elif CC_RENDER_MODE == 1\n vec3 rotEuler = vec3(0.);\n #else\n vec3 rotEuler = vec3(0., 0., rotation.z);\n #endif\n computeVertPos(pos, cornerOffset, quaternionFromEuler(rotEuler), compScale\n #if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , cc_matViewInv\n #endif\n #if CC_RENDER_MODE == 1\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_size_uv.w\n #endif\n );\n #else\n mat4 xformNoScale = matrixFromRT(quaternionFromEuler(rotation), pos.xyz);\n mat4 xform = matFromRTS(quaternionFromEuler(rotation), pos.xyz, compScale);\n pos = xform * vec4(a_texCoord3, 1);\n vec4 normal = xformNoScale * vec4(a_normal, 0);\n color *= a_color1;\n #endif\n pos = cc_matViewProj * pos;\n float frameIndex = 0.;\n #if TEXTURE_ANIMATION_MODULE_ENABLE\n float startFrame = 0.;\n vec3 frameInfo = vec3(0.);\n if (int(u_anim_info.x) == 1) {\n frameInfo = unpackCurveData(texture_animation_tex0, timeCoord0);\n } else {\n vec3 frameInfo0 = unpackCurveData(texture_animation_tex0, timeCoord0);\n vec3 frameInfo1 = unpackCurveData(texture_animation_tex0, timeCoord1);\n float factor_t = pseudoRandom(a_rndSeed + 90794.);\n frameInfo = mix(frameInfo0, frameInfo1, factor_t);\n }\n startFrame = frameInfo.x / u_anim_info.y;\n frameIndex = repeat(u_anim_info.z * (frameInfo.y + startFrame), 1.);\n #endif\n uv = computeUV(frameIndex, vertIdx, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n return pos;\n}\nvoid main() { gl_Position = gpvs_main(); }`, + "frag": `\nprecision mediump float;\nlayout(binding = 23) uniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nvec3 SRGBToLinear (vec3 gamma) {\n return gamma * gamma;\n}\nvec4 CCFragOutput (vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nlayout(location = 0) in vec2 uv;\nlayout(location = 1) in vec4 color;\nlayout(binding = 36) uniform sampler2D mainTexture;\nvec4 addSmooth () {\n vec4 col = color * texture(mainTexture, uv);\n col.rgb *= col.a;\n return CCFragOutput(col);\n}\nlayout(location = 0) out vec4 cc_FragColor;\nvoid main() { cc_FragColor = addSmooth(); }` + }, + "varyings": [ + { "name": "uv", "type": 14, "count": 1, "defines": [], "location": 0 }, + { "name": "color", "type": 16, "count": 1, "defines": [], "location": 1 }, + { "name": "w", "type": 13, "count": 1, "defines": [], "location": 2 } + ], + "builtins": { + "globals": { "blocks": [{ "name": "CCGlobal", "defines": [] }], "samplers": [] }, + "locals": { "blocks": [{ "name": "CCLocal", "defines": [] }], "samplers": [] } + }, + "defines": [ + { "name": "CC_RENDER_MODE", "type": "number", "defines": [], "range": [0, 4] }, + { "name": "COLOR_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "ROTATION_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "SIZE_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "FORCE_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "VELOCITY_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "TEXTURE_ANIMATION_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "CC_USE_WORLD_SPACE", "type": "boolean", "defines": [] }, + { "name": "CC_USE_HDR", "type": "boolean", "defines": [] } + ], + "blocks": [ + {"name": "Constants", "defines": [], "binding": 0, "members": [ + { "name": "mainTiling_Offset", "type": 16, "count": 1 }, + { "name": "frameTile_velLenScale", "type": 16, "count": 1 }, + { "name": "scale", "type": 16, "count": 1 } + ]}, + {"name": "SampleConstants", "defines": [], "binding": 1, "members": [ + { "name": "sampleInterval", "type": 13, "count": 1 }, + { "name": "sampleNum", "type": 13, "count": 1 } + ]}, + {"name": "TickConstants", "defines": [], "binding": 2, "members": [ + { "name": "u_worldRot", "type": 16, "count": 1 }, + { "name": "u_timeDelta", "type": 16, "count": 1 } + ]}, + {"name": "ColorConstant", "defines": ["COLOR_OVER_TIME_MODULE_ENABLE"], "binding": 3, "members": [ + { "name": "u_color_mode", "type": 5, "count": 1 } + ]}, + {"name": "RotationConstant", "defines": ["ROTATION_OVER_TIME_MODULE_ENABLE"], "binding": 4, "members": [ + { "name": "u_rotation_mode", "type": 5, "count": 1 } + ]}, + {"name": "SizeConstant", "defines": ["SIZE_OVER_TIME_MODULE_ENABLE"], "binding": 5, "members": [ + { "name": "u_size_mode", "type": 5, "count": 1 } + ]}, + {"name": "ForceConstant", "defines": ["FORCE_OVER_TIME_MODULE_ENABLE"], "binding": 6, "members": [ + { "name": "u_force_mode", "type": 5, "count": 1 }, + { "name": "u_force_space", "type": 5, "count": 1 } + ]}, + {"name": "VelocityConstant", "defines": ["VELOCITY_OVER_TIME_MODULE_ENABLE"], "binding": 7, "members": [ + { "name": "u_velocity_mode", "type": 5, "count": 1 }, + { "name": "u_velocity_space", "type": 5, "count": 1 } + ]}, + {"name": "AnimationConstant", "defines": ["TEXTURE_ANIMATION_MODULE_ENABLE"], "binding": 8, "members": [ + { "name": "u_anim_info", "type": 16, "count": 1 } + ]} + ], + "samplers": [ + { "name": "color_over_time_tex0", "type": 28, "count": 1, "defines": ["COLOR_OVER_TIME_MODULE_ENABLE"], "binding": 30 }, + { "name": "rotation_over_time_tex0", "type": 28, "count": 1, "defines": ["ROTATION_OVER_TIME_MODULE_ENABLE"], "binding": 31 }, + { "name": "size_over_time_tex0", "type": 28, "count": 1, "defines": ["SIZE_OVER_TIME_MODULE_ENABLE"], "binding": 32 }, + { "name": "force_over_time_tex0", "type": 28, "count": 1, "defines": ["FORCE_OVER_TIME_MODULE_ENABLE"], "binding": 33 }, + { "name": "velocity_over_time_tex0", "type": 28, "count": 1, "defines": ["VELOCITY_OVER_TIME_MODULE_ENABLE"], "binding": 34 }, + { "name": "texture_animation_tex0", "type": 28, "count": 1, "defines": ["TEXTURE_ANIMATION_MODULE_ENABLE"], "binding": 35 }, + { "name": "mainTexture", "type": 28, "count": 1, "defines": [], "binding": 36 } + ], + "attributes": [ + { "name": "a_position_starttime", "type": 16, "count": 1, "defines": [], "format": 43, "location": 0 }, + { "name": "a_size_uv", "type": 16, "count": 1, "defines": [], "format": 43, "location": 1 }, + { "name": "a_rotation_uv", "type": 16, "count": 1, "defines": [], "format": 43, "location": 2 }, + { "name": "a_color", "type": 16, "count": 1, "defines": [], "format": 43, "location": 3 }, + { "name": "a_dir_life", "type": 16, "count": 1, "defines": [], "format": 43, "location": 4 }, + { "name": "a_rndSeed", "type": 13, "count": 1, "defines": [], "format": 11, "location": 5 }, + { "name": "a_texCoord", "type": 15, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 32, "location": 6 }, + { "name": "a_texCoord3", "type": 15, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 32, "location": 7 }, + { "name": "a_normal", "type": 15, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 32, "location": 8 }, + { "name": "a_color1", "type": 16, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 43, "location": 9 } + ] + }, + { + "name": "builtin-particle-gpu|particle-vs-gpu:gpvs_main|no-tint-fs:premultiplied", + "hash": 1612254051, + "glsl3": { + "vert": `\nprecision mediump float;\nvec4 quaternionFromAxis (vec3 xAxis,vec3 yAxis,vec3 zAxis){\n mat3 m = mat3(xAxis,yAxis,zAxis);\n float trace = m[0][0] + m[1][1] + m[2][2];\n vec4 quat;\n if (trace > 0.) {\n float s = 0.5 / sqrt(trace + 1.0);\n quat.w = 0.25 / s;\n quat.x = (m[2][1] - m[1][2]) * s;\n quat.y = (m[0][2] - m[2][0]) * s;\n quat.z = (m[1][0] - m[0][1]) * s;\n } else if ((m[0][0] > m[1][1]) && (m[0][0] > m[2][2])) {\n float s = 2.0 * sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]);\n quat.w = (m[2][1] - m[1][2]) / s;\n quat.x = 0.25 * s;\n quat.y = (m[0][1] + m[1][0]) / s;\n quat.z = (m[0][2] + m[2][0]) / s;\n } else if (m[1][1] > m[2][2]) {\n float s = 2.0 * sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]);\n quat.w = (m[0][2] - m[2][0]) / s;\n quat.x = (m[0][1] + m[1][0]) / s;\n quat.y = 0.25 * s;\n quat.z = (m[1][2] + m[2][1]) / s;\n } else {\n float s = 2.0 * sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]);\n quat.w = (m[1][0] - m[0][1]) / s;\n quat.x = (m[0][2] + m[2][0]) / s;\n quat.y = (m[1][2] + m[2][1]) / s;\n quat.z = 0.25 * s;\n }\n float len = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;\n if (len > 0.) {\n len = 1. / sqrt(len);\n quat.x = quat.x * len;\n quat.y = quat.y * len;\n quat.z = quat.z * len;\n quat.w = quat.w * len;\n }\n return quat;\n}\nvec4 quaternionFromEuler (vec3 angle){\n float x = angle.x / 2.;\n float y = angle.y / 2.;\n float z = angle.z / 2.;\n float sx = sin(x);\n float cx = cos(x);\n float sy = sin(y);\n float cy = cos(y);\n float sz = sin(z);\n float cz = cos(z);\n vec4 quat = vec4(0);\n quat.x = sx * cy * cz + cx * sy * sz;\n quat.y = cx * sy * cz + sx * cy * sz;\n quat.z = cx * cy * sz - sx * sy * cz;\n quat.w = cx * cy * cz - sx * sy * sz;\n return quat;\n}\nmat4 matrixFromRT (vec4 q, vec3 p){\n float x2 = q.x + q.x;\n float y2 = q.y + q.y;\n float z2 = q.z + q.z;\n float xx = q.x * x2;\n float xy = q.x * y2;\n float xz = q.x * z2;\n float yy = q.y * y2;\n float yz = q.y * z2;\n float zz = q.z * z2;\n float wx = q.w * x2;\n float wy = q.w * y2;\n float wz = q.w * z2;\n return mat4(\n 1. - (yy + zz), xy + wz, xz - wy, 0,\n xy - wz, 1. - (xx + zz), yz + wx, 0,\n xz + wy, yz - wx, 1. - (xx + yy), 0,\n p.x, p.y, p.z, 1\n );\n}\nmat4 matFromRTS (vec4 q, vec3 t, vec3 s){\n float x = q.x, y = q.y, z = q.z, w = q.w;\n float x2 = x + x;\n float y2 = y + y;\n float z2 = z + z;\n float xx = x * x2;\n float xy = x * y2;\n float xz = x * z2;\n float yy = y * y2;\n float yz = y * z2;\n float zz = z * z2;\n float wx = w * x2;\n float wy = w * y2;\n float wz = w * z2;\n float sx = s.x;\n float sy = s.y;\n float sz = s.z;\n return mat4((1. - (yy + zz)) * sx, (xy + wz) * sx, (xz - wy) * sx, 0,\n (xy - wz) * sy, (1. - (xx + zz)) * sy, (yz + wx) * sy, 0,\n (xz + wy) * sz, (yz - wx) * sz, (1. - (xx + yy)) * sz, 0,\n t.x, t.y, t.z, 1);\n}\nvec4 quatMultiply (vec4 a, vec4 b){\n vec4 quat;\n quat.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;\n quat.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;\n quat.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;\n quat.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;\n return quat;\n}\nvoid rotateVecFromQuat (inout vec3 v, vec4 q){\n float ix = q.w * v.x + q.y * v.z - q.z * v.y;\n float iy = q.w * v.y + q.z * v.x - q.x * v.z;\n float iz = q.w * v.z + q.x * v.y - q.y * v.x;\n float iw = -q.x * v.x - q.y * v.y - q.z * v.z;\n v.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;\n v.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;\n v.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;\n}\nvec3 rotateInLocalSpace (vec3 pos, vec3 xAxis, vec3 yAxis, vec3 zAxis, vec4 q){\n vec4 viewQuat = quaternionFromAxis(xAxis, yAxis, zAxis);\n vec4 rotQuat = quatMultiply(viewQuat, q);\n rotateVecFromQuat(pos, rotQuat);\n return pos;\n}\nvoid rotateCorner (inout vec2 corner, float angle){\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nuniform Constants {\n vec4 mainTiling_Offset;\n vec4 frameTile_velLenScale;\n vec4 scale;\n};\nuniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nuniform CCLocal {\n highp mat4 cc_matWorld;\n highp mat4 cc_matWorldIT;\n};\nout vec2 uv;\nout vec4 color;\nvoid computeVertPos (inout vec4 pos, vec2 vertOffset, vec4 q, vec3 s\n#if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , mat4 viewInv\n#endif\n#if CC_RENDER_MODE == 1\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float xIndex\n#endif\n) {\n#if CC_RENDER_MODE == 0\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = normalize(vec3(viewInv[0][0], viewInv[1][0], viewInv[2][0]));\n vec3 camY = normalize(vec3(viewInv[0][1], viewInv[1][1], viewInv[2][1]));\n vec3 camZ = normalize(vec3(viewInv[0][2], viewInv[1][2], viewInv[2][2]));\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, camZ, q);\n#elif CC_RENDER_MODE == 1\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz)) * s.x;\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * s.y;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif CC_RENDER_MODE == 2\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = vec3(1, 0, 0);\n vec3 camY = vec3(0, 0, -1);\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, cross(camX, camY), q);\n#elif CC_RENDER_MODE == 3\n vec2 viewSpaceVert = vec2(vertOffset.x * s.x, vertOffset.y * s.y);\n rotateCorner(viewSpaceVert, q.z);\n vec3 camX = normalize(vec3(cc_matView[0][0], cc_matView[1][0], cc_matView[2][0]));\n vec3 camY = vec3(0, 1, 0);\n vec3 offset = camX * viewSpaceVert.x + camY * viewSpaceVert.y;\n pos.xyz += offset;\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV (float frameIndex, vec2 vertIndex, vec2 frameTile){\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n#if CC_RENDER_MODE != 4\n vertIndex.y = 1. - vertIndex.y;\n#endif\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nuniform SampleConstants {\n float sampleInterval;\n float sampleNum;\n};\nuniform TickConstants {\n vec4 u_worldRot;\n vec4 u_timeDelta;\n};\nin vec4 a_position_starttime;\nin vec4 a_size_uv;\nin vec4 a_rotation_uv;\nin vec4 a_color;\nin vec4 a_dir_life;\nin float a_rndSeed;\n#if CC_RENDER_MODE == 4\n in vec3 a_texCoord;\n in vec3 a_texCoord3;\n in vec3 a_normal;\n in vec4 a_color1;\n#endif\nvec3 unpackCurveData (sampler2D tex, vec2 coord) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n return mix(a.xyz, b.xyz, c);\n}\nvec3 unpackCurveData (sampler2D tex, vec2 coord, out float w) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n w = mix(a.w, b.w, c);\n return mix(a.xyz, b.xyz, c);\n}\nfloat pseudoRandom (float seed) {\n seed = mod(seed, 233280.);\n float q = (seed * 9301. + 49297.) / 233280.;\n return fract(q);\n}\n#if COLOR_OVER_TIME_MODULE_ENABLE\n uniform sampler2D color_over_time_tex0;\n uniform ColorConstant {\n int u_color_mode;\n };\n#endif\n#if ROTATION_OVER_TIME_MODULE_ENABLE\n uniform sampler2D rotation_over_time_tex0;\n uniform RotationConstant {\n int u_rotation_mode;\n };\n#endif\n#if SIZE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D size_over_time_tex0;\n uniform SizeConstant {\n int u_size_mode;\n };\n#endif\n#if FORCE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D force_over_time_tex0;\n uniform ForceConstant {\n int u_force_mode;\n int u_force_space;\n };\n#endif\n#if VELOCITY_OVER_TIME_MODULE_ENABLE\n uniform sampler2D velocity_over_time_tex0;\n uniform VelocityConstant {\n int u_velocity_mode;\n int u_velocity_space;\n };\n#endif\n#if TEXTURE_ANIMATION_MODULE_ENABLE\n uniform sampler2D texture_animation_tex0;\n uniform AnimationConstant {\n vec4 u_anim_info;\n };\n#endif\nfloat repeat (float t, float length) {\n return t - floor(t / length) * length;\n}\nvec4 rotateQuat (vec4 p, vec4 q) {\n vec3 iv = cross(q.xyz, p.xyz) + q.w * p.xyz;\n vec3 res = p.xyz + 2.0 * cross(q.xyz, iv);\n return vec4(res.xyz, p.w);\n}\nvec4 gpvs_main () {\n float activeTime = u_timeDelta.x - a_position_starttime.w;\n float normalizedTime = clamp(activeTime / a_dir_life.w, 0.0, 1.0);\n vec2 timeCoord0 = vec2(normalizedTime, 0.);\n vec2 timeCoord1 = vec2(normalizedTime, 1.);\n #if CC_RENDER_MODE == 4\n vec2 vertIdx = vec2(a_texCoord.x, a_texCoord.y);\n #else\n vec2 vertIdx = vec2(a_size_uv.w, a_rotation_uv.w);\n #endif\n vec4 velocity = vec4(a_dir_life.xyz, 0.);\n vec4 pos = vec4(a_position_starttime.xyz, 1.);\n vec3 size = a_size_uv.xyz;\n #if SIZE_OVER_TIME_MODULE_ENABLE\n if (u_size_mode == 1) {\n size *= unpackCurveData(size_over_time_tex0, timeCoord0);\n } else {\n vec3 size_0 = unpackCurveData(size_over_time_tex0, timeCoord0);\n vec3 size_1 = unpackCurveData(size_over_time_tex0, timeCoord1);\n float factor_s = pseudoRandom(a_rndSeed + 39825.);\n size *= mix(size_0, size_1, factor_s);\n }\n #endif\n vec3 compScale = scale.xyz * size;\n #if FORCE_OVER_TIME_MODULE_ENABLE\n vec3 forceAnim = vec3(0.);\n if (u_force_mode == 1) {\n forceAnim = unpackCurveData(force_over_time_tex0, timeCoord0);\n } else {\n vec3 force_0 = unpackCurveData(force_over_time_tex0, timeCoord0);\n vec3 force_1 = unpackCurveData(force_over_time_tex0, timeCoord1);\n float factor_f = pseudoRandom(a_rndSeed + 212165.);\n forceAnim = mix(force_0, force_1, factor_f);\n }\n vec4 forceTrack = vec4(forceAnim, 0.);\n if (u_force_space == 0) {\n forceTrack = rotateQuat(forceTrack, u_worldRot);\n }\n velocity.xyz += forceTrack.xyz * normalizedTime * a_dir_life.w;\n #endif\n #if VELOCITY_OVER_TIME_MODULE_ENABLE\n float speedModifier0 = 1.;\n float speedModifier1 = 1.;\n vec3 velocityAnim = vec3(0.);\n if (u_velocity_mode == 1) {\n velocityAnim = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n } else {\n vec3 vectory_0 = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n vec3 vectory_1 = unpackCurveData(velocity_over_time_tex0, timeCoord1, speedModifier1);\n float factor_v = pseudoRandom(a_rndSeed + 197866.);\n velocityAnim = mix(vectory_0, vectory_1, factor_v);\n speedModifier0 = mix(speedModifier0, speedModifier1, factor_v);\n }\n vec4 velocityTrack = vec4(velocityAnim, 0.);\n if (u_velocity_space == 0) {\n velocityTrack = rotateQuat(velocityTrack, u_worldRot);\n }\n velocity.xyz += velocityTrack.xyz;\n velocity.xyz *= speedModifier0;\n #endif\n pos.xyz += velocity.xyz * normalizedTime * a_dir_life.w;\n #if !CC_USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if CC_RENDER_MODE == 1\n velocity = rotateQuat(velocity, u_worldRot);\n #endif\n #endif\n vec3 rotation = a_rotation_uv.xyz;\n #if ROTATION_OVER_TIME_MODULE_ENABLE\n if (u_rotation_mode == 1) {\n rotation += unpackCurveData(rotation_over_time_tex0, timeCoord0) * normalizedTime * a_dir_life.w;\n } else {\n vec3 rotation_0 = unpackCurveData(rotation_over_time_tex0, timeCoord0);\n vec3 rotation_1 = unpackCurveData(rotation_over_time_tex0, timeCoord1);\n float factor_r = pseudoRandom(a_rndSeed + 125292.);\n rotation += mix(rotation_0, rotation_1, factor_r);\n }\n #endif\n #if COLOR_OVER_TIME_MODULE_ENABLE\n if (u_color_mode == 1) {\n color = a_color * texture(color_over_time_tex0, timeCoord0);\n } else {\n vec4 color_0 = texture(color_over_time_tex0, timeCoord0);\n vec4 color_1 = texture(color_over_time_tex0, timeCoord1);\n float factor_c = pseudoRandom(a_rndSeed + 91041.);\n color = a_color * mix(color_0, color_1, factor_c);\n }\n #else\n color = a_color;\n #endif\n #if CC_RENDER_MODE != 4\n vec2 cornerOffset = vec2((vertIdx - 0.5));\n #if CC_RENDER_MODE == 0\n vec3 rotEuler = rotation.xyz;\n #elif CC_RENDER_MODE == 1\n vec3 rotEuler = vec3(0.);\n #else\n vec3 rotEuler = vec3(0., 0., rotation.z);\n #endif\n computeVertPos(pos, cornerOffset, quaternionFromEuler(rotEuler), compScale\n #if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , cc_matViewInv\n #endif\n #if CC_RENDER_MODE == 1\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_size_uv.w\n #endif\n );\n #else\n mat4 xformNoScale = matrixFromRT(quaternionFromEuler(rotation), pos.xyz);\n mat4 xform = matFromRTS(quaternionFromEuler(rotation), pos.xyz, compScale);\n pos = xform * vec4(a_texCoord3, 1);\n vec4 normal = xformNoScale * vec4(a_normal, 0);\n color *= a_color1;\n #endif\n pos = cc_matViewProj * pos;\n float frameIndex = 0.;\n #if TEXTURE_ANIMATION_MODULE_ENABLE\n float startFrame = 0.;\n vec3 frameInfo = vec3(0.);\n if (int(u_anim_info.x) == 1) {\n frameInfo = unpackCurveData(texture_animation_tex0, timeCoord0);\n } else {\n vec3 frameInfo0 = unpackCurveData(texture_animation_tex0, timeCoord0);\n vec3 frameInfo1 = unpackCurveData(texture_animation_tex0, timeCoord1);\n float factor_t = pseudoRandom(a_rndSeed + 90794.);\n frameInfo = mix(frameInfo0, frameInfo1, factor_t);\n }\n startFrame = frameInfo.x / u_anim_info.y;\n frameIndex = repeat(u_anim_info.z * (frameInfo.y + startFrame), 1.);\n #endif\n uv = computeUV(frameIndex, vertIdx, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n return pos;\n}\nvoid main() { gl_Position = gpvs_main(); }`, + "frag": `\nprecision mediump float;\nuniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nvec3 SRGBToLinear (vec3 gamma) {\n return gamma * gamma;\n}\nvec4 CCFragOutput (vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nin vec2 uv;\nin vec4 color;\nuniform sampler2D mainTexture;\nvec4 premultiplied () {\n vec4 col = color * texture(mainTexture, uv) * color.a;\n return CCFragOutput(col);\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = premultiplied(); }` + }, + "glsl1": { + "vert": `\nprecision mediump float;\nvec4 quaternionFromAxis (vec3 xAxis,vec3 yAxis,vec3 zAxis){\n mat3 m = mat3(xAxis,yAxis,zAxis);\n float trace = m[0][0] + m[1][1] + m[2][2];\n vec4 quat;\n if (trace > 0.) {\n float s = 0.5 / sqrt(trace + 1.0);\n quat.w = 0.25 / s;\n quat.x = (m[2][1] - m[1][2]) * s;\n quat.y = (m[0][2] - m[2][0]) * s;\n quat.z = (m[1][0] - m[0][1]) * s;\n } else if ((m[0][0] > m[1][1]) && (m[0][0] > m[2][2])) {\n float s = 2.0 * sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]);\n quat.w = (m[2][1] - m[1][2]) / s;\n quat.x = 0.25 * s;\n quat.y = (m[0][1] + m[1][0]) / s;\n quat.z = (m[0][2] + m[2][0]) / s;\n } else if (m[1][1] > m[2][2]) {\n float s = 2.0 * sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]);\n quat.w = (m[0][2] - m[2][0]) / s;\n quat.x = (m[0][1] + m[1][0]) / s;\n quat.y = 0.25 * s;\n quat.z = (m[1][2] + m[2][1]) / s;\n } else {\n float s = 2.0 * sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]);\n quat.w = (m[1][0] - m[0][1]) / s;\n quat.x = (m[0][2] + m[2][0]) / s;\n quat.y = (m[1][2] + m[2][1]) / s;\n quat.z = 0.25 * s;\n }\n float len = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;\n if (len > 0.) {\n len = 1. / sqrt(len);\n quat.x = quat.x * len;\n quat.y = quat.y * len;\n quat.z = quat.z * len;\n quat.w = quat.w * len;\n }\n return quat;\n}\nvec4 quaternionFromEuler (vec3 angle){\n float x = angle.x / 2.;\n float y = angle.y / 2.;\n float z = angle.z / 2.;\n float sx = sin(x);\n float cx = cos(x);\n float sy = sin(y);\n float cy = cos(y);\n float sz = sin(z);\n float cz = cos(z);\n vec4 quat = vec4(0);\n quat.x = sx * cy * cz + cx * sy * sz;\n quat.y = cx * sy * cz + sx * cy * sz;\n quat.z = cx * cy * sz - sx * sy * cz;\n quat.w = cx * cy * cz - sx * sy * sz;\n return quat;\n}\nmat4 matrixFromRT (vec4 q, vec3 p){\n float x2 = q.x + q.x;\n float y2 = q.y + q.y;\n float z2 = q.z + q.z;\n float xx = q.x * x2;\n float xy = q.x * y2;\n float xz = q.x * z2;\n float yy = q.y * y2;\n float yz = q.y * z2;\n float zz = q.z * z2;\n float wx = q.w * x2;\n float wy = q.w * y2;\n float wz = q.w * z2;\n return mat4(\n 1. - (yy + zz), xy + wz, xz - wy, 0,\n xy - wz, 1. - (xx + zz), yz + wx, 0,\n xz + wy, yz - wx, 1. - (xx + yy), 0,\n p.x, p.y, p.z, 1\n );\n}\nmat4 matFromRTS (vec4 q, vec3 t, vec3 s){\n float x = q.x, y = q.y, z = q.z, w = q.w;\n float x2 = x + x;\n float y2 = y + y;\n float z2 = z + z;\n float xx = x * x2;\n float xy = x * y2;\n float xz = x * z2;\n float yy = y * y2;\n float yz = y * z2;\n float zz = z * z2;\n float wx = w * x2;\n float wy = w * y2;\n float wz = w * z2;\n float sx = s.x;\n float sy = s.y;\n float sz = s.z;\n return mat4((1. - (yy + zz)) * sx, (xy + wz) * sx, (xz - wy) * sx, 0,\n (xy - wz) * sy, (1. - (xx + zz)) * sy, (yz + wx) * sy, 0,\n (xz + wy) * sz, (yz - wx) * sz, (1. - (xx + yy)) * sz, 0,\n t.x, t.y, t.z, 1);\n}\nvec4 quatMultiply (vec4 a, vec4 b){\n vec4 quat;\n quat.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;\n quat.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;\n quat.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;\n quat.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;\n return quat;\n}\nvoid rotateVecFromQuat (inout vec3 v, vec4 q){\n float ix = q.w * v.x + q.y * v.z - q.z * v.y;\n float iy = q.w * v.y + q.z * v.x - q.x * v.z;\n float iz = q.w * v.z + q.x * v.y - q.y * v.x;\n float iw = -q.x * v.x - q.y * v.y - q.z * v.z;\n v.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;\n v.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;\n v.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;\n}\nvec3 rotateInLocalSpace (vec3 pos, vec3 xAxis, vec3 yAxis, vec3 zAxis, vec4 q){\n vec4 viewQuat = quaternionFromAxis(xAxis, yAxis, zAxis);\n vec4 rotQuat = quatMultiply(viewQuat, q);\n rotateVecFromQuat(pos, rotQuat);\n return pos;\n}\nvoid rotateCorner (inout vec2 corner, float angle){\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nuniform vec4 mainTiling_Offset;\nuniform vec4 frameTile_velLenScale;\nuniform vec4 scale;\nuniform highp mat4 cc_matView;\nuniform highp mat4 cc_matViewInv;\nuniform highp mat4 cc_matViewProj;\nuniform highp vec4 cc_cameraPos;\nuniform highp mat4 cc_matWorld;\nvarying vec2 uv;\nvarying vec4 color;\nvoid computeVertPos (inout vec4 pos, vec2 vertOffset, vec4 q, vec3 s\n#if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , mat4 viewInv\n#endif\n#if CC_RENDER_MODE == 1\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float xIndex\n#endif\n) {\n#if CC_RENDER_MODE == 0\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = normalize(vec3(viewInv[0][0], viewInv[1][0], viewInv[2][0]));\n vec3 camY = normalize(vec3(viewInv[0][1], viewInv[1][1], viewInv[2][1]));\n vec3 camZ = normalize(vec3(viewInv[0][2], viewInv[1][2], viewInv[2][2]));\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, camZ, q);\n#elif CC_RENDER_MODE == 1\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz)) * s.x;\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * s.y;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif CC_RENDER_MODE == 2\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = vec3(1, 0, 0);\n vec3 camY = vec3(0, 0, -1);\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, cross(camX, camY), q);\n#elif CC_RENDER_MODE == 3\n vec2 viewSpaceVert = vec2(vertOffset.x * s.x, vertOffset.y * s.y);\n rotateCorner(viewSpaceVert, q.z);\n vec3 camX = normalize(vec3(cc_matView[0][0], cc_matView[1][0], cc_matView[2][0]));\n vec3 camY = vec3(0, 1, 0);\n vec3 offset = camX * viewSpaceVert.x + camY * viewSpaceVert.y;\n pos.xyz += offset;\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV (float frameIndex, vec2 vertIndex, vec2 frameTile){\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n#if CC_RENDER_MODE != 4\n vertIndex.y = 1. - vertIndex.y;\n#endif\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nuniform float sampleInterval;\nuniform float sampleNum;\nuniform vec4 u_worldRot;\nuniform vec4 u_timeDelta;\nattribute vec4 a_position_starttime;\nattribute vec4 a_size_uv;\nattribute vec4 a_rotation_uv;\nattribute vec4 a_color;\nattribute vec4 a_dir_life;\nattribute float a_rndSeed;\n#if CC_RENDER_MODE == 4\n attribute vec3 a_texCoord;\n attribute vec3 a_texCoord3;\n attribute vec3 a_normal;\n attribute vec4 a_color1;\n#endif\nvec3 unpackCurveData (sampler2D tex, vec2 coord) {\n vec4 a = texture2D(tex, coord);\n vec4 b = texture2D(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n return mix(a.xyz, b.xyz, c);\n}\nvec3 unpackCurveData (sampler2D tex, vec2 coord, out float w) {\n vec4 a = texture2D(tex, coord);\n vec4 b = texture2D(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n w = mix(a.w, b.w, c);\n return mix(a.xyz, b.xyz, c);\n}\nfloat pseudoRandom (float seed) {\n seed = mod(seed, 233280.);\n float q = (seed * 9301. + 49297.) / 233280.;\n return fract(q);\n}\n#if COLOR_OVER_TIME_MODULE_ENABLE\n uniform sampler2D color_over_time_tex0;\n uniform int u_color_mode;\n#endif\n#if ROTATION_OVER_TIME_MODULE_ENABLE\n uniform sampler2D rotation_over_time_tex0;\n uniform int u_rotation_mode;\n#endif\n#if SIZE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D size_over_time_tex0;\n uniform int u_size_mode;\n#endif\n#if FORCE_OVER_TIME_MODULE_ENABLE\n uniform sampler2D force_over_time_tex0;\n uniform int u_force_mode;\nuniform int u_force_space;\n#endif\n#if VELOCITY_OVER_TIME_MODULE_ENABLE\n uniform sampler2D velocity_over_time_tex0;\n uniform int u_velocity_mode;\nuniform int u_velocity_space;\n#endif\n#if TEXTURE_ANIMATION_MODULE_ENABLE\n uniform sampler2D texture_animation_tex0;\n uniform vec4 u_anim_info;\n#endif\nfloat repeat (float t, float length) {\n return t - floor(t / length) * length;\n}\nvec4 rotateQuat (vec4 p, vec4 q) {\n vec3 iv = cross(q.xyz, p.xyz) + q.w * p.xyz;\n vec3 res = p.xyz + 2.0 * cross(q.xyz, iv);\n return vec4(res.xyz, p.w);\n}\nvec4 gpvs_main () {\n float activeTime = u_timeDelta.x - a_position_starttime.w;\n float normalizedTime = clamp(activeTime / a_dir_life.w, 0.0, 1.0);\n vec2 timeCoord0 = vec2(normalizedTime, 0.);\n vec2 timeCoord1 = vec2(normalizedTime, 1.);\n #if CC_RENDER_MODE == 4\n vec2 vertIdx = vec2(a_texCoord.x, a_texCoord.y);\n #else\n vec2 vertIdx = vec2(a_size_uv.w, a_rotation_uv.w);\n #endif\n vec4 velocity = vec4(a_dir_life.xyz, 0.);\n vec4 pos = vec4(a_position_starttime.xyz, 1.);\n vec3 size = a_size_uv.xyz;\n #if SIZE_OVER_TIME_MODULE_ENABLE\n if (u_size_mode == 1) {\n size *= unpackCurveData(size_over_time_tex0, timeCoord0);\n } else {\n vec3 size_0 = unpackCurveData(size_over_time_tex0, timeCoord0);\n vec3 size_1 = unpackCurveData(size_over_time_tex0, timeCoord1);\n float factor_s = pseudoRandom(a_rndSeed + 39825.);\n size *= mix(size_0, size_1, factor_s);\n }\n #endif\n vec3 compScale = scale.xyz * size;\n #if FORCE_OVER_TIME_MODULE_ENABLE\n vec3 forceAnim = vec3(0.);\n if (u_force_mode == 1) {\n forceAnim = unpackCurveData(force_over_time_tex0, timeCoord0);\n } else {\n vec3 force_0 = unpackCurveData(force_over_time_tex0, timeCoord0);\n vec3 force_1 = unpackCurveData(force_over_time_tex0, timeCoord1);\n float factor_f = pseudoRandom(a_rndSeed + 212165.);\n forceAnim = mix(force_0, force_1, factor_f);\n }\n vec4 forceTrack = vec4(forceAnim, 0.);\n if (u_force_space == 0) {\n forceTrack = rotateQuat(forceTrack, u_worldRot);\n }\n velocity.xyz += forceTrack.xyz * normalizedTime * a_dir_life.w;\n #endif\n #if VELOCITY_OVER_TIME_MODULE_ENABLE\n float speedModifier0 = 1.;\n float speedModifier1 = 1.;\n vec3 velocityAnim = vec3(0.);\n if (u_velocity_mode == 1) {\n velocityAnim = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n } else {\n vec3 vectory_0 = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n vec3 vectory_1 = unpackCurveData(velocity_over_time_tex0, timeCoord1, speedModifier1);\n float factor_v = pseudoRandom(a_rndSeed + 197866.);\n velocityAnim = mix(vectory_0, vectory_1, factor_v);\n speedModifier0 = mix(speedModifier0, speedModifier1, factor_v);\n }\n vec4 velocityTrack = vec4(velocityAnim, 0.);\n if (u_velocity_space == 0) {\n velocityTrack = rotateQuat(velocityTrack, u_worldRot);\n }\n velocity.xyz += velocityTrack.xyz;\n velocity.xyz *= speedModifier0;\n #endif\n pos.xyz += velocity.xyz * normalizedTime * a_dir_life.w;\n #if !CC_USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if CC_RENDER_MODE == 1\n velocity = rotateQuat(velocity, u_worldRot);\n #endif\n #endif\n vec3 rotation = a_rotation_uv.xyz;\n #if ROTATION_OVER_TIME_MODULE_ENABLE\n if (u_rotation_mode == 1) {\n rotation += unpackCurveData(rotation_over_time_tex0, timeCoord0) * normalizedTime * a_dir_life.w;\n } else {\n vec3 rotation_0 = unpackCurveData(rotation_over_time_tex0, timeCoord0);\n vec3 rotation_1 = unpackCurveData(rotation_over_time_tex0, timeCoord1);\n float factor_r = pseudoRandom(a_rndSeed + 125292.);\n rotation += mix(rotation_0, rotation_1, factor_r);\n }\n #endif\n #if COLOR_OVER_TIME_MODULE_ENABLE\n if (u_color_mode == 1) {\n color = a_color * texture2D(color_over_time_tex0, timeCoord0);\n } else {\n vec4 color_0 = texture2D(color_over_time_tex0, timeCoord0);\n vec4 color_1 = texture2D(color_over_time_tex0, timeCoord1);\n float factor_c = pseudoRandom(a_rndSeed + 91041.);\n color = a_color * mix(color_0, color_1, factor_c);\n }\n #else\n color = a_color;\n #endif\n #if CC_RENDER_MODE != 4\n vec2 cornerOffset = vec2((vertIdx - 0.5));\n #if CC_RENDER_MODE == 0\n vec3 rotEuler = rotation.xyz;\n #elif CC_RENDER_MODE == 1\n vec3 rotEuler = vec3(0.);\n #else\n vec3 rotEuler = vec3(0., 0., rotation.z);\n #endif\n computeVertPos(pos, cornerOffset, quaternionFromEuler(rotEuler), compScale\n #if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , cc_matViewInv\n #endif\n #if CC_RENDER_MODE == 1\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_size_uv.w\n #endif\n );\n #else\n mat4 xformNoScale = matrixFromRT(quaternionFromEuler(rotation), pos.xyz);\n mat4 xform = matFromRTS(quaternionFromEuler(rotation), pos.xyz, compScale);\n pos = xform * vec4(a_texCoord3, 1);\n vec4 normal = xformNoScale * vec4(a_normal, 0);\n color *= a_color1;\n #endif\n pos = cc_matViewProj * pos;\n float frameIndex = 0.;\n #if TEXTURE_ANIMATION_MODULE_ENABLE\n float startFrame = 0.;\n vec3 frameInfo = vec3(0.);\n if (int(u_anim_info.x) == 1) {\n frameInfo = unpackCurveData(texture_animation_tex0, timeCoord0);\n } else {\n vec3 frameInfo0 = unpackCurveData(texture_animation_tex0, timeCoord0);\n vec3 frameInfo1 = unpackCurveData(texture_animation_tex0, timeCoord1);\n float factor_t = pseudoRandom(a_rndSeed + 90794.);\n frameInfo = mix(frameInfo0, frameInfo1, factor_t);\n }\n startFrame = frameInfo.x / u_anim_info.y;\n frameIndex = repeat(u_anim_info.z * (frameInfo.y + startFrame), 1.);\n #endif\n uv = computeUV(frameIndex, vertIdx, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n return pos;\n}\nvoid main() { gl_Position = gpvs_main(); }`, + "frag": `\nprecision mediump float;\nuniform mediump vec4 cc_exposure;\nvec3 SRGBToLinear (vec3 gamma) {\n return gamma * gamma;\n}\nvec4 CCFragOutput (vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nvarying vec2 uv;\nvarying vec4 color;\nuniform sampler2D mainTexture;\nvec4 premultiplied () {\n vec4 col = color * texture2D(mainTexture, uv) * color.a;\n return CCFragOutput(col);\n}\nvoid main() { gl_FragColor = premultiplied(); }` + }, + "glsl4": { + "vert": `\nprecision mediump float;\nvec4 quaternionFromAxis (vec3 xAxis,vec3 yAxis,vec3 zAxis){\n mat3 m = mat3(xAxis,yAxis,zAxis);\n float trace = m[0][0] + m[1][1] + m[2][2];\n vec4 quat;\n if (trace > 0.) {\n float s = 0.5 / sqrt(trace + 1.0);\n quat.w = 0.25 / s;\n quat.x = (m[2][1] - m[1][2]) * s;\n quat.y = (m[0][2] - m[2][0]) * s;\n quat.z = (m[1][0] - m[0][1]) * s;\n } else if ((m[0][0] > m[1][1]) && (m[0][0] > m[2][2])) {\n float s = 2.0 * sqrt(1.0 + m[0][0] - m[1][1] - m[2][2]);\n quat.w = (m[2][1] - m[1][2]) / s;\n quat.x = 0.25 * s;\n quat.y = (m[0][1] + m[1][0]) / s;\n quat.z = (m[0][2] + m[2][0]) / s;\n } else if (m[1][1] > m[2][2]) {\n float s = 2.0 * sqrt(1.0 + m[1][1] - m[0][0] - m[2][2]);\n quat.w = (m[0][2] - m[2][0]) / s;\n quat.x = (m[0][1] + m[1][0]) / s;\n quat.y = 0.25 * s;\n quat.z = (m[1][2] + m[2][1]) / s;\n } else {\n float s = 2.0 * sqrt(1.0 + m[2][2] - m[0][0] - m[1][1]);\n quat.w = (m[1][0] - m[0][1]) / s;\n quat.x = (m[0][2] + m[2][0]) / s;\n quat.y = (m[1][2] + m[2][1]) / s;\n quat.z = 0.25 * s;\n }\n float len = quat.x * quat.x + quat.y * quat.y + quat.z * quat.z + quat.w * quat.w;\n if (len > 0.) {\n len = 1. / sqrt(len);\n quat.x = quat.x * len;\n quat.y = quat.y * len;\n quat.z = quat.z * len;\n quat.w = quat.w * len;\n }\n return quat;\n}\nvec4 quaternionFromEuler (vec3 angle){\n float x = angle.x / 2.;\n float y = angle.y / 2.;\n float z = angle.z / 2.;\n float sx = sin(x);\n float cx = cos(x);\n float sy = sin(y);\n float cy = cos(y);\n float sz = sin(z);\n float cz = cos(z);\n vec4 quat = vec4(0);\n quat.x = sx * cy * cz + cx * sy * sz;\n quat.y = cx * sy * cz + sx * cy * sz;\n quat.z = cx * cy * sz - sx * sy * cz;\n quat.w = cx * cy * cz - sx * sy * sz;\n return quat;\n}\nmat4 matrixFromRT (vec4 q, vec3 p){\n float x2 = q.x + q.x;\n float y2 = q.y + q.y;\n float z2 = q.z + q.z;\n float xx = q.x * x2;\n float xy = q.x * y2;\n float xz = q.x * z2;\n float yy = q.y * y2;\n float yz = q.y * z2;\n float zz = q.z * z2;\n float wx = q.w * x2;\n float wy = q.w * y2;\n float wz = q.w * z2;\n return mat4(\n 1. - (yy + zz), xy + wz, xz - wy, 0,\n xy - wz, 1. - (xx + zz), yz + wx, 0,\n xz + wy, yz - wx, 1. - (xx + yy), 0,\n p.x, p.y, p.z, 1\n );\n}\nmat4 matFromRTS (vec4 q, vec3 t, vec3 s){\n float x = q.x, y = q.y, z = q.z, w = q.w;\n float x2 = x + x;\n float y2 = y + y;\n float z2 = z + z;\n float xx = x * x2;\n float xy = x * y2;\n float xz = x * z2;\n float yy = y * y2;\n float yz = y * z2;\n float zz = z * z2;\n float wx = w * x2;\n float wy = w * y2;\n float wz = w * z2;\n float sx = s.x;\n float sy = s.y;\n float sz = s.z;\n return mat4((1. - (yy + zz)) * sx, (xy + wz) * sx, (xz - wy) * sx, 0,\n (xy - wz) * sy, (1. - (xx + zz)) * sy, (yz + wx) * sy, 0,\n (xz + wy) * sz, (yz - wx) * sz, (1. - (xx + yy)) * sz, 0,\n t.x, t.y, t.z, 1);\n}\nvec4 quatMultiply (vec4 a, vec4 b){\n vec4 quat;\n quat.x = a.x * b.w + a.w * b.x + a.y * b.z - a.z * b.y;\n quat.y = a.y * b.w + a.w * b.y + a.z * b.x - a.x * b.z;\n quat.z = a.z * b.w + a.w * b.z + a.x * b.y - a.y * b.x;\n quat.w = a.w * b.w - a.x * b.x - a.y * b.y - a.z * b.z;\n return quat;\n}\nvoid rotateVecFromQuat (inout vec3 v, vec4 q){\n float ix = q.w * v.x + q.y * v.z - q.z * v.y;\n float iy = q.w * v.y + q.z * v.x - q.x * v.z;\n float iz = q.w * v.z + q.x * v.y - q.y * v.x;\n float iw = -q.x * v.x - q.y * v.y - q.z * v.z;\n v.x = ix * q.w + iw * -q.x + iy * -q.z - iz * -q.y;\n v.y = iy * q.w + iw * -q.y + iz * -q.x - ix * -q.z;\n v.z = iz * q.w + iw * -q.z + ix * -q.y - iy * -q.x;\n}\nvec3 rotateInLocalSpace (vec3 pos, vec3 xAxis, vec3 yAxis, vec3 zAxis, vec4 q){\n vec4 viewQuat = quaternionFromAxis(xAxis, yAxis, zAxis);\n vec4 rotQuat = quatMultiply(viewQuat, q);\n rotateVecFromQuat(pos, rotQuat);\n return pos;\n}\nvoid rotateCorner (inout vec2 corner, float angle){\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nlayout(binding = 0) uniform Constants {\n vec4 mainTiling_Offset;\n vec4 frameTile_velLenScale;\n vec4 scale;\n};\nlayout(binding = 23) uniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nlayout(binding = 21) uniform CCLocal {\n highp mat4 cc_matWorld;\n highp mat4 cc_matWorldIT;\n};\nlayout(location = 0) out vec2 uv;\nlayout(location = 1) out vec4 color;\nvoid computeVertPos (inout vec4 pos, vec2 vertOffset, vec4 q, vec3 s\n#if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , mat4 viewInv\n#endif\n#if CC_RENDER_MODE == 1\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float xIndex\n#endif\n) {\n#if CC_RENDER_MODE == 0\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = normalize(vec3(viewInv[0][0], viewInv[1][0], viewInv[2][0]));\n vec3 camY = normalize(vec3(viewInv[0][1], viewInv[1][1], viewInv[2][1]));\n vec3 camZ = normalize(vec3(viewInv[0][2], viewInv[1][2], viewInv[2][2]));\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, camZ, q);\n#elif CC_RENDER_MODE == 1\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz)) * s.x;\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * s.y;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif CC_RENDER_MODE == 2\n vec3 viewSpaceVert = vec3(vertOffset.x * s.x, vertOffset.y * s.y, 0.);\n vec3 camX = vec3(1, 0, 0);\n vec3 camY = vec3(0, 0, -1);\n pos.xyz += rotateInLocalSpace(viewSpaceVert, camX, camY, cross(camX, camY), q);\n#elif CC_RENDER_MODE == 3\n vec2 viewSpaceVert = vec2(vertOffset.x * s.x, vertOffset.y * s.y);\n rotateCorner(viewSpaceVert, q.z);\n vec3 camX = normalize(vec3(cc_matView[0][0], cc_matView[1][0], cc_matView[2][0]));\n vec3 camY = vec3(0, 1, 0);\n vec3 offset = camX * viewSpaceVert.x + camY * viewSpaceVert.y;\n pos.xyz += offset;\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV (float frameIndex, vec2 vertIndex, vec2 frameTile){\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n#if CC_RENDER_MODE != 4\n vertIndex.y = 1. - vertIndex.y;\n#endif\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nlayout(binding = 1) uniform SampleConstants {\n float sampleInterval;\n float sampleNum;\n};\nlayout(binding = 2) uniform TickConstants {\n vec4 u_worldRot;\n vec4 u_timeDelta;\n};\nlayout(location = 0) in vec4 a_position_starttime;\nlayout(location = 1) in vec4 a_size_uv;\nlayout(location = 2) in vec4 a_rotation_uv;\nlayout(location = 3) in vec4 a_color;\nlayout(location = 4) in vec4 a_dir_life;\nlayout(location = 5) in float a_rndSeed;\n#if CC_RENDER_MODE == 4\n layout(location = 6) in vec3 a_texCoord;\n layout(location = 7) in vec3 a_texCoord3;\n layout(location = 8) in vec3 a_normal;\n layout(location = 9) in vec4 a_color1;\n#endif\nvec3 unpackCurveData (sampler2D tex, vec2 coord) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n return mix(a.xyz, b.xyz, c);\n}\nvec3 unpackCurveData (sampler2D tex, vec2 coord, layout(location = 2) out float w) {\n vec4 a = texture(tex, coord);\n vec4 b = texture(tex, coord + sampleInterval);\n float c = fract(coord.x * sampleNum);\n w = mix(a.w, b.w, c);\n return mix(a.xyz, b.xyz, c);\n}\nfloat pseudoRandom (float seed) {\n seed = mod(seed, 233280.);\n float q = (seed * 9301. + 49297.) / 233280.;\n return fract(q);\n}\n#if COLOR_OVER_TIME_MODULE_ENABLE\n layout(binding = 30) uniform sampler2D color_over_time_tex0;\n layout(binding = 3) uniform ColorConstant {\n int u_color_mode;\n };\n#endif\n#if ROTATION_OVER_TIME_MODULE_ENABLE\n layout(binding = 31) uniform sampler2D rotation_over_time_tex0;\n layout(binding = 4) uniform RotationConstant {\n int u_rotation_mode;\n };\n#endif\n#if SIZE_OVER_TIME_MODULE_ENABLE\n layout(binding = 32) uniform sampler2D size_over_time_tex0;\n layout(binding = 5) uniform SizeConstant {\n int u_size_mode;\n };\n#endif\n#if FORCE_OVER_TIME_MODULE_ENABLE\n layout(binding = 33) uniform sampler2D force_over_time_tex0;\n layout(binding = 6) uniform ForceConstant {\n int u_force_mode;\n int u_force_space;\n };\n#endif\n#if VELOCITY_OVER_TIME_MODULE_ENABLE\n layout(binding = 34) uniform sampler2D velocity_over_time_tex0;\n layout(binding = 7) uniform VelocityConstant {\n int u_velocity_mode;\n int u_velocity_space;\n };\n#endif\n#if TEXTURE_ANIMATION_MODULE_ENABLE\n layout(binding = 35) uniform sampler2D texture_animation_tex0;\n layout(binding = 8) uniform AnimationConstant {\n vec4 u_anim_info;\n };\n#endif\nfloat repeat (float t, float length) {\n return t - floor(t / length) * length;\n}\nvec4 rotateQuat (vec4 p, vec4 q) {\n vec3 iv = cross(q.xyz, p.xyz) + q.w * p.xyz;\n vec3 res = p.xyz + 2.0 * cross(q.xyz, iv);\n return vec4(res.xyz, p.w);\n}\nvec4 gpvs_main () {\n float activeTime = u_timeDelta.x - a_position_starttime.w;\n float normalizedTime = clamp(activeTime / a_dir_life.w, 0.0, 1.0);\n vec2 timeCoord0 = vec2(normalizedTime, 0.);\n vec2 timeCoord1 = vec2(normalizedTime, 1.);\n #if CC_RENDER_MODE == 4\n vec2 vertIdx = vec2(a_texCoord.x, a_texCoord.y);\n #else\n vec2 vertIdx = vec2(a_size_uv.w, a_rotation_uv.w);\n #endif\n vec4 velocity = vec4(a_dir_life.xyz, 0.);\n vec4 pos = vec4(a_position_starttime.xyz, 1.);\n vec3 size = a_size_uv.xyz;\n #if SIZE_OVER_TIME_MODULE_ENABLE\n if (u_size_mode == 1) {\n size *= unpackCurveData(size_over_time_tex0, timeCoord0);\n } else {\n vec3 size_0 = unpackCurveData(size_over_time_tex0, timeCoord0);\n vec3 size_1 = unpackCurveData(size_over_time_tex0, timeCoord1);\n float factor_s = pseudoRandom(a_rndSeed + 39825.);\n size *= mix(size_0, size_1, factor_s);\n }\n #endif\n vec3 compScale = scale.xyz * size;\n #if FORCE_OVER_TIME_MODULE_ENABLE\n vec3 forceAnim = vec3(0.);\n if (u_force_mode == 1) {\n forceAnim = unpackCurveData(force_over_time_tex0, timeCoord0);\n } else {\n vec3 force_0 = unpackCurveData(force_over_time_tex0, timeCoord0);\n vec3 force_1 = unpackCurveData(force_over_time_tex0, timeCoord1);\n float factor_f = pseudoRandom(a_rndSeed + 212165.);\n forceAnim = mix(force_0, force_1, factor_f);\n }\n vec4 forceTrack = vec4(forceAnim, 0.);\n if (u_force_space == 0) {\n forceTrack = rotateQuat(forceTrack, u_worldRot);\n }\n velocity.xyz += forceTrack.xyz * normalizedTime * a_dir_life.w;\n #endif\n #if VELOCITY_OVER_TIME_MODULE_ENABLE\n float speedModifier0 = 1.;\n float speedModifier1 = 1.;\n vec3 velocityAnim = vec3(0.);\n if (u_velocity_mode == 1) {\n velocityAnim = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n } else {\n vec3 vectory_0 = unpackCurveData(velocity_over_time_tex0, timeCoord0, speedModifier0);\n vec3 vectory_1 = unpackCurveData(velocity_over_time_tex0, timeCoord1, speedModifier1);\n float factor_v = pseudoRandom(a_rndSeed + 197866.);\n velocityAnim = mix(vectory_0, vectory_1, factor_v);\n speedModifier0 = mix(speedModifier0, speedModifier1, factor_v);\n }\n vec4 velocityTrack = vec4(velocityAnim, 0.);\n if (u_velocity_space == 0) {\n velocityTrack = rotateQuat(velocityTrack, u_worldRot);\n }\n velocity.xyz += velocityTrack.xyz;\n velocity.xyz *= speedModifier0;\n #endif\n pos.xyz += velocity.xyz * normalizedTime * a_dir_life.w;\n #if !CC_USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if CC_RENDER_MODE == 1\n velocity = rotateQuat(velocity, u_worldRot);\n #endif\n #endif\n vec3 rotation = a_rotation_uv.xyz;\n #if ROTATION_OVER_TIME_MODULE_ENABLE\n if (u_rotation_mode == 1) {\n rotation += unpackCurveData(rotation_over_time_tex0, timeCoord0) * normalizedTime * a_dir_life.w;\n } else {\n vec3 rotation_0 = unpackCurveData(rotation_over_time_tex0, timeCoord0);\n vec3 rotation_1 = unpackCurveData(rotation_over_time_tex0, timeCoord1);\n float factor_r = pseudoRandom(a_rndSeed + 125292.);\n rotation += mix(rotation_0, rotation_1, factor_r);\n }\n #endif\n #if COLOR_OVER_TIME_MODULE_ENABLE\n if (u_color_mode == 1) {\n color = a_color * texture(color_over_time_tex0, timeCoord0);\n } else {\n vec4 color_0 = texture(color_over_time_tex0, timeCoord0);\n vec4 color_1 = texture(color_over_time_tex0, timeCoord1);\n float factor_c = pseudoRandom(a_rndSeed + 91041.);\n color = a_color * mix(color_0, color_1, factor_c);\n }\n #else\n color = a_color;\n #endif\n #if CC_RENDER_MODE != 4\n vec2 cornerOffset = vec2((vertIdx - 0.5));\n #if CC_RENDER_MODE == 0\n vec3 rotEuler = rotation.xyz;\n #elif CC_RENDER_MODE == 1\n vec3 rotEuler = vec3(0.);\n #else\n vec3 rotEuler = vec3(0., 0., rotation.z);\n #endif\n computeVertPos(pos, cornerOffset, quaternionFromEuler(rotEuler), compScale\n #if CC_RENDER_MODE == 0 || CC_RENDER_MODE == 3\n , cc_matViewInv\n #endif\n #if CC_RENDER_MODE == 1\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_size_uv.w\n #endif\n );\n #else\n mat4 xformNoScale = matrixFromRT(quaternionFromEuler(rotation), pos.xyz);\n mat4 xform = matFromRTS(quaternionFromEuler(rotation), pos.xyz, compScale);\n pos = xform * vec4(a_texCoord3, 1);\n vec4 normal = xformNoScale * vec4(a_normal, 0);\n color *= a_color1;\n #endif\n pos = cc_matViewProj * pos;\n float frameIndex = 0.;\n #if TEXTURE_ANIMATION_MODULE_ENABLE\n float startFrame = 0.;\n vec3 frameInfo = vec3(0.);\n if (int(u_anim_info.x) == 1) {\n frameInfo = unpackCurveData(texture_animation_tex0, timeCoord0);\n } else {\n vec3 frameInfo0 = unpackCurveData(texture_animation_tex0, timeCoord0);\n vec3 frameInfo1 = unpackCurveData(texture_animation_tex0, timeCoord1);\n float factor_t = pseudoRandom(a_rndSeed + 90794.);\n frameInfo = mix(frameInfo0, frameInfo1, factor_t);\n }\n startFrame = frameInfo.x / u_anim_info.y;\n frameIndex = repeat(u_anim_info.z * (frameInfo.y + startFrame), 1.);\n #endif\n uv = computeUV(frameIndex, vertIdx, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n return pos;\n}\nvoid main() { gl_Position = gpvs_main(); }`, + "frag": `\nprecision mediump float;\nlayout(binding = 23) uniform CCGlobal {\n highp vec4 cc_time;\n mediump vec4 cc_screenSize;\n mediump vec4 cc_screenScale;\n mediump vec4 cc_nativeSize;\n highp mat4 cc_matView;\n highp mat4 cc_matViewInv;\n highp mat4 cc_matProj;\n highp mat4 cc_matProjInv;\n highp mat4 cc_matViewProj;\n highp mat4 cc_matViewProjInv;\n highp vec4 cc_cameraPos;\n mediump vec4 cc_exposure;\n mediump vec4 cc_mainLitDir;\n mediump vec4 cc_mainLitColor;\n mediump vec4 cc_ambientSky;\n mediump vec4 cc_ambientGround;\n};\nvec3 SRGBToLinear (vec3 gamma) {\n return gamma * gamma;\n}\nvec4 CCFragOutput (vec4 color) {\n #if CC_USE_HDR\n color.rgb = mix(color.rgb, SRGBToLinear(color.rgb) * cc_exposure.w, vec3(cc_exposure.z));\n #endif\n return color;\n}\nlayout(location = 0) in vec2 uv;\nlayout(location = 1) in vec4 color;\nlayout(binding = 36) uniform sampler2D mainTexture;\nvec4 premultiplied () {\n vec4 col = color * texture(mainTexture, uv) * color.a;\n return CCFragOutput(col);\n}\nlayout(location = 0) out vec4 cc_FragColor;\nvoid main() { cc_FragColor = premultiplied(); }` + }, + "varyings": [ + { "name": "uv", "type": 14, "count": 1, "defines": [], "location": 0 }, + { "name": "color", "type": 16, "count": 1, "defines": [], "location": 1 }, + { "name": "w", "type": 13, "count": 1, "defines": [], "location": 2 } + ], + "builtins": { + "globals": { "blocks": [{ "name": "CCGlobal", "defines": [] }], "samplers": [] }, + "locals": { "blocks": [{ "name": "CCLocal", "defines": [] }], "samplers": [] } + }, + "defines": [ + { "name": "CC_RENDER_MODE", "type": "number", "defines": [], "range": [0, 4] }, + { "name": "COLOR_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "ROTATION_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "SIZE_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "FORCE_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "VELOCITY_OVER_TIME_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "TEXTURE_ANIMATION_MODULE_ENABLE", "type": "boolean", "defines": [] }, + { "name": "CC_USE_WORLD_SPACE", "type": "boolean", "defines": [] }, + { "name": "CC_USE_HDR", "type": "boolean", "defines": [] } + ], + "blocks": [ + {"name": "Constants", "defines": [], "binding": 0, "members": [ + { "name": "mainTiling_Offset", "type": 16, "count": 1 }, + { "name": "frameTile_velLenScale", "type": 16, "count": 1 }, + { "name": "scale", "type": 16, "count": 1 } + ]}, + {"name": "SampleConstants", "defines": [], "binding": 1, "members": [ + { "name": "sampleInterval", "type": 13, "count": 1 }, + { "name": "sampleNum", "type": 13, "count": 1 } + ]}, + {"name": "TickConstants", "defines": [], "binding": 2, "members": [ + { "name": "u_worldRot", "type": 16, "count": 1 }, + { "name": "u_timeDelta", "type": 16, "count": 1 } + ]}, + {"name": "ColorConstant", "defines": ["COLOR_OVER_TIME_MODULE_ENABLE"], "binding": 3, "members": [ + { "name": "u_color_mode", "type": 5, "count": 1 } + ]}, + {"name": "RotationConstant", "defines": ["ROTATION_OVER_TIME_MODULE_ENABLE"], "binding": 4, "members": [ + { "name": "u_rotation_mode", "type": 5, "count": 1 } + ]}, + {"name": "SizeConstant", "defines": ["SIZE_OVER_TIME_MODULE_ENABLE"], "binding": 5, "members": [ + { "name": "u_size_mode", "type": 5, "count": 1 } + ]}, + {"name": "ForceConstant", "defines": ["FORCE_OVER_TIME_MODULE_ENABLE"], "binding": 6, "members": [ + { "name": "u_force_mode", "type": 5, "count": 1 }, + { "name": "u_force_space", "type": 5, "count": 1 } + ]}, + {"name": "VelocityConstant", "defines": ["VELOCITY_OVER_TIME_MODULE_ENABLE"], "binding": 7, "members": [ + { "name": "u_velocity_mode", "type": 5, "count": 1 }, + { "name": "u_velocity_space", "type": 5, "count": 1 } + ]}, + {"name": "AnimationConstant", "defines": ["TEXTURE_ANIMATION_MODULE_ENABLE"], "binding": 8, "members": [ + { "name": "u_anim_info", "type": 16, "count": 1 } + ]} + ], + "samplers": [ + { "name": "color_over_time_tex0", "type": 28, "count": 1, "defines": ["COLOR_OVER_TIME_MODULE_ENABLE"], "binding": 30 }, + { "name": "rotation_over_time_tex0", "type": 28, "count": 1, "defines": ["ROTATION_OVER_TIME_MODULE_ENABLE"], "binding": 31 }, + { "name": "size_over_time_tex0", "type": 28, "count": 1, "defines": ["SIZE_OVER_TIME_MODULE_ENABLE"], "binding": 32 }, + { "name": "force_over_time_tex0", "type": 28, "count": 1, "defines": ["FORCE_OVER_TIME_MODULE_ENABLE"], "binding": 33 }, + { "name": "velocity_over_time_tex0", "type": 28, "count": 1, "defines": ["VELOCITY_OVER_TIME_MODULE_ENABLE"], "binding": 34 }, + { "name": "texture_animation_tex0", "type": 28, "count": 1, "defines": ["TEXTURE_ANIMATION_MODULE_ENABLE"], "binding": 35 }, + { "name": "mainTexture", "type": 28, "count": 1, "defines": [], "binding": 36 } + ], + "attributes": [ + { "name": "a_position_starttime", "type": 16, "count": 1, "defines": [], "format": 43, "location": 0 }, + { "name": "a_size_uv", "type": 16, "count": 1, "defines": [], "format": 43, "location": 1 }, + { "name": "a_rotation_uv", "type": 16, "count": 1, "defines": [], "format": 43, "location": 2 }, + { "name": "a_color", "type": 16, "count": 1, "defines": [], "format": 43, "location": 3 }, + { "name": "a_dir_life", "type": 16, "count": 1, "defines": [], "format": 43, "location": 4 }, + { "name": "a_rndSeed", "type": 13, "count": 1, "defines": [], "format": 11, "location": 5 }, + { "name": "a_texCoord", "type": 15, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 32, "location": 6 }, + { "name": "a_texCoord3", "type": 15, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 32, "location": 7 }, + { "name": "a_normal", "type": 15, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 32, "location": 8 }, + { "name": "a_color1", "type": 16, "count": 1, "defines": ["CC_RENDER_MODE"], "format": 43, "location": 9 } + ] + } + ] + }, { "name": "builtin-particle-trail", "_uuid": "17debcc3-0a6b-4b8a-b00b-dc58b885581e", diff --git a/cocos/core/3d/builtin/init.ts b/cocos/core/3d/builtin/init.ts index bb008cb961..d5c59b932b 100644 --- a/cocos/core/3d/builtin/init.ts +++ b/cocos/core/3d/builtin/init.ts @@ -174,6 +174,12 @@ class BuiltinResMgr { defaultParticleMtl.initialize({ effectName: 'builtin-particle' }); resources[defaultParticleMtl._uuid] = defaultParticleMtl; + // default particle gpu material + const defaultParticleGPUMtl = new cc.Material(); + defaultParticleGPUMtl._uuid = 'default-particle-gpu-material'; + defaultParticleGPUMtl.initialize({ effectName: 'builtin-particle-gpu' }); + resources[defaultParticleGPUMtl._uuid] = defaultParticleGPUMtl; + // default particle material const defaultTrailMtl = new cc.Material(); defaultTrailMtl._uuid = 'default-trail-material'; diff --git a/cocos/core/renderer/core/pass-utils.ts b/cocos/core/renderer/core/pass-utils.ts index e18142653e..d329d50f0b 100644 --- a/cocos/core/renderer/core/pass-utils.ts +++ b/cocos/core/renderer/core/pass-utils.ts @@ -28,7 +28,7 @@ */ import { GFXBindingType, GFXType } from '../../gfx/define'; -import { Color, Mat3, Mat4, Vec2, Vec3, Vec4 } from '../../math'; +import { Color, Mat3, Mat4, Vec2, Vec3, Vec4, Quat } from '../../math'; const btMask = 0xf0000000; // 4 bits => 16 binding types const typeMask = 0x0fc00000; // 6 bits => 64 types @@ -43,7 +43,7 @@ export const getBindingFromHandle = (handle: number) => (handle & bindingMask) > export const getOffsetFromHandle = (handle: number) => (handle & offsetMask); export const customizeType = (handle: number, type: GFXType) => (handle & ~typeMask) | ((type << 22) & typeMask); -export type MaterialProperty = number | Vec2 | Vec3 | Vec4 | Color | Mat3 | Mat4; +export type MaterialProperty = number | Vec2 | Vec3 | Vec4 | Color | Mat3 | Mat4 | Quat; export const type2reader = { [GFXType.UNKNOWN]: (a: Float32Array, v: any, idx: number = 0) => console.warn('illegal uniform handle'), diff --git a/cocos/particle/animator/color-overtime.ts b/cocos/particle/animator/color-overtime.ts index e33facdb7b..121c54958d 100644 --- a/cocos/particle/animator/color-overtime.ts +++ b/cocos/particle/animator/color-overtime.ts @@ -5,23 +5,35 @@ import { ccclass, property } from '../../core/data/class-decorator'; import { pseudoRandom } from '../../core/math'; -import Particle from '../particle'; +import { Particle, PARTICLE_MODULE_NAME } from '../particle'; import GradientRange from './gradient-range'; +import { ModuleRandSeed } from '../enum'; +import { ParticleModuleBase } from '../particle'; // tslint:disable: max-line-length -const COLOR_OVERTIME_RAND_OFFSET = 91041; +const COLOR_OVERTIME_RAND_OFFSET = ModuleRandSeed.COLOR; @ccclass('cc.ColorOvertimeModule') -export default class ColorOvertimeModule { - +export default class ColorOvertimeModule extends ParticleModuleBase { + @property + _enable = false; /** * @zh 是否启用。 */ @property({ displayOrder: 0, }) - public enable = false; + public get enable () { + return this._enable; + } + + public set enable (val) { + if (this._enable === val) return; + this._enable = val; + if (!this.target) return; + this.target.enableModule(this.name, val, this); + } /** * @zh 颜色随时间变化的参数,各个 key 之间线性差值变化。 @@ -31,12 +43,11 @@ export default class ColorOvertimeModule { displayOrder: 1, }) public color = new GradientRange(); + public name = PARTICLE_MODULE_NAME.COLOR; public animate (particle: Particle) { - if (this.enable) { - particle.color.set(particle.startColor); - particle.color.multiply(this.color.evaluate(1.0 - particle.remainingLifetime / particle.startLifetime, pseudoRandom(particle.randomSeed + COLOR_OVERTIME_RAND_OFFSET))); - } + particle.color.set(particle.startColor); + particle.color.multiply(this.color.evaluate(1.0 - particle.remainingLifetime / particle.startLifetime, pseudoRandom(particle.randomSeed + COLOR_OVERTIME_RAND_OFFSET))); } } diff --git a/cocos/particle/animator/curve-range.ts b/cocos/particle/animator/curve-range.ts index ef4e7090a2..9d8f67dcbc 100644 --- a/cocos/particle/animator/curve-range.ts +++ b/cocos/particle/animator/curve-range.ts @@ -6,13 +6,15 @@ import { ccclass, property } from '../../core/data/class-decorator'; import { lerp } from '../../core/math'; import { Enum } from '../../core/value-types'; import { AnimationCurve } from '../../core/geometry'; +import { Texture2D } from '../../core'; +import { PixelFormat, Filter, WrapMode } from '../../core/assets/asset-enum'; import { EDITOR } from 'internal:constants'; const SerializableTable = EDITOR && [ - [ "mode", "constant", "multiplier" ], - [ "mode", "curve", "multiplier" ], - [ "mode", "curveMin", "curveMax", "multiplier" ], - [ "mode", "constantMin", "constantMax", "multiplier"] + [ 'mode', 'constant', 'multiplier' ], + [ 'mode', 'curve', 'multiplier' ], + [ 'mode', 'curveMin', 'curveMax', 'multiplier' ], + [ 'mode', 'constantMin', 'constantMax', 'multiplier'] ]; export const Mode = Enum({ @@ -119,13 +121,146 @@ export default class CurveRange { } } -// CCClass.fastDefine('cc.CurveRange', CurveRange, { -// mode: Mode.Constant, -// constant: 0, -// constantMin: 0, -// constantMax: 0, -// curve: new AnimationCurve(), -// curveMin: new AnimationCurve(), -// curveMax: new AnimationCurve(), -// multiplier: 1 -// }); +function evaluateCurve (cr: CurveRange, time: number, index: number) { + switch (cr.mode) { + case Mode.Constant: + return cr.constant; + case Mode.Curve: + return cr.curve.evaluate(time); + case Mode.TwoCurves: + return index === 0 ? cr.curveMin.evaluate(time) : cr.curveMax.evaluate(time); + case Mode.TwoConstants: + return index === 0 ? cr.constantMin : cr.constantMax; + default: + return 0; + } +} + +function evaluateHeight (cr: CurveRange) { + switch (cr.mode) { + case Mode.TwoConstants: + return 2; + case Mode.TwoCurves: + return 2; + default: + return 1; + } +} + +function packTexture (data, width, height) { + const texture = new Texture2D(); + texture.create(width, height, PixelFormat.RGBA32F); + texture.setFilters(Filter.NEAREST, Filter.NEAREST); + texture.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE); + texture.uploadData(data); + + return texture; +} + +export function packCurveRangeZ (samples:number, cr: CurveRange) { + const height = evaluateHeight(cr); + const data = new Float32Array(samples * height * 4); + const interval = 1.0 / (samples - 1); + let sum = 0; + let average = 0; + let offset = 0; + + for (let h = 0; h < height; h++) { + sum = 0; + for (let j = 0; j < samples; j++) { + const value = evaluateCurve(cr, interval * j, h); + sum += value; + average = sum / (j + 1); + data[offset + 2] = average; + offset += 4; + } + } + return packTexture(data, samples, height); +} +export function packCurveRangeN (samples:number, cr: CurveRange) { + const height = evaluateHeight(cr); + const data = new Float32Array(samples * height * 4); + const interval = 1.0 / (samples - 1); + let sum = 0; + let average = 0; + let offset = 0; + for (let h = 0; h < height; h++) { + sum = 0; + for (let j = 0; j < samples; j++) { + const value = evaluateCurve(cr, interval * j, h); + sum += value; + average = sum / (j + 1); + data[offset] = average; + data[offset + 1] = average; + data[offset + 2] = average; + offset += 4; + } + } + return packTexture(data, samples, height); +} + +export function packCurveRangeXY (samples: number, x: CurveRange, y: CurveRange) { + const height = Math.max(evaluateHeight(x), evaluateHeight(y)); + const data = new Float32Array(samples * height * 4); + const curves: CurveRange[] = [x, y]; + const interval = 1.0 / (samples - 1); + + for (let h = 0; h < height; h++) { + for (let i = 0; i < 2; i++) { + const cr = curves[i]; + let sum = 0; + let average = 0; + for (let j = 0; j < samples; j++) { + const value = evaluateCurve(cr, interval * j, h); + sum += value; + average = sum / (j + 1); + data[j * 4 + i] = average; + } + } + } + return packTexture(data, samples, height); +} + +export function packCurveRangeXYZ (samples: number, x: CurveRange, y: CurveRange, z: CurveRange) { + const height = Math.max(evaluateHeight(x), evaluateHeight(y), evaluateHeight(z)); + const data = new Float32Array(samples * height * 4); + const curves: CurveRange[] = [x, y, z]; + const interval = 1.0 / (samples - 1); + + for (let h = 0; h < height; h++) { + for (let i = 0; i < 3; i++) { + const cr = curves[i]; + let sum = 0; + let average = 0; + for (let j = 0; j < samples; j++) { + const value = evaluateCurve(cr, interval * j, h); + sum += value; + average = sum / (j + 1); + data[j * 4 + i] = average; + } + } + } + return packTexture(data, samples, height); +} + +export function packCurveRangeXYZW (samples: number, x: CurveRange, y: CurveRange, z: CurveRange, w: CurveRange) { + const height = Math.max(evaluateHeight(x), evaluateHeight(y), evaluateHeight(z), evaluateHeight(w)); + const data = new Float32Array(samples * height * 4); + const curves: CurveRange[] = [x, y, z, w]; + const interval = 1.0 / (samples - 1); + + for (let h = 0; h < height; h++) { + for (let i = 0; i < 4; i++) { + const cr = curves[i]; + let sum = 0; + let average = 0; + for (let j = 0; j < samples; j++) { + const value = evaluateCurve(cr, interval * j, h); + sum += value; + average = sum / (j + 1); + data[j * 4 + i] = average; + } + } + } + return packTexture(data, samples, height); +} diff --git a/cocos/particle/animator/force-overtime.ts b/cocos/particle/animator/force-overtime.ts index 0eab23fc9a..31fd2736c0 100644 --- a/cocos/particle/animator/force-overtime.ts +++ b/cocos/particle/animator/force-overtime.ts @@ -8,22 +8,34 @@ import { pseudoRandom, Quat, Vec3 } from '../../core/math'; import { Space } from '../enum'; import { calculateTransform } from '../particle-general-function'; import CurveRange from './curve-range'; +import { ModuleRandSeed } from '../enum'; +import { ParticleModuleBase, PARTICLE_MODULE_NAME} from '../particle'; // tslint:disable: max-line-length -const FORCE_OVERTIME_RAND_OFFSET = 212165; +const FORCE_OVERTIME_RAND_OFFSET = ModuleRandSeed.FORCE; const _temp_v3 = new Vec3(); @ccclass('cc.ForceOvertimeModule') -export default class ForceOvertimeModule { - +export default class ForceOvertimeModule extends ParticleModuleBase { + @property + _enable: Boolean = false; /** * @zh 是否启用。 */ @property({ displayOrder: 0, }) - public enable = false; + public get enable () { + return this._enable; + } + + public set enable (val) { + if (this._enable === val) return; + this._enable = val; + if (!this.target) return; + this.target.enableModule(this.name, val, this); + } /** * @zh X 轴方向上的加速度分量。 @@ -73,10 +85,13 @@ export default class ForceOvertimeModule { private rotation: Quat; private needTransform: boolean; + public name = PARTICLE_MODULE_NAME.FORCE; constructor () { + super(); this.rotation = new Quat(); this.needTransform = false; + this.needUpdate = true; } public update (space, worldTransform) { @@ -90,6 +105,7 @@ export default class ForceOvertimeModule { Vec3.transformQuat(force, force, this.rotation); } Vec3.scaleAndAdd(p.velocity, p.velocity, force, dt); + Vec3.copy(p.ultimateVelocity, p.velocity); } } diff --git a/cocos/particle/animator/gradient-range.ts b/cocos/particle/animator/gradient-range.ts index 94df14dff3..a42b219c27 100644 --- a/cocos/particle/animator/gradient-range.ts +++ b/cocos/particle/animator/gradient-range.ts @@ -8,24 +8,16 @@ import { Color } from '../../core/math'; import { Enum } from '../../core/value-types'; import Gradient, { AlphaKey, ColorKey } from './gradient'; import { EDITOR } from 'internal:constants'; +import { Texture2D } from '../../core'; +import { PixelFormat, Filter, WrapMode } from '../../core/assets/asset-enum'; // tslint:disable: max-line-length - -const GRADIENT_MODE_FIX = 0; -const GRADIENT_MODE_BLEND = 1; - -const GRADIENT_RANGE_MODE_COLOR = 0; -const GRADIENT_RANGE_MODE_TWO_COLOR = 1; -const GRADIENT_RANGE_MODE_RANDOM_COLOR = 2; -const GRADIENT_RANGE_MODE_GRADIENT = 3; -const GRADIENT_RANGE_MODE_TWO_GRADIENT = 4; - const SerializableTable = EDITOR && [ - [ "_mode", "color" ], - [ "_mode", "gradient" ], - [ "_mode", "colorMin", "colorMax" ], - [ "_mode", "gradientMin", "gradientMax"], - [ "_mode", "gradient" ] + [ '_mode', 'color' ], + [ '_mode', 'gradient' ], + [ '_mode', 'colorMin', 'colorMax' ], + [ '_mode', 'gradientMin', 'gradientMax'], + [ '_mode', 'gradient' ] ]; const Mode = Enum({ @@ -147,3 +139,54 @@ export default class GradientRange { // gradientMin: null, // gradientMax: null // }); +function evaluateGradient (gr: GradientRange, time: number, index: number) { + switch (gr.mode) { + case Mode.Color: + return gr.color; + case Mode.TwoColors: + return index === 0 ? gr.colorMin : gr.colorMax; + case Mode.RandomColor: + return gr.gradient.randomColor(); + case Mode.Gradient: + return gr.gradient.evaluate(time); + case Mode.TwoGradients: + return index === 0 ? gr.gradientMin.evaluate(time) : gr.gradientMax.evaluate(time); + default: + return gr.color; + } +} +function evaluateHeight (gr: GradientRange) { + switch (gr.mode) { + case Mode.TwoColors: + return 2; + case Mode.TwoGradients: + return 2; + default: + return 1; + } +} +export function packGradientRange (samples: number, gr: GradientRange) { + const height = evaluateHeight(gr); + const data = new Uint8Array(samples * height * 4); + const interval = 1.0 / (samples - 1); + let offset = 0; + + for (let h = 0; h < height; h++) { + for (let j = 0; j < samples; j++) { + const color = evaluateGradient(gr, interval * j, h); + data[offset] = color.r; + data[offset + 1] = color.g; + data[offset + 2] = color.b; + data[offset + 3] = color.a; + offset += 4; + } + } + + const texture = new Texture2D(); + texture.create(samples, height, PixelFormat.RGBA8888); + texture.setFilters(Filter.LINEAR, Filter.LINEAR); + texture.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE); + texture.uploadData(data); + + return texture; +} \ No newline at end of file diff --git a/cocos/particle/animator/limit-velocity-overtime.ts b/cocos/particle/animator/limit-velocity-overtime.ts index 0265861acd..18034dc43f 100644 --- a/cocos/particle/animator/limit-velocity-overtime.ts +++ b/cocos/particle/animator/limit-velocity-overtime.ts @@ -5,27 +5,38 @@ import { ccclass, property } from '../../core/data/class-decorator'; import { lerp, pseudoRandom, Vec3, Mat4, Quat } from '../../core/math'; -import { Space } from '../enum'; -import Particle from '../particle'; +import { Space, ModuleRandSeed } from '../enum'; +import { Particle, ParticleModuleBase, PARTICLE_MODULE_NAME } from '../particle'; import CurveRange from './curve-range'; import { calculateTransform } from '../particle-general-function'; // tslint:disable: max-line-length -const LIMIT_VELOCITY_RAND_OFFSET = 23541; +const LIMIT_VELOCITY_RAND_OFFSET = ModuleRandSeed.LIMIT; const _temp_v3 = new Vec3(); const _temp_v3_1 = new Vec3(); @ccclass('cc.LimitVelocityOvertimeModule') -export default class LimitVelocityOvertimeModule { +export default class LimitVelocityOvertimeModule extends ParticleModuleBase { + @property + _enable: Boolean = false; /** * @zh 是否启用。 */ @property({ displayOrder: 0, }) - public enable = false; + public get enable () { + return this._enable; + } + + public set enable (val) { + if (this._enable === val) return; + this._enable = val; + if (!this.target) return; + this.target.enableModule(this.name, val, this); + } /** * @zh X 轴方向上的速度下限。 @@ -101,24 +112,24 @@ export default class LimitVelocityOvertimeModule { // TODO:functions related to drag are temporarily not supported public drag = null; - public multiplyDragByParticleSize = false; - public multiplyDragByParticleVelocity = false; - + public name = PARTICLE_MODULE_NAME.LIMIT; private rotation: Quat; private needTransform: boolean; constructor () { + super(); this.rotation = new Quat(); this.needTransform = false; + this.needUpdate = true; } public update (space: number, worldTransform: Mat4) { this.needTransform = calculateTransform(space, this.space, worldTransform, this.rotation); } - public animate (p: Particle) { + public animate (p: Particle, dt: number) { const normalizedTime = 1 - p.remainingLifetime / p.startLifetime; const dampedVel = _temp_v3; if (this.separateAxes) { diff --git a/cocos/particle/animator/rotation-overtime.ts b/cocos/particle/animator/rotation-overtime.ts index 92f514f734..8b832c39bb 100644 --- a/cocos/particle/animator/rotation-overtime.ts +++ b/cocos/particle/animator/rotation-overtime.ts @@ -5,22 +5,33 @@ import { ccclass, property } from '../../core/data/class-decorator'; import { pseudoRandom } from '../../core/math'; -import Particle from '../particle'; +import { Particle, ParticleModuleBase, PARTICLE_MODULE_NAME } from '../particle'; import CurveRange from './curve-range'; +import { ModuleRandSeed } from '../enum'; // tslint:disable: max-line-length -const ROTATION_OVERTIME_RAND_OFFSET = 125292; +const ROTATION_OVERTIME_RAND_OFFSET = ModuleRandSeed.ROTATION; @ccclass('cc.RotationOvertimeModule') -export default class RotationOvertimeModule { - +export default class RotationOvertimeModule extends ParticleModuleBase { + @property + _enable: Boolean = false; /** * @zh 是否启用。 */ @property({ displayOrder: 0, }) - public enable = false; + public get enable () { + return this._enable; + } + + public set enable (val) { + if (this._enable === val) return; + this._enable = val; + if (!this.target) return; + this.target.enableModule(this.name, val, this); + } @property private _separateAxes = false; @@ -76,9 +87,7 @@ export default class RotationOvertimeModule { }) public z = new CurveRange(); - constructor () { - - } + public name = PARTICLE_MODULE_NAME.ROTATION; public animate (p: Particle, dt: number) { const normalizedTime = 1 - p.remainingLifetime / p.startLifetime; diff --git a/cocos/particle/animator/size-overtime.ts b/cocos/particle/animator/size-overtime.ts index 00d2586b5c..0a727b07d9 100644 --- a/cocos/particle/animator/size-overtime.ts +++ b/cocos/particle/animator/size-overtime.ts @@ -5,22 +5,33 @@ import { ccclass, property } from '../../core/data/class-decorator'; import { pseudoRandom, Vec3 } from '../../core/math'; -import Particle from '../particle'; +import { Particle, ParticleModuleBase, PARTICLE_MODULE_NAME } from '../particle'; import CurveRange from './curve-range'; +import { ModuleRandSeed } from '../enum'; // tslint:disable: max-line-length -const SIZE_OVERTIME_RAND_OFFSET = 39825; +const SIZE_OVERTIME_RAND_OFFSET = ModuleRandSeed.SIZE; @ccclass('cc.SizeOvertimeModule') -export default class SizeOvertimeModule { - +export default class SizeOvertimeModule extends ParticleModuleBase { + @property + _enable: Boolean = false; /** * @zh 是否启用。 */ @property({ displayOrder: 0, }) - public enable = false; + public get enable () { + return this._enable; + } + + public set enable (val) { + if (this._enable === val) return; + this._enable = val; + if (!this.target) return; + this.target.enableModule(this.name, val, this); + } /** * @zh 决定是否在每个轴上独立控制粒子大小。 @@ -71,7 +82,9 @@ export default class SizeOvertimeModule { }) public z = new CurveRange(); - public animate (particle: Particle) { + public name = PARTICLE_MODULE_NAME.SIZE; + + public animate (particle: Particle, dt: number) { if (!this.separateAxes) { Vec3.multiplyScalar(particle.size, particle.startSize, this.size.evaluate(1 - particle.remainingLifetime / particle.startLifetime, pseudoRandom(particle.randomSeed + SIZE_OVERTIME_RAND_OFFSET))!); } else { diff --git a/cocos/particle/animator/texture-animation.ts b/cocos/particle/animator/texture-animation.ts index d7a06f0f56..481610a37c 100644 --- a/cocos/particle/animator/texture-animation.ts +++ b/cocos/particle/animator/texture-animation.ts @@ -6,12 +6,13 @@ import { ccclass, property } from '../../core/data/class-decorator'; import { lerp, pseudoRandom, repeat } from '../../core/math'; import { Enum } from '../../core/value-types'; -import Particle from '../particle'; +import { Particle, ParticleModuleBase, PARTICLE_MODULE_NAME } from '../particle'; import { ParticleSystemComponent } from '../particle-system-component'; import CurveRange from './curve-range'; +import { ModuleRandSeed } from '../enum'; // tslint:disable: max-line-length -const TEXTURE_ANIMATION_RAND_OFFSET = 90794; +const TEXTURE_ANIMATION_RAND_OFFSET = ModuleRandSeed.TEXTURE; /** * 粒子贴图动画类型。 @@ -46,7 +47,7 @@ const Animation = Enum({ }); @ccclass('cc.TextureAnimationModule') -export default class TextureAnimationModule { +export default class TextureAnimationModule extends ParticleModuleBase { @property private _enable = false; @@ -72,10 +73,11 @@ export default class TextureAnimationModule { } set enable (val) { + if (this._enable === val) return; this._enable = val; - if (this.ps) { - (this.ps.renderer as any)._updateMaterialParams(); - } + if (!this.target) return; + this.target.updateMaterialParams(); + this.target.enableModule(this.name, val, this); } @property({ @@ -116,9 +118,7 @@ export default class TextureAnimationModule { set numTilesX (val) { if (this._numTilesX !== val) { this._numTilesX = val; - if (this.ps) { - (this.ps.renderer as any)._updateMaterialParams(); - } + this.target!.updateMaterialParams(); } } @@ -136,9 +136,7 @@ export default class TextureAnimationModule { set numTilesY (val) { if (this._numTilesY !== val) { this._numTilesY = val; - if (this.ps) { - (this.ps.renderer as any)._updateMaterialParams(); - } + this.target!.updateMaterialParams(); } } @@ -236,18 +234,13 @@ export default class TextureAnimationModule { tooltip:'从动画贴图中选择特定行以生成动画。\n此选项仅在动画播放方式为 SingleRow 时且禁用 randomRow 时可用' }) public rowIndex = 0; - - private ps: ParticleSystemComponent | null = null; - - public onInit (ps: ParticleSystemComponent) { - this.ps = ps; - } + public name = PARTICLE_MODULE_NAME.TEXTURE; public init (p: Particle) { p.startRow = Math.floor(Math.random() * this.numTilesY); } - public animate (p: Particle) { + public animate (p: Particle, dt: number) { const normalizedTime = 1 - p.remainingLifetime / p.startLifetime; const startFrame = this.startFrame.evaluate(normalizedTime, pseudoRandom(p.randomSeed + TEXTURE_ANIMATION_RAND_OFFSET))! / (this.numTilesX * this.numTilesY); if (this.animation === Animation.WholeSheet) { @@ -269,19 +262,3 @@ export default class TextureAnimationModule { } } } - -// CCClass.fastDefine('cc.TextureAnimationModule', TextureAnimationModule, { -// _enable: false, -// _mode: Mode.Grid, -// numTilesX: 0, -// numTilesY: 0, -// animation: Animation.WholeSheet, -// frameOverTime: new CurveRange(), -// startFrame: new CurveRange(), -// cycleCount: 0, -// _flipU: 0, -// _flipV: 0, -// _uvChannelMask: -1, -// randomRow: false, -// rowIndex: 0 -// }); diff --git a/cocos/particle/animator/velocity-overtime.ts b/cocos/particle/animator/velocity-overtime.ts index af6de5fda1..b89c864342 100644 --- a/cocos/particle/animator/velocity-overtime.ts +++ b/cocos/particle/animator/velocity-overtime.ts @@ -6,27 +6,38 @@ import { ccclass, property } from '../../core/data/class-decorator'; import { Mat4, pseudoRandom, Quat, Vec3 } from '../../core/math'; import { Space } from '../enum'; -import Particle from '../particle'; +import { Particle, ParticleModuleBase, PARTICLE_MODULE_NAME } from '../particle'; import { calculateTransform } from '../particle-general-function'; import CurveRange from './curve-range'; +import { ModuleRandSeed } from '../enum'; // tslint:disable: max-line-length -const VELOCITY_X_OVERTIME_RAND_OFFSET = 197866; -const VELOCITY_Y_OVERTIME_RAND_OFFSET = 156497; -const VELOCITY_Z_OVERTIME_RAND_OFFSET = 984136; +const VELOCITY_X_OVERTIME_RAND_OFFSET = ModuleRandSeed.VELOCITY_X; +const VELOCITY_Y_OVERTIME_RAND_OFFSET = ModuleRandSeed.VELOCITY_Y; +const VELOCITY_Z_OVERTIME_RAND_OFFSET = ModuleRandSeed.VELOCITY_Z; const _temp_v3 = new Vec3(); @ccclass('cc.VelocityOvertimeModule') -export default class VelocityOvertimeModule { - +export default class VelocityOvertimeModule extends ParticleModuleBase { + @property + _enable: Boolean = false; /** * @zh 是否启用。 */ @property({ displayOrder: 0, }) - public enable = false; + public get enable () { + return this._enable; + } + + public set enable (val) { + if (this._enable === val) return; + this._enable = val; + if (!this.target) return; + this.target.enableModule(this.name, val, this); + } /** * @zh X 轴方向上的速度分量。 @@ -84,18 +95,21 @@ export default class VelocityOvertimeModule { private rotation: Quat; private needTransform: boolean; + public name = PARTICLE_MODULE_NAME.VELOCITY; constructor () { + super(); this.rotation = new Quat(); this.speedModifier.constant = 1; this.needTransform = false; + this.needUpdate = true; } public update (space: number, worldTransform: Mat4) { this.needTransform = calculateTransform(space, this.space, worldTransform, this.rotation); } - public animate (p: Particle) { + public animate (p: Particle, dt: number) { const normalizedTime = 1 - p.remainingLifetime / p.startLifetime; const vel = Vec3.set(_temp_v3, this.x.evaluate(normalizedTime, pseudoRandom(p.randomSeed ^ VELOCITY_X_OVERTIME_RAND_OFFSET))!, this.y.evaluate(normalizedTime, pseudoRandom(p.randomSeed ^ VELOCITY_Y_OVERTIME_RAND_OFFSET))!, this.z.evaluate(normalizedTime, pseudoRandom(p.randomSeed ^ VELOCITY_Z_OVERTIME_RAND_OFFSET))!); if (this.needTransform) { @@ -105,7 +119,6 @@ export default class VelocityOvertimeModule { Vec3.add(p.ultimateVelocity, p.velocity, p.animatedVelocity); Vec3.multiplyScalar(p.ultimateVelocity, p.ultimateVelocity, this.speedModifier.evaluate(1 - p.remainingLifetime / p.startLifetime, pseudoRandom(p.randomSeed + VELOCITY_X_OVERTIME_RAND_OFFSET))!); } - } // CCClass.fastDefine('cc.VelocityOvertimeModule', VelocityOvertimeModule, { diff --git a/cocos/particle/emitter/shape-module.ts b/cocos/particle/emitter/shape-module.ts index 885ed62eb5..5e28466f83 100644 --- a/cocos/particle/emitter/shape-module.ts +++ b/cocos/particle/emitter/shape-module.ts @@ -95,13 +95,21 @@ export default class ShapeModule { this._angle = toRadian(val); } + @property + private _enable = false; /** * @zh 是否启用。 */ @property({ displayOrder: 0, }) - public enable = false; + public get enable () { + return this._enable; + } + + public set enable (val) { + this._enable = val; + } /** * @zh 粒子发射器类型 [[ShapeType]]。 diff --git a/cocos/particle/enum.ts b/cocos/particle/enum.ts index c5add5ee53..313703b3e2 100644 --- a/cocos/particle/enum.ts +++ b/cocos/particle/enum.ts @@ -166,3 +166,15 @@ export const TextureMode = Enum({ */ Repeat: 1, }); + +export const ModuleRandSeed = { + LIMIT: 23541, + SIZE: 39825, + TEXTURE: 90794, + COLOR: 91041, + FORCE: 212165, + ROTATION: 125292, + VELOCITY_X: 197866, + VELOCITY_Y: 156497, + VELOCITY_Z: 984136 +} diff --git a/cocos/particle/models/particle-batch-model.ts b/cocos/particle/models/particle-batch-model.ts index be6ec8acb6..12c2eaaac1 100644 --- a/cocos/particle/models/particle-batch-model.ts +++ b/cocos/particle/models/particle-batch-model.ts @@ -28,13 +28,21 @@ */ import { Material } from '../../core/assets/material'; -import { Mesh, RenderingSubMesh } from '../../core/assets/mesh'; +import { RenderingSubMesh, Mesh } from '../../core/assets/mesh'; import { GFX_DRAW_INFO_SIZE, GFXBuffer, IGFXIndirectBuffer } from '../../core/gfx/buffer'; import { GFXAttributeName, GFXBufferUsageBit, GFXFormatInfos, GFXMemoryUsageBit, GFXPrimitiveMode, GFXStatus } from '../../core/gfx/define'; import { IGFXAttribute } from '../../core/gfx/input-assembler'; import { Color } from '../../core/math/color'; import { Model, ModelType } from '../../core/renderer/scene/model'; +import { Particle } from '../particle'; + +const _uvs = [ + 0, 0, // bottom-left + 1, 0, // bottom-right + 0, 1, // top-left + 1, 1, // top-right +]; export default class ParticleBatchModel extends Model { @@ -51,6 +59,8 @@ export default class ParticleBatchModel extends Model { private _mesh: Mesh | null; private _vertCount: number = 0; private _indexCount: number = 0; + private _startTimeOffset: number = 0; + private _lifeTimeOffset: number = 0; constructor () { super(); @@ -169,7 +179,7 @@ export default class ParticleBatchModel extends Model { } } - const indexBuffer = this._device.createBuffer({ + const indexBuffer: GFXBuffer = this._device.createBuffer({ usage: GFXBufferUsageBit.INDEX | GFXBufferUsageBit.TRANSFER_DST, memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, size: this._capacity * this._indexCount * Uint16Array.BYTES_PER_ELEMENT, @@ -193,10 +203,15 @@ export default class ParticleBatchModel extends Model { this._subMeshData = new RenderingSubMesh([vertexBuffer], this._vertAttrs!, GFXPrimitiveMode.TRIANGLE_LIST); this._subMeshData.indexBuffer = indexBuffer; this._subMeshData.indirectBuffer = this._iaInfoBuffer; - this.setSubModelMesh(0, this._subMeshData); + this.setSubModelMesh(0, this._subMeshData!); return vBuffer; } + public setSubModelMaterial (idx: number, mat: Material | null) { + this.initLocalBindings(mat); + super.setSubModelMaterial(idx, mat); + } + public addParticleVertexData (index: number, pvdata: any[]) { if (!this._mesh) { let offset: number = index * this._vertAttrsFloatCount; @@ -239,6 +254,73 @@ export default class ParticleBatchModel extends Model { } } + public addGPUParticleVertexData (p: Particle, num: number, time:number) { + let offset = num * this._vertAttrsFloatCount * this._vertCount; + for (let i = 0; i < this._vertCount; i++) { + let idx = offset; + this._vdataF32![idx++] = p.position.x; + this._vdataF32![idx++] = p.position.y; + this._vdataF32![idx++] = p.position.z; + this._vdataF32![idx++] = time; + + this._vdataF32![idx++] = p.startSize.x; + this._vdataF32![idx++] = p.startSize.y; + this._vdataF32![idx++] = p.startSize.z; + this._vdataF32![idx++] = _uvs[2 * i]; + + this._vdataF32![idx++] = p.rotation.x; + this._vdataF32![idx++] = p.rotation.y; + this._vdataF32![idx++] = p.rotation.z; + this._vdataF32![idx++] = _uvs[2 * i + 1]; + + this._vdataF32![idx++] = p.startColor.r / 255.0; + this._vdataF32![idx++] = p.startColor.g / 255.0; + this._vdataF32![idx++] = p.startColor.b / 255.0; + this._vdataF32![idx++] = p.startColor.a / 255.0; + + this._vdataF32![idx++] = p.velocity.x; + this._vdataF32![idx++] = p.velocity.y; + this._vdataF32![idx++] = p.velocity.z; + this._vdataF32![idx++] = p.startLifetime; + + this._vdataF32![idx++] = p.randomSeed; + + offset += this._vertAttrsFloatCount; + } + } + + public updateGPUParticles (num: number, time: number) { + const pSize = this._vertAttrsFloatCount * this._vertCount; + let pBaseIndex = 0; + let startTime = 0; + let lifeTime = 0; + let lastBaseIndex = 0; + for (let i = 0; i < num; ++i) { + pBaseIndex = i * pSize; + startTime = this._vdataF32![pBaseIndex + this._startTimeOffset]; + lifeTime = this._vdataF32![pBaseIndex + this._lifeTimeOffset]; + if (time - startTime > lifeTime) { + lastBaseIndex = -- num * pSize; + this._vdataF32!.copyWithin(pBaseIndex, lastBaseIndex, lastBaseIndex + pSize); + i--; + } + } + + return num; + } + + public constructAttributeIndex () { + if (!this._vertAttrs) { + return; + } + let vIdx = this._vertAttrs!.findIndex((val) => val.name === 'a_position_starttime'); + let vOffset = (this._vertAttrs![vIdx] as any).offset; + this._startTimeOffset = vOffset / 4 + 3; + vIdx = this._vertAttrs!.findIndex((val) => val.name === 'a_dir_life'); + vOffset = (this._vertAttrs![vIdx] as any).offset; + this._lifeTimeOffset = vOffset / 4 + 3; + } + public updateIA (count: number) { const ia = this.getSubModel(0).inputAssembler!; ia.vertexBuffers[0].update(this._vdataF32!); diff --git a/cocos/particle/particle-general-function.ts b/cocos/particle/particle-general-function.ts index 4bc5f01694..85cbed4df4 100644 --- a/cocos/particle/particle-general-function.ts +++ b/cocos/particle/particle-general-function.ts @@ -110,6 +110,8 @@ export function randomSortArray (arr: any[]) { export function randomSign () { let sgn = randomRange(-1, 1); - sgn === 0 ? sgn++ : sgn; + if (sgn === 0) { + sgn++; + } return sign(sgn); } diff --git a/cocos/particle/particle-system-component.ts b/cocos/particle/particle-system-component.ts index 05cbd0f889..c41412856e 100644 --- a/cocos/particle/particle-system-component.ts +++ b/cocos/particle/particle-system-component.ts @@ -25,22 +25,13 @@ import Burst from './burst'; import ShapeModule from './emitter/shape-module'; import { Space } from './enum'; import { particleEmitZAxis } from './particle-general-function'; -import ParticleSystemRenderer from './renderer/particle-system-renderer'; +import ParticleSystemRenderer from './renderer/particle-system-renderer-data'; import TrailModule from './renderer/trail'; -import { indexOf } from '../core/utils/array'; +import { IParticleSystemRenderer } from './renderer/particle-system-renderer-base'; +import { PARTICLE_MODULE_PROPERTY } from './particle'; const _world_mat = new Mat4(); -const _module_props = [ - "colorOverLifetimeModule", - "shapeModule", - "sizeOvertimeModule", - "velocityOvertimeModule", - "forceOvertimeModule", - "limitVelocityOvertimeModule", - "rotationOvertimeModule", - "textureAnimationModule", - "trailModule" -]; +const _world_rol = new Quat(); @ccclass('cc.ParticleSystemComponent') @help('i18n:cc.ParticleSystemComponent') @@ -437,7 +428,7 @@ export class ParticleSystemComponent extends RenderableComponent { displayOrder: 27, tooltip:'是否剔除非 enable 的模块数据', }) - public isCulling: Boolean = true; + public enableCulling: Boolean = false; /** * @ignore @@ -467,6 +458,8 @@ export class ParticleSystemComponent extends RenderableComponent { @property private _simulationSpace = Space.Local; + public processor: IParticleSystemRenderer | null = null; + constructor () { super(); @@ -495,22 +488,21 @@ export class ParticleSystemComponent extends RenderableComponent { public onLoad () { // HACK, TODO - this.renderer!.onInit(this); + this.renderer.onInit(this); this.shapeModule.onInit(this); this.trailModule.onInit(this); - this.textureAnimationModule.onInit(this); - + this.bindModule(); this._resetPosition(); // this._system.add(this); } public _onMaterialModified (index: number, material: Material) { - this.renderer._onMaterialModified(index, material); + this.processor!.onMaterialModified(index, material); } public _onRebuildPSO (index: number, material: Material) { - this.renderer._onRebuildPSO(index, material); + this.processor!.onRebuildPSO(index, material); } public _collectModels (): Model[] { @@ -522,20 +514,30 @@ export class ParticleSystemComponent extends RenderableComponent { return this._models; } - protected _attachToScene() { - this.renderer._attachToScene(); + protected _attachToScene () { + this.processor!.attachToScene(); if (this.trailModule.enable) { this.trailModule._attachToScene(); } } - protected _detachFromScene() { - this.renderer._detachFromScene(); + protected _detachFromScene () { + this.processor!.detachFromScene(); if (this.trailModule.enable) { this.trailModule._detachFromScene(); } } + public bindModule () { + this.colorOverLifetimeModule.bindTarget(this.processor!); + this.sizeOvertimeModule.bindTarget(this.processor!); + this.rotationOvertimeModule.bindTarget(this.processor!); + this.forceOvertimeModule.bindTarget(this.processor!); + this.limitVelocityOvertimeModule.bindTarget(this.processor!); + this.velocityOvertimeModule.bindTarget(this.processor!); + this.textureAnimationModule.bindTarget(this.processor!); + } + // TODO: fastforward current particle system by simulating particles over given period of time, then pause it. // simulate(time, withChildren, restart, fixedTimeStep) { @@ -605,7 +607,7 @@ export class ParticleSystemComponent extends RenderableComponent { */ public clear () { if (this.enabledInHierarchy) { - this.renderer!.clear(); + this.processor!.clear(); this.trailModule.clear(); } } @@ -614,7 +616,7 @@ export class ParticleSystemComponent extends RenderableComponent { * @zh 获取当前粒子数量 */ public getParticleCount () { - return this.renderer!.getParticleCount(); + return this.processor!.getParticleCount(); } /** @@ -630,7 +632,7 @@ export class ParticleSystemComponent extends RenderableComponent { protected onDestroy () { // this._system.remove(this); - this.renderer.onDestroy(); + this.processor!.onDestroy(); this.trailModule.destroy(); } @@ -638,15 +640,13 @@ export class ParticleSystemComponent extends RenderableComponent { if (this.playOnAwake) { this.play(); } - this.renderer.onEnable(); + this.processor!.onEnable(); this.trailModule.onEnable(); } - protected onDisable () { - this.renderer!.onDisable(); + this.processor!.onDisable(); this.trailModule.onDisable(); } - protected update (dt) { const scaledDeltaTime = dt * this.simulationSpeed; if (this._isPlaying) { @@ -656,12 +656,12 @@ export class ParticleSystemComponent extends RenderableComponent { this._emit(scaledDeltaTime); // simulation, update particles. - if (this.renderer!._updateParticles(scaledDeltaTime) === 0 && !this._isEmitting) { + if (this.processor!.updateParticles(scaledDeltaTime) === 0 && !this._isEmitting) { this.stop(); } // update render data - this.renderer!._updateRenderData(); + this.processor!.updateRenderData(); // update trail if (this.trailModule.enable) { @@ -679,9 +679,15 @@ export class ParticleSystemComponent extends RenderableComponent { } private emit (count, dt) { + const delta = this._time / this.duration; + + if (this._simulationSpace === Space.World) { + this.node.getWorldMatrix(_world_mat); + this.node.getWorldRotation(_world_rol); + } for (let i = 0; i < count; ++i) { - const particle = this.renderer!._getFreeParticle(); + const particle = this.processor!.getFreeParticle(); if (particle === null) { return; } @@ -699,54 +705,45 @@ export class ParticleSystemComponent extends RenderableComponent { this.textureAnimationModule.init(particle); } - Vec3.multiplyScalar(particle.velocity, particle.velocity, this.startSpeed.evaluate(this._time / this.duration, rand)!); + Vec3.multiplyScalar(particle.velocity, particle.velocity, this.startSpeed.evaluate(delta, rand)!); - switch (this._simulationSpace) { - case Space.Local: - break; - case Space.World: - this.node.getWorldMatrix(_world_mat); - Vec3.transformMat4(particle.position, particle.position, _world_mat); - const worldRot = new Quat(); - this.node.getWorldRotation(worldRot); - Vec3.transformQuat(particle.velocity, particle.velocity, worldRot); - break; - case Space.Custom: - // TODO: - break; + if (this._simulationSpace === Space.World) { + Vec3.transformMat4(particle.position, particle.position, _world_mat); + Vec3.transformQuat(particle.velocity, particle.velocity, _world_rol); } + Vec3.copy(particle.ultimateVelocity, particle.velocity); // apply startRotation. if (this.startRotation3D) { - Vec3.set(particle.rotation, this.startRotationX.evaluate(this._time / this.duration, rand)!, - this.startRotationY.evaluate(this._time / this.duration, rand)!, - this.startRotationZ.evaluate(this._time / this.duration, rand)!); + Vec3.set(particle.rotation, this.startRotationX.evaluate(delta, rand)!, + this.startRotationY.evaluate(delta, rand)!, + this.startRotationZ.evaluate(delta, rand)!); } else { - Vec3.set(particle.rotation, 0, 0, this.startRotationZ.evaluate(this._time / this.duration, rand)!); + Vec3.set(particle.rotation, 0, 0, this.startRotationZ.evaluate(delta, rand)!); } // apply startSize. if (this.startSize3D) { - Vec3.set(particle.startSize, this.startSizeX.evaluate(this._time / this.duration, rand)!, - this.startSizeY.evaluate(this._time / this.duration, rand)!, - this.startSizeZ.evaluate(this._time / this.duration, rand)!); + Vec3.set(particle.startSize, this.startSizeX.evaluate(delta, rand)!, + this.startSizeY.evaluate(delta, rand)!, + this.startSizeZ.evaluate(delta, rand)!); } else { - Vec3.set(particle.startSize, this.startSizeX.evaluate(this._time / this.duration, rand)!, 1, 1); + Vec3.set(particle.startSize, this.startSizeX.evaluate(delta, rand)!, 1, 1); particle.startSize.y = particle.startSize.x; } Vec3.copy(particle.size, particle.startSize); // apply startColor. - particle.startColor.set(this.startColor.evaluate(this._time / this.duration, rand)); + particle.startColor.set(this.startColor.evaluate(delta, rand)); particle.color.set(particle.startColor); // apply startLifetime. - particle.startLifetime = this.startLifetime.evaluate(this._time / this.duration, rand)! + dt; + particle.startLifetime = this.startLifetime.evaluate(delta, rand)! + dt; particle.remainingLifetime = particle.startLifetime; particle.randomSeed = randomRangeInt(0, 233280); - this.renderer!._setNewParticle(particle); + this.processor!.setNewParticle(particle); } // end of particles forLoop. } @@ -760,7 +757,7 @@ export class ParticleSystemComponent extends RenderableComponent { for (let i = 0; i < cnt; ++i) { this._time += dt; this._emit(dt); - this.renderer!._updateParticles(dt); + this.processor!.updateParticles(dt); } } @@ -849,6 +846,6 @@ export class ParticleSystemComponent extends RenderableComponent { } public _onBeforeSerialize (props) { - return this.isCulling ? props.filter(p => !_module_props.includes(p) || this[p].enable) : props; + return this.enableCulling ? props.filter(p => !PARTICLE_MODULE_PROPERTY.includes(p) || this[p].enable) : props; } } diff --git a/cocos/particle/particle.ts b/cocos/particle/particle.ts index 655a870f15..b416150f0c 100644 --- a/cocos/particle/particle.ts +++ b/cocos/particle/particle.ts @@ -2,10 +2,11 @@ * @hidden */ -import { Color, Vec3 } from '../core/math'; +import { Color, Vec3, Mat4 } from '../core/math'; import { ParticleSystemComponent } from './particle-system-component'; +import { IParticleSystemRenderer } from './renderer/particle-system-renderer-base'; -export default class Particle { +export class Particle { public particleSystem: ParticleSystemComponent; public position: Vec3; public velocity: Vec3; @@ -48,3 +49,59 @@ export default class Particle { this.startRow = 0; } } + +export const PARTICLE_MODULE_NAME = { + COLOR: 'colorModule', + FORCE: 'forceModule', + LIMIT: 'limitModule', + ROTATION: 'rotationModule', + SIZE: 'sizeModule', + VELOCITY: 'velocityModule', + TEXTURE: 'textureModule' +}; + +export const PARTICLE_MODULE_ORDER = [ + 'sizeModule', + 'colorModule', + 'forceModule', + 'velocityModule', + 'limitModule', + 'rotationModule', + 'textureModule' +]; + +export const PARTICLE_MODULE_PROPERTY = [ + 'colorOverLifetimeModule', + 'shapeModule', + 'sizeOvertimeModule', + 'velocityOvertimeModule', + 'forceOvertimeModule', + 'limitVelocityOvertimeModule', + 'rotationOvertimeModule', + 'textureAnimationModule', + 'trailModule' +]; + +export interface IParticleModule { + target: IParticleSystemRenderer | null; + needUpdate: Boolean; + needAnimate: Boolean; + name: string; + bindTarget (target: any): void; + update (space: number, trans: Mat4): void; + animate (p: Particle, dt?: number): void; +} + +export abstract class ParticleModuleBase implements IParticleModule{ + public target:IParticleSystemRenderer | null = null; + public needUpdate: Boolean = false; + public needAnimate: Boolean = true; + + public bindTarget (target: IParticleSystemRenderer) { + this.target = target; + } + + public update (space: number, trans: Mat4) {} + public abstract name: string; + public abstract animate (p: Particle, dt?: number): void; +} diff --git a/cocos/particle/renderer/particle-system-renderer-base.ts b/cocos/particle/renderer/particle-system-renderer-base.ts new file mode 100644 index 0000000000..b8bd989487 --- /dev/null +++ b/cocos/particle/renderer/particle-system-renderer-base.ts @@ -0,0 +1,109 @@ +import { Component, IGFXAttribute } from '../../core'; +import ParticleBatchModel from '../models/particle-batch-model'; +import ParticleSystemRenderer from './particle-system-renderer-data'; +import { Material } from '../../core/assets'; +import { Particle, IParticleModule } from '../particle'; +import { RenderMode } from '../enum'; + +export interface IParticleSystemRenderer { + onInit (ps: Component): void; + onEnable (): void; + onDisable (): void; + onDestroy (): void; + clear (): void; + attachToScene (): void; + detachFromScene (): void; + updateMaterialParams (): void; + setVertexAttributes (): void; + updateRenderMode (): void; + onMaterialModified (index: number, material: Material): void; + onRebuildPSO (index: number, material: Material) : void; + getParticleCount (): number; + getFreeParticle (): Particle | null; + setNewParticle (p: Particle): void; + updateParticles (dt: number): number; + updateRenderData (): void; + enableModule (name: string, val: Boolean, pm: IParticleModule): void; + updateTrailMaterial (): void; + getDefaultTrailMaterial (): any; +} + +export abstract class ParticleSystemRendererBase implements IParticleSystemRenderer { + protected _particleSystem: any = null; + protected _model: ParticleBatchModel | null = null; + protected _renderInfo: ParticleSystemRenderer | null = null; + protected _vertAttrs: IGFXAttribute[] = []; + + constructor (info: ParticleSystemRenderer) { + this._renderInfo = info; + } + + public onInit (ps: Component) { + this._particleSystem = ps; + } + + public onEnable () { + if (!this._particleSystem) { + return; + } + this.attachToScene(); + this._model!.initialize(this._particleSystem.node); + this._model!.enabled = this._particleSystem.enabledInHierarchy; + } + + public onDisable () { + this.detachFromScene(); + } + + public onDestroy () { + if (this._model) { + cc.director.root.destroyModel(this._model); + this._model = null; + } + } + + public attachToScene () { + if (this._model) { + if (this._model.scene) { + this.detachFromScene(); + } + this._particleSystem!._getRenderScene().addModel(this._model); + } + } + + public detachFromScene () { + if (this._model && this._model.scene) { + this._model.scene.removeModel(this._model); + } + } + + public setVertexAttributes () { + if (this._model) { + this._model.setVertexAttributes(this._renderInfo!.renderMode === RenderMode.Mesh ? this._renderInfo!.mesh : null, this._vertAttrs); + } + } + + protected _updateModel () { + if (!this._model) { + this._model = cc.director.root.createModel(ParticleBatchModel); + this._model!.setCapacity(this._particleSystem.capacity); + this._model!.visFlags = this._particleSystem.visibility; + } + + this._model!.setVertexAttributes(this._renderInfo!.renderMode === RenderMode.Mesh ? this._renderInfo!.mesh : null, this._vertAttrs); + } + + public updateTrailMaterial () {} + public getDefaultTrailMaterial () { return null; } + public abstract getParticleCount () : number; + public abstract getFreeParticle (): Particle | null; + public abstract onMaterialModified (index: number, material: Material) : void; + public abstract onRebuildPSO (index: number, material: Material) : void; + public abstract updateRenderMode () : void; + public abstract updateMaterialParams () : void; + public abstract clear () : void; + public abstract setNewParticle (p: Particle): void; + public abstract updateParticles (dt: number): number; + public abstract updateRenderData (): void; + public abstract enableModule (name: string, val: Boolean, pm: IParticleModule): void; +} \ No newline at end of file diff --git a/cocos/particle/renderer/particle-system-renderer-cpu.ts b/cocos/particle/renderer/particle-system-renderer-cpu.ts new file mode 100644 index 0000000000..a7ea727848 --- /dev/null +++ b/cocos/particle/renderer/particle-system-renderer-cpu.ts @@ -0,0 +1,452 @@ +import { builtinResMgr } from '../../core/3d/builtin'; +import { Material } from '../../core/assets'; +import { GFXAttributeName, GFXFormat } from '../../core/gfx/define'; +import { Mat4, Vec2, Vec3, Vec4, pseudoRandom } from '../../core/math'; +import { RecyclePool } from '../../core/memop'; +import { MaterialInstance, IMaterialInstanceInfo } from '../../core/renderer/core/material-instance'; +import { IDefineMap } from '../../core/renderer/core/pass-utils'; +import { RenderMode, Space } from '../enum'; +import { Particle, IParticleModule, PARTICLE_MODULE_ORDER } from '../particle'; +import { ParticleSystemRendererBase } from './particle-system-renderer-base'; +import { Component } from '../../core'; + +const _tempAttribUV = new Vec3(); +const _tempWorldTrans = new Mat4(); + +const _anim_module = [ + 'colorOverLifetimeModule', + 'sizeOvertimeModule', + 'velocityOvertimeModule', + 'forceOvertimeModule', + 'limitVelocityOvertimeModule', + 'rotationOvertimeModule', + 'textureAnimationModule' +]; + +const _uvs = [ + 0, 0, // bottom-left + 1, 0, // bottom-right + 0, 1, // top-left + 1, 1, // top-right +]; + +const CC_USE_WORLD_SPACE = 'CC_USE_WORLD_SPACE'; + +const CC_RENDER_MODE = 'CC_RENDER_MODE'; +const RENDER_MODE_BILLBOARD = 0; +const RENDER_MODE_STRETCHED_BILLBOARD = 1; +const RENDER_MODE_HORIZONTAL_BILLBOARD = 2; +const RENDER_MODE_VERTICAL_BILLBOARD = 3; +const RENDER_MODE_MESH = 4; + +const _vertex_attrs = [ + { name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // position + { name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, // uv,frame idx + { name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RGB32F }, // size + { name: GFXAttributeName.ATTR_TEX_COORD2, format: GFXFormat.RGB32F }, // rotation + { name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true }, // color +]; + +const _vertex_attrs_stretch = [ + { name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // position + { name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, // uv,frame idx + { name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RGB32F }, // size + { name: GFXAttributeName.ATTR_TEX_COORD2, format: GFXFormat.RGB32F }, // rotation + { name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true }, // color + { name: GFXAttributeName.ATTR_COLOR1, format: GFXFormat.RGB32F }, // particle velocity +]; + +const _vertex_attrs_mesh = [ + { name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // particle position + { name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, // uv,frame idx + { name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RGB32F }, // size + { name: GFXAttributeName.ATTR_TEX_COORD2, format: GFXFormat.RGB32F }, // rotation + { name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true }, // particle color + { name: GFXAttributeName.ATTR_TEX_COORD3, format: GFXFormat.RGB32F }, // mesh position + { name: GFXAttributeName.ATTR_NORMAL, format: GFXFormat.RGB32F }, // mesh normal + { name: GFXAttributeName.ATTR_COLOR1, format: GFXFormat.RGBA8, isNormalized: true }, // mesh color +]; + +const _matInsInfo: IMaterialInstanceInfo = { + parent: null!, + owner: null!, + subModelIdx: 0, +}; + +export default class ParticleSystemRendererCPU extends ParticleSystemRendererBase { + private _defines: IDefineMap; + private _trailDefines: IDefineMap; + private _frameTile_velLenScale: Vec4; + private _defaultMat: Material | null = null; + private _node_scale: Vec4; + private _attrs: any[]; + private _particles: RecyclePool | null = null; + private _defaultTrailMat: Material | null = null; + private _updateList: Map = new Map(); + private _animateList: Map = new Map(); + private _runAnimateList: IParticleModule[] = new Array(); + private _fillDataFunc: any = null; + private _uScaleHandle: number = 0; + private _uLenHandle: number = 0; + + constructor (info: any) { + super(info); + + this._model = null; + + this._frameTile_velLenScale = new Vec4(1, 1, 0, 0); + this._node_scale = new Vec4(); + this._attrs = new Array(5); + this._defines = { + CC_USE_WORLD_SPACE: true, + CC_USE_BILLBOARD: true, + CC_USE_STRETCHED_BILLBOARD: false, + CC_USE_HORIZONTAL_BILLBOARD: false, + CC_USE_VERTICAL_BILLBOARD: false, + }; + this._trailDefines = { + CC_USE_WORLD_SPACE: true, + // CC_DRAW_WIRE_FRAME: true, // + }; + } + + public onInit (ps: Component) { + super.onInit(ps); + + this._particles = new RecyclePool(() => { + return new Particle(this); + }, 16); + this._setVertexAttrib(); + this._updateModel(); + this._setFillFunc(); + this._initModuleList(); + this.updateMaterialParams(); + this.updateTrailMaterial(); + } + + public clear () { + this._particles!.reset(); + this._particleSystem!.trailModule.clear(); + this.updateRenderData(); + } + + public updateRenderMode () { + this._setVertexAttrib(); + this._updateModel(); + this._setFillFunc(); + this.updateMaterialParams(); + } + + public getFreeParticle (): Particle | null { + if (this._particles!.length >= this._particleSystem!.capacity) { + return null; + } + return this._particles!.add(); + } + + public getDefaultTrailMaterial (): any { + return this._defaultTrailMat; + } + + public setNewParticle (p: Particle) { + } + + private _initModuleList () { + _anim_module.forEach(val => { + const pm = this._particleSystem[val]; + if (pm.enable) { + if (pm.needUpdate) { + this._updateList[pm.name] = pm; + } + + if (pm.needAnimate) { + this._animateList[pm.name] = pm; + } + } + }); + + // reorder + this._runAnimateList.length = 0; + for (let i = 0, len = PARTICLE_MODULE_ORDER.length; i < len; i++) { + const p = this._animateList[PARTICLE_MODULE_ORDER[i]]; + if (p) { + this._runAnimateList.push(p); + } + } + } + + public enableModule (name: string, val: Boolean, pm: IParticleModule) { + if (val) { + if (pm.needUpdate) { + this._updateList[pm.name] = pm; + } + + if (pm.needAnimate) { + this._animateList[pm.name] = pm; + } + } else { + delete this._animateList[name]; + delete this._updateList[name]; + } + // reorder + this._runAnimateList.length = 0; + for (let i = 0, len = PARTICLE_MODULE_ORDER.length; i < len; i++) { + const p = this._animateList[PARTICLE_MODULE_ORDER[i]]; + if (p) { + this._runAnimateList.push(p); + } + } + } + + public updateParticles (dt: number) { + const ps = this._particleSystem; + if (!ps) { + return this._particles!.length; + } + ps.node.getWorldMatrix(_tempWorldTrans); + switch (ps.scaleSpace) { + case Space.Local: + ps.node.getScale(this._node_scale); + break; + case Space.World: + ps.node.getWorldScale(this._node_scale); + break; + } + const mat: Material | null = ps.getMaterialInstance(0) || this._defaultMat; + const pass = mat!.passes[0]; + pass.setUniform(this._uScaleHandle, this._node_scale); + + this._updateList.forEach((value: IParticleModule, key: string)=>{ + value.update(ps._simulationSpace, _tempWorldTrans); + }); + + if (ps.trailModule.enable) { + ps.trailModule.update(); + } + + for (let i = 0; i < this._particles!.length; ++i) { + const p = this._particles!.data[i]; + p.remainingLifetime -= dt; + Vec3.set(p.animatedVelocity, 0, 0, 0); + + if (p.remainingLifetime < 0.0) { + if (ps.trailModule.enable) { + ps.trailModule.removeParticle(p); + } + this._particles!.removeAt(i); + --i; + continue; + } + + // apply gravity. + p.velocity.y -= ps.gravityModifier.evaluate(1 - p.remainingLifetime / p.startLifetime, pseudoRandom(p.randomSeed))! * 9.8 * dt; + + Vec3.copy(p.ultimateVelocity, p.velocity); + + this._runAnimateList.forEach(value =>{ + value.animate(p, dt); + }); + + Vec3.scaleAndAdd(p.position, p.position, p.ultimateVelocity, dt); // apply velocity. + if (ps.trailModule.enable) { + ps.trailModule.animate(p, dt); + } + } + return this._particles!.length; + } + + // internal function + public updateRenderData () { + // update vertex buffer + let idx = 0; + for (let i = 0; i < this._particles!.length; ++i) { + const p = this._particles!.data[i]; + let fi = 0; + if (this._particleSystem!.textureAnimationModule.enable) { + fi = p.frameIndex; + } + idx = i * 4; + this._fillDataFunc(p, idx, fi); + } + // because we use index buffer, per particle index count = 6. + this._model!.updateIA(this._particles!.length); + } + + public getParticleCount (): number { + return this._particles!.length; + } + + public onMaterialModified (index: number, material: Material) { + if (index === 0) { + this._updateModel(); + this.updateMaterialParams(); + } else { + this.updateTrailMaterial(); + } + } + + public onRebuildPSO (index: number, material: Material) { + if (this._model && index === 0) { + this._model.setSubModelMaterial(0, material); + } + if (this._particleSystem!.trailModule._trailModel && index === 1) { + this._particleSystem!.trailModule._trailModel.setSubModelMaterial(0, material); + } + } + + private _setFillFunc () { + if (this._renderInfo!.renderMode === RenderMode.Mesh) { + this._fillDataFunc = this._fillMeshData; + } else if (this._renderInfo!.renderMode === RenderMode.StrecthedBillboard) { + this._fillDataFunc = this._fillStrecthedData; + } else { + this._fillDataFunc = this._fillNormalData; + } + } + + private _fillMeshData (p: Particle, idx: number, fi: number) { + const i = idx / 4; + let attrNum = 0; + this._attrs[attrNum++] = p.position; + _tempAttribUV.z = fi; + this._attrs[attrNum++] = _tempAttribUV; + this._attrs[attrNum++] = p.size; + this._attrs[attrNum++] = p.rotation; + this._attrs[attrNum++] = p.color._val; + this._model!.addParticleVertexData(i, this._attrs); + } + + private _fillStrecthedData (p: Particle, idx: number, fi: number) { + let attrNum = 0; + for (let j = 0; j < 4; ++j) { // four verts per particle. + attrNum = 0; + this._attrs[attrNum++] = p.position; + _tempAttribUV.x = _uvs[2 * j]; + _tempAttribUV.y = _uvs[2 * j + 1]; + _tempAttribUV.z = fi; + this._attrs[attrNum++] = _tempAttribUV; + this._attrs[attrNum++] = p.size; + this._attrs[attrNum++] = p.rotation; + this._attrs[attrNum++] = p.color._val; + this._attrs[attrNum++] = p.ultimateVelocity; + this._attrs[attrNum++] = p.ultimateVelocity; + this._model!.addParticleVertexData(idx++, this._attrs); + } + } + + private _fillNormalData (p: Particle, idx: number, fi: number) { + let attrNum = 0; + for (let j = 0; j < 4; ++j) { // four verts per particle. + attrNum = 0; + this._attrs[attrNum++] = p.position; + _tempAttribUV.x = _uvs[2 * j]; + _tempAttribUV.y = _uvs[2 * j + 1]; + _tempAttribUV.z = fi; + this._attrs[attrNum++] = _tempAttribUV; + this._attrs[attrNum++] = p.size; + this._attrs[attrNum++] = p.rotation; + this._attrs[attrNum++] = p.color._val; + this._attrs[attrNum++] = null; + this._model!.addParticleVertexData(idx++, this._attrs); + } + } + + private _setVertexAttrib () { + switch (this._renderInfo!.renderMode) { + case RenderMode.StrecthedBillboard: + this._vertAttrs = _vertex_attrs_stretch.slice(); + break; + case RenderMode.Mesh: + this._vertAttrs = _vertex_attrs_mesh.slice(); + break; + default: + this._vertAttrs = _vertex_attrs.slice(); + } + } + + public updateMaterialParams () { + if (!this._particleSystem) { + return; + } + + const ps = this._particleSystem; + const shareMaterial = ps.sharedMaterial; + if (shareMaterial != null) { + const effectName = shareMaterial._effectAsset._name; + // reset material + if (effectName.indexOf('particle') === -1 || effectName.indexOf('particle-gpu') !== -1) { + ps.setMaterial(null, 0); + } + } + + if (ps.sharedMaterial == null && this._defaultMat == null) { + _matInsInfo.parent = builtinResMgr.get('default-particle-material'); + _matInsInfo.owner = this._particleSystem; + _matInsInfo.subModelIdx = 0; + this._defaultMat = new MaterialInstance(_matInsInfo); + } + const mat: Material = ps.getMaterialInstance(0) || this._defaultMat; + if (ps._simulationSpace === Space.World) { + this._defines[CC_USE_WORLD_SPACE] = true; + } else { + this._defines[CC_USE_WORLD_SPACE] = false; + } + + const pass = mat.passes[0]; + this._uScaleHandle = pass.getHandle('scale')!; + this._uLenHandle = pass.getHandle('frameTile_velLenScale')!; + + const renderMode = this._renderInfo!.renderMode; + const vlenScale = this._frameTile_velLenScale; + if (renderMode === RenderMode.Billboard) { + this._defines[CC_RENDER_MODE] = RENDER_MODE_BILLBOARD; + } else if (renderMode === RenderMode.StrecthedBillboard) { + this._defines[CC_RENDER_MODE] = RENDER_MODE_STRETCHED_BILLBOARD; + vlenScale.z = this._renderInfo!.velocityScale; + vlenScale.w = this._renderInfo!.lengthScale; + } else if (renderMode === RenderMode.HorizontalBillboard) { + this._defines[CC_RENDER_MODE] = RENDER_MODE_HORIZONTAL_BILLBOARD; + } else if (renderMode === RenderMode.VerticalBillboard) { + this._defines[CC_RENDER_MODE] = RENDER_MODE_VERTICAL_BILLBOARD; + } else if (renderMode === RenderMode.Mesh) { + this._defines[CC_RENDER_MODE] = RENDER_MODE_MESH; + } else { + console.warn(`particle system renderMode ${renderMode} not support.`); + } + + if (ps.textureAnimationModule.enable) { + Vec2.set(vlenScale, ps.textureAnimationModule.numTilesX, ps.textureAnimationModule.numTilesY); + pass.setUniform(this._uLenHandle, vlenScale); + } else { + pass.setUniform(this._uLenHandle, vlenScale); + } + mat.recompileShaders(this._defines); + if (this._model) { + this._model.setSubModelMaterial(0, mat); + } + } + + public updateTrailMaterial () { + if (!this._particleSystem) { + return; + } + const ps = this._particleSystem; + if (ps.trailModule.enable) { + if (ps.simulationSpace === Space.World || ps.trailModule.space === Space.World) { + this._trailDefines[CC_USE_WORLD_SPACE] = true; + } else { + this._trailDefines[CC_USE_WORLD_SPACE] = false; + } + let mat = ps.getMaterialInstance(1); + if (mat === null && this._defaultTrailMat === null) { + _matInsInfo.parent = builtinResMgr.get('default-trail-material'); + _matInsInfo.owner = this._particleSystem; + _matInsInfo.subModelIdx = 1; + this._defaultTrailMat = new MaterialInstance(_matInsInfo); + } + mat = mat || this._defaultTrailMat; + mat!.recompileShaders(this._trailDefines); + ps.trailModule._updateMaterial(); + } + } +} diff --git a/cocos/particle/renderer/particle-system-renderer-data.ts b/cocos/particle/renderer/particle-system-renderer-data.ts new file mode 100644 index 0000000000..0beac97ca5 --- /dev/null +++ b/cocos/particle/renderer/particle-system-renderer-data.ts @@ -0,0 +1,179 @@ +import { Material, Mesh } from '../../core/assets'; +import { ccclass, property } from '../../core/data/class-decorator'; +import { RenderMode} from '../enum'; +import ParticleSystemRendererCPU from './particle-system-renderer-cpu'; +import ParticleSystemRendererGPU from './particle-system-renderer-gpu'; +@ccclass('cc.ParticleSystemRenderer') +export default class ParticleSystemRenderer { + + /** + * @zh 设定粒子生成模式。 + */ + @property({ + type: RenderMode, + displayOrder: 0, + tooltip: '设定粒子生成模式', + }) + public get renderMode () { + return this._renderMode; + } + + public set renderMode (val) { + if (this._renderMode === val) { + return; + } + this._renderMode = val; + this._particleSystem.processor.updateRenderMode(); + } + + /** + * @zh 在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按速度大小进行拉伸。 + */ + @property({ + displayOrder: 1, + tooltip: '在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按速度大小进行拉伸', + }) + public get velocityScale () { + return this._velocityScale; + } + + public set velocityScale (val) { + this._velocityScale = val; + this._particleSystem.processor.updateMaterialParams(); + // this._updateModel(); + } + + /** + * @zh 在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按粒子大小进行拉伸。 + */ + @property({ + displayOrder: 2, + tooltip: '在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按粒子大小进行拉伸', + }) + public get lengthScale () { + return this._lengthScale; + } + + public set lengthScale (val) { + this._lengthScale = val; + this._particleSystem.processor.updateMaterialParams(); + // this._updateModel(); + } + + @property({ + type: RenderMode, + displayOrder: 3, + }) + private _renderMode = RenderMode.Billboard; + + @property({ + displayOrder: 4, + }) + private _velocityScale = 1; + + @property({ + displayOrder: 5, + }) + private _lengthScale = 1; + + @property({ + displayOrder: 6, + }) + private _mesh: Mesh | null = null; + + /** + * @zh 粒子发射的模型。 + */ + @property({ + type: Mesh, + displayOrder: 7, + tooltip: '粒子发射的模型', + }) + public get mesh () { + return this._mesh; + } + + public set mesh (val) { + this._mesh = val; + this._particleSystem.processor.setVertexAttributes(); + } + + /** + * @zh 粒子使用的材质。 + */ + @property({ + type: Material, + displayOrder: 8, + tooltip: '粒子使用的材质', + }) + public get particleMaterial () { + if (!this._particleSystem) { + return null; + } + return this._particleSystem.getMaterial(0); + } + + public set particleMaterial (val) { + this._particleSystem.setMaterial(val, 0); + } + + /** + * @zh 拖尾使用的材质。 + */ + @property({ + type: Material, + displayOrder: 9, + tooltip: '拖尾使用的材质', + }) + public get trailMaterial () { + if (!this._particleSystem) { + return null; + } + return this._particleSystem.getMaterial(1)!; + } + + public set trailMaterial (val) { + this._particleSystem.setMaterial(val, 1); + } + + @property + private _useGPU: Boolean = false; + + @property({ + type: Boolean, + displayOrder: 10, + tooltip:'是否启用GPU粒子', + }) + public get useGPU () { + return this._useGPU; + } + + public set useGPU (val) { + if (this._useGPU === val) { + return; + } + + this._useGPU = val; + this._switchProcessor(); + } + + private _particleSystem: any = null; + + onInit (ps: any) { + this._particleSystem = ps; + this._particleSystem.processor = this._useGPU ? new ParticleSystemRendererGPU(this) : new ParticleSystemRendererCPU(this); + this._particleSystem.processor.onInit(ps); + } + + private _switchProcessor () { + if (this._particleSystem.processor) { + this._particleSystem.processor.detachFromScene(); + this._particleSystem.processor.clear(); + this._particleSystem.processor = null; + } + this._particleSystem.processor = this._useGPU ? new ParticleSystemRendererGPU(this) : new ParticleSystemRendererCPU(this); + this._particleSystem.processor.onInit(this._particleSystem); + this._particleSystem.processor.onEnable(); + this._particleSystem.bindModule(); + } +} diff --git a/cocos/particle/renderer/particle-system-renderer-gpu.ts b/cocos/particle/renderer/particle-system-renderer-gpu.ts new file mode 100644 index 0000000000..eea0ae7017 --- /dev/null +++ b/cocos/particle/renderer/particle-system-renderer-gpu.ts @@ -0,0 +1,402 @@ +import { builtinResMgr } from '../../core/3d/builtin'; +import { Material } from '../../core/assets'; +import { Texture2D } from '../../core'; +import { Component } from '../../core/components'; +import { GFXAttributeName, GFXFormat } from '../../core/gfx/define'; +import { IGFXAttribute } from '../../core/gfx/input-assembler'; +import { Mat4, Vec2, Vec4, Quat} from '../../core/math'; +import { MaterialInstance, IMaterialInstanceInfo } from '../../core/renderer/core/material-instance'; +import { IDefineMap } from '../../core/renderer/core/pass-utils'; +import { RenderMode, Space } from '../enum'; +import { Particle, IParticleModule } from '../particle'; +import { packGradientRange } from '../animator/gradient-range'; +import { Pass } from '../../core/renderer'; +import { packCurveRangeXYZ, packCurveRangeZ, packCurveRangeXYZW, packCurveRangeN, packCurveRangeXY } from '../animator/curve-range'; +import { ParticleSystemRendererBase } from './particle-system-renderer-base'; +import { EDITOR } from 'internal:constants'; + +const _tempWorldTrans = new Mat4(); +const _tempVec4 = new Vec4(); +const _world_rot = new Quat(); + +const _sample_num = 32; +const _sample_interval = 1.0 / _sample_num; + +const CC_USE_WORLD_SPACE = 'CC_USE_WORLD_SPACE'; + +const CC_RENDER_MODE = 'CC_RENDER_MODE'; +const RENDER_MODE_BILLBOARD = 0; +const RENDER_MODE_STRETCHED_BILLBOARD = 1; +const RENDER_MODE_HORIZONTAL_BILLBOARD = 2; +const RENDER_MODE_VERTICAL_BILLBOARD = 3; +const RENDER_MODE_MESH = 4; + +const COLOR_OVER_TIME_MODULE_ENABLE = 'COLOR_OVER_TIME_MODULE_ENABLE'; +const ROTATION_OVER_TIME_MODULE_ENABLE = 'ROTATION_OVER_TIME_MODULE_ENABLE'; +const SIZE_OVER_TIME_MODULE_ENABLE = 'SIZE_OVER_TIME_MODULE_ENABLE'; +const VELOCITY_OVER_TIME_MODULE_ENABLE = 'VELOCITY_OVER_TIME_MODULE_ENABLE'; +const FORCE_OVER_TIME_MODULE_ENABLE = 'FORCE_OVER_TIME_MODULE_ENABLE'; +const TEXTURE_ANIMATION_MODULE_ENABLE = 'TEXTURE_ANIMATION_MODULE_ENABLE'; + +const _vert_attr_name = { + POSITION_STARTTIME: 'a_position_starttime', + VERT_SIZE_UV: 'a_size_uv', + VERT_ROTATION_UV: 'a_rotation_uv', + COLOR: 'a_color', + DIR_LIFE: 'a_dir_life', + RANDOM_SEED: 'a_rndSeed' + }; + +const _gpu_vert_attr = [ + { name: _vert_attr_name.POSITION_STARTTIME, format: GFXFormat.RGBA32F}, + { name: _vert_attr_name.VERT_SIZE_UV, format: GFXFormat.RGBA32F}, + { name: _vert_attr_name.VERT_ROTATION_UV, format: GFXFormat.RGBA32F}, + { name: _vert_attr_name.COLOR, format: GFXFormat.RGBA32F}, + { name: _vert_attr_name.DIR_LIFE, format: GFXFormat.RGBA32F}, + { name: _vert_attr_name.RANDOM_SEED, format: GFXFormat.R32F} +]; + +const _gpu_vert_attr_mesh = [ + { name: _vert_attr_name.POSITION_STARTTIME, format: GFXFormat.RGBA32F}, + { name: _vert_attr_name.VERT_SIZE_UV, format: GFXFormat.RGBA32F}, + { name: _vert_attr_name.VERT_ROTATION_UV, format: GFXFormat.RGBA32F}, + { name: _vert_attr_name.COLOR, format: GFXFormat.RGBA32F}, + { name: _vert_attr_name.DIR_LIFE, format: GFXFormat.RGBA32F}, + { name: _vert_attr_name.RANDOM_SEED, format: GFXFormat.R32F}, + { name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, // uv,frame idx + { name: GFXAttributeName.ATTR_TEX_COORD3, format: GFXFormat.RGB32F }, // mesh position + { name: GFXAttributeName.ATTR_NORMAL, format: GFXFormat.RGB32F }, // mesh normal + { name: GFXAttributeName.ATTR_COLOR1, format: GFXFormat.RGBA8, isNormalized: true }, // mesh color +]; + +const _matInsInfo: IMaterialInstanceInfo = { + parent: null!, + owner: null!, + subModelIdx: 0, +}; + +export default class ParticleSystemRendererGPU extends ParticleSystemRendererBase { + private _defines: IDefineMap; + private _frameTile_velLenScale: Vec4; + private _node_scale: Vec4; + protected _vertAttrs: IGFXAttribute[] = []; + protected _defaultMat: Material | null = null; + private _particleNum: number = 0; + private _tempParticle: any = null; + private _colorTexture: Texture2D | null = null; + private _forceTexture: Texture2D | null = null; + private _velocityTexture: Texture2D | null = null; + private _rotationTexture: Texture2D | null = null; + private _sizeTexture: Texture2D | null = null; + private _animTexture: Texture2D | null = null; + private _uTimeHandle: number = 0; + private _uRotHandle: number = 0; + + constructor (info: any) { + super(info); + + this._frameTile_velLenScale = new Vec4(1, 1, 0, 0); + this._node_scale = new Vec4(); + this._defines = { + CC_USE_WORLD_SPACE: true, + CC_USE_BILLBOARD: true, + CC_USE_STRETCHED_BILLBOARD: false, + CC_USE_HORIZONTAL_BILLBOARD: false, + CC_USE_VERTICAL_BILLBOARD: false, + COLOR_OVER_TIME_MODULE_ENABLE: false, + }; + + this._tempParticle = new Particle(null); + this._particleNum = 0; + } + + public onInit (ps: Component) { + super.onInit(ps); + this._setVertexAttrib(); + this._updateModel(); + this._model!.constructAttributeIndex(); + this.updateMaterialParams(); + } + + public updateRenderMode () { + this._setVertexAttrib(); + this._updateModel(); + this.updateMaterialParams(); + } + + public clear () { + this._particleNum = 0; + this.updateRenderData(); + } + + public onDestroy () { + super.onDestroy(); + this._forceTexture && this._forceTexture.destroy(); + this._velocityTexture && this._velocityTexture.destroy(); + this._colorTexture && this._colorTexture.destroy(); + this._sizeTexture && this._sizeTexture.destroy(); + this._rotationTexture && this._rotationTexture.destroy(); + this._animTexture && this._animTexture.destroy(); + } + + public enableModule (name: string, val: Boolean, pm: IParticleModule) { + const mat: Material | null = this._particleSystem.getMaterialInstance(0) || this._defaultMat; + if (!mat) { + return; + } + this.initShaderUniform(mat); + mat.recompileShaders(this._defines); + if (this._model) { + this._model.setSubModelMaterial(0, mat); + } + } + + public getFreeParticle (): Particle | null { + if (this._particleNum >= this._particleSystem._capacity) { + return null; + } + + return this._tempParticle; + } + + public setNewParticle (p: Particle) { + this._model!.addGPUParticleVertexData(p, this._particleNum, this._particleSystem._time); + this._particleNum++; + } + + public updateParticles (dt: number) { + if (EDITOR) { + const mat: Material | null = this._particleSystem.getMaterialInstance(0) || this._defaultMat; + + this._particleSystem.node.getWorldMatrix(_tempWorldTrans); + switch (this._particleSystem.scaleSpace) { + case Space.Local: + this._particleSystem.node.getScale(this._node_scale); + break; + case Space.World: + this._particleSystem.node.getWorldScale(this._node_scale); + break; + } + + this.initShaderUniform(mat!); + } + this._particleNum = this._model!.updateGPUParticles(this._particleNum, this._particleSystem._time); + this.updateShaderUniform(dt); + + return this._particleNum; + } + + // internal function + public updateRenderData () { + // update vertex buffer + this._model!.updateIA(this._particleNum); + } + + public updateShaderUniform (dt: number) { + const mat: Material | null = this._particleSystem.getMaterialInstance(0) || this._defaultMat; + if (!mat) { + return; + } + + const pass = mat.passes[0]; + _tempVec4.x = this._particleSystem._time; + _tempVec4.y = dt; + pass.setUniform(this._uTimeHandle, _tempVec4); + + this._particleSystem.node.getWorldRotation(_world_rot); + pass.setUniform(this._uRotHandle, _world_rot); + } + + public initShaderUniform (mat: Material) { + const pass = mat.passes[0]; + + this._uTimeHandle = pass.getHandle('u_timeDelta')!; + this._uRotHandle = pass.getHandle('u_worldRot')!; + + pass.setUniform(pass.getHandle('scale')!, this._node_scale); + pass.setUniform(pass.getHandle('frameTile_velLenScale')!, this._frameTile_velLenScale); + + // force + const forceModule = this._particleSystem.forceOvertimeModule; + this._defines[FORCE_OVER_TIME_MODULE_ENABLE] = forceModule.enable; + if (forceModule.enable) { + this._forceTexture && this._forceTexture.destroy(); + this._forceTexture = packCurveRangeXYZ(_sample_num, forceModule.x, forceModule.y, forceModule.z); + const handle = pass.getHandle('force_over_time_tex0'); + const binding = Pass.getBindingFromHandle(handle!); + pass.bindTextureView(binding, this._forceTexture.getGFXTextureView()!); + const spaceHandle = pass.getHandle('u_force_space'); + pass.setUniform(spaceHandle!, forceModule.space); + const modeHandle = pass.getHandle('u_force_mode'); + pass.setUniform(modeHandle!, this._forceTexture.height); + } + + // velocity + const velocityModule = this._particleSystem.velocityOvertimeModule; + this._defines[VELOCITY_OVER_TIME_MODULE_ENABLE] = velocityModule.enable; + if (velocityModule.enable) { + this._velocityTexture && this._velocityTexture.destroy(); + this._velocityTexture = packCurveRangeXYZW(_sample_num, velocityModule.x, velocityModule.y, velocityModule.z, velocityModule.speedModifier); + const handle = pass.getHandle('velocity_over_time_tex0'); + const binding = Pass.getBindingFromHandle(handle!); + pass.bindTextureView(binding, this._velocityTexture.getGFXTextureView()!); + const spaceHandle = pass.getHandle('u_velocity_space'); + pass.setUniform(spaceHandle!, velocityModule.space); + const modeHandle = pass.getHandle('u_velocity_mode'); + pass.setUniform(modeHandle!, this._velocityTexture.height); + } + + // color module + const colorModule = this._particleSystem.colorOverLifetimeModule; + this._defines[COLOR_OVER_TIME_MODULE_ENABLE] = colorModule.enable; + if (colorModule.enable) { + this._colorTexture && this._colorTexture.destroy(); + this._colorTexture = packGradientRange(_sample_num, colorModule.color); + const handle = pass.getHandle('color_over_time_tex0'); + const binding = Pass.getBindingFromHandle(handle!); + pass.bindTextureView(binding, this._colorTexture.getGFXTextureView()!); + const modeHandle = pass.getHandle('u_color_mode'); + pass.setUniform(modeHandle!, this._colorTexture.height); + } + + // rotation module + const roationModule = this._particleSystem.rotationOvertimeModule; + this._defines[ROTATION_OVER_TIME_MODULE_ENABLE] = roationModule.enable; + if (roationModule.enable) { + this._rotationTexture && this._rotationTexture.destroy(); + if (roationModule.separateAxes) { + this._rotationTexture = packCurveRangeXYZ(_sample_num, roationModule.x, roationModule.y, roationModule.z); + } else { + this._rotationTexture = packCurveRangeZ(_sample_num, roationModule.z); + } + const handle = pass.getHandle('rotation_over_time_tex0'); + const binding = Pass.getBindingFromHandle(handle!); + pass.bindTextureView(binding, this._rotationTexture.getGFXTextureView()!); + const modeHandle = pass.getHandle('u_rotation_mode'); + pass.setUniform(modeHandle!, this._rotationTexture.height); + } + + // size module + const sizeModule = this._particleSystem.sizeOvertimeModule; + this._defines[SIZE_OVER_TIME_MODULE_ENABLE] = sizeModule.enable; + if (sizeModule.enable) { + this._sizeTexture && this._sizeTexture.destroy(); + if (sizeModule.separateAxes) { + this._sizeTexture = packCurveRangeXYZ(_sample_num, sizeModule.x, sizeModule.y, sizeModule.z); + } else { + this._sizeTexture = packCurveRangeN(_sample_num, sizeModule.size); + } + const handle = pass.getHandle('size_over_time_tex0'); + const binding = Pass.getBindingFromHandle(handle!); + pass.bindTextureView(binding, this._sizeTexture.getGFXTextureView()!); + const modeHandle = pass.getHandle('u_size_mode'); + pass.setUniform(modeHandle!, this._sizeTexture.height); + } + + // texture module + const textureModule = this._particleSystem.textureAnimationModule; + this._defines[TEXTURE_ANIMATION_MODULE_ENABLE] = textureModule.enable; + if (textureModule.enable) { + this._animTexture && this._animTexture.destroy(); + this._animTexture = packCurveRangeXY(_sample_num, textureModule.startFrame, textureModule.frameOverTime); + + const handle = pass.getHandle('texture_animation_tex0'); + const binding = Pass.getBindingFromHandle(handle!); + pass.bindTextureView(binding, this._animTexture.getGFXTextureView()!); + const infoHandle = pass.getHandle('u_anim_info'); + _tempVec4.x = this._animTexture.height; + _tempVec4.y = textureModule.numTilesX * textureModule.numTilesY; + _tempVec4.z = textureModule.cycleCount; + pass.setUniform(infoHandle!, _tempVec4); + } + } + + public getParticleCount (): number { + return this._particleNum; + } + + public onMaterialModified (index: number, material: Material) { + this._updateModel(); + this.updateMaterialParams(); + } + + public onRebuildPSO (index: number, material: Material) { + if (this._model && index === 0) { + this._model.setSubModelMaterial(0, material); + } + } + + private _setVertexAttrib () { + switch (this._renderInfo!.renderMode) { + case RenderMode.StrecthedBillboard: + this._vertAttrs = _gpu_vert_attr.slice(); + break; + case RenderMode.Mesh: + this._vertAttrs = _gpu_vert_attr_mesh.slice(); + break; + default: + this._vertAttrs = _gpu_vert_attr.slice(); + } + } + + public updateMaterialParams () { + if (!this._particleSystem) { + return; + } + if (this._particleSystem.sharedMaterial != null && this._particleSystem.sharedMaterial._effectAsset._name.indexOf('particle-gpu') === -1) { + // reset material + this._particleSystem.setMaterial(null, 0); + } + if (this._particleSystem.sharedMaterial == null && this._defaultMat == null) { + _matInsInfo.parent = builtinResMgr.get('default-particle-gpu-material'); + _matInsInfo.owner = this._particleSystem; + _matInsInfo.subModelIdx = 0; + this._defaultMat = new MaterialInstance(_matInsInfo); + } + const mat: Material | null = this._particleSystem.getMaterialInstance(0) || this._defaultMat; + + this._particleSystem.node.getWorldMatrix(_tempWorldTrans); + switch (this._particleSystem.scaleSpace) { + case Space.Local: + this._particleSystem.node.getScale(this._node_scale); + break; + case Space.World: + this._particleSystem.node.getWorldScale(this._node_scale); + break; + } + + if (this._particleSystem._simulationSpace === Space.World) { + this._defines[CC_USE_WORLD_SPACE] = true; + } else { + this._defines[CC_USE_WORLD_SPACE] = false; + } + const renderMode = this._renderInfo!.renderMode; + if (renderMode === RenderMode.Billboard) { + this._defines[CC_RENDER_MODE] = RENDER_MODE_BILLBOARD; + } else if (renderMode === RenderMode.StrecthedBillboard) { + this._defines[CC_RENDER_MODE] = RENDER_MODE_STRETCHED_BILLBOARD; + this._frameTile_velLenScale.z = this._renderInfo!.velocityScale; + this._frameTile_velLenScale.w = this._renderInfo!.lengthScale; + } else if (renderMode === RenderMode.HorizontalBillboard) { + this._defines[CC_RENDER_MODE] = RENDER_MODE_HORIZONTAL_BILLBOARD; + } else if (renderMode === RenderMode.VerticalBillboard) { + this._defines[CC_RENDER_MODE] = RENDER_MODE_VERTICAL_BILLBOARD; + } else if (renderMode === RenderMode.Mesh) { + this._defines[CC_RENDER_MODE] = RENDER_MODE_MESH; + } else { + console.warn(`particle system renderMode ${renderMode} not support.`); + } + + if (this._particleSystem.textureAnimationModule.enable) { + Vec2.set(this._frameTile_velLenScale, this._particleSystem.textureAnimationModule.numTilesX, this._particleSystem.textureAnimationModule.numTilesY); + } + + this.initShaderUniform(mat!); + + mat!.recompileShaders(this._defines); + + if (this._model) { + this._model.setSubModelMaterial(0, mat); + } + } +} \ No newline at end of file diff --git a/cocos/particle/renderer/particle-system-renderer.ts b/cocos/particle/renderer/particle-system-renderer.ts deleted file mode 100644 index c2209360bb..0000000000 --- a/cocos/particle/renderer/particle-system-renderer.ts +++ /dev/null @@ -1,543 +0,0 @@ -import { builtinResMgr } from '../../core/3d/builtin'; -import { Material, Mesh } from '../../core/assets'; -import { Component } from '../../core/components'; -import { ccclass, property } from '../../core/data/class-decorator'; -import { GFXAttributeName, GFXFormat } from '../../core/gfx/define'; -import { IGFXAttribute } from '../../core/gfx/input-assembler'; -import { Mat4, Vec2, Vec3, Vec4 } from '../../core/math'; -import { RecyclePool } from '../../core/memop'; -import { MaterialInstance, IMaterialInstanceInfo } from '../../core/renderer/core/material-instance'; -import { IDefineMap } from '../../core/renderer/core/pass-utils'; -import { RenderMode, Space } from '../enum'; -import ParticleBatchModel from '../models/particle-batch-model'; -import Particle from '../particle'; - -const _tempAttribUV = new Vec3(); -const _tempAttribUV0 = new Vec2(); -const _tempWorldTrans = new Mat4(); - -const _uvs = [ - 0, 0, // bottom-left - 1, 0, // bottom-right - 0, 1, // top-left - 1, 1, // top-right -]; - -const CC_USE_WORLD_SPACE = 'CC_USE_WORLD_SPACE'; - -const CC_RENDER_MODE = 'CC_RENDER_MODE'; -const RENDER_MODE_BILLBOARD = 0; -const RENDER_MODE_STRETCHED_BILLBOARD = 1; -const RENDER_MODE_HORIZONTAL_BILLBOARD = 2; -const RENDER_MODE_VERTICAL_BILLBOARD = 3; -const RENDER_MODE_MESH = 4; - -const _vertex_attrs = [ - { name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // position - { name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, // uv,frame idx - { name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RGB32F }, // size - { name: GFXAttributeName.ATTR_TEX_COORD2, format: GFXFormat.RGB32F }, // rotation - { name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true }, // color -]; - -const _vertex_attrs_stretch = [ - { name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // position - { name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, // uv,frame idx - { name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RGB32F }, // size - { name: GFXAttributeName.ATTR_TEX_COORD2, format: GFXFormat.RGB32F }, // rotation - { name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true }, // color - { name: GFXAttributeName.ATTR_COLOR1, format: GFXFormat.RGB32F }, // particle velocity -]; - -const _vertex_attrs_mesh = [ - { name: GFXAttributeName.ATTR_POSITION, format: GFXFormat.RGB32F }, // particle position - { name: GFXAttributeName.ATTR_TEX_COORD, format: GFXFormat.RGB32F }, // uv,frame idx - { name: GFXAttributeName.ATTR_TEX_COORD1, format: GFXFormat.RGB32F }, // size - { name: GFXAttributeName.ATTR_TEX_COORD2, format: GFXFormat.RGB32F }, // rotation - { name: GFXAttributeName.ATTR_COLOR, format: GFXFormat.RGBA8, isNormalized: true }, // particle color - { name: GFXAttributeName.ATTR_TEX_COORD3, format: GFXFormat.RGB32F }, // mesh position - { name: GFXAttributeName.ATTR_NORMAL, format: GFXFormat.RGB32F }, // mesh normal - { name: GFXAttributeName.ATTR_COLOR1, format: GFXFormat.RGBA8, isNormalized: true }, // mesh color -]; - -const _matInsInfo: IMaterialInstanceInfo = { - parent: null!, - owner: null!, - subModelIdx: 0, -}; - -@ccclass('cc.ParticleSystemRenderer') -export default class ParticleSystemRenderer { - - /** - * @zh 设定粒子生成模式。 - */ - @property({ - type: RenderMode, - displayOrder: 0, - tooltip: '设定粒子生成模式', - }) - public get renderMode () { - return this._renderMode; - } - - public set renderMode (val) { - if (this._renderMode === val) { - return; - } - this._renderMode = val; - this._setVertexAttrib(); - this._updateModel(); - this._updateMaterialParams(); - } - - /** - * @zh 在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按速度大小进行拉伸。 - */ - @property({ - displayOrder: 1, - tooltip: '在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按速度大小进行拉伸', - }) - public get velocityScale () { - return this._velocityScale; - } - - public set velocityScale (val) { - this._velocityScale = val; - this._updateMaterialParams(); - // this._updateModel(); - } - - /** - * @zh 在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按粒子大小进行拉伸。 - */ - @property({ - displayOrder: 2, - tooltip: '在粒子生成方式为 StrecthedBillboard 时,对粒子在运动方向上按粒子大小进行拉伸', - }) - public get lengthScale () { - return this._lengthScale; - } - - public set lengthScale (val) { - this._lengthScale = val; - this._updateMaterialParams(); - // this._updateModel(); - } - - @property({ - type: RenderMode, - displayOrder: 3, - }) - private _renderMode = RenderMode.Billboard; - - @property({ - displayOrder: 4, - }) - private _velocityScale = 1; - - @property({ - displayOrder: 5, - }) - private _lengthScale = 1; - - @property({ - displayOrder: 6, - }) - private _mesh: Mesh | null = null; - - @property - private _particleSystem: any = null; - - /** - * @zh 粒子发射的模型。 - */ - @property({ - type: Mesh, - displayOrder: 7, - tooltip: '粒子发射的模型', - }) - public get mesh () { - return this._mesh; - } - - public set mesh (val) { - this._mesh = val; - if (this._model) { - this._model.setVertexAttributes(this._renderMode === RenderMode.Mesh ? this._mesh : null, this._vertAttrs); - } - } - - /** - * @zh 粒子使用的材质。 - */ - @property({ - type: Material, - displayOrder: 8, - tooltip: '粒子使用的材质', - }) - public get particleMaterial () { - if (!this._particleSystem) { - return null; - } - return this._particleSystem.getMaterial(0); - } - - public set particleMaterial (val) { - this._particleSystem.setMaterial(val, 0); - } - - /** - * @zh 拖尾使用的材质。 - */ - @property({ - type: Material, - displayOrder: 9, - tooltip: '拖尾使用的材质', - }) - public get trailMaterial () { - if (!this._particleSystem) { - return null; - } - return this._particleSystem.getMaterial(1)!; - } - - public set trailMaterial (val) { - this._particleSystem.setMaterial(val, 1); - } - - private _defines: IDefineMap; - private _trailDefines: IDefineMap; - private _model: ParticleBatchModel | null; - private frameTile_velLenScale: Vec4; - private _node_scale: Vec4; - private attrs: any[]; - private _vertAttrs: IGFXAttribute[] = []; - private _particles: RecyclePool | null = null; - private _defaultMat: Material | null = null; - private _defaultTrailMat: Material | null = null; - - constructor () { - this._model = null; - - this.frameTile_velLenScale = new Vec4(1, 1, 0, 0); - this._node_scale = new Vec4(); - this.attrs = new Array(5); - this._defines = { - CC_USE_WORLD_SPACE: true, - CC_USE_BILLBOARD: true, - CC_USE_STRETCHED_BILLBOARD: false, - CC_USE_HORIZONTAL_BILLBOARD: false, - CC_USE_VERTICAL_BILLBOARD: false, - }; - this._trailDefines = { - CC_USE_WORLD_SPACE: true, - // CC_DRAW_WIRE_FRAME: true, // - }; - } - - public onInit (ps: Component) { - this._particleSystem = ps; - this._particles = new RecyclePool(() => { - return new Particle(this); - }, 16); - this._setVertexAttrib(); - this._updateModel(); - this._updateMaterialParams(); - this._updateTrailMaterial(); - } - - public onEnable () { - if (!this._particleSystem) { - return; - } - this._attachToScene(); - this._model!.initialize(this._particleSystem.node); - this._model!.enabled = this._particleSystem.enabledInHierarchy; - } - - public onDisable () { - this._detachFromScene(); - } - - public onDestroy () { - if (this._model) { - cc.director.root.destroyModel(this._model); - this._model = null; - } - } - - public _attachToScene () { - if (this._model) { - if (this._model.scene) { - this._detachFromScene(); - } - this._particleSystem._getRenderScene().addModel(this._model); - } - } - - public _detachFromScene () { - if (this._model && this._model.scene) { - this._model.scene.removeModel(this._model); - } - } - - public clear () { - this._particles!.reset(); - this._updateRenderData(); - } - - public _getFreeParticle (): Particle | null { - if (this._particles!.length >= this._particleSystem.capacity) { - return null; - } - return this._particles!.add(); - } - - public _setNewParticle (p: Particle) { - - } - - public _updateParticles (dt: number) { - this._particleSystem.node.getWorldMatrix(_tempWorldTrans); - switch (this._particleSystem.scaleSpace) { - case Space.Local: - this._particleSystem.node.getScale(this._node_scale); - break; - case Space.World: - this._particleSystem.node.getWorldScale(this._node_scale); - break; - } - const mat: Material | null = this._particleSystem.getMaterialInstance(0) || this._defaultMat; - mat!.setProperty('scale', this._node_scale); - if (this._particleSystem.velocityOvertimeModule.enable) { - this._particleSystem.velocityOvertimeModule.update(this._particleSystem._simulationSpace, _tempWorldTrans); - } - if (this._particleSystem.limitVelocityOvertimeModule.enable) { - this._particleSystem.limitVelocityOvertimeModule.update(this._particleSystem._simulationSpace, _tempWorldTrans); - } - if (this._particleSystem.forceOvertimeModule.enable) { - this._particleSystem.forceOvertimeModule.update(this._particleSystem._simulationSpace, _tempWorldTrans); - } - if (this._particleSystem.trailModule.enable) { - this._particleSystem.trailModule.update(); - } - for (let i = 0; i < this._particles!.length; ++i) { - const p = this._particles!.data[i]; - p.remainingLifetime -= dt; - Vec3.set(p.animatedVelocity, 0, 0, 0); - - if (p.remainingLifetime < 0.0) { - if (this._particleSystem.trailModule.enable) { - this._particleSystem.trailModule.removeParticle(p); - } - this._particles!.removeAt(i); - --i; - continue; - } - - // apply gravity. - p.velocity.y -= this._particleSystem.gravityModifier.evaluate(1 - p.remainingLifetime / p.startLifetime, p.randomSeed)! * 9.8 * dt; - if (this._particleSystem.sizeOvertimeModule.enable) { - this._particleSystem.sizeOvertimeModule.animate(p); - } - if (this._particleSystem.colorOverLifetimeModule.enable) { - this._particleSystem.colorOverLifetimeModule.animate(p); - } - if (this._particleSystem.forceOvertimeModule.enable) { - this._particleSystem.forceOvertimeModule.animate(p, dt); - } - if (this._particleSystem.velocityOvertimeModule.enable) { - this._particleSystem.velocityOvertimeModule.animate(p); - } else { - Vec3.copy(p.ultimateVelocity, p.velocity); - } - if (this._particleSystem.limitVelocityOvertimeModule.enable) { - this._particleSystem.limitVelocityOvertimeModule.animate(p); - } - if (this._particleSystem.rotationOvertimeModule.enable) { - this._particleSystem.rotationOvertimeModule.animate(p, dt); - } - if (this._particleSystem.textureAnimationModule.enable) { - this._particleSystem.textureAnimationModule.animate(p); - } - Vec3.scaleAndAdd(p.position, p.position, p.ultimateVelocity, dt); // apply velocity. - if (this._particleSystem.trailModule.enable) { - this._particleSystem.trailModule.animate(p, dt); - } - } - return this._particles!.length; - } - - // internal function - public _updateRenderData () { - // update vertex buffer - let idx = 0; - const uploadVel = this._renderMode === RenderMode.StrecthedBillboard; - for (let i = 0; i < this._particles!.length; ++i) { - const p = this._particles!.data[i]; - let fi = 0; - if (this._particleSystem.textureAnimationModule.enable) { - fi = p.frameIndex; - } - idx = i * 4; - let attrNum = 0; - if (this._renderMode !== RenderMode.Mesh) { - for (let j = 0; j < 4; ++j) { // four verts per particle. - attrNum = 0; - this.attrs[attrNum++] = p.position; - _tempAttribUV.x = _uvs[2 * j]; - _tempAttribUV.y = _uvs[2 * j + 1]; - _tempAttribUV.z = fi; - this.attrs[attrNum++] = _tempAttribUV; - this.attrs[attrNum++] = p.size; - this.attrs[attrNum++] = p.rotation; - this.attrs[attrNum++] = p.color._val; - - if (uploadVel) { - this.attrs[attrNum++] = p.ultimateVelocity; - } else { - this.attrs[attrNum++] = null; - } - - this._model!.addParticleVertexData(idx++, this.attrs); - } - } else { - attrNum = 0; - this.attrs[attrNum++] = p.position; - _tempAttribUV.z = fi; - this.attrs[attrNum++] = _tempAttribUV; - this.attrs[attrNum++] = p.size; - this.attrs[attrNum++] = p.rotation; - this.attrs[attrNum++] = p.color._val; - this._model!.addParticleVertexData(i, this.attrs); - } - } - - // because we use index buffer, per particle index count = 6. - this._model!.updateIA(this._particles!.length); - } - - public updateShaderUniform () { - - } - - public getParticleCount (): number { - return this._particles!.length; - } - - public _onMaterialModified (index: number, material: Material) { - if (index === 0) { - this._updateModel(); - this._updateMaterialParams(); - } else { - this._updateTrailMaterial(); - } - } - - public _onRebuildPSO (index: number, material: Material) { - if (this._model && index === 0) { - this._model.setSubModelMaterial(0, material); - } - if (this._particleSystem.trailModule._trailModel && index === 1) { - this._particleSystem.trailModule._trailModel.setSubModelMaterial(0, material); - } - } - - private _setVertexAttrib () { - switch (this._renderMode) { - case RenderMode.StrecthedBillboard: - this._vertAttrs = _vertex_attrs_stretch.slice(); - break; - case RenderMode.Mesh: - this._vertAttrs = _vertex_attrs_mesh.slice(); - break; - default: - this._vertAttrs = _vertex_attrs.slice(); - } - } - - private _updateMaterialParams () { - if (!this._particleSystem) { - return; - } - if (this._particleSystem.sharedMaterial != null && this._particleSystem.sharedMaterial._effectAsset._name.indexOf('particle') === -1) { - this._particleSystem.setMaterial(null, 0); - } - if (this._particleSystem.sharedMaterial == null && this._defaultMat == null) { - _matInsInfo.parent = builtinResMgr.get('default-particle-material'); - _matInsInfo.owner = this._particleSystem; - _matInsInfo.subModelIdx = 0; - this._defaultMat = new MaterialInstance(_matInsInfo); - } - const mat: Material | null = this._particleSystem.getMaterialInstance(0) || this._defaultMat; - if (this._particleSystem._simulationSpace === Space.World) { - this._defines[CC_USE_WORLD_SPACE] = true; - } else { - this._defines[CC_USE_WORLD_SPACE] = false; - } - - if (this._renderMode === RenderMode.Billboard) { - this._defines[CC_RENDER_MODE] = RENDER_MODE_BILLBOARD; - } else if (this._renderMode === RenderMode.StrecthedBillboard) { - this._defines[CC_RENDER_MODE] = RENDER_MODE_STRETCHED_BILLBOARD; - this.frameTile_velLenScale.z = this._velocityScale; - this.frameTile_velLenScale.w = this._lengthScale; - } else if (this._renderMode === RenderMode.HorizontalBillboard) { - this._defines[CC_RENDER_MODE] = RENDER_MODE_HORIZONTAL_BILLBOARD; - } else if (this._renderMode === RenderMode.VerticalBillboard) { - this._defines[CC_RENDER_MODE] = RENDER_MODE_VERTICAL_BILLBOARD; - } else if (this._renderMode === RenderMode.Mesh) { - this._defines[CC_RENDER_MODE] = RENDER_MODE_MESH; - } else { - console.warn(`particle system renderMode ${this._renderMode} not support.`); - } - - if (this._particleSystem.textureAnimationModule.enable) { - Vec2.set(this.frameTile_velLenScale, this._particleSystem.textureAnimationModule.numTilesX, this._particleSystem.textureAnimationModule.numTilesY); - mat!.setProperty('frameTile_velLenScale', this.frameTile_velLenScale); - } else { - mat!.setProperty('frameTile_velLenScale', this.frameTile_velLenScale); - } - mat!.recompileShaders(this._defines); - if (this._model) { - this._model.setSubModelMaterial(0, mat); - } - } - - private _updateTrailMaterial () { - if (this._particleSystem.trailModule.enable) { - if (this._particleSystem._simulationSpace === Space.World || this._particleSystem.trailModule.space === Space.World) { - this._trailDefines[CC_USE_WORLD_SPACE] = true; - } else { - this._trailDefines[CC_USE_WORLD_SPACE] = false; - } - let mat = this._particleSystem.getMaterialInstance(1); - if (mat === null && this._defaultTrailMat === null) { - _matInsInfo.parent = builtinResMgr.get('default-trail-material'); - _matInsInfo.owner = this._particleSystem; - _matInsInfo.subModelIdx = 1; - this._defaultTrailMat = new MaterialInstance(_matInsInfo); - } - if (mat === null) { - mat = this._defaultTrailMat; - } - mat!.recompileShaders(this._trailDefines); - this._particleSystem.trailModule._updateMaterial(); - } - } - - private _updateModel () { - if (!this._model) { - this._model = cc.director.root.createModel(ParticleBatchModel); - this._model!.setCapacity(this._particleSystem.capacity); - this._model!.visFlags = this._particleSystem.visibility; - } - this._model!.setVertexAttributes(this._renderMode === RenderMode.Mesh ? this._mesh : null, this._vertAttrs); - // if (Object.getPrototypeOf(this).constructor.name === 'ParticleSystemGpuRenderer') { - // return; - // } - } -} - -Object.assign(ParticleSystemRenderer, { uv: _uvs }); diff --git a/cocos/particle/renderer/trail.ts b/cocos/particle/renderer/trail.ts index 84c21f9c15..f766ec8c08 100644 --- a/cocos/particle/renderer/trail.ts +++ b/cocos/particle/renderer/trail.ts @@ -17,7 +17,7 @@ import { Model } from '../../core/renderer'; import CurveRange from '../animator/curve-range'; import GradientRange from '../animator/gradient-range'; import { Space, TextureMode, TrailMode } from '../enum'; -import Particle from '../particle'; +import { Particle } from '../particle'; // tslint:disable: max-line-length const PRE_TRIANGLE_INDEX = 1; @@ -162,7 +162,7 @@ export default class TrailModule { } if (val && !this._enable) { this._enable = val; - this._particleSystem.renderer._updateTrailMaterial(); + this._particleSystem.processor.updateTrailMaterial(); } this._enable = val; if (this._trailModel) { @@ -224,7 +224,7 @@ export default class TrailModule { public set space (val) { this._space = val; if (this._particleSystem) { - this._particleSystem.renderer._updateTrailMaterial(); + this._particleSystem.processor.updateTrailMaterial(); } } @@ -410,7 +410,7 @@ export default class TrailModule { if (mat) { this._trailModel.setSubModelMaterial(0, mat); } else { - this._trailModel.setSubModelMaterial(0, this._particleSystem.renderer._defaultTrailMat); + this._trailModel.setSubModelMaterial(0, this._particleSystem.processor._defaultTrailMat); } } }