Skip to content

Instantly share code, notes, and snippets.

@itspacchu
Created September 4, 2022 15:27
Show Gist options
  • Save itspacchu/88c9d557295c9225364d8a1f1eed088c to your computer and use it in GitHub Desktop.
Save itspacchu/88c9d557295c9225364d8a1f1eed088c to your computer and use it in GitHub Desktop.
Smear effect in godot
extends MeshInstance
var mat;
var prev_position;
var zpos = 10;
func _ready() -> void:
mat = self.get_active_material(0)
prev_position = translation
print(mat)
func _process(delta: float) -> void:
self.translation = get_parent().get_node("Camera").project_position(get_viewport().get_mouse_position(),zpos);
mat.set_shader_param("delta", delta);
var new_pos = self.translation;
mat.set_shader_param("previous_position", prev_position)
mat.set_shader_param("new_position", new_pos);
prev_position = new_pos;
func _input(event : InputEvent) -> void:
if event is InputEventMouseButton:
event as InputEventMouseButton
if event.pressed:
match event.button_index:
BUTTON_WHEEL_UP:
zpos += 1
BUTTON_WHEEL_DOWN:
zpos -= 1
shader_type spatial;
uniform vec3 previous_position;
uniform vec3 new_position;
uniform float intensity=1.0;
uniform float freq = 1.0;
uniform float delta;
/** Godot Docs **/
float hash(vec2 p) {
return fract(sin(dot(p * 17.17, vec2(14.91, 67.31))) * 4791.9511);
}
float noise(vec2 x) {
vec2 p = floor(x);
vec2 f = fract(x);
f = f * f * (3.0 - 2.0 * f);
vec2 a = vec2(1.0, 0.0);
return mix(mix(hash(p + a.yy), hash(p + a.xy), f.x),
mix(hash(p + a.yx), hash(p + a.xx), f.x), f.y);
}
float fbm(vec2 x) {
float height = 0.0;
float amplitude = 0.5;
float frequency = 3.0;
for (int i = 0; i < 6; i++){
height += noise(x * frequency * freq) * amplitude;
amplitude *= 0.5;
frequency *= 2.0;
}
return height;
}
/***/
void vertex(){
float height = fbm(VERTEX.xz * 4.0 + TIME);
vec3 move_dir = previous_position - new_position;
float mag = clamp(dot(VERTEX,move_dir),0,1);
VERTEX += intensity * height * move_dir * mag;
//COLOR.xyz = vec3(dot(VERTEX,move_dir));
}
void fragment(){
ALBEDO = vec3(0.3,0.3,1.0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment