Welcome to Software Development on Codidact!
Will you help us build our independent community of developers helping developers? We're small and trying to grow. We welcome questions about all aspects of software development, from design to code to QA and more. Got questions? Got answers? Got code you'd like someone to review? Please join us.
How would I avoid textures being blurred when mapped to a TriangleMesh?
+2
−0
I am making a game using JavaFX and the Fxyz library. I have chunks which are made of Point3D
's, these points are then passed to a ScatterMesh and that is used to generate a TriangleMesh so I can map textures to it. Which is what the below code does:
//Cube object extends Point3D
private final List<Cube> chunk = new ArrayList<>();
private TriangleMesh mesh;
/**
* Iterates through a chunks points and creates a chunk mesh based on which points are active.
*/
public void updateMesh() {
//Retrieving active vertices that should be rendered
List<Cube> cubes = new ArrayList<>();
for (Cube value : chunk) {
if (value.isActive()) {
cubes.add(value);
}
}
//Mesh creation
if (cubes.size() > 0) {
List<Point3D> pCubes = new ArrayList<>(cubes);
//Defining block texture atlas
PhongMaterial mat = new PhongMaterial();
mat.setDiffuseMap(new Image("file:src/main/resources/textures.png"));
//Scatter mesh to be converted to triangle mesh
ScatterMesh scatter = new ScatterMesh(pCubes, 1);
scatter.setMarker(MarkerFactory.Marker.CUBE);
//Triangle mesh creation
mesh = (TriangleMesh) scatter.getMeshFromId("scatter").getMesh();
mesh.setVertexFormat(VertexFormat.POINT_TEXCOORD);
float[] newTex = { //512x512 image file with a 16x16 texture
0f, 0f, //upper left corner
0f, 0.03125f,
0.03125f, 0.03125f, //lower right corner
0.03125f, 0f,
};
//sets texture coords for faces. Every 12 face elements = 2 triangular
//faces = 1 square of a cube, see trianglemesh docs
for (int i = 1; i < mesh.getFaces().size(); i += 12) {
for (int j = 0; j < cubes.size(); j++) {
//every other element in face array is a texture coordinate.
if (cubes.get(j).getType().equals(BlockType.DEFAULT)) {
//triangle1
mesh.getFaces().set(i, 0);
mesh.getFaces().set(i + 2, 1);
mesh.getFaces().set(i + 4, 3);
//triangle2
mesh.getFaces().set(i + 6, 3);
mesh.getFaces().set(i + 8, 1);
mesh.getFaces().set(i + 10, 2);
} else {
mesh.getFaces().set(i, 0);
mesh.getFaces().set(i + 2, 0);
mesh.getFaces().set(i + 4, 0);
mesh.getFaces().set(i + 6, 0);
mesh.getFaces().set(i + 8, 0);
mesh.getFaces().set(i + 10, 0);
}
}
}
mesh.getTexCoords().setAll(newTex);
scatter.setId("scatter");
setCache(true);
setDrawMode(DrawMode.FILL);
setCullFace(CullFace.BACK);
setMesh(mesh);
setMaterial(mat);
}
}
The resulting mesh view looks very blurry and the square faces have transparent gaps in between them if you look closely, is there any way to improve this?:
2 comment threads