From a63e82dddc5e73ec42f3cd0e792b2d188d0bc565 Mon Sep 17 00:00:00 2001 From: jerrywwl Date: Mon, 4 Mar 2019 11:21:07 +0800 Subject: [PATCH] add ambient and main light to scene --- cocos/3d/builtin/effects.js | 71 ++++++++++--- cocos/3d/builtin/init.ts | 8 ++ cocos/3d/framework/light-component.ts | 15 ++- cocos/core/game.js | 4 + cocos/gfx/webgl/webgl-buffer.ts | 7 +- cocos/gfx/webgl/webgl-commands.ts | 20 ++-- cocos/gfx/webgl/webgl-device.ts | 1 - cocos/gfx/webgl2/webgl2-buffer.ts | 4 +- cocos/gfx/webgl2/webgl2-commands.ts | 111 ++++++++------------- cocos/gfx/webgl2/webgl2-device.ts | 3 +- cocos/pipeline/define.ts | 10 +- cocos/pipeline/forward/forward-flow.ts | 6 +- cocos/pipeline/forward/forward-pipeline.ts | 9 +- cocos/pipeline/render-pipeline.ts | 56 ++++++++++- cocos/renderer/core/pass.ts | 4 +- cocos/renderer/scene/ambient.ts | 49 +++++++++ cocos/renderer/scene/directional-light.ts | 25 +++-- cocos/renderer/scene/light.ts | 22 ++-- cocos/renderer/scene/point-light.ts | 8 +- cocos/renderer/scene/render-scene.ts | 68 +++++++++---- cocos/renderer/scene/spot-light.ts | 10 +- 21 files changed, 347 insertions(+), 164 deletions(-) create mode 100644 cocos/renderer/scene/ambient.ts diff --git a/cocos/3d/builtin/effects.js b/cocos/3d/builtin/effects.js index 2358d67a9c..d14535e167 100644 --- a/cocos/3d/builtin/effects.js +++ b/cocos/3d/builtin/effects.js @@ -9,11 +9,11 @@ export default [ { "name": "builtin-particle|particle-vs-legacy:lpvs_main|tinted-fs:add", "glsl3": { - "vert": `\nprecision mediump float;\nuniform Constants {\n vec4 mainTiling_Offset;\n vec4 frameTile_velLenScale;\n};\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \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};\nuniform CCLocal {\n mat4 cc_matWorld;\n mat4 cc_matWorldIT;\n};\nout vec2 uv;\nout vec4 color;\nvoid computeVertPos(inout vec4 pos, vec2 vertOffset\n#if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , mat4 view\n#endif\n#if USE_STRETCHED_BILLBOARD\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float size\n , float xIndex\n#endif\n) {\n#if USE_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = normalize(vec3(view[0][1], view[1][1], view[2][1]));\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_STRETCHED_BILLBOARD\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz));\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * size;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif USE_HORIZONTAL_BILLBOARD\n vec3 camRight = vec3(1, 0, 0);\n vec3 camUp = vec3(0, 0, -1);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_VERTICAL_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = vec3(0, 1, 0);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV(float frameIndex, vec2 vertIndex, vec2 frameTile) {\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n \n vertIndex.x = 1. - vertIndex.x;\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nvoid rotateCorner(inout vec2 corner, float angle) {\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nin vec3 a_position; \nin vec3 a_texCoord; \nin vec2 a_texCoord1; \nin vec4 a_color;\n#if USE_STRETCHED_BILLBOARD\nin vec3 a_color1; \n#endif\nvec4 lpvs_main() {\n vec4 pos = vec4(a_position, 1);\n#if USE_STRETCHED_BILLBOARD\n vec4 velocity = vec4(a_color1.xyz, 0);\n#endif\n#if !USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if USE_STRETCHED_BILLBOARD\n velocity = cc_matWorld * velocity;\n #endif\n#endif\n vec2 cornerOffset = vec2((a_texCoord.xy - 0.5) * a_texCoord1.x);\n#if !USE_STRETCHED_BILLBOARD\n rotateCorner(cornerOffset, a_texCoord1.y);\n#endif\n computeVertPos(pos, cornerOffset\n #if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , cc_matView\n #endif\n #if USE_STRETCHED_BILLBOARD\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_texCoord1.x\n , a_texCoord.x\n #endif\n );\n pos = cc_matViewProj * pos;\n uv = computeUV(a_texCoord.z, a_texCoord.xy, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = lpvs_main(); }\n`, + "vert": `\nprecision mediump float;\nuniform Constants {\n vec4 mainTiling_Offset;\n vec4 frameTile_velLenScale;\n};\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \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_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};\nout vec2 uv;\nout vec4 color;\nvoid computeVertPos(inout vec4 pos, vec2 vertOffset\n#if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , mat4 view\n#endif\n#if USE_STRETCHED_BILLBOARD\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float size\n , float xIndex\n#endif\n) {\n#if USE_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = normalize(vec3(view[0][1], view[1][1], view[2][1]));\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_STRETCHED_BILLBOARD\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz));\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * size;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif USE_HORIZONTAL_BILLBOARD\n vec3 camRight = vec3(1, 0, 0);\n vec3 camUp = vec3(0, 0, -1);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_VERTICAL_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = vec3(0, 1, 0);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV(float frameIndex, vec2 vertIndex, vec2 frameTile) {\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n \n vertIndex.x = 1. - vertIndex.x;\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nvoid rotateCorner(inout vec2 corner, float angle) {\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nin vec3 a_position; \nin vec3 a_texCoord; \nin vec2 a_texCoord1; \nin vec4 a_color;\n#if USE_STRETCHED_BILLBOARD\nin vec3 a_color1; \n#endif\nvec4 lpvs_main() {\n vec4 pos = vec4(a_position, 1);\n#if USE_STRETCHED_BILLBOARD\n vec4 velocity = vec4(a_color1.xyz, 0);\n#endif\n#if !USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if USE_STRETCHED_BILLBOARD\n velocity = cc_matWorld * velocity;\n #endif\n#endif\n vec2 cornerOffset = vec2((a_texCoord.xy - 0.5) * a_texCoord1.x);\n#if !USE_STRETCHED_BILLBOARD\n rotateCorner(cornerOffset, a_texCoord1.y);\n#endif\n computeVertPos(pos, cornerOffset\n #if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , cc_matView\n #endif\n #if USE_STRETCHED_BILLBOARD\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_texCoord1.x\n , a_texCoord.x\n #endif\n );\n pos = cc_matViewProj * pos;\n uv = computeUV(a_texCoord.z, a_texCoord.xy, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = lpvs_main(); }\n`, "frag": `\nprecision mediump float;\nin vec2 uv;\nin vec4 color;\nuniform sampler2D mainTexture;\nuniform FragConstants {\n vec4 tintColor;\n};\nvec4 add () {\n return 2.0 * color * tintColor * texture(mainTexture, uv);\n}\nvec4 multiply () {\n vec4 col;\n vec4 texColor = texture(mainTexture, uv);\n col.rgb = tintColor.rgb * texColor.rgb * color.rgb * vec3(2.0);\n col.a = (1.0 - texColor.a) * (tintColor.a * color.a * 2.0);\n return col;\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = add(); }\n` }, "glsl1": { - "vert": `\nprecision mediump float;\nuniform vec4 mainTiling_Offset;\nuniform vec4 frameTile_velLenScale;\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\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 mat4 cc_matWorld;\nuniform mat4 cc_matWorldIT;\nvarying vec2 uv;\nvarying vec4 color;\nvoid computeVertPos(inout vec4 pos, vec2 vertOffset\n#if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , mat4 view\n#endif\n#if USE_STRETCHED_BILLBOARD\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float size\n , float xIndex\n#endif\n) {\n#if USE_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = normalize(vec3(view[0][1], view[1][1], view[2][1]));\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_STRETCHED_BILLBOARD\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz));\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * size;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif USE_HORIZONTAL_BILLBOARD\n vec3 camRight = vec3(1, 0, 0);\n vec3 camUp = vec3(0, 0, -1);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_VERTICAL_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = vec3(0, 1, 0);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV(float frameIndex, vec2 vertIndex, vec2 frameTile) {\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n \n vertIndex.x = 1. - vertIndex.x;\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nvoid rotateCorner(inout vec2 corner, float angle) {\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nattribute vec3 a_position; \nattribute vec3 a_texCoord; \nattribute vec2 a_texCoord1; \nattribute vec4 a_color;\n#if USE_STRETCHED_BILLBOARD\nattribute vec3 a_color1; \n#endif\nvec4 lpvs_main() {\n vec4 pos = vec4(a_position, 1);\n#if USE_STRETCHED_BILLBOARD\n vec4 velocity = vec4(a_color1.xyz, 0);\n#endif\n#if !USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if USE_STRETCHED_BILLBOARD\n velocity = cc_matWorld * velocity;\n #endif\n#endif\n vec2 cornerOffset = vec2((a_texCoord.xy - 0.5) * a_texCoord1.x);\n#if !USE_STRETCHED_BILLBOARD\n rotateCorner(cornerOffset, a_texCoord1.y);\n#endif\n computeVertPos(pos, cornerOffset\n #if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , cc_matView\n #endif\n #if USE_STRETCHED_BILLBOARD\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_texCoord1.x\n , a_texCoord.x\n #endif\n );\n pos = cc_matViewProj * pos;\n uv = computeUV(a_texCoord.z, a_texCoord.xy, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = lpvs_main(); }\n`, + "vert": `\nprecision mediump float;\nuniform vec4 mainTiling_Offset;\nuniform vec4 frameTile_velLenScale;\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\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_mainLitDir;\nuniform vec4 cc_mainLitColor;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\nuniform mat4 cc_matWorld;\nuniform mat4 cc_matWorldIT;\nvarying vec2 uv;\nvarying vec4 color;\nvoid computeVertPos(inout vec4 pos, vec2 vertOffset\n#if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , mat4 view\n#endif\n#if USE_STRETCHED_BILLBOARD\n , vec3 eye\n , vec4 velocity\n , float velocityScale\n , float lengthScale\n , float size\n , float xIndex\n#endif\n) {\n#if USE_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = normalize(vec3(view[0][1], view[1][1], view[2][1]));\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_STRETCHED_BILLBOARD\n vec3 camRight = normalize(cross(pos.xyz - eye, velocity.xyz));\n vec3 camUp = velocity.xyz * velocityScale + normalize(velocity.xyz) * lengthScale * size;\n pos.xyz += (camRight * abs(vertOffset.x) * sign(vertOffset.y)) - camUp * xIndex;\n#elif USE_HORIZONTAL_BILLBOARD\n vec3 camRight = vec3(1, 0, 0);\n vec3 camUp = vec3(0, 0, -1);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#elif USE_VERTICAL_BILLBOARD\n vec3 camRight = normalize(vec3(view[0][0], view[1][0], view[2][0]));\n vec3 camUp = vec3(0, 1, 0);\n pos.xyz += (camRight * vertOffset.x) + (camUp * vertOffset.y);\n#else\n pos.x += vertOffset.x;\n pos.y += vertOffset.y;\n#endif\n}\nvec2 computeUV(float frameIndex, vec2 vertIndex, vec2 frameTile) {\n vec2 aniUV = vec2(0, floor(frameIndex * frameTile.y));\n aniUV.x = floor(frameIndex * frameTile.x * frameTile.y - aniUV.y * frameTile.x);\n \n vertIndex.x = 1. - vertIndex.x;\n return (aniUV.xy + vertIndex) / vec2(frameTile.x, frameTile.y);\n}\nvoid rotateCorner(inout vec2 corner, float angle) {\n float xOS = cos(angle) * corner.x - sin(angle) * corner.y;\n float yOS = sin(angle) * corner.x + cos(angle) * corner.y;\n corner.x = xOS;\n corner.y = yOS;\n}\nattribute vec3 a_position; \nattribute vec3 a_texCoord; \nattribute vec2 a_texCoord1; \nattribute vec4 a_color;\n#if USE_STRETCHED_BILLBOARD\nattribute vec3 a_color1; \n#endif\nvec4 lpvs_main() {\n vec4 pos = vec4(a_position, 1);\n#if USE_STRETCHED_BILLBOARD\n vec4 velocity = vec4(a_color1.xyz, 0);\n#endif\n#if !USE_WORLD_SPACE\n pos = cc_matWorld * pos;\n #if USE_STRETCHED_BILLBOARD\n velocity = cc_matWorld * velocity;\n #endif\n#endif\n vec2 cornerOffset = vec2((a_texCoord.xy - 0.5) * a_texCoord1.x);\n#if !USE_STRETCHED_BILLBOARD\n rotateCorner(cornerOffset, a_texCoord1.y);\n#endif\n computeVertPos(pos, cornerOffset\n #if USE_BILLBOARD || USE_VERTICAL_BILLBOARD\n , cc_matView\n #endif\n #if USE_STRETCHED_BILLBOARD\n , cc_cameraPos.xyz\n , velocity\n , frameTile_velLenScale.z\n , frameTile_velLenScale.w\n , a_texCoord1.x\n , a_texCoord.x\n #endif\n );\n pos = cc_matViewProj * pos;\n uv = computeUV(a_texCoord.z, a_texCoord.xy, frameTile_velLenScale.xy) * mainTiling_Offset.xy + mainTiling_Offset.zw;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = lpvs_main(); }\n`, "frag": `\nprecision mediump float;\nvarying vec2 uv;\nvarying vec4 color;\nuniform sampler2D mainTexture;\nuniform vec4 tintColor;\nvec4 add () {\n return 2.0 * color * tintColor * texture2D(mainTexture, uv);\n}\nvec4 multiply () {\n vec4 col;\n vec4 texColor = texture2D(mainTexture, uv);\n col.rgb = tintColor.rgb * texColor.rgb * color.rgb * vec3(2.0);\n col.a = (1.0 - texColor.a) * (tintColor.a * color.a * 2.0);\n return col;\n}\nvoid main() { gl_FragColor = add(); }\n` }, "builtins": {"blocks":["CCGlobal", "CCLocal"], "textures":[]}, @@ -49,11 +49,11 @@ export default [ { "name": "builtin-skybox|sky-vs:vert|sky-fs:frag", "glsl3": { - "vert": `\nprecision mediump float;\nin vec3 a_position;\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \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};\nout vec3 viewDir;\nvec4 vert () {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n viewDir = a_position;\n vec4 pos = clipPos;\n pos.z = clipPos.w * 0.99999;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, + "vert": `\nprecision mediump float;\nin vec3 a_position;\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \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_mainLitDir; \n vec4 cc_mainLitColor; \n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nout vec3 viewDir;\nvec4 vert () {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n viewDir = a_position;\n vec4 pos = clipPos;\n pos.z = clipPos.w * 0.99999;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, "frag": `\nprecision mediump float;\nin vec3 viewDir;\nuniform samplerCube cubeMap;\nvec3 gammaToLinearSpaceRGB(vec3 sRGB) { \n return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);\n}\nvec3 linearToGammaSpaceRGB(vec3 RGB) { \n vec3 S1 = sqrt(RGB);\n vec3 S2 = sqrt(S1);\n vec3 S3 = sqrt(S2);\n return 0.585122381 * S1 + 0.783140355 * S2 - 0.368262736 * S3;\n}\nvec4 gammaToLinearSpaceRGBA(vec4 sRGBA) {\n return vec4(gammaToLinearSpaceRGB(sRGBA.rgb), sRGBA.a);\n}\nvec4 linearToGammaSpaceRGBA(vec4 RGBA) {\n return vec4(linearToGammaSpaceRGB(RGBA.rgb), RGBA.a);\n}\nfloat gammaToLinearSpaceExact(float val) {\n if (val <= 0.04045) {\n return val / 12.92;\n } else if (val < 1.0) {\n return pow((val + 0.055) / 1.055, 2.4);\n } else {\n return pow(val, 2.2);\n }\n}\nfloat linearToGammaSpaceExact(float val) {\n if (val <= 0.0) {\n return 0.0;\n } else if (val <= 0.0031308) {\n return 12.92 * val;\n } else if (val < 1.0) {\n return 1.055 * pow(val, 0.4166667) - 0.055;\n } else {\n return pow(val, 0.45454545);\n }\n}\nvec3 unpackNormal(vec4 nmap) {\n return nmap.xyz * 2.0 - 1.0;\n}\nvec3 unpackRGBE(vec4 rgbe) {\n return rgbe.rgb * pow(2.0, rgbe.a * 255.0 - 128.0);\n}\nvec4 frag () {\n#if USE_RGBE_CUBEMAP\n vec3 c = unpackRGBE(texture(cubeMap, viewDir));\n c = linearToGammaSpaceRGB(c / (1.0 + c));\n return vec4(c, 1.0);\n#else\n return texture(cubeMap, viewDir);\n#endif\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = frag(); }\n` }, "glsl1": { - "vert": `\nprecision mediump float;\nattribute vec3 a_position;\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\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;\nvarying vec3 viewDir;\nvec4 vert () {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n viewDir = a_position;\n vec4 pos = clipPos;\n pos.z = clipPos.w * 0.99999;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, + "vert": `\nprecision mediump float;\nattribute vec3 a_position;\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\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_mainLitDir;\nuniform vec4 cc_mainLitColor;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\nvarying vec3 viewDir;\nvec4 vert () {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n viewDir = a_position;\n vec4 pos = clipPos;\n pos.z = clipPos.w * 0.99999;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, "frag": `\nprecision mediump float;\nvarying vec3 viewDir;\nuniform samplerCube cubeMap;\nvec3 gammaToLinearSpaceRGB(vec3 sRGB) { \n return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878);\n}\nvec3 linearToGammaSpaceRGB(vec3 RGB) { \n vec3 S1 = sqrt(RGB);\n vec3 S2 = sqrt(S1);\n vec3 S3 = sqrt(S2);\n return 0.585122381 * S1 + 0.783140355 * S2 - 0.368262736 * S3;\n}\nvec4 gammaToLinearSpaceRGBA(vec4 sRGBA) {\n return vec4(gammaToLinearSpaceRGB(sRGBA.rgb), sRGBA.a);\n}\nvec4 linearToGammaSpaceRGBA(vec4 RGBA) {\n return vec4(linearToGammaSpaceRGB(RGBA.rgb), RGBA.a);\n}\nfloat gammaToLinearSpaceExact(float val) {\n if (val <= 0.04045) {\n return val / 12.92;\n } else if (val < 1.0) {\n return pow((val + 0.055) / 1.055, 2.4);\n } else {\n return pow(val, 2.2);\n }\n}\nfloat linearToGammaSpaceExact(float val) {\n if (val <= 0.0) {\n return 0.0;\n } else if (val <= 0.0031308) {\n return 12.92 * val;\n } else if (val < 1.0) {\n return 1.055 * pow(val, 0.4166667) - 0.055;\n } else {\n return pow(val, 0.45454545);\n }\n}\nvec3 unpackNormal(vec4 nmap) {\n return nmap.xyz * 2.0 - 1.0;\n}\nvec3 unpackRGBE(vec4 rgbe) {\n return rgbe.rgb * pow(2.0, rgbe.a * 255.0 - 128.0);\n}\nvec4 frag () {\n#if USE_RGBE_CUBEMAP\n vec3 c = unpackRGBE(textureCube(cubeMap, viewDir));\n c = linearToGammaSpaceRGB(c / (1.0 + c));\n return vec4(c, 1.0);\n#else\n return textureCube(cubeMap, viewDir);\n#endif\n}\nvoid main() { gl_FragColor = frag(); }\n` }, "builtins": {"blocks":["CCGlobal"], "textures":[]}, @@ -77,11 +77,11 @@ export default [ { "name": "builtin-sprite|sprite-vs:vert|sprite-fs:frag", "glsl3": { - "vert": `\nprecision mediump float;\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \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};\nin vec3 a_position;\nin vec2 a_texCoord;\nin vec4 a_color;\nout vec2 uv0;\nout vec4 color;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matViewProj * pos;\n uv0 = a_texCoord;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, + "vert": `\nprecision mediump float;\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \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_mainLitDir; \n vec4 cc_mainLitColor; \n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nin vec3 a_position;\nin vec2 a_texCoord;\nin vec4 a_color;\nout vec2 uv0;\nout vec4 color;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matViewProj * pos;\n uv0 = a_texCoord;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, "frag": `\nprecision mediump float;\nin vec2 uv0;\nin vec4 color;\nuniform sampler2D mainTexture;\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n o *= texture(mainTexture, uv0);\n o *= color;\n return 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 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;\nattribute vec3 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\nvarying vec2 uv0;\nvarying vec4 color;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matViewProj * pos;\n uv0 = a_texCoord;\n color = a_color;\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 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_mainLitDir;\nuniform vec4 cc_mainLitColor;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\nattribute vec3 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\nvarying vec2 uv0;\nvarying vec4 color;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matViewProj * pos;\n uv0 = a_texCoord;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, "frag": `\nprecision mediump float;\nvarying vec2 uv0;\nvarying vec4 color;\nuniform sampler2D mainTexture;\nvec4 frag () {\n vec4 o = vec4(1, 1, 1, 1);\n o *= texture2D(mainTexture, uv0);\n o *= color;\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n` }, "builtins": {"blocks":["CCGlobal"], "textures":[]}, @@ -94,6 +94,53 @@ export default [ } ] }, + { + "name": "builtin-standard", + "techniques": [ + {"name":"opaque", "passes":[{"program":"builtin-standard|standard-vs:vert|standard-fs:frag", "properties":{"tilingOffset":{"type":16, "value":[1, 1, 0, 0]}, "albedo":{"type":17, "value":[1, 1, 1, 1]}, "albedoScale":{"type":16, "value":[1, 1, 1, 0]}, "pbrParams":{"type":16, "value":[0.6, 0.2, 1, 1]}, "pbrScale":{"type":16, "value":[1, 1, 1, 1]}, "emissive":{"type":17, "value":[1, 1, 1, 1]}, "emissiveScale":{"type":16, "value":[1, 1, 1, 1]}, "albedoSampler":{"type":29, "value":"grey"}, "normalSampler":{"type":29, "value":"normal"}, "pbrSampler":{"type":29, "value":"grey"}, "emissiveSampler":{"type":29, "value":"grey"}}}]} + ], + "shaders": [ + { + "name": "builtin-standard|standard-vs:vert|standard-fs:frag", + "glsl3": { + "vert": `\nprecision mediump float;\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \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_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 mat4 cc_matJoint[128];\n vec4 cc_jointsTextureSize;\n};\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\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#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\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;\nout vec3 v_worldPos;\nout vec3 v_worldNormal;\n#if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP\nin vec2 a_texCoord;\nout vec2 v_uv;\n#endif\nuniform Constants {\n vec4 tilingOffset;\n vec4 albedo;\n vec4 albedoScale; \n vec4 pbrParams; \n vec4 pbrScale; \n vec4 emissive;\n vec4 emissiveScale;\n};\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 return cc_matViewProj * cc_matWorld * pos;\n}\nvoid main() { gl_Position = vert(); }\n`, + "frag": `\nprecision mediump float;\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \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_mainLitDir; \n vec4 cc_mainLitColor; \n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\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 )\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 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 normLit, vec3 litDir, float litAngleScale, float litAngleOffset)\n{\n float cd = dot(litDir, normLit);\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 RoL, 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}\nin vec3 v_worldPos;\nin vec3 v_worldNormal;\n#if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP\nin vec2 v_uv;\n#endif\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 USE_ALBEDO_MAP\nuniform sampler2D albedoSampler;\n#endif\n#if USE_NORMAL_MAP\nuniform sampler2D normalSampler;\n#endif\n#if USE_PBR_MAP\nuniform sampler2D pbrSampler;\n#endif\n#if USE_EMISSIVE_MAP\nuniform sampler2D emissiveSampler;\n#endif\n#if USE_NORMAL_MAP\nvec3 getNormal(vec3 pos, vec3 normal) {\n vec3 q0 = vec3( dFdx( pos.x ), dFdx( pos.y ), dFdx( pos.z ) );\n vec3 q1 = vec3( dFdy( pos.x ), dFdy( pos.y ), dFdy( pos.z ) );\n vec2 st0 = dFdx( v_uv.st );\n vec2 st1 = dFdy( v_uv.st );\n vec3 S = normalize( q0 * st1.t - q1 * st0.t );\n vec3 T = normalize( -q0 * st1.s + q1 * st0.s );\n vec3 N = normal;\n vec3 mapN = texture(normalSampler, v_uv).rgb * 2.0 - 1.0;\n mapN.xy = 1.0 * mapN.xy;\n mat3 tsn = mat3( S, T, N );\n return normalize( tsn * mapN );\n}\n#endif\nvec4 frag () {\n #if USE_ALBEDO_MAP\n vec4 baseColor = texture(albedoSampler, v_uv);\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 N = getNormal(v_worldPos, N);\n #endif\n #if USE_PBR_MAP\n vec4 pbr = texture(pbrSampler, v_uv);\n #else\n vec4 pbr = pbrParams;\n #endif\n pbr *= pbrScale;\n float roughness = clamp(pbr.r, 0.04, 1.0);\n float metallic = clamp(pbr.g, 0.0, 1.0);\n vec3 diffuse = baseColor.rgb * 0.96 * (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 = clamp(dot(N, V), 0.0, 1.0);\n float NL = clamp(dot(N, L), 0.0, 1.0);\n float NH = clamp(dot(N, H), 0.0, 1.0);\n specular = BRDFApprox(specular, roughness, NV);\n vec3 R = normalize(reflect(-V, N));\n float RL = clamp(dot(R, L), 0.0, 1.0);\n vec3 specularContrib = specular * CalcSpecular(roughness, RL, NH, H, N);\n vec3 diffuseContrib = diffuse / 3.14159265359;\n vec3 finalColor = NL * cc_mainLitColor.rgb * cc_mainLitColor.w * (diffuseContrib + specularContrib);\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 finalColor = LinearToSRGB(finalColor);\n finalColor = max(finalColor, vec3(pbrParams.g));\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 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_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 mat4 cc_matJoint[128];\nuniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\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#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\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;\nvarying vec3 v_worldPos;\nvarying vec3 v_worldNormal;\n#if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP\nattribute vec2 a_texCoord;\nvarying vec2 v_uv;\n#endif\nuniform vec4 tilingOffset;\nuniform vec4 albedo;\nuniform vec4 albedoScale;\nuniform vec4 pbrParams;\nuniform vec4 pbrScale;\nuniform vec4 emissive;\nuniform vec4 emissiveScale;\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 return cc_matViewProj * cc_matWorld * 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 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_mainLitDir;\nuniform vec4 cc_mainLitColor;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\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 )\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 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 normLit, vec3 litDir, float litAngleScale, float litAngleOffset)\n{\n float cd = dot(litDir, normLit);\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 RoL, 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}\nvarying vec3 v_worldPos;\nvarying vec3 v_worldNormal;\n#if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP\nvarying vec2 v_uv;\n#endif\nuniform vec4 tilingOffset;\nuniform vec4 albedo;\nuniform vec4 albedoScale;\nuniform vec4 pbrParams;\nuniform vec4 pbrScale;\nuniform vec4 emissive;\nuniform vec4 emissiveScale;\n#if USE_ALBEDO_MAP\nuniform sampler2D albedoSampler;\n#endif\n#if USE_NORMAL_MAP\nuniform sampler2D normalSampler;\n#endif\n#if USE_PBR_MAP\nuniform sampler2D pbrSampler;\n#endif\n#if USE_EMISSIVE_MAP\nuniform sampler2D emissiveSampler;\n#endif\n#if USE_NORMAL_MAP\nvec3 getNormal(vec3 pos, vec3 normal) {\n vec3 q0 = vec3( dFdx( pos.x ), dFdx( pos.y ), dFdx( pos.z ) );\n vec3 q1 = vec3( dFdy( pos.x ), dFdy( pos.y ), dFdy( pos.z ) );\n vec2 st0 = dFdx( v_uv.st );\n vec2 st1 = dFdy( v_uv.st );\n vec3 S = normalize( q0 * st1.t - q1 * st0.t );\n vec3 T = normalize( -q0 * st1.s + q1 * st0.s );\n vec3 N = normal;\n vec3 mapN = texture2D(normalSampler, v_uv).rgb * 2.0 - 1.0;\n mapN.xy = 1.0 * mapN.xy;\n mat3 tsn = mat3( S, T, N );\n return normalize( tsn * mapN );\n}\n#endif\nvec4 frag () {\n #if USE_ALBEDO_MAP\n vec4 baseColor = texture2D(albedoSampler, v_uv);\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 N = getNormal(v_worldPos, N);\n #endif\n #if USE_PBR_MAP\n vec4 pbr = texture2D(pbrSampler, v_uv);\n #else\n vec4 pbr = pbrParams;\n #endif\n pbr *= pbrScale;\n float roughness = clamp(pbr.r, 0.04, 1.0);\n float metallic = clamp(pbr.g, 0.0, 1.0);\n vec3 diffuse = baseColor.rgb * 0.96 * (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 = clamp(dot(N, V), 0.0, 1.0);\n float NL = clamp(dot(N, L), 0.0, 1.0);\n float NH = clamp(dot(N, H), 0.0, 1.0);\n specular = BRDFApprox(specular, roughness, NV);\n vec3 R = normalize(reflect(-V, N));\n float RL = clamp(dot(R, L), 0.0, 1.0);\n vec3 specularContrib = specular * CalcSpecular(roughness, RL, NH, H, N);\n vec3 diffuseContrib = diffuse / 3.14159265359;\n vec3 finalColor = NL * cc_mainLitColor.rgb * cc_mainLitColor.w * (diffuseContrib + specularContrib);\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 finalColor = LinearToSRGB(finalColor);\n finalColor = max(finalColor, vec3(pbrParams.g));\n return vec4(finalColor, baseColor.a);\n}\nvoid main() { gl_FragColor = frag(); }\n` + }, + "builtins": {"blocks":["CCGlobal", "CCLocal", "CCSkinning"], "textures":["cc_jointsTexture"]}, + "defines": [ + {"name":"CC_USE_SKINNING", "type":"boolean", "defines":[]}, + {"name":"CC_USE_JOINTS_TEXTURE", "type":"boolean", "defines":["CC_USE_SKINNING"]}, + {"name":"USE_ALBEDO_MAP", "type":"boolean", "defines":[]}, + {"name":"USE_NORMAL_MAP", "type":"boolean", "defines":[]}, + {"name":"USE_PBR_MAP", "type":"boolean", "defines":[]}, + {"name":"USE_EMISSIVE_MAP", "type":"boolean", "defines":[]}, + {"name":"USE_ALPHA_TEST", "type":"boolean", "defines":[]} + ], + "blocks": [ + {"name": "Constants", "size": 112, "defines": [], "binding": 0, "members": [ + {"name":"tilingOffset", "type":16, "count":1, "size":16}, + {"name":"albedo", "type":16, "count":1, "size":16}, + {"name":"albedoScale", "type":16, "count":1, "size":16}, + {"name":"pbrParams", "type":16, "count":1, "size":16}, + {"name":"pbrScale", "type":16, "count":1, "size":16}, + {"name":"emissive", "type":16, "count":1, "size":16}, + {"name":"emissiveScale", "type":16, "count":1, "size":16} + ]} + ], + "samplers": [ + {"name":"albedoSampler", "type":29, "count":1, "defines":["USE_ALBEDO_MAP"], "binding":1}, + {"name":"normalSampler", "type":29, "count":1, "defines":["USE_NORMAL_MAP"], "binding":2}, + {"name":"pbrSampler", "type":29, "count":1, "defines":["USE_PBR_MAP"], "binding":3}, + {"name":"emissiveSampler", "type":29, "count":1, "defines":["USE_EMISSIVE_MAP"], "binding":4} + ], + "dependencies": {} + } + ] + }, { "name": "builtin-tonemap", "techniques": [ @@ -103,12 +150,12 @@ export default [ { "name": "builtin-tonemap|tonemap-vs:vert|tonemap-fs:frag", "glsl3": { - "vert": `\nprecision mediump float;\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 )\nvec2 ScreenSpaceTexCoord(vec2 pos) {\n return pos * 0.5 + 0.5;\n}\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \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};\nin vec2 a_position;\nin vec2 a_texCoord;\nout vec2 v_uv;\nvec4 vert () {\n vec4 pos = vec4(a_position, 0, 1);\n v_uv = a_texCoord * cc_screenScale.xy;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, - "frag": `\nprecision mediump float;\nin vec2 v_uv;\nuniform sampler2D u_texSampler;\nvec4 frag () {\n vec4 o = texture(u_texSampler, v_uv);\n return o;\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = frag(); }\n` + "vert": `\nprecision mediump float;\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 )\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \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_mainLitDir; \n vec4 cc_mainLitColor; \n vec4 cc_ambientSky;\n vec4 cc_ambientGround;\n};\nin vec2 a_position;\nin vec2 a_texCoord;\nout vec2 v_uv;\nvec4 vert () {\n vec4 pos = vec4(a_position, 0, 1);\n v_uv = a_texCoord * cc_screenScale.xy;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, + "frag": `\nprecision mediump float;\nfloat ComputeEV100(float aperture , float shutterTime , float iso) {\n return log2((aperture * aperture) / shutterTime * 100.0 / iso);\n}\nfloat ComputeExposure(float ev100) {\n return 0.833333 * pow(2.0, ev100);\n}\nvec3 ACESToneMap(vec3 color) {\n const float A = 2.51;\n const float B = 0.03;\n const float C = 2.43;\n const float D = 0.59;\n const float E = 0.14;\n return (color * (A * color + B)) / (color * (C * color + D) + E);\n}\nin vec2 v_uv;\nuniform sampler2D u_texSampler;\nvec4 frag () {\n vec4 o = texture(u_texSampler, v_uv);\n \n return o;\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = frag(); }\n` }, "glsl1": { - "vert": `\nprecision mediump float;\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 )\nvec2 ScreenSpaceTexCoord(vec2 pos) {\n return pos * 0.5 + 0.5;\n}\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\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;\nattribute vec2 a_position;\nattribute vec2 a_texCoord;\nvarying vec2 v_uv;\nvec4 vert () {\n vec4 pos = vec4(a_position, 0, 1);\n v_uv = a_texCoord * cc_screenScale.xy;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, - "frag": `\nprecision mediump float;\nvarying vec2 v_uv;\nuniform sampler2D u_texSampler;\nvec4 frag () {\n vec4 o = texture2D(u_texSampler, v_uv);\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n` + "vert": `\nprecision mediump float;\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 )\nuniform vec4 cc_time;\nuniform vec4 cc_screenSize;\nuniform vec4 cc_screenScale;\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_mainLitDir;\nuniform vec4 cc_mainLitColor;\nuniform vec4 cc_ambientSky;\nuniform vec4 cc_ambientGround;\nattribute vec2 a_position;\nattribute vec2 a_texCoord;\nvarying vec2 v_uv;\nvec4 vert () {\n vec4 pos = vec4(a_position, 0, 1);\n v_uv = a_texCoord * cc_screenScale.xy;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n`, + "frag": `\nprecision mediump float;\nfloat ComputeEV100(float aperture , float shutterTime , float iso) {\n return log2((aperture * aperture) / shutterTime * 100.0 / iso);\n}\nfloat ComputeExposure(float ev100) {\n return 0.833333 * pow(2.0, ev100);\n}\nvec3 ACESToneMap(vec3 color) {\n const float A = 2.51;\n const float B = 0.03;\n const float C = 2.43;\n const float D = 0.59;\n const float E = 0.14;\n return (color * (A * color + B)) / (color * (C * color + D) + E);\n}\nvarying vec2 v_uv;\nuniform sampler2D u_texSampler;\nvec4 frag () {\n vec4 o = texture2D(u_texSampler, v_uv);\n \n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n` }, "builtins": {"blocks":["CCGlobal"], "textures":[]}, "defines": [], @@ -129,11 +176,11 @@ export default [ { "name": "builtin-unlit|unlit-vs:vert|unlit-fs:frag", "glsl3": { - "vert": `\nprecision mediump float;\nuniform CCGlobal {\n \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \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};\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 mat4 cc_matJoint[128];\n vec4 cc_jointsTextureSize;\n};\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\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#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\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 \n vec4 cc_time; \n vec4 cc_screenSize; \n vec4 cc_screenScale; \n \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_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 mat4 cc_matJoint[128];\n vec4 cc_jointsTextureSize;\n};\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\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#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\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`, "frag": `\nprecision mediump float;\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 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 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 mat4 cc_matWorld;\nuniform mat4 cc_matWorldIT;\n#if CC_USE_SKINNING\nattribute vec4 a_weights;\nattribute vec4 a_joints;\nuniform mat4 cc_matJoint[128];\nuniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\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#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\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 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_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 mat4 cc_matJoint[128];\nuniform vec4 cc_jointsTextureSize;\n#if CC_USE_JOINTS_TEXTURE\nuniform sampler2D cc_jointsTexture;\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#else\nmat4 getBoneMatrix(const in float i) {\n return cc_matJoint[int(i)];\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`, "frag": `\nprecision mediump float;\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 o;\n}\nvoid main() { gl_FragColor = frag(); }\n` }, "builtins": {"blocks":["CCGlobal", "CCLocal", "CCSkinning"], "textures":["cc_jointsTexture"]}, diff --git a/cocos/3d/builtin/init.ts b/cocos/3d/builtin/init.ts index 76e743f3bd..cfdf3ab286 100644 --- a/cocos/3d/builtin/init.ts +++ b/cocos/3d/builtin/init.ts @@ -134,6 +134,14 @@ class BuiltinResMgr { }); resources[tonemapMtl._uuid] = tonemapMtl; + // standard material + const standardMtl = new cc.Material(); + standardMtl._uuid = 'standard-material'; + standardMtl.initialize({ + effectName: 'builtin-standard', + }); + resources[standardMtl._uuid] = standardMtl; + // default material const defaultMtl = new cc.Material(); defaultMtl._uuid = 'default-material'; diff --git a/cocos/3d/framework/light-component.ts b/cocos/3d/framework/light-component.ts index 81ace6dc80..2ab9ef0f1a 100644 --- a/cocos/3d/framework/light-component.ts +++ b/cocos/3d/framework/light-component.ts @@ -149,7 +149,6 @@ export class LightComponent extends Component { // protected _shadowBias = 0.0005; protected _light: Light | null = null; - protected _colorData = [1, 1, 1]; /** * !#en The light source type @@ -185,11 +184,10 @@ export class LightComponent extends Component { set color (val) { this._color = val; const scale = this._intensity / 255; - this._colorData[0] = val.r * scale; - this._colorData[1] = val.g * scale; - this._colorData[2] = val.b * scale; if (this._light) { - this._light.color = this._colorData; + this._light.color[0] = val.r * scale; + this._light.color[1] = val.g * scale; + this._light.color[2] = val.b * scale; } } @@ -207,11 +205,10 @@ export class LightComponent extends Component { set intensity (val) { this._intensity = val; const scale = val / 255; - this._colorData[0] = this._color.r * scale; - this._colorData[1] = this._color.g * scale; - this._colorData[2] = this._color.b * scale; if (this._light) { - this._light.color = this._colorData; + this._light.color[0] = this._color.r * scale; + this._light.color[1] = this._color.g * scale; + this._light.color[2] = this._color.b * scale; } } diff --git a/cocos/core/game.js b/cocos/core/game.js index 1d2d530a47..e80b4d547d 100644 --- a/cocos/core/game.js +++ b/cocos/core/game.js @@ -764,11 +764,15 @@ var game = { } { + this._gfxDevice = new cc.WebGLGFXDevice(); + + /* if (window.WebGL2RenderingContext) { this._gfxDevice = new cc.WebGL2GFXDevice(); } else { this._gfxDevice = new cc.WebGLGFXDevice(); } + */ this._gfxDevice.initialize({ canvasElm: localCanvas, diff --git a/cocos/gfx/webgl/webgl-buffer.ts b/cocos/gfx/webgl/webgl-buffer.ts index 8a1a589769..134c77bc31 100644 --- a/cocos/gfx/webgl/webgl-buffer.ts +++ b/cocos/gfx/webgl/webgl-buffer.ts @@ -34,7 +34,7 @@ export class WebGLGFXBuffer extends GFXBuffer { if (this._usage & GFXBufferUsageBit.INDIRECT) { this._indirectBuffer = { drawInfos: [] }; - } else if (this._usage & GFXBufferUsageBit.UNIFORM) { + } else if ((this._usage & GFXBufferUsageBit.UNIFORM) && this._size > 0) { this._uniformBuffer = new ArrayBuffer(this._size); } @@ -85,8 +85,9 @@ export class WebGLGFXBuffer extends GFXBuffer { this._gpuBuffer.buffer = this._uniformBuffer; } this._gpuBuffer.size = this._size; - - WebGLCmdFuncResizeBuffer(this._device as WebGLGFXDevice, this._gpuBuffer); + if (this._size > 0) { + WebGLCmdFuncResizeBuffer(this._device as WebGLGFXDevice, this._gpuBuffer); + } } } diff --git a/cocos/gfx/webgl/webgl-commands.ts b/cocos/gfx/webgl/webgl-commands.ts index 305aa0e605..fb1fa9c82f 100644 --- a/cocos/gfx/webgl/webgl-commands.ts +++ b/cocos/gfx/webgl/webgl-commands.ts @@ -565,13 +565,13 @@ export class WebGLCmdPackage { export function WebGLCmdFuncCreateBuffer (device: WebGLGFXDevice, gpuBuffer: WebGLGPUBuffer) { const gl = device.gl; - const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.HOST ? WebGL2RenderingContext.DYNAMIC_DRAW : WebGL2RenderingContext.STATIC_DRAW; + const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.HOST ? WebGLRenderingContext.DYNAMIC_DRAW : WebGLRenderingContext.STATIC_DRAW; if (gpuBuffer.usage & GFXBufferUsageBit.VERTEX) { gpuBuffer.glTarget = WebGLRenderingContext.ARRAY_BUFFER; const glBuffer = gl.createBuffer(); - if (glBuffer) { + if (glBuffer && gpuBuffer.size > 0) { gpuBuffer.glBuffer = glBuffer; if (device.stateCache.glArrayBuffer !== gpuBuffer.glBuffer) { @@ -587,7 +587,7 @@ export function WebGLCmdFuncCreateBuffer (device: WebGLGFXDevice, gpuBuffer: Web gpuBuffer.glTarget = WebGLRenderingContext.ELEMENT_ARRAY_BUFFER; const glBuffer = gl.createBuffer(); - if (glBuffer) { + if (glBuffer && gpuBuffer.size > 0) { gpuBuffer.glBuffer = glBuffer; if (device.stateCache.glElementArrayBuffer !== gpuBuffer.glBuffer) { @@ -628,7 +628,7 @@ export function WebGLCmdFuncDestroyBuffer (device: WebGLGFXDevice, gpuBuffer: We export function WebGLCmdFuncResizeBuffer (device: WebGLGFXDevice, gpuBuffer: WebGLGPUBuffer) { const gl = device.gl; - const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.HOST ? WebGL2RenderingContext.DYNAMIC_DRAW : WebGL2RenderingContext.STATIC_DRAW; + const glUsage: GLenum = gpuBuffer.memUsage & GFXMemoryUsageBit.HOST ? WebGLRenderingContext.DYNAMIC_DRAW : WebGLRenderingContext.STATIC_DRAW; if (gpuBuffer.usage & GFXBufferUsageBit.VERTEX) { @@ -853,7 +853,7 @@ export function WebGLCmdFuncCreateFramebuffer (device: WebGLGFXDevice, gpuFrameb gpuColorView.gpuTexture.glTexture, gpuColorView.baseLevel); - attachments.push(WebGL2RenderingContext.COLOR_ATTACHMENT0 + i); + attachments.push(WebGLRenderingContext.COLOR_ATTACHMENT0 + i); } } @@ -874,7 +874,6 @@ export function WebGLCmdFuncCreateFramebuffer (device: WebGLGFXDevice, gpuFrameb device.WEBGL_draw_buffers.drawBuffersWEBGL(attachments); } - /* const status = gl.checkFramebufferStatus(WebGLRenderingContext.FRAMEBUFFER); if (status !== WebGLRenderingContext.FRAMEBUFFER_COMPLETE) { switch (status) { @@ -897,7 +896,6 @@ export function WebGLCmdFuncCreateFramebuffer (device: WebGLGFXDevice, gpuFrameb default: } } - */ } } } @@ -1301,11 +1299,9 @@ export function WebGLCmdFuncExecuteCmds (device: WebGLGFXDevice, cmdPackage: Web gl.colorMask(true, true, true, true); } - if (!cmd0.gpuFramebuffer.isOffscreen) { - const clearColor = cmd0.clearColors[0]; - gl.clearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); - clears |= WebGL2RenderingContext.COLOR_BUFFER_BIT; - } + const clearColor = cmd0.clearColors[0]; + gl.clearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a); + clears |= WebGLRenderingContext.COLOR_BUFFER_BIT; break; } diff --git a/cocos/gfx/webgl/webgl-device.ts b/cocos/gfx/webgl/webgl-device.ts index c6f3cda52d..e01be9ecf5 100644 --- a/cocos/gfx/webgl/webgl-device.ts +++ b/cocos/gfx/webgl/webgl-device.ts @@ -6,7 +6,6 @@ import { GFXCommandBuffer, IGFXCommandBufferInfo } from '../command-buffer'; import { GFXBufferTextureCopy, GFXFormat, - GFXFormatInfos, GFXFormatSize, GFXQueueType, } from '../define'; diff --git a/cocos/gfx/webgl2/webgl2-buffer.ts b/cocos/gfx/webgl2/webgl2-buffer.ts index c96c93c334..8bd7525423 100644 --- a/cocos/gfx/webgl2/webgl2-buffer.ts +++ b/cocos/gfx/webgl2/webgl2-buffer.ts @@ -73,7 +73,9 @@ export class WebGL2GFXBuffer extends GFXBuffer { if (this._gpuBuffer) { this._gpuBuffer.size = this._size; - WebGL2CmdFuncResizeBuffer(this._device as WebGL2GFXDevice, this._gpuBuffer); + if (this._size > 0) { + WebGL2CmdFuncResizeBuffer(this._device as WebGL2GFXDevice, this._gpuBuffer); + } } } diff --git a/cocos/gfx/webgl2/webgl2-commands.ts b/cocos/gfx/webgl2/webgl2-commands.ts index 9034602021..87edfdc060 100644 --- a/cocos/gfx/webgl2/webgl2-commands.ts +++ b/cocos/gfx/webgl2/webgl2-commands.ts @@ -25,7 +25,6 @@ import { IGFXViewport, WebGLEXT, } from '../define'; -import { GFXUniformBlock } from '../shader'; import { WebGL2GFXCommandAllocator } from './webgl2-command-allocator'; import { IWebGL2DepthBias, @@ -52,10 +51,10 @@ import { import { IWebGL2TexUnit } from './webgl2-state-cache'; const WebGLWraps: GLenum[] = [ - WebGL2RenderingContext.REPEAT, - WebGL2RenderingContext.MIRRORED_REPEAT, - WebGL2RenderingContext.CLAMP_TO_EDGE, - WebGL2RenderingContext.CLAMP_TO_EDGE, + WebGLRenderingContext.REPEAT, + WebGLRenderingContext.MIRRORED_REPEAT, + WebGLRenderingContext.CLAMP_TO_EDGE, + WebGLRenderingContext.CLAMP_TO_EDGE, ]; const _uniformValues = new Array(1024 * 4); @@ -473,51 +472,51 @@ function WebGLGetComponentCount (glType: GLenum): GFXType { } const WebGLCmpFuncs: GLenum[] = [ - WebGL2RenderingContext.NEVER, - WebGL2RenderingContext.LESS, - WebGL2RenderingContext.EQUAL, - WebGL2RenderingContext.LEQUAL, - WebGL2RenderingContext.GREATER, - WebGL2RenderingContext.NOTEQUAL, - WebGL2RenderingContext.GEQUAL, - WebGL2RenderingContext.ALWAYS, + WebGLRenderingContext.NEVER, + WebGLRenderingContext.LESS, + WebGLRenderingContext.EQUAL, + WebGLRenderingContext.LEQUAL, + WebGLRenderingContext.GREATER, + WebGLRenderingContext.NOTEQUAL, + WebGLRenderingContext.GEQUAL, + WebGLRenderingContext.ALWAYS, ]; const WebGLStencilOps: GLenum[] = [ - WebGL2RenderingContext.ZERO, - WebGL2RenderingContext.KEEP, - WebGL2RenderingContext.REPLACE, - WebGL2RenderingContext.INCR, - WebGL2RenderingContext.DECR, - WebGL2RenderingContext.INVERT, - WebGL2RenderingContext.INCR_WRAP, - WebGL2RenderingContext.DECR_WRAP, + WebGLRenderingContext.ZERO, + WebGLRenderingContext.KEEP, + WebGLRenderingContext.REPLACE, + WebGLRenderingContext.INCR, + WebGLRenderingContext.DECR, + WebGLRenderingContext.INVERT, + WebGLRenderingContext.INCR_WRAP, + WebGLRenderingContext.DECR_WRAP, ]; const WebGLBlendOps: GLenum[] = [ - WebGL2RenderingContext.FUNC_ADD, - WebGL2RenderingContext.FUNC_SUBTRACT, - WebGL2RenderingContext.FUNC_REVERSE_SUBTRACT, - WebGL2RenderingContext.FUNC_ADD, - WebGL2RenderingContext.FUNC_ADD, + WebGLRenderingContext.FUNC_ADD, + WebGLRenderingContext.FUNC_SUBTRACT, + WebGLRenderingContext.FUNC_REVERSE_SUBTRACT, + WebGLRenderingContext.FUNC_ADD, + WebGLRenderingContext.FUNC_ADD, ]; const WebGLBlendFactors: GLenum[] = [ - WebGL2RenderingContext.ZERO, - WebGL2RenderingContext.ONE, - WebGL2RenderingContext.SRC_ALPHA, - WebGL2RenderingContext.DST_ALPHA, - WebGL2RenderingContext.ONE_MINUS_SRC_ALPHA, - WebGL2RenderingContext.ONE_MINUS_DST_ALPHA, - WebGL2RenderingContext.SRC_COLOR, - WebGL2RenderingContext.DST_COLOR, - WebGL2RenderingContext.ONE_MINUS_SRC_COLOR, - WebGL2RenderingContext.ONE_MINUS_DST_COLOR, - WebGL2RenderingContext.SRC_ALPHA_SATURATE, - WebGL2RenderingContext.CONSTANT_COLOR, - WebGL2RenderingContext.ONE_MINUS_CONSTANT_COLOR, - WebGL2RenderingContext.CONSTANT_ALPHA, - WebGL2RenderingContext.ONE_MINUS_CONSTANT_ALPHA, + WebGLRenderingContext.ZERO, + WebGLRenderingContext.ONE, + WebGLRenderingContext.SRC_ALPHA, + WebGLRenderingContext.DST_ALPHA, + WebGLRenderingContext.ONE_MINUS_SRC_ALPHA, + WebGLRenderingContext.ONE_MINUS_DST_ALPHA, + WebGLRenderingContext.SRC_COLOR, + WebGLRenderingContext.DST_COLOR, + WebGLRenderingContext.ONE_MINUS_SRC_COLOR, + WebGLRenderingContext.ONE_MINUS_DST_COLOR, + WebGLRenderingContext.SRC_ALPHA_SATURATE, + WebGLRenderingContext.CONSTANT_COLOR, + WebGLRenderingContext.ONE_MINUS_CONSTANT_COLOR, + WebGLRenderingContext.CONSTANT_ALPHA, + WebGLRenderingContext.ONE_MINUS_CONSTANT_ALPHA, ]; export enum WebGL2Cmd { @@ -712,7 +711,7 @@ export function WebGL2CmdFuncCreateBuffer (device: WebGL2GFXDevice, gpuBuffer: W gpuBuffer.glTarget = WebGL2RenderingContext.ARRAY_BUFFER; const glBuffer = gl.createBuffer(); - if (glBuffer) { + if (glBuffer && gpuBuffer.size > 0) { gpuBuffer.glBuffer = glBuffer; if (device.stateCache.glArrayBuffer !== gpuBuffer.glBuffer) { @@ -729,7 +728,7 @@ export function WebGL2CmdFuncCreateBuffer (device: WebGL2GFXDevice, gpuBuffer: W gpuBuffer.glTarget = WebGL2RenderingContext.ELEMENT_ARRAY_BUFFER; const glBuffer = gl.createBuffer(); - if (glBuffer) { + if (glBuffer && gpuBuffer.size > 0) { gpuBuffer.glBuffer = glBuffer; if (device.stateCache.glElementArrayBuffer !== gpuBuffer.glBuffer) { @@ -746,7 +745,7 @@ export function WebGL2CmdFuncCreateBuffer (device: WebGL2GFXDevice, gpuBuffer: W gpuBuffer.glTarget = WebGL2RenderingContext.UNIFORM_BUFFER; const glBuffer = gl.createBuffer(); - if (glBuffer) { + if (glBuffer && gpuBuffer.size > 0) { gpuBuffer.glBuffer = glBuffer; if (device.stateCache.glUniformBuffer !== gpuBuffer.glBuffer) { gl.bindBuffer(WebGL2RenderingContext.UNIFORM_BUFFER, gpuBuffer.glBuffer); @@ -891,7 +890,6 @@ export function WebGL2CmdFuncCreateTexture (device: WebGL2GFXDevice, gpuTexture: switch (gpuTexture.viewType) { case GFXTextureViewType.TV2D: { - gpuTexture.viewType = GFXTextureViewType.TV2D; gpuTexture.glTarget = WebGL2RenderingContext.TEXTURE_2D; @@ -922,21 +920,10 @@ export function WebGL2CmdFuncCreateTexture (device: WebGL2GFXDevice, gpuTexture: h = Math.max(1, h >> 1); } } - - gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_WRAP_S, WebGL2RenderingContext.CLAMP_TO_EDGE); - gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_WRAP_T, WebGL2RenderingContext.CLAMP_TO_EDGE); - gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_MIN_FILTER, WebGL2RenderingContext.LINEAR); - gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_MAG_FILTER, WebGL2RenderingContext.LINEAR); - gpuTexture.glWrapS = WebGL2RenderingContext.CLAMP_TO_EDGE; - gpuTexture.glWrapT = WebGL2RenderingContext.CLAMP_TO_EDGE; - gpuTexture.glMinFilter = WebGL2RenderingContext.LINEAR; - gpuTexture.glMagFilter = WebGL2RenderingContext.LINEAR; } - break; } case GFXTextureViewType.CUBE: { - gpuTexture.viewType = GFXTextureViewType.CUBE; gpuTexture.glTarget = WebGL2RenderingContext.TEXTURE_CUBE_MAP; @@ -973,17 +960,7 @@ export function WebGL2CmdFuncCreateTexture (device: WebGL2GFXDevice, gpuTexture: } } } - - gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_WRAP_S, WebGL2RenderingContext.CLAMP_TO_EDGE); - gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_WRAP_T, WebGL2RenderingContext.CLAMP_TO_EDGE); - gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_MIN_FILTER, WebGL2RenderingContext.LINEAR); - gl.texParameteri(gpuTexture.glTarget, WebGL2RenderingContext.TEXTURE_MAG_FILTER, WebGL2RenderingContext.LINEAR); - gpuTexture.glWrapS = WebGL2RenderingContext.CLAMP_TO_EDGE; - gpuTexture.glWrapT = WebGL2RenderingContext.CLAMP_TO_EDGE; - gpuTexture.glMinFilter = WebGL2RenderingContext.LINEAR; - gpuTexture.glMagFilter = WebGL2RenderingContext.LINEAR; } - break; } default: { @@ -1098,7 +1075,6 @@ export function WebGL2CmdFuncCreateFramebuffer (device: WebGL2GFXDevice, gpuFram gl.drawBuffers(attachments); - /* const status = gl.checkFramebufferStatus(WebGL2RenderingContext.FRAMEBUFFER); if (status !== WebGL2RenderingContext.FRAMEBUFFER_COMPLETE) { switch (status) { @@ -1121,7 +1097,6 @@ export function WebGL2CmdFuncCreateFramebuffer (device: WebGL2GFXDevice, gpuFram default: } } - */ } } } diff --git a/cocos/gfx/webgl2/webgl2-device.ts b/cocos/gfx/webgl2/webgl2-device.ts index 73e88dad71..b1051ff498 100644 --- a/cocos/gfx/webgl2/webgl2-device.ts +++ b/cocos/gfx/webgl2/webgl2-device.ts @@ -102,7 +102,8 @@ export class WebGL2GFXDevice extends GFXDevice { try { const webGLCtxAttribs: WebGLContextAttributes = { alpha: true, - antialias: this._isAntialias, + // antialias: this._isAntialias, + antialias: false, depth: true, stencil: true, premultipliedAlpha: this._isPremultipliedAlpha, diff --git a/cocos/pipeline/define.ts b/cocos/pipeline/define.ts index 83f0deab60..c24fc4c221 100644 --- a/cocos/pipeline/define.ts +++ b/cocos/pipeline/define.ts @@ -37,7 +37,11 @@ export class UBOGlobal { public static MAT_VIEW_PROJ_OFFSET: number = 76; public static MAT_VIEW_PROJ_INV_OFFSET: number = 92; public static CAMERA_POS_OFFSET: number = 108; - public static COUNT: number = 112; + public static MAIN_LIT_DIR_OFFSET: number = 112; + public static MAIN_LIT_COLOR_OFFSET: number = 116; + public static AMBIENT_SKY_OFFSET: number = 120; + public static AMBIENT_GROUND_OFFSET: number = 124; + public static COUNT: number = 128; public static SIZE: number = UBOGlobal.COUNT * 4; public static BLOCK: GFXUniformBlock = { @@ -54,6 +58,10 @@ export class UBOGlobal { { name: 'cc_matViewProj', type: GFXType.MAT4, count: 1 }, { name: 'cc_matViewProjInv', type: GFXType.MAT4, count: 1 }, { name: 'cc_cameraPos', type: GFXType.FLOAT4, count: 1 }, + { name: 'cc_mainLitDir', type: GFXType.FLOAT4, count: 1 }, + { name: 'cc_mainLitColor', type: GFXType.FLOAT4, count: 1 }, + { name: 'cc_ambientSky', type: GFXType.FLOAT4, count: 1 }, + { name: 'cc_ambientGround', type: GFXType.FLOAT4, count: 1 }, ], }; diff --git a/cocos/pipeline/forward/forward-flow.ts b/cocos/pipeline/forward/forward-flow.ts index 87676eaf36..0be70052c9 100644 --- a/cocos/pipeline/forward/forward-flow.ts +++ b/cocos/pipeline/forward/forward-flow.ts @@ -20,13 +20,13 @@ export class ForwardFlow extends RenderFlow { this._priority = info.priority; - const framebuffer = this._pipeline.root.mainWindow!.framebuffer!; + // const framebuffer = this._pipeline.root.mainWindow!.framebuffer!; this.createStage(ForwardStage, { name: 'ForwardStage', priority: ForwardStagePriority.FORWARD, - // framebuffer: this._pipeline.shadingFBO, - framebuffer, + framebuffer: this._pipeline.shadingFBO, + // framebuffer, }); return true; diff --git a/cocos/pipeline/forward/forward-pipeline.ts b/cocos/pipeline/forward/forward-pipeline.ts index e48be928bd..4b06295a4a 100644 --- a/cocos/pipeline/forward/forward-pipeline.ts +++ b/cocos/pipeline/forward/forward-pipeline.ts @@ -61,12 +61,10 @@ export class ForwardPipeline extends RenderPipeline { priority: ForwardFlowPriority.FORWARD, }); - /* this.createFlow(ToneMapFlow, { name: 'ToneMapFlow', priority: 0, }); - */ return true; } @@ -118,12 +116,13 @@ export class ForwardPipeline extends RenderPipeline { const scene = view.camera.scene; const dLights = scene.directionalLights; + for (let i = 0; i < UBOForwardLights.MAX_DIR_LIGHTS; i++) { const light = dLights[i]; if (light && light.enabled) { light.update(); this._uboLights.view.set(light.directionArray, UBOForwardLights.DIR_LIGHT_DIR_OFFSET + i * 4); - this._uboLights.view.set(light.colorData, UBOForwardLights.DIR_LIGHT_COLOR_OFFSET + i * 4); + this._uboLights.view.set(light.color, UBOForwardLights.DIR_LIGHT_COLOR_OFFSET + i * 4); } else { this._uboLights.view.set(_idVec4Array, UBOForwardLights.DIR_LIGHT_DIR_OFFSET + i * 4); this._uboLights.view.set(_idVec4Array, UBOForwardLights.DIR_LIGHT_COLOR_OFFSET + i * 4); @@ -135,7 +134,7 @@ export class ForwardPipeline extends RenderPipeline { if (light && light.enabled) { light.update(); this._uboLights.view.set(light.positionAndRange, UBOForwardLights.POINT_LIGHT_POS_RANGE_OFFSET + i * 4); - this._uboLights.view.set(light.colorData, UBOForwardLights.POINT_LIGHT_COLOR_OFFSET + i * 4); + this._uboLights.view.set(light.color, UBOForwardLights.POINT_LIGHT_COLOR_OFFSET + i * 4); } else { this._uboLights.view.set(_idVec4Array, UBOForwardLights.POINT_LIGHT_POS_RANGE_OFFSET + i * 4); this._uboLights.view.set(_idVec4Array, UBOForwardLights.POINT_LIGHT_COLOR_OFFSET + i * 4); @@ -148,7 +147,7 @@ export class ForwardPipeline extends RenderPipeline { light.update(); this._uboLights.view.set(light.positionAndRange, UBOForwardLights.SPOT_LIGHT_POS_RANGE_OFFSET + i * 4); this._uboLights.view.set(light.directionArray, UBOForwardLights.SPOT_LIGHT_DIR_OFFSET + i * 4); - this._uboLights.view.set(light.colorData, UBOForwardLights.SPOT_LIGHT_COLOR_OFFSET + i * 4); + this._uboLights.view.set(light.color, UBOForwardLights.SPOT_LIGHT_COLOR_OFFSET + i * 4); } else { this._uboLights.view.set(_idVec4Array, UBOForwardLights.SPOT_LIGHT_POS_RANGE_OFFSET + i * 4); this._uboLights.view.set(_idVec4Array, UBOForwardLights.SPOT_LIGHT_DIR_OFFSET + i * 4); diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index ebfc753e1f..f56e01a347 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -170,7 +170,7 @@ export abstract class RenderPipeline { protected createShadingTarget (): boolean { let colorFmt: GFXFormat; - let depthFmt: GFXFormat; + let depthStencilFmt: GFXFormat; // Try to use HDR format if (this._device.hasFeature(GFXFeature.FORMAT_R11G11B10F)) { @@ -188,15 +188,15 @@ export abstract class RenderPipeline { } if (this._device.hasFeature(GFXFeature.FORMAT_D24S8)) { - depthFmt = GFXFormat.D24S8; + depthStencilFmt = GFXFormat.D24S8; } else { - depthFmt = GFXFormat.D16; + depthStencilFmt = GFXFormat.D16; } // colorFmt = GFXFormat.RGBA16F; console.info('Shading Color Format: ' + GFXFormatInfos[colorFmt].name); - console.info('Shading Depth Format: ' + GFXFormatInfos[depthFmt].name); + console.info('Shading Depth Format: ' + GFXFormatInfos[depthStencilFmt].name); this._shadingTex = this._device.createTexture({ type: GFXTextureType.TEX2D, @@ -212,6 +212,20 @@ export abstract class RenderPipeline { format : colorFmt, }); + this._depthStencilTex = this._device.createTexture({ + type : GFXTextureType.TEX2D, + usage : GFXTextureUsageBit.DEPTH_STENCIL_ATTACHMENT | GFXTextureUsageBit.SAMPLED, + format : depthStencilFmt, + width : this._device.nativeWidth, + height : this._device.nativeHeight, + }); + + this._depthStencilTexView = this._device.createTextureView({ + texture : this._depthStencilTex, + type : GFXTextureViewType.TV2D, + format : depthStencilFmt, + }); + this._shadingPass = this._device.createRenderPass({ colorAttachments: [{ format: colorFmt, @@ -222,7 +236,7 @@ export abstract class RenderPipeline { endLayout: GFXTextureLayout.COLOR_ATTACHMENT_OPTIMAL, }], depthStencilAttachment: { - format : depthFmt, + format : depthStencilFmt, depthLoadOp : GFXLoadOp.CLEAR, depthStoreOp : GFXStoreOp.STORE, stencilLoadOp : GFXLoadOp.CLEAR, @@ -253,6 +267,16 @@ export abstract class RenderPipeline { this._shadingTex = null; } + if (this._depthStencilTexView) { + this._depthStencilTexView.destroy(); + this._depthStencilTexView = null; + } + + if (this._depthStencilTex) { + this._depthStencilTex.destroy(); + this._depthStencilTex = null; + } + if (this._shadingFBO) { this._shadingFBO.destroy(); this._shadingFBO = null; @@ -380,8 +404,16 @@ export abstract class RenderPipeline { protected updateUBOs (view: RenderView) { const camera = view.camera; + const scene = camera.scene; const device = this._root.device; + const mainLight = scene.mainLight; + if (mainLight && mainLight.enabled) { + mainLight.update(); + } + + const ambient = scene.ambient; + // update UBOGlobal _vec4Array[0] = this._root.frameTime; _vec4Array[1] = 0.0; @@ -425,6 +457,20 @@ export abstract class RenderPipeline { _vec4Array[3] = 1.0; this._defaultUboGlobal!.view.set(_vec4Array, UBOGlobal.CAMERA_POS_OFFSET); + vec3.array(_vec4Array, mainLight.direction); + this._defaultUboGlobal!.view.set(_vec4Array, UBOGlobal.MAIN_LIT_DIR_OFFSET); + + _vec4Array.set(mainLight.color); + _vec4Array[3] = 1.0; + this._defaultUboGlobal!.view.set(_vec4Array, UBOGlobal.MAIN_LIT_COLOR_OFFSET); + + _vec4Array.set(ambient.skyColor); + _vec4Array[3] = ambient.skyIllum; + this._defaultUboGlobal!.view.set(_vec4Array, UBOGlobal.AMBIENT_SKY_OFFSET); + + _vec4Array.set(ambient.groundAlbedo); + this._defaultUboGlobal!.view.set(_vec4Array, UBOGlobal.AMBIENT_GROUND_OFFSET); + // update ubos this._globalBindings.get(UBOGlobal.BLOCK.name)!.buffer!.update(this._defaultUboGlobal!.view.buffer); } diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index 3fa11b51e1..d193c8c723 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -1,6 +1,6 @@ // Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. -import { IPassInfo, IShaderInfo, ISamplerInfo } from '../../3d/assets/effect-asset'; +import { IPassInfo, IShaderInfo } from '../../3d/assets/effect-asset'; import { builtinResMgr } from '../../3d/builtin'; import { TextureBase } from '../../assets/texture-base'; import { color4, mat2, mat3, mat4, vec2, vec3, vec4 } from '../../core/vmath'; @@ -13,7 +13,7 @@ import { GFXPipelineLayout } from '../../gfx/pipeline-layout'; import { GFXBlendState, GFXBlendTarget, GFXDepthStencilState, GFXInputState, GFXPipelineState, GFXRasterizerState } from '../../gfx/pipeline-state'; import { GFXRenderPass } from '../../gfx/render-pass'; -import { GFXSampler, IGFXSamplerInfo, GFXSamplerState } from '../../gfx/sampler'; +import { GFXSampler, IGFXSamplerInfo } from '../../gfx/sampler'; import { GFXShader } from '../../gfx/shader'; import { GFXTextureView } from '../../gfx/texture-view'; import { RenderPassStage, RenderPriority } from '../../pipeline/define'; diff --git a/cocos/renderer/scene/ambient.ts b/cocos/renderer/scene/ambient.ts new file mode 100644 index 0000000000..0203b776af --- /dev/null +++ b/cocos/renderer/scene/ambient.ts @@ -0,0 +1,49 @@ +import { vec3 } from '../../core/vmath'; +import { RenderScene } from './render-scene'; + +export class Ambient { + + set enabled (val) { + this._enabled = val; + } + get enabled () { + return this._enabled; + } + + get skyColor (): Float32Array { + return this._skyColor; + } + + set skyColor (color: Float32Array) { + this._skyColor = color; + } + + set skyIllum (illum: number) { + this._skyIllum = illum; + } + + get skyIllum (): number { + return this._skyIllum; + } + + get groundAlbedo (): Float32Array { + return this._groundAlbedo; + } + + set groundAlbedo (color: Float32Array) { + this._groundAlbedo = color; + } + + protected _enabled = true; + protected _skyColor = Float32Array.from([0.2, 0.5, 0.8, 1.0]); + protected _skyIllum: number = 1.0; + protected _groundAlbedo = Float32Array.from([0.2, 0.2, 0.2, 1.0]); + + protected _scene: RenderScene; + + constructor (scene: RenderScene) { + this._scene = scene; + } + + public update () {} +} diff --git a/cocos/renderer/scene/directional-light.ts b/cocos/renderer/scene/directional-light.ts index 0c1f50fac6..c47b5af628 100644 --- a/cocos/renderer/scene/directional-light.ts +++ b/cocos/renderer/scene/directional-light.ts @@ -9,21 +9,32 @@ const _v3 = new Vec3(); const _qt = new Quat(); export class DirectionalLight extends Light { - protected _direction = Float32Array.from([0, 0, 1, 0]); + protected _direction: vec3 = new vec3(0, 0, 1); + protected _directionArray: Float32Array = Float32Array.from([0, 0, 1, 0]); - set direction (val: Vec3) { - vec3.array(this._direction, val); + set direction (val: vec3) { + this._direction = val; + vec3.normalize(this._direction, this._direction); + vec3.array(this._directionArray, this._direction); } - get directionArray () { + + get direction (): vec3 { return this._direction; } - constructor (scene: RenderScene, node: Node, name: string) { - super(scene, node, name); + get directionArray (): Float32Array { + return this._directionArray; + } + + constructor (scene: RenderScene, name: string) { + super(scene, name); this._type = LightType.DIRECTIONAL; } public update () { - this.direction = vec3.transformQuat(_v3, _forward, this._node.getWorldRotation(_qt)); + if (this._node) { + this._direction = vec3.transformQuat(_v3, _forward, this._node.getWorldRotation(_qt)); + vec3.array(this._directionArray, this._direction); + } } } diff --git a/cocos/renderer/scene/light.ts b/cocos/renderer/scene/light.ts index ced5291010..563b52f1c1 100644 --- a/cocos/renderer/scene/light.ts +++ b/cocos/renderer/scene/light.ts @@ -1,5 +1,6 @@ import { Node } from '../../scene-graph/node'; import { RenderScene } from './render-scene'; +import { vec4 } from '../../core/vmath'; export enum LightType { DIRECTIONAL, @@ -17,10 +18,11 @@ export class Light { return this._enabled; } - set color (val: number[]) { - this._color.set(val); + set color (color: Float32Array) { + this._color.set(color); } - get colorData () { + + get color () { return this._color; } @@ -30,16 +32,24 @@ export class Light { get name (): string { return this._name; } + + set node (n: Node | null) { + this._node = n; + } + + get node (): Node | null { + return this._node; + } + protected _color = Float32Array.from([1, 1, 1, 1]); protected _enabled = false; protected _scene: RenderScene; - protected _node: Node; + protected _node: Node | null = null; protected _type: LightType; protected _name: string; - constructor (scene: RenderScene, node: Node, name: string) { + constructor (scene: RenderScene, name: string) { this._scene = scene; - this._node = node; this._name = name; this._type = LightType.UNKNOWN; } diff --git a/cocos/renderer/scene/point-light.ts b/cocos/renderer/scene/point-light.ts index 0a1953e01f..3495d390a3 100644 --- a/cocos/renderer/scene/point-light.ts +++ b/cocos/renderer/scene/point-light.ts @@ -20,12 +20,14 @@ export class PointLight extends Light { return this._positionAndRange; } - constructor (scene: RenderScene, node: Node, name: string) { - super(scene, node, name); + constructor (scene: RenderScene, name: string) { + super(scene, name); this._type = LightType.POINT; } public update () { - this.position = this._node.getWorldPosition(_v3); + if (this._node) { + this.position = this._node.getWorldPosition(_v3); + } } } diff --git a/cocos/renderer/scene/render-scene.ts b/cocos/renderer/scene/render-scene.ts index ef9b6003c0..59fd7dae0f 100644 --- a/cocos/renderer/scene/render-scene.ts +++ b/cocos/renderer/scene/render-scene.ts @@ -5,15 +5,16 @@ import { Root } from '../../core/root'; import { Mat4, Vec3 } from '../../core/value-types'; import { mat4, vec3 } from '../../core/vmath'; import { GFXPrimitiveMode } from '../../gfx/define'; +import { UBOForwardLights } from '../../pipeline/define'; import { Layers } from '../../scene-graph/layers'; import { Node } from '../../scene-graph/node'; +import { Ambient } from './ambient'; import { Camera, ICameraInfo } from './camera'; import { DirectionalLight } from './directional-light'; import { Light } from './light'; import { Model } from './model'; import { PointLight } from './point-light'; import { SpotLight } from './spot-light'; -import { UBOForwardLights } from '../../pipeline/define'; export interface IRenderSceneInfo { name: string; @@ -44,6 +45,14 @@ export class RenderScene { return this._cameras; } + get ambient (): Ambient { + return this._ambient; + } + + get mainLight (): DirectionalLight { + return this._mainLight; + } + get directionalLights (): DirectionalLight[] { return this._directionalLights; } @@ -67,6 +76,9 @@ export class RenderScene { private _root: Root; private _name: string = ''; private _cameras: Camera[] = []; + private _ambient: Ambient; + private _mainLight: DirectionalLight; + private _mainLightNode: Node; private _pointLights: PointLight[] = []; private _directionalLights: DirectionalLight[] = []; private _spotLights: SpotLight[] = []; @@ -75,10 +87,15 @@ export class RenderScene { constructor (root: Root) { this._root = root; + this._ambient = new Ambient (this); + this._mainLight = new DirectionalLight(this, name); + this._mainLightNode = new Node('mainLightNode'); + this._mainLight.node = this._mainLightNode; } public initialize (info: IRenderSceneInfo): boolean { this._name = info.name; + this._mainLight.direction = new vec3(1.0, -1.0, -1.0); return true; } @@ -110,25 +127,12 @@ export class RenderScene { this._cameras = []; } - public createPointLight (name: string, node: Node): Light | null { - if (this._pointLights.length >= UBOForwardLights.MAX_POINT_LIGHTS) { return null; } - const light = new PointLight(this, node, name); - this._pointLights.push(light); - return light; - } - - public destroyPointLight (light: Light) { - for (let i = 0; i < this._pointLights.length; ++i) { - if (this._pointLights[i] === light) { - this._pointLights.splice(i, 1); - return; - } - } - } - - public createDirectionalLight (name: string, node: Node): Light | null { + public createDirectionalLight (name: string, node?: Node): Light | null { if (this._directionalLights.length >= UBOForwardLights.MAX_DIR_LIGHTS) { return null; } - const light = new DirectionalLight(this, node, name); + const light = new DirectionalLight(this, name); + if (node) { + light.node = node; + } this._directionalLights.push(light); return light; } @@ -142,9 +146,31 @@ export class RenderScene { } } - public createSpotLight (name: string, node: Node): Light | null { + public createPointLight (name: string, node?: Node): Light | null { + if (this._pointLights.length >= UBOForwardLights.MAX_POINT_LIGHTS) { return null; } + const light = new PointLight(this, name); + if (node) { + light.node = node; + } + this._pointLights.push(light); + return light; + } + + public destroyPointLight (light: Light) { + for (let i = 0; i < this._pointLights.length; ++i) { + if (this._pointLights[i] === light) { + this._pointLights.splice(i, 1); + return; + } + } + } + + public createSpotLight (name: string, node?: Node): Light | null { if (this._spotLights.length >= UBOForwardLights.MAX_SPOT_LIGHTS) { return null; } - const light = new SpotLight(this, node, name); + const light = new SpotLight(this, name); + if (node) { + light.node = node; + } this._spotLights.push(light); return light; } diff --git a/cocos/renderer/scene/spot-light.ts b/cocos/renderer/scene/spot-light.ts index c48fbfe517..fb7d774d11 100644 --- a/cocos/renderer/scene/spot-light.ts +++ b/cocos/renderer/scene/spot-light.ts @@ -32,13 +32,15 @@ export class SpotLight extends Light { return this._direction; } - constructor (scene: RenderScene, node: Node, name: string) { - super(scene, node, name); + constructor (scene: RenderScene, name: string) { + super(scene, name); this._type = LightType.DIRECTIONAL; } public update () { - this.position = this._node.getWorldPosition(_v3); - this.direction = vec3.transformQuat(_v3, _forward, this._node.getWorldRotation(_qt)); + if (this._node) { + this.position = this._node.getWorldPosition(_v3); + this.direction = vec3.transformQuat(_v3, _forward, this._node.getWorldRotation(_qt)); + } } }