This commit is contained in:
2026-05-06 12:16:29 +08:00
parent 4207fcf7c2
commit 6cefd063f2
5 changed files with 26 additions and 54 deletions

View File

@ -545,7 +545,7 @@
const modelName = kernel.model.findModelNameByMesh?.(pickedMesh); const modelName = kernel.model.findModelNameByMesh?.(pickedMesh);
if (modelName) { if (modelName) {
console.log('旋转90度模型名:', modelName); console.log('旋转90度模型名:', modelName);
kernel.transform.addRotation({ kernel.transform.rotation({
modelId: modelName, modelId: modelName,
vector3: { x: 0, y: 90, z: 0 } vector3: { x: 0, y: 90, z: 0 }
}); });
@ -564,9 +564,9 @@
const modelName = kernel.model.findModelNameByMesh?.(pickedMesh); const modelName = kernel.model.findModelNameByMesh?.(pickedMesh);
if (modelName) { if (modelName) {
console.log('旋转180度模型名:', modelName); console.log('旋转180度模型名:', modelName);
kernel.transform.addRotation({ kernel.transform.rotation({
modelId: modelName, modelId: modelName,
vector3: { x: 0, y: 180, z: 0 } vector3: { x: 0, y: 30, z: 0 }
}); });
} else { } else {
console.log('未找到模型名称'); console.log('未找到模型名称');

View File

@ -29,7 +29,8 @@ const config = {
kernel.init(config); kernel.init(config);
kernel.model.add({ kernel.model.add({
modelId: "框架", modelId: "框架",
modelUrl: "https://sdk.zguiy.com/resurces/model/框架.glb" modelUrl: "https://sdk.zguiy.com/resurces/model/框架.glb",
modelControlType: "color"
}); });
kernel.model.add({ kernel.model.add({
modelId: "卷帘大", modelId: "卷帘大",

View File

@ -145,15 +145,6 @@ export class AppModel extends Monobehiver {
* @param meshes 网格数组 * @param meshes 网格数组
* @param modelName 模型名称 * @param modelName 模型名称
*/ */
private cloneMaterials(meshes: AbstractMesh[], modelName: string): void {
meshes.forEach(mesh => {
if (mesh.material) {
const originalMaterial = mesh.material;
const clonedMaterial = originalMaterial.clone(`${originalMaterial.name}_${modelName}`);
mesh.material = clonedMaterial;
}
});
}
/** 为网格设置阴影(投射和接收) */ /** 为网格设置阴影(投射和接收) */
private createModelRoot(modelId: string, meshes: AbstractMesh[]): AbstractMesh[] { private createModelRoot(modelId: string, meshes: AbstractMesh[]): AbstractMesh[] {
const scene = this.mainApp.appScene.object; const scene = this.mainApp.appScene.object;
@ -252,7 +243,6 @@ export class AppModel extends Monobehiver {
}); });
if (result.success && result.meshes) { if (result.success && result.meshes) {
// this.cloneMaterials(result.meshes, modelName);
result.meshes = this.createModelRoot(modelName, result.meshes); result.meshes = this.createModelRoot(modelName, result.meshes);
this.modelDic.Set(modelName, result.meshes); this.modelDic.Set(modelName, result.meshes);
@ -292,7 +282,6 @@ export class AppModel extends Monobehiver {
if (result.success && result.meshes) { if (result.success && result.meshes) {
result.meshes = this.createModelRoot(modelId, result.meshes); result.meshes = this.createModelRoot(modelId, result.meshes);
this.cloneMaterials(result.meshes, modelId);
this.modelDic.Set(modelId, result.meshes); this.modelDic.Set(modelId, result.meshes);
// 存储元数据 // 存储元数据
@ -436,9 +425,10 @@ export class AppModel extends Monobehiver {
return; return;
} }
meshes.forEach(mesh => mesh.dispose()); this.getModelTransformTargets(meshes).forEach(mesh => mesh.dispose(false, true));
this.modelDic.Remove(modelName); this.modelDic.Remove(modelName);
this.modelMetadataDic.Remove(modelName); this.modelMetadataDic.Remove(modelName);
this.mainApp.gameManager?.updateDictionaries();
console.log(`Model removed: ${modelName}`); console.log(`Model removed: ${modelName}`);
} }

View File

@ -88,18 +88,26 @@ export class GameManager extends Monobehiver {
const scene = this.mainApp.appScene?.object; const scene = this.mainApp.appScene?.object;
if (!scene) return; if (!scene) return;
this.materialDic.Clear();
this.meshDic.Clear();
// 更新材质字典 // 更新材质字典
for (const mat of scene.materials) { for (const mat of scene.materials) {
if (!this.materialDic.Has(mat.name)) { if (!(mat as any)._isDisposed && !this.materialDic.Has(mat.name)) {
this.materialDic.Set(mat.name, mat as PBRMaterial); this.materialDic.Set(mat.name, mat as PBRMaterial);
} }
} }
// 更新网格字典 // 更新网格字典
for (const mesh of scene.meshes) { for (const mesh of scene.meshes) {
if (mesh instanceof Mesh && !this.meshDic.Has(mesh.name)) { if (mesh instanceof Mesh && !mesh.isDisposed() && !this.meshDic.Has(mesh.name)) {
this.meshDic.Set(mesh.name, mesh); this.meshDic.Set(mesh.name, mesh);
} }
const mat = mesh.material;
if (mat instanceof PBRMaterial && !(mat as any)._isDisposed) {
this.materialDic.Set(mat.name, mat);
}
} }
} }
@ -742,17 +750,15 @@ export class GameManager extends Monobehiver {
* @param target 目标对象 * @param target 目标对象
* @param material 材质路径 * @param material 材质路径
*/ */
applyMaterial(target: string, attribute: string, value: number | string): void { applyMaterial(target: string, attribute: string, value:string): void {
// 这里需要根据实际的材质管理逻辑实现 if (attribute !== 'baseColor' || typeof value !== 'string') return;
console.log(`Applying attribute ${attribute} to ${value}`); this.updateDictionaries();
console.log(this.materialDic);
// 示例实现:根据目标和材质路径应用材质 // 示例实现:根据目标和材质路径应用材质
// 1. 查找目标网格 // 1. 查找目标网格
const targetMaterials: PBRMaterial[] = []; const targetMaterials: PBRMaterial[] = [];
this.materialDic.Values().forEach(material => { this.materialDic.Values().forEach(material => {
if (material.name === target) { if (material.name === target) {
console.log(`${this.materialDic.Get(material.name)}`, material);
targetMaterials.push(material); targetMaterials.push(material);
} }
}); });
@ -767,30 +773,15 @@ console.log(this.materialDic);
// 例如paint/blue 可以映射到特定的材质配置 // 例如paint/blue 可以映射到特定的材质配置
// 3. 应用材质到目标网格 // 3. 应用材质到目标网格
targetMaterials.forEach(material => {
if (attribute === 'baseColor' && typeof value === 'string') {
// 如果是 baseColor 且值是字符串16进制颜色转换为 Color3
const color = Color3.FromHexString(value); const color = Color3.FromHexString(value);
targetMaterials.forEach(material => {
console.log(`Before: albedoColor =`, material.albedoColor); // 如果是 baseColor 且值是字符串16进制颜色转换为 Color3
console.log(`Before: albedoTexture =`, material.albedoTexture); material.albedoColor.copyFrom(color);
material.albedoColor = color;
// 如果有纹理,颜色会作为纹理的乘法因子 // 如果有纹理,颜色会作为纹理的乘法因子
if (material.albedoTexture) {
console.log(`Material ${material.name} has albedoTexture, color will tint the texture`);
}
// 强制刷新材质 // 强制刷新材质
material.markDirty(); material.markDirty();
console.log(`After: albedoColor =`, material.albedoColor);
console.log(`Applying baseColor ${value} to material: ${material.name}`, color);
} else if (material[attribute]) {
material[attribute] = value;
console.log(`Applying attribute ${attribute} to ${value} to mesh: ${material.name}`);
}
}); });
} }
} }

View File

@ -66,7 +66,7 @@ export class KernelAdapter {
* 应用材质 * 应用材质
* @param options 材质应用选项 * @param options 材质应用选项
*/ */
apply: (options: { target: string; attribute: string,value:number|string }): void => { apply: (options: { target: string; attribute: string,value:string }): void => {
this.mainApp.gameManager.applyMaterial(options.target, options.attribute,options.value); this.mainApp.gameManager.applyMaterial(options.target, options.attribute,options.value);
}, },
/** /**
@ -150,17 +150,7 @@ export class KernelAdapter {
/** 模型变换管理 */ /** 模型变换管理 */
transform = { transform = {
/** /**
* 设置模型旋转
* @param options 旋转配置 { modelId: string, vector3: { x, y, z }, useDegrees?: boolean }
* @example
* // 使用角度(默认)
* kernel.transform.rotation({ modelId: "model1", vector3: { x: 0, y: 90, z: 0 } });
* // 使用弧度
* kernel.transform.rotation({ modelId: "model1", vector3: { x: 0, y: Math.PI / 2, z: 0 }, useDegrees: false });
*/
rotation: (options: { modelId: string; vector3: { x: number; y: number; z: number }; useDegrees?: boolean }): void => {
this.mainApp.appModel.setRotation(options.modelId, options.vector3, options.useDegrees !== false);
},
/** /**
* 累加模型旋转 * 累加模型旋转
* @param options 旋转配置 { modelId: string, vector3: { x, y, z }, useDegrees?: boolean } * @param options 旋转配置 { modelId: string, vector3: { x, y, z }, useDegrees?: boolean }
@ -168,7 +158,7 @@ export class KernelAdapter {
* // 使用角度(默认) * // 使用角度(默认)
* kernel.transform.addRotation({ modelId: "model1", vector3: { x: 0, y: 90, z: 0 } }); * kernel.transform.addRotation({ modelId: "model1", vector3: { x: 0, y: 90, z: 0 } });
*/ */
addRotation: (options: { modelId: string; vector3: { x: number; y: number; z: number }; useDegrees?: boolean }): void => { rotation: (options: { modelId: string; vector3: { x: number; y: number; z: number }; useDegrees?: boolean }): void => {
this.mainApp.appModel.addRotation(options.modelId, options.vector3, options.useDegrees !== false); this.mainApp.appModel.addRotation(options.modelId, options.vector3, options.useDegrees !== false);
}, },
/** /**