Started from the backup so I’m working with a “clean” version from before any attempts at implementing terrain code were made.
First thing I’m noticing is adding render_mode world_vertex_coords; is what’s working with VERTEX *= mix(size_small, size_large, patch_factor); to cause the world origin thing/reduction of the grass. This is before making any additional changes to the code (or commenting anything out)/implementing any terrain code.
(ignore difference in plane color, is impostor system stuff)
With everything inside of void vertex() commented out, and the only addition to the code being render_mode world_vertex_coords; we start with this below:
From here I implemented the code below (with the void vertex() content being added at the end of void vertex() and got this result:
uniform sampler2d heightmap;
uniform float height_scale = 700.0;
uniform vec2 world_scale = vec2(8192.0, 8192.0);
vec3 vertex_world = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
vec2 world_uv = (vertex_world.xz / world_scale) + 0.5;
float height = texture(heightmap, world_uv).r;
VERTEX.y += height;
It’s pretty much the same.
Moving the grass around in altitude and up slopes shows nothing.
The Terrain3D height is closer to 690, rounded it to 700 for now, I’ll scale it to the exact decimal height once the grass is actually conforming.
Swapping to VERTEX.y += height * height_scale; does this below:
It moves, technically. Not sure where to go from here. Removing render_mode world_vertex_coords; removes all grass and leaves behind the impostor plane.
Just in case for clarity this is the entire grass.gdshader script currently
shader_type spatial;
render_mode cull_disabled, world_vertex_coords;
uniform float size_small = 0.2;
uniform float size_large = 0.6;
uniform float blade_bend = 0.5;
uniform vec3 color_small: source_color = vec3(0.3, 0.6, 0.1);
uniform vec3 color_large: source_color = vec3(0.9, 0.9, 0.2);
uniform sampler2D patch_noise;
uniform float patch_scale = 5.0;
varying float patch_factor;
uniform sampler2D wind_noise;
uniform float wind_strength = 0.1;
uniform vec2 wind_direction = vec2(1.0, 0.0);
uniform float wind_bend_strength = 2.0;
uniform float wind_ao_affect = 1.5;
uniform float object_radius = 1.0;
uniform vec3 object_position;
instance uniform float alpha = 1.0;
varying float bottom_to_top;
varying float current_wind_bend;
varying float cut_height;
uniform sampler2D heightmap;
uniform float height_scale = 700.0;
uniform vec2 world_scale = vec2(8192.0, 8192.0);
void vertex() {
//cut_height = 1.0;
//// Cutting all grass on the right
//if (NODE_POSITION_WORLD.x > 0.0) {
//cut_height = 0.5;
//
//VERTEX.y = min(VERTEX.y, cut_height);
//UV.y = max(UV.y, 1.0 - cut_height);
//}
//bottom_to_top = 1.0 - UV.y;
// Wind logic
//vec2 wind_position = NODE_POSITION_WORLD.xz / 10.0;
//wind_position -= (TIME + 8.0) * wind_direction * wind_strength;
//current_wind_bend = texture(wind_noise, wind_position).x;
//current_wind_bend *= wind_strength;
//current_wind_bend *= bottom_to_top * 2.0;
//mat4 inv_model = inverse(MODEL_MATRIX);
//vec2 local_direction = (inv_model * vec4(wind_direction.x, 0.0, wind_direction.y, 0.0)).xz;
//VERTEX.xz += current_wind_bend * local_direction * wind_bend_strength;
// Bend away from the object
//float object_distance = distance(object_position, NODE_POSITION_WORLD);
//float bend_away_strength = max(object_radius - object_distance, 0.0) / object_radius;
//vec2 bend_direction = normalize(object_position.xz - NODE_POSITION_WORLD.xz);
//VERTEX.xz -= (inv_model * vec4(bend_direction.x, 0.0, bend_direction.y, 0.0)).xz
//* bend_away_strength * bottom_to_top;
//VERTEX.y -= bend_away_strength * bottom_to_top * 0.5;
// General appearance
//VERTEX.z += blade_bend * pow(bottom_to_top, 2.0);
//patch_factor = texture(patch_noise, NODE_POSITION_WORLD.xz / patch_scale).r;
//VERTEX *= mix(size_small, size_large, patch_factor);
//NORMAL = mix(NORMAL, vec3(0.0, 1.0, 0.0), mix(1.0, bottom_to_top, alpha + 0.2));
vec3 vertex_world = (MODEL_MATRIX * vec4(VERTEX, 1.0)).xyz;
vec2 world_uv = (vertex_world.xz / world_scale) + 0.5;
float height = texture(heightmap, world_uv).r;
VERTEX.y += height * height_scale;
}
void fragment() {
if (!FRONT_FACING) NORMAL = -NORMAL;
AO = bottom_to_top - current_wind_bend * wind_ao_affect;
AO_LIGHT_AFFECT = mix(0.2, 1.0, alpha);
ALBEDO = mix(color_small, color_large, patch_factor);
BACKLIGHT = vec3(0.2);
ROUGHNESS = 0.4;
SPECULAR = 0.2;
ALPHA = alpha;
ALPHA_HASH_SCALE = 1.0;
}