'use strict';
const { materialTechniquePolyfill } = require('../utils/material');
const { setDisabled, setReadonly, setHidden, loopSetAssetDumpDataReadonly } = require('../utils/prop');
const { join, sep, normalize } = require('path');
exports.style = `
ui-button.location { flex: none; margin-left: 6px; }
`;
exports.template = /* html */ `
`;
exports.$ = {
pass: '.pass',
header: '.header',
section: '.section',
effect: '.effect',
location: '.location',
technique: '.technique',
materialDump: '.material-dump',
useInstancing: '.useInstancing',
useBatching: '.useBatching',
customSection: '.customSection',
customPanel: '.customPanel',
};
exports.methods = {
record() {
return JSON.stringify({
material: this.material,
cacheData: this.cacheData,
});
},
async restore(record) {
record = JSON.parse(record);
if (!record || typeof record !== 'object' || !record.material) {
return false;
}
this.material = record.material;
this.cacheData = record.cacheData;
await this.updateInterface({ snapshot: false });
return true;
},
async getCustomInspector() {
const currentEffectInfo = this._effects.find((effect) => {
return effect.name === this.material.effect;
});
if (currentEffectInfo && currentEffectInfo.uuid) {
const meta = await Editor.Message.request('asset-db', 'query-asset-meta', currentEffectInfo.uuid);
return meta && meta.userData && meta.userData.editor && meta.userData.editor.inspector;
}
return '';
},
/**
* Custom Save
*/
async apply() {
this.reset();
await Editor.Message.request('scene', 'apply-material', this.asset.uuid, this.material);
},
reset() {
this.dirtyData.origin = this.dirtyData.realtime;
this.dirtyData.uuid = '';
this.cacheData = {};
},
/**
*
* @param {string} inspector
*/
async updateCustomInspector(inspector) {
this.$.customPanel.hidden = false;
this.$.section.hidden = true;
this.$.materialDump.hidden = true;
try {
if (inspector.startsWith('packages://')) {
const relatePath = normalize(inspector.replace('packages://', ''));
const name = relatePath.split(sep)[0];
const packagePath = Editor.Package.getPackages({ name, enable: true })[0].path;
const path = join(packagePath, relatePath.split(name)[1]);
if (this.$.customPanel.getAttribute('src') !== path) {
this.$.customPanel.setAttribute('src', path);
}
this.$.customPanel.update(this.material, this.assetList, this.metaList);
} else {
throw Editor.I18n.t('ENGINE.assets.material.illegal-inspector-url');
}
} catch (error) {
console.error(error);
console.error(Editor.I18n.t('ENGINE.assets.material.fail-to-load-custom-inspector', { effect: this.material.effect }));
this.updatePasses();
}
},
/**
* Detection of data changes only determines the currently selected technique
*/
setDirtyData(state) {
this.dirtyData.realtime = JSON.stringify({
effect: this.material.effect,
technique: this.material.technique,
techniqueData: this.material.data[this.material.technique],
});
if (!this.dirtyData.origin) {
this.dirtyData.origin = this.dirtyData.realtime;
this.dispatch('snapshot');
} else {
this.dispatch('change', state);
}
this.canUpdatePreview = true;
},
isDirty() {
const isDirty = this.dirtyData.origin !== this.dirtyData.realtime;
return isDirty;
},
async updateInterface(state) {
if (this.canUpdatePreview) {
await this.updatePreview();
}
// effect