2D grass shader
Asked Answered
A

2

0

I cant figure out why is this shader not working.(it is not moving it just displace the mesh once)

shader_type canvas_item;

uniform vec2 position = vec2(1.0, 1.0);

uniform float wind_speed = 1.0;
uniform float wind_strength = 2.0;

uniform float wind_texture_tile_size = 20.0;
uniform vec2 wind_direction = vec2(1.0, 1.0);

uniform sampler2D wind_noise : hint_default_black;



void vertex() {
	vec2 world_vert = position + VERTEX;
	
	vec2 noise_x_position = world_vert / wind_texture_tile_size + TIME * wind_speed;
	vec2 noise_y_position = world_vert / wind_texture_tile_size + TIME * wind_speed + vec2(1.0, 1.0);
	
	float noise_x = textureLod(wind_noise, noise_x_position, 0.0).r - 0.5;
	float noise_y = textureLod(wind_noise, noise_y_position, 0.0).r - 0.5;
	
	vec2 bump_wind = vec2(noise_x, noise_y);
	bump_wind *= wind_strength;
	
	float displacement_affect = (1.0 - UV.y);
	
	VERTEX += bump_wind * displacement_affect;
}

I have set up the noise texture, and setting the position as I create the instance. I want to use noise so I can simulate the wind

Avaavadavat answered 29/9, 2023 at 13:8 Comment(0)
G
0

Make sure to use a 2DMeshInstance and a Quad mesh with some subdivisions. Also try encapsulating the TIME + wind_speed or perhaps just TIME in sin() and/or cos().

Also I think you will find it'll be the lower vertices "flapping in the wind" instead of the top ones so it'll look more like a flag or a banner in the wind with this shader. If so then just try changing the 1.0 to 0.0 in the float displacement_affect = (1.0 - UV.y); line and change it to addition.

shader_type canvas_item;

uniform vec2 position = vec2(1.0, 1.0);

uniform float wind_speed = 1.0;
uniform float wind_strength = 2.0;

uniform float wind_texture_tile_size = 20.0;
uniform vec2 wind_direction = vec2(1.0, 1.0);

uniform sampler2D wind_noise : hint_default_black;



void vertex() {
	vec2 world_vert = position + VERTEX;
	
	vec2 noise_x_position = world_vert / wind_texture_tile_size + sin(TIME * wind_speed);
	vec2 noise_y_position = world_vert / wind_texture_tile_size + cos(TIME * wind_speed) + vec2(1.0, 1.0);
	
	float noise_x = textureLod(wind_noise, noise_x_position, 0.0).r - 0.5;
	float noise_y = textureLod(wind_noise, noise_y_position, 0.0).r - 0.5;
	
	vec2 bump_wind = vec2(noise_x, noise_y);
	bump_wind *= wind_strength;
	
	float displacement_affect = (0.0 + UV.y/2.0);
	
	VERTEX += bump_wind * displacement_affect;
}
Gebhardt answered 29/9, 2023 at 14:55 Comment(0)
G
0

Gebhardt Make sure to use a 2DMeshInstance and a Quad mesh with some subdivisions.

I should have added, for initial testing a quad is fine but since you likely will want quantity too and not at too high a cost you'll ultimately want to familiarize yourself with array mesh.

Gebhardt answered 30/9, 2023 at 4:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.