Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to crop image correctly to minimize glb size after slice_mesh_plane a scene? #2184

Open
yuezhilanyi opened this issue Mar 14, 2024 · 0 comments

Comments

@yuezhilanyi
Copy link

I successfully split a glb file into two parts (including textures), but after the split, each part retained a complete texture file. I want to reduce the size of the textures to decrease the glb file size. I tried to crop the textures based on the UV extremes while stretching the UVs to fit within the 0-1 range, and then assigned the new textures and UVs to the split files. However, there's an issue with the experiment results: the texture range has shifted slightly.
The image below shows the original side of the truck.
image

The image below shows the side of the truck after the split. You can see several black areas have been added to the upper part of the side, which are closely adjacent to the truck's side on the texture.
image

The image below shows the back of the original truck.
image

The image below shows the back of the truck after the split, with the bottom part of the taillights showing less than in the original file.
image

Does anyone know what went wrong in the process? Thanks very much in advance.

here is the original image and my code.
test

import io
import trimesh 

from trimesh.intersections import slice_mesh_plane
from PIL import Image

sc = trimesh.load('CesiumMilkTruck.glb')
mesh = sc.geometry['Cesium_Milk_Truck']

clipped = slice_mesh_plane(mesh, [-1, 0, 0], [ -1.40848477,  2.05412436, -0.00354755])

# # read material image 
material = mesh.visual.material.to_simple()
data, name = material.to_obj()
image_data = data[name + '.png']
image = Image.open(io.BytesIO(image_data))
image.size

uv = clipped.visual.uv
uv_maxs = uv.max(axis=0)
uv_mins = uv.min(axis=0)
height, width = image.height, image.width
uv.shape, uv.max(axis=0), uv.min(axis=0), uv.mean(axis=0)

# # crop material image
left = width * uv_mins[0]
right = width * uv_maxs[0]
upper = height * uv_mins[1]
lower = height * uv_maxs[1]
left, upper, right, lower

cropped_image = image.crop((left, upper, right, lower))
right-left, lower-upper, cropped_image.size

# # normalize uv
uv_norm = (uv - uv_mins) / (uv_maxs - uv_mins)
uv_norm.max(axis=0), uv_norm.min(axis=0), uv_norm.mean(axis=0), uv_norm[:3], uv[:3]

# # write material image to mesh 
color_visuals = trimesh.visual.TextureVisuals(uv=uv_norm, image=cropped_image)
clipped.visual = color_visuals
clipped.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant