Communities

Writing
Writing
Codidact Meta
Codidact Meta
The Great Outdoors
The Great Outdoors
Photography & Video
Photography & Video
Scientific Speculation
Scientific Speculation
Cooking
Cooking
Electrical Engineering
Electrical Engineering
Judaism
Judaism
Languages & Linguistics
Languages & Linguistics
Software Development
Software Development
Mathematics
Mathematics
Christianity
Christianity
Code Golf
Code Golf
Music
Music
Physics
Physics
Linux Systems
Linux Systems
Power Users
Power Users
Tabletop RPGs
Tabletop RPGs
Community Proposals
Community Proposals
tag:snake search within a tag
answers:0 unanswered questions
user:xxxx search by author id
score:0.5 posts with 0.5+ score
"snake oil" exact phrase
votes:4 posts with 4+ votes
created:<1w created < 1 week ago
post_type:xxxx type of post
Search help
Notifications
Mark all as read See all your notifications »
Q&A

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.

Comments on How would I avoid textures being blurred when mapped to a TriangleMesh?

Post

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?:Image showing blurry texture

History
Why does this post require attention from curators or moderators?
You might want to add some details to your flag.
Why should this post be closed?

2 comment threads

Interpolation (1 comment)
Extra information that might be useful to receive an answer (2 comments)
Extra information that might be useful to receive an answer
Alexei‭ wrote almost 2 years ago

I am not into 3d development, but I am wondering about the following:

  • how src/main/resources/textures.png actually looks?
  • shouldn't the picture file be in some vectorial format to properly be displayed?
cuzzo‭ wrote almost 2 years ago · edited almost 2 years ago

Thanks for the response, the image file is 512x512 and the texture is 16x16 (everything else is transparent given its a png. plan is to put more than one texture on it.) I am unaware if it needs to be in a vectorial format or if JavaFX does it differently but to give background when retrieving the texture from the image 2D coordinates are required. This means the 512x512 image is normalized so 0,0 is in the top left corner and 1,1 is in the bottom right corner which explains my existing texture coordinates, newTex retrieving a 16x16 texture from the top left corner of the texture image. (16/512 = 0.03125)