geng_utils/
tiled.rs

1use geng::prelude::*;
2
3use crate::conversions::Vec2RealConversions;
4
5/// Tiles the `area` with the texture uv's.
6pub fn tile_area<T: Float>(
7    tile_size: vec2<T>,
8    offset: vec2<T>,
9    area: Aabb2<T>,
10) -> Vec<draw2d::TexturedVertex> {
11    tile_area_subtexture(
12        Aabb2::ZERO.extend_positive(vec2::splat(1.0)),
13        tile_size,
14        offset,
15        area,
16    )
17}
18
19/// Tiles the `area` with the subtexture uv's.
20pub fn tile_area_subtexture<T: Float>(
21    sub_texture: Aabb2<f32>,
22    tile_size: vec2<T>,
23    offset: vec2<T>,
24    area: Aabb2<T>,
25) -> Vec<draw2d::TexturedVertex> {
26    let tile_size = tile_size.map(T::as_f32);
27    let offset = offset.map(T::as_f32);
28    let area = area.map(T::as_f32);
29
30    let [a, b, c, d] = crate::geometry::unit_quad();
31    let [va, vb, vc, vd] = sub_texture.corners();
32    let set_uv = |v, uv| draw2d::TexturedVertex { a_vt: uv, ..v };
33    let unit = [set_uv(a, va), set_uv(b, vb), set_uv(c, vc), set_uv(d, vd)];
34
35    let tiles = (area.size() / tile_size).map(|x| x.ceil() as usize);
36    (0..tiles.x)
37        .flat_map(|x| {
38            (0..tiles.y).flat_map(move |y| {
39                let vs = unit.map(|v| draw2d::TexturedVertex {
40                    a_pos: area.bottom_left() + offset + vec2(x, y).as_f32() * tile_size,
41                    ..v
42                });
43                let indices = [0, 1, 2, 0, 2, 3];
44                indices.map(|i| vs[i])
45            })
46        })
47        .collect()
48}