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 b9fac4ce7b..d3f1483175 100644 --- a/cocos/3d/framework/skinning-model-component.ts +++ b/cocos/3d/framework/skinning-model-component.ts @@ -154,22 +154,21 @@ export class SkinningModelComponent extends ModelComponent { const skeleton = this._skeleton; const skinningTarget = this._skinningTarget; - mat4.invert(_m4_tmp2, this.node.worldMatrix); - 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, _m4_tmp2, 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/3d/ui/assembler/label/bmfontUtils.ts b/cocos/3d/ui/assembler/label/bmfontUtils.ts index 693268e47f..0e56004749 100644 --- a/cocos/3d/ui/assembler/label/bmfontUtils.ts +++ b/cocos/3d/ui/assembler/label/bmfontUtils.ts @@ -160,7 +160,7 @@ export const bmfontUtils = { this._updateProperties(); this._updateContent(); - // _comp._actualFontSize = _fontSize; + _comp.actualFontSize = _fontSize; _comp.node.setContentSize(_contentSize); _comp.renderData!.vertDirty = _comp.renderData!.uvDirty = false; diff --git a/cocos/3d/ui/assembler/label/letter-font.ts b/cocos/3d/ui/assembler/label/letter-font.ts index d02bb2ed2e..4bffdcdbfd 100644 --- a/cocos/3d/ui/assembler/label/letter-font.ts +++ b/cocos/3d/ui/assembler/label/letter-font.ts @@ -490,7 +490,7 @@ export const letterFont = { this._updateContent(); - // _comp._actualFontSize = _fontSize; + _comp.actualFontSize = _fontSize; _comp.node.setContentSize(_contentSize); _comp.renderData!.vertDirty = _comp.renderData!.uvDirty = false; diff --git a/cocos/3d/ui/assembler/label/ttfUtils.ts b/cocos/3d/ui/assembler/label/ttfUtils.ts index 704496429b..dbd0e6b1ab 100644 --- a/cocos/3d/ui/assembler/label/ttfUtils.ts +++ b/cocos/3d/ui/assembler/label/ttfUtils.ts @@ -30,6 +30,7 @@ import { Color, Size } from '../../../../core/value-types'; import { HorizontalTextAlignment, LabelComponent, VerticalTextAlignment } from '../../components/label-component'; import { LabelOutlineComponent } from '../../components/label-outline-component'; import { CanvasPool, ISharedLabelData } from './font-utils'; +import { LetterRenderTexture } from './letter-font'; const Overflow = LabelComponent.Overflow; const WHITE = Color.WHITE; @@ -37,7 +38,7 @@ const OUTLINE_SUPPORTED = cc.js.isChildClassOf(LabelOutlineComponent, Component) let _context: CanvasRenderingContext2D | null = null; let _canvas: HTMLCanvasElement | null = null; -let _texture: SpriteFrame | null = null; +let _texture: SpriteFrame | LetterRenderTexture | null = null; let _fontDesc = ''; let _string = ''; @@ -102,7 +103,7 @@ export const ttfUtils = { this._calculateTextBaseline(); this._updateTexture(); - // comp._actualFontSize = _fontSize; + comp.actualFontSize = _fontSize; comp.node.setContentSize(_canvasSize); this.updateVerts(comp); diff --git a/cocos/3d/ui/components/canvas-component.ts b/cocos/3d/ui/components/canvas-component.ts index 2509a17d2d..dcf160e800 100644 --- a/cocos/3d/ui/components/canvas-component.ts +++ b/cocos/3d/ui/components/canvas-component.ts @@ -139,17 +139,18 @@ export class CanvasComponent extends Component { * @default new cc.Size(960, 640) */ @property - private _designResolution = cc.size(960, 640); + protected _designResolution = cc.size(960, 640); @property - private _fitWidth = false; + protected _fitWidth = false; @property - private _fitHeight = true; + protected _fitHeight = true; @property - private _priority = 0; + protected _priority = 0; - private _thisOnResized: () => void; + protected _thisOnResized: () => void; - private _camera: Camera | null = null; + protected _camera: Camera | null = null; + private _pos = new Vec3(); constructor () { super(); @@ -218,10 +219,14 @@ export class CanvasComponent extends Component { public alignWithScreen () { let nodeSize; let designSize; + this.node.getPosition(this._pos); if (CC_EDITOR) { // nodeSize = designSize = cc.engine.getDesignResolutionSize(); nodeSize = designSize = this._designResolution; - this.node.setPosition(designSize.width * 0.5, designSize.height * 0.5, 1); + _tempPos.x = designSize.width * 0.5; + _tempPos.y = designSize.height * 0.5; + _tempPos.z = 0; + } else { const canvasSize = cc.visibleRect; @@ -235,7 +240,14 @@ export class CanvasComponent extends Component { offsetX = (designSize.width - canvasSize.width) * 0.5; offsetY = (designSize.height - canvasSize.height) * 0.5; } - this.node.setPosition(canvasSize.width * 0.5 + offsetX, canvasSize.height * 0.5 + offsetY, 1); + + _tempPos.x = canvasSize.width * 0.5 + offsetX; + _tempPos.y = canvasSize.height * 0.5 + offsetY; + _tempPos.z = 0; + } + + if (this._pos.equals(_tempPos)) { + this.node.setPosition(_tempPos); } if (this.node.width !== nodeSize.width) { @@ -275,6 +287,11 @@ export class CanvasComponent extends Component { // cc.engine.setDesignResolutionSize(designRes.width, designRes.height); } else { + const root = cc.director.root; + if (root && root.ui && root.ui.debugScreen && root.ui.debugScreen === this ){ + return; + } + cc.view.setDesignResolutionSize(designRes.width, designRes.height, policy); } } diff --git a/cocos/3d/ui/components/debug-canvas-component.ts b/cocos/3d/ui/components/debug-canvas-component.ts new file mode 100644 index 0000000000..274d6b9a33 --- /dev/null +++ b/cocos/3d/ui/components/debug-canvas-component.ts @@ -0,0 +1,63 @@ +import { Vec3 } from '../../../core/value-types'; +import { CanvasComponent } from './canvas-component'; + +const _worldPos = new Vec3(); +export class DebugCanvasComponent extends CanvasComponent{ + constructor (){ + super(); + this._thisOnResized = this.alignWithScreen.bind(this); + } + + // don't remove + public onEnable (){ + } + + // don't remove + public onDisable (){ + } + + public onDestroy (){ + if (CC_EDITOR) { + cc.director.off(cc.Director.EVENT_AFTER_UPDATE, this.alignWithScreen, this); + } + + cc.view.off('design-resolution-changed', this._thisOnResized); + } + + public alignWithScreen (){ + const canvasSize = cc.visibleRect; + const nodeSize = canvasSize; + const designSize = cc.view.getDesignResolutionSize(); + const clipTopRight = !this.fitHeight && !this.fitWidth; + let offsetX = 0; + let offsetY = 0; + if (clipTopRight) { + // offset the canvas to make it in the center of screen + offsetX = (designSize.width - canvasSize.width) * 0.5; + offsetY = (designSize.height - canvasSize.height) * 0.5; + } + this.node.setPosition(canvasSize.width * 0.5 + offsetX, canvasSize.height * 0.5 + offsetY, 1); + + if (this.node.width !== nodeSize.width) { + this.node.width = nodeSize.width; + } + + if (this.node.height !== nodeSize.height) { + this.node.height = nodeSize.height; + } + + this.node.getWorldPosition(_worldPos); + if (this._camera) { + const size = cc.view.getVisibleSize(); + this._camera.resize(size.width, size.height); + this._camera.orthoHeight = this._camera.height / 2; + this._camera.node.setPosition(_worldPos.x, _worldPos.y, 1000); + this._camera.update(); + } + } + + // don't remove + public applySettings (){ + } + +} diff --git a/cocos/3d/ui/components/graphics-component.ts b/cocos/3d/ui/components/graphics-component.ts index 4e020bc646..72dae290c7 100644 --- a/cocos/3d/ui/components/graphics-component.ts +++ b/cocos/3d/ui/components/graphics-component.ts @@ -209,7 +209,7 @@ export class GraphicsComponent extends UIRenderComponent { super.__preload(); } - this._flushAssembler(); + // this._flushAssembler(); this.impl = this._assembler && (this._assembler as IAssembler).createImpl!(this); } diff --git a/cocos/3d/ui/components/label-component.ts b/cocos/3d/ui/components/label-component.ts index bd3caa6026..1618c7456c 100644 --- a/cocos/3d/ui/components/label-component.ts +++ b/cocos/3d/ui/components/label-component.ts @@ -238,12 +238,15 @@ export class LabelComponent extends UIRenderComponent { @property({ readonly: true, displayName: 'Actual Font Size', - visible: false, }) get actualFontSize () { return this._actualFontSize; } + set actualFontSize (value) { + this._actualFontSize = value; + } + /** * !#en Font size of label. * !#zh 文本字体大小。 @@ -672,7 +675,7 @@ export class LabelComponent extends UIRenderComponent { } protected _canRender () { - if (!super._canRender){ + if (!super._canRender()){ return false; } diff --git a/cocos/3d/ui/components/mask-component.ts b/cocos/3d/ui/components/mask-component.ts index 52e062dfb2..b9d2400b92 100644 --- a/cocos/3d/ui/components/mask-component.ts +++ b/cocos/3d/ui/components/mask-component.ts @@ -251,7 +251,7 @@ export class MaskComponent extends UIRenderComponent { } public onLoad (){ - this._flushAssembler(); + // this._flushAssembler(); this._createGraphics(); } diff --git a/cocos/3d/ui/components/sprite-component.ts b/cocos/3d/ui/components/sprite-component.ts index b8067cd083..ae527a638a 100644 --- a/cocos/3d/ui/components/sprite-component.ts +++ b/cocos/3d/ui/components/sprite-component.ts @@ -32,7 +32,7 @@ import { clampf } from '../../../core/utils/misc'; import { Vec2 } from '../../../core/value-types'; import { ccenum } from '../../../core/value-types/enum'; import { UI } from '../../../renderer/ui/ui'; -import { InstanceMaterialType, UIRenderComponent } from './ui-render-component'; +import { UIRenderComponent } from './ui-render-component'; /** * !#en Enum for sprite type. @@ -431,7 +431,7 @@ export class SpriteComponent extends UIRenderComponent { } } - this._flushAssembler(); + // this._flushAssembler(); this._activateMaterial(); } diff --git a/cocos/3d/ui/components/toggle-component.ts b/cocos/3d/ui/components/toggle-component.ts index 944ec5a1b1..df4f9b5f91 100644 --- a/cocos/3d/ui/components/toggle-component.ts +++ b/cocos/3d/ui/components/toggle-component.ts @@ -27,7 +27,7 @@ import { EventHandler as ComponentEventHandler } from '../../../components/compo import { ccclass, executeInEditMode, executionOrder, menu, property } from '../../../core/data/class-decorator'; import { ButtonComponent} from './button-component'; import { SpriteComponent} from './sprite-component'; -import { ToggleContainerComponent} from './toggle-group-component'; +import { ToggleContainerComponent} from './toggle-container-component'; /** * !#en The toggle component is a CheckBox, when it used together with a ToggleGroup, it diff --git a/cocos/3d/ui/components/toggle-group-component.ts b/cocos/3d/ui/components/toggle-container-component.ts similarity index 99% rename from cocos/3d/ui/components/toggle-group-component.ts rename to cocos/3d/ui/components/toggle-container-component.ts index 960121a994..4abe26cdc2 100644 --- a/cocos/3d/ui/components/toggle-group-component.ts +++ b/cocos/3d/ui/components/toggle-container-component.ts @@ -39,7 +39,7 @@ import { ToggleComponent} from './toggle-component'; @ccclass('cc.ToggleContainerComponent') @executionOrder(100) -@menu('UI/ToggleGroup') +@menu('UI/ToggleContainer') @executeInEditMode export class ToggleContainerComponent extends Component { @property({ diff --git a/cocos/3d/ui/components/ui-model-component.ts b/cocos/3d/ui/components/ui-model-component.ts index 3a4501cffb..edd6a85f11 100644 --- a/cocos/3d/ui/components/ui-model-component.ts +++ b/cocos/3d/ui/components/ui-model-component.ts @@ -12,12 +12,12 @@ import { UIComponent } from './ui-component'; @menu('UI/Model') export class UIModelComponent extends UIComponent { - private _modelComponent: RenderableComponent | null = null; - public get modelComponent () { return this._modelComponent; } + private _modelComponent: RenderableComponent | null = null; + public onLoad () { this._modelComponent = this.getComponent(cc.RenderableComponent) as RenderableComponent; (this._modelComponent as any)._sceneGetter = cc.director.root.ui.getRenderSceneGetter(); @@ -27,6 +27,19 @@ export class UIModelComponent extends UIComponent { this._fitUIRenderQueue(); } + public updateAssembler (render: UI) { + if (this._modelComponent) { + render.commitComp.call(render, this); + return true; + } + + return false; + } + + public update () { + this._fitUIRenderQueue(); + } + private _fitUIRenderQueue () { if (!this._modelComponent) { return; @@ -58,17 +71,4 @@ export class UIModelComponent extends UIComponent { } } } - - public updateAssembler (render: UI) { - if (this._modelComponent) { - render.commitComp.call(render, this); - return true; - } - - return false; - } - - public update () { - this._fitUIRenderQueue(); - } } diff --git a/cocos/3d/ui/components/ui-render-component.ts b/cocos/3d/ui/components/ui-render-component.ts index 266de385dd..4fb0fbfdbb 100644 --- a/cocos/3d/ui/components/ui-render-component.ts +++ b/cocos/3d/ui/components/ui-render-component.ts @@ -207,6 +207,9 @@ export class UIRenderComponent extends UIComponent { public __preload (){ this._instanceMaterial(); + if (this._flushAssembler){ + this._flushAssembler(); + } } public onEnable () { diff --git a/cocos/3d/ui/components/ui-transfrom-component.ts b/cocos/3d/ui/components/ui-transfrom-component.ts index 9b7286f05f..6dd8f6ffc0 100644 --- a/cocos/3d/ui/components/ui-transfrom-component.ts +++ b/cocos/3d/ui/components/ui-transfrom-component.ts @@ -4,6 +4,7 @@ import { EventType } from '../../../core/platform/event-manager/event-enum'; import { EventListener, ILinstenerMask } from '../../../core/platform/event-manager/event-listener'; import { Mat4, Rect, Size, Vec2, Vec3 } from '../../../core/value-types'; import * as vmath from '../../../core/vmath'; +import { CanvasComponent } from './canvas-component'; const _vec2a = new Vec2(); const _vec2b = new Vec2(); @@ -379,7 +380,7 @@ export class UITransformComponent extends Component { // 获取被渲染相机的 visibility while (parent) { if (parent) { - const canvasComp = parent.getComponent(cc.CanvasComponent); + const canvasComp = parent.getComponent('cc.CanvasComponent') as CanvasComponent; if (canvasComp) { visibility = canvasComp.visibility; break; diff --git a/cocos/3d/ui/index.ts b/cocos/3d/ui/index.ts index bf46fe6d9b..72f0443375 100644 --- a/cocos/3d/ui/index.ts +++ b/cocos/3d/ui/index.ts @@ -13,7 +13,7 @@ export { ScrollViewComponent } from './components/scroll-view-component'; export { SliderComponent } from './components/slider-component'; export { SpriteComponent } from './components/sprite-component'; export { ToggleComponent } from './components/toggle-component'; -export { ToggleContainerComponent } from './components/toggle-group-component'; +export { ToggleContainerComponent } from './components/toggle-container-component'; export { UIModelComponent } from './components/ui-model-component'; export { UIRenderComponent } from './components/ui-render-component'; export { UITransformComponent } from './components/ui-transfrom-component'; @@ -21,3 +21,4 @@ export { ViewGroupComponent } from './components/view-group-component'; export { WebviewComponent } from './components/webview/webView-component'; export { WidgetComponent } from './components/widget-component'; export { LabelOutlineComponent } from './components/label-outline-component'; +export { DebugCanvasComponent } from './components/debug-canvas-component'; diff --git a/cocos/core/platform/event-manager/CCTouch.ts b/cocos/core/platform/event-manager/CCTouch.ts index 11c7ede929..988bb01972 100644 --- a/cocos/core/platform/event-manager/CCTouch.ts +++ b/cocos/core/platform/event-manager/CCTouch.ts @@ -150,6 +150,20 @@ export default class Touch { return out; } + /** + * !#en Returns the start touch location in OpenGL coordinates. + * !#zh 获获取触点落下时的位置对象,对象包含 x 和 y 属性。 + */ + public getUIStartLocation (out?: Vec2) { + if (!out) { + out = new Vec2(); + } + + vec2.set(out, this._startPoint.x, this._startPoint.y); + cc.view._convertPointWithScale(out); + return out; + } + /** * !#en Returns the delta distance from the previous touche to the current one in screen coordinates. * !#zh 获取触点距离上一次事件移动的距离对象,对象包含 x 和 y 属性。 @@ -235,7 +249,7 @@ export default class Touch { this._id = id; if (!this._startPointCaptured) { this._startPoint = new Vec2(this._point); - cc.view._convertPointWithScale(this._startPoint); + // cc.view._convertPointWithScale(this._startPoint); this._startPointCaptured = true; } } diff --git a/cocos/core/utils/profiler/profiler.ts b/cocos/core/utils/profiler/profiler.ts index 70f0e6dde1..efad7c8819 100644 --- a/cocos/core/utils/profiler/profiler.ts +++ b/cocos/core/utils/profiler/profiler.ts @@ -24,7 +24,7 @@ THE SOFTWARE. ****************************************************************************/ -import { CanvasComponent} from '../../../3d/ui/components/canvas-component'; +import { DebugCanvasComponent } from '../../../3d/ui/components/debug-canvas-component'; import { LabelComponent } from '../../../3d/ui/components/label-component'; import { UITransformComponent } from '../../../3d/ui/components/ui-transfrom-component'; import { WidgetComponent } from '../../../3d/ui/components/widget-component'; @@ -171,8 +171,8 @@ function generateNode () { _rootNode = new Node('PROFILER-NODE'); _rootNode.addComponent(UITransformComponent); - _rootNode.addComponent(CanvasComponent); - cc.director.root.ui.debugScreen = _rootNode; + const screen = _rootNode.addComponent(DebugCanvasComponent); + cc.director.root.ui.debugScreen = screen; cc.game.addPersistRootNode(_rootNode); // const camera = cc.director.root. @@ -214,7 +214,7 @@ function generateNode () { rightLabel.horizontalAlign = cc.LabelComponent.HorizontalAlign.RIGHT; rightLabel.font = _atlas; rightLabel.fontSize = _fontSize; - rightLabel.lineHeight = _fontSize; + rightLabel.lineHeight = _fontSize + 1; } _label[stringLeft] = leftLabel; 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); +} diff --git a/cocos/renderer/ui/ui.ts b/cocos/renderer/ui/ui.ts index bdc1a50019..1c7f54d53b 100644 --- a/cocos/renderer/ui/ui.ts +++ b/cocos/renderer/ui/ui.ts @@ -80,12 +80,6 @@ export class UI { set debugScreen (value){ this._debugScreen = value; - if (this._debugScreen){ - const screen = this.getScreen(this._debugScreen.visibility); - if (screen) { - this.removeScreen(screen.visibility); - } - } } public device: GFXDevice; @@ -187,9 +181,11 @@ export class UI { public addScreen (comp: CanvasComponent) { this._screens.push(comp); + if (comp.camera) { comp.camera.view.visibility = this._screens.length; } + } public getScreen (visibility: number) { @@ -280,7 +276,7 @@ export class UI { } } - public commitComp (comp: UIComponent, frame: GFXTextureView | null, assembler: IAssembler) { + public commitComp (comp: UIComponent, frame: GFXTextureView | null = null, assembler?: IAssembler) { if (comp instanceof UIRenderComponent) { const renderComp = comp as UIRenderComponent; const texView = frame; @@ -294,7 +290,9 @@ export class UI { this._currCanvas = renderComp.visibility; } - assembler.fillBuffers(renderComp, this); + if (assembler) { + assembler.fillBuffers(renderComp, this); + } } else { // if the last comp is spriteComp, previous comps should be batched. if (this._currMaterial !== this._emptyMaterial) { diff --git a/cocos/scene-graph/node.ts b/cocos/scene-graph/node.ts index 11889015cc..4ce15ab918 100644 --- a/cocos/scene-graph/node.ts +++ b/cocos/scene-graph/node.ts @@ -18,6 +18,7 @@ enum NodeSpace { LOCAL, WORLD, } +const TRANFORM_ON = 1 << 0; @ccclass('cc.Node') class Node extends BaseNode { @@ -56,6 +57,7 @@ class Node extends BaseNode { protected _eulerDirty = false; protected _eventProcessor; + protected _eventMask = 0; private _uiTransfromComp: UITransformComponent | null = null; @property({ type: Vec3 }) @@ -192,6 +194,7 @@ class Node extends BaseNode { quat.copy(this._rot, this._lrot); vec3.copy(this._scale, this._lscale); this._eulerDirty = this._dirty = this._hasChanged = true; + this._eventMask = 0; for (const child of this._children) { child._onBatchCreated(); } @@ -221,7 +224,9 @@ class Node extends BaseNode { vec3.copy(this._pos, this._lpos); this.invalidateChildren(); - this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART); + if (this._eventMask & TRANFORM_ON) { + this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART); + } } /** @@ -420,7 +425,9 @@ class Node extends BaseNode { vec3.copy(this._pos, this._lpos); this.invalidateChildren(); - this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART); + if (this._eventMask & TRANFORM_ON) { + this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART); + } } /** @@ -465,7 +472,9 @@ class Node extends BaseNode { this._eulerDirty = true; this.invalidateChildren(); - this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART); + if (this._eventMask & TRANFORM_ON) { + this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART); + } } /** @@ -481,7 +490,9 @@ class Node extends BaseNode { quat.copy(this._rot, this._lrot); this.invalidateChildren(); - this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART); + if (this._eventMask & TRANFORM_ON) { + this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART); + } } /** @@ -530,7 +541,9 @@ class Node extends BaseNode { vec3.copy(this._scale, this._lscale); this.invalidateChildren(); - this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART); + if (this._eventMask & TRANFORM_ON) { + this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART); + } } /** @@ -588,7 +601,9 @@ class Node extends BaseNode { } this.invalidateChildren(); - this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART); + if (this._eventMask & TRANFORM_ON) { + this.emit(EventType.TRANSFORM_CHANGED, EventType.POSITION_PART); + } } public getWorldPosition (out: Out): Out; @@ -651,7 +666,9 @@ class Node extends BaseNode { this._eulerDirty = true; this.invalidateChildren(); - this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART); + if (this._eventMask & TRANFORM_ON) { + this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART); + } } /** @@ -671,7 +688,9 @@ class Node extends BaseNode { this._eulerDirty = true; this.invalidateChildren(); - this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART); + if (this._eventMask & TRANFORM_ON) { + this.emit(EventType.TRANSFORM_CHANGED, EventType.ROTATION_PART); + } } public getWorldRotation (out: Out): Out; @@ -731,7 +750,9 @@ class Node extends BaseNode { } this.invalidateChildren(); - this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART); + if (this._eventMask & TRANFORM_ON) { + this.emit(EventType.TRANSFORM_CHANGED, EventType.SCALE_PART); + } } public getWorldScale (out: Out): Out; @@ -825,11 +846,26 @@ class Node extends BaseNode { // Event: maybe remove public on (type: string | EventType, callback: Function, target?: Object, useCapture?: any) { + switch (type) { + case EventType.TRANSFORM_CHANGED: + this._eventMask |= TRANFORM_ON; + break; + } this._eventProcessor.on(type, callback, target, useCapture); } public off (type: string, callback: Function, target?: Object, useCapture?: any) { this._eventProcessor.off(type, callback, target, useCapture); + + var hasListeners = this._eventProcessor.hasEventListener(type); + // All listener removed + if (!hasListeners) { + switch (type) { + case EventType.TRANSFORM_CHANGED: + this._eventMask &= ~TRANFORM_ON; + break; + } + } } public once (type: string, callback: Function, target?: Object, useCapture?: any) { @@ -850,6 +886,10 @@ class Node extends BaseNode { public targetOff (target: string | Object) { this._eventProcessor.targetOff(target); + // Check for event mask reset + if ((this._eventMask & TRANFORM_ON) && !this._eventProcessor.hasEventListener(EventType.TRANSFORM_CHANGED)) { + this._eventMask &= ~TRANFORM_ON; + } } public pauseSystemEvents (recursive: boolean) {