Page 1 of 1

A little help with vertex shaders

Posted: Thu Jan 02, 2014 2:12 am
by LocoShaman
I'm pretty inexperienced with shaders, and especially vertex shaders, but I've been fooling around with them trying to distort geometry (meshes now) based on the position of a light or camera. The meshes aren't distorting the way I expected them to, I've used the same formulas that are in the shader to create the effect I wanted using just love's newMesh function but it was a bit slow creating the new geometry every frame so I was hoping a vertex shader would be a more efficient approach.

I've looked through the forums a bit but haven't found to much information regarding vertex shaders, so I was hoping someone could point out what is causing the shader to perform differently than I expect. Additionally if there's a better way to do what I'm trying to do, I would also appreciate the insight.


This is what happens.
Image

This is the desired effect.
Image

Thank you in advance for any help you can provide, I hope I was clear in explaining my problem.

EDIT: Uploaded the working version of the shader, for those interested. I would still like to figure out a way to make it work with out using an if statment, but I'm pretty happy with it.

Re: A little help with vertex shaders

Posted: Thu Jan 02, 2014 12:39 pm
by Germanunkol
I haven't been able to fix it yet, but I think you need to send along the positions of the fixed vertices as well.

What I think would work is:
for every free vertex, mirror the light position over the corresponding (neighbouring) fixed vertex and then scale or normalize it somehow.

This would of course also draw over the object which is casting the "shadow", but that can be compensated by drawing the object after drawing its shadows.

My approach was:

Code: Select all

	-- ...
	if self == tiles[1] then --TOP
		
		effect:send('free_edge', {0, 0, tile_size, 0})
		effect:send('fixed_edge', {0, tile_size, tile_size, tile_size})
	-- ...
And in the shader:

Code: Select all

	if (v.x == free_edge[0] && v.y == free_edge[1] )
	{
		v.xy = vec2(fixed_edge[0], fixed_edge[1]) - camPos;
	}
	else if (v.x == free_edge[2] && v.y == free_edge[3])
	{
		v.xy = vec2(fixed_edge[2], fixed_edge[3]) - camPos;
	}
As mentioned, this doesn't work yet (it's off by some factor, I think). But maybe it helps...

Re: A little help with vertex shaders

Posted: Thu Jan 02, 2014 10:40 pm
by LocoShaman
Thanks for taking the time to look at it Germanunkol, your suggestion was actually quite helpful. Also like you said normalizing was normalization was key. Dividing the light position by the new length of the vector gave a result that seems to be what I was going for. Now I just need to see if doing all this will actually be useful. Thanks again.

Re: A little help with vertex shaders

Posted: Fri Jan 03, 2014 3:07 am
by Ref
LocoShaman wrote:Thanks for taking the time to look at it Germanunkol, your suggestion was actually quite helpful. Also like you said normalizing was normalization was key. Dividing the light position by the new length of the vector gave a result that seems to be what I was going for. Now I just need to see if doing all this will actually be useful. Thanks again.
Could you please update your demo with your new & improved shader?

Re: A little help with vertex shaders

Posted: Mon Sep 28, 2015 7:41 am
by dnk72
I was newbe in vertex shading. I found your work very usefull.
I added some improvement to your love2d code and shader to add shadow also to polygons.
Image