This repository has been archived by the owner on Feb 14, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ascii_tilemap_shadermaterial.tres
50 lines (43 loc) · 1.8 KB
/
ascii_tilemap_shadermaterial.tres
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
[gd_resource type="ShaderMaterial" load_steps=3 format=3 uid="uid://bc7kfuwl8i4xp"]
[ext_resource type="Texture2D" uid="uid://kdm0pyu6qvmo" path="res://images/cp437_14x14.png" id="1"]
[sub_resource type="Shader" id="1"]
code = "shader_type canvas_item;
uniform sampler2D tileset;
uniform vec2 glyph_size;
uniform sampler2D tilemap_ids;
uniform sampler2D tilemap_colors;
uniform vec2 tilemap_size;
float srgb_to_linear(float srgb) {
if(srgb <= 0.0031308) {
return srgb * 12.92;
} else {
return pow(srgb, 1.0 / 2.4) * 1.055 - 0.055;
}
}
vec4 vec4_srgb_to_linear(vec4 srgb) {
return vec4(srgb_to_linear(srgb.r), srgb_to_linear(srgb.g), srgb_to_linear(srgb.b), srgb.a);
}
void fragment() {
// Get tile ID in tileset from tilemap_data
ivec2 tilemap_data_space = ivec2(UV * vec2(tilemap_size));
lowp int tile_id = int(texelFetch(tilemap_ids, tilemap_data_space, 0).r * 255.);
// Get foreground and background colors from tilemap_data
tilemap_data_space.x *= 2;
vec4 fg = vec4_srgb_to_linear(texelFetch(tilemap_colors, ivec2(tilemap_data_space.x, tilemap_data_space.y), 0));
vec4 bg = vec4_srgb_to_linear(texelFetch(tilemap_colors, ivec2(tilemap_data_space.x + 1, tilemap_data_space.y), 0));
// Get either black or white from the tileset
// Choose the foreground color if white, background color if black
ivec2 tileset_coord = ivec2(tile_id % 16, int(float(tile_id) / 16.));
ivec2 position_within_glyph = ivec2(UV * tilemap_size * glyph_size) % ivec2(glyph_size);
vec4 tileset_color = texelFetch(tileset, (tileset_coord * ivec2(glyph_size)) + position_within_glyph, 0);
if (tileset_color == vec4(1.)) {
COLOR = fg;
} else {
COLOR = bg;
}
}"
[resource]
shader = SubResource("1")
shader_parameter/glyph_size = Vector2(14, 14)
shader_parameter/tilemap_size = Vector2(21, 16)
shader_parameter/tileset = ExtResource("1")