Fix rail button screw normals

This commit is contained in:
SiboVG 2023-08-14 03:34:29 +02:00
parent 7ebed840b6
commit 2ac695955e
2 changed files with 40 additions and 21 deletions

View File

@ -51,18 +51,19 @@ public class RailButtonExporter extends RocketComponentExporter<RailButton> {
float screwHeight, InstanceContext context) { float screwHeight, InstanceContext context) {
final int startIdx = obj.getNumVertices(); final int startIdx = obj.getNumVertices();
final int normalStartIdx = obj.getNumNormals(); final int normalStartIdx = obj.getNumNormals();
final int nrOfSides = LOD.getNrOfSides(outerRadius);
// Generate base cylinder // Generate base cylinder
List<Integer> baseCylinderForeVertices = new ArrayList<>(); List<Integer> baseCylinderForeVertices = new ArrayList<>();
List<Integer> baseCylinderAftVertices = new ArrayList<>(); List<Integer> baseCylinderAftVertices = new ArrayList<>();
CylinderExporter.addCylinderMesh(obj, transformer, null, outerRadius, baseHeight, false, LOD, CylinderExporter.addCylinderMesh(obj, transformer, null, outerRadius, baseHeight, false, nrOfSides,
baseCylinderForeVertices, baseCylinderAftVertices); baseCylinderForeVertices, baseCylinderAftVertices);
// Generate inner cylinder // Generate inner cylinder
int tmpStartIdx = obj.getNumVertices(); int tmpStartIdx = obj.getNumVertices();
List<Integer> innerCylinderForeVertices = new ArrayList<>(); List<Integer> innerCylinderForeVertices = new ArrayList<>();
List<Integer> innerCylinderAftVertices = new ArrayList<>(); List<Integer> innerCylinderAftVertices = new ArrayList<>();
CylinderExporter.addCylinderMesh(obj, transformer, null, innerRadius, innerHeight, false, LOD, CylinderExporter.addCylinderMesh(obj, transformer, null, innerRadius, innerHeight, false, nrOfSides,
innerCylinderForeVertices, innerCylinderAftVertices); innerCylinderForeVertices, innerCylinderAftVertices);
int tmpEndIdx = Math.max(obj.getNumVertices() - 1, tmpStartIdx); int tmpEndIdx = Math.max(obj.getNumVertices() - 1, tmpStartIdx);
FloatTuple locOffs = transformer.convertLocWithoutOriginOffs(baseHeight, 0, 0); FloatTuple locOffs = transformer.convertLocWithoutOriginOffs(baseHeight, 0, 0);
@ -72,8 +73,9 @@ public class RailButtonExporter extends RocketComponentExporter<RailButton> {
tmpStartIdx = obj.getNumVertices(); tmpStartIdx = obj.getNumVertices();
List<Integer> flangeCylinderForeVertices = new ArrayList<>(); List<Integer> flangeCylinderForeVertices = new ArrayList<>();
List<Integer> flangeCylinderAftVertices = new ArrayList<>(); List<Integer> flangeCylinderAftVertices = new ArrayList<>();
CylinderExporter.addCylinderMesh(obj, transformer, null, outerRadius, flangeHeight, false, LOD, List<Integer> flangeCylinderAftNormals = new ArrayList<>();
flangeCylinderForeVertices, flangeCylinderAftVertices); CylinderExporter.addCylinderMesh(obj, transformer, null, outerRadius, outerRadius, flangeHeight, nrOfSides,
false, true, flangeCylinderForeVertices, flangeCylinderAftVertices, null, flangeCylinderAftNormals);
tmpEndIdx = Math.max(obj.getNumVertices() - 1, tmpStartIdx); tmpEndIdx = Math.max(obj.getNumVertices() - 1, tmpStartIdx);
locOffs = transformer.convertLocWithoutOriginOffs(baseHeight + innerHeight, 0, 0); locOffs = transformer.convertLocWithoutOriginOffs(baseHeight + innerHeight, 0, 0);
ObjUtils.translateVertices(obj, tmpStartIdx, tmpEndIdx, locOffs.getX(), locOffs.getY(), locOffs.getZ()); ObjUtils.translateVertices(obj, tmpStartIdx, tmpEndIdx, locOffs.getX(), locOffs.getY(), locOffs.getZ());
@ -91,7 +93,8 @@ public class RailButtonExporter extends RocketComponentExporter<RailButton> {
if (Float.compare(screwHeight, 0) == 0) { if (Float.compare(screwHeight, 0) == 0) {
DiskExporter.closeDiskMesh(obj, transformer, null, flangeCylinderAftVertices, false, false); DiskExporter.closeDiskMesh(obj, transformer, null, flangeCylinderAftVertices, false, false);
} else { } else {
addScrew(obj, baseHeight, innerHeight, flangeHeight, outerRadius, screwHeight, LOD, flangeCylinderAftVertices); addScrew(obj, baseHeight, innerHeight, flangeHeight, outerRadius, screwHeight, LOD,
flangeCylinderAftVertices, flangeCylinderAftNormals);
} }
@ -123,15 +126,20 @@ public class RailButtonExporter extends RocketComponentExporter<RailButton> {
} }
private void addScrew(DefaultObj obj, float baseHeight, float innerHeight, float flangeHeight, float outerRadius, private void addScrew(DefaultObj obj, float baseHeight, float innerHeight, float flangeHeight, float outerRadius,
float screwHeight, ObjUtils.LevelOfDetail LOD, List<Integer> flangeCylinderTopVertices) { float screwHeight, ObjUtils.LevelOfDetail LOD,
List<Integer> flangeCylinderAftVertices, List<Integer> flangeCylinderAftNormals) {
final int nrOfStacks = LOD.getValue() / 10; final int nrOfStacks = LOD.getValue() / 10;
final int nrOfSlices = flangeCylinderTopVertices.size(); final int nrOfSlices = flangeCylinderAftVertices.size();
final int startIdx = obj.getNumVertices(); final int startIdx = obj.getNumVertices();
final int normalStartIdx = obj.getNumNormals(); final int normalStartIdx = obj.getNumNormals();
final float buttonHeight = baseHeight + innerHeight + flangeHeight; final float buttonHeight = baseHeight + innerHeight + flangeHeight;
final float centerX = buttonHeight; final float centerX = buttonHeight;
if (flangeCylinderAftNormals.size() != nrOfSlices) {
throw new IllegalArgumentException("The number of normals must be equal to the number of slices");
}
// Generate the mesh vertices (no tip) // Generate the mesh vertices (no tip)
for (int i = 1; i <= nrOfStacks-1; i++) { for (int i = 1; i <= nrOfStacks-1; i++) {
for (int j = 0; j < nrOfSlices; j++) { for (int j = 0; j < nrOfSlices; j++) {
@ -158,16 +166,16 @@ public class RailButtonExporter extends RocketComponentExporter<RailButton> {
for (int i = 0; i < nrOfSlices; i++) { for (int i = 0; i < nrOfSlices; i++) {
int nextIdx = (i + 1) % nrOfSlices; int nextIdx = (i + 1) % nrOfSlices;
int[] vertexIndices = new int[] { int[] vertexIndices = new int[] {
flangeCylinderTopVertices.get(i), // Bottom-left of quad flangeCylinderAftVertices.get(i), // Bottom-left of quad
startIdx + i, // Top-left of quad startIdx + i, // Top-left of quad
startIdx + nextIdx, // Top-right of quad startIdx + nextIdx, // Top-right of quad
flangeCylinderTopVertices.get(nextIdx), // Bottom-right of quad flangeCylinderAftVertices.get(nextIdx), // Bottom-right of quad
}; };
int[] normalIndices = new int[] { int[] normalIndices = new int[] {
flangeCylinderTopVertices.get(i), // Bottom-left of quad flangeCylinderAftNormals.get(i), // Bottom-left of quad
normalStartIdx + i, // Top-left of quad normalStartIdx + i, // Top-left of quad
normalStartIdx + nextIdx, // Top-right of quad normalStartIdx + nextIdx, // Top-right of quad
flangeCylinderTopVertices.get(nextIdx), // Bottom-right of quad flangeCylinderAftNormals.get(nextIdx), // Bottom-right of quad
}; };
// No need to offset! We already directly reference the indices // No need to offset! We already directly reference the indices

View File

@ -24,10 +24,13 @@ public class CylinderExporter {
* @param isOutside Whether the cylinder is an outside face (true) or inside face (false) * @param isOutside Whether the cylinder is an outside face (true) or inside face (false)
* @param foreRingVertices A list to add the fore (top) ring vertex indices to * @param foreRingVertices A list to add the fore (top) ring vertex indices to
* @param aftRingVertices A list to add the aft (bottom) ring vertex indices to * @param aftRingVertices A list to add the aft (bottom) ring vertex indices to
* @param foreRingNormals A list to add the fore (top) ring normal indices to
* @param aftRingNormals A list to add the aft (bottom) ring normal indices to
*/ */
public static void addCylinderMesh(@NotNull DefaultObj obj, @NotNull CoordTransform transformer, String groupName, public static void addCylinderMesh(@NotNull DefaultObj obj, @NotNull CoordTransform transformer, String groupName,
float foreRadius, float aftRadius, float length, int numSides, boolean solid, boolean isOutside, float foreRadius, float aftRadius, float length, int numSides, boolean solid, boolean isOutside,
List<Integer> foreRingVertices, List<Integer> aftRingVertices) { List<Integer> foreRingVertices, List<Integer> aftRingVertices,
List<Integer> foreRingNormals, List<Integer> aftRingNormals) {
// Set the new group // Set the new group
if (groupName != null) { if (groupName != null) {
obj.setActiveGroupNames(groupName); obj.setActiveGroupNames(groupName);
@ -48,10 +51,10 @@ public class CylinderExporter {
} }
// Generate side top vertices // Generate side top vertices
generateRingVertices(obj, transformer, startIdx, numSides, 0, foreRadius, isOutside, foreRingVertices); generateRingVertices(obj, transformer, numSides, 0, foreRadius, isOutside, foreRingVertices, foreRingNormals);
// Generate side bottom vertices // Generate side bottom vertices
generateRingVertices(obj, transformer, startIdx + numSides, numSides, length, aftRadius, isOutside, aftRingVertices); generateRingVertices(obj, transformer, numSides, length, aftRadius, isOutside, aftRingVertices, aftRingNormals);
// Create faces for the bottom and top // Create faces for the bottom and top
if (solid) { if (solid) {
@ -112,6 +115,13 @@ public class CylinderExporter {
} }
} }
public static void addCylinderMesh(@NotNull DefaultObj obj, @NotNull CoordTransform transformer, String groupName,
float foreRadius, float aftRadius, float length, int numSides, boolean solid, boolean isOutside,
List<Integer> foreRingVertices, List<Integer> aftRingVertices) {
addCylinderMesh(obj, transformer, groupName, foreRadius, aftRadius, length, numSides, solid, isOutside,
foreRingVertices, aftRingVertices, null, null);
}
public static void addCylinderMesh(@NotNull DefaultObj obj, @NotNull CoordTransform transformer, String groupName, public static void addCylinderMesh(@NotNull DefaultObj obj, @NotNull CoordTransform transformer, String groupName,
float radius, float length, int numSides, boolean solid, boolean isOutside, float radius, float length, int numSides, boolean solid, boolean isOutside,
List<Integer> foreRingVertices, List<Integer> aftRingVertices) { List<Integer> foreRingVertices, List<Integer> aftRingVertices) {
@ -142,14 +152,12 @@ public class CylinderExporter {
addCylinderMesh(obj, transformer, groupName, radius, height, nrOfSlices, solid, foreRingVertices, aftRingVertices); addCylinderMesh(obj, transformer, groupName, radius, height, nrOfSlices, solid, foreRingVertices, aftRingVertices);
} }
public static void addCylinderMesh(@NotNull DefaultObj obj, @NotNull CoordTransform transformer, String groupName, public static void generateRingVertices(DefaultObj obj, CoordTransform transformer,
float radius, float height, boolean solid, ObjUtils.LevelOfDetail LOD) {
addCylinderMesh(obj, transformer, groupName, radius, height, LOD.getNrOfSides(radius), solid, null, null);
}
public static void generateRingVertices(DefaultObj obj, CoordTransform transformer, int startIdx,
int numSides, float x, float radius, boolean isOutside, int numSides, float x, float radius, boolean isOutside,
List<Integer> vertexList) { List<Integer> vertexList, List<Integer> normalList) {
int startIdx = obj.getNumVertices();
int normalsStartIdx = obj.getNumNormals();
for (int i = 0; i < numSides; i++) { for (int i = 0; i < numSides; i++) {
final double angle = 2 * Math.PI * i / numSides; final double angle = 2 * Math.PI * i / numSides;
final float y = radius * (float) Math.cos(angle); final float y = radius * (float) Math.cos(angle);
@ -162,6 +170,9 @@ public class CylinderExporter {
if (vertexList != null) { if (vertexList != null) {
vertexList.add(startIdx + i); vertexList.add(startIdx + i);
} }
if (normalList != null) {
normalList.add(normalsStartIdx + i);
}
} }
} }
} }