添加编辑模式

This commit is contained in:
2025-05-09 19:29:09 +08:00
parent d92cdedc57
commit 37158e7cb1
9 changed files with 105 additions and 26 deletions

View File

@@ -19,4 +19,4 @@ export const useEvent = defineStore('event', () => {
return { Subscribe, Publish, Unsubscribe };
});
export type EventNames = 'submit' | 'update-texture';
export type EventNames = 'submit' | 'update-texture' | 'update-config';

View File

@@ -5,7 +5,7 @@ import { Database, ObjectId, PhysicalMaterialRecord, TextureTableRecord } from "
import { LoadImageFromUrl } from "../helpers/helper.imageLoader";
import { Texture } from "three";
import { materialRenderer } from "../common/MaterialRenderer";
import { MaterialOut } from "../common/MaterialSerializer";
import { MaterialIn, MaterialOut } from "../common/MaterialSerializer";
const sceneSetup = () => {
let _editor: MaterialEditor | undefined;
@@ -77,7 +77,7 @@ const sceneSetup = () => {
Update();
}
async function ChangeTextureAsync(url: string) {
async function ChangeTextureFromUrlAsync(url: string) {
const img = await LoadImageFromUrl(url);
// 关联贴图
@@ -97,25 +97,38 @@ const sceneSetup = () => {
await UpdateMaterialAsync();
}
function UpdateTexture(adjustment: TextureAdjustment) {
const texture = _currTexture.value;
texture.wrapS = adjustment.wrapS;
texture.wrapT = adjustment.wrapT;
texture.anisotropy = 16;
texture.rotation = adjustment.rotation;
texture.repeat.set(adjustment.repeatX, adjustment.repeatY);
texture.offset.set(adjustment.moveX, adjustment.moveY);
texture.needsUpdate = true;
Update();
}
async function SerializeMaterialAsync() {
const matJson = MaterialOut(Material.value as PhysicalMaterialRecord);
console.log(matJson);
return matJson;
}
async function ApplyTextureAsync(textureRecord: TextureTableRecord)
{
if (!textureRecord.imageUrl) {
alert("该纹理无效");
return;
}
// 绑定纹理
let newTexture = textureRecord.Clone() as TextureTableRecord;
newTexture.Owner = undefined;
newTexture.Name = _database.TextureTable.AllocateName();
_database.TextureTable.Add(newTexture);
// 替换map
Material.value.map = newTexture.Id;
await UpdateMaterialAsync();
}
async function ImportMaterialAsync(materialJson: string) {
const material = MaterialIn(JSON.parse(materialJson));
Material.value = material;
_editor.setMaterial(material);
await UpdateMaterialAsync();
}
async function GenerateMaterialLogoAsync() {
const blob = await materialRenderer.getBlob(Material.value.Material);
return blob;
@@ -135,14 +148,15 @@ const sceneSetup = () => {
return {
CurrGeometry,
CurrTexture,
Geometries,
Material,
Initial,
Update,
UpdateMaterialAsync,
ChangeTextureAsync,
UpdateTexture,
ChangeTextureFromUrlAsync,
SerializeMaterialAsync,
ImportMaterialAsync,
GenerateMaterialLogoAsync,
Dispose
};