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.
This is the desired effect.
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.
A little help with vertex shaders
Forum rules
Before you make a thread asking for help, read this.
Before you make a thread asking for help, read this.
-
- Prole
- Posts: 2
- Joined: Tue Sep 03, 2013 8:22 pm
A little help with vertex shaders
- Attachments
-
- vertices.love
- Working as intended.
- (8.26 KiB) Downloaded 193 times
Last edited by LocoShaman on Fri Jan 03, 2014 8:51 am, edited 1 time in total.
-
- Party member
- Posts: 712
- Joined: Fri Jun 22, 2012 4:54 pm
- Contact:
Re: A little help with vertex shaders
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:
And in the shader:
As mentioned, this doesn't work yet (it's off by some factor, I think). But maybe it helps...
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})
-- ...
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;
}
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
Bandana (Dev blog) - Platformer featuring an awesome little ninja by Micha and me
GridCars - Our jam entry for LD31
Germanunkol.de
-
- Prole
- Posts: 2
- Joined: Tue Sep 03, 2013 8:22 pm
Re: A little help with vertex shaders
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
Could you please update your demo with your new & improved shader?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.
Re: A little help with vertex shaders
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.
I added some improvement to your love2d code and shader to add shadow also to polygons.
- Attachments
-
- testPolyShadows.love
- Polygon shadows example
- (9.63 KiB) Downloaded 119 times
Who is online
Users browsing this forum: Semrush [Bot] and 7 guests