Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Unity 2022.2 is now available as the latest Tech release.
    Dismiss Notice
  3. We are making some changes to the DOTS forums.
    Dismiss Notice
  4. Have a look at our Games Focus blog post series which will show what Unity is doing for all game developers – now, next year, and in the future.
    Dismiss Notice

[RELEASED] SpriteLights

Discussion in 'Assets and Asset Store' started by Elecman, Oct 10, 2015.

  1. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    Introduction

    Rendering light sources is typically done using individual sprites but this can become computationally expensive pretty quickly if you use thousands of lights. A better approach is to use the triangles in a large mesh as lights, and make these triangles face the camera in a shader. This way you can render 1.4 billion (!) lights when using a 32 bit mesh in Unity 2017.3+, in only one draw call.

    Video:



    Possible use cases

    -City lights.
    -Airport taxiway lights.
    -Airport runway and approach lights.
    -3D star fields.
    -Spherical star field.
    -Point cloud.


    How it works

    Because the lights are a regular mesh object, it can be both generated at runtime, or generated offline and stored as a prefab. The CityLights.cs file contains an example how to save a prefab. You can also add or remove lights after the initial light mesh is created by modifying the mesh array.

    Below you can see one GameObject selected. It's mesh contains lots of lights with different colors and intensity (depending on the viewing angle in this case). All individual light properties are dealt with in the shader, making it very fast. Also note that the lights are HDR, which means that the center of the light can have an over saturated color, making it appear brighter.

    lights object.JPG

    The lights don't actually light other objects. The aim is to make the lights themselves look realistic. A good bloom shader is a must. The Unity post processing stack V2 works quite well.

    The funny thing is that there are thousands of references available on how a light affects an object. But the amount of references available on how the light itself looks you can count on one hand. I once found a scientific paper, but that's about it. Perhaps that is why very few people get it right. Often you see an emissive sphere with a flare sprite slapped on top of it. But that is a far cry from a physically based approach, which I will describe here.

    Most lights have a lens, which makes them either highly directional like a flashlight, or horizontally directional, the result of a cylindrical Fresnel lens. This directional behavior is simulated with a phase function which shows nicely on a polar graph. Here you can see two common light radiation patterns:



    The blue graph has the function 1 + cos(theta*2) where theta is the angle between the light normal and the vector from the light to the camera. The output of the function is the irradiance. Adding this to the shader gives the lights a nice angular effect.



    Next is the attenuation. Contrary to popular belief, focused lights (in the extreme case, lasers) still attenuate with the inverse square law, as described here:
    http://www.quora.com/Is-the-light-f...distance-grows-similar-to-other-light-sources

    But contrary to even popular scientific belief, lights themselves don't behave in quite the same way, or at least not perceptually. The inverse square law states that the intensity is inversely proportional to the square of the distance. Because of this:



    You see this reference all over, for example here:



    Yet the light itself is brighter than bar number 4, which is about at the same distance as the light to the camera. The light itself doesn't seem to attenuate with the inverse square law. So why is this? Turns out that in order to model high gain light sources (such as directional lights), you need to place the source location far behind the actual source location. Then you can apply the inverse square law like this:



    Note that highly directional lights have a very flat attenuation curve, which can be approximated with a linear function if needed in order to save GPU cycles.

    Some more reading about the subject here (chapter Validity of the Inverse Square Law):
    http://blazelabs.com/f-u-photons.asp

    One other problem is that the light will disappear if it gets too far from the camera. This is the result of the light being smaller than one pixel. That is fine for normal objects but not for lights because even extremely distant or small lights are easily visible in real life, for example a star. It would be nice if we would have a programmable rasterizer, but so far no luck. Instead, I scale the lights up when they are smaller than one pixel, so they remain the same screen size. Together with the attenuation, this gives a very realistic effect. And all of this is done in the shader so it is very fast, about 0.4 ms for 10.000 lights on a 780ti.

    Since I made this system for a flight simulator, I included some specific lights you find in aviation, like walking strobe lights (also done entirely in the shader):



    And PAPI lights, which are a bit of a corner case. They radiate light in a split pattern like this (used by pilots to see if they are high or low on the approach):



    Simulated here, also entirely in the shader.



    Normally there are only 4 of these lights in a row, but here are 10.000, just for the fun of it. They have a small transition where the colors are blended (just like in reality), which you won't find in any simulator product, even multi million dollar professional simulators. That's a simple lerp() by the way.

    I should also note that the shaders don't use any conditional if-else statements (bad for performance) but it uses very fast lerp, clamp, and scaling trickery instead. So it plays nice even on low-end hardware.


    Performance

    With a few million lights on a 1080ti, the frame rate is about this in the demo scene:
    URP: 1200 fps
    Build-in pipeline: 600 fps
    HDRP: 400 fps

    To get even faster performance, set the light texture to point sampling with a max size of 32. The difference in quality is practically unnoticeable but it gives a nice performance boost if lots of lights are in view when using a fast video card, or when not a medium amount of lights are in view when using a slow video card.

    If you want to change the light mesh at runtime, don't create a new light mesh and destroy the old one as this is very slow. Instead, keep a copy of the mesh data in an array, modify the array (preferably using ECS), then update the light mesh with the new mesh data. Depending on how often you change the mesh data, you also might have to enable Mesh.MarkDynamic.


    Troubleshooting

    Can't see any lights:
    -Create a light mesh using the supplied API. Regular meshes are not supported.
    -Lights are black by default. If you do not set the color using the API, they will be invisible.
    -The default size is 0, so if the individual light size is not set, it will be invisible.
    -Unless you use omnidirectional lights, the lights are invisible if the camera is not pointing at them. Note that the light direction is set by the API.
    -The brightness is not set using the API or the brightness offset in the material is set too low.

    Lights placed at wrong location:
    -The position in the light data struct is relative to the game object. So if you place the lights in world space position, make sure you place the light GameObject at location 0,0,0.
    -The position value in the API is not set correctly.

    The strobe lights do not work:
    -Strobe lights are directional by default so they are invisible if the camera is not pointing at them. Note that the light direction is set by the API.
    -The strobe ID is not set. See the manual chapter Strobe Setup for details.
    -The strobe time step in the Init() function is set incorrectly. See the manual chapter Strobe Setup for details.

    Post processing related errors:
    -Make sure you have the Unity post processing stack installed.

    Distant lights are disappearing:
    -Make sure the Init() function is correctly setup. If the field of view and screen size are set incorrectly, distant lights may disappear. Note that when using VR, the required variables might be incorrect when using the Unity API to retrieve those values. In that case, use the native VR API for the device to retrieve the correct values.

    Lights are flickering:
    -Light size is too small. Try size 1.0f. Either that, or set a smaller size but increase the "minimum screen size" value on the material.
    -The brightness is set too high. A value of 1 will usually lead to flickering. Try 0.2 instead.
    -Call the Init() function each time you create new lights. The SprightLight shaders contain global variables which need to be set correctly.
    -Too aggressive post processing.


    Download

    Support for Build-in render pipeline, URP, and HDRP:
    https://assetstore.unity.com/packages/vfx/shaders/spritelights-46409
     
    Last edited: Aug 20, 2021
  2. b4c5p4c3

    b4c5p4c3

    Joined:
    Jan 4, 2013
    Posts:
    537
    WOW that demo video looks very cool
     
  3. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    Thanks. It looks even better in VR :)
     
  4. Gozdek

    Gozdek

    Joined:
    Jun 21, 2015
    Posts:
    356
    Excellent!
     
  5. ikemen_blueD

    ikemen_blueD

    Joined:
    Jan 19, 2013
    Posts:
    329
    Bookmarked. Your demo is impressed :) I gotta finish my AI system soon, so I can play with your Lights system.
     
  6. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    Thanks. If you like it, please leave some nice words in the Asset Store review section. That will at least pimp up my profile a bit.
     
  7. Skolstvo

    Skolstvo

    Joined:
    Dec 21, 2015
    Posts:
    107
    I absolutely love this asset. Free is way too cheap for this.

    I'm having a bit of a noob problem with generating new lights. I can use the debug material but if I use any other material then my lights don't show up.
    I think it's the SpriteLights.Init that is causing the problems. The debug material doesn't need SpriteLights.Init to function. When I attempt to initialize the lights they become bugged somehow.

    Code (csharp):
    1.  
    2.   public float globalBrightnessOffset = 0.0f;
    3.   public Material material;
    4.  
    5.   private GameObject[] m_AircraftWarningLightsGameObjects;
    6.   private Transform[] m_AircraftWarningLights;
    7.   private float strobeTimeStep = 0;
    8.   private float FOV = 60f;
    9.   private float screenHeight = 600f;
    10.  
    11.   // Use this for initialization
    12.   void Awake()
    13.   {
    14.  
    15.    
    16.   m_AircraftWarningLightsGameObjects = GameObject.FindGameObjectsWithTag("Aircraft Warning Light");
    17.   m_AircraftWarningLights = new Transform[m_AircraftWarningLightsGameObjects.Length];
    18.  
    19.  
    20.   for (int i = 0; i < m_AircraftWarningLightsGameObjects.Length; i++)
    21.   {
    22.   m_AircraftWarningLights[i] = m_AircraftWarningLightsGameObjects[i].transform;
    23.   }
    24.    
    25.  
    26.  
    27.   }
    28.  
    29.   void Start()
    30.   {
    31.   FOV = Camera.main.fieldOfView;
    32.   screenHeight = Screen.height;
    33.   GenerateFunc();
    34.   }
    35.  
    36.   private void GenerateFunc(){
    37.  
    38.   SpriteLights.LightData[] lightData = new SpriteLights.LightData[m_AircraftWarningLights.Length];
    39.  
    40.  
    41.   for (int i = 0; i < m_AircraftWarningLights.Length; i++)
    42.   {
    43.   lightData[i] = new SpriteLights.LightData();
    44.   lightData[i].position = m_AircraftWarningLights[i].position;
    45.   }
    46.  
    47.   SpriteLights.CreateLights("AircraftWarningLights", lightData, material);
    48.  
    49.   SpriteLights.Init(strobeTimeStep, globalBrightnessOffset, FOV, screenHeight);
    50.  
    51.  
    52.   }
    53.  
    54.  


    Also, do you know if I got the parameters of the DK2 rift correctly?

    FOV = 106;

    Height = 1080

    Width = 1920
     
  8. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    I was going to make it payware but a lack of support time and a bad experience with the Asset Store staff made me change my mind.

    As for your lights not showing up, that happens because you forgot to set the color. If it still doesn't show, move your camera around the light as it might be directional, depending on the material you chose.

    Let me know if that works.
     
  9. Skolstvo

    Skolstvo

    Joined:
    Dec 21, 2015
    Posts:
    107
    The color wasn't the problem. I added the color but the lights are completely bugged after I ran the Init part once. They cover a large area of the screen end appear on screen intermittently.

    The light initialization is the one part of the manual that I didn't fully understand.
     
  10. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    In that case you better send me a repo project because after I added the color to your script it worked for me, so I assumed that was the problem.

    The init function sets a few global shader variables. The main thing is that the light scaling at a distance works correctly. For that it needs to know the pixel size, hence the requirement for the FOV and resolution.

    EDIT:
    By the way, for the correct Rift variables, it is best to get them via the Oculus Utilities API. I had trouble finding the correct values as well.
     
  11. Skolstvo

    Skolstvo

    Joined:
    Dec 21, 2015
    Posts:
    107
    I got everything sorted. It was a problem with the bloom script. I really can't thank you enough for this asset, and for your help. Have you had a chance to look at my reply to your baking thread?
     
  12. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    No problem, glad you like it. Feel free to post a nice review on the Asset Store ;-)

    Thanks, I have replied to the baking thread.
     
  13. Pascal-

    Pascal-

    Joined:
    Mar 23, 2013
    Posts:
    29
    Such a great release Elecman!
    I just discovered your project, and I'd like to know more about how to generate a custom light mesh from a .fbx file.
    Could you please help?
    Thanks a lot,
    Pascal
     
  14. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    Sorry for the late reply. I have turned on email notifications now.

    You would have to modify the mesh generation script so that it takes the vertices from the fbx and uses that to build a new light mesh. Using the fbx directly is not possible because the light mesh is abusing the mesh format to store custom data which is not compatible with a regular mesh format.

    Why would you want to do that? I think it is easier to use the API to generate the mesh on the fly.
     
  15. Antipirina

    Antipirina

    Joined:
    Aug 7, 2013
    Posts:
    18
    Hi, I want to change the color in runtime of an omnidirectional light. I tried but I couldn't. Is it possible?
     
  16. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    The color for omnidirectional lights is stored in the mesh. This is so that every individual light can have a different color. If the color is set in the shader, all lights would have the same color.

    If you don't need to change the color every frame, you can just make a new mesh. If you want to change the color every frame, you can use the the Debug light shader. It is not optimized for speed, but it is still much faster than creating a new light mesh every frame.

    If the Debug lights are still too slow, let me know.
     
  17. Antipirina

    Antipirina

    Joined:
    Aug 7, 2013
    Posts:
    18
    Thanks for the reply, Elecman.
    I want to change the color of about 57000 lights each frame. It's like a TV screen but spread in shapes, each light would be a pixel of a video.
    You say that Debug lights are the one that can change the color each frame? I'll try and check if they're performant enough
    UPDATE: looks like it doesn't work for Debug lights to set the color by code. Omnidirectonal lights can change it but not after its creation.
    Am I doing something wrong? Is there a way to change the color in runtime each frame of an omnidirectional light (or similar)?
    Thanks!
     
    Last edited: Apr 1, 2017
  18. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    You should be able to change the debug lights by code by setting a shader property. But in your case that won't help because that will change the same color of all lights.

    It will require a new shader but so far I can't come up with a way to store color information dynamically for each light individually.
     
  19. Antipirina

    Antipirina

    Joined:
    Aug 7, 2013
    Posts:
    18
    Thanks anyway!
     
  20. SatanshuMishra

    SatanshuMishra

    Joined:
    Jul 12, 2017
    Posts:
    6
    Can anyone link me to a page which will teach me how to write the code to generate the lights? I am fairly new and need some help with this kind of stuff.

    By the way, excellent demo...looks great
     
  21. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
  22. SatanshuMishra

    SatanshuMishra

    Joined:
    Jul 12, 2017
    Posts:
    6
  23. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,470
    I purchased this in 2015 and finally, I'm close to using it. I open it in Unity 2017.1.f and there are 12 warnings. I try to maintain my console clean. Keep in mind that If each asset from asset store gives me 12 warnings, I will get hundreds for just 10 asset. Can you take out from the console the depreciation code warnings including the code in an answer? and eventually, submit an update?

    1) in RunwayLights.cs(67,10) and RunwayLights

    Code (CSharp):
    1. //Randomize the random function.
    2. Random.seed = ...
    3.  

    2) in SpriteLights.cs(310,17) and RunwayLights

    Code (CSharp):
    1. //Set the gameObject properties;
    2. meshRenderer.useLightProbes = ...
    3.  
    3) in BloomEditor.cs(88,64)
    What do we need to do here? We need to take it out and use the new rendering stag?
    Are you using the Unity bloom that I can substitute

    Code (CSharp):
    1. // display info text when screen blend mode cannot be used
    2. UnityEngine.Camera.hdr ...
    3.  
    4) same for TonemappingEditor inside Image Effects folder

    5) same in PostEffectsBase in ImageEffects folder

    Code (CSharp):
    1. protected bool CheckSupport (...
    2. if ( SystemInfo.supportsRenderTextures ...
    3.  
    6) Shader warnings: Use of UNITY_MATRIX_MV is detected.
    in Lights/Debug, Strobe, PAPI, Directional and Omnidirectional shaders

    I test it with VR and is so cool!
    Big thanks
     
  24. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    Thanks for the report. I will look into it and fix it.
     
    AlanMattano likes this.
  25. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    I have fixed it and added a link to the Unity 2017.1 version at the first page.

    It contains the new Unity Post Processing Stack (v2 beta).
     
    Last edited: Sep 22, 2017
    AlanMattano likes this.
  26. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,470
    Hoo, :eek: I didn´t purchase it; It was free and awesome support! Big thanks! :rolleyes:

    PS: If you have time, make a pro pay version that can include a custom array inspector for putting manually the light in a big scene. Some random light effects [random size, random on off on rotation, random intencity] and instantiate distant lights in a pool manager system...?
     
  27. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    Random effects are already possible. See the API for details. I won't make a custom inspector though, because that is only handy if you are editing a small amount of lights. In that case you might as well use normal gameObjects instead of the light mesh system. The power of SpriteLights is in rendering millions and millions of lights efficiently but editing those lights manually is not realistic.

    I also won't make a pay-pro version because of lack of time to support it. There are so many assets on the asset store which are abandoned. The same may happen with this one but at least I won't give false hope.
     
  28. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,470
    a donation button link?
     
  29. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    No need. I have a well paid day job ;-)
     
  30. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,470
    Elegant solution!

    I did not integrate this light to the sim yet. Do you have any suggestion idea or improvement to make to this asset so I include it in my todo list? something that you would have liked to see or integrate.
    For example, I was thinking to illuminate the sky, the base of the clouds over cities...or try to add a shiny shape.
    Any new idea?
     
  31. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    The illuminated clouds at night over a city is a good idea. I don't think any sim has implemented that. I think the most important thing is to use a proper atmospheric scattering implementation, which includes rayleigh and mie scattering, and height fog with a depth blur effect.
     
    AlanMattano likes this.
  32. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    Unity 2017.3 supports a 32 bit mesh index buffer. This allows for 2^32 vertices, which is 4.294.967.295 vertices. Divide this by 3 and you get 1.431.655.765 triangles and the equivalent amount of lights. Divide this by the old 16 bit triangle count of 21.844 and you get a draw call (set pass) reduction of 65.540!

    I need to source a new Open Street Maps data set for that many lights to test. For science.
     
    AlanMattano and hopeful like this.
  33. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,470
    In my game, before 2017.3, I was including several meshes with more than 65.000. I think my game was like 300.000 triangles. And there are some others that push it until 10.000.000 triangles (rendering PBR shader material) using several meshes. I presume that with the new 32 mesh index, is possible to make one big mesh but I do not think the GPU can handle 1.431.655.765 triangles (rendering PBR shader material). Is interesting to look if it is possible using your sprite lights.
     
  34. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,521
    That's why they call it science. Be sure to put your safety goggles on. :)
     
    AlanMattano and Elecman like this.
  35. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    Added a Unity 2017.3 version which supports up to 1.4 billion lights in a single mesh. See the first post for the link.
     
    AlanMattano and hopeful like this.
  36. hopeful

    hopeful

    Joined:
    Nov 20, 2013
    Posts:
    5,521
    L.O.L. :D
     
  37. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    Updated to v2.1

    Change log:
    -Added a shader to be used for city lights or other single color, single brightness omni directional lights. It uses a smaller mesh size and is faster to render.
    -Generated city lights are not overwritten if they already exist.
    -City light generation is faster and uses less data.

    Unitypackage available at the same 2017.3 link in the first post.
     
    hopeful likes this.
  38. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    Updated to v2.2

    Change log:
    -Fixed a bug which caused incorrect city light generation.
    -Updated the manual to include the asset generation function.
     
  39. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    Updated to v2.3

    Change log:
    -Much faster mesh generation.
    -Faster light shaders.

    You will have to re-generate any saved light mesh if you used a scale other than 1 because the new shader uses slightly different scale logic.
     
    AlanMattano likes this.
  40. WatchYourVector

    WatchYourVector

    Joined:
    Jun 17, 2013
    Posts:
    24
    Hello,

    Thank you for making this free on the Asset Store! I realize support is limited, but I will ask on here in case someone already solved it.

    I am trying to make the runway lights generate on top of a rectangular, thin, shape that is meant to represent a runway. I figured out how to position and rotate the generated runway lights within the CreateRunwayLights function. However, I don't understand if there is a way to space out the lights so they match the size of the object I am attaching it to.

    Is there a length variable I should be setting somewhere, or does the sample code work by just generating new lights x distance from the previous ones? I did see settings for a width in CreateRunwayLights, but not for length. It also seems to be grabbing svgData.length data from a file, which makes me think the sizes are hard coded.
     
  41. AlanMattano

    AlanMattano

    Joined:
    Aug 22, 2013
    Posts:
    1,470
    There are so many lights that I miss the airport position!! super awesome :D
     
  42. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    You can set the length and the width of the runway in the RunwayLights script. Have a look at the CreateRunwayLights() function, there you will find the variable .width and the function .SetThresholdPosition()
    The second input variable of SetThresholdPosition() is the runway length.
     
    WatchYourVector likes this.
  43. WatchYourVector

    WatchYourVector

    Joined:
    Jun 17, 2013
    Posts:
    24
    Thanks for the suggestion, but it doesn't seem to work for me. I tried a width of 8 and a length of 32 and the generated lights were still taking up a much larger space.
     
  44. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    This is probably due to the light spacing being 15 m for center line lights and 60 m for edge lights. A unit in Unity is 1 m, so if you want to use a runway of 32 x 8 m (for model airplanes?), you need to change the light spacing.

    The approach lights are hard coded in the svg files. I suppose you can scale them but it is probably easier to build them from scratch using code if you want to use a non-standard layout.
     
  45. WatchYourVector

    WatchYourVector

    Joined:
    Jun 17, 2013
    Posts:
    24
    Happy Holidays, and thanks for the reply!

    The runway I am working with is indeed a very small size, so I realize it will be tricky to fit all the lights in the example, within it. I thought the layout you have of the lights looked awesome though, so I hope I can recreate it from scratch on a smaller scale.

    The only tricky part in recreating it, is that I am not familiar with the exact rules and regulations of where each type of light should be positioned and spaced out, based on the size of the runway. I am guessing there is a formula or cheat sheet somewhere, per type of light.
     
  46. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    I think it would be much easier to just scale the game object instead. I discovered some bugs related to this but I have fixed it now. If you download the 2017.3 version again, it will contain a scale variable in the CreateRunwayLights function. Try setting it to 0.1 and you will see that it will scale both the runway dimensions and the light sizes. You will still need to set the runway width and length though, to make sure the runway has the correct proportions. If you use a scale factor of 0.1 then you need to set the runway size to 320x80 in your case.

    However, if you use those dimensions, the layout still looks weird. This is because the light layout I use is the same as real runway lighting. Real runways are much longer compared to the width and length you are using. You have to make the runway less wide for the current layout to look good. If you insist in using the same light layout with your runway dimensions, the only option you have is to construct the light mesh from scratch and choose your own light layout which looks good for the wide runway.
     
    WatchYourVector likes this.
  47. haleyon

    haleyon

    Joined:
    Jan 3, 2014
    Posts:
    2
    Hi Elecman, wonderful asset you have created here. It's cool to see the massive lightshow using so much less computing power. I was planning to use the SpriteLights to create faraway house lights.

    I have a question, by the way. Everytime I close my Unity window and reopens the scene, the lights don't appear anymore and I have to redo the lights. How do I make it so the light stays, even after I close the program?
     
    Last edited: Jan 5, 2018
  48. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    You will have to save the light mesh after creating it. You then add this mesh to a game object and place it in the scene. This way it becomes persistent. The latest version does this automatically for the city lights so you can have a look at the code to see how that works.

    You want to have a look at the function CreateAssets() in CityLights.cs. Note that saving assets only works in Editor mode. If you want to save an asset created at runtime (some sort of cashing solution) you will have to use a different method.
     
    Last edited: Jan 5, 2018
  49. Mad_Mark

    Mad_Mark

    Joined:
    Oct 30, 2014
    Posts:
    476
    I have been playing with this a little, and it's absolutely great! Good work @Elecman, and thank you for making it free!

    I realize there is no support as it is a free asset, however, on the chance that others are in the same predicament, or have found a solution...
    I am working on a space simulator. In order to overcome Unity's float precision issues, I have scaled my entire environment by a factor of 10. Space is a big place, and it is not intended to represent reality... I have scaled down the landing strip through code, but of course that messes up the scale of the lights, pretty much merging all of the glorious artwork into blobs of light on the left, and blobs of light on the right. It works better than nothing, and helps players find the repair station.

    1) I didn't really want it to reach that far into space. Is there an easy way to make the landing strip shorter?
    2) Is there a better way to make the landing strip narrower? Besides the above merging issue, the green and red landing approach lights are virtually lost.
    3) I would also love to use the city lights on some of my procedurally generated planets. Is there a way to use that mesh as the target mesh?
    4) are there any useful videos or tutorials that might answer my questions?

    Thanks again,
    Mark
     
  50. Elecman

    Elecman

    Joined:
    May 5, 2011
    Posts:
    1,362
    First make sure you are using the latest version from the forum, not the asset store.

    If making the landing strip shorter and narrower in code (as described in earlier posts) doesn't work properly, try just scaling the game object itself. Just pick the empty parent object and scale that in x and z to match the length and with look you are going for.

    To adjust both the size of the runway and the size of the lights itself, go to RunwayLights.cs and look for the function CreateRunwayLights(). It contains a scale variable which does exactly that. You can use this in combination of scaling the runway mesh.

    If you want to scale only the runway lights size itself (not the light location), find this piece of code in CreateRunwayLights()

    Code (CSharp):
    1. //Loop through all runways.
    2. for(int i = 0; i < runways.Length; i++){
    3.  
    4.     //Create the light data and store in a temporary buffer.
    5.     SetupApproachLights(ref directionalLightData, allApproachLightData, runways[i], randomBrightness, brightness, largerLightSize * scale);
    6.     SetupTDZLights(ref directionalLightData, allApproachLightData, runways[i], randomBrightness, brightness, smallerLightSize * scale);
    7.     SetupStrobeLights(ref strobeLightData, allApproachLightData, runways[i], strobeTimeStep, largerLightSize * 3f * scale);
    8.     SetupThresholdLights(ref directionalLightData, runways[i], false, 0.6f, largerLightSize * scale);
    9.     SetupCenterlineLights(ref directionalLightData, runways[i], randomBrightness, brightness, smallerLightSize * scale);
    10.     SetupEdgeLights(ref omnidirectionalLightData, runways[i], randomBrightness, brightness, largerLightSize * scale);
    11.     SetupPapiLights(ref papiLightData, runways[i], largerLightSize * scale);          
    12. }
    Then adjust the scale input of the function accordingly.

    You can definitely use the city lights to cover an entire planet but the city lights mesh shipped with the example is placed on a flat plane so that won't look good on a planet from space. In this case, it is better to generate the lights from scratch in code using the provided API so that it wraps nicely around a sphere.

    There are no tutorial videos available unfortunately.

    Let me know if this helps.
     
    hopeful likes this.