bevy_fast_tilemap 0.8.1

A GPU accelerated tilemap for bevy
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
#import bevy_sprite::{
    mesh2d_bindings::mesh,
    mesh2d_functions::{get_world_from_local, mesh2d_position_local_to_clip, mesh2d_position_local_to_world},
}
#import mesh_view_bindings::globals;

struct ExtractIn {
    /// tile_index: Index of the tile in the atlas 0-based, x-axis first
    tile_index: u32,
    /// 2d logical map position
    tile_position: vec2<i32>,
    /// offset from the tile anchor point in pixel/world coordinates to render
    tile_offset: vec2<f32>,
    animation_state: f32,
};

#[user_code]

struct Map {
    /// Size of the map, in tiles.
    /// Will be derived from underlying map texture.
    map_size: vec2<u32>,

    /// Size of the tile atlas, in pixels.
    /// Will be derived from the tile atlas texture.
    atlas_size: vec2<f32>,

    /// Size of each tile, in pixels.
    tile_size: vec2<f32>,

    /// Tiles in the atlas are bigger than `tile_size` by this factor to allow a pattern effect
    atlas_tile_size_factor: i32,

    /// Padding between tiles in atlas.
    inner_padding: vec2<f32>,

    /// Padding at atlas top/left and bottom/right
    outer_padding_topleft: vec2<f32>,
    outer_padding_bottomright: vec2<f32>,

    /// Relative anchor point position in a tile (in [0..1]^2)
    tile_anchor_point: vec2<f32>,

    /// fractional 2d map index -> relative local world pos
    projection: mat3x3<f32>,

    /// Global transform of the entity holding the map as transformation matrix & offset.
    /// This is currently redundant with mesh.model,
    /// which should represent the same info as a 4x4 affine matrix, but we consider it a bit
    /// more consistent in conjunction with the inverse below. May be removed in the future.
    global_transform_matrix: mat3x3<f32>,
    global_transform_translation: vec3<f32>,

    // -----
    /// [derived] Size of the map in world units necessary to display
    /// all tiles according to projection.
    world_size: vec2<f32>,

    /// [derived] Offset of the projected map in world coordinates
    world_offset: vec2<f32>,

    /// [derived] Number of tiles in atlas
    n_tiles: vec2<u32>,

    /// [derived] local world pos -> fractional 2d map index
    inverse_projection: mat2x2<f32>,

    /// [derived] Inverse of global transform of the entity holding the map as transformation matrix & offset.
    global_inverse_transform_matrix: mat3x3<f32>,
    global_inverse_transform_translation: vec3<f32>,
};

@group(2) @binding(0)
var<uniform> map: Map;

@group(2) @binding(1)
var<uniform> user_data: UserData;

@group(2) @binding(100)
var<storage> map_texture: array<u32>;

@group(2) @binding(101)
var atlas_texture: texture_2d<f32>;

@group(2) @binding(102)
var atlas_sampler: sampler;


struct Vertex {
    @builtin(instance_index) instance_index: u32,
    @location(0) position: vec3<f32>,
    @location(1) map_position: vec2<f32>,
    @location(2) mix_color: vec4<f32>,
    @location(3) animation_state: f32,
};

struct VertexOutput {
    // this is `clip position` when the struct is used as a vertex stage output
    // and `frag coord` when used as a fragment stage input
    @builtin(position) position: vec4<f32>,
    @location(0) world_position: vec4<f32>,
    @location(1) map_position: vec2<f32>,
    @location(2) mix_color: vec4<f32>,
    @location(3) animation_state: f32,
}

/// Custom vertex shader for passing along the UV coordinate
@vertex
fn vertex(v: Vertex) -> VertexOutput {
    var out: VertexOutput;

    var model: mat4x4<f32> = get_world_from_local(v.instance_index);

    out.position = mesh2d_position_local_to_clip(model, vec4<f32>(v.position, 1.0));
    out.world_position = mesh2d_position_local_to_world(model, vec4<f32>(v.position, 1.0));
    out.mix_color = v.mix_color;
    out.map_position = v.map_position;
    out.animation_state = v.animation_state;
    return out;
}

/// Position (world/pixel units) in tilemap atlas of the top left corner
/// of the tile with the given index
fn atlas_index_to_position(index: u32, tile_position: vec2<i32>) -> vec2<f32> {
    var index_f = f32(index);
    var index_y = floor(index_f / f32(map.n_tiles.x));
    var index_x = index_f - index_y * f32(map.n_tiles.x);

    var index2d = vec2<f32>(index_x, index_y);

    if map.atlas_tile_size_factor > 1 {
        return
            index2d * (map.tile_size * f32(map.atlas_tile_size_factor) + map.inner_padding)
            + map.outer_padding_topleft
            + map.tile_size * vec2<f32>(
                f32(tile_position.x % map.atlas_tile_size_factor),
                f32(tile_position.y % map.atlas_tile_size_factor)
            );
    }
    else {

        var pos = index2d * (map.tile_size + map.inner_padding) + map.outer_padding_topleft;
        return pos;
    }
}

/// Same as atlas_index_to_position, but allow control of all the parameters
fn atlas_index_to_position_direct(
    index: u32,
    tile_position: vec2<i32>,
    map_n_tiles: vec2<u32>,
    map_atlas_tile_size_factor: i32,
    map_inner_padding: vec2<f32>,
    map_outer_padding_topleft: vec2<f32>,
    map_tile_size: vec2<f32>,
) -> vec2<f32> {
    var index_f = f32(index);
    var index_y = floor(index_f / f32(map_n_tiles.x));
    var index_x = index_f - index_y * f32(map_n_tiles.x);
    var index2d = vec2<f32>(index_x, index_y);

    if map_atlas_tile_size_factor > 1 {
        return
            index2d * (map_tile_size * f32(map_atlas_tile_size_factor) + map_inner_padding)
            + map_outer_padding_topleft
            + map_tile_size * vec2<f32>(
                f32(tile_position.x % map_atlas_tile_size_factor),
                f32(tile_position.y % map_atlas_tile_size_factor)
            );
    }
    else {

        var pos = index2d * (map_tile_size + map_inner_padding) + map_outer_padding_topleft;
        return pos;
    }
}

/// Compute offset into the tile by world position and world position of tile reference point
fn world_to_tile_offset(world_position: vec2<f32>, world_tile_base: vec2<f32>) -> vec2<f32> {
    return vec2<f32>(1.0, -1.0) * (world_position - world_tile_base);
}

/// Sample tile from the tile atlas
/// tile_index: Index of the tile in the atlas
/// tile_offset: Offset from tile anchor point in pixel/world coordinates
fn _sample_tile(
    tile_index: u32,
    pos: MapPosition,
    animation_state: f32,
) -> vec4<f32> {

    var e: ExtractIn;
    e.tile_index = tile_index;
    e.tile_position = pos.tile;
    e.tile_offset = pos.offset;
    e.animation_state = animation_state;

    return sample_tile(e);
}

fn sample_tile_at(
    tile_index: u32,
    tile_position: vec2<i32>,
    tile_offset: vec2<f32>,
) -> vec4<f32> {
    // Tile start position in the atlas
    var tile_start = atlas_index_to_position(tile_index, tile_position);

    // Offset in pixels from tile_start to sample from
    // let DBG_tile_offset = vec2<f32>(0.0, 0.0);
    var rect_offset = tile_offset + map.tile_anchor_point * map.tile_size;
    var total_offset = tile_start + rect_offset;

    // At most half of the inner "padding" is still rendered
    // as overhang of any given tile.
    // Outer padding is not taken into account
    var max_overhang = map.inner_padding / 2.0;

    // Outside of "our" part of the padding, dont render anything as part of this tile,
    // as it might be used for overhang of a neighbouring tile in the tilemap
    if rect_offset.x < -max_overhang.x
        || rect_offset.y < -max_overhang.y
        || rect_offset.x >= (map.tile_size.x + max_overhang.x)
        || rect_offset.y >= (map.tile_size.y + max_overhang.y)
    {
        return vec4<f32>(0.0, 1.0, 0.0, 1.0);
    }

    return textureSample(
        atlas_texture, atlas_sampler, total_offset / map.atlas_size
    );
}

/// Same as sample_tile_at, but allow control of all the parameters
fn sample_tile_direct(
    /// The tile index to render
    tile_index: u32,
    /// Will be used for the correct offset for pattern tiles
    tile_position: vec2<i32>,
    /// Offset from the tile anchor point in pixel/world coordinates to render
    tile_offset: vec2<f32>,
    /// Tile anchor point. When in doubt, use map.tile_anchor_point.
    map_tile_anchor_point: vec2<f32>,
    /// Tile size in pixels. When in doubt, use map.tile_size.
    map_tile_size: vec2<f32>,
    /// Size of the atlas in tiles. When in doubt, use map.n_tiles.
    map_n_tiles: vec2<u32>,
    /// Size of the atlas in pixels. When in doubt, use map.atlas_size.
    map_atlas_size: vec2<f32>,
    /// Inner padding in the tile atlas. When in doubt, use map.inner_padding.
    map_inner_padding: vec2<f32>,
    /// Outer padding at atlas top/left. When in doubt, use map.outer_padding_topleft.
    map_outer_padding_topleft: vec2<f32>,
    /// Tile size factor. When in doubt, use map.atlas_tile_size_factor.
    map_atlas_tile_size_factor: i32,
    /// Texture
    atlas_texture: texture_2d<f32>,
    /// Sampler
    atlas_sampler: sampler,
) -> vec4<f32> {

    // Tile start position in the atlas
    var tile_start = atlas_index_to_position_direct(
        tile_index,
        tile_position,
        map_n_tiles,
        map_atlas_tile_size_factor,
        map_inner_padding,
        map_outer_padding_topleft,
        map_tile_size
    );

    // Offset in pixels from tile_start to sample from
    var rect_offset = tile_offset + map_tile_anchor_point * map_tile_size;
    var total_offset = tile_start + rect_offset;

    // At most half of the inner "padding" is still rendered
    // as overhang of any given tile.
    // Outer padding is not taken into account
    var max_overhang = map_inner_padding / 2.0;

    // Outside of "our" part of the padding, dont render anything as part of this tile,
    // as it might be used for overhang of a neighbouring tile in the tilemap
    if rect_offset.x < -max_overhang.x
        || rect_offset.y < -max_overhang.y
        || rect_offset.x >= (map_tile_size.x + max_overhang.x)
        || rect_offset.y >= (map_tile_size.y + max_overhang.y)
    {
        return vec4<f32>(0.0, 0.0, 0.0, 0.0);
    }

    return textureSample(
        atlas_texture, atlas_sampler, total_offset / map_atlas_size
    );
}

/// 2d map tile position and offset in map tile coordinates
struct MapPosition {
    /// The 2d tile position on the map
    tile: vec2<i32>,
    /// Offset in pixels/world coordinates from the reference position of that tile
    offset: vec2<f32>
};


///
fn get_tile_index(map_position: vec2<i32>) -> u32 {
    return map_texture[map_position.y * i32(map.map_size.x) + map_position.x];
}

fn get_tile_index_checked(map_position: vec2<i32>) -> u32 {
    if !is_valid_tile(map_position) {
        return 0u;
    }
    return get_tile_index(map_position);
}

/// Blend c1 on top of c0
fn blend(c0: vec4<f32>, c1: vec4<f32>) -> vec4<f32> {
    // See https://de.wikipedia.org/wiki/Alpha_Blending

    if c0.a == 0.0 && c1.a == 0.0 {
        return vec4<f32>(0.0, 0.0, 0.0, 0.0);
    }

    // If c1.a = 1, this is 1, if c1.a = 0, this is c0.a
    // That is if c1 is fully opaque, we take c1, otherwise we let some of c0 shine through
    let a_mix = c1.a + (1 - c1.a) * c0.a;
    var r = (c1 * c1.a + c0 * c0.a * (1 - c1.a)) / a_mix;
    r.a = a_mix;
    return r;
}

fn is_valid_tile(tile: vec2<i32>) -> bool {
    if tile.x < 0 || tile.y < 0 {
        return false;
    }
    let map_size = vec2<i32>(map.map_size);
    if tile.x >= map_size.x || tile.y >= map_size.y {
        return false;
    }
    return true;
}

///
///
/// tile_index: Tile index in the atlas
/// pos: The original map position
/// tile_offset: The offset of the tile (in number of whole tiles) to sample from
fn sample_neighbor_tile_index(tile_index: u32, pos_: MapPosition, tile_offset: vec2<i32>, animation_state: f32) -> vec4<f32> {
    // Position in the neighboring tile (in world coordinates),
    // that matches 'pos' in the original tile

    // TODO: Consider precomputing this before shader instantiation for the 8 possible offsets.
    var overhang = (map.projection * vec3<f32>(vec2<f32>(-tile_offset), 0.0)).xy * map.tile_size;

    var pos = pos_;
    pos.tile = pos.tile + tile_offset;
    pos.offset = pos.offset + vec2<f32>(1.0, -1.0) * overhang;
    return _sample_tile(tile_index, pos, animation_state);
}

/// pos: The map position to sample
/// tile_offset: The offset of the tile (in number of whole tiles) to sample from
fn sample_neighbor(pos: MapPosition, tile_offset: vec2<i32>, animation_state: f32) -> vec4<f32> {
    // integral position of the neighbouring tile
    var tile = pos.tile + tile_offset;
    if !is_valid_tile(tile) {
        return vec4<f32>(0.0, 0.0, 0.0, 0.0);
    }

    // kind of tile being displayed at that position
    var tile_index = get_tile_index(tile);
    return sample_neighbor_tile_index(tile_index, pos, tile_offset, animation_state);
}

fn render_dominance_overhangs(color: vec4<f32>, index: u32, pos: MapPosition, animation_state: f32) -> vec4<f32> {
    var c = color;

    // We want to render overhangs from all the neighbors where the tile index is greater than the
    // current tile index. More so we want to render them in order of tile index (from lowest to
    // highest) to ensure that the overhangs are rendered in the correct order.

    // First, collect all the indices of the neighbors that are greater than the current tile index
    var neighbor_offsets = array<vec2<i32>, 8>(
        vec2<i32>(-1, -1),
        vec2<i32>(-1, 0),
        vec2<i32>(-1, 1),
        vec2<i32>(0, 1),
        vec2<i32>(1, 1),
        vec2<i32>(1, 0),
        vec2<i32>(1, -1),
        vec2<i32>(0, -1),
    );
    var neighbors: array<u32, 8> = array<u32, 8>(
        get_tile_index_checked(pos.tile + neighbor_offsets[0]),
        get_tile_index_checked(pos.tile + neighbor_offsets[1]),
        get_tile_index_checked(pos.tile + neighbor_offsets[2]),
        get_tile_index_checked(pos.tile + neighbor_offsets[3]),
        get_tile_index_checked(pos.tile + neighbor_offsets[4]),
        get_tile_index_checked(pos.tile + neighbor_offsets[5]),
        get_tile_index_checked(pos.tile + neighbor_offsets[6]),
        get_tile_index_checked(pos.tile + neighbor_offsets[7])
    );

    // Then, sort the neighbors by index
    for (var i = 0u; i < 8u; i = i + 1u) {
        for (var j = i + 1u; j < 8u; j = j + 1u) {
            if neighbors[i] > neighbors[j] {
                var tmp = neighbors[i];
                neighbors[i] = neighbors[j];
                neighbors[j] = tmp;

                var tmp1 = neighbor_offsets[i];
                neighbor_offsets[i] = neighbor_offsets[j];
                neighbor_offsets[j] = tmp1;
            }
        }
    }

    // Finally, render the overhangs in order of index
    for (var i = 0u; i < 8u; i = i + 1u) {
        if neighbors[i] > index {
            c = blend(c, sample_neighbor_tile_index(neighbors[i], pos, neighbor_offsets[i], animation_state));
        }
    }

    return c;
}

/// Render underhangs for perspective projection
/// color: The color of the current fragment so far
/// pos: The position of the current fragment as map position
fn render_perspective_underhangs(color: vec4<f32>, pos: MapPosition, animation_state: f32) -> vec4<f32> {
    var c = color;

    // Form is PERSPECTIVE_UNDER_{X}{Y}
    // whereas {X} and {Y} are replaced with one of:
    // N: Negative (-1)
    // P: Positive (1)
    // Z: Zero (0)
    #ifdef PERSPECTIVE_UNDER_NN
        c = blend(c, sample_neighbor(pos, vec2<i32>( -1, -1), animation_state));
    #endif

    #ifdef PERSPECTIVE_UNDER_NP
        c = blend(c, sample_neighbor(pos, vec2<i32>( -1,  1), animation_state));
    #endif

    #ifdef PERSPECTIVE_UNDER_PN
        c = blend(c, sample_neighbor(pos, vec2<i32>(  1, -1), animation_state));
    #endif

    #ifdef PERSPECTIVE_UNDER_PP
        c = blend(c, sample_neighbor(pos, vec2<i32>(  1,  1), animation_state));
    #endif

    #ifdef PERSPECTIVE_UNDER_ZN
        c = blend(c, sample_neighbor(pos, vec2<i32>(  0, -1), animation_state));
    #endif

    #ifdef PERSPECTIVE_UNDER_NZ
        c = blend(c, sample_neighbor(pos, vec2<i32>( -1,  0), animation_state));
    #endif

    #ifdef PERSPECTIVE_UNDER_ZP
        c = blend(c, sample_neighbor(pos, vec2<i32>(  0,  1), animation_state));
    #endif

    #ifdef PERSPECTIVE_UNDER_PZ
        c = blend(c, sample_neighbor(pos, vec2<i32>(  1,  0), animation_state));
    #endif

    return c;
}


fn render_perspective_overhangs(color: vec4<f32>, pos: MapPosition, animation_state: f32) -> vec4<f32> {
    var c = color;

    #ifdef PERSPECTIVE_UNDER_ZN
        c = blend(c, sample_neighbor(pos, vec2<i32>(  0,  1), animation_state));
    #endif

    #ifdef PERSPECTIVE_UNDER_NZ
        c = blend(c, sample_neighbor(pos, vec2<i32>(  1,  0), animation_state));
    #endif

    #ifdef PERSPECTIVE_UNDER_ZP
        c = blend(c, sample_neighbor(pos, vec2<i32>(  0, -1), animation_state));
    #endif

    #ifdef PERSPECTIVE_UNDER_PZ
        c = blend(c, sample_neighbor(pos, vec2<i32>( -1,  0), animation_state));
    #endif

    #ifdef PERSPECTIVE_UNDER_NN
        c = blend(c, sample_neighbor(pos, vec2<i32>(  1,  1), animation_state));
    #endif

    #ifdef PERSPECTIVE_UNDER_NP
        c = blend(c, sample_neighbor(pos, vec2<i32>(  1, -1), animation_state));
    #endif

    #ifdef PERSPECTIVE_UNDER_PN
        c = blend(c, sample_neighbor(pos, vec2<i32>( -1,  1), animation_state));
    #endif

    #ifdef PERSPECTIVE_UNDER_PP
        c = blend(c, sample_neighbor(pos, vec2<i32>( -1, -1), animation_state));
    #endif

    return c;
}

@fragment
fn fragment(
    in: VertexOutput
) -> @location(0) vec4<f32> {
    var world_position = in.world_position.xy;
    var color = vec4<f32>(0.0, 0.0, 0.0, 0.0);

    var tile = floor(in.map_position);
    var map_space_offset = in.map_position - tile;

    var world_space_offset = map.global_transform_matrix * (
        map.projection * vec3<f32>(map_space_offset, 0.0)
    ) * vec3<f32>(map.tile_size, 1.0);

    var pos: MapPosition;
    pos.tile = vec2<i32>(tile);
    pos.offset = vec2<f32>(1.0, -1.0) * world_space_offset.xy;

    var index = get_tile_index(pos.tile);
    var is_valid = is_valid_tile(pos.tile);
    var sample_color = color;

    if is_valid {
        sample_color = _sample_tile(index, pos, in.animation_state);
    }
    else {
        // for invalid tile, assume low index so (almost) everything overlaps in dominance rendering
        index = 0u;
    }

    #ifdef PERSPECTIVE_UNDERHANGS
    if sample_color.a < 1.0 {
        color = render_perspective_underhangs(color, pos, in.animation_state);
    }
    #endif // PERSPECTIVE_UNDERHANGS

    if is_valid {
        color = blend(color, sample_color);
    }

    #ifdef DOMINANCE_OVERHANGS
        color = render_dominance_overhangs(color, index, pos, in.animation_state);
    #endif

    #ifdef PERSPECTIVE_OVERHANGS
        color = render_perspective_overhangs(color, pos, in.animation_state);
    #endif

    color = color * in.mix_color;

    return color;
}