mirror of
https://github.com/cocos/cocos4.git
synced 2026-12-02 00:22:43 +08:00
modify material structure (#3424)
This commit is contained in:
+24
-26
@@ -27,8 +27,6 @@ import { _decorator } from "../../core/data";
|
||||
const { ccclass, property } = _decorator;
|
||||
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) {
|
||||
@@ -38,10 +36,10 @@ function _objArrayClone(val) {
|
||||
@ccclass('cc.Material')
|
||||
export default class Material extends Asset {
|
||||
/**
|
||||
* @type {cc.Effect}
|
||||
* @type {Object}
|
||||
*/
|
||||
@property(cc.Effect)
|
||||
_effect = null;
|
||||
@property(Object)
|
||||
_effectAsset = null;
|
||||
|
||||
/**
|
||||
* @type {Object}
|
||||
@@ -52,23 +50,23 @@ export default class Material extends Asset {
|
||||
/**
|
||||
* @type {cc.renderer.Effect}
|
||||
*/
|
||||
_effecInst = null;
|
||||
_effect = null;
|
||||
|
||||
/**
|
||||
* @return {Effect}
|
||||
* @return {Object}
|
||||
*/
|
||||
get effect() {
|
||||
return this._effect;
|
||||
get effectAsset() {
|
||||
return this._effectAsset;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {Effect} val
|
||||
* @param {Object} 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));
|
||||
set effectAsset(val) {
|
||||
if (this._effectAsset !== val) {
|
||||
this._effectAsset = val;
|
||||
this._effect = Effect.parseEffect(this._effectAsset);
|
||||
let propCls = cc.js.getClassByName(Effect.getPropertyClassName(this._effectAsset));
|
||||
this._props = propCls ? new propCls() : {}
|
||||
}
|
||||
}
|
||||
@@ -76,8 +74,8 @@ export default class Material extends Asset {
|
||||
/**
|
||||
* @return {cc.renderer.Effect}
|
||||
*/
|
||||
get effectInst() {
|
||||
return this._effectInst;
|
||||
get effect() {
|
||||
return this._effect;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -85,9 +83,9 @@ export default class Material extends Asset {
|
||||
* @param {Material} mat
|
||||
*/
|
||||
copy(mat) {
|
||||
if (this._effect !== mat._effect) {
|
||||
this._effect = mat._effect;
|
||||
this._effecInst = Effect.parseEffect(this._effect);
|
||||
if (this._effectAsset !== mat._effectAsset) {
|
||||
this._effectAsset = mat._effectAsset;
|
||||
this._effect = Effect.parseEffect(this._effectAsset);
|
||||
}
|
||||
|
||||
for (let name in mat._props) {
|
||||
@@ -104,9 +102,9 @@ export default class Material extends Asset {
|
||||
this._props[name] = val;
|
||||
|
||||
if (val instanceof Texture) {
|
||||
this._effectInst.setProperty(name, val._texture);
|
||||
this._effect.setProperty(name, val._texture);
|
||||
} else {
|
||||
this._effectInst.setProperty(name, val);
|
||||
this._effect.setProperty(name, val);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -116,7 +114,7 @@ export default class Material extends Asset {
|
||||
* @param {*} val
|
||||
*/
|
||||
define(name, val) {
|
||||
this._effectInst.define(name, val);
|
||||
this._effect.define(name, val);
|
||||
}
|
||||
|
||||
destroy() {
|
||||
@@ -127,7 +125,7 @@ export default class Material extends Asset {
|
||||
_serialize() {
|
||||
// todo: this may be refactored for it is editor only
|
||||
return {
|
||||
effect: (this._effect && this._effect._uuid) || 'unknown',
|
||||
effect: (this._effectAsset && this._effectAsset._uuid) || 'unknown',
|
||||
props: Editor.serialize(this._props),
|
||||
};
|
||||
}
|
||||
@@ -135,8 +133,8 @@ export default class Material extends Asset {
|
||||
_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._effectAsset = cc.game._builtins[data.effect];
|
||||
this._effect = Effect.parseEffect(this._effectAsset);
|
||||
this._props = cc.deserialize(data.props);
|
||||
} else {
|
||||
// todo: add custom effect
|
||||
|
||||
@@ -199,7 +199,7 @@ export default function (device) {
|
||||
// 'font'
|
||||
// ].forEach(name => {
|
||||
// let mat = new Material();
|
||||
// mat.effect = effects[`builtin-effect-${name}`];
|
||||
// mat.effectAsset = effects[`builtin-effect-${name}`];
|
||||
// mat._uuid = `builtin-material-${name}`;
|
||||
// mat._loaded = true;
|
||||
// materials[mat._uuid] = mat;
|
||||
|
||||
@@ -173,7 +173,7 @@ export default class ModelComponent extends RenderableComponent {
|
||||
|
||||
/** @type {import("../../../engine/cocos/3d/assets/material").default} */
|
||||
let mtl = new cc.Material();
|
||||
mtl.effect = cc.game._builtins['builtin-effect-unlit'];
|
||||
mtl.effectAsset = cc.game._builtins['builtin-effect-unlit'];
|
||||
mtl.setProperty("color", new cc.vmath.color4(0, 0, 0, 1));
|
||||
this.material = mtl;
|
||||
}
|
||||
@@ -221,7 +221,7 @@ export default class ModelComponent extends RenderableComponent {
|
||||
|
||||
if (this._models.length > 0) {
|
||||
this._models.forEach((model) => {
|
||||
model.setEffect(val.effectInst);
|
||||
model.setEffect(val.effect);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -259,7 +259,7 @@ export default class ModelComponent extends RenderableComponent {
|
||||
let inputAssembler = this._mesh.getSubMesh(i);
|
||||
|
||||
model.setInputAssembler(inputAssembler);
|
||||
model.setEffect(material ? material.effectInst : null);
|
||||
model.setEffect(material ? material.effect : null);
|
||||
model.setNode(this.node);
|
||||
}
|
||||
}
|
||||
@@ -273,7 +273,7 @@ export default class ModelComponent extends RenderableComponent {
|
||||
}
|
||||
|
||||
_onMaterialModified(idx, mat) {
|
||||
this._models[idx].setEffect(mat.effectInst);
|
||||
this._models[idx].setEffect(mat.effect);
|
||||
}
|
||||
|
||||
_updateCastShadow() {
|
||||
|
||||
@@ -111,7 +111,7 @@ export default class SkinningModelComponent extends ModelComponent {
|
||||
|
||||
/** @type {import("../assets/material").default} */
|
||||
let mtl = new cc.Material();
|
||||
mtl.effect = cc.game._builtins['builtin-effect-unlit'];
|
||||
mtl.effectAsset = cc.game._builtins['builtin-effect-unlit'];
|
||||
mtl.setProperty("color", new cc.vmath.color4(0, 0, 0, 1));
|
||||
this.material = mtl;
|
||||
}
|
||||
@@ -268,4 +268,4 @@ export default class SkinningModelComponent extends ModelComponent {
|
||||
this._skinningTarget.set(joint, targetNode);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -78,11 +78,11 @@ export default class SkyboxComponent extends RenderableComponent {
|
||||
|
||||
if (this._material === null) {
|
||||
this._material = new Material();
|
||||
// this._material.effect = this._app.assets.get('builtin-effect-skybox');
|
||||
// this._material.effectAsset = this._app.assets.get('builtin-effect-skybox');
|
||||
}
|
||||
|
||||
this._updateMaterialParams();
|
||||
this._model.setEffect(this._material.effectInst);
|
||||
this._model.setEffect(this._material.effect);
|
||||
}
|
||||
|
||||
onEnable() {
|
||||
@@ -104,7 +104,7 @@ export default class SkyboxComponent extends RenderableComponent {
|
||||
|
||||
_onMaterialModified(idx, mat) {
|
||||
this._updateMaterialParams(mat);
|
||||
this._model.setEffect(mat.effectInst);
|
||||
this._model.setEffect(mat.effect);
|
||||
}
|
||||
|
||||
_updateMaterialParams(mat) {
|
||||
|
||||
Reference in New Issue
Block a user