Files
material-editor/src/stores/sceneStore.ts

182 lines
6.0 KiB
TypeScript
Raw Normal View History

2025-04-14 16:37:17 +08:00
import { defineStore } from "pinia";
import { computed, ref } from "vue";
2025-04-14 16:37:17 +08:00
import { MaterialEditor } from "../common/MaterialEditor";
import { Database, PhysicalMaterialRecord, TextureTableRecord } from "webcad_ue4_api";
2025-04-14 16:37:17 +08:00
import { LoadImageFromUrl } from "../helpers/helper.imageLoader";
import { Texture } from "three";
import { materialRenderer } from "../common/MaterialRenderer";
2025-05-09 19:29:09 +08:00
import { MaterialIn, MaterialOut } from "../common/MaterialSerializer";
2025-05-29 15:53:01 +08:00
import { GetConfig } from "../lib/libOutputConfig";
2025-04-14 16:37:17 +08:00
2025-05-09 16:31:28 +08:00
const sceneSetup = () => {
let _editor: MaterialEditor | undefined;
let _database: Database | undefined;
2025-05-29 19:00:36 +08:00
const _material = ref<PhysicalMaterialRecord>(new PhysicalMaterialRecord());
2025-04-14 16:37:17 +08:00
const _currGeometry = ref<string>('球');
const _currTexture = ref<Texture>();
const CurrGeometry = computed({
get: () => _currGeometry.value,
set: (val: string) => ChangeGeometry(val)
})
const CurrTexture = computed<Texture>(() => _currTexture.value);
const Geometries = ref<string[]>([]);
2025-05-29 19:00:36 +08:00
const Material = computed(() => _material.value);
2025-05-29 15:53:01 +08:00
const CurrentShowObject = computed(() => _editor.ShowObject);
2025-04-14 16:37:17 +08:00
function Initial(canvas: HTMLElement) {
if (_editor) {
console.warn("SceneStore has already been initialized");
return;
}
// 初始化Database
_database = new Database();
_database.hm.Enable = false; // 关闭历史记录功能
Material.value.Name = _database.MaterialTable.AllocateName(); // 使用Database为材质分配材质名
_database.MaterialTable.Add(Material.value as PhysicalMaterialRecord);
2025-05-29 15:53:01 +08:00
2025-04-14 16:37:17 +08:00
// 为Material配置一个ObjectId否则其无法被序列化
// Material.value.objectId = new ObjectId(undefined, undefined);
2025-04-14 16:37:17 +08:00
2025-05-09 16:31:28 +08:00
_editor = new MaterialEditor();
2025-04-14 16:37:17 +08:00
Geometries.value = Array.from(_editor.Geometrys.keys());
_currGeometry.value = _editor.CurGeometryName;
_editor.SetViewer(canvas);
_editor.setMaterial((Material.value) as PhysicalMaterialRecord);
const view = _editor.Viewer;
window.onresize = () => {
view.SetSize(canvas.clientWidth, canvas.clientHeight);
2025-04-14 16:37:17 +08:00
view.UpdateRender();
}
}
function Dispose() {
Material.value.GoodBye();
_editor?.Dispose();
_editor = undefined;
_database.Destroy();
_database = undefined;
window.onresize = undefined;
}
2025-04-14 16:37:17 +08:00
function ChangeGeometry(geoName: string) {
_currGeometry.value = geoName;
let geo = _editor.Geometrys.get(_currGeometry.value);
if (geo) {
_editor.ShowMesh.geometry = geo;
_editor.Viewer.UpdateRender();
}
}
function Update() {
_editor.Viewer.UpdateRender();
}
async function UpdateMaterialAsync() {
2025-05-29 19:00:36 +08:00
// TODO: Danger: 如果等待下面这一行,会导致更新卡住(未知问题)
Material.value.Update();
// Material.value.Material.needsUpdate = true;
2025-04-14 16:37:17 +08:00
await _editor.Update();
Update();
}
2025-05-29 15:53:01 +08:00
async function ChangeTextureFromUrlAsync(url?: string) {
// 关联贴图
const db = Material.value.Db;
2025-05-29 15:53:01 +08:00
if (!db) return; // 材质未初始化
let record = Material.value.map?.Object as TextureTableRecord;
if (!record) {
// record = db.TextureTable.Symbols.values().next().value;
// if(!record){
record = new TextureTableRecord();
record.Name = db.TextureTable.AllocateName();
db.TextureTable.Add(record);
// 替换map
Material.value.map = record.Id;
// Material.value.map = img ? record.Id : undefined;
// }
}
2025-04-14 16:37:17 +08:00
2025-05-29 15:53:01 +08:00
// record.objectId = new ObjectId(undefined, record);
2025-04-14 16:37:17 +08:00
_currTexture.value = record['texture'] as Texture;
2025-05-29 15:53:01 +08:00
if (url) {
record.imageUrl = url;
_currTexture.value.image = undefined;
}
2025-05-29 15:53:01 +08:00
// 设置Store
if (!_currTexture.value.image) {
const img = await LoadImageFromUrl(GetConfig().host + '/' + record.imageUrl);
_currTexture.value.image = img;
_currTexture.value.needsUpdate = true;
}
2025-04-14 16:37:17 +08:00
await UpdateMaterialAsync();
}
async function SerializeMaterialAsync() {
2025-04-14 16:37:17 +08:00
const matJson = MaterialOut(Material.value as PhysicalMaterialRecord);
console.log(matJson);
return matJson;
2025-04-14 16:37:17 +08:00
}
2025-05-09 19:29:09 +08:00
async function ImportMaterialAsync(materialJson: string) {
const material = MaterialIn(JSON.parse(materialJson));
2025-05-29 19:00:36 +08:00
_material.value = material;
_editor.setMaterial(_material.value as PhysicalMaterialRecord);
2025-05-29 15:53:01 +08:00
await ChangeTextureFromUrlAsync();
2025-05-29 19:00:36 +08:00
await UpdateMaterialAsync();
2025-05-09 19:29:09 +08:00
}
async function GenerateMaterialLogoAsync() {
2025-04-14 16:37:17 +08:00
const blob = await materialRenderer.getBlob(Material.value.Material);
return blob;
// const file = new File([blob], "blob.png", { type: blob.type });
2025-04-14 16:37:17 +08:00
// const formData = new FormData();
// formData.append("file", file);
2025-04-14 16:37:17 +08:00
// let data = await Post(ImgsUrl.logo, formData);
2025-04-14 16:37:17 +08:00
// let logoPath = "";
// if (data.err_code === RequestStatus.Ok) {
// logoPath = data.images.path;
// }
// return logoPath;
2025-04-14 16:37:17 +08:00
}
return {
CurrGeometry,
2025-05-09 19:29:09 +08:00
CurrTexture,
2025-04-14 16:37:17 +08:00
Geometries,
Material,
Initial,
Update,
UpdateMaterialAsync,
2025-05-09 19:29:09 +08:00
ChangeTextureFromUrlAsync,
SerializeMaterialAsync,
2025-05-09 19:29:09 +08:00
ImportMaterialAsync,
GenerateMaterialLogoAsync,
2025-05-29 15:53:01 +08:00
Dispose,
2025-05-29 19:00:36 +08:00
CurrentShowObject,
GetEditor: () => _editor
2025-04-14 16:37:17 +08:00
};
2025-05-09 16:31:28 +08:00
};
export const useScene = defineStore('scene', sceneSetup);
export const useSceneRaw = defineStore('sceneRaw', sceneSetup); // 独立场景,用来静默执行材质序列化
2025-04-14 16:37:17 +08:00
export type TextureAdjustment = {
wrapS: number,
wrapT: number,
rotation: number,
repeatX: number,
repeatY: number,
moveX: number,
moveY: number
}
export interface UploadMaterialRequest {
dirId: string;
materialName: string;
2025-04-14 16:37:17 +08:00
}