44shader_type spatial ;
55render_mode blend_mix ,depth_draw_opaque ,cull_back ,diffuse_burley ,specular_schlick_ggx ,skip_vertex_transform ;
66
7- // Defined Constants
8- #define SKIP_PASS 0
9- #define VERTEX_PASS 1
10- #define FRAGMENT_PASS 2
11-
127#if CURRENT_RENDERER == RENDERER_COMPATIBILITY
138 #define fma (a , b , c ) ((a ) * (b ) + (c ))
149 #define dFdxCoarse (a ) dFdx (a )
@@ -17,15 +12,16 @@ render_mode blend_mix,depth_draw_opaque,cull_back,diffuse_burley,specular_schlic
1712
1813// Private uniforms
1914// Commented uniforms aren't needed for this shader, but are available for your own needs.
15+ group_uniforms shader_uniforms ;
2016uniform vec3 _target_pos = vec3 (0.f );
2117uniform float _mesh_size = 48.f ;
2218uniform float _subdiv = 1.f ;
2319uniform uint _background_mode = 1u ; // NONE = 0, FLAT = 1, NOISE = 2
2420uniform uint _mouse_layer = 0x80000000u; // Layer 32
2521uniform float _vertex_spacing = 1.0 ;
26- uniform float _vertex_density = 1.0 ; // = 1/_vertex_spacing
22+ uniform float _vertex_density = 1.0 ; // = 1. /_vertex_spacing
2723uniform float _region_size = 1024.0 ;
28- uniform float _region_texel_size = 0.0009765625 ; // = 1/1024
24+ uniform float _region_texel_size = 0.0009765625 ; // = 1./region_size
2925uniform int _region_map_size = 32 ;
3026uniform int _region_map [1024 ];
3127//uniform vec2 _region_locations[1024];
@@ -41,9 +37,12 @@ uniform highp sampler2DArray _control_maps : repeat_disable;
4137//uniform highp sampler2DArray _color_maps : source_color, filter_linear_mipmap_anisotropic, repeat_disable;
4238//uniform highp sampler2DArray _texture_array_albedo : source_color, filter_linear_mipmap_anisotropic, repeat_enable;
4339//uniform highp sampler2DArray _texture_array_normal : hint_normal, filter_linear_mipmap_anisotropic, repeat_enable;
40+ group_uniforms ;
4441
45- // Public Uniforms
42+ // Public uniforms
43+ group_uniforms shader_uniforms .general ;
4644uniform bool flat_terrain_normals = false ;
45+ group_uniforms ;
4746
4847// Varyings & Types
4948// Some are required for editor functions
@@ -55,24 +54,16 @@ varying vec3 v_camera_pos;
5554// Vertex
5655////////////////////////
5756
58- // Takes in world space XZ (UV) coordinates & search depth (only applicable for background mode none)
57+ // Takes in world space XZ (UV) coordinates
5958// Returns ivec3 with:
6059// XY: (0 to _region_size - 1) coordinates within a region
6160// Z: layer index used for texturearrays, -1 if not in a region
62- ivec3 get_index_coord (const vec2 uv , const int search ) {
61+ ivec3 get_index_coord (const vec2 uv ) {
6362 vec2 r_uv = round (uv );
64- vec2 o_uv = mod (r_uv ,_region_size );
65- ivec2 pos ;
66- int bounds , layer_index = - 1 ;
67- for (int i = - 1 ; i < clamp (search , SKIP_PASS , FRAGMENT_PASS ); i ++ ) {
68- if ((layer_index == - 1 && _background_mode == 0u ) || i < 0 ) {
69- r_uv -= i == - 1 ? vec2 (0.0 ) : vec2 (float (o_uv .x <= o_uv .y ), float (o_uv .y <= o_uv .x ));
70- pos = ivec2 (floor ((r_uv ) * _region_texel_size )) + (_region_map_size / 2 );
71- bounds = int (uint (pos .x | pos .y ) < uint (_region_map_size ));
72- layer_index = (_region_map [ pos .y * _region_map_size + pos .x ] * bounds - 1 );
73- }
74- }
75- return ivec3 (ivec2 (mod (r_uv ,_region_size )), layer_index );
63+ ivec2 pos = ivec2 (floor (r_uv * _region_texel_size )) + (_region_map_size / 2 );
64+ int bounds = int (uint (pos .x | pos .y ) < uint (_region_map_size ));
65+ int layer_index = _region_map [pos .y * _region_map_size + pos .x ] * bounds - 1 ;
66+ return ivec3 (ivec2 (mod (r_uv , _region_size )), layer_index );
7667}
7768
7869// Takes in world space XZ (UV) and returns if the coordinate should be part of the NONE background.
@@ -89,10 +80,10 @@ float interpolated_height(vec2 pos) {
8980 const vec2 offsets = vec2 (0 , 1 );
9081 vec2 index_id = floor (pos );
9182 ivec3 index [4 ];
92- index [0 ] = get_index_coord (index_id + offsets .xy , VERTEX_PASS );
93- index [1 ] = get_index_coord (index_id + offsets .yy , VERTEX_PASS );
94- index [2 ] = get_index_coord (index_id + offsets .yx , VERTEX_PASS );
95- index [3 ] = get_index_coord (index_id + offsets .xx , VERTEX_PASS );
83+ index [0 ] = get_index_coord (index_id + offsets .xy );
84+ index [1 ] = get_index_coord (index_id + offsets .yy );
85+ index [2 ] = get_index_coord (index_id + offsets .yx );
86+ index [3 ] = get_index_coord (index_id + offsets .xx );
9687 float h0 = texelFetch (_height_maps , index [0 ], 0 ).r ;
9788 float h1 = texelFetch (_height_maps , index [1 ], 0 ).r ;
9889 float h2 = texelFetch (_height_maps , index [2 ], 0 ).r ;
@@ -139,7 +130,7 @@ void vertex() {
139130 UV2 = fma (UV , vec2 (_region_texel_size ), vec2 (0.5 * _region_texel_size ));
140131
141132 // Discard vertices for Holes. 1 lookup
142- ivec3 v_region = get_index_coord (start_pos , VERTEX_PASS );
133+ ivec3 v_region = get_index_coord (start_pos );
143134 uint control = floatBitsToUint (texelFetch (_control_maps , v_region , 0 )).r ;
144135 bool hole = bool (control >> 2u & 0x1u);
145136
@@ -155,8 +146,8 @@ void vertex() {
155146 if (scale < _vertex_spacing ) {
156147 h = interpolated_height (UV );
157148 } else {
158- ivec3 coord_a = get_index_coord (start_pos , VERTEX_PASS );
159- ivec3 coord_b = get_index_coord (end_pos , VERTEX_PASS );
149+ ivec3 coord_a = get_index_coord (start_pos );
150+ ivec3 coord_b = get_index_coord (end_pos );
160151 h = mix (texelFetch (_height_maps , coord_a , 0 ).r ,texelFetch (_height_maps , coord_b , 0 ).r ,vertex_lerp );
161152 }
162153 v_vertex .y = h ;
@@ -200,10 +191,10 @@ void fragment() {
200191
201192 ivec3 index [4 ];
202193 // control map lookups, used for some normal lookups as well
203- index [0 ] = get_index_coord (index_id + offsets .xy , FRAGMENT_PASS );
204- index [1 ] = get_index_coord (index_id + offsets .yy , FRAGMENT_PASS );
205- index [2 ] = get_index_coord (index_id + offsets .yx , FRAGMENT_PASS );
206- index [3 ] = get_index_coord (index_id + offsets .xx , FRAGMENT_PASS );
194+ index [0 ] = get_index_coord (index_id + offsets .xy );
195+ index [1 ] = get_index_coord (index_id + offsets .yy );
196+ index [2 ] = get_index_coord (index_id + offsets .yx );
197+ index [3 ] = get_index_coord (index_id + offsets .xx );
207198
208199 // Terrain normals
209200 vec3 index_normal [4 ];
@@ -226,10 +217,10 @@ void fragment() {
226217 // 5 lookups
227218 // Fetch the additional required height values for smooth normals
228219 h [1 ] = texelFetch (_height_maps , index [1 ], 0 ).r ; // 3 (1,1)
229- float h_4 = texelFetch (_height_maps , get_index_coord (index_id + offsets .yz , FRAGMENT_PASS ), 0 ).r ; // 4 (1,2)
230- float h_5 = texelFetch (_height_maps , get_index_coord (index_id + offsets .zy , FRAGMENT_PASS ), 0 ).r ; // 5 (2,1)
231- float h_6 = texelFetch (_height_maps , get_index_coord (index_id + offsets .zx , FRAGMENT_PASS ), 0 ).r ; // 6 (2,0)
232- float h_7 = texelFetch (_height_maps , get_index_coord (index_id + offsets .xz , FRAGMENT_PASS ), 0 ).r ; // 7 (0,2)
220+ float h_4 = texelFetch (_height_maps , get_index_coord (index_id + offsets .yz ), 0 ).r ; // 4 (1,2)
221+ float h_5 = texelFetch (_height_maps , get_index_coord (index_id + offsets .zy ), 0 ).r ; // 5 (2,1)
222+ float h_6 = texelFetch (_height_maps , get_index_coord (index_id + offsets .zx ), 0 ).r ; // 6 (2,0)
223+ float h_7 = texelFetch (_height_maps , get_index_coord (index_id + offsets .xz ), 0 ).r ; // 7 (0,2)
233224
234225 // Calculate the normal for the remaining index ids.
235226 index_normal [0 ] = normalize (vec3 (h [0 ] - h [1 ] + u , _vertex_spacing , h [0 ] - h_7 + v ));
0 commit comments