Skip to content

Commit

Permalink
disable LODs since they are broken and never used
Browse files Browse the repository at this point in the history
- JME's LodControl is not suitable, even with lowered thresholds
- also sets levels independently for each submesh
- a mesh split among subgroups due to number of triangles isn't handled
  • Loading branch information
Trass3r committed Feb 2, 2025
1 parent 2e082f5 commit 75ffc50
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 25 deletions.
2 changes: 1 addition & 1 deletion src/toniarts/openkeeper/tools/convert/AssetsConverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public abstract class AssetsConverter implements IConversionTaskUpdate {
public enum ConvertProcess {

TEXTURES(7, new ConvertProcess[]{}),
MODELS(10, new ConvertProcess[]{TEXTURES}),
MODELS(11, new ConvertProcess[]{TEXTURES}),
MOUSE_CURSORS(4, new ConvertProcess[]{}),
MUSIC_AND_SOUNDS(4, new ConvertProcess[]{}),
INTERFACE_TEXTS(3, new ConvertProcess[]{}),
Expand Down
55 changes: 31 additions & 24 deletions src/toniarts/openkeeper/tools/convert/KmfModelLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
import com.jme3.scene.Node;
import com.jme3.scene.VertexBuffer;
import com.jme3.scene.VertexBuffer.Type;
import com.jme3.scene.control.LodControl;
import com.jme3.texture.Texture;
import com.jme3.util.BufferUtils;
import com.jme3.util.mikktspace.MikktspaceTangentGenerator;
Expand All @@ -64,7 +63,6 @@
import toniarts.openkeeper.tools.convert.kmf.Anim;
import toniarts.openkeeper.tools.convert.kmf.Grop;
import toniarts.openkeeper.tools.convert.kmf.KmfFile;
import toniarts.openkeeper.tools.convert.kmf.MeshSprite;
import toniarts.openkeeper.tools.convert.kmf.MeshVertex;
import toniarts.openkeeper.tools.convert.kmf.Triangle;
import toniarts.openkeeper.tools.convert.kmf.Uv;
Expand Down Expand Up @@ -185,17 +183,20 @@ private Node handleMesh(toniarts.openkeeper.tools.convert.kmf.Mesh sourceMesh, M
node.setLocalTranslation(new Vector3f(sourceMesh.getPos().x, -sourceMesh.getPos().z, sourceMesh.getPos().y));

int index = 0;
for (MeshSprite meshSprite : sourceMesh.getSprites()) {
for (var subMesh : sourceMesh.getSprites()) {

if (subMesh.getTriangles().get(0).isEmpty())
continue; // FIXME: LODs are broken so we only take L0

//Each sprite represents a geometry (+ mesh) since they each have their own material
Mesh mesh = new Mesh();

//Vertices, UV (texture coordinates), normals
Vector3f[] vertices = new Vector3f[meshSprite.getVertices().size()];
Vector2f[] texCoord = new Vector2f[meshSprite.getVertices().size()];
Vector3f[] normals = new Vector3f[meshSprite.getVertices().size()];
var vertices = new Vector3f[subMesh.getVertices().size()];
var texCoord = new Vector2f[subMesh.getVertices().size()];
var normals = new Vector3f[subMesh.getVertices().size()];
int i = 0;
for (MeshVertex meshVertex : meshSprite.getVertices()) {
for (MeshVertex meshVertex : subMesh.getVertices()) {

//Vertice
javax.vecmath.Vector3f v = sourceMesh.getGeometries().get(meshVertex.getGeomIndex());
Expand All @@ -212,19 +213,19 @@ private Node handleMesh(toniarts.openkeeper.tools.convert.kmf.Mesh sourceMesh, M
i++;
}

// Triangles, we have LODs here
VertexBuffer[] lodLevels = createIndices(meshSprite.getTriangles());
// Create LOD levels
// FIXME: LODs are broken so we only take L0
var lodLevels = createIndices(subMesh.getTriangles().subList(0, 1));
mesh.setBuffer(lodLevels[0]);
// mesh.setLodLevels(lodLevels); // needs to include L0!

//Set the buffers
mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
mesh.setBuffer(lodLevels[0]);
mesh.setLodLevels(lodLevels);
mesh.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
mesh.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normals));
mesh.setStatic();

// Create geometry
Geometry geom = createGeometry(index, sourceMesh.getName(), mesh, materials, meshSprite.getMaterialIndex());
Geometry geom = createGeometry(index, sourceMesh.getName(), mesh, materials, subMesh.getMaterialIndex());

//Attach the geometry to the node
node.attachChild(geom);
Expand Down Expand Up @@ -259,6 +260,9 @@ private Node handleAnim(Anim anim, Map<Integer, List<Material>> materials) {
int subMeshIndex = 0;
for (var subMesh : anim.getSprites()) {

if (subMesh.getTriangles().get(0).isEmpty())
continue; // FIXME: LODs are broken so we only take L0

// Animation
// Poses for each key frame (aproximate that every 1/3 is a key frame, pessimistic)
// Note that a key frame may not have all the vertices
Expand Down Expand Up @@ -424,17 +428,20 @@ private Node handleAnim(Anim anim, Map<Integer, List<Material>> materials) {
}

// Create LOD levels
var lodLevels = createIndices(subMesh.getTriangles());

//Set the buffers
mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));
mesh.setBuffer(Type.BindPosePosition, 3, BufferUtils.createFloatBuffer(vertices));
// FIXME: LODs are broken so we only take L0
var lodLevels = createIndices(subMesh.getTriangles().subList(0, 1));
mesh.setBuffer(lodLevels[0]);
mesh.setLodLevels(lodLevels);
//mesh.setLodLevels(lodLevels); // needs to include L0!

mesh.setBuffer(Type.TexCoord, 2, BufferUtils.createFloatBuffer(texCoord));
// used by PoseTrack
mesh.setBuffer(Type.BindPosePosition, 3, BufferUtils.createFloatBuffer(vertices));
// no BindPoseNormal! not animated in KMF
mesh.setBuffer(Type.Normal, 3, BufferUtils.createFloatBuffer(normals));
mesh.setBuffer(Type.BindPoseNormal, 3, BufferUtils.createFloatBuffer(normals));
mesh.setStreamed();

// only position buffer is dynamic
mesh.setStatic();
mesh.setBuffer(Type.Position, 3, BufferUtils.createFloatBuffer(vertices));

// Create geometry
Geometry geom = createGeometry(subMeshIndex, anim.getName(), mesh, materials, subMesh.getMaterialIndex());
Expand Down Expand Up @@ -513,9 +520,9 @@ private Geometry createGeometry(int subMeshIndex, String meshName, Mesh mesh, Ma
//Create geometry
var geom = new Geometry(meshName + '_' + subMeshIndex, mesh);

//Add LOD control
LodControl lc = new LodControl();
geom.addControl(lc);
// LODs are broken
//var lc = new LodControl();
//geom.addControl(lc);

// Material, set the first
geom.setMaterial(materials.get(materialIndex).get(0));
Expand Down

0 comments on commit 75ffc50

Please sign in to comment.