[SOLVED] Project a PNG on a mesh

I hope this is a very easy and common use case but I didn’t find any solution about it.

I have a mesh with a lot of triangles. This mesh was created by a 3D scanner and represents a human body. And I have a PNG(or any other type) file and now I want to project this image on a certain point on the mesh.

I hope there’s an easy way.

1 Like

Welcome to the JMonkeyEngine community.

Does the 3D mesh have texture coordinates? If not, generating texture coordinates seems like the quickest way to get what you want.

i think he also might mean:

That’s what I need. Just the projection must be fixed. It’s like a label/sticker which is sticked on the model. I didn’t find a class with the ParallelProjection.
But to use a texture would be nice, too.

well imo best if you would have UV coords for this model, then it would be quite easy just overlay texture with icon.

you can add UV in blender on this scanned model.

The mesh has no texture coordinates. The model was recontructed by a point cloud.
I have a hit point with this model and I want stick a label/sticker on this position of the model.

how is it textured then?

or is it just a single color?

btw. you can just generate UV yourself in Blender.

No, the model is created to the runtime and I have only the coordinates where the sticker must be located on the model. I cannot use blender because it’s in a program.
It is possible to calculate the UV coordinates of the mesh for a certain point?

The model has no texture, just a color.

well, everything is possible, but its not quite easy.

you would need aply proper UV manually by updating UV mesh buffer.
here link that can help:
https://wiki.jmonkeyengine.org/jme3/advanced/custom_meshes.html

BUT:
why not:
step 1 → scan model
step 2 → load scanned model into Blender
step 3 → UV this model in Blender
step 4 → export from blender to use in JME
step 5 → add sticker based on UV coords.
done.

you could even texture all model too.

?

I can not use Blender. The model is everytime new. The user makes scan and then model will displayed with the sticker on it.

ah, i understand.

well i think you could use something like 3d texture coords. like some voxel terrain uses.
but im not familiar with it much.

or like i said, write some own solution using custom UV buffer generation.

so shader no UV texture placement solution or somehow generate UV for it.

Generating texture (UV) coordinates for a mesh is not difficult, if you’re willing to write a little code:

  1. pick a view direction and central point on the mesh
  2. loop over the vertices, projecting each position into the view coordinates and writing the projected coordinates to a new buffer
  3. add the new buffer to the mesh
1 Like

one issue that “90 degree to camera tris” will have almost no pixels in UV and back and front tris will have same position

but well, will work for just color + sticker

Thanks for your hint and I’m willing to write code but I’m unable to this. I’m not 3D expert, I’m proud of me to show the model on a canvas and that I can rotate it. But there, my skills end.

1 Like

What do you mean with tris?
I have the coordinates and the normals. It is not possible to says:
That’s my coordinate on the model and this it’s start point of image which should be draw on the this part of the model.

“tris” is the word that mean triangle in mesh, mesh(geometry shape) is constructed from triangles.

also one “face”(quad) = 2 trangles.

normals refer to quads(face direction) [if i good remember not tris, but quads.]

Tris have position, correct, each verticle of tris have position, but please note UV is 2d coords, while verticle have 3d coords. its just about how you want map your model triangles to match 2d coords in UV.

what i said was that some “tris” what is almost not visible in viewport, while doing his projecting solution it would have “too small area in 2d UV coords”, for color it would be ok, but if you would want to perfectly texture model, then you need better UV than a projected one.

Color would be perfect. Exerything I want is to show a white circle on the model. It must not be a png image or something.

An alternate approach I just thought of would be to import the mesh into Blender 3D and generate the texture coordinates there.

I’m also willing to walk you through the coding process in more detail. In JMonkeyEngine, a mesh is composed mostly of buffers. Have you worked with buffers in Java before?

An alternate approach I just thought of would be to import the mesh into Blender 3D and generate the texture coordinates there.

he said he cant, because it change each time in runtime. or maybe you mean some automatic solution? like running via console “.blender -generate-uv .someFileWithMesh.obj” ? kind of?

@homerj
anyway what @sgold said would be quite easy and will work for color + circle.

step 1) read each quad(2 triangles) from mesh
step 2) for each verticle of quad, check position in viewport(monitor window)
step 3) based on position in viewport, set same position in some UV list(but can be scaled)
step 4) update mesh UV buffer with new coords.
step 5) apply image with circle you want as image for UV where circle will be in position where UV match place you want to have this circle

fix me if i said something wrong.

That’s is everything I could be implemented:

	public static void calcUVCoords(Mesh mesh) {
		List<Triangle> tris = new ArrayList<>();
		for(int i=0;i<mesh.getTriangleCount();i++) {
			Triangle tri = new Triangle();
			mesh.getTriangle(i, tri );
			tris.add(tri);
		}
		List<Quad> quads = new ArrayList<>();
		for(Triangle tri:tris) {
			// triangle to quads????
		}
	}

What do you mean with check each verticle of quad in viewport?