修改更新

This commit is contained in:
xief
2025-05-29 15:53:01 +08:00
parent 16fac2ab19
commit 26f1c60a43
4 changed files with 69 additions and 61 deletions

View File

@@ -6,6 +6,7 @@ import { LoadImageFromUrl } from "../helpers/helper.imageLoader";
import { Texture } from "three";
import { materialRenderer } from "../common/MaterialRenderer";
import { MaterialIn, MaterialOut } from "../common/MaterialSerializer";
import { GetConfig } from "../lib/libOutputConfig";
const sceneSetup = () => {
let _editor: MaterialEditor | undefined;
@@ -19,6 +20,7 @@ const sceneSetup = () => {
const CurrTexture = computed<Texture>(() => _currTexture.value);
const Geometries = ref<string[]>([]);
const Material = ref<PhysicalMaterialRecord>(new PhysicalMaterialRecord());
const CurrentShowObject = computed(() => _editor.ShowObject);
function Initial(canvas: HTMLElement) {
if (_editor) {
@@ -31,7 +33,7 @@ const sceneSetup = () => {
_database.hm.Enable = false; // 关闭历史记录功能
Material.value.Name = _database.MaterialTable.AllocateName(); // 使用Database为材质分配材质名
_database.MaterialTable.Add(Material.value as PhysicalMaterialRecord);
// 为Material配置一个ObjectId否则其无法被序列化
// Material.value.objectId = new ObjectId(undefined, undefined);
@@ -72,28 +74,44 @@ const sceneSetup = () => {
}
async function UpdateMaterialAsync() {
console.log('UpdateMaterialAsync')
await Material.value.Update();
await _editor.Update();
Update();
}
async function ChangeTextureFromUrlAsync(url: string) {
const img = await LoadImageFromUrl(url);
async function ChangeTextureFromUrlAsync(url?: string) {
console.log('ChangeTextureFromUrlAsync')
// 关联贴图
const db = Material.value.Db;
const record = new TextureTableRecord();
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;
// }
}
// record.objectId = new ObjectId(undefined, record);
record.Name = db.TextureTable.AllocateName();
db.TextureTable.Add(record);
_currTexture.value = record['texture'] as Texture;
if (url) {
record.imageUrl = url;
_currTexture.value.image = undefined;
}
// 设置Store
_currTexture.value = record['texture'] as Texture;
_currTexture.value.image = img;
// 替换map
Material.value.map = img ? record.Id : undefined;
_currTexture.value.needsUpdate = true;
if (!_currTexture.value.image) {
const img = await LoadImageFromUrl(GetConfig().host + '/' + record.imageUrl);
_currTexture.value.image = img;
_currTexture.value.needsUpdate = true;
}
await UpdateMaterialAsync();
}
@@ -103,30 +121,11 @@ const sceneSetup = () => {
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();
await ChangeTextureFromUrlAsync();
}
async function GenerateMaterialLogoAsync() {
@@ -158,7 +157,8 @@ const sceneSetup = () => {
SerializeMaterialAsync,
ImportMaterialAsync,
GenerateMaterialLogoAsync,
Dispose
Dispose,
CurrentShowObject
};
};