From 611c95880b9e7a194352b45db79867670c55eb4f Mon Sep 17 00:00:00 2001 From: Segey Lapin Date: Tue, 26 Oct 2021 23:03:23 +0300 Subject: [PATCH] Added terrain.gshader --- terrain.gdshader | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 terrain.gdshader diff --git a/terrain.gdshader b/terrain.gdshader new file mode 100644 index 0000000..a21bbd4 --- /dev/null +++ b/terrain.gdshader @@ -0,0 +1,31 @@ +shader_type spatial; +// Bitmask telling which of the 6 faces of the block are bordered by a block of lower resolution +uniform int u_transition_mask; + +vec3 get_transvoxel_position(vec3 vertex_pos, vec4 vertex_col) { + + int border_mask = int(vertex_col.a); + int cell_border_mask = border_mask & 63; // Which sides the cell is touching + int vertex_border_mask = (border_mask >> 6) & 63; // Which sides the vertex is touching + + // If the vertex is near a side where there is a low-resolution neighbor, + // move it to secondary position + int m = u_transition_mask & (cell_border_mask & 63); + float t = float(m != 0); + + // If the vertex lies on one or more sides, and at least one side has no low-resolution neighbor, + // don't move the vertex. + t *= float((vertex_border_mask & ~u_transition_mask) == 0); + + // Position to use when border mask matches + vec3 secondary_position = vertex_col.rgb; + return mix(vertex_pos, secondary_position, t); +} + +void vertex() { + VERTEX = get_transvoxel_position(VERTEX, COLOR); +} + +void fragment() { + ALBEDO = vec3(1, 1, 0); +}