Skip to content

Commit 1cb07d9

Browse files
authored
Merge pull request #38 from GeoplexGIS/duck-fix
Support paletted textures from trimesh
2 parents f373ff0 + ac5d0ee commit 1cb07d9

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

examples/duck.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from pyrender import Mesh, Scene, Viewer
2+
from io import BytesIO
3+
import numpy as np
4+
import trimesh
5+
import requests
6+
7+
duck_source = "https://github.com/KhronosGroup/glTF-Sample-Models/raw/master/2.0/Duck/glTF-Binary/Duck.glb"
8+
9+
duck = trimesh.load(BytesIO(requests.get(duck_source).content), file_type='glb')
10+
duckmesh = Mesh.from_trimesh(list(duck.geometry.values())[0])
11+
scene = Scene(ambient_light=np.array([1.0, 1.0, 1.0, 1.0]))
12+
scene.add(duckmesh)
13+
Viewer(scene)

pyrender/utils.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,10 @@ def format_texture_source(texture, target_channels='RGB'):
5656

5757
# Convert PIL images into numpy arrays
5858
if isinstance(texture, Image.Image):
59-
texture = np.array(texture)
59+
if texture.mode == 'P' and target_channels in ('RGB', 'RGBA'):
60+
texture = np.array(texture.convert(target_channels))
61+
else:
62+
texture = np.array(texture)
6063

6164
# Format numpy arrays
6265
if isinstance(texture, np.ndarray):

tests/data/Duck.glb

118 KB
Binary file not shown.

tests/unit/test_meshes.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,3 +123,11 @@ def test_meshes():
123123

124124
# From point cloud
125125
x = Mesh.from_points(fm.vertices)
126+
127+
def test_duck():
128+
bm = trimesh.load('tests/data/Duck.glb').dump()[0]
129+
x = Mesh.from_trimesh(bm)
130+
assert x.primitives[0].material.baseColorTexture is not None
131+
pixel = x.primitives[0].material.baseColorTexture.source[100, 100]
132+
yellowish = np.array([1.0, 0.7411765, 0.0, 1.0])
133+
assert np.allclose(pixel, yellowish)

0 commit comments

Comments
 (0)