From 70bbf741a40b821e06f6f9ea1d86cedd84bd2feb Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Tue, 23 Oct 2018 14:30:11 +0800 Subject: [PATCH] raw version of serialize of deserialize of material (#3406) * raw version of serialize of deserialize of material * adjust class for material serialization generation * add registerEffect function --- cocos/3d/assets/effect.js | 108 -------------- cocos/3d/assets/index.js | 3 +- cocos/3d/assets/material.js | 239 ++++++++++++----------------- cocos/3d/builtin/init.js | 28 ++-- cocos/renderer/core/effect.js | 272 +++++++++++++++++++++++++--------- 5 files changed, 310 insertions(+), 340 deletions(-) delete mode 100644 cocos/3d/assets/effect.js diff --git a/cocos/3d/assets/effect.js b/cocos/3d/assets/effect.js deleted file mode 100644 index f32dc55a2b..0000000000 --- a/cocos/3d/assets/effect.js +++ /dev/null @@ -1,108 +0,0 @@ -/**************************************************************************** - Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. - - http://www.cocos.com - - Permission is hereby granted, free of charge, to any person obtaining a copy - of this software and associated engine source code (the "Software"), a limited, - worldwide, royalty-free, non-assignable, revocable and non-exclusive license - to use Cocos Creator solely to develop games on your target platforms. You shall - not use Cocos Creator software for developing other software or tools that's - used for developing games. You are not granted to publish, distribute, - sublicense, and/or sell copies of Cocos Creator. - - The software or tools in this License Agreement are licensed, not sold. - Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you. - - THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - THE SOFTWARE. - ****************************************************************************/ -// @ts-check -import Asset from "../../assets/CCAsset"; - -export default class Effect extends Asset { - /** - * @type {Object[]} - */ - _techniques = []; - /** - * @type {Object} - */ - _properties = {}; - /** - * @type {Object[]} - */ - _defines = []; - /** - * @type {Object[]} - */ - _dependencies = []; - - /** - * @param {Object[]} - */ - set techniques(val) { - this._techniques = val; - } - - /** - * @return {Object[]} - */ - get techniques() { - return this._techniques; - } - - /** - * @param {Object} val - */ - set properties(val) { - this._properties = val; - } - - /** - * @return {Object} - */ - get properties() { - return this._properties; - } - - /** - * @param {Object[]}} - */ - set defines(val) { - this._defines = val; - } - - /** - * @return {Object[]}} - */ - get defines() { - return this._defines; - } - - /** - * @param {Object[]} val - */ - set dependencies(val) { - this._dependencies = val; - } - - /** - * @return {Object[]} - */ - get dependencies() { - return this._dependencies; - } - - destroy() { - // TODO: what should we do here ??? - return super.destroy(); - } -} - -cc.Effect = Effect; diff --git a/cocos/3d/assets/index.js b/cocos/3d/assets/index.js index 2ab8f11573..bab2bbae25 100644 --- a/cocos/3d/assets/index.js +++ b/cocos/3d/assets/index.js @@ -26,7 +26,6 @@ import "./animation-clip"; import "./audio-clip"; import "./dom-audio-clip"; -import "./effect"; import "./bitmap-font"; import "./material"; import "./mesh"; @@ -35,4 +34,4 @@ import "./skeleton"; import "./sprite"; import "./web-audio-clip"; import "./wx-game-audio-clip"; -import "./utils/gltf-utils.js"; \ No newline at end of file +import "./utils/gltf-utils.js"; diff --git a/cocos/3d/assets/material.js b/cocos/3d/assets/material.js index 93a1c67c44..0e8b7ff4ff 100644 --- a/cocos/3d/assets/material.js +++ b/cocos/3d/assets/material.js @@ -29,177 +29,120 @@ import Asset from "../../assets/CCAsset"; import Texture from '../../assets/CCTexture2D'; import renderer from "../../renderer"; import { vec2, vec3, vec4, color3, color4 } from '../../core/vmath'; +import Effect from '../../renderer/core/effect'; function _objArrayClone(val) { - return val.map(obj => Object.assign({}, obj)); + return val.map(obj => Object.assign({}, obj)); } -@ccclass +@ccclass('cc.Material') export default class Material extends Asset { - /** - * @type {cc.Effect} - */ - @property(cc.Effect) - _effect = null; + /** + * @type {cc.Effect} + */ + @property(cc.Effect) + _effect = null; - /** - * @type {Object} - */ - @property - _props = {}; + /** + * @type {Object} + */ + @property + _props = {}; - /** - * @type {cc.renderer.Effect} - */ - _effecInst = null; + /** + * @type {cc.renderer.Effect} + */ + _effecInst = null; - _updateEffectInst() { - let techNum = this._effect.techniques.length; - /** @type {cc.renderer.Technique[]} */ - let techniques = new Array(techNum); - let props = {}; + /** + * @return {Effect} + */ + get effect() { + return this._effect; + } - for (let j = 0; j < techNum; ++j) { - let tech = this._effect.techniques[j]; - - for (let k = 0; k < tech.params.length; ++k) { - let param = tech.params[k]; - switch (param.type) { - case renderer.PARAM_FLOAT: - props[param.name] = param.value; - break; - case renderer.PARAM_FLOAT2: - props[param.name] = vec2.create(param.value[0], param.value[1]); - break; - case renderer.PARAM_FLOAT3: - props[param.name] = vec3.create(param.value[0], param.value[1], param.value[2]); - break; - case renderer.PARAM_FLOAT4: - props[param.name] = vec4.create(param.value[0], param.value[1], param.value[2], param.value[3]); - break; - case renderer.PARAM_COLOR3: - props[param.name] = color3.create(param.value[0], param.value[1], param.value[2]); - break; - case renderer.PARAM_COLOR4: - props[param.name] = color4.create(param.value[0], param.value[1], param.value[2], param.value[3]); - break; - case renderer.PARAM_TEXTURE_2D: - case renderer.PARAM_TEXTURE_CUBE: - props[param.name] = null; - break; - default: - console.warn('unsupport param type in effect json.'); + /** + * @param {Effect} val + */ + set effect(val) { + if (this._effect !== val) { + this._effect = val; + this._effectInst = Effect.parseEffect(this._effect); + let propCls = cc.js.getClassByName(Effect.getPropertyClassName(this._effect)); + this._props = propCls ? new propCls() : {} } - } + } - let passNum = tech.passes.length; - /** @type {cc.renderer.Pass[]} */ - let passes = new Array(passNum); - for (let k = 0; k < passNum; ++k) { - let pass = tech.passes[k]; - passes[k] = new renderer.Pass(pass.program); - if (pass.depthTest !== undefined && pass.depthWrite !== undefined) { - passes[k].setDepth(pass.depthTest, pass.depthWrite); + /** + * @return {cc.renderer.Effect} + */ + get effectInst() { + return this._effectInst; + } + + /** + * + * @param {Material} mat + */ + copy(mat) { + if (this._effect !== mat._effect) { + this._effect = mat._effect; + this._effecInst = Effect.parseEffect(this._effect); } - if (pass.cullMode !== undefined) { - passes[k].setCullMode(pass.cullMode); + + for (let name in mat._props) { + this.setProperty(name, mat._props[name]); } - if (pass.blend === true) { - passes[k].setBlend(pass.blendEq, pass.blendSrc, pass.blendDst, pass.blendAlphaEq, pass.blendSrcAlpha, pass.blendDstAlpha); + } + + /** + * + * @param {string} name + * @param {*} val + */ + setProperty(name, val) { + this._props[name] = val; + + if (val instanceof Texture) { + this._effectInst.setProperty(name, val._texture); + } else { + this._effectInst.setProperty(name, val); } - } - - techniques[j] = new renderer.Technique(tech.stages, tech.params, passes, tech.layer); } - for (let name in props) { - this._props[name] = props[name]; + /** + * + * @param {string} name + * @param {*} val + */ + define(name, val) { + this._effectInst.define(name, val); } - // reset material's props - let defs = _objArrayClone(this._effect.defines); - let deps = _objArrayClone(this._effect.dependencies); - - if (this._effectInst) { - this._effectInst.clear(); - this._effectInst._techniques = techniques; - this._effectInst._properties = props; - this._effectInst._defines = defs; - this._effectInst._dependencies = deps; - } else { - this._effectInst = new renderer.Effect(techniques, props, defs, deps); - } - } - - /** - * @return {Effect} - */ - get effect() { - return this._effect; - } - - /** - * @param {Effect} val - */ - set effect(val) { - if (this._effect !== val) { - this._effect = val; - this._updateEffectInst(); - } - } - - /** - * @return {cc.renderer.Effect} - */ - get effectInst() { - return this._effectInst; - } - - /** - * - * @param {Material} mat - */ - copy(mat) { - if (this._effect !== mat._effect) { - this._effect = mat._effect; - this._updateEffectInst(); + destroy() { + // TODO: what should we do here ??? + return super.destroy(); } - this._effectInst._defines = _objArrayClone(mat._effectInst._defines); - this._effectInst._dependencies = _objArrayClone(mat._effectInst._dependencies); - for (let name in mat._props) { - this.setProperty(name, mat._props[name]); + _serialize() { + // todo: this may be refactored for it is editor only + return { + effect: (this._effect && this._effect._uuid) || 'unknown', + props: Editor.serialize(this._props), + }; } - } - /** - * - * @param {string} name - * @param {*} val - */ - setProperty(name, val) { - this._props[name] = val; - - if (val instanceof Texture) { - this._effectInst.setProperty(name, val._texture); - } else { - this._effectInst.setProperty(name, val); + _deserialize(data, handle) { + console.log(`try to deserialize material`); + if (data.effect.indexOf('builtin-effect') !== -1) { + this._effect = cc.game._builtins[data.effect]; + this._effecInst = Effect.parseEffect(this._effect); + this._props = cc.deserialize(data.props); + } else { + // todo: add custom effect + console.error('Cannot support custom effect now') + } } - } - - /** - * - * @param {string} name - * @param {*} val - */ - define(name, val) { - this._effectInst.define(name, val); - } - - destroy() { - // TODO: what should we do here ??? - return super.destroy(); - } static getInstantiatedMaterial(mat, rndCom) { if (mat._owner === rndCom) { diff --git a/cocos/3d/builtin/init.js b/cocos/3d/builtin/init.js index 48060dd0eb..78d5fd249f 100644 --- a/cocos/3d/builtin/init.js +++ b/cocos/3d/builtin/init.js @@ -5,7 +5,7 @@ import Mesh from '../assets/mesh'; import Material from '../assets/material'; import Texture2D from '../../assets/CCTexture2D'; // import TextureCube from '../assets/texture-cube'; -import Effect from '../assets/effect'; +import Effect from '../../renderer/core/effect'; // import Technique from '../../renderer/core/technique'; // import Sprite from '../assets/sprite'; import { vec3 } from '../../core/vmath'; @@ -176,15 +176,17 @@ export default function (device) { let effects = {}; for (let i = 0; i < effectJsons.length; ++i) { let effectJson = effectJsons[i]; - let effect = new Effect(); + // let effect = new Effect(); + let effect = {}; effect._name = effectJson.name; effect._uuid = `builtin-effect-${effectJson.name}`; - effect._loaded = true; + // effect._loaded = true; effect.techniques = effectJson.techniques; effect.properties = effectJson.properties; effect.defines = effectJson.defines; effect.dependencies = effectJson.dependencies ? effectJson.dependencies : []; effects[effect._uuid] = effect; + Effect.registerEffect(effect); } // ============================ @@ -192,16 +194,16 @@ export default function (device) { // ============================ let materials = {}; - [ - 'sprite', - 'font' - ].forEach(name => { - let mat = new Material(); - mat.effect = effects[`builtin-effect-${name}`]; - mat._uuid = `builtin-material-${name}`; - mat._loaded = true; - materials[mat._uuid] = mat; - }); + // [ + // 'sprite', + // 'font' + // ].forEach(name => { + // let mat = new Material(); + // mat.effect = effects[`builtin-effect-${name}`]; + // mat._uuid = `builtin-material-${name}`; + // mat._loaded = true; + // materials[mat._uuid] = mat; + // }); // return Object.assign({ diff --git a/cocos/renderer/core/effect.js b/cocos/renderer/core/effect.js index 29073879df..ab0c9b0d0e 100644 --- a/cocos/renderer/core/effect.js +++ b/cocos/renderer/core/effect.js @@ -1,90 +1,224 @@ // Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. import config from '../config'; +import enums from '../enums'; +import { vec2, vec3, vec4, color3, color4 } from '../../core/vmath'; +import Pass from './pass'; +import Technique from './technique'; +import { parseEffect } from '../utils'; +import * as ValueType from '../../core/value-types'; + export default class Effect { - /** - * @param {Array} techniques - */ - constructor(techniques, properties = {}, defines = [], dependencies = []) { - this._techniques = techniques; - this._properties = properties; - this._defines = defines; - this._dependencies = dependencies; + /** + * @param {Array} techniques + */ + constructor(techniques, properties = {}, defines = [], dependencies = []) { + this._techniques = techniques; + this._properties = properties; + this._defines = defines; + this._dependencies = dependencies; - // TODO: check if params is valid for current technique??? - } - - clear() { - this._techniques.length = 0; - this._properties = null; - this._defines.length = 0; - } - - getTechnique(stage) { - let stageID = config.stageID(stage); - if (stageID === -1) { - return null; + // TODO: check if params is valid for current technique??? } - for (let i = 0; i < this._techniques.length; ++i) { - let tech = this._techniques[i]; - if (tech.stageIDs & stageID) { - return tech; - } + clear() { + this._techniques.length = 0; + this._properties = null; + this._defines.length = 0; } - return null; - } + getTechnique(stage) { + let stageID = config.stageID(stage); + if (stageID === -1) { + return null; + } - getProperty(name) { - return this._properties[name]; - } + for (let i = 0; i < this._techniques.length; ++i) { + let tech = this._techniques[i]; + if (tech.stageIDs & stageID) { + return tech; + } + } - setProperty(name, value) { - // TODO: check if params is valid for current technique??? - this._properties[name] = value; - } - - getDefine(name) { - for (let i = 0; i < this._defines.length; ++i) { - let def = this._defines[i]; - if ( def.name === name ) { - return def.value; - } + return null; } - console.warn(`Failed to get define ${name}, define not found.`); - return null; - } - - define(name, value) { - for (let i = 0; i < this._defines.length; ++i) { - let def = this._defines[i]; - if ( def.name === name ) { - def.value = value; - return; - } + getProperty(name) { + return this._properties[name]; } - console.warn(`Failed to set define ${name}, define not found.`); - } - - extractDefines(out = {}) { - for (let i = 0; i < this._defines.length; ++i) { - let def = this._defines[i]; - out[def.name] = def.value; + setProperty(name, value) { + // TODO: check if params is valid for current technique??? + this._properties[name] = value; } - return out; - } + getDefine(name) { + for (let i = 0; i < this._defines.length; ++i) { + let def = this._defines[i]; + if (def.name === name) { + return def.value; + } + } - extractDependencies(out = {}) { - for (let i = 0; i < this._dependencies.length; ++i) { - let dep = this._dependencies[i]; - out[dep.define] = dep.extension; + console.warn(`Failed to get define ${name}, define not found.`); + return null; } - return out; - } -} \ No newline at end of file + define(name, value) { + for (let i = 0; i < this._defines.length; ++i) { + let def = this._defines[i]; + if (def.name === name) { + def.value = value; + return; + } + } + + console.warn(`Failed to set define ${name}, define not found.`); + } + + extractDefines(out = {}) { + for (let i = 0; i < this._defines.length; ++i) { + let def = this._defines[i]; + out[def.name] = def.value; + } + + return out; + } + + extractDependencies(out = {}) { + for (let i = 0; i < this._dependencies.length; ++i) { + let dep = this._dependencies[i]; + out[dep.define] = dep.extension; + } + + return out; + } + + static paramProcessing(json, processers) { + let name = json.name; + let type = json.type; + let value = json.value; + let processer = processers[type] || processers.default; + return processer(name, type, value); + } + + static getPropertyClassName(json) { + return `cc.Effect.${json._uuid}.Props`; + } + + static parseProperties(json) { + function processColor(name, type, value) { + return cc.color(value[0] * 255, value[1] * 255, value[2] * 255, (value[3] || 1) * 255); + } + function processVec2(name, type, value) { + return cc.v2(value[0], value[1]); + } + function processVec3(name, type, value) { + return cc.v3(value[0], value[1], value[2]); + } + function processTexture2d(name, type, value) { + return null; + } + function processDefault(name, type, value) { + return null; + } + let processers = { + [enums.PARAM_COLOR3]: processColor, + [enums.PARAM_COLOR4]: processColor, + [enums.PARAM_FLOAT2]: processVec2, + [enums.PARAM_FLOAT3]: processVec3, + [enums.PARAM_TEXTURE_2D]: processTexture2d, + default: processDefault + } + let properties = {}; + // process params in techniques + // note: this should be moved to effect later + json.techniques.forEach(tech => { + tech.params.forEach(param => { + properties[param.name] = Effect.paramProcessing(param, processers); + }); + }); + + // process defines + // todo: + // 1. only add artistic defines + // 2. only bool supported, may contains other type + json.defines.forEach((define) => { + properties[define.name] = false; + }); + return properties; + } + + static registerEffect(json) { + cc.Class({ + name: Effect.getPropertyClassName(json), + properties: Effect.parseProperties(json), + }); + } + + static parsePass(json) { + // console.error('try to parse pass, should add render state here'); + let pass = new Pass(json.program); + if (json.depthTest !== undefined && json.depthWrite !== undefined) { + pass.setDepth(json.depthTest, json.depthWrite); + } + if (json.cullMode !== undefined) { + pass.setCullMode(json.cullMode); + } + if (json.blend === true) { + pass.setBlend(json.blendEq, json.blendSrc, json.blendDst, json.blendAlphaEq, json.blendSrcAlpha, json.blendDstAlpha); + } + + return pass; + } + + static parseTechnique(json) { + function processColor(value) { + return new ValueType.Color(value[0] * 255, value[1] * 255, value[2] * 255, (value[3] || 1.0) * 255); + } + function processVec2(value) { + return new ValueType.Vec2(value[0], value[1]); + } + function processVec3(value) { + return new ValueType.Vec3(value[0], value[1], value[2]); + } + let processers = { + color3: processColor, + color4: processColor, + float2: processVec2, + float3: processVec3 + } + let stages = json.stages; + let passes = []; + json.passes.forEach(pass => { + passes.push(Effect.parsePass(pass)); + }); + let params = []; + for (let paramKey in json.params) { + // let param = Effect.paramProcessing(, processers); + params.push(json.params[paramKey]); + } + let layer = json.layer; + return new Technique(stages, params, passes, layer); + } + + static parseEffect(effectJson) { + function _objArrayClone(val) { + return val.map(obj => Object.assign({}, obj)); + } + + let techniques = []; + effectJson.techniques.forEach(tech => { + techniques.push(Effect.parseTechnique(tech)); + }); + // todo add props extraction + let props = {}; + let defines = _objArrayClone(effectJson.defines); + let deps = _objArrayClone(effectJson.dependencies); + + return new Effect(techniques, props, defines, deps); + } +} + +cc.Effect = Effect;