模型材质动态修改。

This commit is contained in:
liteng 2018-12-31 20:39:57 +08:00
parent a36b4616c6
commit 108a5607fa
2 changed files with 34 additions and 0 deletions

View File

@ -1393,6 +1393,11 @@ MaterialComponent.prototype.updateMaterial = function () {
editor.execute(new SetMaterialValueCommand(object, 'wireframeLinewidth', wireframeLinewidth.getValue()));
}
if (this.selected.userData.Server === true) { // 服务端模型
this.selected.userData.materials = this.selected.userData.materials || {};
this.selected.userData.materials[material.uuid] = (new MaterialsSerializer()).toJSON(material);
}
this.updateUI();
if (textureWarning) {

View File

@ -1,6 +1,7 @@
import BaseSerializer from '../BaseSerializer';
import Object3DSerializer from './Object3DSerializer';
import ModelLoader from '../../loader/ModelLoader';
import MaterialsSerializer from '../material/MaterialsSerializer';
/**
* ServerObject
@ -20,6 +21,16 @@ ServerObject.prototype.toJSON = function (obj) {
delete json.userData.obj; // 以后下载对象缓存统一改为obj
delete json.userData.root; // 模型根节点
delete json.userData.helper;
// 清理无用材质
if (obj.userData.materials) {
Object.keys(obj.userData.materials).forEach(n => {
if (n !== obj.material.uuid) {
delete obj.userData.materials[n];
}
});
}
return json;
};
@ -37,6 +48,7 @@ ServerObject.prototype.fromJSON = function (json, options, environment) {
loader.load(url, json.userData, environment).then(obj => {
if (obj) {
Object3DSerializer.prototype.fromJSON.call(this, json, obj);
this.parseMaterials(json, obj);
resolve(obj);
} else {
resolve(null);
@ -45,4 +57,21 @@ ServerObject.prototype.fromJSON = function (json, options, environment) {
});
};
ServerObject.prototype.parseMaterials = function (json, obj) {
if (json.userData.materials) {
Object.values(json.userData.materials).forEach(n => {
this.parseMaterial(n, obj);
});
}
};
ServerObject.prototype.parseMaterial = function (json, obj) {
if (obj.material.uuid === json.uuid) {
var material = obj.material;
obj.material = (new MaterialsSerializer()).fromJSON(json);
obj.material.needsUpdate = true;
material.dispose();
}
};
export default ServerObject;