strata-render 0.1.0

High-performance Display List, Stacking Context Compositor, and GPU rasterization pipeline
Documentation
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
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
219
220
221
222
223
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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
//! Hardware-Accelerated 3D GPU Render Pipeline and WGSL Shaders.
//!
//! Provides direct GPU vertex and fragment pipelines with depth-stencil textures (`Depth32Float`),
//! uniform buffers for model-view-projection matrices, multi-light Blinn-Phong shading,
//! and compositing the rendered 3D viewport into the 2D display list.

use crate::camera64::DVec3;
use crate::gpu_context::{GpuBufferHandle, GpuBufferUsage, GpuDevice, GpuRenderPassDescriptor};
use crate::math3d::{Mat4, Vec3};
use crate::scene3d::{Camera3D, Light3D, Material3D, Scene3D};
use strata_layout::Rect;

/// Standard WGSL shader for hardware-accelerated 3D meshes with depth testing and lighting.
pub const WGSL_3D_STANDARD_SHADER: &str = r#"
struct VertexInput {
    @location(0) position: vec3<f32>,
    @location(1) normal: vec3<f32>,
    @location(2) uv: vec2<f32>,
};

struct VertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) world_pos: vec3<f32>,
    @location(1) world_normal: vec3<f32>,
    @location(2) uv: vec2<f32>,
};

struct Uniforms {
    model_matrix: mat4x4<f32>,
    view_proj_matrix: mat4x4<f32>,
    normal_matrix: mat4x4<f32>,
    camera_pos: vec3<f32>,
    _pad0: f32,
    ambient_color: vec3<f32>,
    _pad1: f32,
    dir_light_dir: vec3<f32>,
    _pad2: f32,
    dir_light_color: vec3<f32>,
    _pad3: f32,
    material_color: vec4<f32>,
    roughness: f32,
    metalness: f32,
    _pad4: vec2<f32>,
};

@group(0) @binding(0) var<uniform> uniforms: Uniforms;

@vertex
fn vs_main(model: VertexInput) -> VertexOutput {
    var out: VertexOutput;

    let world_pos = uniforms.model_matrix * vec4<f32>(model.position, 1.0);
    out.world_pos = world_pos.xyz;
    out.clip_position = uniforms.view_proj_matrix * world_pos;
    out.world_normal = normalize((uniforms.normal_matrix * vec4<f32>(model.normal, 0.0)).xyz);
    out.uv = model.uv;

    return out;
}

@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
    let N = normalize(in.world_normal);
    let V = normalize(uniforms.camera_pos - in.world_pos);
    let L = normalize(-uniforms.dir_light_dir);
    let H = normalize(L + V);

    // Ambient
    let ambient = uniforms.ambient_color * uniforms.material_color.rgb;

    // Diffuse
    let diff = max(dot(N, L), 0.0);
    let diffuse = diff * uniforms.dir_light_color * uniforms.material_color.rgb;

    // Specular (Blinn-Phong)
    let spec = pow(max(dot(N, H), 0.0), max((1.0 - uniforms.roughness) * 64.0, 1.0));
    let specular = spec * uniforms.dir_light_color * mix(vec3<f32>(0.04), uniforms.material_color.rgb, uniforms.metalness);

    let final_color = ambient + diffuse + specular;
    return vec4<f32>(final_color, uniforms.material_color.a);
}
"#;

/// Standard WGSL shader for Relative-To-Eye (RTE) double-single 3D meshes on GPU.
///
/// Prevents 32-bit vertex jitter and shaking at planetary coordinates (~6.378e6 m)
/// by evaluating relative subtraction `(pos_high - eye_high) + (pos_low - eye_low)`
/// directly inside the GPU vertex shader.
pub const WGSL_3D_RTE_SHADER: &str = r#"
struct RteVertexInput {
    @location(0) position_high: vec3<f32>,
    @location(1) position_low: vec3<f32>,
    @location(2) normal: vec3<f32>,
    @location(3) uv: vec2<f32>,
};

struct RteVertexOutput {
    @builtin(position) clip_position: vec4<f32>,
    @location(0) view_relative_pos: vec3<f32>,
    @location(1) world_normal: vec3<f32>,
    @location(2) uv: vec2<f32>,
};

struct RteUniforms {
    view_proj_relative: mat4x4<f32>,
    normal_matrix: mat4x4<f32>,
    eye_high: vec3<f32>,
    _pad0: f32,
    eye_low: vec3<f32>,
    _pad1: f32,
    ambient_color: vec3<f32>,
    _pad2: f32,
    dir_light_dir: vec3<f32>,
    _pad3: f32,
    dir_light_color: vec3<f32>,
    _pad4: f32,
    material_color: vec4<f32>,
    roughness: f32,
    metalness: f32,
    _pad5: vec2<f32>,
};

@group(0) @binding(0) var<uniform> uniforms: RteUniforms;

@vertex
fn vs_rte_main(input: RteVertexInput) -> RteVertexOutput {
    var out: RteVertexOutput;

    // Double-single Relative-To-Eye (RTE) subtraction on GPU
    let rel_high = input.position_high - uniforms.eye_high;
    let rel_low = input.position_low - uniforms.eye_low;
    let rel_pos = rel_high + rel_low;

    out.view_relative_pos = rel_pos;
    out.clip_position = uniforms.view_proj_relative * vec4<f32>(rel_pos, 1.0);
    out.world_normal = normalize((uniforms.normal_matrix * vec4<f32>(input.normal, 0.0)).xyz);
    out.uv = input.uv;

    return out;
}

@fragment
fn fs_rte_main(in: RteVertexOutput) -> @location(0) vec4<f32> {
    let N = normalize(in.world_normal);
    let V = normalize(-in.view_relative_pos);
    let L = normalize(-uniforms.dir_light_dir);
    let H = normalize(L + V);

    // Ambient
    let ambient = uniforms.ambient_color * uniforms.material_color.rgb;

    // Diffuse
    let diff = max(dot(N, L), 0.0);
    let diffuse = diff * uniforms.dir_light_color * uniforms.material_color.rgb;

    // Specular (Blinn-Phong)
    let spec = pow(max(dot(N, H), 0.0), max((1.0 - uniforms.roughness) * 64.0, 1.0));
    let specular = spec * uniforms.dir_light_color * mix(vec3<f32>(0.04), uniforms.material_color.rgb, uniforms.metalness);

    let final_color = ambient + diffuse + specular;
    return vec4<f32>(final_color, uniforms.material_color.a);
}
"#;

/// 3D vertex layout for GPU vertex buffer upload.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Gpu3DVertex {
    pub position: [f32; 3],
    pub normal: [f32; 3],
    pub uv: [f32; 2],
}

/// Packed uniform buffer data for 3D shaders.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Gpu3DUniforms {
    pub model_matrix: [[f32; 4]; 4],
    pub view_proj_matrix: [[f32; 4]; 4],
    pub normal_matrix: [[f32; 4]; 4],
    pub camera_pos: [f32; 3],
    pub _pad0: f32,
    pub ambient_color: [f32; 3],
    pub _pad1: f32,
    pub dir_light_dir: [f32; 3],
    pub _pad2: f32,
    pub dir_light_color: [f32; 3],
    pub _pad3: f32,
    pub material_color: [f32; 4],
    pub roughness: f32,
    pub metalness: f32,
    pub _pad4: [f32; 2],
}

#[inline]
pub fn mat4_to_cols_2d(m: &Mat4) -> [[f32; 4]; 4] {
    [
        [m.m[0], m.m[1], m.m[2], m.m[3]],
        [m.m[4], m.m[5], m.m[6], m.m[7]],
        [m.m[8], m.m[9], m.m[10], m.m[11]],
        [m.m[12], m.m[13], m.m[14], m.m[15]],
    ]
}

impl Gpu3DUniforms {
    /// Safe byte view for uploading to GPU uniform buffer.
    pub fn as_bytes(&self) -> &[u8] {
        unsafe {
            std::slice::from_raw_parts(
                self as *const Self as *const u8,
                std::mem::size_of::<Self>(),
            )
        }
    }
}

impl Default for Gpu3DUniforms {
    fn default() -> Self {
        let id = mat4_to_cols_2d(&Mat4::IDENTITY);
        Self {
            model_matrix: id,
            view_proj_matrix: id,
            normal_matrix: id,
            camera_pos: [0.0, 0.0, 5.0],
            _pad0: 0.0,
            ambient_color: [0.2, 0.2, 0.25],
            _pad1: 0.0,
            dir_light_dir: [-0.5, -1.0, -0.5],
            _pad2: 0.0,
            dir_light_color: [0.9, 0.9, 0.85],
            _pad3: 0.0,
            material_color: [0.8, 0.8, 0.8, 1.0],
            roughness: 0.4,
            metalness: 0.1,
            _pad4: [0.0, 0.0],
        }
    }
}

/// 3D vertex layout for GPU Relative-To-Eye (RTE) double-single vertex buffer upload.
///
/// Stores position split into high and low 32-bit floats (`vec3<f32> high`, `vec3<f32> low`),
/// eliminating 32-bit floating point precision loss at planetary scales.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct GpuRteVertex {
    pub position_high: [f32; 3],
    pub position_low: [f32; 3],
    pub normal: [f32; 3],
    pub uv: [f32; 2],
}

impl GpuRteVertex {
    pub const STRIDE_FLOATS: usize = 11; // 11 floats = 44 bytes

    pub fn new(position: DVec3, normal: [f32; 3], uv: [f32; 2]) -> Self {
        let (high, low) = position.split_high_low();
        Self {
            position_high: high,
            position_low: low,
            normal,
            uv,
        }
    }
}

/// Packed uniform buffer data for Relative-To-Eye (RTE) 3D shaders.
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct GpuRteUniforms {
    pub view_proj_relative: [[f32; 4]; 4],
    pub normal_matrix: [[f32; 4]; 4],
    pub eye_high: [f32; 3],
    pub _pad0: f32,
    pub eye_low: [f32; 3],
    pub _pad1: f32,
    pub ambient_color: [f32; 3],
    pub _pad2: f32,
    pub dir_light_dir: [f32; 3],
    pub _pad3: f32,
    pub dir_light_color: [f32; 3],
    pub _pad4: f32,
    pub material_color: [f32; 4],
    pub roughness: f32,
    pub metalness: f32,
    pub _pad5: [f32; 2],
}

impl GpuRteUniforms {
    /// Safe byte view for uploading to GPU uniform buffer.
    pub fn as_bytes(&self) -> &[u8] {
        unsafe {
            std::slice::from_raw_parts(
                self as *const Self as *const u8,
                std::mem::size_of::<Self>(),
            )
        }
    }
}

impl Default for GpuRteUniforms {
    fn default() -> Self {
        let id = mat4_to_cols_2d(&Mat4::IDENTITY);
        Self {
            view_proj_relative: id,
            normal_matrix: id,
            eye_high: [0.0, 0.0, 0.0],
            _pad0: 0.0,
            eye_low: [0.0, 0.0, 0.0],
            _pad1: 0.0,
            ambient_color: [0.2, 0.2, 0.25],
            _pad2: 0.0,
            dir_light_dir: [-0.5, -1.0, -0.5],
            _pad3: 0.0,
            dir_light_color: [0.9, 0.9, 0.85],
            _pad4: 0.0,
            material_color: [0.8, 0.8, 0.8, 1.0],
            roughness: 0.4,
            metalness: 0.1,
            _pad5: [0.0, 0.0],
        }
    }
}

/// Hardware GPU 3D rendering pipeline managing vertex/index buffers and draw passes.
pub struct Gpu3DPipeline {
    pub vertex_buffer: GpuBufferHandle,
    pub index_buffer: GpuBufferHandle,
    pub uniform_buffer: GpuBufferHandle,
    pub depth_stencil: crate::gpu_pipeline::GpuDepthStencilState,
    pub viewport_width: u32,
    pub viewport_height: u32,
    pub early_z_culled_fragments: usize,
    pub total_tested_fragments: usize,
}

impl Gpu3DPipeline {
    /// Creates a new 3D GPU pipeline on the specified device.
    pub fn new(device: &GpuDevice, width: u32, height: u32) -> Self {
        let vb = device.create_buffer(1024 * 1024, GpuBufferUsage::Vertex);
        let ib = device.create_buffer(512 * 1024, GpuBufferUsage::Index);
        let ub = device.create_buffer(
            std::mem::size_of::<Gpu3DUniforms>(),
            GpuBufferUsage::Uniform,
        );

        Self {
            vertex_buffer: vb,
            index_buffer: ib,
            uniform_buffer: ub,
            depth_stencil: crate::gpu_pipeline::GpuDepthStencilState::default(),
            viewport_width: width,
            viewport_height: height,
            early_z_culled_fragments: 0,
            total_tested_fragments: 0,
        }
    }

    /// Evaluates hardware early-Z fragment occlusion test, updating occlusion stats.
    pub fn test_early_z(&mut self, current_depth: f32, new_depth: f32) -> bool {
        self.total_tested_fragments += 1;
        if self.depth_stencil.passes_test(current_depth, new_depth) {
            true
        } else {
            self.early_z_culled_fragments += 1;
            false
        }
    }

    /// Renders a Scene3D directly onto the hardware GPU device target, resolving any
    /// [`crate::texture3d::TextureStore`] attached to `scene`.
    pub fn render_scene(
        &mut self,
        device: &mut GpuDevice,
        scene: &Scene3D,
        viewport: Rect,
        clip: Option<Rect>,
    ) {
        self.render_scene_with_textures(device, scene, viewport, clip, scene.textures_deref());
    }

    /// Renders a Scene3D onto the hardware GPU device target using `textures` (or `scene.textures_deref()`).
    pub fn render_scene_with_textures(
        &mut self,
        device: &mut GpuDevice,
        scene: &Scene3D,
        viewport: Rect,
        clip: Option<Rect>,
        textures: Option<&crate::texture3d::TextureStore>,
    ) {
        let eff_textures = textures.or_else(|| scene.textures_deref());
        let mut encoder = device.create_command_encoder();

        let pass_desc = GpuRenderPassDescriptor {
            color_texture_id: device.swapchain_texture.id,
            depth_texture_id: Some(device.depth_texture.id),
            clear_color: Some([0.05, 0.05, 0.06, 1.0]),
            clear_depth: Some(1.0),
            scissor_rect: clip.or(Some(viewport)),
        };

        encoder.begin_render_pass(pass_desc);

        // Upload uniforms
        let mut uniforms = Gpu3DUniforms::default();
        let aspect = viewport.width / viewport.height.max(1.0);
        let up_vec = scene
            .camera_up()
            .map(|u| Vec3::new(u[0] as f32, u[1] as f32, u[2] as f32))
            .unwrap_or(Vec3::UP);
        let (proj_mat, view_mat, cam_pos) = match &scene.camera {
            Camera3D::Perspective {
                fov_horizontal_deg,
                near,
                far,
                position,
                target,
            } => {
                let fov_x = fov_horizontal_deg.to_radians();
                let fov_y = 2.0 * ((fov_x * 0.5).tan() / aspect).atan();
                let p = Mat4::perspective(fov_y, aspect, *near, *far);
                let eye = Vec3::new(position[0], position[1], position[2]);
                let tgt = Vec3::new(target[0], target[1], target[2]);
                let v = Mat4::look_at(eye, tgt, up_vec);
                (p, v, *position)
            }
            Camera3D::Orthographic {
                left,
                right,
                top,
                bottom,
                near,
                far,
                position,
                target,
            } => {
                let p = Mat4::orthographic(*left, *right, *bottom, *top, *near, *far);
                let eye = Vec3::new(position[0], position[1], position[2]);
                let tgt = Vec3::new(target[0], target[1], target[2]);
                let v = Mat4::look_at(eye, tgt, up_vec);
                (p, v, *position)
            }
        };

        let view_proj = proj_mat.multiply(&view_mat);
        uniforms.view_proj_matrix = mat4_to_cols_2d(&view_proj);
        uniforms.camera_pos = cam_pos;

        // Process lights
        for light in &scene.lights {
            match light {
                Light3D::Ambient { color, intensity } => {
                    let (r, g, b, _) = color.to_f32();
                    uniforms.ambient_color = [r * intensity, g * intensity, b * intensity];
                }
                Light3D::Directional {
                    position,
                    color,
                    intensity,
                    ..
                } => {
                    let dir = Vec3::new(position[0], position[1], position[2]).normalize();
                    uniforms.dir_light_dir = [dir.x, dir.y, dir.z];
                    let (r, g, b, _) = color.to_f32();
                    uniforms.dir_light_color = [r * intensity, g * intensity, b * intensity];
                }
                _ => {}
            }
        }

        // Draw meshes
        for mesh in &scene.meshes {
            let t = Vec3::new(
                mesh.transform.position[0],
                mesh.transform.position[1],
                mesh.transform.position[2],
            );
            let r = Vec3::new(
                mesh.transform.rotation[0],
                mesh.transform.rotation[1],
                mesh.transform.rotation[2],
            );
            let s = Vec3::new(
                mesh.transform.scale[0],
                mesh.transform.scale[1],
                mesh.transform.scale[2],
            );
            let model_mat = Mat4::from_trs(t, r, s);
            let normal_mat = model_mat.inverse().unwrap_or(Mat4::IDENTITY).transpose();

            uniforms.model_matrix = mat4_to_cols_2d(&model_mat);
            uniforms.normal_matrix = mat4_to_cols_2d(&normal_mat);

            let mesh_index_count: usize = match &mesh.geometry {
                crate::scene3d::Geometry3D::Custom { indices, .. }
                | crate::scene3d::Geometry3D::CustomTextured { indices, .. } => indices.len(),
                _ => 36,
            };

            match &mesh.material {
                Material3D::Standard {
                    color,
                    roughness,
                    metalness,
                    ..
                } => {
                    let (r, g, b, a) = color.to_f32();
                    uniforms.material_color = [r, g, b, a];
                    uniforms.roughness = *roughness;
                    uniforms.metalness = *metalness;
                    self.uniform_buffer.write_bytes(0, uniforms.as_bytes());
                    encoder.draw_indexed(mesh_index_count.max(3), 1);
                }
                Material3D::Basic { color } => {
                    let (r, g, b, a) = color.to_f32();
                    uniforms.material_color = [r, g, b, a];
                    uniforms.roughness = 1.0;
                    uniforms.metalness = 0.0;
                    self.uniform_buffer.write_bytes(0, uniforms.as_bytes());
                    encoder.draw_indexed(mesh_index_count.max(3), 1);
                }
                Material3D::Textured(tex_mat) => {
                    uniforms.material_color = sample_textured_material_color(tex_mat, eff_textures);
                    uniforms.roughness = 0.9;
                    uniforms.metalness = 0.0;
                    self.uniform_buffer.write_bytes(0, uniforms.as_bytes());
                    encoder.draw_indexed(mesh_index_count.max(3), 1);
                }
                Material3D::TexturedBatch(batch) => {
                    uniforms.roughness = 0.9;
                    uniforms.metalness = 0.0;
                    for draw in &batch.draws {
                        if draw.index_count == 0 {
                            continue;
                        }
                        uniforms.material_color =
                            sample_textured_material_color(&draw.material, eff_textures);
                        self.uniform_buffer.write_bytes(0, uniforms.as_bytes());
                        encoder.draw_indexed(draw.index_count as usize, 1);
                    }
                }
                _ => {
                    self.uniform_buffer.write_bytes(0, uniforms.as_bytes());
                    encoder.draw_indexed(mesh_index_count.max(3), 1);
                }
            }
        }

        encoder.end_render_pass();
        device.submit(encoder);
    }

    /// Renders a Relative-To-Eye (RTE) double-single mesh using GPU vertex shaders.
    pub fn render_rte_mesh(
        &mut self,
        device: &mut GpuDevice,
        uniforms: &GpuRteUniforms,
        vertices: &[GpuRteVertex],
        indices: &[u32],
        viewport: Rect,
        clip: Option<Rect>,
    ) {
        let mut encoder = device.create_command_encoder();

        let pass_desc = GpuRenderPassDescriptor {
            color_texture_id: device.swapchain_texture.id,
            depth_texture_id: Some(device.depth_texture.id),
            clear_color: None,
            clear_depth: None,
            scissor_rect: clip.or(Some(viewport)),
        };

        encoder.begin_render_pass(pass_desc);

        // Upload RTE uniforms
        self.uniform_buffer.write_bytes(0, uniforms.as_bytes());

        // Upload vertex & index buffers if data provided
        if !vertices.is_empty() {
            let v_bytes = unsafe {
                std::slice::from_raw_parts(
                    vertices.as_ptr() as *const u8,
                    std::mem::size_of_val(vertices),
                )
            };
            self.vertex_buffer.write_bytes(0, v_bytes);
        }

        if !indices.is_empty() {
            let i_bytes = unsafe {
                std::slice::from_raw_parts(
                    indices.as_ptr() as *const u8,
                    std::mem::size_of_val(indices),
                )
            };
            self.index_buffer.write_bytes(0, i_bytes);
            encoder.draw_indexed(indices.len(), 1);
        } else {
            encoder.draw_indexed(36, 1);
        }

        encoder.end_render_pass();
        device.submit(encoder);
    }
}

fn sample_textured_material_color(
    mat: &crate::scene3d::TexturedMaterial3D,
    textures: Option<&crate::texture3d::TextureStore>,
) -> [f32; 4] {
    let (tr, tg, tb, ta) = mat.tint.to_f32();
    let opacity = mat.opacity.clamp(0.0, 1.0);
    if let Some(store) = textures {
        if let Some(tex) = store.get(mat.texture) {
            let uv = mat.uv_transform.apply((0.5, 0.5));
            let sampled = crate::texture3d::sample_texture(tex, uv);
            return [
                (sampled[0] as f32 / 255.0) * tr,
                (sampled[1] as f32 / 255.0) * tg,
                (sampled[2] as f32 / 255.0) * tb,
                (sampled[3] as f32 / 255.0) * ta * opacity,
            ];
        }
    }
    [tr, tg, tb, ta * opacity]
}