优化纹理参数绑定处理逻辑,添加默认纹理参数

This commit is contained in:
2025-06-13 15:24:17 +08:00
parent 3d27bccdea
commit 3aebd9ed95
4 changed files with 59 additions and 49 deletions

View File

@@ -3,7 +3,7 @@ import { computed, ref } from "vue";
import { MaterialEditor } from "../common/MaterialEditor";
import { Database, PhysicalMaterialRecord, TextureTableRecord } from "webcad_ue4_api";
import { LoadImageFromUrl } from "../helpers/helper.imageLoader";
import { Texture } from "three";
import { ClampToEdgeWrapping, MirroredRepeatWrapping, RepeatWrapping, Texture } from "three";
import { materialRenderer } from "../common/MaterialRenderer";
import { MaterialIn, MaterialOut } from "../common/MaterialSerializer";
import { GetConfig } from "../lib/libOutputConfig";
@@ -13,12 +13,12 @@ const sceneSetup = () => {
let _database: Database | undefined;
const _material = ref<PhysicalMaterialRecord>(new PhysicalMaterialRecord());
const _currGeometry = ref<string>('球');
const _currTexture = ref<Texture>();
const _currTexture = ref<TextureTableRecord>();
const CurrGeometry = computed({
get: () => _currGeometry.value,
set: (val: string) => ChangeGeometry(val)
})
const CurrTexture = computed<Texture>(() => _currTexture.value);
const CurrTexture = computed<TextureTableRecord>(() => _currTexture.value);
const Geometries = ref<string[]>([]);
const Material = computed(() => _material.value);
const CurrentShowObject = computed(() => _editor.ShowObject);
@@ -82,10 +82,30 @@ const sceneSetup = () => {
Update();
}
/** 这是自定义材质更新方法WebCAD库中的TextureTableRecord.TextureUpdate方法在导出的时候被删除了所以需要自己实现 */
function UpdateTexture() {
const record = _currTexture.value;
const texture = record['texture'] as Texture;
texture.wrapS = record.WrapS;
texture.wrapT = record.WrapT;
texture.anisotropy = 16;
texture.rotation = record.rotation;
texture.repeat.set(record.repeatX, record.repeatY);
texture.offset.set(record.moveX, record.moveY);
texture.needsUpdate = true;
Update();
}
async function ChangeTextureFromUrlAsync(url?: string) {
console.warn("Update texture from url:", url);
// 关联贴图
const db = Material.value.Db;
if (!db) return; // 材质未初始化
// 材质未初始化
if (!db) {
console.warn("Material has not been initialized");
return;
}
let record = Material.value.map?.Object as TextureTableRecord;
if (!record) {
// record = db.TextureTable.Symbols.values().next().value;
@@ -99,19 +119,21 @@ const sceneSetup = () => {
// }
}
// record.objectId = new ObjectId(undefined, record);
_currTexture.value = record['texture'] as Texture;
// 设置Store
_currTexture.value = record;
const texture = record['texture'] as Texture;
if (url) {
record.imageUrl = url;
_currTexture.value.image = undefined;
texture.image = undefined;
}
// 设置Store
if (!_currTexture.value.image) {
if (!texture.image) {
const img = await LoadImageFromUrl(GetConfig().host + '/' + record.imageUrl);
_currTexture.value.image = img;
_currTexture.value.needsUpdate = true;
texture.image = img;
}
texture.needsUpdate = true;
UpdateTexture();
await UpdateMaterialAsync();
}
@@ -153,6 +175,7 @@ const sceneSetup = () => {
Material,
Initial,
Update,
UpdateTexture,
UpdateMaterialAsync,
ChangeTextureFromUrlAsync,
SerializeMaterialAsync,