Godot Version
4.2.2
Question
Hi, I am making a mandelbrot set zoomer in godot. i generated it using shaders for high iterations and resolutions. To move around, i tell the shader to increase of decreate a offset parameter. So by increasing offset.x i have moved the mandelbrot set to the right. And i also have a zoom parameter in my mandelbrot set shader which i increase or decrease to zoom.
However, The more i zoom, The faster it moves to the left, right, up, down directions. How can i make it so that the more it zooms, It automatically decreases the move speed properly? I know why this is happening but i cant figure out how to properly do it without just guessing how much i should decrease the move speed.
Heres my script for moving around:
extends Sprite2D
var zoom_speed = 0.1
var move_speed = 0.5
@onready var shader_material : ShaderMaterial = material as ShaderMaterial
func _process(delta):
var zoom = shader_material.get_shader_parameter('zoom')
var view = shader_material.get_shader_parameter('view')
# Zoom control
if Input.is_action_pressed('zoom in'):
shader_material.set_shader_parameter('zoom', zoom + zoom_speed)
if Input.is_action_pressed('zoom out'):
shader_material.set_shader_parameter('zoom', zoom - zoom_speed)
# Movement control
var movement_adjustment = move_speed * delta
if Input.is_action_pressed('move right'):
view.x += movement_adjustment
if Input.is_action_pressed('move left'):
view.x -= movement_adjustment
if Input.is_action_pressed('move down'):
view.y += movement_adjustment
if Input.is_action_pressed('move up'):
view.y -= movement_adjustment
shader_material.set_shader_parameter('view', view)
And my shader if you really need it:
shader_type canvas_item;
uniform int iteration_limit = 500;
uniform vec2 view = vec2(-0.75, 0.0);
uniform float zoom: hint_range(0.0, 15.0) = 0.0;
uniform float ratio = 1.0;
uniform sampler2D palette_image: source_color;
// Returns: vec4(final position, iterations, is inside the set)
vec4 mandelbrot(vec2 c, int iterations) {
vec2 z = vec2(0.0, 0.0);
vec2 z2 = vec2(0.0, 0.0);
int i = 0;
while (z2.x + z2.y <= 4.0 && ++i < iterations) {
z = vec2(z2.x - z2.y + c.x, 2.0 * z.x * z.y + c.y);
z2 = z * z;
}
return vec4(z, float(i), i == iterations ? 1.0 : 0.0);
}
vec4 mandelbrot_smooth(vec2 c, int iterations) {
vec4 m = mandelbrot(c, iterations);
return vec4(
m.xy,
m.z + 1.0 - log(log(m.x * m.x + m.y * m.y) / 2.0 / log(2)) / log(2),
m.w
);
}
vec3 palette(vec4 m) {
int color_count = textureSize(palette_image, 0).x - 1;
if (m.w == 1.0) {
return texelFetch(palette_image, ivec2(0, 0), 0).rgb;
}
vec3 color1 = texelFetch(palette_image, ivec2(1 + int(m.z) % color_count, 0), 0).rgb;
vec3 color2 = texelFetch(palette_image, ivec2(1 + (int(m.z) + 1) % color_count, 0), 0).rgb;
return mix(color1, color2, fract(m.z));
}
void fragment() {
vec2 uv = ((UV - vec2(0.5)) * vec2(ratio, 1.0)) / exp(zoom - 1.25) + view;
vec4 result = mandelbrot_smooth(uv, iteration_limit);
vec3 color = palette(result);
COLOR = vec4(color, 1.0);
}