diff --git a/cocos/3d/assets/effect-asset.ts b/cocos/3d/assets/effect-asset.ts index 62b5387b6d..66b8368d05 100644 --- a/cocos/3d/assets/effect-asset.ts +++ b/cocos/3d/assets/effect-asset.ts @@ -15,6 +15,7 @@ export interface IPropertyInfo { export interface IPassInfo { program: string; // effect-writer part + switch?: string; priority?: number; primitive?: GFXPrimitiveMode; stage?: RenderPassStage; diff --git a/cocos/3d/assets/material.ts b/cocos/3d/assets/material.ts index 91173e50ab..3edf04ae1c 100644 --- a/cocos/3d/assets/material.ts +++ b/cocos/3d/assets/material.ts @@ -98,7 +98,7 @@ export class Material extends Asset { const cur = this._defines; let patch = info.defines; if (!Array.isArray(patch) && this._effectAsset) { // fill all the passes if not specified - const len = Effect.getPassInfos(this._effectAsset, this._techIdx).length; + const len = Effect.getPassesInfo(this._effectAsset, this._techIdx).length; patch = Array(len).fill(info.defines); } for (let i = 0; i < patch.length; i++) { @@ -181,10 +181,10 @@ export class Material extends Asset { public overridePipelineStates (overrides: PassOverrides, passIdx?: number) { if (!this._passes || !this._effectAsset) { return; } - const passInfos = Effect.getPassInfos(this._effectAsset, this._techIdx); + const passInfos = Effect.getPassesInfo(this._effectAsset, this._techIdx); if (passIdx === undefined) { - for (let i = 0; i < this._passes.length; i++) { - this._passes[i].overridePipelineStates(passInfos[i], overrides); + for (const pass of this._passes) { + pass.overridePipelineStates(passInfos[pass.idxInTech], overrides); } } else { this._passes[passIdx].overridePipelineStates(passInfos[passIdx], overrides); diff --git a/cocos/3d/builtin/effects.js b/cocos/3d/builtin/effects.js index ddcbe513d4..8091d6fee5 100644 --- a/cocos/3d/builtin/effects.js +++ b/cocos/3d/builtin/effects.js @@ -14,28 +14,7 @@ export default [ }, "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 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": ` - precision mediump float; - varying vec2 uv; - varying vec4 color; - - uniform sampler2D mainTexture; - uniform vec4 tintColor; - - vec4 add () { - return 2.0 * color * tintColor * texture2D(mainTexture, uv); - } - - vec4 multiply () { - vec4 col; - vec4 texColor = texture2D(mainTexture, uv); - col.rgb = tintColor.rgb * texColor.rgb * color.rgb * vec3(2.0); - col.a = (1.0 - texColor.a) * (tintColor.a * color.a * 2.0); - return col; - } - - void main() { gl_FragColor = add(); } - ` + "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":[]}, "defines": [ @@ -75,73 +54,7 @@ export default [ }, "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;\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": ` - precision mediump float; - varying vec3 viewDir; - - uniform samplerCube cubeMap; - - vec3 gammaToLinearSpaceRGB(vec3 sRGB) { - return sRGB * (sRGB * (sRGB * 0.305306011 + 0.682171111) + 0.012522878); - } - - vec3 linearToGammaSpaceRGB(vec3 RGB) { - vec3 S1 = sqrt(RGB); - vec3 S2 = sqrt(S1); - vec3 S3 = sqrt(S2); - return 0.585122381 * S1 + 0.783140355 * S2 - 0.368262736 * S3; - } - - vec4 gammaToLinearSpaceRGBA(vec4 sRGBA) { - return vec4(gammaToLinearSpaceRGB(sRGBA.rgb), sRGBA.a); - } - - vec4 linearToGammaSpaceRGBA(vec4 RGBA) { - return vec4(linearToGammaSpaceRGB(RGBA.rgb), RGBA.a); - } - - float gammaToLinearSpaceExact(float val) { - if (val <= 0.04045) { - return val / 12.92; - } else if (val < 1.0) { - return pow((val + 0.055) / 1.055, 2.4); - } else { - return pow(val, 2.2); - } - } - - float linearToGammaSpaceExact(float val) { - if (val <= 0.0) { - return 0.0; - } else if (val <= 0.0031308) { - return 12.92 * val; - } else if (val < 1.0) { - return 1.055 * pow(val, 0.4166667) - 0.055; - } else { - return pow(val, 0.45454545); - } - } - - vec3 unpackNormal(vec4 nmap) { - return nmap.xyz * 2.0 - 1.0; - } - - vec3 unpackRGBE(vec4 rgbe) { - return rgbe.rgb * pow(2.0, rgbe.a * 255.0 - 128.0); - } - - vec4 frag () { - #if USE_RGBE_CUBEMAP - vec3 c = unpackRGBE(textureCube(cubeMap, viewDir)); - c = linearToGammaSpaceRGB(c / (1.0 + c)); - return vec4(c, 1.0); - #else - return textureCube(cubeMap, viewDir); - #endif - } - - void main() { gl_FragColor = frag(); } - ` + "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":[]}, "defines": [ @@ -169,24 +82,7 @@ export default [ }, "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;\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": ` - precision mediump float; - varying vec2 uv0; - varying vec4 color; - - uniform sampler2D mainTexture; - - vec4 frag () { - vec4 o = vec4(1, 1, 1, 1); - - o *= texture2D(mainTexture, uv0); - o *= color; - - return o; - } - - void main() { gl_FragColor = frag(); } - ` + "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":[]}, "defines": [], @@ -201,253 +97,20 @@ 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"}}}]} + {"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"}}}, {"depthStencilState":{"depthTest":true, "depthWrite":false}, "program":"builtin-standard|planar-shadow-vs:vert|planar-shadow-fs:frag", "switch":"USE_PLANAR_SHADOW"}]} ], "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\n in vec2 a_texCoord;\n out 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 MAX_LIGHTS 4\nuniform CCForwardLights {\n vec4 cc_dirLightDirection[4]; \n vec4 cc_dirLightColor[4]; \n vec4 cc_pointLightPositionAndRange[4]; \n vec4 cc_pointLightColor[4]; \n vec4 cc_spotLightPositionAndRange[4]; \n vec4 cc_spotLightDirection[4]; \n vec4 cc_spotLightColor[4]; \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 sqrDist, float invSqrAttRadius)\n{\n float factor = sqrDist * 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 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}\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\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\n uniform sampler2D albedoSampler;\n#endif\n#if USE_NORMAL_MAP\n uniform sampler2D normalSampler;\n#endif\n#if USE_PBR_MAP\n uniform sampler2D pbrSampler;\n#endif\n#if USE_EMISSIVE_MAP\n uniform sampler2D emissiveSampler;\n#endif\n#if USE_NORMAL_MAP\n vec3 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 = 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 R = normalize(reflect(-V, N));\n \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 < 4; i++) {\n \n vec3 dL = normalize(-cc_dirLightDirection[i].xyz);\n vec3 dH = normalize(dL+V);\n float dNL = max(dot(N, dL), 0.001);\n float dNH = max(dot(N, dH), 0.0);\n vec3 dSpec = specular * CalcSpecular(roughness, dNH, dH, N);\n finalColor += dNL * cc_dirLightColor[i].rgb * cc_dirLightColor[i].w * (diffuseContrib + dSpec);\n vec3 PLU = cc_pointLightPositionAndRange[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 pInvSqrAttRadius = 1.0 / max(cc_pointLightPositionAndRange[i].w, 0.01);\n pInvSqrAttRadius *= pInvSqrAttRadius;\n float pDistSqr = dot(PLU, PLU);\n float pAtt = GetDistAtt(pDistSqr, pInvSqrAttRadius);\n vec3 pSpec = specular * CalcSpecular(roughness, PNH, PH, N);\n finalColor += PNL * cc_pointLightColor[i].rgb * cc_pointLightColor[i].w * pAtt * (diffuseContrib + pSpec);\n vec3 SLU = cc_spotLightPositionAndRange[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 cosConeAngle = max(dot(-cc_spotLightDirection[i].xyz, SL), 0.01);\n float sInvSqrAttRadius = 1.0 / max(cc_spotLightPositionAndRange[i].w, 0.01);\n float sDistSqr = dot(SLU, SLU);\n float sAtt = GetDistAtt(sDistSqr, sInvSqrAttRadius);\n sAtt *= cosConeAngle;\n vec3 sSpec = specular * CalcSpecular(roughness, SNH, SH, N);\n finalColor += SNL * cc_spotLightColor[i].rgb * cc_spotLightColor[i].w * sAtt * (diffuseContrib + sSpec);\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 = max(finalColor, vec3(0.0));\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` + "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\n in 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\n uniform sampler2D albedoSampler;\n#endif\n#if USE_NORMAL_MAP\n uniform sampler2D normalSampler;\n#endif\n#if USE_PBR_MAP\n uniform sampler2D pbrSampler;\n#endif\n#if USE_EMISSIVE_MAP\n uniform sampler2D emissiveSampler;\n#endif\n#if USE_NORMAL_MAP\n vec3 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 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\n attribute vec2 a_texCoord;\n varying 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": ` - precision mediump float; - - uniform vec4 cc_time; - uniform vec4 cc_screenSize; - uniform vec4 cc_screenScale; - uniform mat4 cc_matView; - uniform mat4 cc_matViewInv; - uniform mat4 cc_matProj; - uniform mat4 cc_matProjInv; - uniform mat4 cc_matViewProj; - uniform mat4 cc_matViewProjInv; - uniform vec4 cc_cameraPos; - uniform vec4 cc_mainLitDir; - uniform vec4 cc_mainLitColor; - uniform vec4 cc_ambientSky; - uniform vec4 cc_ambientGround; - - #define MAX_LIGHTS 4 - - uniform vec4 cc_dirLightDirection[4]; - uniform vec4 cc_dirLightColor[4]; - uniform vec4 cc_pointLightPositionAndRange[4]; - uniform vec4 cc_pointLightColor[4]; - uniform vec4 cc_spotLightPositionAndRange[4]; - uniform vec4 cc_spotLightDirection[4]; - uniform vec4 cc_spotLightColor[4]; - - #define PI 3.14159265359 - #define PI2 6.28318530718 - #define EPSILON 1e-6 - #define LOG2 1.442695 - - #define saturate(a) clamp( a, 0.0, 1.0 ) - - vec3 SRGBToLinear(vec3 gamma) - { - return pow(gamma, vec3(2.2)); - - } - - vec3 LinearToSRGB(vec3 linear) - { - return pow(linear, vec3(0.454545)); - - } - - float SmoothDistAtt(float sqrDist, float invSqrAttRadius) - { - float factor = sqrDist * invSqrAttRadius; - float factor2 = factor * factor; - float factor3 = factor2 * factor2; - float smoothFactor = clamp(1.0 - factor3 * factor3, 0.0, 1.0); - return smoothFactor * smoothFactor; - } - - float GetDistAtt(float distSqr, float invSqrAttRadius) - { - float attenuation = 1.0 / max(distSqr, 0.01*0.01); - attenuation *= SmoothDistAtt(distSqr , invSqrAttRadius); - return attenuation; - } - - float GetAngleAtt(vec3 L, vec3 litDir, float litAngleScale, float litAngleOffset) - { - float cd = dot(litDir, L); - float attenuation = clamp(cd * litAngleScale + litAngleOffset, 0.0, 1.0); - return (attenuation * attenuation); - } - - float GGXMobile(float roughness, float NoH, vec3 H, vec3 N) - { - vec3 NxH = cross(N, H); - float OneMinusNoHSqr = dot(NxH, NxH); - float a = roughness * roughness; - float n = NoH * a; - float p = a / (OneMinusNoHSqr + n * n); - return p * p; - } - - float CalcSpecular(float roughness, float NoH, vec3 H, vec3 N) - { - return (roughness*0.25 + 0.25) * GGXMobile(roughness, NoH, H, N); - } - - vec3 BRDFApprox(vec3 specular, float roughness, float NoV) - { - const vec4 c0 = vec4(-1.0, -0.0275, -0.572, 0.022); - const vec4 c1 = vec4(1.0, 0.0425, 1.04, -0.04); - vec4 r = roughness * c0 + c1; - float a004 = min( r.x * r.x, exp2( -9.28 * NoV ) ) * r.x + r.y; - vec2 AB = vec2( -1.04, 1.04 ) * a004 + r.zw; - AB.y *= clamp(50.0 * specular.g, 0.0, 1.0); - return specular * AB.x + AB.y; - } - - varying vec3 v_worldPos; - varying vec3 v_worldNormal; - - #if USE_ALBEDO_MAP || USE_NORMAL_MAP || USE_PBR_MAP || USE_EMISSIVE_MAP - varying vec2 v_uv; - #endif - - uniform vec4 tilingOffset; - uniform vec4 albedo; - uniform vec4 albedoScale; - uniform vec4 pbrParams; - uniform vec4 pbrScale; - uniform vec4 emissive; - uniform vec4 emissiveScale; - - #if USE_ALBEDO_MAP - uniform sampler2D albedoSampler; - #endif - #if USE_NORMAL_MAP - uniform sampler2D normalSampler; - #endif - #if USE_PBR_MAP - uniform sampler2D pbrSampler; - #endif - #if USE_EMISSIVE_MAP - uniform sampler2D emissiveSampler; - #endif - - #if USE_NORMAL_MAP - vec3 getNormal(vec3 pos, vec3 normal) { - vec3 q0 = vec3( dFdx( pos.x ), dFdx( pos.y ), dFdx( pos.z ) ); - vec3 q1 = vec3( dFdy( pos.x ), dFdy( pos.y ), dFdy( pos.z ) ); - vec2 st0 = dFdx( v_uv.st ); - vec2 st1 = dFdy( v_uv.st ); - vec3 S = normalize( q0 * st1.t - q1 * st0.t ); - vec3 T = normalize( -q0 * st1.s + q1 * st0.s ); - vec3 N = normal; - vec3 mapN = texture2D(normalSampler, v_uv).rgb * 2.0 - 1.0; - mapN.xy = 1.0 * mapN.xy; - mat3 tsn = mat3( S, T, N ); - return normalize( tsn * mapN ); - } - #endif - - vec4 frag () { - #if USE_ALBEDO_MAP - vec4 baseColor = texture2D(albedoSampler, v_uv); - #else - vec4 baseColor = albedo; - #endif - baseColor.rgb *= albedoScale.rgb; - baseColor.rgb = SRGBToLinear(baseColor.rgb); - - #if USE_ALPHA_TEST - if(baseColor.a < albedoScale.a) - discard; - #endif - - vec3 N = normalize(v_worldNormal); - #if USE_NORMAL_MAP - N = getNormal(v_worldPos, N); - #endif - - #if USE_PBR_MAP - vec4 pbr = texture2D(pbrSampler, v_uv); - #else - vec4 pbr = pbrParams; - #endif - pbr *= pbrScale; - float roughness = clamp(pbr.r, 0.04, 1.0); - float metallic = clamp(pbr.g, 0.0, 1.0); - - vec3 diffuse = baseColor.rgb * 0.96 * (1.0 - metallic); - vec3 specular = mix(vec3(0.04), baseColor.rgb, metallic); - - vec3 V = normalize(cc_cameraPos.xyz - v_worldPos); - vec3 L = normalize(-cc_mainLitDir.xyz); - vec3 H = normalize(L+V); - float NV = max(abs(dot(N, V)), 0.001); - float NL = max(dot(N, L), 0.001); - float NH = max(dot(N, H), 0.0); - - specular = BRDFApprox(specular, roughness, NV); - - vec3 R = normalize(reflect(-V, N)); - - vec3 specularContrib = specular * CalcSpecular(roughness, NH, H, N); - - vec3 diffuseContrib = diffuse / 3.14159265359; - vec3 finalColor = NL * cc_mainLitColor.rgb * cc_mainLitColor.w * (diffuseContrib + specularContrib); - - for (int i = 0; i < 4; i++) { - - vec3 dL = normalize(-cc_dirLightDirection[i].xyz); - vec3 dH = normalize(dL+V); - float dNL = max(dot(N, dL), 0.001); - float dNH = max(dot(N, dH), 0.0); - vec3 dSpec = specular * CalcSpecular(roughness, dNH, dH, N); - finalColor += dNL * cc_dirLightColor[i].rgb * cc_dirLightColor[i].w * (diffuseContrib + dSpec); - - vec3 PLU = cc_pointLightPositionAndRange[i].xyz - v_worldPos; - vec3 PL = normalize(PLU); - vec3 PH = normalize(PL+V); - float PNL = max(dot(N, PL), 0.001); - float PNH = max(dot(N, PH), 0.0); - float pInvSqrAttRadius = 1.0 / max(cc_pointLightPositionAndRange[i].w, 0.01); - pInvSqrAttRadius *= pInvSqrAttRadius; - float pDistSqr = dot(PLU, PLU); - float pAtt = GetDistAtt(pDistSqr, pInvSqrAttRadius); - vec3 pSpec = specular * CalcSpecular(roughness, PNH, PH, N); - finalColor += PNL * cc_pointLightColor[i].rgb * cc_pointLightColor[i].w * pAtt * (diffuseContrib + pSpec); - - vec3 SLU = cc_spotLightPositionAndRange[i].xyz - v_worldPos; - vec3 SL = normalize(SLU); - vec3 SH = normalize(SL+V); - float SNL = max(dot(N, SL), 0.001); - float SNH = max(dot(N, SH), 0.0); - float cosConeAngle = max(dot(-cc_spotLightDirection[i].xyz, SL), 0.01); - float sInvSqrAttRadius = 1.0 / max(cc_spotLightPositionAndRange[i].w, 0.01); - float sDistSqr = dot(SLU, SLU); - float sAtt = GetDistAtt(sDistSqr, sInvSqrAttRadius); - sAtt *= cosConeAngle; - vec3 sSpec = specular * CalcSpecular(roughness, SNH, SH, N); - finalColor += SNL * cc_spotLightColor[i].rgb * cc_spotLightColor[i].w * sAtt * (diffuseContrib + sSpec); - } - - float fAmb = dot(N, vec3(0.0, -1.0, 0.0)) * 0.5 + 0.5; - vec3 ambDiff = mix(cc_ambientSky.rgb, cc_ambientGround.rgb, fAmb) * cc_ambientSky.w; - finalColor += (ambDiff.rgb * diffuse); - finalColor = max(finalColor, vec3(0.0)); - finalColor = LinearToSRGB(finalColor); - finalColor = max(finalColor, vec3(pbrParams.g)); - - return vec4(finalColor, baseColor.a); - } - - void main() { gl_FragColor = frag(); } - ` + "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\n varying 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\n uniform sampler2D albedoSampler;\n#endif\n#if USE_NORMAL_MAP\n uniform sampler2D normalSampler;\n#endif\n#if USE_PBR_MAP\n uniform sampler2D pbrSampler;\n#endif\n#if USE_EMISSIVE_MAP\n uniform sampler2D emissiveSampler;\n#endif\n#if USE_NORMAL_MAP\n vec3 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 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", "CCForwardLights"], "textures":["cc_jointsTexture"]}, + "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"]}, @@ -475,6 +138,22 @@ export default [ {"name":"emissiveSampler", "type":29, "count":1, "defines":["USE_EMISSIVE_MAP"], "binding":4} ], "dependencies": {} + }, + { + "name": "builtin-standard|planar-shadow-vs:vert|planar-shadow-fs:frag", + "glsl3": { + "vert": `\nprecision highp 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};\nuniform CCShadow {\n mat4 cc_matLightPlaneProj;\n vec4 cc_shadowColor;\n};\nin vec3 a_position;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n return cc_matViewProj * cc_matLightPlaneProj * cc_matWorld * pos;\n}\nvoid main() { gl_Position = vert(); }\n`, + "frag": `\nprecision highp float;\nuniform CCShadow {\n mat4 cc_matLightPlaneProj;\n vec4 cc_shadowColor;\n};\nvec4 frag () {\n return cc_shadowColor;\n}\nout vec4 cc_FragColor;\nvoid main() { cc_FragColor = frag(); }\n` + }, + "glsl1": { + "vert": `\nprecision highp 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;\nuniform mat4 cc_matLightPlaneProj;\nuniform vec4 cc_shadowColor;\nattribute vec3 a_position;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n return cc_matViewProj * cc_matLightPlaneProj * cc_matWorld * pos;\n}\nvoid main() { gl_Position = vert(); }\n`, + "frag": `\nprecision highp float;\nuniform mat4 cc_matLightPlaneProj;\nuniform vec4 cc_shadowColor;\nvec4 frag () {\n return cc_shadowColor;\n}\nvoid main() { gl_FragColor = frag(); }\n` + }, + "builtins": {"blocks":["CCGlobal", "CCLocal", "CCShadow"], "textures":[]}, + "defines": [], + "blocks": [], + "samplers": [], + "dependencies": {} } ] }, @@ -492,37 +171,7 @@ export default [ }, "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 )\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": ` - precision mediump float; - - float ComputeEV100(float aperture , float shutterTime , float iso) { - return log2((aperture * aperture) / shutterTime * 100.0 / iso); - } - - float ComputeExposure(float ev100) { - return 0.833333 * pow(2.0, ev100); - } - - vec3 ACESToneMap(vec3 color) { - const float A = 2.51; - const float B = 0.03; - const float C = 2.43; - const float D = 0.59; - const float E = 0.14; - return (color * (A * color + B)) / (color * (C * color + D) + E); - } - - varying vec2 v_uv; - uniform sampler2D u_texSampler; - - vec4 frag () { - vec4 o = texture2D(u_texSampler, v_uv); - - return o; - } - - void main() { gl_FragColor = frag(); } - ` + "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": [], @@ -548,35 +197,7 @@ export default [ }, "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;\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": ` - precision mediump float; - - #if USE_TEXTURE - varying vec2 uv0; - uniform sampler2D mainTexture; - #endif - - #if USE_COLOR - uniform vec4 color; - - #endif - - vec4 frag () { - vec4 o = vec4(1, 1, 1, 1); - - #if USE_TEXTURE - o *= texture2D(mainTexture, uv0); - #endif - - #if USE_COLOR - o *= color; - #endif - - return o; - } - - void main() { gl_FragColor = frag(); } - ` + "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"]}, "defines": [ diff --git a/cocos/3d/framework/light-component.ts b/cocos/3d/framework/light-component.ts index f9db6b126c..cff7dfc7c3 100644 --- a/cocos/3d/framework/light-component.ts +++ b/cocos/3d/framework/light-component.ts @@ -296,6 +296,8 @@ export class LightComponent extends Component { scene.destroySpotLight(this._light); break; } + } else { + this._light.node = scene.defaultMainLightNode; } this._light = null; } diff --git a/cocos/3d/primitive/plane.ts b/cocos/3d/primitive/plane.ts index 7b5ede6391..b842e5c80a 100644 --- a/cocos/3d/primitive/plane.ts +++ b/cocos/3d/primitive/plane.ts @@ -26,10 +26,10 @@ interface IPlaneOptions extends RecursivePartial { function applyDefaultPlaneOptions (options?: RecursivePartial): IPlaneOptions { options = applyDefaultGeometryOptions(options); - options.width = 10; - options.length = 10; - options.widthSegments = 10; - options.lengthSegments = 10; + options.width = options.width || 10; + options.length = options.length || 10; + options.widthSegments = options.widthSegments || 10; + options.lengthSegments = options.lengthSegments || 10; return options as IPlaneOptions; } diff --git a/cocos/gfx/define.ts b/cocos/gfx/define.ts index 51a47c65f0..6c580647cb 100644 --- a/cocos/gfx/define.ts +++ b/cocos/gfx/define.ts @@ -1,5 +1,3 @@ -import { ccenum } from '../core/value-types/enum'; - // Extensions export enum WebGLEXT { COMPRESSED_RGB_S3TC_DXT1_EXT = 0x83F0, @@ -360,8 +358,6 @@ export enum GFXBlendFactor { ONE_MINUS_CONSTANT_ALPHA, } -ccenum(GFXBlendFactor); - export enum GFXColorMask { NONE = 0x0, R = 0x1, diff --git a/cocos/pipeline/define.ts b/cocos/pipeline/define.ts index e8d4488226..5432bfa5df 100644 --- a/cocos/pipeline/define.ts +++ b/cocos/pipeline/define.ts @@ -22,6 +22,7 @@ export enum UniformBinding { UBO_FORWARD_LIGHTS = MAX_BINDING_SUPPORTED - 3, UBO_SKINNING = MAX_BINDING_SUPPORTED - 4, UBO_UI = MAX_BINDING_SUPPORTED - 5, + UBO_SHADOW = MAX_BINDING_SUPPORTED - 6, // samplers SAMPLER_JOINTS = MAX_BINDING_SUPPORTED + 1, } @@ -84,34 +85,34 @@ export class UBOLocal { public view: Float32Array = new Float32Array(UBOLocal.COUNT); } -export class UBOForwardLights { +export class UBOForwardLight { public static MAX_DIR_LIGHTS = 4; public static MAX_POINT_LIGHTS = 4; public static MAX_SPOT_LIGHTS = 4; public static DIR_LIGHT_DIR_OFFSET: number = 0; - public static DIR_LIGHT_COLOR_OFFSET: number = UBOForwardLights.DIR_LIGHT_DIR_OFFSET + UBOForwardLights.MAX_DIR_LIGHTS * 4; - public static POINT_LIGHT_POS_RANGE_OFFSET: number = UBOForwardLights.DIR_LIGHT_COLOR_OFFSET + UBOForwardLights.MAX_DIR_LIGHTS * 4; - public static POINT_LIGHT_COLOR_OFFSET: number = UBOForwardLights.POINT_LIGHT_POS_RANGE_OFFSET + UBOForwardLights.MAX_POINT_LIGHTS * 4; - public static SPOT_LIGHT_POS_RANGE_OFFSET: number = UBOForwardLights.POINT_LIGHT_COLOR_OFFSET + UBOForwardLights.MAX_POINT_LIGHTS * 4; - public static SPOT_LIGHT_DIR_OFFSET: number = UBOForwardLights.SPOT_LIGHT_POS_RANGE_OFFSET + UBOForwardLights.MAX_SPOT_LIGHTS * 4; - public static SPOT_LIGHT_COLOR_OFFSET: number = UBOForwardLights.SPOT_LIGHT_DIR_OFFSET + UBOForwardLights.MAX_SPOT_LIGHTS * 4; - public static COUNT: number = UBOForwardLights.SPOT_LIGHT_COLOR_OFFSET + UBOForwardLights.MAX_SPOT_LIGHTS * 4; - public static SIZE: number = UBOForwardLights.COUNT * 4; + public static DIR_LIGHT_COLOR_OFFSET: number = UBOForwardLight.DIR_LIGHT_DIR_OFFSET + UBOForwardLight.MAX_DIR_LIGHTS * 4; + public static POINT_LIGHT_POS_RANGE_OFFSET: number = UBOForwardLight.DIR_LIGHT_COLOR_OFFSET + UBOForwardLight.MAX_DIR_LIGHTS * 4; + public static POINT_LIGHT_COLOR_OFFSET: number = UBOForwardLight.POINT_LIGHT_POS_RANGE_OFFSET + UBOForwardLight.MAX_POINT_LIGHTS * 4; + public static SPOT_LIGHT_POS_RANGE_OFFSET: number = UBOForwardLight.POINT_LIGHT_COLOR_OFFSET + UBOForwardLight.MAX_POINT_LIGHTS * 4; + public static SPOT_LIGHT_DIR_OFFSET: number = UBOForwardLight.SPOT_LIGHT_POS_RANGE_OFFSET + UBOForwardLight.MAX_SPOT_LIGHTS * 4; + public static SPOT_LIGHT_COLOR_OFFSET: number = UBOForwardLight.SPOT_LIGHT_DIR_OFFSET + UBOForwardLight.MAX_SPOT_LIGHTS * 4; + public static COUNT: number = UBOForwardLight.SPOT_LIGHT_COLOR_OFFSET + UBOForwardLight.MAX_SPOT_LIGHTS * 4; + public static SIZE: number = UBOForwardLight.COUNT * 4; public static BLOCK: GFXUniformBlock = { binding: UniformBinding.UBO_FORWARD_LIGHTS, name: 'CCForwardLights', members: [ - { name: 'cc_dirLightDirection', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_DIR_LIGHTS }, - { name: 'cc_dirLightColor', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_DIR_LIGHTS }, - { name: 'cc_pointLightPositionAndRange', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_POINT_LIGHTS }, - { name: 'cc_pointLightColor', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_POINT_LIGHTS }, - { name: 'cc_spotLightPositionAndRange', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_SPOT_LIGHTS }, - { name: 'cc_spotLightDirection', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_SPOT_LIGHTS }, - { name: 'cc_spotLightColor', type: GFXType.FLOAT4, count: UBOForwardLights.MAX_SPOT_LIGHTS }, + { name: 'cc_dirLightDirection', type: GFXType.FLOAT4, count: UBOForwardLight.MAX_DIR_LIGHTS }, + { name: 'cc_dirLightColor', type: GFXType.FLOAT4, count: UBOForwardLight.MAX_DIR_LIGHTS }, + { name: 'cc_pointLightPositionAndRange', type: GFXType.FLOAT4, count: UBOForwardLight.MAX_POINT_LIGHTS }, + { name: 'cc_pointLightColor', type: GFXType.FLOAT4, count: UBOForwardLight.MAX_POINT_LIGHTS }, + { name: 'cc_spotLightPositionAndRange', type: GFXType.FLOAT4, count: UBOForwardLight.MAX_SPOT_LIGHTS }, + { name: 'cc_spotLightDirection', type: GFXType.FLOAT4, count: UBOForwardLight.MAX_SPOT_LIGHTS }, + { name: 'cc_spotLightColor', type: GFXType.FLOAT4, count: UBOForwardLight.MAX_SPOT_LIGHTS }, ], }; - public view: Float32Array = new Float32Array(UBOForwardLights.COUNT); + public view: Float32Array = new Float32Array(UBOForwardLight.COUNT); } export class UBOSkinning { @@ -146,6 +147,20 @@ export class UBOUI { public view: Float32Array = new Float32Array(UBOUI.COUNT); } +export class UBOShadow { + public static MAT_LIGHT_PLANE_PROJ_OFFSET: number = 0; + public static SHADOW_COLOR_OFFSET: number = UBOShadow.MAT_LIGHT_PLANE_PROJ_OFFSET + 16; + public static COUNT: number = UBOShadow.SHADOW_COLOR_OFFSET + 4; + public static SIZE: number = UBOShadow.COUNT * 4; + + public static BLOCK: GFXUniformBlock = { + binding: UniformBinding.UBO_SHADOW, name: 'CCShadow', members: [ + { name: 'cc_matLightPlaneProj', type: GFXType.MAT4, count: 1 }, + { name: 'cc_shadowColor', type: GFXType.FLOAT4, count: 1 }, + ], + }; +} + export interface IGlobalBindingDesc { type: GFXBindingType; blockInfo?: GFXUniformBlock; diff --git a/cocos/pipeline/forward/forward-pipeline.ts b/cocos/pipeline/forward/forward-pipeline.ts index 1aeff3498f..77b15ed01e 100644 --- a/cocos/pipeline/forward/forward-pipeline.ts +++ b/cocos/pipeline/forward/forward-pipeline.ts @@ -2,7 +2,7 @@ import { Root } from '../../core/root'; import { GFXBuffer } from '../../gfx/buffer'; import { GFXBindingType, GFXBufferUsageBit, GFXMemoryUsageBit } from '../../gfx/define'; import { GFXRenderPass } from '../../gfx/render-pass'; -import { RenderPassStage, UBOForwardLights } from '../define'; +import { RenderPassStage, UBOForwardLight } from '../define'; import { ToneMapFlow } from '../ppfx/tonemap-flow'; import { RenderPipeline } from '../render-pipeline'; import { RenderView } from '../render-view'; @@ -16,7 +16,7 @@ const _idVec4Array = Float32Array.from([1, 1, 1, 0]); export class ForwardPipeline extends RenderPipeline { - protected _uboLights: UBOForwardLights = new UBOForwardLights(); + protected _uboLights: UBOForwardLight = new UBOForwardLight(); protected _lightsUBO: GFXBuffer | null = null; public get lightsUBO (): GFXBuffer { @@ -80,20 +80,20 @@ export class ForwardPipeline extends RenderPipeline { protected createUBOs (): boolean { super.createUBOs(); - if (!this._globalBindings.get(UBOForwardLights.BLOCK.name)) { + if (!this._globalBindings.get(UBOForwardLight.BLOCK.name)) { const lightsUBO = this._root.device.createBuffer({ usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, - size: UBOForwardLights.SIZE, + size: UBOForwardLight.SIZE, }); if (!lightsUBO) { return false; } - this._globalBindings.set(UBOForwardLights.BLOCK.name, { + this._globalBindings.set(UBOForwardLight.BLOCK.name, { type: GFXBindingType.UNIFORM_BUFFER, - blockInfo: UBOForwardLights.BLOCK, + blockInfo: UBOForwardLight.BLOCK, buffer: lightsUBO, }); } @@ -104,10 +104,10 @@ export class ForwardPipeline extends RenderPipeline { protected destroyUBOs () { super.destroyUBOs(); - const lightsUBO = this._globalBindings.get(UBOForwardLights.BLOCK.name); + const lightsUBO = this._globalBindings.get(UBOForwardLight.BLOCK.name); if (lightsUBO) { lightsUBO.buffer!.destroy(); - this._globalBindings.delete(UBOForwardLights.BLOCK.name); + this._globalBindings.delete(UBOForwardLight.BLOCK.name); } } @@ -117,45 +117,45 @@ export class ForwardPipeline extends RenderPipeline { const scene = view.camera.scene; const dLights = scene.directionalLights; - for (let i = 0; i < UBOForwardLights.MAX_DIR_LIGHTS; i++) { + for (let i = 0; i < UBOForwardLight.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.color, UBOForwardLights.DIR_LIGHT_COLOR_OFFSET + i * 4); + this._uboLights.view.set(light.directionArray, UBOForwardLight.DIR_LIGHT_DIR_OFFSET + i * 4); + this._uboLights.view.set(light.color, UBOForwardLight.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); + this._uboLights.view.set(_idVec4Array, UBOForwardLight.DIR_LIGHT_DIR_OFFSET + i * 4); + this._uboLights.view.set(_idVec4Array, UBOForwardLight.DIR_LIGHT_COLOR_OFFSET + i * 4); } } const pLights = scene.pointLights; - for (let i = 0; i < UBOForwardLights.MAX_POINT_LIGHTS; i++) { + for (let i = 0; i < UBOForwardLight.MAX_POINT_LIGHTS; i++) { const light = pLights[i]; 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.color, UBOForwardLights.POINT_LIGHT_COLOR_OFFSET + i * 4); + this._uboLights.view.set(light.positionAndRange, UBOForwardLight.POINT_LIGHT_POS_RANGE_OFFSET + i * 4); + this._uboLights.view.set(light.color, UBOForwardLight.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); + this._uboLights.view.set(_idVec4Array, UBOForwardLight.POINT_LIGHT_POS_RANGE_OFFSET + i * 4); + this._uboLights.view.set(_idVec4Array, UBOForwardLight.POINT_LIGHT_COLOR_OFFSET + i * 4); } } const sLights = scene.spotLights; - for (let i = 0; i < UBOForwardLights.MAX_SPOT_LIGHTS; i++) { + for (let i = 0; i < UBOForwardLight.MAX_SPOT_LIGHTS; i++) { const light = sLights[i]; if (light && light.enabled) { 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.color, UBOForwardLights.SPOT_LIGHT_COLOR_OFFSET + i * 4); + this._uboLights.view.set(light.positionAndRange, UBOForwardLight.SPOT_LIGHT_POS_RANGE_OFFSET + i * 4); + this._uboLights.view.set(light.directionArray, UBOForwardLight.SPOT_LIGHT_DIR_OFFSET + i * 4); + this._uboLights.view.set(light.color, UBOForwardLight.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); - this._uboLights.view.set(_idVec4Array, UBOForwardLights.SPOT_LIGHT_COLOR_OFFSET + i * 4); + this._uboLights.view.set(_idVec4Array, UBOForwardLight.SPOT_LIGHT_POS_RANGE_OFFSET + i * 4); + this._uboLights.view.set(_idVec4Array, UBOForwardLight.SPOT_LIGHT_DIR_OFFSET + i * 4); + this._uboLights.view.set(_idVec4Array, UBOForwardLight.SPOT_LIGHT_COLOR_OFFSET + i * 4); } } // update ubos - this._globalBindings.get(UBOForwardLights.BLOCK.name)!.buffer!.update(this._uboLights.view); + this._globalBindings.get(UBOForwardLight.BLOCK.name)!.buffer!.update(this._uboLights.view); } } diff --git a/cocos/pipeline/render-pipeline.ts b/cocos/pipeline/render-pipeline.ts index 4c2c7177c3..0534e63272 100644 --- a/cocos/pipeline/render-pipeline.ts +++ b/cocos/pipeline/render-pipeline.ts @@ -21,7 +21,7 @@ import { GFXInputAssembler, IGFXInputAttribute } from '../gfx/input-assembler'; import { GFXRenderPass } from '../gfx/render-pass'; import { GFXTexture } from '../gfx/texture'; import { GFXTextureView } from '../gfx/texture-view'; -import { UBOGlobal } from './define'; +import { UBOGlobal, UBOShadow } from './define'; import { IGlobalBindingDesc } from './define'; import { IRenderFlowInfo, RenderFlow } from './render-flow'; import { RenderQueue } from './render-queue'; @@ -390,15 +390,38 @@ export abstract class RenderPipeline { }); } + if (!this._globalBindings.get(UBOShadow.BLOCK.name)) { + const shadowUBO = this._root.device.createBuffer({ + usage: GFXBufferUsageBit.UNIFORM | GFXBufferUsageBit.TRANSFER_DST, + memUsage: GFXMemoryUsageBit.HOST | GFXMemoryUsageBit.DEVICE, + size: UBOShadow.SIZE, + }); + + if (!shadowUBO) { + return false; + } + + this._globalBindings.set(UBOShadow.BLOCK.name, { + type: GFXBindingType.UNIFORM_BUFFER, + blockInfo: UBOShadow.BLOCK, + buffer: shadowUBO, + }); + } + return true; } protected destroyUBOs () { - const defaultUBO = this._globalBindings.get(UBOGlobal.BLOCK.name); - if (defaultUBO) { - defaultUBO.buffer!.destroy(); + const globalUBO = this._globalBindings.get(UBOGlobal.BLOCK.name); + if (globalUBO) { + globalUBO.buffer!.destroy(); this._globalBindings.delete(UBOGlobal.BLOCK.name); } + const shadowUBO = this._globalBindings.get(UBOShadow.BLOCK.name); + if (shadowUBO) { + shadowUBO.buffer!.destroy(); + this._globalBindings.delete(UBOShadow.BLOCK.name); + } } protected updateUBOs (view: RenderView) { @@ -473,6 +496,10 @@ export abstract class RenderPipeline { // update ubos this._globalBindings.get(UBOGlobal.BLOCK.name)!.buffer!.update(this._defaultUboGlobal!.view.buffer); + + const planarShadow = scene.planarShadow; + planarShadow.update(scene.mainLight); + this._globalBindings.get(UBOShadow.BLOCK.name)!.buffer!.update(planarShadow.data); } protected sceneCulling (view: RenderView) { diff --git a/cocos/renderer/core/effect.ts b/cocos/renderer/core/effect.ts index d7c660fc48..9155a4c2b7 100644 --- a/cocos/renderer/core/effect.ts +++ b/cocos/renderer/core/effect.ts @@ -10,7 +10,7 @@ export interface IEffectInfo { } export class Effect { - public static getPassInfos (effect: EffectAsset, techIdx: number) { + public static getPassesInfo (effect: EffectAsset, techIdx: number) { return effect.techniques[techIdx].passes; } @@ -19,13 +19,15 @@ export class Effect { const { techIdx, defines } = info || {} as IEffectInfo; const tech = effect.techniques[techIdx || 0]; const passNum = tech.passes.length; - const passes: Pass[] = new Array(passNum); + const passes: Pass[] = []; for (let k = 0; k < passNum; ++k) { const passInfo = tech.passes[k] as IPassInfoFull; - passInfo.defines = defines && defines[k] || {}; + const defs = passInfo.curDefs = defines && defines[k] || {}; + if (passInfo.switch && !defs[passInfo.switch]) { continue; } + passInfo.idxInTech = k; const pass = new Pass(cc.game._gfxDevice); pass.initialize(passInfo); - passes[k] = pass; + passes.push(pass); } return passes; } diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index bc9fd31a43..23eb70fd28 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -23,7 +23,8 @@ import { programLib } from './program-lib'; export interface IDefineMap { [name: string]: number | boolean; } export interface IPassInfoFull extends IPassInfo { // generated part - defines: IDefineMap; + idxInTech: number; + curDefs: IDefineMap; } export type PassOverrides = RecursivePartial; @@ -101,7 +102,8 @@ export class Pass { protected _textureViews: Record = {}; protected _resources: IPassResources[] = []; // internal data - protected _programName: string = ''; + protected _idxInTech = 0; + protected _programName = ''; protected _priority: RenderPriority = RenderPriority.DEFAULT; protected _primitive: GFXPrimitiveMode = GFXPrimitiveMode.TRIANGLE_LIST; protected _stage: RenderPassStage = RenderPassStage.DEFAULT; @@ -125,8 +127,9 @@ export class Pass { } public initialize (info: IPassInfoFull) { + this._idxInTech = info.idxInTech; this._programName = info.program; - this._defines = info.defines; + this._defines = info.curDefs; const shaderInfo = this._shaderInfo = programLib.getTemplate(info.program); // pipeline state const device = this._device; @@ -144,7 +147,6 @@ export class Pass { else { console.error('create buffer failed.'); return; } // buffer data processing system const block: IBlock = this._blocks[u.binding] = { - // buffer: buffer.buffer as ArrayBuffer, buffer: new ArrayBuffer(u.size), dirty: false, views: [], @@ -182,11 +184,7 @@ export class Pass { } public getHandle (name: string) { - const handle = this._handleMap[name]; - if (!handle) { - console.warn('illegal property name ' + name); - } - return handle; + return this._handleMap[name]; } public getBinding (name: string) { @@ -376,6 +374,7 @@ export class Pass { Object.assign(this._dss, info.depthStencilState); } + get idxInTech () { return this._idxInTech; } get programName () { return this._programName; } get priority () { return this._priority; } get primitive () { return this._primitive; } diff --git a/cocos/renderer/core/program-lib.ts b/cocos/renderer/core/program-lib.ts index d367944fc3..cc41385f12 100644 --- a/cocos/renderer/core/program-lib.ts +++ b/cocos/renderer/core/program-lib.ts @@ -123,7 +123,7 @@ class ProgramLib { // get template const tmpl = this._templates[name]; const customDef = _generateDefines(device, defines, tmpl.defines, tmpl.dependencies) + '\n'; - if (!tmpl.builtinInited) { insertPipelineBuiltins(tmpl, pipeline); } + if (!tmpl.builtinInited) { insertPipelineGlobalBindings(tmpl, pipeline); } let vert: string = ''; let frag: string = ''; @@ -150,7 +150,7 @@ class ProgramLib { } } -function insertPipelineBuiltins (tmpl: IProgramInfo, pipeline: RenderPipeline) { +function insertPipelineGlobalBindings (tmpl: IProgramInfo, pipeline: RenderPipeline) { const source = pipeline.globalBindings; const target = tmpl.builtins; for (const b of target.blocks) { diff --git a/cocos/renderer/scene/directional-light.ts b/cocos/renderer/scene/directional-light.ts index ae724a9b32..4ead99592d 100644 --- a/cocos/renderer/scene/directional-light.ts +++ b/cocos/renderer/scene/directional-light.ts @@ -9,8 +9,8 @@ const _v3 = new Vec3(); const _qt = new Quat(); export class DirectionalLight extends Light { - protected _direction: Vec3 = new Vec3(0, 0, 1); - protected _directionArray: Float32Array = Float32Array.from([0, 0, 1, 0]); + protected _direction = new Vec3(0, 0, 1); + protected _directionArray = Float32Array.from([0, 0, 1, 0]); set direction (val: Vec3) { this._direction = val; diff --git a/cocos/renderer/scene/planar-shadow.ts b/cocos/renderer/scene/planar-shadow.ts new file mode 100644 index 0000000000..36ea6ef8d7 --- /dev/null +++ b/cocos/renderer/scene/planar-shadow.ts @@ -0,0 +1,79 @@ +import { Color, Vec3 } from '../../core/value-types'; +import { color4, vec3 } from '../../core/vmath'; +import { UBOShadow } from '../../pipeline/define'; +import { Light } from './light'; +import { RenderScene } from './render-scene'; + +const _v3 = new Vec3(); + +export class PlanarShadow { + + set normal (val: Vec3) { + vec3.copy(this._normal, val); + } + get normal () { + return this._normal; + } + + set distance (val: number) { + this._distance = val; + } + get distance () { + return this._distance; + } + + set offset (val: number) { + this._offset = val; + } + get offset () { + return this._offset; + } + + set shadowColor (color: Color) { + color4.array(this._data, color, UBOShadow.SHADOW_COLOR_OFFSET); + } + + get data () { + return this._data; + } + + protected _scene: RenderScene; + protected _normal = new Vec3(0, 1, 0); + protected _distance = 0; + protected _offset = 0.01; + protected _data = Float32Array.from([ + 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, // matLightPlaneProj + 0.3, 0.3, 0.3, 1.0, // shadowColor + ]); + + constructor (scene: RenderScene) { + this._scene = scene; + } + + // tslint:disable: one-variable-per-declaration + public update (light: Light) { + light.node.getWorldPosition(_v3); + const n = this._normal, d = this._distance + this._offset; + const lx = _v3.x, ly = _v3.y, lz = _v3.z; + const nx = n.x, ny = n.y, nz = n.z; + const NdL = vec3.dot(n, _v3); + const m = this._data; + m[0] = NdL - d - lx * nx; + m[1] = -ly * nx; + m[2] = -lz * nx; + m[3] = -nx; + m[4] = -lx * ny; + m[5] = NdL - d - ly * ny; + m[6] = -lz * ny; + m[7] = -ny; + m[8] = -lx * nz; + m[9] = -ly * nz; + m[10] = NdL - d - lz * nz; + m[11] = -nz; + m[12] = lx * d; + m[13] = ly * d; + m[14] = lz * d; + m[15] = NdL; + } + // tslint:enable: one-variable-per-declaration +} diff --git a/cocos/renderer/scene/render-scene.ts b/cocos/renderer/scene/render-scene.ts index 58a825ad31..30f2a78772 100644 --- a/cocos/renderer/scene/render-scene.ts +++ b/cocos/renderer/scene/render-scene.ts @@ -5,7 +5,7 @@ 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 { UBOForwardLight } from '../../pipeline/define'; import { Layers } from '../../scene-graph/layers'; import { Node } from '../../scene-graph/node'; import { Ambient } from './ambient'; @@ -13,6 +13,7 @@ import { Camera, ICameraInfo } from './camera'; import { DirectionalLight } from './directional-light'; import { Light } from './light'; import { Model } from './model'; +import { PlanarShadow } from './planar-shadow'; import { PointLight } from './point-light'; import { Skybox } from './skybox'; import { SpotLight } from './spot-light'; @@ -54,6 +55,14 @@ export class RenderScene { return this._skybox; } + get planarShadow (): PlanarShadow { + return this._planarShadow; + } + + get defaultMainLightNode (): Node { + return this._defaultMainLightNode; + } + get mainLight (): DirectionalLight { return this._mainLight; } @@ -83,7 +92,9 @@ export class RenderScene { private _cameras: Camera[] = []; private _ambient: Ambient; private _skybox: Skybox; + private _planarShadow: PlanarShadow; private _mainLight: DirectionalLight; + private _defaultMainLightNode: Node; private _pointLights: PointLight[] = []; private _directionalLights: DirectionalLight[] = []; private _spotLights: SpotLight[] = []; @@ -92,14 +103,15 @@ export class RenderScene { constructor (root: Root) { this._root = root; - this._ambient = new Ambient (this); - this._mainLight = new DirectionalLight(this, 'Main Light', new Node('Main Light')); + this._ambient = new Ambient(this); + this._defaultMainLightNode = new Node('Main Light'); + this._mainLight = new DirectionalLight(this, 'Main Light', this._defaultMainLightNode); this._skybox = new Skybox(this); + this._planarShadow = new PlanarShadow(this); } public initialize (info: IRenderSceneInfo): boolean { this._name = info.name; - // this._mainLight.direction = new Vec3(1.0, -1.0, -1.0); // should be in editor prefab return true; } @@ -131,7 +143,7 @@ export class RenderScene { } public createDirectionalLight (name: string, node: Node): Light | null { - if (this._directionalLights.length >= UBOForwardLights.MAX_DIR_LIGHTS) { return null; } + if (this._directionalLights.length >= UBOForwardLight.MAX_DIR_LIGHTS) { return null; } const light = new DirectionalLight(this, name, node); this._directionalLights.push(light); return light; @@ -147,7 +159,7 @@ export class RenderScene { } public createPointLight (name: string, node: Node): Light | null { - if (this._pointLights.length >= UBOForwardLights.MAX_POINT_LIGHTS) { return null; } + if (this._pointLights.length >= UBOForwardLight.MAX_POINT_LIGHTS) { return null; } const light = new PointLight(this, name, node); this._pointLights.push(light); return light; @@ -163,7 +175,7 @@ export class RenderScene { } public createSpotLight (name: string, node: Node): Light | null { - if (this._spotLights.length >= UBOForwardLights.MAX_SPOT_LIGHTS) { return null; } + if (this._spotLights.length >= UBOForwardLight.MAX_SPOT_LIGHTS) { return null; } const light = new SpotLight(this, name, node); this._spotLights.push(light); return light; diff --git a/cocos/scene-graph/scene-globals.ts b/cocos/scene-graph/scene-globals.ts index d0adc9212e..49ad637a0b 100644 --- a/cocos/scene-graph/scene-globals.ts +++ b/cocos/scene-graph/scene-globals.ts @@ -25,11 +25,17 @@ import { TextureCube } from '../3d/assets/texture-cube'; import { ccclass, property } from '../core/data/class-decorator'; -import { Color } from '../core/value-types'; -import { color4 } from '../core/vmath'; +import { Color, Quat, Vec3 } from '../core/value-types'; +import { color4, vec3 } from '../core/vmath'; import { Ambient } from '../renderer/scene/ambient'; +import { PlanarShadow } from '../renderer/scene/planar-shadow'; import { RenderScene } from '../renderer/scene/render-scene'; import { Skybox } from '../renderer/scene/skybox'; +import { Node } from './node'; + +const _up = new Vec3(0, 1, 0); +const _v3 = new Vec3(); +const _qt = new Quat(); @ccclass('cc.AmbientInfo') export class AmbientInfo { @@ -44,7 +50,7 @@ export class AmbientInfo { @property({ type: Color }) set skyColor (val: Color) { - this._skyColor = val; + this._skyColor.set(val); if (this._resource) { color4.array(this._resource.skyColor, this.skyColor); } } get skyColor () { @@ -62,7 +68,7 @@ export class AmbientInfo { @property({ type: Color }) set groundAlbedo (val: Color) { - this._groundAlbedo = val; + this._groundAlbedo.set(val); if (this._resource) { color4.array(this._resource.groundAlbedo, this.groundAlbedo); } } get groundAlbedo () { @@ -80,7 +86,7 @@ cc.AmbientInfo = AmbientInfo; @ccclass('cc.SkyboxInfo') export class SkyboxInfo { - @property + @property(TextureCube) protected _cubemap: TextureCube | null = null; @property protected _isRGBE: boolean = false; @@ -125,16 +131,73 @@ export class SkyboxInfo { } cc.SkyboxInfo = SkyboxInfo; +@ccclass('cc.PlanarShadowInfo') +export class PlanarShadowInfo { + @property + protected _normal = new Vec3(0, 1, 0); + @property + protected _distance = 0; + @property + protected _shadowColor = new Color(76, 76, 76, 255); + + protected _resource: PlanarShadow | null = null; + + @property({ type: Vec3 }) + set normal (val: Vec3) { + vec3.copy(this._normal, val); + if (this._resource) { this._resource.normal = val; } + } + get normal () { + return this._normal; + } + + @property({ type: Number }) + set distance (val: number) { + this._distance = val; + if (this._resource) { this._resource.distance = val; } + } + get distance () { + return this._distance; + } + + @property({ type: Color }) + set shadowColor (val: Color) { + this._shadowColor.set(val); + if (this._resource) { this._resource.shadowColor = val; } + } + get shadowColor () { + return this._shadowColor; + } + + public setPlaneFromNode (node: Node) { + node.getWorldRotation(_qt); + this.normal = vec3.transformQuat(_v3, _up, _qt); + node.getWorldPosition(_v3); + this.distance = vec3.dot(this._normal, _v3); + } + + set renderScene (val: RenderScene) { + this._resource = val.planarShadow; + this.normal = this._normal; + this.distance = this._distance; + this.shadowColor = this._shadowColor; + } +} +cc.PlanarShadowInfo = PlanarShadowInfo; + @ccclass('cc.SceneGlobals') export class SceneGlobals { @property({ type: AmbientInfo }) public ambient = new AmbientInfo(); @property({ type: SkyboxInfo }) public skybox = new SkyboxInfo(); + @property({ type: PlanarShadowInfo }) + public planarShadow = new PlanarShadowInfo(); set renderScene (rs: RenderScene) { this.ambient.renderScene = rs; this.skybox.renderScene = rs; + this.planarShadow.renderScene = rs; } } cc.SceneGlobals = SceneGlobals; diff --git a/playground/planarShadow.html b/playground/planarShadow.html new file mode 100644 index 0000000000..da719b0a2b --- /dev/null +++ b/playground/planarShadow.html @@ -0,0 +1,118 @@ + + + + + planar shadow + + + + + + + + + + + + + +