Skip to content

Commit

Permalink
fixed propeller model
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke100000 committed Dec 29, 2024
1 parent fd2091f commit 3c4d85f
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,36 +82,39 @@ public BBCube(JsonObject element, BBModel model) {
double[] v = new double[24];
for (int i = 0; i < 6; i++) {
JsonObject faceObject = element.getAsJsonObject("faces").getAsJsonObject(SIDES[i]);
int id = Utils.getIntElement(element, "texture");
BBTexture texture = model.getTexture(id);

BBFace f = faces.get(i);
f.texture = texture;

float[] uv = new float[4];
Iterator<JsonElement> uvArray = faceObject.getAsJsonArray("uv").iterator();
for (int j = 0; j < 4; j++) {
uv[j] = uvArray.next().getAsFloat();
}
if (!Utils.isNull(faceObject, "texture")) {
int id = Utils.getIntElement(element, "texture");
BBTexture texture = model.getTexture(id);

int rot = Utils.getIntElement(faceObject, "rotation");
while (rot > 0) {
roll(u, i * 4);
roll(v, i * 4);
rot -= 90;
BBFace f = faces.get(i);
f.texture = texture;

float[] uv = new float[4];
Iterator<JsonElement> uvArray = faceObject.getAsJsonArray("uv").iterator();
for (int j = 0; j < 4; j++) {
uv[j] = uvArray.next().getAsFloat();
}

int rot = Utils.getIntElement(faceObject, "rotation");
while (rot > 0) {
roll(u, i * 4);
roll(v, i * 4);
rot -= 90;
}

float textureWidth = model.getTextureWidth(texture);
float textureHeight = model.getTextureHeight(texture);

f.vertices[0].u = uv[0] / textureWidth;
f.vertices[0].v = uv[3] / textureHeight;
f.vertices[1].u = uv[2] / textureWidth;
f.vertices[1].v = uv[3] / textureHeight;
f.vertices[2].u = uv[2] / textureWidth;
f.vertices[2].v = uv[1] / textureHeight;
f.vertices[3].u = uv[0] / textureWidth;
f.vertices[3].v = uv[1] / textureHeight;
}

float textureWidth = model.getTextureWidth(texture);
float textureHeight = model.getTextureHeight(texture);

f.vertices[0].u = uv[0] / textureWidth;
f.vertices[0].v = uv[3] / textureHeight;
f.vertices[1].u = uv[2] / textureWidth;
f.vertices[1].v = uv[3] / textureHeight;
f.vertices[2].u = uv[2] / textureWidth;
f.vertices[2].v = uv[1] / textureHeight;
f.vertices[3].u = uv[0] / textureWidth;
f.vertices[3].v = uv[1] / textureHeight;
}

// Remove degenerate faces
Expand All @@ -134,7 +137,7 @@ public BBCube(JsonObject element, BBModel model) {
private Vector3f[] getPositions() {
Vector3f adjustedFrom = this.from;
Vector3f adjustedTo = this.to;

Vector3f inflate = new Vector3f(this.inflate, this.inflate, this.inflate);
adjustedFrom.sub(inflate);
adjustedTo.add(inflate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,48 +58,50 @@ public BBMesh(JsonObject element, BBModel model) {
element.get("faces").getAsJsonObject().entrySet().forEach(face -> {
JsonObject faceObject = face.getValue().getAsJsonObject();

// Get the texture
int id = Utils.getIntElement(faceObject, "texture");
BBTexture texture = model.getTexture(id);

// Get the uv
Map<String, float[]> uvs = getArrayMap(faceObject.get("uv"), 2);

// Get the vertex identifiers spanning the face
List<String> vertexIdentifiers = new LinkedList<>();
for (JsonElement jsonElement : faceObject.getAsJsonArray("vertices")) {
vertexIdentifiers.add(jsonElement.getAsString());
}

if (vertexIdentifiers.size() != 4) {
Main.LOGGER.warn("Face found, which is not a quad.");
} else {
// Get normal vector
float[] n = getNormal(vertexIdentifiers, positions);
int index = 0;
BBFace.BBVertex[] vertices = new BBFace.BBVertex[4];
for (String identifier : vertexIdentifiers) {
float[] uv = uvs.get(identifier);
float[] pos = positions.get(identifier);

float textureWidth = model.getTextureWidth(texture);
float textureHeight = model.getTextureHeight(texture);

BBFace.BBVertex vd = new BBFace.BBVertex();
vd.x = pos[0] / 16.0f;
vd.y = pos[1] / 16.0f;
vd.z = pos[2] / 16.0f;
vd.nx = n[0];
vd.ny = n[1];
vd.nz = n[2];
vd.u = uv[0] / textureWidth;
vd.v = uv[1] / textureHeight;
vertices[index++] = vd;
if (!Utils.isNull(faceObject, "texture")) {
// Get the texture
int id = Utils.getIntElement(faceObject, "texture");
BBTexture texture = model.getTexture(id);

// Get the uv
Map<String, float[]> uvs = getArrayMap(faceObject.get("uv"), 2);

// Get the vertex identifiers spanning the face
List<String> vertexIdentifiers = new LinkedList<>();
for (JsonElement jsonElement : faceObject.getAsJsonArray("vertices")) {
vertexIdentifiers.add(jsonElement.getAsString());
}

BBFace f = new BBFace(vertices);
f.texture = texture;
this.faces.add(f);
if (vertexIdentifiers.size() != 4) {
Main.LOGGER.warn("Face found, which is not a quad.");
} else {
// Get normal vector
float[] n = getNormal(vertexIdentifiers, positions);
int index = 0;
BBFace.BBVertex[] vertices = new BBFace.BBVertex[4];
for (String identifier : vertexIdentifiers) {
float[] uv = uvs.get(identifier);
float[] pos = positions.get(identifier);

float textureWidth = model.getTextureWidth(texture);
float textureHeight = model.getTextureHeight(texture);

BBFace.BBVertex vd = new BBFace.BBVertex();
vd.x = pos[0] / 16.0f;
vd.y = pos[1] / 16.0f;
vd.z = pos[2] / 16.0f;
vd.nx = n[0];
vd.ny = n[1];
vd.nz = n[2];
vd.u = uv[0] / textureWidth;
vd.v = uv[1] / textureHeight;
vertices[index++] = vd;
}

BBFace f = new BBFace(vertices);
f.texture = texture;
this.faces.add(f);
}
}
});
}
Expand Down
10 changes: 9 additions & 1 deletion common/src/main/java/immersive_aircraft/util/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonPrimitive;
import com.mojang.datafixers.util.Pair;
import com.mojang.math.Quaternion;
import com.mojang.math.Vector3f;
Expand Down Expand Up @@ -90,7 +91,10 @@ public static int getIntElement(JsonObject object, String member, int defaultVal
if (element == null) {
return defaultValue;
}
return element.getAsInt();
if (element instanceof JsonPrimitive primitive && primitive.isNumber()) {
return primitive.getAsInt();
}
return defaultValue;
}

public static float getFloatElement(JsonObject object, String member) {
Expand All @@ -105,6 +109,10 @@ public static float getFloatElement(JsonObject object, String member, float defa
return element.getAsFloat();
}

public static boolean isNull(JsonObject object, String member) {
return object.has(member) && object.get(member).isJsonNull();
}

public static Vector3f parseVector(JsonObject element, String member) {
JsonArray array = element.getAsJsonArray(member);
if (array == null) {
Expand Down

Large diffs are not rendered by default.

Large diffs are not rendered by default.

Large diffs are not rendered by default.

0 comments on commit 3c4d85f

Please sign in to comment.