A little help with vertex shaders

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
LocoShaman
Prole
Posts: 2
Joined: Tue Sep 03, 2013 8:22 pm

A little help with vertex shaders

Post 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.
Attachments
vertices.love
Working as intended.
(8.26 KiB) Downloaded 189 times
Last edited by LocoShaman on Fri Jan 03, 2014 8:51 am, edited 1 time in total.
Germanunkol
Party member
Posts: 712
Joined: Fri Jun 22, 2012 4:54 pm
Contact:

Re: A little help with vertex shaders

Post 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...
trAInsported - Write AI to control your trains
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
LocoShaman
Prole
Posts: 2
Joined: Tue Sep 03, 2013 8:22 pm

Re: A little help with vertex shaders

Post 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.
User avatar
Ref
Party member
Posts: 702
Joined: Wed May 02, 2012 11:05 pm

Re: A little help with vertex shaders

Post 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?
dnk72
Prole
Posts: 2
Joined: Mon Sep 28, 2015 7:23 am

Re: A little help with vertex shaders

Post 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
Attachments
testPolyShadows.love
Polygon shadows example
(9.63 KiB) Downloaded 117 times
Post Reply

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 208 guests