How to get mouse position in shader?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By hunar 1
:warning: Old Version Published before Godot 3 was released.

Hi
I Don’t know much about shaders but i was messing around and i made this

color col = COLOR;
float speed = 6;
float time = sin(TIME*speed)+1;
vec3 VERTEX;
float avg = (col.r+col.g+col.b)/3;

col.rgb = vec3( col.r+(avg-col.r)*time/2, col.g+(avg-col.g)*time/2, col.b+(avg-col.b)*time/2 );

COLOR = col;

It makes the image change between B&W and Color, ok that is cool but i want to get mouse position and make the parts to the left of the mouse B&W and to the right of the mouse Color

how to get the mouse position to the shader. also another question… how to get the pixel position from Vertex

thanks :smiley:

:bust_in_silhouette: Reply From: eons

Add an uniform like: uniform vec2 mouse_position;

Then get the material and set_shader_param("mouse_position",vector2_with_current_mouse_position) in a processing script.

Great thanks :D, but one more thing … how can i know where in the screen is the current pixel located? :confused: , I need that too

hunar 1 | 2017-08-10 07:45

I found it … now my gdscript is:

extends Sprite

func _ready():
	set_process(true)
	pass
func _process(delta):
	var mouse = get_global_mouse_pos();
	var screen = get_viewport().get_rect().size
	mouse.x /= screen.x
	mouse.y /= screen.y
	get_material().set_shader_param("mouse_position", mouse)

and my fragment shader is:

uniform vec2 mouse_position;

color col = COLOR;

float avg = (col.r+col.g+col.b)/3;

if (UV.x-mouse_position.x > 0){
	col.rgb = vec3(avg, avg, avg);
}

COLOR = col;

shader is very cooool :smiley:

hunar 1 | 2017-08-10 08:05