修复材质导入问题

This commit is contained in:
陈梓阳 2025-05-29 19:00:36 +08:00
parent 26f1c60a43
commit 7e46c010a8
3 changed files with 39 additions and 37 deletions

View File

@ -167,15 +167,17 @@ export class MaterialEditor
private Material: PhysicalMaterialRecord; private Material: PhysicalMaterialRecord;
setMaterial(mat: PhysicalMaterialRecord) setMaterial(mat: PhysicalMaterialRecord)
{ {
console.log("set", mat.Material);
this.Material = mat; this.Material = mat;
this._MeshMaterial.copy(mat.Material); // for (const child of this.ShowObject.children) {
// if (child instanceof Mesh) {
// child.material = mat.Material;
// }
// }
let mtl = this._MeshMaterial; console.log(mat.Material);
console.log("set2", this._MeshMaterial)
this.ShowMesh.material = mtl; this._MeshMaterial.copy(mat.Material);
let mtl = this._MeshMaterial;
if (mtl.metalness > 0.8) if (mtl.metalness > 0.8)
this.LoadMetalEnv().then(env => this.LoadMetalEnv().then(env =>
{ {
@ -214,6 +216,7 @@ export class MaterialEditor
async Update() async Update()
{ {
console.log("copy:", this.Material.Material.metalness);
let mat = this.ShowMesh.material as MeshPhysicalMaterial; let mat = this.ShowMesh.material as MeshPhysicalMaterial;
mat.needsUpdate = true; mat.needsUpdate = true;

View File

@ -38,25 +38,25 @@
<label>金属度</label> <label>金属度</label>
<CfFlex gap="1em"> <CfFlex gap="1em">
<input v-model="Material.matalness" @change="UpdateMaterial" type="range" min="0" max="1" step="0.01" /> <input v-model="Material.matalness" @input="UpdateMaterial" type="range" min="0" max="1" step="0.01" />
<span>{{ Material.matalness }}</span> <span>{{ Material.matalness }}</span>
</CfFlex> </CfFlex>
<label>粗糙度</label> <label>粗糙度</label>
<CfFlex gap="1em"> <CfFlex gap="1em">
<input v-model="Material.roughness" @change="UpdateMaterial" type="range" min="0" max="1" step="0.01" /> <input v-model="Material.roughness" @input="UpdateMaterial" type="range" min="0" max="1" step="0.01" />
<span>{{ Material.roughness }}</span> <span>{{ Material.roughness }}</span>
</CfFlex> </CfFlex>
<label>法线强度</label> <label>法线强度</label>
<CfFlex gap="1em"> <CfFlex gap="1em">
<input v-model="Material.bumpScale" @change="UpdateMaterial" type="range" min="0" max="1" step="0.01" /> <input v-model="Material.bumpScale" @input="UpdateMaterial" type="range" min="0" max="1" step="0.01" />
<span>{{ Material.bumpScale }}</span> <span>{{ Material.bumpScale }}</span>
</CfFlex> </CfFlex>
<label>高光</label> <label>高光</label>
<CfFlex gap="1em"> <CfFlex gap="1em">
<input v-model="Material.specular" @change="UpdateMaterial" type="range" min="0" max="1" step="0.01" /> <input v-model="Material.specular" @input="UpdateMaterial" type="range" min="0" max="1" step="0.01" />
<span>{{ Material.specular }}</span> <span>{{ Material.specular }}</span>
</CfFlex> </CfFlex>
</div> </div>
@ -64,9 +64,9 @@
<div class="adjust-section"> <div class="adjust-section">
<h3>纹理调节</h3> <h3>纹理调节</h3>
<label>启用纹理</label> <label>启用纹理</label>
<input type="checkbox" v-model="enableTexture" /> <input type="checkbox" v-model="Material.useMap" @change="UpdateMaterial" />
<!-- <label>平铺U</label> <label>平铺U</label>
<select v-model="textureAdjustment.wrapS"> <select v-model="textureAdjustment.wrapS">
<option value="0">镜像平铺</option> <option value="0">镜像平铺</option>
<option value="1">平铺</option> <option value="1">平铺</option>
@ -78,7 +78,7 @@
<option value="0">镜像平铺</option> <option value="0">镜像平铺</option>
<option value="1">平铺</option> <option value="1">平铺</option>
<option value="2">展开</option> <option value="2">展开</option>
</select> --> </select>
<label>旋转</label> <label>旋转</label>
<input v-model="textureAdjustment.rotation" type="number" step="0.01" min="0" max="359" /> <input v-model="textureAdjustment.rotation" type="number" step="0.01" min="0" max="359" />
@ -108,9 +108,8 @@
</template> </template>
<script setup lang='ts'> <script setup lang='ts'>
import { ref, reactive, watch, onMounted } from "vue" import { ref, reactive, watch, onMounted, computed } from "vue"
import { useScene, type TextureAdjustment } from "../stores/sceneStore"; import { useScene, type TextureAdjustment } from "../stores/sceneStore";
import { storeToRefs } from "pinia";
import CfFlex from "./CfFlex.vue"; import CfFlex from "./CfFlex.vue";
import { DirectoryId } from "../api/Request"; import { DirectoryId } from "../api/Request";
import { IsNullOrWhitespace } from "../helpers/helper.string"; import { IsNullOrWhitespace } from "../helpers/helper.string";
@ -135,9 +134,11 @@ const emits = defineEmits<{
(e: 'submit', data: MaterialRequest): void; (e: 'submit', data: MaterialRequest): void;
}>(); }>();
const debugMode = ref(true); const Material = computed(() => scene.Material);
const { CurrGeometry, Geometries, Material } = storeToRefs(scene); const CurrGeometry = computed(() => scene.CurrGeometry);
const enableTexture = ref(Material.value.useMap); const Geometries = computed(() => scene.Geometries);
const debugMode = ref(false);
const _textureSrc = ref(props.textureSrc); const _textureSrc = ref(props.textureSrc);
const textureAdjustment = ref<TextureAdjustment>({ const textureAdjustment = ref<TextureAdjustment>({
wrapS: 0, wrapS: 0,
@ -170,11 +171,6 @@ watch(() => props.textureSrc, async (val) => {
await scene.ChangeTextureFromUrlAsync(_textureSrc.value); await scene.ChangeTextureFromUrlAsync(_textureSrc.value);
}); });
watch(enableTexture, async (val) => {
await EnableTexture(val);
});
watch(textureAdjustment, async (val) => { watch(textureAdjustment, async (val) => {
UpdateTexture(); UpdateTexture();
}); });
@ -194,13 +190,18 @@ watch(() => scene.CurrTexture, (val) => {
}) })
async function UpdateMaterial() { async function UpdateMaterial() {
console.log(scene.CurrentShowObject) // console.log((scene.CurrentShowObject.children[0] as Mesh).material.metalness);
// console.log(Material.value.Material.metalness);
// console.log(scene.Material == scene.GetEditor().Material)
// Material.value.matalness = model.metallic; // Material.value.matalness = model.metallic;
// Material.value.roughness = model.roughness; // Material.value.roughness = model.roughness;
// Material.value.bumpScale = model.normalScale; // Material.value.bumpScale = model.normalScale;
// Material.value.specular = model.emissiveIntensity; // Material.value.specular = model.emissiveIntensity;
// Material.value.side = DoubleSide; // Material.value.side = DoubleSide;
await scene.UpdateMaterialAsync(); await scene.UpdateMaterialAsync();
scene.Update();
await scene.UpdateMaterialAsync();
} }
function UpdateTexture() { function UpdateTexture() {
@ -218,11 +219,6 @@ function UpdateTexture() {
scene.Update(); scene.Update();
} }
async function EnableTexture(enable: boolean) {
Material.value.useMap = enable;
await scene.UpdateMaterialAsync();
}
async function loadData() { async function loadData() {
if(!materialInfo.inputText) return; if(!materialInfo.inputText) return;
const json = JSON.parse(materialInfo.inputText); const json = JSON.parse(materialInfo.inputText);

View File

@ -11,6 +11,7 @@ import { GetConfig } from "../lib/libOutputConfig";
const sceneSetup = () => { const sceneSetup = () => {
let _editor: MaterialEditor | undefined; let _editor: MaterialEditor | undefined;
let _database: Database | undefined; let _database: Database | undefined;
const _material = ref<PhysicalMaterialRecord>(new PhysicalMaterialRecord());
const _currGeometry = ref<string>('球'); const _currGeometry = ref<string>('球');
const _currTexture = ref<Texture>(); const _currTexture = ref<Texture>();
const CurrGeometry = computed({ const CurrGeometry = computed({
@ -19,7 +20,7 @@ const sceneSetup = () => {
}) })
const CurrTexture = computed<Texture>(() => _currTexture.value); const CurrTexture = computed<Texture>(() => _currTexture.value);
const Geometries = ref<string[]>([]); const Geometries = ref<string[]>([]);
const Material = ref<PhysicalMaterialRecord>(new PhysicalMaterialRecord()); const Material = computed(() => _material.value);
const CurrentShowObject = computed(() => _editor.ShowObject); const CurrentShowObject = computed(() => _editor.ShowObject);
function Initial(canvas: HTMLElement) { function Initial(canvas: HTMLElement) {
@ -74,15 +75,15 @@ const sceneSetup = () => {
} }
async function UpdateMaterialAsync() { async function UpdateMaterialAsync() {
console.log('UpdateMaterialAsync') console.log(Material.value.Update);
await Material.value.Update(); // TODO: Danger: 如果等待下面这一行,会导致更新卡住(未知问题)
Material.value.Update();
// Material.value.Material.needsUpdate = true;
await _editor.Update(); await _editor.Update();
Update(); Update();
} }
async function ChangeTextureFromUrlAsync(url?: string) { async function ChangeTextureFromUrlAsync(url?: string) {
console.log('ChangeTextureFromUrlAsync')
// 关联贴图 // 关联贴图
const db = Material.value.Db; const db = Material.value.Db;
if (!db) return; // 材质未初始化 if (!db) return; // 材质未初始化
@ -123,9 +124,10 @@ const sceneSetup = () => {
async function ImportMaterialAsync(materialJson: string) { async function ImportMaterialAsync(materialJson: string) {
const material = MaterialIn(JSON.parse(materialJson)); const material = MaterialIn(JSON.parse(materialJson));
Material.value = material; _material.value = material;
_editor.setMaterial(material); _editor.setMaterial(_material.value as PhysicalMaterialRecord);
await ChangeTextureFromUrlAsync(); await ChangeTextureFromUrlAsync();
await UpdateMaterialAsync();
} }
async function GenerateMaterialLogoAsync() { async function GenerateMaterialLogoAsync() {
@ -158,7 +160,8 @@ const sceneSetup = () => {
ImportMaterialAsync, ImportMaterialAsync,
GenerateMaterialLogoAsync, GenerateMaterialLogoAsync,
Dispose, Dispose,
CurrentShowObject CurrentShowObject,
GetEditor: () => _editor
}; };
}; };