mlx3d.renderer¶
mlx3d.renderer
¶
Renderer
¶
Bases: Protocol
Anything callable as renderer(camera, *scene) -> RenderOutput.
Use it purely as a type hint for code that accepts a pluggable renderer::
def turntable(renderer: Renderer, cameras, scene): ...
Both library functions and your own closures satisfy it without subclassing.
Source code in src/mlx3d/renderer/protocols.py
51 52 53 54 55 56 57 58 59 60 61 62 | |
RenderOutput
¶
Bases: TypedDict
The dict every image-space renderer returns.
Keys are optional so depth-only or alpha-only passes are still valid outputs, but RGB renderers populate all three:
image:(H, W, 3)RGB in[0, 1].alpha:(H, W)coverage / opacity in[0, 1].depth:(H, W)expected depth in world units.
Renderers may attach extra keys (e.g. means2d for splatting); consumers
should read by key rather than assume an exact set.
Source code in src/mlx3d/renderer/protocols.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
Fragments
dataclass
¶
Per-pixel rasterizer output.
Attributes:
| Name | Type | Description |
|---|---|---|
pix_to_face |
array
|
|
zbuf |
array
|
|
bary |
array
|
|
valid |
array
|
|
vert_ids |
array
|
|
Source code in src/mlx3d/renderer/rasterizer.py
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
AmbientLights
dataclass
¶
Uniform ambient illumination.
Source code in src/mlx3d/renderer/shading.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
DirectionalLights
dataclass
¶
A light at infinity. direction is the direction the light travels.
Source code in src/mlx3d/renderer/shading.py
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 | |
PointLights
dataclass
¶
A point light at location (world space).
Source code in src/mlx3d/renderer/shading.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
render_mesh_soft(camera, mesh_or_verts, faces=None, verts_colors=None, face_colors=None, texcoords=None, faces_texcoords_idx=None, texture=None, sigma=0.01, depth_temperature=25.0, background=0.0, eps=1e-08, face_chunk_size=256)
¶
Render a triangle mesh with a SoftRas-style differentiable rasterizer.
Fully MLX-differentiable w.r.t. vertices, vertex colors, face colors, and texture values. Topology and UV indices are treated as discrete.
The rasterizer processes faces in batches of face_chunk_size to keep
memory use bounded: each chunk creates (chunk, H, W) intermediates so
even large meshes can be rendered on 8-16 GB machines. Set
face_chunk_size=None to disable chunking (faster for small meshes that
fit comfortably in memory).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera
|
Camera
|
Pinhole camera. |
required |
mesh_or_verts
|
Meshes | array
|
A :class: |
required |
faces
|
array | None
|
|
None
|
verts_colors
|
array | None
|
|
None
|
face_colors
|
array | None
|
|
None
|
texcoords
|
array | None
|
|
None
|
faces_texcoords_idx
|
array | None
|
|
None
|
texture
|
array | None
|
|
None
|
sigma
|
float
|
Soft boundary width (larger → smoother, less sharp edges). |
0.01
|
depth_temperature
|
float
|
Controls depth-based face ordering; higher values make nearer faces dominate more sharply. |
25.0
|
background
|
array | tuple[float, float, float] | float
|
Background color — scalar, |
0.0
|
eps
|
float
|
Numerical epsilon for safe divisions. |
1e-08
|
face_chunk_size
|
int | None
|
Process this many faces per chunk; |
256
|
Returns:
| Type | Description |
|---|---|
dict[str, array]
|
A dict with keys |
dict[str, array]
|
and |
Source code in src/mlx3d/renderer/mesh.py
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 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 | |
sample_texture(texture, uv)
¶
Bilinearly sample an image texture at UV coordinates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
texture
|
array
|
|
required |
uv
|
array
|
|
required |
Source code in src/mlx3d/renderer/mesh.py
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 | |
render_points(camera, points, colors=None, radius=2.0, window=5, depth_temperature=10.0, background=0.0, eps=1e-08)
¶
Render a point cloud with soft Gaussian splats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera
|
Camera
|
the :class: |
required |
points
|
array
|
(P, 3) world-space positions. |
required |
colors
|
array | None
|
(P, 3) per-point colors in [0, 1] (defaults to white). |
None
|
radius
|
float
|
Gaussian sigma in pixels. |
2.0
|
window
|
int
|
splat window size in pixels (odd). |
5
|
depth_temperature
|
float
|
sharpness of the soft depth weighting; larger values approach hard z-ordering. |
10.0
|
background
|
float
|
scalar background intensity. |
0.0
|
Returns:
| Type | Description |
|---|---|
dict[str, array]
|
dict with |
dict[str, array]
|
|
Source code in src/mlx3d/renderer/points.py
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 | |
interpolate_face_attributes(frag, attrs)
¶
Interpolate a per-vertex attribute over the rasterized fragments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
frag
|
Fragments
|
fragments from :func: |
required |
attrs
|
array
|
|
required |
Returns:
| Type | Description |
|---|---|
array
|
|
array
|
w.r.t. both |
Source code in src/mlx3d/renderer/rasterizer.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 | |
rasterize_meshes(camera, mesh_or_verts, faces=None)
¶
Rasterize a triangle mesh to per-pixel :class:Fragments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera
|
Camera
|
the viewing camera. |
required |
mesh_or_verts
|
Meshes | array
|
a single-mesh :class: |
required |
faces
|
array | None
|
|
None
|
Source code in src/mlx3d/renderer/rasterizer.py
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
sample_along_rays(origins, directions, near, far, num_samples, stratified=True)
¶
Sample points along rays between near and far.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
origins
|
array
|
(R, 3) ray origins. |
required |
directions
|
array
|
(R, 3) ray directions (need not be normalized). |
required |
num_samples
|
int
|
samples per ray. |
required |
stratified
|
bool
|
jitter samples within their bins (use |
True
|
Returns:
| Type | Description |
|---|---|
tuple[array, array]
|
|
Source code in src/mlx3d/renderer/rays.py
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 | |
sample_pdf(bins, weights, num_samples, deterministic=False, eps=1e-05)
¶
Importance-sample new t-values from a piecewise-constant PDF.
Used for the hierarchical (fine) sampling stage of NeRF.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
bins
|
array
|
(R, S+1) bin edges (e.g. midpoints of the coarse samples). |
required |
weights
|
array
|
(R, S) unnormalized bin weights. |
required |
num_samples
|
int
|
new samples per ray. |
required |
Returns:
| Type | Description |
|---|---|
array
|
(R, num_samples) sampled t-values. Gradients are not propagated |
array
|
through the sampling (treated as constants, as in NeRF). |
Source code in src/mlx3d/renderer/rays.py
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 | |
volume_render(densities, colors, t_vals, directions=None, white_background=False)
¶
Composite densities and colors along rays with the NeRF quadrature rule.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
densities
|
array
|
(R, S) non-negative volume densities (sigma). |
required |
colors
|
array
|
(R, S, 3) per-sample RGB in [0, 1]. |
required |
t_vals
|
array
|
(R, S) sample distances along each ray. |
required |
directions
|
array | None
|
optional (R, 3) ray directions; if given, deltas are scaled by their norms so densities live in world units. |
None
|
white_background
|
bool
|
composite onto white instead of black. |
False
|
Returns:
| Type | Description |
|---|---|
dict[str, array]
|
dict with |
dict[str, array]
|
|
Source code in src/mlx3d/renderer/rays.py
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 | |
pbr_shading(points, normals, albedo, camera_center, lights, roughness=0.5, metallic=0.0)
¶
Cook-Torrance/GGX material shading.
This is a compact real-time PBR shader: GGX normal distribution, Schlick Fresnel, and Smith-Schlick geometry term. It is not a full environment-lit renderer, but it gives glTF-style base-color/metallic/roughness controls with stable MLX autodiff.
Source code in src/mlx3d/renderer/shading.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 | |
phong_shading(points, normals, albedo, camera_center, lights, shininess=32.0, specular_strength=0.3)
¶
Blinn-Phong shade per-pixel buffers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
points
|
array
|
|
required |
normals
|
array
|
|
required |
albedo
|
array
|
|
required |
camera_center
|
array
|
|
required |
lights
|
list[Light]
|
list of light sources. |
required |
shininess
|
float
|
specular exponent. |
32.0
|
specular_strength
|
float
|
scalar weight on the specular term. |
0.3
|
Source code in src/mlx3d/renderer/shading.py
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | |
render_mesh(camera, mesh_or_verts, faces=None, verts_colors=None, texture=None, verts_uvs=None, faces_uvs=None, lights=None, shininess=32.0, specular_strength=0.3, roughness=0.5, metallic=0.0, background=0.0, shading='phong', ssaa=1)
¶
Render a mesh with the hard rasterizer and Blinn-Phong lighting.
Albedo comes from a UV texture when texture is given, otherwise from
verts_colors (default mid-grey). ssaa > 1 supersamples (renders at
ssaa x resolution and box-downsamples) for antialiased edges.
Besides image/alpha/depth/normals the result includes render
passes (AOVs): position ((H, W, 3) world-space hit point) and
face_id ((H, W) nearest-face index, -1 where empty).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
camera
|
Camera
|
viewing camera. |
required |
mesh_or_verts
|
Meshes | array
|
a single-mesh :class: |
required |
faces
|
array | None
|
|
None
|
verts_colors
|
array | None
|
|
None
|
texture
|
array | None
|
|
None
|
verts_uvs
|
array | None
|
|
None
|
faces_uvs
|
array | None
|
|
None
|
lights
|
list[Light] | None
|
light list; defaults to one key light + ambient. |
None
|
shading
|
str
|
|
'phong'
|
roughness
|
float | array
|
scalar or |
0.5
|
metallic
|
float | array
|
scalar or |
0.0
|
background
|
tuple[float, float, float] | float
|
scalar or |
0.0
|
Returns:
| Type | Description |
|---|---|
RenderOutput
|
|
Source code in src/mlx3d/renderer/shading.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |