- Home /
How to apply a png texture to a selfmade mesh?
Hello everybody,
I am currently working at a project where I have to cut out some form from a rectangle, by pressing down a certain button and draw any form I want to cut out. I have added a picture where you can see it, maybe it get's more clear seeing the picture.
I split the rectangle with the corresponding hole inside and the cutted out piece in two different gameobjects by using an algorithm I found in the internet, which works well.
But when I want to apply a texture it comes to problems; If I apply the texture to the 'normal'/'uncutted' object, all is fine. But when I apply such a texture to a cutted out piece or the rectangle with the hole inside, it only changes it's color, but the texture is not correctly applied.
I am not sure what the problem is, do I have to do something like UV-remapping ? And how ? In the second picture you can see the shaded wireframe of the meshes, I want to apply the texture to.
Thanks for your help!
Answer by Bunny83 · May 24, 2018 at 03:06 PM
Yes, you have to define UV coordinates for each of your vertex. There are several ways to accomplish this. Some are easier some are a bit more difficult. The easier methods depend on certain pre-knowledge of the original object. The other method is a more general approach which should work with potentially any mesh.
First of all if you use a MeshCollider on your original mesh and using a Raycast against that collider you already get the interpolated UV coordinate of the hit point. This would be the simplest general approach. We don't know how you "draw" your shape in the first place but i guess you already use raycasting?
The second general approach would be to determine the triangle as well as baricentric coordinate of each vertex position within the triangle of the original mesh. Once you have that you can simply use the baricentric coordinate to interpolate the UV of the 3 corners of the triangle to get the UV at a given point.
The more straight forward solutions require certain conditions of the original mesh. If you have your new vertex coordinates in local space of the object and the original rectangle is perfectly aligned with two local axis, you can simply divide the vertex coordinate by the size of the mesh to get the interpolcated UV coordinate. Though for this you have to know where the origin of the local space is (in one of the corners of the rectangle or the center) and how large the rectangle is in local space. All this information is missing from your question so it would be pointless to show an example for one way if it doesn't apply to your case.
If you need further help, provide more details about how you do the "drawing" and how the original mesh is defined.
For reference over here i posted a 2d cartesian coordinate to barycentriic conversion, and here's a 3d version.
I've packed this all together into a custom struct on the wiki which should simplfy the usage.
At first - thank you really much for your detailed answer, I really appreciate it!
I think the first approach will work for me, because I define the form of the cutted out piece in form of a List with the raycast hit points.
But when reading your answer I noticed that I should get to know a bit more about meshes, uv-mapping, textures etc. in general before implementing something..
I think with a better understanding of the topic in general, combined with your answer I'll be able to find a solution which works for me.
Thanks for your help!
Your answer
Follow this Question
Related Questions
Layered Textures/Materials 0 Answers
How to texture an irregular (hexagon) shape (mesh) ? 1 Answer
How to confirm the vertex of a mesh that mouse in and change its texture 0 Answers
rendering hollow mesh ? 0 Answers
Texture layers on single mesh 1 Answer