From 8fda3ce34ce381875bac310755805702676c8cb3 Mon Sep 17 00:00:00 2001 From: zxg0622 <247274249@qq.com> Date: Mon, 8 Oct 2018 17:09:33 +0800 Subject: [PATCH] add material instance (#3337) --- cocos/3d/assets/material.js | 30 +++++--- cocos/3d/framework/model-component.js | 71 +++++-------------- cocos/3d/framework/renderable-component.js | 81 ++++++++++++++++++++++ cocos/3d/framework/skybox-component.js | 35 +++------- 4 files changed, 129 insertions(+), 88 deletions(-) create mode 100644 cocos/3d/framework/renderable-component.js diff --git a/cocos/3d/assets/material.js b/cocos/3d/assets/material.js index 6e10e11d2a..7d687c6b98 100644 --- a/cocos/3d/assets/material.js +++ b/cocos/3d/assets/material.js @@ -156,8 +156,8 @@ export default class Material extends Asset { } /** - * - * @param {Material} mat + * + * @param {Material} mat */ copy(mat) { if (this._effect !== mat._effect) { @@ -173,9 +173,9 @@ export default class Material extends Asset { } /** - * - * @param {string} name - * @param {*} val + * + * @param {string} name + * @param {*} val */ setProperty(name, val) { this._props[name] = val; @@ -188,9 +188,9 @@ export default class Material extends Asset { } /** - * - * @param {string} name - * @param {*} val + * + * @param {string} name + * @param {*} val */ define(name, val) { this._effectInst.define(name, val); @@ -200,6 +200,18 @@ export default class Material extends Asset { // TODO: what should we do here ??? return super.destroy(); } + + static getInstantiatedMaterial(mat, rndCom) { + if (mat._owner === rndCom) { + return this; + } + else { + let instance = new Material(); + instance.copy(mat); + instance._native = mat._native + ' (Instance)'; + instance._owner = rndCom; + } + } } -cc.Material = Material; \ No newline at end of file +cc.Material = Material; diff --git a/cocos/3d/framework/model-component.js b/cocos/3d/framework/model-component.js index af44f0439d..656792de44 100644 --- a/cocos/3d/framework/model-component.js +++ b/cocos/3d/framework/model-component.js @@ -28,6 +28,7 @@ import renderer from '../../renderer/index'; import { ccclass, property, menu } from '../../core/data/class-decorator'; import Mesh from '../assets/mesh'; import Enum from '../../core/value-types/enum'; +import RenderableComponent from './renderable-component'; /** * @typedef {import('../assets/material').default} Material */ @@ -83,13 +84,11 @@ let ModelShadowCastingMode = Enum({ * * !#ch 模型组件 * @class ModelComponent - * @extends RenderSystemActor + * @extends RenderableComponent */ @ccclass('cc.ModelComponent') @menu('Components/ModelComponent') -export default class ModelComponent extends RenderSystemActor { - @property - _materials = []; +export default class ModelComponent extends RenderableComponent { @property _mesh = null; @@ -100,22 +99,6 @@ export default class ModelComponent extends RenderSystemActor { @property _receiveShadows = false; - /** - * !#en The material of the model - * - * !#ch 模型材质 - * @type {Material[]} - */ - @property - get materials() { - return this._materials; - } - - set materials(val) { - this._materials = val; - this._updateModelParams(); - } - /** * !#en The mesh of the model * @@ -187,40 +170,6 @@ export default class ModelComponent extends RenderSystemActor { } } - /** - * !#en Returns the material corresponding to the sequence number - * - * !#ch 返回相对应序号的材质 - * @param {Number} idx - Look for the material list number - */ - getMaterial(idx) { - if (this._materials.length === 0) { - return null; - } - - if (idx < this._materials.length) { - return this._materials[idx]; - } - - return this._materials[this._materials.length - 1]; - } - - get material() { - return this.getMaterial(0); - } - - set material(val) { - if (this._materials.length === 1 && this._materials[0] === val) { - return; - } - - this._materials[0] = val; - - if (this._models.length > 0) { - this._models[0].setEffect(val.effectInst); - } - } - _updateModels() { let meshCount = this._mesh ? this._mesh.subMeshCount : 0; let oldModels = this._models; @@ -247,7 +196,7 @@ export default class ModelComponent extends RenderSystemActor { _updateModelParams() { for (let i = 0; i < this._models.length; ++i) { let model = this._models[i]; - let material = this.getMaterial(i); + let material = this._getSubMeshMaterial(i); let inputAssembler = this._mesh.getSubMesh(i); model.setInputAssembler(inputAssembler); @@ -256,6 +205,18 @@ export default class ModelComponent extends RenderSystemActor { } } + _getSubMeshMaterial(idx) { + let mat = this.getSharedMaterial(idx); + if (mat == null && this._materials.length > 0) { + mat = this._materials[this._materials.length - 1]; + } + return mat; + } + + _onMaterialModified(idx, mat) { + this._models[idx].setEffect(mat.effectInst); + } + _updateCastShadow() { if (this._shadowCastingMode === 'off') { for (let i = 0; i < this._models.length; ++i) { diff --git a/cocos/3d/framework/renderable-component.js b/cocos/3d/framework/renderable-component.js new file mode 100644 index 0000000000..f1e3d94266 --- /dev/null +++ b/cocos/3d/framework/renderable-component.js @@ -0,0 +1,81 @@ +import RenderSystemActor from "./renderSystemActor"; +import { _decorator } from '../../core/data/index'; +import Material from "../assets/material"; +const { ccclass, property } = _decorator; + +@ccclass('cc.RenderableComponent') +export default class RenderableComponent extends RenderSystemActor { + @property + _materials = []; + + /** + * !#en The material of the model + * + * !#ch 模型材质 + * @type {Material[]} + */ + @property + get materials() { + for (let i = 0; i < this._materials.length; i++) { + this._materials[i] = this.getMaterial(i); + } + return this._materials; + } + + get sharedMaterials() { + return this._materials; + } + + set materials(val) { + this._materials = val; + for (let i = 0; i < val.length; i++) { + this._onMaterialModified(i, val[i]); + } + } + + /** + * !#en Returns the material corresponding to the sequence number + * + * !#ch 返回相对应序号的材质 + * @param {Number} idx - Look for the material list number + */ + getMaterial(idx) { + if (idx < 0 || idx >= this._materials.length) { + return null; + } + + let instantiated = Material.getInstantiatedMaterial(this._materials[idx], this); + if (instantiated != this) { + this._materials[idx] = instantiated; + this._onMaterialModified(idx, instantiated); + } + + return this._materials[idx]; + } + + getSharedMaterial(idx) { + if (idx < 0 || idx >= this._materials.length) { + return null; + } + return this._materials[idx]; + } + + get material() { + return this.getMaterial(0); + } + + get sharedMaterial() { + return this.getSharedMaterial(0); + } + + set material(val) { + if (this._materials.length === 1 && this._materials[0] === val) { + return; + } + + if (val != this._materials[0]) { + this._onMaterialModified(0, val); + } + this._materials[0] = val; + } +} diff --git a/cocos/3d/framework/skybox-component.js b/cocos/3d/framework/skybox-component.js index db6b7dd6e5..553b9d39ba 100644 --- a/cocos/3d/framework/skybox-component.js +++ b/cocos/3d/framework/skybox-component.js @@ -28,40 +28,22 @@ import renderer from '../../renderer/index'; import { box } from '../primitive/index'; import Material from '../assets/material'; import { ccclass, property, menu } from '../../core/data/class-decorator'; +import RenderableComponent from './renderable-component'; /** * !#en The Skybox Component * * !#ch 天空盒组件 * @class SkyboxComponent - * @extends RenderSystemActor + * @extends RenderableComponent */ @ccclass('cc.SkyboxComponent') @menu('Components/SkyboxComponent') -export default class SkyboxComponent extends RenderSystemActor { - @property - _material = null; +export default class SkyboxComponent extends RenderableComponent { @property _cubeMap = null; - /** - * !#en The material of the model - * - * !#ch 模型普通材质 - * @type {Material} - */ - get material() { - return this._material; - } - - set material(val) { - this._material = val; - if (!this._material) return; - this._updateMaterialParams(); - this._model.setEffect(val.effectInst); - } - /** * !#en The material of the model * @@ -120,12 +102,17 @@ export default class SkyboxComponent extends RenderSystemActor { } } - _updateMaterialParams() { - if (this._material === null || this._material === undefined) { + _onMaterialModified(idx, mat) { + this._updateMaterialParams(mat); + this._model.setEffect(mat.effectInst); + } + + _updateMaterialParams(mat) { + if (mat === null || mat === undefined) { return; } if (this._cubeMap !== null && this._cubeMap !== undefined) { - this._material.setProperty('cubeMap', this._cubeMap); + mat.setProperty('cubeMap', this._cubeMap); } } }