The Theory of Sector Lighting

Discussion in 'Sectoring' started by Agetian, Jul 24, 2007.

Remove all ads!
  1. Agetian

    Agetian Attorney General Administrator

    Joined:
    Aug 14, 2004
    Messages:
    2,526
    Likes Received:
    0
    So guys, I'm starting a new research on how the sector lighting works. I hope I'll be able to shed some light on it, in order to simplify the process of adding new lights to your new maps. ;)

    THE GENERAL THEORY OF LIGHTING
    I'm assuming you're using the Sector Light Editor mode (Ctrl+E) of the ToEE World Builder to create and edit sector lights. If you're not familiar with this editor mode, I highly recommend you to read the "Lighting and Particle FX" tutorial that comes with the ToEE World Builder before reading this thread.

    First of all, it's important to understand that every light object in the game is a sort of emitter that is located at a given coordinate (X,Y), at a given height (Z), and emits light either to all sides (a POINT light), or in a given direction only (SPOT and DIRECTIONAL lights). There's also an AMBIENT type of light, but I'm not sure if it works because it's supposed to emit light across all the map uniformly - an effect which is already achieved by the global lighting in GLOBAL.LIT.

    The most used object types that you'll definitely use the most, too, are the POINT and the SPOT lights. The difference between the two is that the POINT light, while focusing in the direction you've specified (if any) and lighting mostly the cone you've specified (if any), also spreads outside of the cone and outside of the specified direction, however less brightly. The SPOT light, on the contrary, is completely confined to the given direction and cone, so that the areas outside of the cone are completely unlit. It may be a good idea to use SPOT lights to set up your light direction and cone (since their boundaries are instantly easily seen), and then if necessary you can switch them to POINT lights.

    The range of the light emission is defined by the Range parameter of the light objects, the higher this value is, the further the light is being spread. Of course, the lighting decays with distance, so the biggest light level is at the light emitter itself, and the further from the emitter, the less the level of the light is.

    Note that sector visibility blocking (SVB) dampens the effects of lighting, but it doesn't absorb the light completely, so care should be taken when choosing the type of the light and when placing it, so that the lighting doesn't go through walls or other solid obstacles.

    The important concept when creating lights is Direction, which is a vector consisting of three axis (X,Y, and Z) that defines where exactly the light points.

    Now, the important thing to understand in general is that the X direction is used to rotate the light emitter in the up-down fashion, while the Z direction is used to rotate the emitter in the left-right fashion (so, modifying the X direction should make the light target an area either nearer of further from the emitter itself; while modifying the Z direction should make the light target an area to the left, to the right, etc. of the emitter). You don't really have to worry about the Y direction for now unless you're extra good at understanding how things rotate in 3D... Most required effects can be achieved by tweaking only the X and the Z axis anyway.

    Direction may be difficult to understand, so I made some guidelines for you as to what X and Z values can be to achieve some standard effects (lighting an area to the north, to the south, to the west, and to the east from the emitter, that is). Note that all directions can accept negative values as well. Take a look to understand the concept better (note that these values are valid for height=80; if you change your light's height you may need to modify the X axis accordingly):

    [​IMG]
    IMPORTANT: The huge value (1000 and -1000) I've chosen for the Z direction is arbitrary. Actually, it's much bigger than the maximum value necessary to achieve the given effect. Therefore, to fine-tune it, try working from a smaller value instead (for instance, Z=180 and Z=-180) to see how the changes affect your light.

    NOTE: The same values as in the chart are proven to work with half-height as well (height=40). Also, note that it's best to avoid placing lights at height=0, I don't know how to configure them properly yet.

    Now, modifying the "Angle" property will make your light cover either a smaller or a larger area. The spotlight emitters emit light in a cone shape (think of a "Cone of Cold" spell, for instance ;)) The bigger the "Angle" is, the larger the cone. I think it's pretty self-explanatory. The value for the cone angle is usually between 0 and 180. I advise you to use "60" or "90" for your testing, and then tweak to a smaller or a bigger value as necessary.

    I'll be using my favorite first room in the Tutorial Map 1 (map 5116 - "Tutorial Map 1", coords (509,458) - in the first room of the tutorial map) to demonstrate the different lights.

    POINT LIGHTS
    Point lights are the most common type of lights. Point lights generally spread light in all directions. If no direction is specified, the light spread is pretty much equal in all directions. If you specify the direction and the cone, the light will be focused within the given cone and the given direction, but the areas outside the cone will also be lit (though less brightly). To create a point light at the (509,458), do the following:

    1. Open the light editor and open the sector with sector coords (SX=7,SY=7), which includes the coordinate (509,458). If necessary, create the given sector file in the sector editor mode first (I'm assuming you know how to do this; if not, you should probably read the basic ToEEWB tutorials first).

    2. (OPTIONAL) it may be the best for you now to click "Delete a light" until no other lights are present in the sector. This will ensure that you instantly see the effect of your light. You may also want to delete all lights in the neighboring sectors (SX=7,SY=6) and (SX=7,SY=8) too, to avoid interference with other area lighting. This is only for the demonstrational purposes - you don't have to take this step if you don't want to.

    3. Add a new light.

    4. Set the Light Type to "Point", put a check mark in the "Uses Primary Light" and "Primary Light has Particle System" attributes. We're only using the primary light for demonstration. If necessary, you can set up the secondary (night) light yourself, following the lighting tutorial that comes with the world builder.

    5. Set the color you like. For instance, R=255,G=255,B=0 for a yellow lighting. Make it some non-white color so that you can see the effect.

    6. Choose Location X=509, Location Y=458

    7. Set the height you want. Height=0 is ground level, height=30 is approximately waist level, height=80 is approximately head level.

    8. Set the Range to the distance you want. It's hard to describe what exactly the range corresponds to, I advise you to experiment with the values 100, 200, and 500 to see the difference for yourself.

    9. If you'd like to see the torch flame at the height (in order to see the effect of the "Height" parameter), click on the "Create" button next to the Particle System Hash ID, and type "Torch" in the dialog window.

    10. If you'd like to specify no direction/cone for your light, leave all other values at "0" for now and go to step 13. Otherwise, proceed to step 11.

    11. We need to set the angle of the cone for your light. Remember: the bigger the value, the bigger the angle of the cone is (obviously). Let's choose a 60-degree cone for demonstration purposes, so type 60 into the "Angle" box.

    12. Now, we need to set up the direction. For starters, consult the chart above to choose the basic direction, then work from there to tweak. For demonstration purposes, we'll light up the arch where we need to go, so we'll choose the first basic direction from the chart above. Type the following values for your direction:
    Direction X: -360
    Direction Y: 0
    Direction Z: 0

    13. Remember to click "Update A Light" and then "Save a sector" to save your changes. If you don't hit the "Update A Light" button first, your changes will be lost.

    14. Test your changes in the game.

    IMPORTANT: The "Direction" and "Angle" also have effect on the Point lights, similar to Spot lights. However, the effect is a little bit different - for spot lights, the light is only emitted within the cone, while for point lights it's also emitted outside of the cone, but less brightly. Consult the chart above to see how to set up the direction and the angle, and do some experiments yourself to figure out how the directed point light differs from a directed spot light.

    SPOT LIGHTS
    Spot lights are different from the point lights in that they only emit light to the given direction and within the given cone. Areas outside of the cone are completely unlit.

    Let's create a test spot light so that you can experiment a bit:

    1. Follow steps 1,2, and 3 for the Point lights. These steps are the same. Step 2 is, once again, optional. However, if you created a point light at (509,458) earlier on, you'll need to delete it (obviously) so that you can see your new spot light.

    2. Set the Light Type to "Spot", put a check mark in the "Uses Primary Light" and "Primary Light has Particle System" attributes. We're only using the primary light for demonstration. If necessary, you can set up the secondary (night) light yourself, following the lighting tutorial that comes with the world builder.

    3. Follow steps 5,6,7,8, and 9 for the Point lights. They are the same for spot lights, too. It's recommended to choose a large value for the Range for now (so that you see the effect immediately), so choose 500 for the range.

    4. Now, we need to set the angle of the cone for your light. Remember: the bigger the value, the bigger the angle of the cone is (obviously). Let's choose a 60-degree cone for demonstration purposes, so type 60 into the "Angle" box.

    5. Now, we need to set up the direction. For starters, consult the chart above to choose the basic direction, then work from there to tweak. For demonstration purposes, we'll light up the arch where we need to go, so we'll choose the first basic direction from the chart above. Type the following values for your direction:
    Direction X: -360
    Direction Y: 0
    Direction Z: 0

    6. Remember to click "Update A Light" and then "Save a sector" to save your changes. If you don't hit the "Update A Light" button first, your changes will be lost.

    7. Test your new spot light in the game! Try walking towards the arch and see how your character gets gradually lit with your 60-deg spotlight. ;)

    DIRECTIONAL LIGHTS
    Information on how to create this type of light is coming soon (I hope ;)).

    ---
    More to come soon.

    - Agetian
     
    Last edited: Jul 25, 2007
  2. Shiningted

    Shiningted I changed this damn title, finally! Administrator

    Joined:
    Oct 23, 2004
    Messages:
    12,740
    Likes Received:
    374
    Looking forward to more Ag :thumbsup: this is one area that we can genuinely go beyond ToEE.
     
  3. krunch

    krunch moving on in life

    Joined:
    Aug 9, 2005
    Messages:
    3,280
    Likes Received:
    0
    This is great, absolutely wonderful. The information from post # 1 in this thread will see light in my mod upgrade. And, I am looking forward to each new breakthrough and new information being posted.
     
  4. Agetian

    Agetian Attorney General Administrator

    Joined:
    Aug 14, 2004
    Messages:
    2,526
    Likes Received:
    0
    OK, a little info update in the first post. Not much, but still better than nothing. ;)
    Also, I updated ToEEWB to version 2.0.4a (go download a patch), that fixes a certain big bug in the sector light editor. This update is an absolute must if you plan to work with sector lights right now or in the future.

    - Agetian
     
  5. Agetian

    Agetian Attorney General Administrator

    Joined:
    Aug 14, 2004
    Messages:
    2,526
    Likes Received:
    0
    IMPORTANT: I completely rewrote the first post to take all the recently uncovered information into consideration. If you've read anything before, it's a good idea to re-read the whole thing now, for there were some mistakes in the previous info.

    - Agetian
     
  6. DarkStorm

    DarkStorm Established Member

    Joined:
    Oct 2, 2003
    Messages:
    514
    Likes Received:
    3
    Hi,

    I have a small addition to this:

    If my reverse engineering ist correct, the light-type in Agetians world-builder is incorrect.

    I deduced from my reverse engineering, that ToEE directly uses the type value from the sector lighting as the direct3d light type. Those are defined as follows:
    D3DLIGHT_POINT = 1,
    D3DLIGHT_SPOT = 2,
    D3DLIGHT_DIRECTIONAL = 3,
    There are additional (deprecated) values that are no longer in use.

    In Agetians World-Builder, the light-type list is ordered as follows:
    None
    Point
    Directional
    Spot

    So Directional and Spot are switched. I think if you switch those, the types are correct. Other than that, I deduced the following information:

    The "color" in the lighting information is both the Diffuse and Specular color of the light source.

    Position is used to position the light in the world. For directional lights, this is normally ignored, but I think the engine uses this information to disable/enable light sources based on proximity (You can only have about 10 active light sources at a time when rendering with current hardware. So when you render a model, you only enable those lights that can influence the model. I think they do that based on the distance from the light and the range).

    Location and Direction should be okay. Although I am switching Y/Z in the direction vector for my engine since I am using a different (more intuitive) coordinate system.

    The order of color is actually red, blue, green. This seems to be correctly implemented in Agetians editor (by misnaming the textfields :p).

    How range and angle are interpreted:
    (You need some direct3d light background to understand this. Google direct3d lighting if you wish)

    The range value is simply used as-is as the range value of the light. But it's also used to deduce the attenuation as follows:
    Attenuation0 = 0
    Attenuation1 = 0
    Attenuation2 = 4 / (Range * Range) // This is quadratic attenuation
    This does not apply to directional lights in the d3d light model, but it is set anyway...

    The given Angle is the phi angle in the lighting model. It's converted directly to radians by multiplying with Pi/180. The Theta angle is deduced by multiplying Phi with 0.6, which means the inner angle of the cone is always 60% of the outer angle.

    That's all for now.

    Cu,
    Storm
     
  7. Shiningted

    Shiningted I changed this damn title, finally! Administrator

    Joined:
    Oct 23, 2004
    Messages:
    12,740
    Likes Received:
    374
    Darkstorm, do you see any possibility that we could have moving lights in the game? Ie, light-sources attached to items or critters? Some of the mob flags suggest this is possible but I have had no luck getting it going (nor has Ax Thrower).
     
  8. DarkStorm

    DarkStorm Established Member

    Joined:
    Oct 2, 2003
    Messages:
    514
    Likes Received:
    3
    Hi. I don't know about the original engine. It's in theory possible that this functionality is hidden in there somewhere, but I don't think it's easy to find out...

    But really... That's one of the reasons I am doing my own engine. Stuff like that could be added there much easier.

    Cu,
    Storm
     
  9. GuardianAngel82

    GuardianAngel82 Senior Member

    Joined:
    Oct 3, 2007
    Messages:
    3,481
    Likes Received:
    5
    Equip a magic sword and have blue light spill out. Or yellow light if it's a flaming sword. That would rock! :thumbsup:
     
  10. marc1967

    marc1967 Established Member

    Joined:
    Jan 19, 2014
    Messages:
    672
    Likes Received:
    110
    I'd like clear up the somewhat confusing way that the Day and Night lighting is controlled by the flags in the Sector Light Editor within World Builder. This has not really been detailed in this thread yet. The 3 flags in question (Uses Primary Light, Uses Secondary Light, and Primary Light has Particle System) are worded a bit ambiguously and misleadingly from what they actually do. All of these comments are based on examining existing sector light data, and also creating lights for maps I am laying out.


    QUICK REVIEW OF THE 5 FLAGS:

    First, leave the 2 flags called Animated and View Controls unchecked. I've never seen the Animated flag set anywhere, and the View Controls flag is used with the "newly discovered" WorldEd (not to be confused with World Builder).

    OK, so the 3 remaining flags do all the work:

    1) Uses Primary Light: Checking this flag means the light you enter under "Primary Light (Day)" will be used for both Day AND Night". Yep, even though the word "Day" appears in the description, the light will go at night too. But don't check this flag alone (see setting 1 below for the proper setup).

    2) Uses Secondary Light: Checking this flag will use the light you enter under "Primary Light (Day)" for Day, but then at Night switches to the "Secondary Light (Night)".

    3) Primary Light has a Particle System: Checks either light for an accompanying particle ID. Don't check this flag alone (see below).


    WORKING FLAG SETTINGS:

    Here are the 4 flag setting combinations that will do everything you need, and I have found safe and reliable:

    1) To get the same light for Day and Night, with No particles:

    - Uncheck all flags.
    - Enter the light under "Primary Light (Day)".

    Oddly, leaving all 5 flags unchecked is a viable and safe option, and is used profusely in the existing sector lighting. Do not check "Uses Primary Light" alone to get this, even though it might seem like the proper thing to do.

    Lighting 000.jpg


    2) To get a different light for Day and Night, with No particles:

    - Check the "Uses Secondary Light" flag.
    - Enter the light for Day under "Primary Light (Day)".
    - Enter the light for Night under "Secondary Light (Night)".

    Lighting 010.jpg


    3) To get the same light and particle for Day and Night:

    - Check the "Uses Primary Light" flag. (unlike case 1, you actually check the "Uses Primary Light" flag here)
    - Check the "Primary Light has a Particle System" flag.
    - Enter the light and particle under "Primary Light (Day)".

    Lighting 101.jpg


    4) To get different light and particle for Day and Night.

    - Check the "Uses Secondary Light" flag.
    - Check the "Primary Light has a Particle System" flag.
    - Enter the light and particle for Day under "Primary Light (Day)".
    - Enter the light and particle for Night under "Secondary Light (Night)".​

    Lighting 011.jpg


    FLAG COMBINATIONS TO AVOID:

    I have found that setting any of these flag combinations will cause the game to act buggy - blue circles with missing characters, sluggishness, etc. These never appear in the original game's sector data files, except for the first one listed very rarely. And I wonder if that may be the cause of some of the random sluggishness and lag that pops up here and there.

    - Don't set "Uses Primary Light" alone
    - Don't set "Primary Light has a Particle System" alone
    - Don't set "Uses Primary Light" and "Uses Secondary Light" together.
    - Don't set all 3 flags together. Oh the humanity! I'm still reformatting my hard drive.


    MISC. OBSERVATIONS OF OBVIOUSNESS:

    1) The sector lights only affect mobile objects and static objects on the map; they don't actually lighten and darken the artwork. Most people know this, but it took me a while to catch on. :roll:

    2) If the Light Type is set to "None", the accompanying particle effect will still be activated.

    3) If the Particle System Hash ID is set to "0" then no particle will appear, but the light will still work.


    EDIT: I forgot to mention something important I noticed, unrelated to the Flags. The setting for Range (floating point) under Secondary Light (Night) does not work. It uses the Range that is set under Primary Light (Day).
     
    Last edited: Jul 7, 2015
Our Host!