08-02-2009, 09:24 AM
|
#1
|
|
|
Lighting Example in Top View Game with SWF
Hey guys,
I have been knocking about some different light examples as a part of a larger project, the link below shows one version. This is just an experiment on light, not on collision detection for the little guy or anything like that.
http://www.users.on.net/~cjke/swf_ex...lightTest.html
This version projects a ray of light in code until it hits an object. Once this has been established, it moves on the next ray of light. By the end of all this we have a rough diamond shape that acts as a mask to the world.
Notice also there is a "lamp" as such in the upper right room projecting light also, just a test for overlapping light sources.
How do you guys normally tackle lighting in flash and as3?
|
|
|
08-02-2009, 05:59 PM
|
#5
|
|
|
Quote:
Originally Posted by cjke.7777
Ideally I also want to build it without a mask - and with this the light source effecting bitmapData instead.
|
you like to do things realistically  me too, i always code my physics the way they work in the real universe. if something as big as the universe can do it without lag, it must be efficient 
|
|
|
08-02-2009, 07:29 PM
|
#6
|
|
|
Quote:
Originally Posted by flyingmonkey456
you like to do things realistically  me too, i always code my physics the way they work in the real universe. if something as big as the universe can do it without lag, it must be efficient 
|
Yep eventually I will program ever aspect of physics using just flash, I mean with the power of AS3 it should be possible right??
Na but seriously I don't want to go over board with the lighting, as its not the focus of the larger project - for example I am ignoring all light refraction, etc.
I don't think the bitmapData thing will be that difficult to achieve, just alter the pixels that overlap that lighting diamond by ## amount.
|
|
|
08-05-2009, 11:46 AM
|
#10
|
|
|
Wow this questions comes up way too much. Front face culling is probably the cheapest way. I made an example years ago for people on kirupa. It showed off LOS shadows but the idea is similar.
Since then I've lost the source code, but I've implemented this in XNA. It's pretty trivial. Bound all objects with concave polygons such that you have an array of vertices. For all static polygons store the normal of the edge.
Iterate all polygons and for each polygon iterate the edges. Use the dot product to figure out of the edge is facing you (vertOnEdge - player) dot normal < 0. If it is then skip it. If it's > 0 then fire a ray from the player to the 2 vertices. Check these rays with the camera's rectangle. That is perform a ray to line-segment intersection test with each of the 4 edges of the camera. You'll end up with 2 vertices. The last step is to see if the corners of the camera need to be included. A possible of 2 corners can be included. This can be done with 2 dot products since you're checking to see if a corner is between the two rays. So find the LHS and RHS normals of each ray respectively and perform dot products with (corner - player). If both are > 0 then the corner is included. If you handled everything correctly you'll have a 2 edge vertices + corner vertices (if any were included) in an array. These form a convex polygon that can be rendered. Do this for each edge and you'll end up with shadow volumes very similar to the 3D implementation of the algorithm. There are many optimizations to this algorithm. I'll leave those for you to find since they're cool to figure out.
Here's some C++ intersection equations which come in handy:
http://sirisian.pastebin.com/f2d380dd3
There are other way to render lights. You can render true light volumes which is the inverse of the LOS shadow algorithm I explained. It's extremely algorithm heavy. I started implementing it a long time ago but had other things to do.
This might help you to visualize things though. (Like I said it was never finished).It ends up being a very cost effective algorithm. I was researching the use of a dynamic occlusion algorithm with it, but a basic naive version of the algorithm shouldn't be too hard.
Last edited by Sirisian; 08-05-2009 at 11:52 AM..
Reason: fixed grammar
|
|
|
08-05-2009, 01:11 PM
|
#11
|
|
|
oh, i remember that one (the occlusion algorithm)
you showed it in a similar thread about lights shadows and 3D in 2D etc etc..
i know it's not the right way but... with mask layers and the LOS shadows is it possible to make the same result of the occlusion?
cause despite beeing the oposite, they look very similar to me.
(i know that sounded weird and generic XD)
__________________

"A human can interpret missing logic but a computer can't." - wkt 
"That goes into the 'too bad' box." - TOdorus 
"This is a three year old thread, there's no reason to post here..." - flyingmonkey456 
"Despite beeing the oposite, they look very similar to me." - SparK
|
|
|
08-05-2009, 02:22 PM
|
#12
|
|
|
What's so bad about the mask way? It works really well and it's easier to program and less error prone than running the other algorithm. And yes it's possible to get the light effect using the LOS algorithm. Just render the LOS stuff to a bitmapdata using 0 alpha. Then just render your light texture using copy pixels with the LOS bitmapdata as the alpha part.
For more than one light you have to do something that's kind of annoying. Render all of the shadows from the light's perspective to a bitmapdata and then render the light texture (circle gradient or something) using that bitmapdata LOS for the alpha. So basically you're doing multiple passes basically for each light.
off topic:
-------------------------
That occlusion test wasn't even me trying to render lights so much as implementing occlusion and some other ideas. Light volumes have the single advantage that if you need to render more than one light you don't need to render the whole scene again so it's much faster. I had multiple spot lights at one point. Basically tons of lights can be rendered. 
Last edited by Sirisian; 08-05-2009 at 02:28 PM..
|
|
|
|
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -4. The time now is 05:44 PM.
|
|