refactor(model):优化网格序列化逻辑并修复测试文件路径
- 使用Set避免重复序列化网格数据 - 在模型加载时自动补充缺失的网格引用 - 更新测试文件路径至统一的testing.model - 移除冗余的部件位置设置代码
This commit is contained in:
@@ -323,19 +323,23 @@ public class ModelData implements Serializable {
|
|||||||
private void serializeMeshes(Model2D model) {
|
private void serializeMeshes(Model2D model) {
|
||||||
meshes.clear();
|
meshes.clear();
|
||||||
|
|
||||||
// 首先序列化模型级别的所有网格
|
// 使用Set记录已序列化的网格名称,避免重复
|
||||||
|
Set<String> serializedMeshNames = new HashSet<>();
|
||||||
|
|
||||||
|
// 先序列化模型级别的网格
|
||||||
for (Mesh2D mesh : model.getMeshes()) {
|
for (Mesh2D mesh : model.getMeshes()) {
|
||||||
if (!meshExists(mesh.getName())) {
|
if (!serializedMeshNames.contains(mesh.getName())) {
|
||||||
meshes.add(new MeshData(mesh));
|
meshes.add(new MeshData(mesh));
|
||||||
|
serializedMeshNames.add(mesh.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 然后序列化所有部件中的网格
|
// 再序列化部件中的网格(去重)
|
||||||
for (ModelPart part : model.getParts()) {
|
for (ModelPart part : model.getParts()) {
|
||||||
for (Mesh2D mesh : part.getMeshes()) {
|
for (Mesh2D mesh : part.getMeshes()) {
|
||||||
// 检查是否已经序列化过(避免重复)
|
if (!serializedMeshNames.contains(mesh.getName())) {
|
||||||
if (!meshExists(mesh.getName())) {
|
|
||||||
meshes.add(new MeshData(mesh));
|
meshes.add(new MeshData(mesh));
|
||||||
|
serializedMeshNames.add(mesh.getName());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -502,6 +506,12 @@ public class ModelData implements Serializable {
|
|||||||
for (PartData partData : parts) {
|
for (PartData partData : parts) {
|
||||||
ModelPart part = partData.toModelPart(meshMap);
|
ModelPart part = partData.toModelPart(meshMap);
|
||||||
partMap.put(part.getName(), part);
|
partMap.put(part.getName(), part);
|
||||||
|
|
||||||
|
for (Mesh2D mesh : part.getMeshes()) {
|
||||||
|
if (!model.getMeshes().contains(mesh)) {
|
||||||
|
model.addMesh(mesh);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. 建立父子关系
|
// 2. 建立父子关系
|
||||||
|
|||||||
@@ -142,6 +142,7 @@ public class ModelLayerPanelTest {
|
|||||||
renderPanel.dispose();
|
renderPanel.dispose();
|
||||||
} catch (Throwable ignored) {
|
} catch (Throwable ignored) {
|
||||||
}
|
}
|
||||||
|
model.saveToFile("C:\\Users\\Administrator\\Desktop\\testing.model");
|
||||||
System.exit(0);
|
System.exit(0);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ public class ModelLoadTest {
|
|||||||
private boolean enableWireframe = false;
|
private boolean enableWireframe = false;
|
||||||
|
|
||||||
// 要加载的 model 文件路径(请根据实际保存位置修改)
|
// 要加载的 model 文件路径(请根据实际保存位置修改)
|
||||||
private static final String MODEL_PATH = "C:\\Users\\Administrator\\Desktop\\trump_texture.model";
|
private static final String MODEL_PATH = "C:\\Users\\Administrator\\Desktop\\testing.model";
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
new ModelLoadTest().run();
|
new ModelLoadTest().run();
|
||||||
@@ -318,20 +318,6 @@ public class ModelLoadTest {
|
|||||||
} catch (Throwable ignored) {
|
} catch (Throwable ignored) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 如果 parts 不为空,尝试把第一个 part 放到窗口中心(如果有 setPosition)
|
|
||||||
if (!parts.isEmpty()) {
|
|
||||||
Object first = parts.get(0);
|
|
||||||
try {
|
|
||||||
Method setPosition = tryGetMethod(first.getClass(), "setPosition", float.class, float.class);
|
|
||||||
if (setPosition != null) {
|
|
||||||
setPosition.invoke(first, (float) WINDOW_WIDTH / 2f, (float) WINDOW_HEIGHT / 2f);
|
|
||||||
System.out.println("Moved first part to window center.");
|
|
||||||
}
|
|
||||||
} catch (Throwable t) {
|
|
||||||
// ignore
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user