diff --git a/cocos/3d/assets/material.ts b/cocos/3d/assets/material.ts index 90ad28af0e..0517ad898f 100644 --- a/cocos/3d/assets/material.ts +++ b/cocos/3d/assets/material.ts @@ -120,14 +120,14 @@ export class Material extends Asset { * pass.setUniform should be used instead * if you need to do per-frame property update. */ - public setProperty (name: string, val: any, passIdx?: number) { + public setProperty (name: string, val: any, passIdx?: number, arrayIdx?: number) { let success = false; if (passIdx === undefined) { // set property for all applicable passes const passes = this._passes; const len = passes.length; for (let i = 0; i < len; i++) { const pass = passes[i]; - if (this._uploadProperty(pass, name, val)) { + if (this._uploadProperty(pass, name, val, arrayIdx)) { this._props[i][name] = val; success = true; } @@ -135,7 +135,7 @@ export class Material extends Asset { } else { if (passIdx >= this._passes.length) { console.warn(`illegal pass index: ${passIdx}.`); return; } const pass = this._passes[passIdx]; - if (this._uploadProperty(pass, name, val)) { + if (this._uploadProperty(pass, name, val, arrayIdx)) { this._props[passIdx][name] = val; success = true; } @@ -207,17 +207,17 @@ export class Material extends Asset { this.onPassChange(); } - protected _uploadProperty (pass: Pass, name: string, val: any) { + protected _uploadProperty (pass: Pass, name: string, val: any, arrayIdx?: number) { const handle = pass.getHandle(name); if (handle === undefined) { return false; } const bindingType = Pass.getBindingTypeFromHandle(handle); if (bindingType === GFXBindingType.UNIFORM_BUFFER) { - pass.setUniform(handle, val); + pass.setUniform(handle, val, arrayIdx); } else if (bindingType === GFXBindingType.SAMPLER) { const textureView = val instanceof GFXTextureView ? val : val.getGFXTextureView(); if (!textureView) { console.warn('texture asset incomplete!'); return false; } const binding = Pass.getBindingFromHandle(handle); - pass.bindTextureView(binding, textureView); + pass.bindTextureView(binding, textureView, arrayIdx); } return true; } diff --git a/cocos/3d/builtin/effects.js b/cocos/3d/builtin/effects.js index 83ef3ff9a7..0bacfd6b65 100644 --- a/cocos/3d/builtin/effects.js +++ b/cocos/3d/builtin/effects.js @@ -1,41 +1,12 @@ +// absolute essential effects export default [ - { - "name": "builtin-effect-unlit", - "techniques": [{"passes":[{"program":"builtin-effect-unlit|unlit-vs:vert|unlit-fs:frag", "properties":{"color":{"type":17, "value":[1, 1, 1, 1]}, "mainTiling":{"type":14, "value":[1, 1]}, "mainOffset":{"type":14, "value":[0, 0]}, "mainTexture":{"type":29, "value":"grey"}}}]}], - "shaders": [ - { - "name": "builtin-effect-unlit|unlit-vs:vert|unlit-fs:frag", - "vert": "\nattribute vec3 a_position;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform vec4 cc_cameraSize;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection;\n uniform vec4 cc_dirLightColor;\n uniform vec4 cc_pointLightPositionAndRange;\n uniform vec4 cc_pointLightColor;\n uniform vec4 cc_spotLightPositionAndRange;\n uniform vec4 cc_spotLightDirection;\n uniform vec4 cc_spotLightColor;\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\n#if USE_TEXTURE\n attribute vec2 a_texCoord;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n varying vec2 uv0;\n#endif\n#if CC_USE_SKINNING\n \nattribute vec4 a_weights;\nattribute vec4 a_joints;\n uniform mat4 cc_matJoint;\n uniform 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\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 * mainTiling + mainOffset;\n #endif\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", - "frag": "\n#if USE_TEXTURE\n uniform sampler2D mainTexture;\n varying vec2 uv0;\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", - "defines": [ - {"name":"USE_TEXTURE", "type":"boolean", "defines":[]}, - {"name":"CC_USE_SKINNING", "type":"boolean", "defines":[]}, - {"name":"CC_USE_JOINTS_TEXTURE", "type":"boolean", "defines":["CC_USE_SKINNING"]}, - {"name":"USE_COLOR", "type":"boolean", "defines":[]} - ], - "blocks": [ - {"name": "TexCoords", "size": 16, "defines": ["USE_TEXTURE"], "binding": 0, "members": [ - {"name":"mainTiling", "type":14, "count":1, "size":8}, - {"name":"mainOffset", "type":14, "count":1, "size":8} - ]}, - {"name": "Constant", "size": 16, "defines": ["USE_COLOR"], "binding": 1, "members": [ - {"name":"color", "type":16, "count":1, "size":16} - ]} - ], - "samplers": [ - {"name":"mainTexture", "type":29, "count":1, "defines":["USE_TEXTURE"], "binding":2} - ], - "dependencies": {} - } - ] - }, { "name": "builtin-effect-skybox", "techniques": [{"passes":[{"rasterizerState":{"cullMode":0}, "depthStencilState":{"depthTest":false, "depthWrite":false}, "program":"builtin-effect-skybox|sky-vs|sky-fs", "properties":{"cubeMap":{"type":32, "value":"white-cube"}}, "priority":245}]}], "shaders": [ { "name": "builtin-effect-skybox|sky-vs|sky-fs", - "vert": "\n attribute vec3 a_position;\n \n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform vec4 cc_cameraSize;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection;\n uniform vec4 cc_dirLightColor;\n uniform vec4 cc_pointLightPositionAndRange;\n uniform vec4 cc_pointLightColor;\n uniform vec4 cc_spotLightPositionAndRange;\n uniform vec4 cc_spotLightDirection;\n uniform vec4 cc_spotLightColor;\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n varying vec3 viewDir;\n void main() {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n gl_Position = clipPos.xyww;\n viewDir = a_position;\n }\n", + "vert": "\n attribute vec3 a_position;\n \n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform vec4 cc_cameraSize;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n varying vec3 viewDir;\n void main() {\n mat4 matViewRotOnly = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * matViewRotOnly * vec4(a_position, 1.0);\n gl_Position = clipPos.xyww;\n viewDir = a_position;\n }\n", "frag": "\n varying vec3 viewDir;\n uniform samplerCube cubeMap;\n \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}\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}\n void main() {\n #if USE_RGBE_CUBEMAP\n vec3 c = unpackRGBE(textureCube(cubeMap, viewDir));\n c = linearToGammaSpaceRGB(c / (1.0 + c));\n gl_FragColor = vec4(c, 1.0);\n #else\n gl_FragColor = textureCube(cubeMap, viewDir);\n #endif\n }\n", "defines": [ {"name":"USE_RGBE_CUBEMAP", "type":"boolean", "defines":[]} @@ -54,7 +25,7 @@ export default [ "shaders": [ { "name": "builtin-effect-sprite|sprite-vs:vert|sprite-fs:frag", - "vert": "\nattribute vec3 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform vec4 cc_cameraSize;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection;\n uniform vec4 cc_dirLightColor;\n uniform vec4 cc_pointLightPositionAndRange;\n uniform vec4 cc_pointLightColor;\n uniform vec4 cc_spotLightPositionAndRange;\n uniform vec4 cc_spotLightDirection;\n uniform vec4 cc_spotLightColor;\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\nvarying vec2 uv0;\nvarying vec4 color;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matViewProj * cc_matWorld * pos;\n uv0 = a_texCoord;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", + "vert": "\nattribute vec3 a_position;\nattribute vec2 a_texCoord;\nattribute vec4 a_color;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform vec4 cc_cameraSize;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\nvarying vec2 uv0;\nvarying vec4 color;\nvec4 vert () {\n vec4 pos = vec4(a_position, 1);\n pos = cc_matViewProj * cc_matWorld * pos;\n uv0 = a_texCoord;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", "frag": "\nuniform sampler2D mainTexture;\nvarying vec2 uv0;\nvarying vec4 color;\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", "defines": [], "blocks": [], @@ -66,37 +37,33 @@ export default [ ] }, { - "name": "builtin-effect-particle-add", - "techniques": [{"passes":[{"rasterizerState":{"cullMode":0}, "depthStencilState":{"depthTest":true, "depthWrite":false}, "blendState":{"targets":[{"blend":true, "blendSrc":2, "blendDst":1, "blendSrcAlpha":2, "blendDstAlpha":1}]}, "program":"builtin-effect-particle-add|particle-vs-legacy:lpvs_main|particle-add-fs", "properties":{"mainTexture":{"type":29, "value":"grey"}, "mainTiling":{"type":14, "value":[1, 1]}, "mainOffset":{"type":14, "value":[0, 0]}, "frameTile":{"type":14, "value":[1, 1]}, "velocityScale":{"type":13, "value":[0]}, "lengthScale":{"type":13, "value":[0]}, "tintColor":{"type":17, "value":[0.5, 0.5, 0.5, 0.5]}}}]}], + "name": "builtin-effect-unlit", + "techniques": [{"passes":[{"program":"builtin-effect-unlit|unlit-vs:vert|unlit-fs:frag", "properties":{"color":{"type":17, "value":[1, 1, 1, 1]}, "mainTiling":{"type":14, "value":[1, 1]}, "mainOffset":{"type":14, "value":[0, 0]}, "mainTexture":{"type":29, "value":"grey"}}}]}], "shaders": [ { - "name": "builtin-effect-particle-add|particle-vs-legacy:lpvs_main|particle-add-fs", - "vert": "\n uniform vec2 frameTile;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n uniform float velocityScale;\n uniform float lengthScale;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform vec4 cc_cameraSize;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection;\n uniform vec4 cc_dirLightColor;\n uniform vec4 cc_pointLightPositionAndRange;\n uniform vec4 cc_pointLightColor;\n uniform vec4 cc_spotLightPositionAndRange;\n uniform vec4 cc_spotLightDirection;\n uniform vec4 cc_spotLightColor;\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform 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_uv; \nattribute vec2 a_uv0; \nattribute vec4 a_color;\n#if USE_STRETCHED_BILLBOARD\nattribute vec3 a_color0; \n#endif\nvec4 lpvs_main() {\n vec4 pos = vec4(a_position, 1);\n#if USE_STRETCHED_BILLBOARD\n vec4 velocity = vec4(a_color0.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_uv.xy - 0.5) * a_uv0.x);\n#if !USE_STRETCHED_BILLBOARD\n \n rotateCorner(cornerOffset, a_uv0.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\n , velocity\n , velocityScale\n , lengthScale\n , a_uv0.x\n , a_uv.x\n #endif\n );\n pos = cc_matViewProj * pos;\n uv = computeUV(a_uv.z, a_uv.xy, frameTile) * mainTiling + mainOffset;\n color = a_color;\n return pos;\n}\nvoid main() { gl_Position = lpvs_main(); }\n", - "frag": "\n uniform sampler2D mainTexture;\n uniform vec4 tintColor;\n varying vec2 uv;\n varying vec4 color;\n void main () {\n \n gl_FragColor = 2.0 * color * tintColor * texture2D(mainTexture, uv);\n }\n", + "name": "builtin-effect-unlit|unlit-vs:vert|unlit-fs:frag", + "vert": "\nattribute vec3 a_position;\n uniform mat4 cc_matProj;\n uniform mat4 cc_matProjInv;\n uniform mat4 cc_matView;\n uniform mat4 cc_matViewInv;\n uniform mat4 cc_matViewProj;\n uniform mat4 cc_matViewProjInv;\n uniform vec4 cc_time;\n uniform vec4 cc_screenSize;\n uniform vec4 cc_screenScale;\n uniform vec4 cc_cameraSize;\n uniform vec4 cc_cameraPos;\n uniform vec4 cc_dirLightDirection[4];\n uniform vec4 cc_dirLightColor[4];\n uniform vec4 cc_pointLightPositionAndRange[4];\n uniform vec4 cc_pointLightColor[4];\n uniform vec4 cc_spotLightPositionAndRange[4];\n uniform vec4 cc_spotLightDirection[4];\n uniform vec4 cc_spotLightColor[4];\n uniform mat4 cc_matViewProjLight;\n uniform vec4 cc_shadowParam1;\n uniform vec4 cc_shadowParam2;\n uniform mat4 cc_matWorld;\n uniform mat4 cc_matWorldIT;\n#if USE_TEXTURE\n attribute vec2 a_texCoord;\n uniform vec2 mainTiling;\n uniform vec2 mainOffset;\n varying vec2 uv0;\n#endif\n#if CC_USE_SKINNING\n \nattribute vec4 a_weights;\nattribute vec4 a_joints;\n uniform mat4 cc_matJoint[128];\n uniform 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\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 * mainTiling + mainOffset;\n #endif\n return pos;\n}\nvoid main() { gl_Position = vert(); }\n", + "frag": "\n#if USE_TEXTURE\n uniform sampler2D mainTexture;\n varying vec2 uv0;\n#endif\n#if USE_COLOR\n uniform vec4 color[2];\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[0] + color[1];\n #endif\n return o;\n}\nvoid main() { gl_FragColor = frag(); }\n", "defines": [ - {"name":"USE_BILLBOARD", "type":"boolean", "defines":[]}, - {"name":"USE_STRETCHED_BILLBOARD", "type":"boolean", "defines":[]}, - {"name":"USE_HORIZONTAL_BILLBOARD", "type":"boolean", "defines":[]}, - {"name":"USE_VERTICAL_BILLBOARD", "type":"boolean", "defines":[]}, - {"name":"USE_WORLD_SPACE", "type":"boolean", "defines":[]} + {"name":"USE_TEXTURE", "type":"boolean", "defines":[]}, + {"name":"CC_USE_SKINNING", "type":"boolean", "defines":[]}, + {"name":"CC_USE_JOINTS_TEXTURE", "type":"boolean", "defines":["CC_USE_SKINNING"]}, + {"name":"USE_COLOR", "type":"boolean", "defines":[]} ], "blocks": [ - {"name": "Constants", "size": 32, "defines": [], "binding": 0, "members": [ - {"name":"frameTile", "type":14, "count":1, "size":8}, + {"name": "TexCoords", "size": 16, "defines": ["USE_TEXTURE"], "binding": 0, "members": [ {"name":"mainTiling", "type":14, "count":1, "size":8}, - {"name":"mainOffset", "type":14, "count":1, "size":8}, - {"name":"velocityScale", "type":13, "count":1, "size":4}, - {"name":"lengthScale", "type":13, "count":1, "size":4} + {"name":"mainOffset", "type":14, "count":1, "size":8} ]}, - {"name": "FragConstants", "size": 16, "defines": [], "binding": 1, "members": [ - {"name":"tintColor", "type":16, "count":1, "size":16} + {"name": "Constant", "size": 32, "defines": ["USE_COLOR"], "binding": 1, "members": [ + {"name":"color", "type":16, "count":2, "size":32} ]} ], "samplers": [ - {"name":"mainTexture", "type":29, "count":1, "defines":[], "binding":2} + {"name":"mainTexture", "type":29, "count":1, "defines":["USE_TEXTURE"], "binding":2} ], "dependencies": {} } ] - }, + } ]; diff --git a/cocos/3d/framework/model-component.ts b/cocos/3d/framework/model-component.ts index 506c278e90..adfb368fbe 100644 --- a/cocos/3d/framework/model-component.ts +++ b/cocos/3d/framework/model-component.ts @@ -169,9 +169,6 @@ export class ModelComponent extends RenderableComponent { this._updateModels(); this._updateCastShadow(); this._updateReceiveShadow(); - if (this._model) { - this._model.enabled = true; - } } public onDisable () { @@ -196,6 +193,10 @@ export class ModelComponent extends RenderableComponent { this._model = null; } + if (this._model) { + this._model.enabled = this.enabledInHierarchy; + } + this._model = this._createModel(); this._model!.createBoundingShape(this._mesh.minPosition, this._mesh.maxPosition); diff --git a/cocos/core/director.js b/cocos/core/director.js index 3eec6f45d7..eb6e6e56d1 100644 --- a/cocos/core/director.js +++ b/cocos/core/director.js @@ -925,7 +925,7 @@ class Director extends EventTarget { this.emit(cc.Director.EVENT_AFTER_DRAW); eventManager.frameUpdateListeners(); - this._scene.resetHasChanged(); + if (this._scene) { this._scene.resetHasChanged(); } this._totalFrames++; } } diff --git a/cocos/core/vmath/color4.ts b/cocos/core/vmath/color4.ts index 13e1007529..e177ec8714 100644 --- a/cocos/core/vmath/color4.ts +++ b/cocos/core/vmath/color4.ts @@ -222,12 +222,12 @@ class color4 { * @param a - The color. * @return out. */ - public static array> (out: Out, a: color4) { + public static array> (out: Out, a: color4, ofs = 0) { const scale = (a instanceof cc.Color || a.a > 1) ? 1 / 255 : 1; - out[0] = a.r * scale; - out[1] = a.g * scale; - out[2] = a.b * scale; - out[3] = a.a * scale; + out[ofs + 0] = a.r * scale; + out[ofs + 1] = a.g * scale; + out[ofs + 2] = a.b * scale; + out[ofs + 3] = a.a * scale; return out; } diff --git a/cocos/core/vmath/mat2.ts b/cocos/core/vmath/mat2.ts index 1637a2d533..4df9217f45 100644 --- a/cocos/core/vmath/mat2.ts +++ b/cocos/core/vmath/mat2.ts @@ -271,11 +271,11 @@ class mat2 { * @param m - The matrix. * @return out. */ - public static array(out, m) { - out[0] = m.m00; - out[1] = m.m01; - out[2] = m.m02; - out[3] = m.m03; + public static array(out, m, ofs = 0) { + out[ofs + 0] = m.m00; + out[ofs + 1] = m.m01; + out[ofs + 2] = m.m02; + out[ofs + 3] = m.m03; return out; } diff --git a/cocos/core/vmath/mat3.ts b/cocos/core/vmath/mat3.ts index 20d0eb2c4f..d2934883a4 100644 --- a/cocos/core/vmath/mat3.ts +++ b/cocos/core/vmath/mat3.ts @@ -625,16 +625,16 @@ class mat3 { * @param m - The matrix. * @return out. */ - public static array (out, m) { - out[0] = m.m00; - out[1] = m.m01; - out[2] = m.m02; - out[3] = m.m03; - out[4] = m.m04; - out[5] = m.m05; - out[6] = m.m06; - out[7] = m.m07; - out[8] = m.m08; + public static array (out, m, ofs = 0) { + out[ofs + 0] = m.m00; + out[ofs + 1] = m.m01; + out[ofs + 2] = m.m02; + out[ofs + 3] = m.m03; + out[ofs + 4] = m.m04; + out[ofs + 5] = m.m05; + out[ofs + 6] = m.m06; + out[ofs + 7] = m.m07; + out[ofs + 8] = m.m08; return out; } diff --git a/cocos/core/vmath/mat4.ts b/cocos/core/vmath/mat4.ts index c200b7aa9e..3890527d26 100644 --- a/cocos/core/vmath/mat4.ts +++ b/cocos/core/vmath/mat4.ts @@ -1460,23 +1460,23 @@ class mat4 { * @param m - The matrix. * @return out. */ - public static array (out, m) { - out[0] = m.m00; - out[1] = m.m01; - out[2] = m.m02; - out[3] = m.m03; - out[4] = m.m04; - out[5] = m.m05; - out[6] = m.m06; - out[7] = m.m07; - out[8] = m.m08; - out[9] = m.m09; - out[10] = m.m10; - out[11] = m.m11; - out[12] = m.m12; - out[13] = m.m13; - out[14] = m.m14; - out[15] = m.m15; + public static array (out, m, ofs = 0) { + out[ofs + 0] = m.m00; + out[ofs + 1] = m.m01; + out[ofs + 2] = m.m02; + out[ofs + 3] = m.m03; + out[ofs + 4] = m.m04; + out[ofs + 5] = m.m05; + out[ofs + 6] = m.m06; + out[ofs + 7] = m.m07; + out[ofs + 8] = m.m08; + out[ofs + 9] = m.m09; + out[ofs + 10] = m.m10; + out[ofs + 11] = m.m11; + out[ofs + 12] = m.m12; + out[ofs + 13] = m.m13; + out[ofs + 14] = m.m14; + out[ofs + 15] = m.m15; return out; } diff --git a/cocos/core/vmath/vec2.ts b/cocos/core/vmath/vec2.ts index 80b54bc8e7..ffe65ac5cb 100644 --- a/cocos/core/vmath/vec2.ts +++ b/cocos/core/vmath/vec2.ts @@ -545,9 +545,9 @@ export default class vec2 { * @param v - The vector. * @return out. */ - public static array> (out: Out, v: vec2) { - out[0] = v.x; - out[1] = v.y; + public static array> (out: Out, v: vec2, ofs = 0) { + out[ofs + 0] = v.x; + out[ofs + 1] = v.y; return out; } diff --git a/cocos/core/vmath/vec3.ts b/cocos/core/vmath/vec3.ts index 310aeccad8..4e7e320504 100644 --- a/cocos/core/vmath/vec3.ts +++ b/cocos/core/vmath/vec3.ts @@ -737,10 +737,10 @@ export default class vec3 { * @param v - The vector. * @return out. */ - public static array> (out: Out, v: vec3) { - out[0] = v.x; - out[1] = v.y; - out[2] = v.z; + public static array> (out: Out, v: vec3, ofs = 0) { + out[ofs + 0] = v.x; + out[ofs + 1] = v.y; + out[ofs + 2] = v.z; return out; } diff --git a/cocos/core/vmath/vec4.ts b/cocos/core/vmath/vec4.ts index 7eeb1c22f5..c941721d64 100644 --- a/cocos/core/vmath/vec4.ts +++ b/cocos/core/vmath/vec4.ts @@ -570,11 +570,11 @@ export default class vec4 { * @param v - The vector. * @return out. */ - public static array> (out: Out, v: vec4) { - out[0] = v.x; - out[1] = v.y; - out[2] = v.z; - out[3] = v.w; + public static array> (out: Out, v: vec4, ofs = 0) { + out[ofs + 0] = v.x; + out[ofs + 1] = v.y; + out[ofs + 2] = v.z; + out[ofs + 3] = v.w; return out; } diff --git a/cocos/renderer/core/pass.ts b/cocos/renderer/core/pass.ts index 22f284e785..e26f2fac38 100644 --- a/cocos/renderer/core/pass.ts +++ b/cocos/renderer/core/pass.ts @@ -36,18 +36,18 @@ type RecursivePartial = { export type PassOverrides = RecursivePartial; const _type2fn = { - [GFXType.INT]: (a: Float32Array, v: any) => a[0] = v, - [GFXType.INT2]: (a: Float32Array, v: any) => vec2.array(a, v), - [GFXType.INT3]: (a: Float32Array, v: any) => vec3.array(a, v), - [GFXType.INT4]: (a: Float32Array, v: any) => vec4.array(a, v), - [GFXType.FLOAT]: (a: Float32Array, v: any) => a[0] = v, - [GFXType.FLOAT2]: (a: Float32Array, v: any) => vec2.array(a, v), - [GFXType.FLOAT3]: (a: Float32Array, v: any) => vec3.array(a, v), - [GFXType.FLOAT4]: (a: Float32Array, v: any) => vec4.array(a, v), - [GFXType.COLOR4]: (a: Float32Array, v: any) => color4.array(a, v), - [GFXType.MAT2]: (a: Float32Array, v: any) => mat2.array(a, v), - [GFXType.MAT3]: (a: Float32Array, v: any) => mat3.array(a, v), - [GFXType.MAT4]: (a: Float32Array, v: any) => mat4.array(a, v), + [GFXType.INT]: (a: Float32Array, v: any, idx: number = 0) => a[idx] = v, + [GFXType.INT2]: (a: Float32Array, v: any, idx: number = 0) => vec2.array(a, v, idx * 2), + [GFXType.INT3]: (a: Float32Array, v: any, idx: number = 0) => vec3.array(a, v, idx * 3), + [GFXType.INT4]: (a: Float32Array, v: any, idx: number = 0) => vec4.array(a, v, idx * 4), + [GFXType.FLOAT]: (a: Float32Array, v: any, idx: number = 0) => a[idx] = v, + [GFXType.FLOAT2]: (a: Float32Array, v: any, idx: number = 0) => vec2.array(a, v, idx * 2), + [GFXType.FLOAT3]: (a: Float32Array, v: any, idx: number = 0) => vec3.array(a, v, idx * 3), + [GFXType.FLOAT4]: (a: Float32Array, v: any, idx: number = 0) => vec4.array(a, v, idx * 4), + [GFXType.COLOR4]: (a: Float32Array, v: any, idx: number = 0) => color4.array(a, v, idx * 4), + [GFXType.MAT2]: (a: Float32Array, v: any, idx: number = 0) => mat2.array(a, v, idx * 4), + [GFXType.MAT3]: (a: Float32Array, v: any, idx: number = 0) => mat3.array(a, v, idx * 9), + [GFXType.MAT4]: (a: Float32Array, v: any, idx: number = 0) => mat4.array(a, v, idx * 16), }; const _type2default = { @@ -206,16 +206,16 @@ export class Pass { return Pass.getBindingFromHandle(this.getHandle(name)); } - public setUniform (handle: number, value: any) { + public setUniform (handle: number, value: any, arrayIdx?: number) { const binding = Pass.getBindingFromHandle(handle); const type = Pass.getTypeFromHandle(handle); const idx = Pass.getIndexFromHandle(handle); const block = this._blocks[binding]; - _type2fn[type](block.views[idx], value); + _type2fn[type](block.views[idx], value, arrayIdx); block.dirty = true; } - public bindTextureView (binding: number, value: GFXTextureView) { + public bindTextureView (binding: number, value: GFXTextureView, arrayIdx?: number) { this._textureViews[binding] = value; for (const res of this._resources) { res.bindingLayout.bindTextureView(binding, value); diff --git a/playground/simple.html b/playground/simple.html index 7b6edcfc33..aaf4842886 100644 --- a/playground/simple.html +++ b/playground/simple.html @@ -64,6 +64,8 @@ material.setDefines({ USE_COLOR: true }); // parsed effect file is embedded in cocos/3d/builtin/effects.js material.effectName = 'builtin-effect-unlit'; + material.setProperty('color', cc.Color.RED, 0, 0); + material.setProperty('color', cc.Color.BLUE, 0, 1); const len = 50; const models = [], passes = []; @@ -115,8 +117,8 @@ const x = Math.cos(rad + t), y = Math.sin(rad + t); let z = m._lpos.z + dt; if (z > len * 2) z = 0; m.setPosition(x * 2, y * 2, z); - color.fromHSV((x + 1) * 0.5, 0.5, 1); - passes[i].setUniform(handle, color); + // color.fromHSV((x + 1) * 0.5, 0.5, 1); + // passes[i].setUniform(handle, color); }); });