-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrender_room.py
More file actions
106 lines (85 loc) · 3.51 KB
/
Copy pathrender_room.py
File metadata and controls
106 lines (85 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import trimesh
import numpy as np
import os
# Load the room mesh
room = trimesh.load('room.glb')
print(f"Loaded mesh: {room}")
print(f"Vertices: {room.vertices.shape}")
print(f"Faces: {room.faces.shape}")
print(f"Bounds: {room.bounds}")
# Add visual attributes if present
if hasattr(room, 'visual') and hasattr(room.visual, 'uv'):
print(f"UVs: {room.visual.uv.shape}")
# Scene setup for rendering
scene = room.scene()
# Set camera to eye-level looking at the center of the room
# Default camera in trimesh is usually okay, but let's adjust
# to look at the center from a 'human eye' position
camera_distance = 2.0 # meters
eye_height = 1.6 # meters
room_center = room.centroid
# Look from the front (positive Z direction)
camera_pos = room_center + np.array([0, eye_height - room.centroid[1], camera_distance])
# Set the camera
scene.camera.resolution = (1920, 1080)
scene.camera.focal = (800, 800) # Approximate focal length
scene.camera.fov = (60, 60) # Field of view
# Look at the room center
look_at = room_center
camera_transform = trimesh.transformations.lookat(camera_pos, look_at, np.array([0, 1, 0]))
scene.camera.transform = camera_transform
print(f"Camera position: {camera_pos}")
print(f"Camera transform: {camera_transform}")
# Note: Pyrender is required for actual 3D rendering with lighting
# For this quick background, we'll use trimesh's simple screenshot if available
# or create a point projection
# Let's try to save an image using trimesh's built-in (requires pyrender)
try:
# Try saving with pyrender
scene.save_image('room_render.png', resolution=(1920, 1080))
print("Rendered room_render.png successfully")
except Exception as e:
print(f"Pyrender not available or failed: {e}")
# Fallback: Create a simple depth-based image
from PIL import Image, ImageDraw
# Project vertices to 2D using a simple perspective projection
vertices = room.vertices
# Simple camera projection
# Assuming camera at camera_pos, looking at room_center
# Create a look-at matrix
def normalize(v):
return v / np.linalg.norm(v)
forward = normalize(room_center - camera_pos)
right = normalize(np.cross(forward, np.array([0, 1, 0])))
up = np.cross(right, forward)
# View matrix (simplified)
view_matrix = np.array([
[right[0], right[1], right[2], -np.dot(right, camera_pos)],
[up[0], up[1], up[2], -np.dot(up, camera_pos)],
[forward[0], forward[1], forward[2], -np.dot(forward, camera_pos)],
[0, 0, 0, 1]
])
# Project vertices
proj_vertices = []
for v in vertices:
# Transform to view space
p = np.append(v, 1)
p_view = view_matrix @ p
# Perspective projection (simplified)
if p_view[2] > 0:
x = p_view[0] / p_view[2] * 400 + 960 # Center at 960, scale by 400
y = -p_view[1] / p_view[2] * 400 + 540 # Center at 540, flip Y
proj_vertices.append((x, y, p_view[2]))
# Create image
img = Image.new('RGB', (1920, 1080), (50, 50, 70))
draw = ImageDraw.Draw(img)
# Sort by depth (draw far -> near)
proj_vertices.sort(key=lambda p: p[2], reverse=True)
# Draw points
for x, y, z in proj_vertices:
if 0 <= x < 1920 and 0 <= y < 1080:
# Color based on depth
depth_color = int(255 - min(z * 10, 255))
draw.point((x, y), fill=(depth_color, depth_color, depth_color + 20))
img.save('room_fallback.png')
print("Saved fallback room_fallback.png")