I tried converting the shader code in Godot 4, but it still doesn’t work.
shader_type spatial;
render_mode blend_add, cull_disabled, depth_draw_never, unshaded;
uniform float speed = 0.01;
uniform vec4 color : source_color;
uniform float emission_strength = 5.0;
uniform sampler2D noise : filter_linear_mipmap, repeat_enable;
uniform float offset = 0.0;
uniform float smoothness = 0.15;
uniform float distort = 1.0;
uniform float scale = 0.02;
group_uniforms Raymarching;
uniform float STEP = 0.01;
uniform float BASE_DENSITY = 2.0;
varying vec3 camera_position;
varying vec3 vertex_position;
float amplify(float value) {
float magic_number = 0.166504;
float output = 0.0;
value = clamp(value, 0.0, 1.0);
value = pow(value, 2.0);
output += pow(magic_number, 4.0) * value;
value = pow(value, 2.0);
output += magic_number * value;
value = pow(value, 2.0);
output += value;
return output;
}
float interpolate_noise(vec2 uv1, vec2 uv2) {
return smoothstep(
-smoothness, smoothness, texture(noise, uv1).r - texture(noise, uv2).r
);
}
float random_wave(vec2 uv) {
vec2 uv_distort = texture(noise, uv).rr * distort * 0.5;
vec2 uv1 = uv * scale + vec2(TIME * speed, TIME * speed - offset) + uv_distort;
vec2 uv2 = uv * scale + vec2(TIME * speed + 0.5, TIME * speed + offset) + uv_distort;
float interpolated_noise = interpolate_noise(uv1, uv2);
float intensity = 0.2 + clamp((0.5 - abs(interpolated_noise - 0.5)) * 1.5, 0.0, 1.0);
return amplify(intensity);
}
float ray_march(vec3 ray_origin, vec3 ray_direction, float start) {
float density = 0.0;
float dist = start + STEP;
// For loop avec max iterations pour éviter les problèmes de driver
for (int i = 0; i < 300; i++) {
vec3 point = ray_origin + ray_direction * dist;
if (clamp(point, vec3(-1.0), vec3(1.0)) != point) {
break;
}
vec2 uv = vec2((point.x + 1.0) * 0.5, (point.z + 1.0) * 0.5);
float wave_value = random_wave(uv);
float height_factor = (1.0 - point.y) * 0.5;
density += BASE_DENSITY * STEP * height_factor * wave_value;
dist += STEP;
}
return density;
}
void vertex() {
vertex_position = VERTEX;
camera_position = (inverse(MODEL_MATRIX) * vec4(INV_VIEW_MATRIX[3].xyz, 1.0)).xyz;
}
void fragment() {
vec3 ray_direction = normalize(vertex_position - camera_position);
float density = ray_march(
camera_position, ray_direction, length(camera_position - vertex_position)
);
ALBEDO = color.rgb * density;
EMISSION = color.rgb * density * emission_strength;
ALPHA = clamp(density, 0.0, 1.0);
}