mikedh / trimesh

Python library for loading and using triangular meshes.
https://trimesh.org
MIT License
2.99k stars 577 forks source link

add texture from image to shape #1064

Open pvnieo opened 3 years ago

pvnieo commented 3 years ago

Hi,

I have a 3D shape loaded from an .off file, and I have an image load as a PIL image.

I want to add this image as a texture to the mesh but I can't figure out how to do it. I did the following manipulation, but when I show the mesh, the texture is not applied.

import trimesh
from PIL import Image

im = Image.open("image.png")
m = trimesh.load("mesh.off", process=False)

tex = trimesh.visual.TextureVisuals(image=im)
m.visual.texture = tex

m.show()

How can I do this? Thank you in advance.

pvnieo commented 3 years ago

Hi @mikedh I'm getting a step closer!

I succeeded to add the image to the shape, but because my surface coordinates are random, the texture it self is random. This is what I did:

uv = np.random.rand(m.vertices.shape[0], 2)

material = trimesh.visual.texture.SimpleMaterial(image=im)
color_visuals = trimesh.visual.TextureVisuals(uv=uv, image=im, material=material)
mesh=trimesh.Trimesh(vertices=m.vertices, faces=m.faces, visual=color_visuals, validate=True, process=False)
mesh.show

and this gives me the following: image

Can you suggest me a way to compute the uv?

Thank you in advance!

Xelawk commented 3 years ago

Hi @mikedh I'm getting a step closer!

I succeeded to add the image to the shape, but because my surface coordinates are random, the texture it self is random. This is what I did:

uv = np.random.rand(m.vertices.shape[0], 2)

material = trimesh.visual.texture.SimpleMaterial(image=im)
color_visuals = trimesh.visual.TextureVisuals(uv=uv, image=im, material=material)
mesh=trimesh.Trimesh(vertices=m.vertices, faces=m.faces, visual=color_visuals, validate=True, process=False)
mesh.show

and this gives me the following: image

Can you suggest me a way to compute the uv?

Thank you in advance! I can tell you the way to compute the uv cordinate, but I want to know how apply multiple texture to a Trimesh and show it

robot0321 commented 3 years ago

image

In my case, the .obj file contains uv information. Note that if the mesh.show() present a dark texture, use pyrender library

legel commented 1 year ago

Just a note for others, I managed to manually calculate and pack my UV coordinates into a .ply, then I read that in by Trimesh, then applied my image as a texture, and boom!

import trimesh
from PIL import Image
mesh = trimesh.load("model.ply", process=False)
uv = mesh.visual.uv
im = Image.open("image.png")
material = trimesh.visual.texture.SimpleMaterial(image=im)
color_visuals = trimesh.visual.TextureVisuals(uv=uv, image=im, material=material)
mesh.visual = color_visuals
mesh.export(file_obj='model.glb')
eddjharrison commented 1 year ago

Hey! Nice solution, how did you manage to manually calculate your UV coords though?

rohit7044 commented 12 months ago

Hey! Nice solution, how did you manage to manually calculate your UV coords though?

You can use xatlas-python