-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStaticMeshLoader.java
More file actions
127 lines (107 loc) · 4 KB
/
StaticMeshLoader.java
File metadata and controls
127 lines (107 loc) · 4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// from https://github.com/steelswing/assimp4j-52
import jassimp.AiMaterial;
import jassimp.AiMesh;
import jassimp.AiPostProcessSteps;
import jassimp.AiScene;
import jassimp.IHMCJassimp;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
/**
* Copied from https://github.com/steelswing/assimp4j-52
* File: StaticMeshLoader.java
* Created on 30.12.2023, 2:35:16
*
* @author LWJGL2
*/
public class StaticMeshLoader {
public static final Set<AiPostProcessSteps> ASSIMP_POST = new HashSet<AiPostProcessSteps>() {
{
add(AiPostProcessSteps.Triangulate);
add(AiPostProcessSteps.GenSmoothNormals);
add(AiPostProcessSteps.GenUVCoords);
add(AiPostProcessSteps.FlipUVs);
add(AiPostProcessSteps.CalcTangentSpace);
add(AiPostProcessSteps.JoinIdenticalVertices);
add(AiPostProcessSteps.OptimizeMeshes);
}
};
public static List<Mesh> load(File filePath) throws IOException {
List<Mesh> meshes = new ArrayList<>();
AiScene scene = IHMCJassimp.importFile(filePath.getPath(), ASSIMP_POST);
for (AiMesh mesh : scene.getMeshes()) {
meshes.add(loadData(mesh, null));
}
return meshes;
}
private static Mesh loadData(AiMesh mesh, AiMaterial material) {
float[] vertices = new float[mesh.getNumVertices() * 3];
float[] textureCoords = new float[mesh.getNumVertices() * 2];
float[] normals = new float[mesh.getNumVertices() * 3];
int[] indices = new int[mesh.getNumFaces() * 3];
int counter = 0;
for (int v = 0; v < mesh.getNumVertices(); v++) {
vertices[counter++] = mesh.getPositionX(v);
vertices[counter++] = mesh.getPositionY(v);
vertices[counter++] = mesh.getPositionZ(v);
}
counter = 0;
for (int t = 0; t < mesh.getNumVertices(); t++) {
textureCoords[counter++] = mesh.getTexCoordU(t, 0);
textureCoords[counter++] = mesh.getTexCoordV(t, 0);
}
counter = 0;
for (int n = 0; n < mesh.getNumVertices(); n++) {
normals[counter++] = mesh.getNormalX(n);
normals[counter++] = mesh.getNormalY(n);
normals[counter++] = mesh.getNormalZ(n);
}
counter = 0;
for (int f = 0; f < mesh.getNumFaces(); f++) {
indices[counter++] = mesh.getFaceVertex(f, 0);
indices[counter++] = mesh.getFaceVertex(f, 1);
indices[counter++] = mesh.getFaceVertex(f, 2);
}
return new Mesh(vertices, textureCoords, normals, indices, 0, material);
}
public static class Mesh {
protected AiMaterial material;
protected float[] vertices;
protected float[] textureCoords;
protected float[] normals;
protected int[] indices;
protected float furthestPoint;
public Mesh(float[] vertices, float[] textureCoords, float[] normals, int[] indices, float furthestPoint, AiMaterial material) {
this.vertices = vertices;
this.textureCoords = textureCoords;
this.normals = normals;
this.indices = indices;
this.furthestPoint = furthestPoint;
this.material = material;
}
public Mesh(float[] vertices, float[] textureCoords, float[] normals, int[] indices, float furthestPoint) {
this(vertices, textureCoords, normals, indices, furthestPoint, null);
}
public float[] getVertices() {
return vertices;
}
public float[] getTextureCoords() {
return textureCoords;
}
public float[] getNormals() {
return normals;
}
public int[] getIndices() {
return indices;
}
public float getFurthestPoint() {
return furthestPoint;
}
public AiMaterial getMaterial() {
return material;
}
}
}