2D Lights

after many attempts i’ve gotten a basic version of 2D lights to work in codea - :smiley:

the demo project is attached

a few areas i’d like to improve on are

  • the x and y positions is kinda weird, I’ll have to recalculate it to fit into whatever resolution the active device is so 0,0 is bottom left and 1,1 is top right
  • the light algorithm, N dot L is rather a bit too simple and doesn’t get the same effect as https://29a.ch/sandbox/2016/normalmap.js/buddah.html
  • is there a better way to use multiple meshes without each of them needing their own duplicate shader? maybe i need to pass the textures and normal maps as arrays
  • try to get a spotlight effect (solid light color, i think this one is easy to do just remove the falloff)
  • test to see how to get a flashlight effect (with the light scattering and lines one would see as they focus and unfocus)

fyi i used this guide - https://github.com/mattdesl/lwjgl-basics/wiki/ShaderLesson6

eventually i hope to get some shadows going too

Edit - updated 12/21
most points above are addressed, flashlight requires using a png

That’s darn cool.

@skar - that’s awesome!

great. i really wish i understood shaders!

@RonJeffries In the old days this is where @Ignatz would chime in with a link to his tutorial book about shaders in Codea.

( Miss ya Iggy ??)

Seeing as he’s pursuing other things now (not a euphemism for death, he’s just actually more interested in other things, so I hear) I’ll do the honors:


thanks! I had forgotten that.

thanks everyone!

and thanks @UberGoober for that link, it’s almost too much info

this project is now available on the web repo with all sorts of improvements

This is fantastic. One thing I noticed is that when I play with the lightZ, if I slide it all the way up the image seems to grow very slightly.

@UberGoober i believe you’re seeing an optical illusion, if you add a cutoff value so that the light only applies to part of the image in a circle, you will see the image doesn’t grow, just the way the light scatters makes it look like it’s moving

Maybe. Try layering the attached images on top of each other in an art program and then toggling between the layers. Some amount of increased contour definition could certainly be contributing to the effect, but there seem to be several places where the change in the apparent borders of the rocks looks more sizable than that. I could be wrong though for sure.

the light scattering depends on the normal map and it is not perfect, here i highlighted the bottom example showing it has the light dropping out sooner than the actual shape of the stone

the rocks normal maps is probably the lowest quality but it does have a nice phong effect like it’s wet stone so i thought it would be ok to keep it