water waves 2d

need 

mousePos = event.position-global_position 

and

material.set_shader_parameter("scale",global_scale)

Need to send outside time in “func _process(delta):“. 

Need an Array which has the right size, and send it to “mPosTime[100]” in shader. Like:

var time = fmod(Time.get_ticks_msec()/1000.,3600.)
pointArray.pop_front()
pointArray.append(Vector3(mousePos.x,mousePos.y,time))
material.set_shader_parameter("mPosTime",pointArray)
Shader code
shader_type canvas_item;
uniform vec2 scale;
uniform vec2 mpos = vec2(-1.,-1.);
uniform float waveSize = 20.;
uniform float kTime = 0.0;
uniform float nowTime = 0.0;
uniform float speedOff = 3.0;
uniform vec3 mPosTime[100] ;

vec2 uvOff(vec2 pPos,vec2 mouse_pos,float kickTime){
	float linear = length(pPos-mouse_pos)/waveSize -(nowTime-kickTime)*(speedOff/1.);

	float disOff = (clamp(exp(-length(pPos-mouse_pos)/200.),0.05,1.0)-0.05)/0.95;
	float midOff = clamp(linear-0.5,-0.5,0.5);
	float mask = (1.0-abs(midOff)*2.);
//	float a = (1.0-abs(midOff)*2.)*sign(midOff);
	vec2 uv2 = disOff*mask*sin(-PI*midOff/2.0)*normalize(pPos-mouse_pos)*0.2;
	return uv2;
}

void fragment() {
	vec2 uv = UV;
//	vec2 mouse_uv = mouse_pos ;
	
	//拉伸变形校正
	float scale_min = scale.x/scale.y;
	vec2 uv_max = vec2(scale_min,1.);
	float trueScale;
	if(scale.y<scale.x){
		scale_min = scale.y/scale.x;
		uv.y = uv.y * scale_min;
		uv_max = vec2(1.,scale_min);
//		mouse_uv = mouse_pos * TEXTURE_PIXEL_SIZE /scale.x;
		trueScale = scale.x;
		}
	else{
		uv.x = uv.x*scale_min;
//		mouse_uv = mouse_pos * TEXTURE_PIXEL_SIZE /scale.y;
		trueScale = scale.y;
	}
	
	COLOR= texture(TEXTURE,uv);

	
	//鼠标响应
	vec2 pPos = uv / TEXTURE_PIXEL_SIZE * trueScale;
	vec2 left_bottom = vec2(0.,uv_max.y/TEXTURE_PIXEL_SIZE.y * trueScale);
	
	vec2 uv2 = vec2(0.);
	for (int i = 0; i < mPosTime.length(); i++) {
		if((mPosTime[i].x >0.)&&(mPosTime[i].y>0.))
		{
//		if (i < int(ceil(float(mPosTime.length())*0.5)))//早先的一些
//		{
//			float dis = float(i)/ceil(float(mPosTime.length())*0.5);
//			uv2 += uvOff(pPos,vec2(mPosTime[i].x,mPosTime[i].y),mPosTime[i].z) * dis;
//		}
//		else
			float dis = float(i)/ceil(float(mPosTime.length()));
			uv2 += uvOff(pPos,vec2(mPosTime[i].x,mPosTime[i].y),mPosTime[i].z) * dis;
		}
	}
	
	COLOR = texture(TEXTURE,uv + uv2);
	// Place fragment code here.

//	FRAGCOORD.xy;
}
Tags
2d, water, waves
The shader code and all code snippets in this post are under MIT license and can be used freely. Images and videos, and assets depicted in those, do not fall under this license. For more info, see our License terms.

More from 50tu

page flip turing

Related shaders

PSX Style Water Surface – Pixelation, Waves, Scrolling Textures

Neuronal Network Waves

Pink waves

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Elmapul
Elmapul
8 months ago

i dont get how to use it.
can you provide an link with this working?