diff --git a/cocos/3d/builtin/effects.js b/cocos/3d/builtin/effects.js index 6ba12043c1..a1fe9d561a 100644 --- a/cocos/3d/builtin/effects.js +++ b/cocos/3d/builtin/effects.js @@ -110,16 +110,16 @@ export default [ "shaders": [ { "name": "builtin-standard|standard-vs:vert|standard-fs:frag", - "hash": 3245055239, + "hash": 1715009002, "glsl3": { - "vert": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n vec4 cc_nativeSize; \n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n vec4 cc_exposure; \n vec4 cc_mainLitDir; \n vec4 cc_mainLitColor; \n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\nuniform Constants {\n vec4 tilingOffset;\n vec4 albedo;\n vec4 albedoScale; \n vec4 pbrParams; \n vec4 pbrScale; \n vec4 emissive;\n vec4 emissiveScale;\n};\n#if CC_USE_SKINNING\nin vec4 a_weights;\nin vec4 a_joints;\nuniform CCSkinning {\n vec4 cc_matJoint[65 * 3];\n vec4 cc_jointsTextureSize;\n};\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\n#if CC_USE_JOINTS_TEXTURE_RGBA8888\nfloat decode32(vec4 rgba) {\n float Sign = 1.0 - step(128.0,rgba[0])*2.0;\n float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0;\n if (Exponent == -127.0) {\n return 0.0;\n }\n float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000);\n return Sign * exp2(Exponent - 23.0) * Mantissa;\n}\nvec4 decodevec4 (vec4 x, vec4 y, vec4 z, vec4 w) {\n return vec4(\n decode32(x * 255.0),\n decode32(y * 255.0),\n decode32(z * 255.0),\n decode32(w * 255.0)\n );\n}\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 16.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 0.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 1.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 2.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 3.5), y))\n );\n vec4 v2 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 4.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 5.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 6.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 7.5), y))\n );\n vec4 v3 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 8.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 9.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 10.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 11.5), y))\n );\n vec4 v4 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 12.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 13.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 14.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 15.5), y))\n );\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#endif\n#else\nmat4 getBoneMatrix(const in float i) {\n vec4 _0 = cc_matJoint[3 * int(i) + 0];\n vec4 _1 = cc_matJoint[3 * int(i) + 1];\n vec4 _2 = cc_matJoint[3 * int(i) + 2];\n return mat4(\n vec4(_0.x, _0.y, _0.z, 0),\n vec4(_0.w, _1.x, _1.y, 0),\n vec4(_1.z, _1.w, _2.x, 0),\n vec4(_2.y, _2.z, _2.w, 1)\n );\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nin vec3 a_position;\nin vec3 a_normal;\nin vec4 a_tangent;\nout vec3 v_worldPos;\nout vec3 v_worldNormal;\n#if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP\n in vec2 a_texCoord;\n out vec2 v_uv;\n#endif\n#if USE_NORMAL_MAP\n out vec3 v_tangent;\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1.0);\n vec4 normal = vec4(a_normal, 0.0);\n #if CC_USE_SKINNING\n skinVertex(pos, normal);\n #endif\n v_worldPos = (cc_matWorld * pos).xyz;\n v_worldNormal = (cc_matWorldIT * normal).xyz;\n #if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP\n v_uv = a_texCoord * tilingOffset.xy + tilingOffset.zw;\n #endif\n #if USE_NORMAL_MAP\n v_tangent = (cc_matWorldIT * vec4(a_tangent.xyz, 0.0)).xyz;\n #endif\n return cc_matViewProj * cc_matWorld * pos;\n}\nvoid main() { gl_Position = vert(); }\n`, + "vert": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n vec4 cc_nativeSize; \n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n vec4 cc_exposure; \n vec4 cc_mainLitDir; \n vec4 cc_mainLitColor; \n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\nuniform Constants {\n vec4 tilingOffset;\n vec4 albedo;\n vec4 albedoScale; \n vec4 pbrParams; \n vec4 pbrScale; \n vec4 emissive;\n vec4 emissiveScale;\n};\n#if CC_USE_SKINNING\nin vec4 a_weights;\nin vec4 a_joints;\n#if CC_USE_JOINTS_TEXTURE\n \n uniform CCSkinningTextureCase {\n vec4 cc_jointsTextureSize;\n };\n#else\n \n uniform CCSkinning {\n vec4 cc_matJoint[65 * 3];\n };\n#endif\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\n#if CC_USE_JOINTS_TEXTURE_RGBA8888\nfloat decode32(vec4 rgba) {\n float Sign = 1.0 - step(128.0,rgba[0])*2.0;\n float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0;\n if (Exponent == -127.0) {\n return 0.0;\n }\n float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000);\n return Sign * exp2(Exponent - 23.0) * Mantissa;\n}\nvec4 decodevec4 (vec4 x, vec4 y, vec4 z, vec4 w) {\n return vec4(\n decode32(x * 255.0),\n decode32(y * 255.0),\n decode32(z * 255.0),\n decode32(w * 255.0)\n );\n}\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 16.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 0.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 1.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 2.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 3.5), y))\n );\n vec4 v2 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 4.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 5.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 6.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 7.5), y))\n );\n vec4 v3 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 8.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 9.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 10.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 11.5), y))\n );\n vec4 v4 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 12.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 13.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 14.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 15.5), y))\n );\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x * 2.0 + 1.0) * getBoneMatrix(a_joints.x * 2.0 + 0.0) * a_weights.x +\n getBoneMatrix(a_joints.y * 2.0 + 1.0) * getBoneMatrix(a_joints.y * 2.0 + 0.0) * a_weights.y +\n getBoneMatrix(a_joints.z * 2.0 + 1.0) * getBoneMatrix(a_joints.z * 2.0 + 0.0) * a_weights.z +\n getBoneMatrix(a_joints.w * 2.0 + 1.0) * getBoneMatrix(a_joints.w * 2.0 + 0.0) * a_weights.w;\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n vec4 _0 = cc_matJoint[3 * int(i) + 0];\n vec4 _1 = cc_matJoint[3 * int(i) + 1];\n vec4 _2 = cc_matJoint[3 * int(i) + 2];\n return mat4(\n vec4(_0.x, _0.y, _0.z, 0),\n vec4(_0.w, _1.x, _1.y, 0),\n vec4(_1.z, _1.w, _2.x, 0),\n vec4(_2.y, _2.z, _2.w, 1)\n );\n}\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\n#endif\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nin vec3 a_position;\nin vec3 a_normal;\nin vec4 a_tangent;\nout vec3 v_worldPos;\nout vec3 v_worldNormal;\n#if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP\n in vec2 a_texCoord;\n out vec2 v_uv;\n#endif\n#if USE_NORMAL_MAP\n out vec3 v_tangent;\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1.0);\n vec4 normal = vec4(a_normal, 0.0);\n #if CC_USE_SKINNING\n skinVertex(pos, normal);\n #else\n pos = cc_matWorld * pos;\n normal = cc_matWorldIT * normal;\n #endif\n v_worldPos = pos.xyz;\n v_worldNormal = normal.xyz;\n #if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP\n v_uv = a_texCoord * tilingOffset.xy + tilingOffset.zw;\n #endif\n #if USE_NORMAL_MAP\n v_tangent = (cc_matWorldIT * vec4(a_tangent.xyz, 0.0)).xyz;\n #endif\n return cc_matViewProj * pos;\n}\nvoid main() { gl_Position = vert(); }\n`, "frag": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n vec4 cc_nativeSize; \n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n vec4 cc_exposure; \n vec4 cc_mainLitDir; \n vec4 cc_mainLitColor; \n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\n#define MAX_LIGHTS 2\nuniform CCForwardLight {\n vec4 cc_sphereLitPos[2]; \n vec4 cc_sphereLitSizeRange[2]; \n vec4 cc_sphereLitColor[2]; \n vec4 cc_spotLitPos[2]; \n vec4 cc_spotLitSizeRangeAngle[2]; \n vec4 cc_spotLitDir[2]; \n vec4 cc_spotLitColor[2]; \n};\n#define PI 3.14159265359\n#define PI2 6.28318530718\n#define EPSILON 1e-6\n#define LOG2 1.442695\n#define saturate(a) clamp( a, 0.0, 1.0 )\n#define FP_MAX 65504.0\n#define FP_SCALE 0.0009765625\n#define FP_SCALE_INV 1024.0\nvec3 SRGBToLinear(vec3 gamma)\n{\n return pow(gamma, vec3(2.2));\n \n}\nvec3 LinearToSRGB(vec3 linear)\n{\n return pow(linear, vec3(0.454545));\n \n}\nfloat SmoothDistAtt2(float distSqr, float invSqrAttRadius)\n{\n float factor = distSqr * invSqrAttRadius; \n float factor2 = factor * factor; \n float factor3 = factor2 * factor2; \n float smoothFactor = clamp(1.0 - factor3 * factor3, 0.0, 1.0);\n return smoothFactor * smoothFactor;\n}\nfloat SmoothDistAtt(float distSqr, float invSqrAttRadius)\n{\n float factor = distSqr * invSqrAttRadius;\n float smoothFactor = clamp(1.0 - factor * factor, 0.0, 1.0);\n return smoothFactor * smoothFactor;\n}\nfloat GetDistAtt(float distSqr, float invSqrAttRadius)\n{\n float attenuation = 1.0 / max(distSqr, 0.01*0.01);\n attenuation *= SmoothDistAtt(distSqr , invSqrAttRadius);\n return attenuation;\n}\nfloat GetAngleAtt(vec3 L, vec3 litDir, float litAngleScale, float litAngleOffset)\n{\n float cd = dot(litDir, L);\n float attenuation = clamp(cd * litAngleScale + litAngleOffset, 0.0, 1.0);\n return (attenuation * attenuation);\n}\nfloat GGXMobile(float roughness, float NoH, vec3 H, vec3 N)\n{\n vec3 NxH = cross(N, H);\n float OneMinusNoHSqr = dot(NxH, NxH);\n float a = roughness * roughness;\n float n = NoH * a;\n float p = a / (OneMinusNoHSqr + n * n);\n return p * p;\n}\nfloat CalcSpecular(float roughness, float NoH, vec3 H, vec3 N)\n{\n return (roughness*0.25 + 0.25) * GGXMobile(roughness, NoH, H, N);\n}\nvec3 BRDFApprox(vec3 specular, float roughness, float NoV)\n{\n const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);\n const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);\n vec4 r = roughness * c0 + c1;\n float a004 = min( r.x * r.x, exp2( -9.28 * NoV ) ) * r.x + r.y;\n vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw;\n AB.y *= clamp(50.0 * specular.g, 0.0, 1.0);\n return specular * AB.x + AB.y;\n}\nuniform Constants {\n vec4 tilingOffset;\n vec4 albedo;\n vec4 albedoScale; \n vec4 pbrParams; \n vec4 pbrScale; \n vec4 emissive;\n vec4 emissiveScale;\n};\nin vec3 v_worldPos;\nin vec3 v_worldNormal;\n#if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP\n in vec2 v_uv;\n#endif\n#if USE_NORMAL_MAP\n in vec3 v_tangent;\n#endif\n#if USE_ALBEDO_MAP\n uniform sampler2D albedoMap;\n#endif\n#if USE_NORMAL_MAP\n uniform sampler2D normalMap;\n#endif\n#if USE_PBR_MAP\n uniform sampler2D pbrMap;\n#endif\n#if USE_EMISSIVE_MAP\n uniform sampler2D emissiveMap;\n#endif\n#ifdef USE_IBL\n uniform samplerCube envMap;\n#endif\nvec4 frag () {\n #if USE_ALBEDO_MAP\n vec4 baseColor = texture(albedoMap, v_uv) * albedo;\n #else\n vec4 baseColor = albedo;\n #endif\n baseColor.rgb *= albedoScale.rgb;\n baseColor.rgb = SRGBToLinear(baseColor.rgb);\n #if USE_ALPHA_TEST\n if(baseColor.a < albedoScale.a)\n discard;\n #endif\n vec3 N = normalize(v_worldNormal);\n #if USE_NORMAL_MAP\n vec3 normal = texture(normalMap, v_uv).rgb;\n normal = 2.0 * normal.xyz - 1.0;\n vec3 tangent = normalize(v_tangent);\n vec3 binormal = normalize(cross(tangent, N));\n mat3 matTBN = mat3(tangent, binormal, N);\n N = normalize(matTBN * normal);\n #endif\n #if USE_PBR_MAP\n vec4 pbr = texture(pbrMap, v_uv);\n #else\n vec4 pbr = pbrParams;\n #endif\n pbr *= pbrScale;\n float roughness = clamp(pbr.ROUGHNESS_CHANNEL, 0.04, 1.0);\n float metallic = clamp(pbr.METALLIC_CHANNEL, 0.0, 0.96);\n vec3 diffuse = baseColor.rgb * (1.0 - metallic);\n vec3 specular = mix(vec3(0.04), baseColor.rgb, metallic);\n vec3 V = normalize(cc_cameraPos.xyz - v_worldPos);\n vec3 L = normalize(-cc_mainLitDir.xyz);\n vec3 H = normalize(L+V);\n float NV = max(abs(dot(N, V)), 0.001);\n float NL = max(dot(N, L), 0.001);\n float NH = max(dot(N, H), 0.0);\n specular = BRDFApprox(specular, roughness, NV);\n vec3 specularContrib = specular * CalcSpecular(roughness, NH, H, N);\n vec3 diffuseContrib = diffuse / 3.14159265359;\n vec3 finalColor = NL * cc_mainLitColor.rgb * cc_mainLitColor.w * (diffuseContrib + specularContrib);\n for (int i = 0; i < 2; i++) {\n \n vec3 PLU = cc_sphereLitPos[i].xyz - v_worldPos;\n vec3 PL = normalize(PLU);\n vec3 PH = normalize(PL+V);\n float PNL = max(dot(N, PL), 0.001);\n float PNH = max(dot(N, PH), 0.0);\n float distSqr = dot(PLU, PLU);\n float litRadius = cc_sphereLitSizeRange[i].x;\n float litRadiusSqr = litRadius * litRadius;\n float illum = 3.14159265359 * (litRadiusSqr / max(litRadiusSqr , distSqr));\n float attRadiusSqrInv = 1.0 / max(cc_sphereLitSizeRange[i].y, 0.01);\n attRadiusSqrInv *= attRadiusSqrInv;\n float att = GetDistAtt(distSqr, attRadiusSqrInv);\n vec3 lspec = specular * CalcSpecular(roughness, PNH, PH, N);\n finalColor += PNL * cc_sphereLitColor[i].rgb * cc_sphereLitColor[i].w * illum * att * (diffuseContrib + lspec);\n }\n for (int i = 0; i < 2; i++) {\n \n vec3 SLU = cc_spotLitPos[i].xyz - v_worldPos;\n vec3 SL = normalize(SLU);\n vec3 SH = normalize(SL+V);\n float SNL = max(dot(N, SL), 0.001);\n float SNH = max(dot(N, SH), 0.0);\n float distSqr = dot(SLU, SLU);\n float litRadius = cc_spotLitSizeRangeAngle[i].x;\n float litRadiusSqr = litRadius * litRadius;\n float illum = 3.14159265359 * (litRadiusSqr / max(litRadiusSqr , distSqr));\n float attRadiusSqrInv = 1.0 / max(cc_spotLitSizeRangeAngle[i].y, 0.01);\n attRadiusSqrInv *= attRadiusSqrInv;\n float cosInner = max(dot(-cc_spotLitDir[i].xyz, SL), 0.01);\n float cosOuter = cc_spotLitSizeRangeAngle[i].z;\n float litAngleScale = 1.0 / max(0.001, cosInner - cosOuter);\n float litAngleOffset = -cosOuter * litAngleScale;\n float att = GetDistAtt(distSqr, attRadiusSqrInv);\n att *= GetAngleAtt(SL, -cc_spotLitDir[i].xyz, litAngleScale, litAngleOffset);\n vec3 lspec = specular * CalcSpecular(roughness, SNH, SH, N);\n finalColor += SNL * cc_spotLitColor[i].rgb * cc_spotLitColor[i].w * illum * att * (diffuseContrib + lspec);\n }\n float fAmb = dot(N, vec3(0.0, -1.0, 0.0)) * 0.5 + 0.5;\n vec3 ambDiff = mix(cc_ambientSky.rgb, cc_ambientGround.rgb, fAmb) * cc_ambientSky.w;\n finalColor += (ambDiff.rgb * diffuse);\n #if USE_IBL\n vec3 R = normalize(reflect(-V, N));\n vec4 env = texture(envMap, R);\n env.rgb = SRGBToLinear(env.rgb) * cc_ambientSky.w;\n finalColor += (env.rgb * specular);\n #endif\n finalColor = mix(finalColor, finalColor * pbr.AO_CHANNEL, pbrParams.z);\n #if USE_EMISSIVE_MAP\n vec3 emissiveColor = texture(emissiveMap, v_uv).rgb;\n emissiveColor = SRGBToLinear(emissiveColor);\n #if CC_USE_HDR\n emissiveColor *= cc_exposure.w;\n #endif\n finalColor += emissiveColor * emissive.rgb * emissiveScale.xyz;\n #endif\n #if !CC_USE_HDR\n finalColor = LinearToSRGB(finalColor);\n #endif\n return vec4(finalColor, baseColor.a);\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = frag(); }\n` }, "glsl1": { - "vert": `\nprecision mediump float;\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\nuniform vec4 cc_nativeSize;\nuniform mat4 cc_matView;\nuniform mat4 cc_matViewInv;\nuniform mat4 cc_matProj;\nuniform mat4 cc_matProjInv;\nuniform mat4 cc_matViewProj;\nuniform mat4 cc_matViewProjInv;\nuniform vec4 cc_cameraPos;\nuniform vec4 cc_exposure;\nuniform vec4 cc_mainLitDir;\nuniform vec4 cc_mainLitColor;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\nuniform mat4 cc_matWorld;\nuniform mat4 cc_matWorldIT;\nuniform vec4 tilingOffset;\nuniform vec4 albedo;\nuniform vec4 albedoScale;\nuniform vec4 pbrParams;\nuniform vec4 pbrScale;\nuniform vec4 emissive;\nuniform vec4 emissiveScale;\n#if CC_USE_SKINNING\nattribute vec4 a_weights;\nattribute vec4 a_joints;\nuniform vec4 cc_matJoint[195];\nuniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\n#if CC_USE_JOINTS_TEXTURE_RGBA8888\nfloat decode32(vec4 rgba) {\n float Sign = 1.0 - step(128.0,rgba[0])*2.0;\n float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0;\n if (Exponent == -127.0) {\n return 0.0;\n }\n float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000);\n return Sign * exp2(Exponent - 23.0) * Mantissa;\n}\nvec4 decodevec4 (vec4 x, vec4 y, vec4 z, vec4 w) {\n return vec4(\n decode32(x * 255.0),\n decode32(y * 255.0),\n decode32(z * 255.0),\n decode32(w * 255.0)\n );\n}\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 16.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y))\n );\n vec4 v2 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 4.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 5.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 6.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 7.5), y))\n );\n vec4 v3 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 8.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 9.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 10.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 11.5), y))\n );\n vec4 v4 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 12.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 13.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 14.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 15.5), y))\n );\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#endif\n#else\nmat4 getBoneMatrix(const in float i) {\n vec4 _0 = cc_matJoint[3 * int(i) + 0];\n vec4 _1 = cc_matJoint[3 * int(i) + 1];\n vec4 _2 = cc_matJoint[3 * int(i) + 2];\n return mat4(\n vec4(_0.x, _0.y, _0.z, 0),\n vec4(_0.w, _1.x, _1.y, 0),\n vec4(_1.z, _1.w, _2.x, 0),\n vec4(_2.y, _2.z, _2.w, 1)\n );\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nattribute vec3 a_position;\nattribute vec3 a_normal;\nattribute vec4 a_tangent;\nvarying vec3 v_worldPos;\nvarying vec3 v_worldNormal;\n#if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP\n attribute vec2 a_texCoord;\n varying vec2 v_uv;\n#endif\n#if USE_NORMAL_MAP\n varying vec3 v_tangent;\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1.0);\n vec4 normal = vec4(a_normal, 0.0);\n #if CC_USE_SKINNING\n skinVertex(pos, normal);\n #endif\n v_worldPos = (cc_matWorld * pos).xyz;\n v_worldNormal = (cc_matWorldIT * normal).xyz;\n #if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP\n v_uv = a_texCoord * tilingOffset.xy + tilingOffset.zw;\n #endif\n #if USE_NORMAL_MAP\n v_tangent = (cc_matWorldIT * vec4(a_tangent.xyz, 0.0)).xyz;\n #endif\n return cc_matViewProj * cc_matWorld * pos;\n}\nvoid main() { gl_Position = vert(); }\n`, + "vert": `\nprecision mediump float;\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\nuniform vec4 cc_nativeSize;\nuniform mat4 cc_matView;\nuniform mat4 cc_matViewInv;\nuniform mat4 cc_matProj;\nuniform mat4 cc_matProjInv;\nuniform mat4 cc_matViewProj;\nuniform mat4 cc_matViewProjInv;\nuniform vec4 cc_cameraPos;\nuniform vec4 cc_exposure;\nuniform vec4 cc_mainLitDir;\nuniform vec4 cc_mainLitColor;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\nuniform mat4 cc_matWorld;\nuniform mat4 cc_matWorldIT;\nuniform vec4 tilingOffset;\nuniform vec4 albedo;\nuniform vec4 albedoScale;\nuniform vec4 pbrParams;\nuniform vec4 pbrScale;\nuniform vec4 emissive;\nuniform vec4 emissiveScale;\n#if CC_USE_SKINNING\nattribute vec4 a_weights;\nattribute vec4 a_joints;\n#if CC_USE_JOINTS_TEXTURE\n \n uniform vec4 cc_jointsTextureSize;\n#else\n \n uniform vec4 cc_matJoint[195];\n#endif\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\n#if CC_USE_JOINTS_TEXTURE_RGBA8888\nfloat decode32(vec4 rgba) {\n float Sign = 1.0 - step(128.0,rgba[0])*2.0;\n float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0;\n if (Exponent == -127.0) {\n return 0.0;\n }\n float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000);\n return Sign * exp2(Exponent - 23.0) * Mantissa;\n}\nvec4 decodevec4 (vec4 x, vec4 y, vec4 z, vec4 w) {\n return vec4(\n decode32(x * 255.0),\n decode32(y * 255.0),\n decode32(z * 255.0),\n decode32(w * 255.0)\n );\n}\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 16.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y))\n );\n vec4 v2 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 4.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 5.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 6.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 7.5), y))\n );\n vec4 v3 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 8.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 9.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 10.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 11.5), y))\n );\n vec4 v4 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 12.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 13.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 14.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 15.5), y))\n );\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x * 2.0 + 1.0) * getBoneMatrix(a_joints.x * 2.0 + 0.0) * a_weights.x +\n getBoneMatrix(a_joints.y * 2.0 + 1.0) * getBoneMatrix(a_joints.y * 2.0 + 0.0) * a_weights.y +\n getBoneMatrix(a_joints.z * 2.0 + 1.0) * getBoneMatrix(a_joints.z * 2.0 + 0.0) * a_weights.z +\n getBoneMatrix(a_joints.w * 2.0 + 1.0) * getBoneMatrix(a_joints.w * 2.0 + 0.0) * a_weights.w;\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n vec4 _0 = cc_matJoint[3 * int(i) + 0];\n vec4 _1 = cc_matJoint[3 * int(i) + 1];\n vec4 _2 = cc_matJoint[3 * int(i) + 2];\n return mat4(\n vec4(_0.x, _0.y, _0.z, 0),\n vec4(_0.w, _1.x, _1.y, 0),\n vec4(_1.z, _1.w, _2.x, 0),\n vec4(_2.y, _2.z, _2.w, 1)\n );\n}\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\n#endif\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nattribute vec3 a_position;\nattribute vec3 a_normal;\nattribute vec4 a_tangent;\nvarying vec3 v_worldPos;\nvarying vec3 v_worldNormal;\n#if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP\n attribute vec2 a_texCoord;\n varying vec2 v_uv;\n#endif\n#if USE_NORMAL_MAP\n varying vec3 v_tangent;\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1.0);\n vec4 normal = vec4(a_normal, 0.0);\n #if CC_USE_SKINNING\n skinVertex(pos, normal);\n #else\n pos = cc_matWorld * pos;\n normal = cc_matWorldIT * normal;\n #endif\n v_worldPos = pos.xyz;\n v_worldNormal = normal.xyz;\n #if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP\n v_uv = a_texCoord * tilingOffset.xy + tilingOffset.zw;\n #endif\n #if USE_NORMAL_MAP\n v_tangent = (cc_matWorldIT * vec4(a_tangent.xyz, 0.0)).xyz;\n #endif\n return cc_matViewProj * pos;\n}\nvoid main() { gl_Position = vert(); }\n`, "frag": `\nprecision mediump float;\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\nuniform vec4 cc_nativeSize;\nuniform mat4 cc_matView;\nuniform mat4 cc_matViewInv;\nuniform mat4 cc_matProj;\nuniform mat4 cc_matProjInv;\nuniform mat4 cc_matViewProj;\nuniform mat4 cc_matViewProjInv;\nuniform vec4 cc_cameraPos;\nuniform vec4 cc_exposure;\nuniform vec4 cc_mainLitDir;\nuniform vec4 cc_mainLitColor;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\n#define MAX_LIGHTS 2\nuniform vec4 cc_sphereLitPos[2];\nuniform vec4 cc_sphereLitSizeRange[2];\nuniform vec4 cc_sphereLitColor[2];\nuniform vec4 cc_spotLitPos[2];\nuniform vec4 cc_spotLitSizeRangeAngle[2];\nuniform vec4 cc_spotLitDir[2];\nuniform vec4 cc_spotLitColor[2];\n#define PI 3.14159265359\n#define PI2 6.28318530718\n#define EPSILON 1e-6\n#define LOG2 1.442695\n#define saturate(a) clamp( a, 0.0, 1.0 )\n#define FP_MAX 65504.0\n#define FP_SCALE 0.0009765625\n#define FP_SCALE_INV 1024.0\nvec3 SRGBToLinear(vec3 gamma)\n{\n return pow(gamma, vec3(2.2));\n \n}\nvec3 LinearToSRGB(vec3 linear)\n{\n return pow(linear, vec3(0.454545));\n \n}\nfloat SmoothDistAtt2(float distSqr, float invSqrAttRadius)\n{\n float factor = distSqr * invSqrAttRadius; \n float factor2 = factor * factor; \n float factor3 = factor2 * factor2; \n float smoothFactor = clamp(1.0 - factor3 * factor3, 0.0, 1.0);\n return smoothFactor * smoothFactor;\n}\nfloat SmoothDistAtt(float distSqr, float invSqrAttRadius)\n{\n float factor = distSqr * invSqrAttRadius;\n float smoothFactor = clamp(1.0 - factor * factor, 0.0, 1.0);\n return smoothFactor * smoothFactor;\n}\nfloat GetDistAtt(float distSqr, float invSqrAttRadius)\n{\n float attenuation = 1.0 / max(distSqr, 0.01*0.01);\n attenuation *= SmoothDistAtt(distSqr , invSqrAttRadius);\n return attenuation;\n}\nfloat GetAngleAtt(vec3 L, vec3 litDir, float litAngleScale, float litAngleOffset)\n{\n float cd = dot(litDir, L);\n float attenuation = clamp(cd * litAngleScale + litAngleOffset, 0.0, 1.0);\n return (attenuation * attenuation);\n}\nfloat GGXMobile(float roughness, float NoH, vec3 H, vec3 N)\n{\n vec3 NxH = cross(N, H);\n float OneMinusNoHSqr = dot(NxH, NxH);\n float a = roughness * roughness;\n float n = NoH * a;\n float p = a / (OneMinusNoHSqr + n * n);\n return p * p;\n}\nfloat CalcSpecular(float roughness, float NoH, vec3 H, vec3 N)\n{\n return (roughness*0.25 + 0.25) * GGXMobile(roughness, NoH, H, N);\n}\nvec3 BRDFApprox(vec3 specular, float roughness, float NoV)\n{\n const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022);\n const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04);\n vec4 r = roughness * c0 + c1;\n float a004 = min( r.x * r.x, exp2( -9.28 * NoV ) ) * r.x + r.y;\n vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw;\n AB.y *= clamp(50.0 * specular.g, 0.0, 1.0);\n return specular * AB.x + AB.y;\n}\nuniform vec4 tilingOffset;\nuniform vec4 albedo;\nuniform vec4 albedoScale;\nuniform vec4 pbrParams;\nuniform vec4 pbrScale;\nuniform vec4 emissive;\nuniform vec4 emissiveScale;\nvarying vec3 v_worldPos;\nvarying vec3 v_worldNormal;\n#if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP\n varying vec2 v_uv;\n#endif\n#if USE_NORMAL_MAP\n varying vec3 v_tangent;\n#endif\n#if USE_ALBEDO_MAP\n uniform sampler2D albedoMap;\n#endif\n#if USE_NORMAL_MAP\n uniform sampler2D normalMap;\n#endif\n#if USE_PBR_MAP\n uniform sampler2D pbrMap;\n#endif\n#if USE_EMISSIVE_MAP\n uniform sampler2D emissiveMap;\n#endif\n#ifdef USE_IBL\n uniform samplerCube envMap;\n#endif\nvec4 frag () {\n #if USE_ALBEDO_MAP\n vec4 baseColor = texture2D(albedoMap, v_uv) * albedo;\n #else\n vec4 baseColor = albedo;\n #endif\n baseColor.rgb *= albedoScale.rgb;\n baseColor.rgb = SRGBToLinear(baseColor.rgb);\n #if USE_ALPHA_TEST\n if(baseColor.a < albedoScale.a)\n discard;\n #endif\n vec3 N = normalize(v_worldNormal);\n #if USE_NORMAL_MAP\n vec3 normal = texture2D(normalMap, v_uv).rgb;\n normal = 2.0 * normal.xyz - 1.0;\n vec3 tangent = normalize(v_tangent);\n vec3 binormal = normalize(cross(tangent, N));\n mat3 matTBN = mat3(tangent, binormal, N);\n N = normalize(matTBN * normal);\n #endif\n #if USE_PBR_MAP\n vec4 pbr = texture2D(pbrMap, v_uv);\n #else\n vec4 pbr = pbrParams;\n #endif\n pbr *= pbrScale;\n float roughness = clamp(pbr.ROUGHNESS_CHANNEL, 0.04, 1.0);\n float metallic = clamp(pbr.METALLIC_CHANNEL, 0.0, 0.96);\n vec3 diffuse = baseColor.rgb * (1.0 - metallic);\n vec3 specular = mix(vec3(0.04), baseColor.rgb, metallic);\n vec3 V = normalize(cc_cameraPos.xyz - v_worldPos);\n vec3 L = normalize(-cc_mainLitDir.xyz);\n vec3 H = normalize(L+V);\n float NV = max(abs(dot(N, V)), 0.001);\n float NL = max(dot(N, L), 0.001);\n float NH = max(dot(N, H), 0.0);\n specular = BRDFApprox(specular, roughness, NV);\n vec3 specularContrib = specular * CalcSpecular(roughness, NH, H, N);\n vec3 diffuseContrib = diffuse / 3.14159265359;\n vec3 finalColor = NL * cc_mainLitColor.rgb * cc_mainLitColor.w * (diffuseContrib + specularContrib);\n for (int i = 0; i < 2; i++) {\n \n vec3 PLU = cc_sphereLitPos[i].xyz - v_worldPos;\n vec3 PL = normalize(PLU);\n vec3 PH = normalize(PL+V);\n float PNL = max(dot(N, PL), 0.001);\n float PNH = max(dot(N, PH), 0.0);\n float distSqr = dot(PLU, PLU);\n float litRadius = cc_sphereLitSizeRange[i].x;\n float litRadiusSqr = litRadius * litRadius;\n float illum = 3.14159265359 * (litRadiusSqr / max(litRadiusSqr , distSqr));\n float attRadiusSqrInv = 1.0 / max(cc_sphereLitSizeRange[i].y, 0.01);\n attRadiusSqrInv *= attRadiusSqrInv;\n float att = GetDistAtt(distSqr, attRadiusSqrInv);\n vec3 lspec = specular * CalcSpecular(roughness, PNH, PH, N);\n finalColor += PNL * cc_sphereLitColor[i].rgb * cc_sphereLitColor[i].w * illum * att * (diffuseContrib + lspec);\n }\n for (int i = 0; i < 2; i++) {\n \n vec3 SLU = cc_spotLitPos[i].xyz - v_worldPos;\n vec3 SL = normalize(SLU);\n vec3 SH = normalize(SL+V);\n float SNL = max(dot(N, SL), 0.001);\n float SNH = max(dot(N, SH), 0.0);\n float distSqr = dot(SLU, SLU);\n float litRadius = cc_spotLitSizeRangeAngle[i].x;\n float litRadiusSqr = litRadius * litRadius;\n float illum = 3.14159265359 * (litRadiusSqr / max(litRadiusSqr , distSqr));\n float attRadiusSqrInv = 1.0 / max(cc_spotLitSizeRangeAngle[i].y, 0.01);\n attRadiusSqrInv *= attRadiusSqrInv;\n float cosInner = max(dot(-cc_spotLitDir[i].xyz, SL), 0.01);\n float cosOuter = cc_spotLitSizeRangeAngle[i].z;\n float litAngleScale = 1.0 / max(0.001, cosInner - cosOuter);\n float litAngleOffset = -cosOuter * litAngleScale;\n float att = GetDistAtt(distSqr, attRadiusSqrInv);\n att *= GetAngleAtt(SL, -cc_spotLitDir[i].xyz, litAngleScale, litAngleOffset);\n vec3 lspec = specular * CalcSpecular(roughness, SNH, SH, N);\n finalColor += SNL * cc_spotLitColor[i].rgb * cc_spotLitColor[i].w * illum * att * (diffuseContrib + lspec);\n }\n float fAmb = dot(N, vec3(0.0, -1.0, 0.0)) * 0.5 + 0.5;\n vec3 ambDiff = mix(cc_ambientSky.rgb, cc_ambientGround.rgb, fAmb) * cc_ambientSky.w;\n finalColor += (ambDiff.rgb * diffuse);\n #if USE_IBL\n vec3 R = normalize(reflect(-V, N));\n vec4 env = textureCube(envMap, R);\n env.rgb = SRGBToLinear(env.rgb) * cc_ambientSky.w;\n finalColor += (env.rgb * specular);\n #endif\n finalColor = mix(finalColor, finalColor * pbr.AO_CHANNEL, pbrParams.z);\n #if USE_EMISSIVE_MAP\n vec3 emissiveColor = texture2D(emissiveMap, v_uv).rgb;\n emissiveColor = SRGBToLinear(emissiveColor);\n #if CC_USE_HDR\n emissiveColor *= cc_exposure.w;\n #endif\n finalColor += emissiveColor * emissive.rgb * emissiveScale.xyz;\n #endif\n #if !CC_USE_HDR\n finalColor = LinearToSRGB(finalColor);\n #endif\n return vec4(finalColor, baseColor.a);\n}\nvoid main() { gl_FragColor = frag(); }\n` }, - "builtins": {"globals":{"blocks":["CCGlobal"], "samplers":[]}, "locals":{"blocks":["CCLocal", "CCSkinning", "CCForwardLight"], "samplers":["cc_jointsTexture"]}}, + "builtins": {"globals":{"blocks":["CCGlobal"], "samplers":[]}, "locals":{"blocks":["CCLocal", "CCSkinningTextureCase", "CCSkinning", "CCForwardLight"], "samplers":["cc_jointsTexture"]}}, "defines": [ {"name":"CC_USE_SKINNING", "type":"boolean", "defines":[]}, {"name":"CC_USE_JOINTS_TEXTURE", "type":"boolean", "defines":["CC_USE_SKINNING"]}, @@ -157,16 +157,16 @@ export default [ }, { "name": "builtin-standard|planar-shadow-vs:vert|planar-shadow-fs:frag", - "hash": 1415604583, + "hash": 3166272625, "glsl3": { - "vert": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n vec4 cc_nativeSize; \n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n vec4 cc_exposure; \n vec4 cc_mainLitDir; \n vec4 cc_mainLitColor; \n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\nuniform CCShadow {\n mat4 cc_matLightPlaneProj;\n vec4 cc_shadowColor;\n};\n#if CC_USE_SKINNING\nin vec4 a_weights;\nin vec4 a_joints;\nuniform CCSkinning {\n vec4 cc_matJoint[65 * 3];\n vec4 cc_jointsTextureSize;\n};\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\n#if CC_USE_JOINTS_TEXTURE_RGBA8888\nfloat decode32(vec4 rgba) {\n float Sign = 1.0 - step(128.0,rgba[0])*2.0;\n float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0;\n if (Exponent == -127.0) {\n return 0.0;\n }\n float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000);\n return Sign * exp2(Exponent - 23.0) * Mantissa;\n}\nvec4 decodevec4 (vec4 x, vec4 y, vec4 z, vec4 w) {\n return vec4(\n decode32(x * 255.0),\n decode32(y * 255.0),\n decode32(z * 255.0),\n decode32(w * 255.0)\n );\n}\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 16.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 0.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 1.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 2.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 3.5), y))\n );\n vec4 v2 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 4.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 5.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 6.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 7.5), y))\n );\n vec4 v3 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 8.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 9.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 10.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 11.5), y))\n );\n vec4 v4 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 12.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 13.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 14.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 15.5), y))\n );\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#endif\n#else\nmat4 getBoneMatrix(const in float i) {\n vec4 _0 = cc_matJoint[3 * int(i) + 0];\n vec4 _1 = cc_matJoint[3 * int(i) + 1];\n vec4 _2 = cc_matJoint[3 * int(i) + 2];\n return mat4(\n vec4(_0.x, _0.y, _0.z, 0),\n vec4(_0.w, _1.x, _1.y, 0),\n vec4(_1.z, _1.w, _2.x, 0),\n vec4(_2.y, _2.z, _2.w, 1)\n );\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nin vec3 a_position;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #endif\n pos = cc_matViewProj * cc_matLightPlaneProj * cc_matWorld * pos;\n pos.z -= 0.0001;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, + "vert": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n vec4 cc_nativeSize; \n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n vec4 cc_exposure; \n vec4 cc_mainLitDir; \n vec4 cc_mainLitColor; \n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\nuniform CCShadow {\n mat4 cc_matLightPlaneProj;\n vec4 cc_shadowColor;\n};\n#if CC_USE_SKINNING\nin vec4 a_weights;\nin vec4 a_joints;\n#if CC_USE_JOINTS_TEXTURE\n \n uniform CCSkinningTextureCase {\n vec4 cc_jointsTextureSize;\n };\n#else\n \n uniform CCSkinning {\n vec4 cc_matJoint[65 * 3];\n };\n#endif\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\n#if CC_USE_JOINTS_TEXTURE_RGBA8888\nfloat decode32(vec4 rgba) {\n float Sign = 1.0 - step(128.0,rgba[0])*2.0;\n float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0;\n if (Exponent == -127.0) {\n return 0.0;\n }\n float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000);\n return Sign * exp2(Exponent - 23.0) * Mantissa;\n}\nvec4 decodevec4 (vec4 x, vec4 y, vec4 z, vec4 w) {\n return vec4(\n decode32(x * 255.0),\n decode32(y * 255.0),\n decode32(z * 255.0),\n decode32(w * 255.0)\n );\n}\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 16.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 0.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 1.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 2.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 3.5), y))\n );\n vec4 v2 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 4.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 5.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 6.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 7.5), y))\n );\n vec4 v3 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 8.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 9.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 10.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 11.5), y))\n );\n vec4 v4 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 12.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 13.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 14.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 15.5), y))\n );\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x * 2.0 + 1.0) * getBoneMatrix(a_joints.x * 2.0 + 0.0) * a_weights.x +\n getBoneMatrix(a_joints.y * 2.0 + 1.0) * getBoneMatrix(a_joints.y * 2.0 + 0.0) * a_weights.y +\n getBoneMatrix(a_joints.z * 2.0 + 1.0) * getBoneMatrix(a_joints.z * 2.0 + 0.0) * a_weights.z +\n getBoneMatrix(a_joints.w * 2.0 + 1.0) * getBoneMatrix(a_joints.w * 2.0 + 0.0) * a_weights.w;\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n vec4 _0 = cc_matJoint[3 * int(i) + 0];\n vec4 _1 = cc_matJoint[3 * int(i) + 1];\n vec4 _2 = cc_matJoint[3 * int(i) + 2];\n return mat4(\n vec4(_0.x, _0.y, _0.z, 0),\n vec4(_0.w, _1.x, _1.y, 0),\n vec4(_1.z, _1.w, _2.x, 0),\n vec4(_2.y, _2.z, _2.w, 1)\n );\n}\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\n#endif\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nin vec3 a_position;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #else\n pos = cc_matWorld * pos;\n #endif\n pos = cc_matViewProj * cc_matLightPlaneProj * pos;\n pos.z -= 0.0001;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, "frag": `\nprecision mediump float;\nuniform CCShadow {\n mat4 cc_matLightPlaneProj;\n vec4 cc_shadowColor;\n};\nuniform CCGlobal {\n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n vec4 cc_nativeSize; \n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n vec4 cc_exposure; \n vec4 cc_mainLitDir; \n vec4 cc_mainLitColor; \n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nvec4 PostProcess(vec4 color)\n{\n#if CC_USE_HDR\n color.rgb = mix(color.rgb, pow(color.rgb, vec3(2.2)) * cc_exposure.w, vec3(cc_exposure.z));\n#endif\n return color;\n}\nvec4 frag () {\n return PostProcess(cc_shadowColor);\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = frag(); }\n` }, "glsl1": { - "vert": `\nprecision mediump float;\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\nuniform vec4 cc_nativeSize;\nuniform mat4 cc_matView;\nuniform mat4 cc_matViewInv;\nuniform mat4 cc_matProj;\nuniform mat4 cc_matProjInv;\nuniform mat4 cc_matViewProj;\nuniform mat4 cc_matViewProjInv;\nuniform vec4 cc_cameraPos;\nuniform vec4 cc_exposure;\nuniform vec4 cc_mainLitDir;\nuniform vec4 cc_mainLitColor;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\nuniform mat4 cc_matWorld;\nuniform mat4 cc_matWorldIT;\nuniform mat4 cc_matLightPlaneProj;\nuniform vec4 cc_shadowColor;\n#if CC_USE_SKINNING\nattribute vec4 a_weights;\nattribute vec4 a_joints;\nuniform vec4 cc_matJoint[195];\nuniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\n#if CC_USE_JOINTS_TEXTURE_RGBA8888\nfloat decode32(vec4 rgba) {\n float Sign = 1.0 - step(128.0,rgba[0])*2.0;\n float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0;\n if (Exponent == -127.0) {\n return 0.0;\n }\n float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000);\n return Sign * exp2(Exponent - 23.0) * Mantissa;\n}\nvec4 decodevec4 (vec4 x, vec4 y, vec4 z, vec4 w) {\n return vec4(\n decode32(x * 255.0),\n decode32(y * 255.0),\n decode32(z * 255.0),\n decode32(w * 255.0)\n );\n}\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 16.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y))\n );\n vec4 v2 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 4.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 5.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 6.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 7.5), y))\n );\n vec4 v3 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 8.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 9.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 10.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 11.5), y))\n );\n vec4 v4 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 12.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 13.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 14.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 15.5), y))\n );\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#endif\n#else\nmat4 getBoneMatrix(const in float i) {\n vec4 _0 = cc_matJoint[3 * int(i) + 0];\n vec4 _1 = cc_matJoint[3 * int(i) + 1];\n vec4 _2 = cc_matJoint[3 * int(i) + 2];\n return mat4(\n vec4(_0.x, _0.y, _0.z, 0),\n vec4(_0.w, _1.x, _1.y, 0),\n vec4(_1.z, _1.w, _2.x, 0),\n vec4(_2.y, _2.z, _2.w, 1)\n );\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nattribute vec3 a_position;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #endif\n pos = cc_matViewProj * cc_matLightPlaneProj * cc_matWorld * pos;\n pos.z -= 0.0001;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, + "vert": `\nprecision mediump float;\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\nuniform vec4 cc_nativeSize;\nuniform mat4 cc_matView;\nuniform mat4 cc_matViewInv;\nuniform mat4 cc_matProj;\nuniform mat4 cc_matProjInv;\nuniform mat4 cc_matViewProj;\nuniform mat4 cc_matViewProjInv;\nuniform vec4 cc_cameraPos;\nuniform vec4 cc_exposure;\nuniform vec4 cc_mainLitDir;\nuniform vec4 cc_mainLitColor;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\nuniform mat4 cc_matWorld;\nuniform mat4 cc_matWorldIT;\nuniform mat4 cc_matLightPlaneProj;\nuniform vec4 cc_shadowColor;\n#if CC_USE_SKINNING\nattribute vec4 a_weights;\nattribute vec4 a_joints;\n#if CC_USE_JOINTS_TEXTURE\n \n uniform vec4 cc_jointsTextureSize;\n#else\n \n uniform vec4 cc_matJoint[195];\n#endif\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\n#if CC_USE_JOINTS_TEXTURE_RGBA8888\nfloat decode32(vec4 rgba) {\n float Sign = 1.0 - step(128.0,rgba[0])*2.0;\n float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0;\n if (Exponent == -127.0) {\n return 0.0;\n }\n float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000);\n return Sign * exp2(Exponent - 23.0) * Mantissa;\n}\nvec4 decodevec4 (vec4 x, vec4 y, vec4 z, vec4 w) {\n return vec4(\n decode32(x * 255.0),\n decode32(y * 255.0),\n decode32(z * 255.0),\n decode32(w * 255.0)\n );\n}\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 16.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y))\n );\n vec4 v2 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 4.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 5.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 6.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 7.5), y))\n );\n vec4 v3 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 8.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 9.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 10.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 11.5), y))\n );\n vec4 v4 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 12.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 13.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 14.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 15.5), y))\n );\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x * 2.0 + 1.0) * getBoneMatrix(a_joints.x * 2.0 + 0.0) * a_weights.x +\n getBoneMatrix(a_joints.y * 2.0 + 1.0) * getBoneMatrix(a_joints.y * 2.0 + 0.0) * a_weights.y +\n getBoneMatrix(a_joints.z * 2.0 + 1.0) * getBoneMatrix(a_joints.z * 2.0 + 0.0) * a_weights.z +\n getBoneMatrix(a_joints.w * 2.0 + 1.0) * getBoneMatrix(a_joints.w * 2.0 + 0.0) * a_weights.w;\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n vec4 _0 = cc_matJoint[3 * int(i) + 0];\n vec4 _1 = cc_matJoint[3 * int(i) + 1];\n vec4 _2 = cc_matJoint[3 * int(i) + 2];\n return mat4(\n vec4(_0.x, _0.y, _0.z, 0),\n vec4(_0.w, _1.x, _1.y, 0),\n vec4(_1.z, _1.w, _2.x, 0),\n vec4(_2.y, _2.z, _2.w, 1)\n );\n}\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\n#endif\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nattribute vec3 a_position;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #else\n pos = cc_matWorld * pos;\n #endif\n pos = cc_matViewProj * cc_matLightPlaneProj * pos;\n pos.z -= 0.0001;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, "frag": `\nprecision mediump float;\nuniform mat4 cc_matLightPlaneProj;\nuniform vec4 cc_shadowColor;\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\nuniform vec4 cc_nativeSize;\nuniform mat4 cc_matView;\nuniform mat4 cc_matViewInv;\nuniform mat4 cc_matProj;\nuniform mat4 cc_matProjInv;\nuniform mat4 cc_matViewProj;\nuniform mat4 cc_matViewProjInv;\nuniform vec4 cc_cameraPos;\nuniform vec4 cc_exposure;\nuniform vec4 cc_mainLitDir;\nuniform vec4 cc_mainLitColor;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\nvec4 PostProcess(vec4 color)\n{\n#if CC_USE_HDR\n color.rgb = mix(color.rgb, pow(color.rgb, vec3(2.2)) * cc_exposure.w, vec3(cc_exposure.z));\n#endif\n return color;\n}\nvec4 frag () {\n return PostProcess(cc_shadowColor);\n}\nvoid main() { gl_FragColor = frag(); }\n` }, - "builtins": {"globals":{"blocks":["CCGlobal", "CCShadow"], "samplers":[]}, "locals":{"blocks":["CCLocal", "CCSkinning"], "samplers":["cc_jointsTexture"]}}, + "builtins": {"globals":{"blocks":["CCGlobal", "CCShadow"], "samplers":[]}, "locals":{"blocks":["CCLocal", "CCSkinningTextureCase", "CCSkinning"], "samplers":["cc_jointsTexture"]}}, "defines": [ {"name":"CC_USE_SKINNING", "type":"boolean", "defines":[]}, {"name":"CC_USE_JOINTS_TEXTURE", "type":"boolean", "defines":["CC_USE_SKINNING"]}, @@ -187,16 +187,16 @@ export default [ "shaders": [ { "name": "builtin-unlit|unlit-vs:vert|unlit-fs:frag", - "hash": 182872457, + "hash": 3776009997, "glsl3": { - "vert": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n vec4 cc_nativeSize; \n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n vec4 cc_exposure; \n vec4 cc_mainLitDir; \n vec4 cc_mainLitColor; \n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\n#if CC_USE_SKINNING\nin vec4 a_weights;\nin vec4 a_joints;\nuniform CCSkinning {\n vec4 cc_matJoint[65 * 3];\n vec4 cc_jointsTextureSize;\n};\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\n#if CC_USE_JOINTS_TEXTURE_RGBA8888\nfloat decode32(vec4 rgba) {\n float Sign = 1.0 - step(128.0,rgba[0])*2.0;\n float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0;\n if (Exponent == -127.0) {\n return 0.0;\n }\n float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000);\n return Sign * exp2(Exponent - 23.0) * Mantissa;\n}\nvec4 decodevec4 (vec4 x, vec4 y, vec4 z, vec4 w) {\n return vec4(\n decode32(x * 255.0),\n decode32(y * 255.0),\n decode32(z * 255.0),\n decode32(w * 255.0)\n );\n}\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 16.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 0.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 1.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 2.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 3.5), y))\n );\n vec4 v2 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 4.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 5.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 6.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 7.5), y))\n );\n vec4 v3 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 8.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 9.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 10.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 11.5), y))\n );\n vec4 v4 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 12.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 13.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 14.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 15.5), y))\n );\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#endif\n#else\nmat4 getBoneMatrix(const in float i) {\n vec4 _0 = cc_matJoint[3 * int(i) + 0];\n vec4 _1 = cc_matJoint[3 * int(i) + 1];\n vec4 _2 = cc_matJoint[3 * int(i) + 2];\n return mat4(\n vec4(_0.x, _0.y, _0.z, 0),\n vec4(_0.w, _1.x, _1.y, 0),\n vec4(_1.z, _1.w, _2.x, 0),\n vec4(_2.y, _2.z, _2.w, 1)\n );\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nin vec3 a_position;\n#if USE_TEXTURE\n in vec2 a_texCoord;\n out vec2 uv0;\n uniform TexCoords {\n vec4 tilingOffset;\n };\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #endif\n pos = cc_matViewProj * cc_matWorld * pos;\n #if USE_TEXTURE\n uv0 = a_texCoord * tilingOffset.xy + tilingOffset.zw;\n #endif\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, + "vert": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n vec4 cc_nativeSize; \n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n vec4 cc_exposure; \n vec4 cc_mainLitDir; \n vec4 cc_mainLitColor; \n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\n#if CC_USE_SKINNING\nin vec4 a_weights;\nin vec4 a_joints;\n#if CC_USE_JOINTS_TEXTURE\n \n uniform CCSkinningTextureCase {\n vec4 cc_jointsTextureSize;\n };\n#else\n \n uniform CCSkinning {\n vec4 cc_matJoint[65 * 3];\n };\n#endif\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\n#if CC_USE_JOINTS_TEXTURE_RGBA8888\nfloat decode32(vec4 rgba) {\n float Sign = 1.0 - step(128.0,rgba[0])*2.0;\n float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0;\n if (Exponent == -127.0) {\n return 0.0;\n }\n float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000);\n return Sign * exp2(Exponent - 23.0) * Mantissa;\n}\nvec4 decodevec4 (vec4 x, vec4 y, vec4 z, vec4 w) {\n return vec4(\n decode32(x * 255.0),\n decode32(y * 255.0),\n decode32(z * 255.0),\n decode32(w * 255.0)\n );\n}\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 16.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 0.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 1.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 2.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 3.5), y))\n );\n vec4 v2 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 4.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 5.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 6.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 7.5), y))\n );\n vec4 v3 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 8.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 9.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 10.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 11.5), y))\n );\n vec4 v4 = decodevec4(\n texture(cc_jointsTexture, vec2(dx * (x + 12.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 13.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 14.5), y)),\n texture(cc_jointsTexture, vec2(dx * (x + 15.5), y))\n );\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x * 2.0 + 1.0) * getBoneMatrix(a_joints.x * 2.0 + 0.0) * a_weights.x +\n getBoneMatrix(a_joints.y * 2.0 + 1.0) * getBoneMatrix(a_joints.y * 2.0 + 0.0) * a_weights.y +\n getBoneMatrix(a_joints.z * 2.0 + 1.0) * getBoneMatrix(a_joints.z * 2.0 + 0.0) * a_weights.z +\n getBoneMatrix(a_joints.w * 2.0 + 1.0) * getBoneMatrix(a_joints.w * 2.0 + 0.0) * a_weights.w;\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n vec4 _0 = cc_matJoint[3 * int(i) + 0];\n vec4 _1 = cc_matJoint[3 * int(i) + 1];\n vec4 _2 = cc_matJoint[3 * int(i) + 2];\n return mat4(\n vec4(_0.x, _0.y, _0.z, 0),\n vec4(_0.w, _1.x, _1.y, 0),\n vec4(_1.z, _1.w, _2.x, 0),\n vec4(_2.y, _2.z, _2.w, 1)\n );\n}\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\n#endif\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nin vec3 a_position;\n#if USE_TEXTURE\n in vec2 a_texCoord;\n out vec2 uv0;\n uniform TexCoords {\n vec4 tilingOffset;\n };\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #else\n pos = cc_matWorld * pos;\n #endif\n pos = cc_matViewProj * pos;\n #if USE_TEXTURE\n uv0 = a_texCoord * tilingOffset.xy + tilingOffset.zw;\n #endif\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, "frag": `\nprecision mediump float;\nuniform CCGlobal {\n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n vec4 cc_nativeSize; \n mat4 cc_matView;\n mat4 cc_matViewInv;\n mat4 cc_matProj;\n mat4 cc_matProjInv;\n mat4 cc_matViewProj;\n mat4 cc_matViewProjInv;\n vec4 cc_cameraPos; \n vec4 cc_exposure; \n vec4 cc_mainLitDir; \n vec4 cc_mainLitColor; \n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nvec4 PostProcess(vec4 color)\n{\n#if CC_USE_HDR\n color.rgb = mix(color.rgb, pow(color.rgb, vec3(2.2)) * cc_exposure.w, vec3(cc_exposure.z));\n#endif\n return color;\n}\n#if USE_TEXTURE\n in vec2 uv0;\n uniform sampler2D mainTexture;\n#endif\n#if USE_COLOR\n uniform Constant {\n vec4 color;\n };\n#endif\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n #if USE_TEXTURE\n o *= texture(mainTexture, uv0);\n #endif\n #if USE_COLOR\n o *= color;\n #endif\n return PostProcess(o);\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = frag(); }\n` }, "glsl1": { - "vert": `\nprecision mediump float;\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\nuniform vec4 cc_nativeSize;\nuniform mat4 cc_matView;\nuniform mat4 cc_matViewInv;\nuniform mat4 cc_matProj;\nuniform mat4 cc_matProjInv;\nuniform mat4 cc_matViewProj;\nuniform mat4 cc_matViewProjInv;\nuniform vec4 cc_cameraPos;\nuniform vec4 cc_exposure;\nuniform vec4 cc_mainLitDir;\nuniform vec4 cc_mainLitColor;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\nuniform mat4 cc_matWorld;\nuniform mat4 cc_matWorldIT;\n#if CC_USE_SKINNING\nattribute vec4 a_weights;\nattribute vec4 a_joints;\nuniform vec4 cc_matJoint[195];\nuniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\n#if CC_USE_JOINTS_TEXTURE_RGBA8888\nfloat decode32(vec4 rgba) {\n float Sign = 1.0 - step(128.0,rgba[0])*2.0;\n float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0;\n if (Exponent == -127.0) {\n return 0.0;\n }\n float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000);\n return Sign * exp2(Exponent - 23.0) * Mantissa;\n}\nvec4 decodevec4 (vec4 x, vec4 y, vec4 z, vec4 w) {\n return vec4(\n decode32(x * 255.0),\n decode32(y * 255.0),\n decode32(z * 255.0),\n decode32(w * 255.0)\n );\n}\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 16.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y))\n );\n vec4 v2 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 4.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 5.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 6.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 7.5), y))\n );\n vec4 v3 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 8.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 9.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 10.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 11.5), y))\n );\n vec4 v4 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 12.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 13.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 14.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 15.5), y))\n );\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#endif\n#else\nmat4 getBoneMatrix(const in float i) {\n vec4 _0 = cc_matJoint[3 * int(i) + 0];\n vec4 _1 = cc_matJoint[3 * int(i) + 1];\n vec4 _2 = cc_matJoint[3 * int(i) + 2];\n return mat4(\n vec4(_0.x, _0.y, _0.z, 0),\n vec4(_0.w, _1.x, _1.y, 0),\n vec4(_1.z, _1.w, _2.x, 0),\n vec4(_2.y, _2.z, _2.w, 1)\n );\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nattribute vec3 a_position;\n#if USE_TEXTURE\n attribute vec2 a_texCoord;\n varying vec2 uv0;\n uniform vec4 tilingOffset;\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #endif\n pos = cc_matViewProj * cc_matWorld * pos;\n #if USE_TEXTURE\n uv0 = a_texCoord * tilingOffset.xy + tilingOffset.zw;\n #endif\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, + "vert": `\nprecision mediump float;\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\nuniform vec4 cc_nativeSize;\nuniform mat4 cc_matView;\nuniform mat4 cc_matViewInv;\nuniform mat4 cc_matProj;\nuniform mat4 cc_matProjInv;\nuniform mat4 cc_matViewProj;\nuniform mat4 cc_matViewProjInv;\nuniform vec4 cc_cameraPos;\nuniform vec4 cc_exposure;\nuniform vec4 cc_mainLitDir;\nuniform vec4 cc_mainLitColor;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\nuniform mat4 cc_matWorld;\nuniform mat4 cc_matWorldIT;\n#if CC_USE_SKINNING\nattribute vec4 a_weights;\nattribute vec4 a_joints;\n#if CC_USE_JOINTS_TEXTURE\n \n uniform vec4 cc_jointsTextureSize;\n#else\n \n uniform vec4 cc_matJoint[195];\n#endif\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\n#if CC_USE_JOINTS_TEXTURE_RGBA8888\nfloat decode32(vec4 rgba) {\n float Sign = 1.0 - step(128.0,rgba[0])*2.0;\n float Exponent = 2.0 * mod(rgba[0],128.0) + step(128.0,rgba[1]) - 127.0;\n if (Exponent == -127.0) {\n return 0.0;\n }\n float Mantissa = mod(rgba[1],128.0)*65536.0 + rgba[2]*256.0 +rgba[3] + float(0x800000);\n return Sign * exp2(Exponent - 23.0) * Mantissa;\n}\nvec4 decodevec4 (vec4 x, vec4 y, vec4 z, vec4 w) {\n return vec4(\n decode32(x * 255.0),\n decode32(y * 255.0),\n decode32(z * 255.0),\n decode32(w * 255.0)\n );\n}\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 16.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y))\n );\n vec4 v2 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 4.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 5.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 6.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 7.5), y))\n );\n vec4 v3 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 8.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 9.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 10.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 11.5), y))\n );\n vec4 v4 = decodevec4(\n texture2D(cc_jointsTexture, vec2(dx * (x + 12.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 13.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 14.5), y)),\n texture2D(cc_jointsTexture, vec2(dx * (x + 15.5), y))\n );\n return mat4(v1, v2, v3, v4);\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n float size = cc_jointsTextureSize.x;\n float j = i * 4.0;\n float x = mod(j, size);\n float y = floor(j / size);\n float dx = 1.0 / size;\n float dy = 1.0 / size;\n y = dy * (y + 0.5);\n vec4 v1 = texture2D(cc_jointsTexture, vec2(dx * (x + 0.5), y));\n vec4 v2 = texture2D(cc_jointsTexture, vec2(dx * (x + 1.5), y));\n vec4 v3 = texture2D(cc_jointsTexture, vec2(dx * (x + 2.5), y));\n vec4 v4 = texture2D(cc_jointsTexture, vec2(dx * (x + 3.5), y));\n return mat4(v1, v2, v3, v4);\n}\n#endif\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x * 2.0 + 1.0) * getBoneMatrix(a_joints.x * 2.0 + 0.0) * a_weights.x +\n getBoneMatrix(a_joints.y * 2.0 + 1.0) * getBoneMatrix(a_joints.y * 2.0 + 0.0) * a_weights.y +\n getBoneMatrix(a_joints.z * 2.0 + 1.0) * getBoneMatrix(a_joints.z * 2.0 + 0.0) * a_weights.z +\n getBoneMatrix(a_joints.w * 2.0 + 1.0) * getBoneMatrix(a_joints.w * 2.0 + 0.0) * a_weights.w;\n}\n#else\nmat4 getBoneMatrix(const in float i) {\n vec4 _0 = cc_matJoint[3 * int(i) + 0];\n vec4 _1 = cc_matJoint[3 * int(i) + 1];\n vec4 _2 = cc_matJoint[3 * int(i) + 2];\n return mat4(\n vec4(_0.x, _0.y, _0.z, 0),\n vec4(_0.w, _1.x, _1.y, 0),\n vec4(_1.z, _1.w, _2.x, 0),\n vec4(_2.y, _2.z, _2.w, 1)\n );\n}\nmat4 skinMatrix() {\n return\n getBoneMatrix(a_joints.x) * a_weights.x +\n getBoneMatrix(a_joints.y) * a_weights.y +\n getBoneMatrix(a_joints.z) * a_weights.z +\n getBoneMatrix(a_joints.w) * a_weights.w;\n}\n#endif\nvoid skinVertex(inout vec4 a1) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n}\nvoid skinVertex(inout vec4 a1, inout vec4 a2, inout vec4 a3) {\n mat4 m = skinMatrix();\n a1 = m * a1;\n a2 = m * a2;\n a3 = m * a3;\n}\n#endif\nattribute vec3 a_position;\n#if USE_TEXTURE\n attribute vec2 a_texCoord;\n varying vec2 uv0;\n uniform vec4 tilingOffset;\n#endif\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n #if CC_USE_SKINNING\n skinVertex(pos);\n #else\n pos = cc_matWorld * pos;\n #endif\n pos = cc_matViewProj * pos;\n #if USE_TEXTURE\n uv0 = a_texCoord * tilingOffset.xy + tilingOffset.zw;\n #endif\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, "frag": `\nprecision mediump float;\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\nuniform vec4 cc_nativeSize;\nuniform mat4 cc_matView;\nuniform mat4 cc_matViewInv;\nuniform mat4 cc_matProj;\nuniform mat4 cc_matProjInv;\nuniform mat4 cc_matViewProj;\nuniform mat4 cc_matViewProjInv;\nuniform vec4 cc_cameraPos;\nuniform vec4 cc_exposure;\nuniform vec4 cc_mainLitDir;\nuniform vec4 cc_mainLitColor;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\nvec4 PostProcess(vec4 color)\n{\n#if CC_USE_HDR\n color.rgb = mix(color.rgb, pow(color.rgb, vec3(2.2)) * cc_exposure.w, vec3(cc_exposure.z));\n#endif\n return color;\n}\n#if USE_TEXTURE\n varying vec2 uv0;\n uniform sampler2D mainTexture;\n#endif\n#if USE_COLOR\n uniform vec4 color;\n#endif\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n #if USE_TEXTURE\n o *= texture2D(mainTexture, uv0);\n #endif\n #if USE_COLOR\n o *= color;\n #endif\n return PostProcess(o);\n}\nvoid main() { gl_FragColor = frag(); }\n` }, - "builtins": {"globals":{"blocks":["CCGlobal"], "samplers":[]}, "locals":{"blocks":["CCLocal", "CCSkinning"], "samplers":["cc_jointsTexture"]}}, + "builtins": {"globals":{"blocks":["CCGlobal"], "samplers":[]}, "locals":{"blocks":["CCLocal", "CCSkinningTextureCase", "CCSkinning"], "samplers":["cc_jointsTexture"]}}, "defines": [ {"name":"CC_USE_SKINNING", "type":"boolean", "defines":[]}, {"name":"CC_USE_JOINTS_TEXTURE", "type":"boolean", "defines":["CC_USE_SKINNING"]}, diff --git a/cocos/3d/framework/skinning-model-component.ts b/cocos/3d/framework/skinning-model-component.ts index 1701da74e1..d3f1483175 100644 --- a/cocos/3d/framework/skinning-model-component.ts +++ b/cocos/3d/framework/skinning-model-component.ts @@ -154,23 +154,21 @@ export class SkinningModelComponent extends ModelComponent { const skeleton = this._skeleton; const skinningTarget = this._skinningTarget; - const cancelThisNodeTransform = this.node.getWorldMatrix(_m4_tmp2); - mat4.invert(cancelThisNodeTransform, cancelThisNodeTransform); - this._skeleton.joints.forEach((joint, index) => { - // If target joint doesn't exists in scene graph, skip it. - const targetNode = skinningTarget.get(joint); + const isTextureStorage = skinningModel.isTextureStorage(); + for (let iJoint = 0; iJoint < this._skeleton.joints.length; ++iJoint) { + const targetNode = skinningTarget.get(this._skeleton.joints[iJoint]); if (!targetNode) { - return; + continue; } - // 1. transform mesh to joint's local space - // 2. transform from joint' local space to world space - // 3. because it has been in world space, just cancel this mesh's original local-world transform - const bindpose = skeleton.bindposes[index]; - const jointMatrix = _m4_tmp; - mat4.multiply(jointMatrix, cancelThisNodeTransform, targetNode.worldMatrix); - mat4.multiply(jointMatrix, jointMatrix, bindpose); - skinningModel.updateJointMatrix(index, jointMatrix); - }); + if (isTextureStorage) { + skinningModel.updateJointMatrix(iJoint, targetNode.worldMatrix); + } else { + const bindpose = skeleton.bindposes[iJoint]; + const jointMatrix = _m4_tmp; + mat4.multiply(jointMatrix, targetNode.worldMatrix, bindpose); + skinningModel.updateJointMatrix(iJoint, jointMatrix); + } + } skinningModel.commitJointMatrices(); } diff --git a/cocos/pipeline/define.ts b/cocos/pipeline/define.ts index 061450c4b3..1669ba5754 100644 --- a/cocos/pipeline/define.ts +++ b/cocos/pipeline/define.ts @@ -49,7 +49,8 @@ export enum UniformBinding { UBO_LOCAL = MAX_BINDING_SUPPORTED - 3, UBO_FORWARD_LIGHTS = MAX_BINDING_SUPPORTED - 4, UBO_SKINNING = MAX_BINDING_SUPPORTED - 5, - UBO_UI = MAX_BINDING_SUPPORTED - 6, + UBO_SKINNING_TEXTURE_CASE = MAX_BINDING_SUPPORTED - 6, + UBO_UI = MAX_BINDING_SUPPORTED - 7, // samplers SAMPLER_JOINTS = MAX_BINDING_SUPPORTED + 1, } @@ -173,14 +174,12 @@ export const JointUniformCapacity = 65; export class UBOSkinning { public static MAT_JOINT_OFFSET: number = 0; - public static JOINTS_TEXTURE_SIZE_OFFSET: number = UBOSkinning.MAT_JOINT_OFFSET + JointUniformCapacity * 12; - public static COUNT: number = UBOSkinning.JOINTS_TEXTURE_SIZE_OFFSET + 4; + public static COUNT: number = JointUniformCapacity * 12; public static SIZE: number = UBOSkinning.COUNT * 4; public static BLOCK: GFXUniformBlock = { binding: UniformBinding.UBO_SKINNING, name: 'CCSkinning', members: [ { name: 'cc_matJoint', type: GFXType.FLOAT4, count: JointUniformCapacity * 3 }, // mat 4 * 3 - { name: 'cc_jointsTextureSize', type: GFXType.FLOAT4, count: 1 }, ], }; } @@ -189,6 +188,22 @@ localBindingsDesc.set(UBOSkinning.BLOCK.name, { blockInfo: UBOSkinning.BLOCK, }); +export class UBOSkinningTextureCase { + public static JOINTS_TEXTURE_SIZE_OFFSET: number = 0; + public static COUNT: number = 4; + public static SIZE: number = UBOSkinningTextureCase.COUNT * 4; + + public static BLOCK: GFXUniformBlock = { + binding: UniformBinding.UBO_SKINNING_TEXTURE_CASE, name: 'CCSkinningTextureCase', members: [ + { name: 'cc_jointsTextureSize', type: GFXType.FLOAT4, count: 1 }, + ], + }; +} +localBindingsDesc.set(UBOSkinningTextureCase.BLOCK.name, { + type: GFXBindingType.UNIFORM_BUFFER, + blockInfo: UBOSkinningTextureCase.BLOCK, +}); + export const UNIFORM_JOINTS_TEXTURE: GFXUniformSampler = { binding: UniformBinding.SAMPLER_JOINTS, name: 'cc_jointsTexture', type: GFXType.SAMPLER2D, count: 1, }; diff --git a/cocos/renderer/models/skinning-model.ts b/cocos/renderer/models/skinning-model.ts index f0f087fb41..bfd49759af 100644 --- a/cocos/renderer/models/skinning-model.ts +++ b/cocos/renderer/models/skinning-model.ts @@ -3,11 +3,11 @@ import Skeleton from '../../3d/assets/skeleton'; import { Texture2D } from '../../assets/texture-2d'; import { Filter, PixelFormat, WrapMode } from '../../assets/texture-base'; -import { mat4 } from '../../core/vmath'; +import { mat4, vec3, vec4 } from '../../core/vmath'; import { GFXBuffer } from '../../gfx/buffer'; import { GFXBufferUsageBit, GFXMemoryUsageBit } from '../../gfx/define'; import { GFXAPI, GFXDevice, GFXFeature } from '../../gfx/device'; -import { JointUniformCapacity, UBOSkinning, UNIFORM_JOINTS_TEXTURE } from '../../pipeline/define'; +import { JointUniformCapacity, UBOSkinning, UBOSkinningTextureCase, UNIFORM_JOINTS_TEXTURE } from '../../pipeline/define'; import { Node } from '../../scene-graph/node'; import { Pass } from '../core/pass'; import { samplerLib } from '../core/sampler-lib'; @@ -22,51 +22,38 @@ export enum JointStorageKind { uniform, } -interface IFJointTextureStorage { - kind: JointStorageKind.textureRGBA32F; - nativeData: Float32Array; - texture: Texture2D; -} - -interface IBJointTextureStorage { - kind: JointStorageKind.textureRGBA8; - nativeData: Uint8Array; - texture: Texture2D; -} - -interface IJointUniformsStorage { +interface IUniformStorage { kind: JointStorageKind.uniform; nativeData: Float32Array; } -type JointStorage = IFJointTextureStorage | IBJointTextureStorage | IJointUniformsStorage; - -function isTextureStorage (storage: JointStorage): storage is (IFJointTextureStorage | IBJointTextureStorage) { - return storage.kind === JointStorageKind.textureRGBA8 || storage.kind === JointStorageKind.textureRGBA32F; +interface ITextureStorage { + kind: JointStorageKind.textureRGBA32F | JointStorageKind.textureRGBA8; + texture: MatrixTexture; } -const pixelsPerJointOfFJointTexture = 4; -const pixelsPerJointOfBJointTexture = 16; +type JointStorage = ITextureStorage | IUniformStorage; + +function isTextureStorage (storage: JointStorage): storage is ITextureStorage { + return storage.kind === JointStorageKind.textureRGBA8 || storage.kind === JointStorageKind.textureRGBA32F; +} export const __FORCE_USE_UNIFORM_STORAGE__ = false; export class SkinningModel extends Model { - private _jointStorage: JointStorage | null = null; - private _skinningUBO: GFXBuffer; + private _binded: null | { + jointStorage: JointStorage, + skinningUBO: GFXBuffer, + skinningUBOBinding: number, + } = null; constructor (scene: RenderScene, node: Node) { super(scene, node); this._type = 'skinning'; - this._skinningUBO = this._device.createBuffer({ - usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, - memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, - size: UBOSkinning.SIZE, - stride: UBOSkinning.SIZE, - }); } - get storageKind () { - return this._jointStorage ? this._jointStorage.kind : null; + public isTextureStorage () { + return this._binded && isTextureStorage(this._binded.jointStorage); } public bindSkeleton (skeleton: Skeleton) { @@ -75,77 +62,104 @@ export class SkinningModel extends Model { if (storageKind === JointStorageKind.textureRGBA32F || storageKind === JointStorageKind.textureRGBA8) { - const jointsTexture = _createJointsTexture(skeleton, storageKind); - textureSizeBuffer[0] = jointsTexture.width; - this._skinningUBO.update( - textureSizeBuffer, UBOSkinning.JOINTS_TEXTURE_SIZE_OFFSET, textureSizeBuffer.byteLength); - if (storageKind === JointStorageKind.textureRGBA32F) { - this._jointStorage = { - kind: JointStorageKind.textureRGBA32F, - texture: jointsTexture, - nativeData: new Float32Array(jointsTexture.width * jointsTexture.height * pixelsPerJointOfFJointTexture * 4), - }; - } else { - this._jointStorage = { - kind: JointStorageKind.textureRGBA8, - texture: jointsTexture, - nativeData: new Uint8Array(jointsTexture.width * jointsTexture.height * pixelsPerJointOfBJointTexture * 4), - }; + const mat4TextureKind = storageKind === JointStorageKind.textureRGBA32F ? Mat4TextureKind.rgba32f : Mat4TextureKind.rgba8888; + const mat4Texture = new MatrixTexture(skeleton.joints.length * 2, mat4TextureKind); + + for (let iJoint = 0; iJoint < skeleton.bindposes.length; ++iJoint) { + mat4Texture.set(iJoint * 2 + 0, skeleton.bindposes[iJoint]); } + + this._binded = { + skinningUBOBinding: UBOSkinningTextureCase.BLOCK.binding, + skinningUBO: this._device.createBuffer({ + usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, + memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, + size: UBOSkinningTextureCase.SIZE, + stride: UBOSkinningTextureCase.SIZE, + }), + jointStorage: { + kind: storageKind, + texture: mat4Texture, + }, + }; + + textureSizeBuffer[0] = mat4Texture.texture.width; + this._binded.skinningUBO.update( + textureSizeBuffer, UBOSkinningTextureCase.JOINTS_TEXTURE_SIZE_OFFSET, textureSizeBuffer.byteLength); } else { if (skeleton.joints.length > JointUniformCapacity) { console.error( `Skeleton ${skeleton.name} has ${skeleton.joints.length} joints ` + `which exceeds Cocos's max allowed joint count(${JointUniformCapacity}).`); } - this._jointStorage = { - kind: JointStorageKind.uniform, - nativeData: new Float32Array(skeleton.joints.length * 12), + + this._binded = { + skinningUBOBinding: UBOSkinning.BLOCK.binding, + skinningUBO: this._device.createBuffer({ + usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, + memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, + size: UBOSkinning.SIZE, + stride: UBOSkinning.SIZE, + }), + jointStorage: { + kind: JointStorageKind.uniform, + nativeData: new Float32Array(skeleton.joints.length * 12), + }, }; } } public updateJointMatrix (iMatrix: number, matrix: mat4) { - if (!this._jointStorage) { + if (!this._binded) { return; } - _setJointMatrix(this._jointStorage.nativeData, iMatrix, matrix, this._jointStorage.kind); + const { jointStorage, skinningUBO } = this._binded; + if (isTextureStorage(jointStorage)) { + jointStorage.texture.set(iMatrix * 2 + 1, matrix); + } else { + setMat4InUniform(jointStorage.nativeData, iMatrix, matrix); + } } public commitJointMatrices () { - if (!this._jointStorage) { + if (!this._binded) { return; } - if (isTextureStorage(this._jointStorage)) { - this._jointStorage.texture.directUpdate(this._jointStorage!.nativeData.buffer); + const { jointStorage, skinningUBO } = this._binded; + if (isTextureStorage(jointStorage)) { + jointStorage.texture.commit(); } else { - this._skinningUBO.update(this._jointStorage.nativeData, UBOSkinning.MAT_JOINT_OFFSET); + skinningUBO.update(jointStorage.nativeData, UBOSkinning.MAT_JOINT_OFFSET); } } protected _doCreatePSO (pass: Pass) { const pso = super._doCreatePSO(pass); - pso.pipelineLayout.layouts[0].bindBuffer(UBOSkinning.BLOCK.binding, this._skinningUBO); - if (this._jointStorage && isTextureStorage(this._jointStorage)) { - const jointTexture = this._jointStorage.texture; - const view = jointTexture.getGFXTextureView(); - const sampler = samplerLib.getSampler(this._device, jointTexture.getGFXSamplerInfo()); - if (view && sampler) { - pso.pipelineLayout.layouts[0].bindTextureView(UNIFORM_JOINTS_TEXTURE.binding, view); - pso.pipelineLayout.layouts[0].bindSampler(UNIFORM_JOINTS_TEXTURE.binding, sampler); + if (this._binded) { + const { jointStorage, skinningUBO, skinningUBOBinding } = this._binded; + pso.pipelineLayout.layouts[0].bindBuffer(skinningUBOBinding, skinningUBO); + if (isTextureStorage(jointStorage)) { + const jointTexture = jointStorage.texture.texture; + const view = jointTexture.getGFXTextureView(); + const sampler = samplerLib.getSampler(this._device, jointTexture.getGFXSamplerInfo()); + if (view && sampler) { + pso.pipelineLayout.layouts[0].bindTextureView(UNIFORM_JOINTS_TEXTURE.binding, view); + pso.pipelineLayout.layouts[0].bindSampler(UNIFORM_JOINTS_TEXTURE.binding, sampler); + } } } return pso; } private _destroyJointStorage () { - if (!this._jointStorage) { - return; + if (this._binded) { + const { jointStorage, skinningUBO } = this._binded; + if (isTextureStorage(jointStorage)) { + jointStorage.texture.destroy(); + } + skinningUBO.destroy(); + this._binded = null; } - if (isTextureStorage(this._jointStorage)) { - this._jointStorage.texture.destroy(); - } - this._jointStorage = null; } private _selectStorageKind (): JointStorageKind { @@ -165,84 +179,135 @@ export function selectStorageKind (device: GFXDevice): JointStorageKind { } } -function _setJointMatrix (out: Float32Array | Uint8Array, iMatrix: number, matrix: mat4, kind: JointStorageKind) { - if (kind === JointStorageKind.uniform) { - const base = 12 * iMatrix; - // Discard the last row - out[base + 0] = matrix.m00; - out[base + 1] = matrix.m01; - out[base + 2] = matrix.m02; - out[base + 3] = matrix.m04; - out[base + 4] = matrix.m05; - out[base + 5] = matrix.m06; - out[base + 6] = matrix.m08; - out[base + 7] = matrix.m09; - out[base + 8] = matrix.m10; - out[base + 9] = matrix.m12; - out[base + 10] = matrix.m13; - out[base + 11] = matrix.m14; - } else if (kind === JointStorageKind.textureRGBA32F) { - mat4.array(out, matrix, iMatrix * pixelsPerJointOfFJointTexture * 4); - } else { - const base = iMatrix * 16 * 4; - const o = out as Uint8Array; - encode32(matrix.m00, o, base + 0 * 4); - encode32(matrix.m01, o, base + 1 * 4); - encode32(matrix.m02, o, base + 2 * 4); - encode32(matrix.m03, o, base + 3 * 4); - encode32(matrix.m04, o, base + 4 * 4); - encode32(matrix.m05, o, base + 5 * 4); - encode32(matrix.m06, o, base + 6 * 4); - encode32(matrix.m07, o, base + 7 * 4); - encode32(matrix.m08, o, base + 8 * 4); - encode32(matrix.m09, o, base + 9 * 4); - encode32(matrix.m10, o, base + 10 * 4); - encode32(matrix.m11, o, base + 11 * 4); - encode32(matrix.m12, o, base + 12 * 4); - encode32(matrix.m13, o, base + 13 * 4); - encode32(matrix.m14, o, base + 14 * 4); - encode32(matrix.m15, o, base + 15 * 4); - } +function setMat4InUniform (out: Float32Array, iMatrix: number, matrix: mat4) { + const base = 12 * iMatrix; + // Discard the last row + out[base + 0] = matrix.m00; + out[base + 1] = matrix.m01; + out[base + 2] = matrix.m02; + out[base + 3] = matrix.m04; + out[base + 4] = matrix.m05; + out[base + 5] = matrix.m06; + out[base + 6] = matrix.m08; + out[base + 7] = matrix.m09; + out[base + 8] = matrix.m10; + out[base + 9] = matrix.m12; + out[base + 10] = matrix.m13; + out[base + 11] = matrix.m14; } -function createTointTextureCapacityTable (maxCapacity: number, pixelsPerJoint: number) { - const result: Array<{ textureExtent: number; capacity: number; }> = []; - for (let i = 0; ; ++i) { - const textureExtent = 1 << i; - const capacity = (textureExtent * textureExtent) / pixelsPerJoint; - if (capacity < 1) { - continue; - } - result.push({ textureExtent, capacity }); - if (capacity >= maxCapacity) { - break; - } - } - return result; +enum Mat4TextureKind { + rgba8888, + rgba32f, } -const fJointTextureCapacityTable = createTointTextureCapacityTable(1024, pixelsPerJointOfFJointTexture); -const bJointTextureCapacityTable = createTointTextureCapacityTable(1024, pixelsPerJointOfBJointTexture); +class MatrixTexture { -function _createJointsTexture (skinning: { joints: any[]; }, kind: JointStorageKind.textureRGBA8 | JointStorageKind.textureRGBA32F) { - const flt = (kind === JointStorageKind.textureRGBA32F); - const jointCount = skinning.joints.length; - let textureExtent = -1; - for (const item of (flt ? fJointTextureCapacityTable : bJointTextureCapacityTable)) { - if (item.capacity >= jointCount) { - textureExtent = item.textureExtent; - break; + get texture () { + return this._texture; + } + + private static Mat4TextureAttributes = { + [Mat4TextureKind.rgba8888]: { + localStorage: Uint8Array, + pixelsPerMatrix: 16, + capacityTable: MatrixTexture.createCapacityTable(1024, 16), + pixelFormat: PixelFormat.RGBA8888, + }, + [Mat4TextureKind.rgba32f]: { + localStorage: Float32Array, + pixelsPerMatrix: 4, + capacityTable: MatrixTexture.createCapacityTable(1024, 4), + pixelFormat: PixelFormat.RGBA32F, + }, + }; + private static createCapacityTable (maxCapacity: number, pixelsPerJoint: number) { + const result: Array<{ textureExtent: number; capacity: number; }> = []; + for (let i = 0; ; ++i) { + const textureExtent = 1 << i; + if (textureExtent % pixelsPerJoint !== 0) { + continue; + } + const rowCapacity = textureExtent / pixelsPerJoint; + if (rowCapacity < 1) { + continue; + } + const capacity = textureExtent * rowCapacity; + result.push({ textureExtent, capacity }); + if (capacity >= maxCapacity) { + break; + } + } + return result; + } + + private _texture: Texture2D; + private _nativeData: Float32Array | Uint8Array; + private _kind: Mat4TextureKind; + + /** + * Creates a texture which is able to store matrices(4x4). + * The matrices is directly emplaced (in textureRGBA32F case) or encoded (in textureRGBA8888 case), + * one by one, in column major order, into texture pixels, without any spacing pixels. + * Every row of texture is guaranteed to store integer multiple of matrix count. + * This means when retrive a single matrix from such a texture, every matrix element has a same texcoord 'v', + * and their texcoord 'u' is incremented by '1 step'. + * @param capacity The count of matrices this texture is able store. + * @param kind The texture format. + * @throws Throws error if matrix count is too large. + */ + constructor (capacity: number, kind: Mat4TextureKind) { + this._kind = kind; + const attribute = MatrixTexture.Mat4TextureAttributes[kind]; + let textureExtent = -1; + for (const item of attribute.capacityTable) { + if (item.capacity >= capacity) { + textureExtent = item.textureExtent; + break; + } + } + if (textureExtent < 0) { + throw new Error('Too many joints.'); + } + this._texture = new Texture2D(); + this._texture.create(textureExtent, textureExtent, attribute.pixelFormat); + this._texture.setFilters(Filter.NEAREST, Filter.NEAREST); + this._texture.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE); + this._nativeData = new (attribute.localStorage)(this._texture.width * this._texture.height * attribute.pixelsPerMatrix * 4); + } + + public destroy () { + this._texture.destroy(); + } + + public set (iMatrix: number, matrix: mat4) { + if (this._kind === Mat4TextureKind.rgba32f) { + mat4.array(this._nativeData, matrix, iMatrix * 4 * 4); + } else { + const base = iMatrix * 16 * 4; + const o = this._nativeData as Uint8Array; + encode32(matrix.m00, o, base + 0 * 4); + encode32(matrix.m01, o, base + 1 * 4); + encode32(matrix.m02, o, base + 2 * 4); + encode32(matrix.m03, o, base + 3 * 4); + encode32(matrix.m04, o, base + 4 * 4); + encode32(matrix.m05, o, base + 5 * 4); + encode32(matrix.m06, o, base + 6 * 4); + encode32(matrix.m07, o, base + 7 * 4); + encode32(matrix.m08, o, base + 8 * 4); + encode32(matrix.m09, o, base + 9 * 4); + encode32(matrix.m10, o, base + 10 * 4); + encode32(matrix.m11, o, base + 11 * 4); + encode32(matrix.m12, o, base + 12 * 4); + encode32(matrix.m13, o, base + 13 * 4); + encode32(matrix.m14, o, base + 14 * 4); + encode32(matrix.m15, o, base + 15 * 4); } } - if (textureExtent < 0) { - throw new Error('Too many joints.'); - } - const texture = new Texture2D(); - texture.create(textureExtent, textureExtent, (flt ? PixelFormat.RGBA32F : PixelFormat.RGBA8888)); - texture.setFilters(Filter.NEAREST, Filter.NEAREST); - texture.setWrapMode(WrapMode.CLAMP_TO_EDGE, WrapMode.CLAMP_TO_EDGE); - return texture; + public commit () { + this._texture.directUpdate(this._nativeData.buffer); + } } const encode32 = (() => { @@ -252,71 +317,81 @@ const encode32 = (() => { const EXP2_XX_4 = exp2(23.0); const EXP2_XX_5 = exp2(-15.0); + function decode32 (r: number, g: number, b: number, a: number) { + const Sign = 1.0 - step(128.0, r) * 2.0; + const Exponent = 2.0 * mod(r, 128.0) + step(128.0, g) - 127.0; + + // From Arjan's answer. + if (Exponent === -127) { + return 0; + } + + const Mantissa = mod(g, 128.0) * 65536.0 + b * 256.0 + a + (0x800000); + + // Original + // const Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); + // return Result; + + // From Arjan's answer. + return Sign * exp2(Exponent - 23.0) * Mantissa; + } + return (f: number, output: Uint8Array, offset: number) => { - // https://stackoverflow.com/questions/7059962/how-do-i-convert-a-vec4-rgba-value-to-a-float - // http://www.shaderific.com/glsl-functions/ - const e = 5.0; - const F = Math.abs(f); + f = numberToFloat32(f); - // From Arjan's answer. - if (F === 0) { - output[offset + 0] = 0; - output[offset + 1] = 0; - output[offset + 2] = 0; - output[offset + 3] = 0; - return; - } + // https://stackoverflow.com/questions/7059962/how-do-i-convert-a-vec4-rgba-value-to-a-float + // http://www.shaderific.com/glsl-functions/ + const e = 5.0; + const F = Math.abs(f); - const Sign = step(0.0, -f); - let Exponent = Math.floor(Math.log2(F)); + // From Arjan's answer. + if (F === 0) { + output[offset + 0] = 0; + output[offset + 1] = 0; + output[offset + 2] = 0; + output[offset + 3] = 0; + return; + } - // Original. - // const Mantissa = (exp2(- Exponent) * F); - // Exponent = Math.floor(Math.log2(F) + 127.0) + Math.floor(Math.log2(Mantissa)); + const Sign = step(0.0, -f); + let Exponent = Math.floor(Math.log2(F)); - // From Arjan's answer. - const Mantissa = F / exp2(Exponent); - if (Mantissa < 1) { - Exponent -= 1; - } - Exponent += 127; + // Original. + // const Mantissa = (exp2(- Exponent) * F); + // Exponent = Math.floor(Math.log2(F) + 127.0) + Math.floor(Math.log2(Mantissa)); - const r = 128.0 * Sign + Math.floor(Exponent * EXP2_XX_1); - const g = 128.0 * mod(Exponent, 2.0) + mod(Math.floor(Mantissa * 128.0), 128.0); - const b = Math.floor(mod(Math.floor(Mantissa * EXP2_XX_2), EXP2_XX_3)); - const a = Math.floor(EXP2_XX_4 * mod(Mantissa, EXP2_XX_5)); + // From Arjan's answer. + const Mantissa = F / exp2(Exponent); + if (Mantissa < 1) { + Exponent -= 1; + } + Exponent += 127; - // Verification. - // const g = decode32(r, g, b, a); - // if (Math.abs(g - f) > 0.00001) { - // f = g; - // } + const r = 128.0 * Sign + Math.floor(Exponent * EXP2_XX_1); + const g = 128.0 * mod(Exponent, 2.0) + mod(Math.floor(Mantissa * 128.0), 128.0); + const b = Math.floor(mod(Math.floor(Mantissa * EXP2_XX_2), EXP2_XX_3)); + const a = Math.floor(EXP2_XX_4 * mod(Mantissa, EXP2_XX_5)); - output[offset + 0] = r; - output[offset + 1] = g; - output[offset + 2] = b; - output[offset + 3] = a; + // Verification. + // const x = decode32(r, g, b, a); + // if (Math.abs(x - f) > 0.00001) { + // f = x; + // } + + output[offset + 0] = r; + output[offset + 1] = g; + output[offset + 2] = b; + output[offset + 3] = a; }; })(); -function decode32 (r: number, g: number, b: number, a: number) { - const Sign = 1.0 - step(128.0, r) * 2.0; - const Exponent = 2.0 * mod(r, 128.0) + step(128.0, g) - 127.0; - - // From Arjan's answer. - if (Exponent === -127) { - return 0; - } - - const Mantissa = mod(g, 128.0) * 65536.0 + b * 256.0 + a + (0x800000); - - // Original - // const Result = Sign * exp2(Exponent) * (Mantissa * exp2(-23.0 )); - // return Result; - - // From Arjan's answer. - return Sign * exp2(Exponent - 23.0) * Mantissa; -} +const numberToFloat32 = (() => { + const f32storage = new Float32Array(1); + return (x: number) => { + f32storage[0] = x; + return f32storage[0]; + }; +})(); function step (edge: number, x: number): number { return x < edge ? 0.0 : 1.0; @@ -329,3 +404,11 @@ function mod (x: number, y: number): number { function exp2 (x: number): number { return Math.pow(2, x); } + +function int (x: number) { + return Math.trunc(x); +} + +function fract (x: number) { + return x - Math.floor(x); +}