From 033c115f13fc2ecd92de11a27e19793a1a22ab6a Mon Sep 17 00:00:00 2001 From: Yun Hsiao Wu Date: Tue, 15 Jan 2019 20:49:54 +0800 Subject: [PATCH] shader name & test scene --- cocos/3d/builtin/effects.js | 36 ++++++++--------- cocos/renderer/core/program-lib.ts | 25 ++++++------ playground/simple.html | 65 ++++++++++++++++++++---------- 3 files changed, 74 insertions(+), 52 deletions(-) diff --git a/cocos/3d/builtin/effects.js b/cocos/3d/builtin/effects.js index fc429b37ac..b3676546b9 100644 --- a/cocos/3d/builtin/effects.js +++ b/cocos/3d/builtin/effects.js @@ -1,29 +1,29 @@ export default [ { "name": "builtin-effect-unlit", - "techniques": [{"queue": "opaque", "lod": 100, "passes": [{"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"}}, "program": "185ba10e9dc3fd42752e6035d32ff7842c9e0e3cda549999b7ef5f68"}]}], + "techniques": [{"queue":"opaque", "lod":100, "passes":[{"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"}}, "program":"builtin-effect-unlit|unlit-vs:vert|unlit-fs:frag"}]}], "shaders": [ { - "name": "185ba10e9dc3fd42752e6035d32ff7842c9e0e3cda549999b7ef5f68", + "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": []} + {"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":"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} + {"name":"color", "type":16, "count":1, "size":16} ]} ], "samplers": [ - {"name": "mainTexture", "type": 29, "count": 1, "defines": ["USE_TEXTURE"], "binding": 2} + {"name":"mainTexture", "type":29, "count":1, "defines":["USE_TEXTURE"], "binding":2} ], "dependencies": {} } @@ -31,18 +31,18 @@ export default [ }, { "name": "builtin-effect-skybox", - "techniques": [{"queue": "opaque-1000", "lod": 200, "passes": [{"rasterizerState": {"cullMode": 0}, "depthStencilState": {"depthTest": false, "depthWrite": false}, "properties": {"cubeMap": {"type": 32, "value": "greyCube"}}, "program": "d9cd35729516d899db5cd6b6ecf025e61a484094405a391a3056591d"}]}], + "techniques": [{"queue":"opaque-1000", "lod":200, "passes":[{"rasterizerState":{"cullMode":0}, "depthStencilState":{"depthTest":false, "depthWrite":false}, "properties":{"cubeMap":{"type":32, "value":"greyCube"}}, "program":"builtin-effect-skybox|sky-vs|sky-fs"}]}], "shaders": [ { - "name": "d9cd35729516d899db5cd6b6ecf025e61a484094405a391a3056591d", - "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 rotView = mat4(mat3(cc_matView));\n vec4 clipPos = cc_matProj * rotView * vec4(a_position, 1.0);\n gl_Position = clipPos.xyww;\n viewDir = a_position;\n }\n", + "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", "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": []} + {"name":"USE_RGBE_CUBEMAP", "type":"boolean", "defines":[]} ], "blocks": [], "samplers": [ - {"name": "cubeMap", "type": 32, "count": 1, "defines": [], "binding": 0} + {"name":"cubeMap", "type":32, "count":1, "defines":[], "binding":0} ], "dependencies": {} } @@ -50,16 +50,16 @@ export default [ }, { "name": "builtin-effect-sprite", - "techniques": [{"queue": "overlay", "passes": [{"depthStencilState": {"depthTest": true, "depthWrite": false}, "blendState": {"targets": [{"blend": true, "blendSrc": 2, "blendDst": 4, "blendDstAlpha": 4}]}, "properties": {"mainTexture": {"type": 29, "value": "grey"}}, "program": "a86b95b64dc75bf2cc5b528318547813f86f5cd9e03ee48ca69556f6"}]}], + "techniques": [{"queue":"overlay", "passes":[{"depthStencilState":{"depthTest":true, "depthWrite":false}, "blendState":{"targets":[{"blend":true, "blendSrc":2, "blendDst":4, "blendDstAlpha":4}]}, "properties":{"mainTexture":{"type":29, "value":"grey"}}, "program":"builtin-effect-sprite|sprite-vs:vert|sprite-fs:frag"}]}], "shaders": [ { - "name": "a86b95b64dc75bf2cc5b528318547813f86f5cd9e03ee48ca69556f6", + "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", "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": [], "samplers": [ - {"name": "mainTexture", "type": 29, "count": 1, "defines": [], "binding": 0} + {"name":"mainTexture", "type":29, "count":1, "defines":[], "binding":0} ], "dependencies": {} } diff --git a/cocos/renderer/core/program-lib.ts b/cocos/renderer/core/program-lib.ts index 5541098bd1..d0f80131ca 100644 --- a/cocos/renderer/core/program-lib.ts +++ b/cocos/renderer/core/program-lib.ts @@ -1,11 +1,11 @@ // Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. -import { IDefineInfo, IShaderInfo, IBlockInfo, IBlockMember } from '../../3d/assets/effect-asset'; -import { GFXShaderType, GFXGetTypeSize } from '../../gfx/define'; +import { IBlockInfo, IBlockMember, IDefineInfo, IShaderInfo } from '../../3d/assets/effect-asset'; +import { GFXGetTypeSize, GFXShaderType } from '../../gfx/define'; import { GFXDevice } from '../../gfx/device'; -import { GFXShader, GFXUniformBlock, GFXUniform } from '../../gfx/shader'; -import { IDefineMap } from './effect'; +import { GFXShader, GFXUniform, GFXUniformBlock } from '../../gfx/shader'; import { UBOGlobal, UBOLocal } from '../../pipeline/render-pipeline'; +import { IDefineMap } from './effect'; function _generateDefines ( device: GFXDevice, @@ -128,8 +128,9 @@ export class ProgramLib { tmpl.blocks = tmpl.blocks.concat(convertToBlockInfo(UBOGlobal.BLOCK), convertToBlockInfo(UBOLocal.BLOCK)); + const instanceName = Object.keys(defines).reduce((acc, cur) => `${acc}|${cur}`, name); program = this._device.createShader({ - name, + name: instanceName, blocks: tmpl.blocks, samplers: tmpl.samplers, stages: [ @@ -142,17 +143,17 @@ export class ProgramLib { } } -function convertToUniformInfo(uniform: GFXUniform): IBlockMember { +function convertToUniformInfo (uniform: GFXUniform): IBlockMember { return { name: uniform.name, type: uniform.type, count: uniform.count, - size: GFXGetTypeSize(uniform.type) * uniform.count - } + size: GFXGetTypeSize(uniform.type) * uniform.count, + }; } -function convertToBlockInfo(block: GFXUniformBlock): IBlockInfo { - let members: IBlockMember[] = []; +function convertToBlockInfo (block: GFXUniformBlock): IBlockInfo { + const members: IBlockMember[] = []; let size: number = 0; for (let i = 0; i < block.members.length; i++) { members.push(convertToUniformInfo(block.members[i])); @@ -162,7 +163,7 @@ function convertToBlockInfo(block: GFXUniformBlock): IBlockInfo { name: block.name, binding: block.binding, defines: [], - members: members, - size: size + members, + size, }; } diff --git a/playground/simple.html b/playground/simple.html index adb6516f7d..689fefbf9f 100644 --- a/playground/simple.html +++ b/playground/simple.html @@ -44,38 +44,59 @@ textureAsset.image = new cc.ImageAsset(imgElms[0]); return textureAsset; } - + function getRandom() { return Math.random() * 128 + 127; } function onStart() { const root = cc.director.root; const scene = new cc.Scene(); const cameraNode = new cc.Node(); cameraNode.parent = scene; - cameraNode.setPosition(2, 2, 2); - cameraNode.lookAt(cc.v3()); + cameraNode.setPosition(4, 4, -2); + cameraNode.lookAt(cc.v3(0, 0, 20)); const camComp = cameraNode.addComponent(cc.CameraComponent); - const mainWindow = root.mainWindow; - const material = new cc.Material(); - material.setDefines({ USE_COLOR: true, USE_TEXTURE: true }); - material.effectName = 'builtin-effect-unlit'; // parsed effect file is embedded in cocos/3d/builtin/effects.js - material.setProperty('color', cc.color('#ffaaaa')); - material.setProperty('mainTexture', createTexture(cc.game.canvas.getElementsByTagName('img'))); - material.passes[0].update(); + const texture = createTexture(cc.game.canvas.getElementsByTagName('img')); - const modelNode = new cc.Node(); - modelNode.parent = scene; - const modelCom = modelNode.addComponent('cc.ModelComponent'); - modelCom.material = material; - modelCom.mesh = cc.utils.createMesh(cc.primitives.box()); + const len = 50; + const models = [], passes = []; + const color = cc.color() + for (let i = 0; i < len; i++) { + const material = new cc.Material(); + material.setDefines({ USE_COLOR: true, USE_TEXTURE: true }); + // parsed effect file is embedded in cocos/3d/builtin/effects.js + material.effectName = 'builtin-effect-unlit'; + material.setProperty('mainTexture', texture); + passes.push(material.passes[0]); - canv.addEventListener('mousemove', (e) => { - if (!(e.buttons & 1)) return; - const { clientX: x, clientY: y } = e; - const ray = camComp.screenPointToRay(x, canv.height - y); - const res = cc.director._scene._renderScene.raycast(ray); - if (res.length) res.forEach((r) => console.log(r)); - }) + const modelNode = new cc.Node(`${i}`); + modelNode.parent = scene; + const modelCom = modelNode.addComponent('cc.ModelComponent'); + modelCom.material = material; + modelCom.mesh = cc.utils.createMesh(cc.primitives.box()); + models.push(modelNode); + } + const handle = passes[0].getHandle('color'); + + let buttonDown = false, pos = cc.v2(); + canv.addEventListener('mousedown', () => buttonDown = true); + canv.addEventListener('mouseup', () => buttonDown = false); + canv.addEventListener('mousemove', (e) => (pos.x = e.x, pos.y = e.y)); + + cc.director.on(cc.Director.EVENT_AFTER_UPDATE, () => { + if (buttonDown) { + const ray = camComp.screenPointToRay(pos.x, canv.height - pos.y); + const res = cc.director._scene._renderScene.raycast(ray); + if (res.length) res.forEach((r) => console.log(r.node.name, r.distance)); + } + models.forEach((m, i) => { + const t = cc.director._totalFrames / 60; + const rad = i * 0.4; + const x = Math.cos(rad + t), y = Math.sin(rad + t); + m.setPosition(x * 2, y * 2, i * 2); + color.fromHSV((x + 1) * 0.5, 0.5, 1); + passes[i].setUniform(handle, color); + }); + }); cc.director.runSceneImmediate(scene); }