tawasuyu/llimphi
Sovereign retained-mode GPU UI framework in Rust (wgpu+vello+taffy+parley), Elm loop, 40+ widgets.
chore: refresco desde el monorepo — vello 0.9 / wgpu 29, 120 miembros
1//! Prueba de **sombras suaves** de una luz de interior: un piso con un par de
2//! objetos, iluminado por la lámpara como fuente de área (`PointLight.radius`) →
3//! penumbra que se abre con la distancia. `point_shadows` activo.
4//!
5//! `cargo run -p llimphi-voxel --example lit_demo --release` → `/tmp/window.png`
6
7use std::fs::File;
8use std::io::BufWriter;
9
10use llimphi_3d::glam::Vec3;
11use llimphi_3d::{Atmosphere, Camera3d, Scene3d, VoxelGrid, VoxelRenderer};
12use llimphi_hal::{wgpu, Hal};
13use llimphi_raster::peniko::Color;
14use llimphi_raster::{vello, Renderer};
15use llimphi_voxel::{stamp_mueble, Material, Mueble};
16
17const W: u32 = 1100;
18const H: u32 = 640;
19const FMT: wgpu::TextureFormat = wgpu::TextureFormat::Rgba8Unorm;
20
21fn main() {
22 let dim = [56u32, 44, 52];
23 let mut g = VoxelGrid::new(dim);
24 let (x0, x1, z0, z1) = (10i32, 44, 10, 42);
25 let floor_h = 3i32;
26 let wall_top = floor_h + 16;
27
28 let wall = Material::Hormigon.color();
29 let wa = VoxelGrid::shine_to_alpha(0.0);
30 let glass = Material::Vidrio.color();
31 let ga = VoxelGrid::shine_to_alpha(Material::Vidrio.brillo());
32 let put = |g: &mut VoxelGrid, x: i32, y: i32, z: i32, c: [u8; 3], a: u8| {
33 if x >= 0 && y >= 0 && z >= 0 {
34 g.set_rgba(x as u32, y as u32, z as u32, c, a);
35 }
36 };
37
38 // Piso.
39 for x in x0..=x1 {
40 for z in z0..=z1 {
41 for y in 0..floor_h {
42 put(&mut g, x, y, z, Material::Ceramica.color(), VoxelGrid::shine_to_alpha(0.0));
43 }
44 }
45 }
46 // Muros (menos la pared frontal z=z1 → cutaway para la cámara) + techo.
47 for y in floor_h..wall_top {
48 for x in x0..=x1 {
49 put(&mut g, x, y, z0, wall, wa); // pared del fondo
50 }
51 for z in z0..=z1 {
52 put(&mut g, x0, y, z, wall, wa);
53 put(&mut g, x1, y, z, wall, wa);
54 }
55 }
56 for x in x0..=x1 {
57 for z in z0..=z1 {
58 put(&mut g, x, wall_top, z, wall, wa); // techo
59 }
60 }
61 // Ventana de vidrio en la pared del fondo (z0), centrada, a media altura.
62 let wy0 = floor_h + 5;
63 for x in 19..=35 {
64 for y in wy0..(wy0 + 7) {
65 put(&mut g, x, y, z0, glass, ga);
66 }
67 }
68 // Un par de objetos para que se vea el charco de luz y su sombra.
69 stamp_mueble(&mut g, Mueble::Silla, 27, floor_h as u32, 24, 0);
70 stamp_mueble(&mut g, Mueble::Ficus, 34, floor_h as u32, 22, 0);
71
72 let hal = pollster::block_on(Hal::new(None)).expect("hal");
73 let mut renderer = Renderer::new(&hal).expect("renderer");
74 let mut vr = VoxelRenderer::new(&hal.device, &hal.queue, FMT, &g);
75 // Sol BAJO detrás del fondo (-z) → el haz entra por la ventana al piso.
76 vr.sun_dir = [0.1, 0.62, -0.62];
77 vr.atmosphere = Atmosphere {
78 sky_zenith: [8, 10, 18], // interior casi a oscuras → el haz destaca
79 sky_horizon: [70, 80, 110],
80 fog_density: 0.0,
81 god_rays: 0.0,
82 };
83 vr.point_shadows = true;
84 vr.lights = vec![]; // sólo luz del día
85 eprintln!("cuarto cerrado con una ventana; sol bajo por el fondo");
86
87 let camera = Camera3d::orbit(
88 Vec3::new(0.0, 0.0, 2.0),
89 4_f32.to_radians(),
90 4_f32.to_radians(),
91 dim[0] as f32 * 0.8,
92 );
93 let mut scene = Scene3d::new();
94 let pixels = render(&hal, &mut renderer, &mut scene, &mut vr, &camera);
95 write_png(&pixels, "/tmp/window.png");
96 eprintln!("escrito /tmp/window.png");
97}
98
99fn render(
100 hal: &Hal,
101 renderer: &mut Renderer,
102 scene: &mut Scene3d,
103 vr: &mut VoxelRenderer,
104 camera: &Camera3d,
105) -> Vec<u8> {
106 let inter = hal.device.create_texture(&wgpu::TextureDescriptor {
107 label: Some("inter"),
108 size: wgpu::Extent3d { width: W, height: H, depth_or_array_layers: 1 },
109 mip_level_count: 1,
110 sample_count: 1,
111 dimension: wgpu::TextureDimension::D2,
112 format: FMT,
113 usage: wgpu::TextureUsages::STORAGE_BINDING
114 | wgpu::TextureUsages::TEXTURE_BINDING
115 | wgpu::TextureUsages::RENDER_ATTACHMENT
116 | wgpu::TextureUsages::COPY_SRC,
117 view_formats: &[],
118 });
119 let view = inter.create_view(&wgpu::TextureViewDescriptor::default());
120 renderer
121 .render_to_view(hal, &vello::Scene::new(), &view, W, H, Color::from_rgba8(0, 0, 0, 255))
122 .expect("base");
123 let mut enc = hal
124 .device
125 .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("lit") });
126 scene.render(&hal.device, &hal.queue, &mut enc, &view, (W, H), camera, Some(vr), &[]);
127 hal.queue.submit(std::iter::once(enc.finish()));
128 let _ = hal.device.poll(wgpu::PollType::wait_indefinitely());
129 readback(hal, &inter)
130}
131
132fn readback(hal: &Hal, target: &wgpu::Texture) -> Vec<u8> {
133 let unpadded = (W * 4) as usize;
134 let align = wgpu::COPY_BYTES_PER_ROW_ALIGNMENT as usize;
135 let padded = unpadded.div_ceil(align) * align;
136 let buf = hal.device.create_buffer(&wgpu::BufferDescriptor {
137 label: Some("readback"),
138 size: (padded * H as usize) as u64,
139 usage: wgpu::BufferUsages::MAP_READ | wgpu::BufferUsages::COPY_DST,
140 mapped_at_creation: false,
141 });
142 let mut enc = hal
143 .device
144 .create_command_encoder(&wgpu::CommandEncoderDescriptor { label: None });
145 enc.copy_texture_to_buffer(
146 wgpu::TexelCopyTextureInfo {
147 texture: target,
148 mip_level: 0,
149 origin: wgpu::Origin3d::ZERO,
150 aspect: wgpu::TextureAspect::All,
151 },
152 wgpu::TexelCopyBufferInfo {
153 buffer: &buf,
154 layout: wgpu::TexelCopyBufferLayout {
155 offset: 0,
156 bytes_per_row: Some(padded as u32),
157 rows_per_image: Some(H),
158 },
159 },
160 wgpu::Extent3d { width: W, height: H, depth_or_array_layers: 1 },
161 );
162 hal.queue.submit(std::iter::once(enc.finish()));
163 let slice = buf.slice(..);
164 let (tx, rx) = std::sync::mpsc::channel();
165 slice.map_async(wgpu::MapMode::Read, move |r| {
166 let _ = tx.send(r);
167 });
168 let _ = hal.device.poll(wgpu::PollType::wait_indefinitely());
169 rx.recv().unwrap().unwrap();
170 let data = slice.get_mapped_range();
171 let mut pixels = Vec::with_capacity((W * H * 4) as usize);
172 for row in 0..H as usize {
173 let srow = row * padded;
174 pixels.extend_from_slice(&data[srow..srow + unpadded]);
175 }
176 drop(data);
177 buf.unmap();
178 pixels
179}
180
181fn write_png(pixels: &[u8], path: &str) {
182 let file = File::create(path).expect("png");
183 let mut enc = png::Encoder::new(BufWriter::new(file), W, H);
184 enc.set_color(png::ColorType::Rgba);
185 enc.set_depth(png::BitDepth::Eight);
186 let mut wtr = enc.write_header().unwrap();
187 wtr.write_image_data(pixels).unwrap();
188}