2D pixelated grass waving shader

I have been trying to create a simple 2D swaying shader and I think I may have done it. Lol.
Git it a try. It might do the job…

– Vertex program

attribute mediump vec4 position;
attribute mediump vec2 texcoord0;

uniform mediump mat4 mtx_view;
uniform mediump mat4 mtx_proj;

uniform mediump vec4 time;
uniform mediump vec4 speed;
uniform mediump vec4 intensity;

varying mediump vec2 var_texcoord0;

void main() {
    var_texcoord0 = texcoord0;
    vec4 p = position;

    p.x += 10.0 * (sin(speed.x * time.x) + cos(speed.x * time.x)) * intensity.x * max(0.0, texcoord0.y);
    gl_Position = mtx_proj * mtx_view * p;
}    

– Script

go.property("speed", 1.0)
go.property("intensity", 1.6)

function init(self)
	-- Time variable
	self.time = 0
	-- Start the randomizer
	math.randomseed(os.time())

	-- Values to pass along to the VP shader
	self.intensity = (self.intensity == 0 and 1.0 or self.intensity)
	self.speed = (self.speed == 0 and 1.0 or self.speed)
	--
	sprite.set_constant("#sprite", "intensity", vmath.vector4(self.intensity))
	sprite.set_constant("#sprite", "speed", vmath.vector4(self.speed, 0, 0, 0))
end


function update(self, dt)

	self.time = self.time +  dt 
	print ("Time: ", self.time)

	-- Send time to the VP shader
	sprite.set_constant("#sprite", "time", vmath.vector4(self.time, 0, 0, 0))
end
3 Likes