I’ve been trying to use trimesh to convert a .ply to a .glb with uv mapping.
.glb viewed with MeshLab
I do this by creating for each layer in my .ply a uv map, and then placing that uv map into a large texture image. I store each vertex in a dataframe and calculate its uv coordinates. I go on to create a trimesh object along with the visuals attribute (which is where the uv coordinates go), but there are dark seams on the result when I view it (darker lines towards the bottom). I think these might be cutoff artifacts of some sort. How might I address them?
So far, I have tried adding 0.5 before normalizing the uv coordinates to adjust for the pixel’s center. Also, I added inner padding through PyTexturePacker, but that didn’t work either. Finally, I manually adjusted extreme coordinates of 0 and 1 inwards. No improvement.
I doubt this is normal, but I don’t know how to address it. Any guidance is much appreciated!
.ply I started with
from skimage import measure, filters, color
import matplotlib.pyplot as plt
import numpy as np
import os
from PIL import Image
import trimesh
import pandas as pd
from trimesh.exchange.ply import _parse_header
fp2 = 'starter_meshes/braunschweig_altstadt_panorama.ply'
H, W = 1024, 2048
with open(fp2, 'rb') as f:
elements, is_ascii, image_name = _parse_header(f)
print(is_ascii)
vertex_data = np.loadtxt(f, max_rows=elements['vertex']['length'])
face_data = np.loadtxt(f, dtype='int32')[:, 1:]
# Extract vertex data
xyz = vertex_data[:, :3] # 3D position
rgb = vertex_data[:, 3:6].astype('uint8') # color
idx = vertex_data[:, 6].astype('int32') # layer index
pix = vertex_data[:, 7:9].astype('int32') # 2D pixel location in panorama (row, col order)
num_layers = idx.max() + 1
# intialize dataframe to store pixel information
pixel_info = pd.DataFrame({
'row': pix[:, 0],
'col': pix[:, 1],
'layer': idx
})
output_folder = "./connected_comps"
os.makedirs(output_folder, exist_ok=True)
# Set dimensions for the combined texture map
combined_height = H * num_layers
combined_width = W
# Initialize the giant texture map
giant_texture_map = np.zeros((combined_height, combined_width, 4), dtype='uint8')
for i in range(num_layers):
sel = (idx == i) # Select the pixels belonging to the current layer
uv_map = np.zeros((H, W, 4), dtype='uint8')
uv_map[pix[sel, 0], pix[sel, 1], :3] = rgb[sel]
uv_map[pix[sel, 0], pix[sel, 1], 3] = 255
# Update the giant texture map
giant_texture_map[i*H:(i+1)*H, :W] = uv_map # This updates the entire slice
# Save the giant texture map for visualization
giant_texture_map_image = Image.fromarray(giant_texture_map)
giant_texture_map_image.save("giant_texture_map.png")
# Update UV coordinates to map to the giant texture map
pixel_info['u'] = pixel_info['col'].astype('float64')
pixel_info['v'] = combined_height - (H * pixel_info['layer'] + pixel_info['row'].astype('float64'))
# Normalize UV coordinates
pixel_info['u'] = (pixel_info['u'] + 0.5) / combined_width
pixel_info['v'] = (pixel_info['v'] + 0.5) / combined_height
# Visualize the new UV map
uv_coordinates = pixel_info[['u','v']].to_numpy()
# Make the mesh and export it with the updated UV coordinates
visuals = trimesh.visual.TextureVisuals(uv=uv_coordinates, image=giant_texture_map_image)
mesh = trimesh.Trimesh(vertices=xyz, faces=face_data, visual=visuals)
mesh.export("uv_mesh.glb")