Skip to content

Commit 4fa21a8

Browse files
committed
fix: 3D models in .obj format without normals are now loaded correctly, related to #1245
1 parent 590f25b commit 4fa21a8

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

fxgl/src/main/kotlin/com/almasb/fxgl/scene3d/obj/ObjData.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ package com.almasb.fxgl.scene3d.obj
99
import javafx.scene.paint.Color
1010
import javafx.scene.paint.Material
1111
import javafx.scene.paint.PhongMaterial
12+
import javafx.scene.shape.VertexFormat
1213
import java.net.URL
1314

1415
/**
@@ -52,6 +53,8 @@ internal class SubGroup {
5253
var ambientColor: Color? = null
5354

5455
var smoothingGroup = -1
56+
57+
var vertexFormat = VertexFormat.POINT_TEXCOORD
5558
}
5659

5760
internal class MtlData(

fxgl/src/main/kotlin/com/almasb/fxgl/scene3d/obj/ObjModelLoader.kt

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,14 +99,14 @@ class ObjModelLoader : Model3DLoader {
9999
// f v1
100100
1 -> {
101101
data.currentGroup.currentSubGroup.faces += faceVertex[0].toInt() - 1
102-
data.currentGroup.currentSubGroup.faces += 0
102+
// add vt1 as 0
103103
data.currentGroup.currentSubGroup.faces += 0
104104
}
105105

106106
// f v1/vt1
107107
2 -> {
108108
data.currentGroup.currentSubGroup.faces += faceVertex[0].toInt() - 1
109-
data.currentGroup.currentSubGroup.faces += 0
109+
// add vt1
110110
data.currentGroup.currentSubGroup.faces += faceVertex[1].toInt() - 1
111111
}
112112

@@ -115,7 +115,9 @@ class ObjModelLoader : Model3DLoader {
115115
3 -> {
116116
data.currentGroup.currentSubGroup.faces += faceVertex[0].toInt() - 1
117117
data.currentGroup.currentSubGroup.faces += faceVertex[2].toInt() - 1
118+
// add vt1 if present, else 0
118119
data.currentGroup.currentSubGroup.faces += (faceVertex[1].toIntOrNull() ?: 1) - 1
120+
data.currentGroup.currentSubGroup.vertexFormat = VertexFormat.POINT_NORMAL_TEXCOORD
119121
}
120122
}
121123
}
@@ -237,7 +239,7 @@ class ObjModelLoader : Model3DLoader {
237239
// TODO: ?
238240
if (!it.faces.isEmpty()) {
239241

240-
val mesh = TriangleMesh(VertexFormat.POINT_NORMAL_TEXCOORD)
242+
val mesh = TriangleMesh(it.vertexFormat)
241243

242244
mesh.points.addAll(*data.vertices.map { it * 0.05f }.toFloatArray())
243245

@@ -248,11 +250,13 @@ class ObjModelLoader : Model3DLoader {
248250
mesh.texCoords.addAll(*data.vertexTextures.toFloatArray())
249251
}
250252

251-
// if there are no vertex normals, just add 3 values
252-
if (data.vertexNormals.isEmpty()) {
253-
mesh.normals.addAll(*FloatArray(3) { _ -> 0.0f })
254-
} else {
255-
mesh.normals.addAll(*data.vertexNormals.toFloatArray())
253+
if (it.vertexFormat === VertexFormat.POINT_NORMAL_TEXCOORD) {
254+
// if there are no vertex normals, just add 3 values
255+
if (data.vertexNormals.isEmpty()) {
256+
mesh.normals.addAll(*FloatArray(3) { _ -> 0.0f })
257+
} else {
258+
mesh.normals.addAll(*data.vertexNormals.toFloatArray())
259+
}
256260
}
257261

258262
mesh.faces.addAll(*it.faces.toIntArray())

0 commit comments

Comments
 (0)