Casting light with newMesh
Posted: Mon Dec 23, 2013 1:53 pm
(See this post onward instead)
I'm currently trying to code a lightning system and I first tried using kikito's bresenham lib to detect which coordinate can be light and then, well, I'd light them. Here's the ouput:
(Warning: You might find dead pieces of code in there) It looks nice but the thing is that map is only 19x19 and since the algorithem checks every possible pixel it runs 361 times in one frame, if I use a bigger map, 1920x1080, it'd run 2073600 (minus not visible pixels) times and that'd consume lots of resources possibly decreasing the FPS so I decided to explore the net for a better lightning system.
I found this which explains quite a few ways of how to code light, I think that wall tracking'd be the best option and since it uses triangles to draw the light I thought about the newMesh function which creates polygons out of triangles, and since the light polygons are made out of trinagles centered on one point I could use the "fan" mode, (first question) what do you guys think, will this work ?
(Next questions)
Do any of you know a good way to do that with a map built with walls made out of rectangles like:
Or maybe better way to code light ?
I'm currently trying to code a lightning system and I first tried using kikito's bresenham lib to detect which coordinate can be light and then, well, I'd light them. Here's the ouput:
(Warning: You might find dead pieces of code in there) It looks nice but the thing is that map is only 19x19 and since the algorithem checks every possible pixel it runs 361 times in one frame, if I use a bigger map, 1920x1080, it'd run 2073600 (minus not visible pixels) times and that'd consume lots of resources possibly decreasing the FPS so I decided to explore the net for a better lightning system.
I found this which explains quite a few ways of how to code light, I think that wall tracking'd be the best option and since it uses triangles to draw the light I thought about the newMesh function which creates polygons out of triangles, and since the light polygons are made out of trinagles centered on one point I could use the "fan" mode, (first question) what do you guys think, will this work ?
(Next questions)
and(...) we want to find the nearest wall (...). Our strategy will be to sweep around 360° and process all of the wall endpoints.
With that I don't know how to "sweep around 360º" and how to calculate neither the nearest wall and the endpoints of walls.How do you figure out which wall is nearest? The simplest thing is to calculate the distance from the center to the wall. However, this approach doesn’t work well if the walls are of different sizes, so the demo uses a slightly more complicated approach, which I won’t explain here.
Do any of you know a good way to do that with a map built with walls made out of rectangles like:
Code: Select all
wall.x=(wall's X coord)
wall.y=(wall's Y coord)
wall.width=(wall's width)
wall.height=(wall's height)