Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[wip] Free Day/Night cycle

Discussion in 'Works In Progress - Archive' started by jister, Oct 31, 2015.

  1. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Hey I'm building a Day/Night cycle with some environmental transition options:

    - blend skybox
    - lerp fogDensity
    - lerp fogColor
    - lerp AmbientIntensity

    To do:

    - add Moon cycle Phases (no moon to full moon)

    optional:

    - Geographic based lightning
    - Weather system

    UPDATES:
    added Sounds to fade in/out during a transition.
    added Parallax moving Clouds.

    - added moving Stars
    - made parallax movement optional.

    Screen Shot 2015-11-06 at 18.49.52.png
    (download in attachment)

    it runs a world-clock which you can set your "seconds in a day" and based of of that you have timestamp entries.

    so for example say i want a sunrise starting at 6.30 ending at 7.30, you just add a transition with this start and end time, set a skybox (or not), set the density, color and intensity.

    I wanna make a free asset out of it, since i didn't find a good free one (if i'm mistaken please let me know)

    so any suggestions what to add to this?
    here are some vids:
    With Stars

    Moving Clouds

    procedural sky with sun and moon

    skybox sky


    Latest Package version:

    EnvironmentEventHandler (Download Link)
     
    Last edited: Feb 18, 2016
    Farelle, wakuwalt7 and Whiteleaf like this.
  2. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    added the possibility to start/stop a collection of atmosphere sounds.
    ... any other ideas to add?
    should i make an editor window for it for ease of use?
     
  3. Ysgramor

    Ysgramor

    Joined:
    Jan 23, 2014
    Posts:
    69
    Hi this is for Unity5?
    I'll try this if already released, im also searching some good day night cycle, and this will be my best choice.

    For editor window, i think that a good idea, for easy changing settings
    also maybe add stars in the sky? :)
    Great work so far
     
  4. mliukka

    mliukka

    Joined:
    Nov 1, 2015
    Posts:
    19
    This is looking great! Is there a possibility to control the sun angle? Now it looks like the sun is rising more or less vertically.

    Custom editor window for easy adjustment of color curves would be great. :)

    Some suggestions:

    -Possibility to adjust sunrise/sunset time (day is longer than night during summer season, a 6 AM to 6 PM day basically only happens near the Equator)
    -Moon phase based on moon position relative to the Sun
    -Stars skybox rotation at night (I don't know if you already have this?) - basically, all stars should move across the sky at the same pace as the Sun does

    Keep up the good work! Looking forward to seeing more of this!
     
  5. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    ok thanks I'll try implementing these things tomorrow, and start on a editor window :)
    i'll have to think about the possibility of changing day/night length. this happens mostly because we move around the sun in an ellipse... but here its the sun rotating around the world.
    you would need to be able to set the position for the center where the sun rotates around, set an ellipse path and be able to give the path a rotation offset so that there is a difference in the day/night curve...

    edit: or i 'll just fake it with a speed you can set for your sun/moon with a entering time and exiting time
     
    Last edited: Nov 2, 2015
  6. mliukka

    mliukka

    Joined:
    Nov 1, 2015
    Posts:
    19
    The problem with setting different speeds for the sun during day and night is that actually during summer months the sun stays above the horizon more than 180º (so it doesn't rise from east and set in west - on the Northern hemisphere it rises from northeast and sets closer to northwest, which makes the day last longer). Probably the easiest way to replicate this would be to do some math in the script that determines the sun position on the skydome at each moment, and then just set the position / rotation of the sunlight each frame, instead of continuously rotating the sun at a certain speed.

    I'm just being picky about the accuracy of such a system, the thing is I've been wishing for a proper day/nicht cycle for a long time; I used Cryengine for years and at some point started to notice flaws like the moon being lit from the opposite side of where the sun actually set, the stars and moon being stationary during nighttime, and the inability to create a longer day and shorter night with accurate results. :)
     
  7. Ysgramor

    Ysgramor

    Joined:
    Jan 23, 2014
    Posts:
    69
    Hi i have moon and sun position script based on this http://www.stjarnhimlen.se/comp/ppcomp.html from my old project, this acurate enough i think, if u want it i'll try cleaning it since this realy old.

    basically this calculate sun and moon altitude and azimuth based on time (hh/mm/ss/dd/mm/yyyy) and position LAT/LON.
    Im also try implementing stars rotation, but didn't accurate enough since i didn't have base start time when stars position are in reality.

    since im not good at shader, im stuck at making moon, clouds, and stars realistically :(. so i just grab sphere object for moon :p

    sorry for bad english.
     
    Last edited: Nov 2, 2015
    mliukka likes this.
  8. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    @mliukka that would mean that you should be able to choose a geographical position on a sphere.
    all sounds interesting but maybe a bit more work than i bargained for a free asset :)
    @Ysgramor thanks I'll have a look. if it works i'll implement it. If not, i'll finish this as is and maybe start a new one where you have all these global positioning based options.
     
  9. mliukka

    mliukka

    Joined:
    Nov 1, 2015
    Posts:
    19
    Wow Ysgramor, I was more thinking along the lines of "choose desired day length -> offset sun's circular path upwards so that the path is long enough"... :D

    Got to admit astronomically correct sun/moon positions based on lat/lon would be cool though. :)
     
  10. Ysgramor

    Ysgramor

    Joined:
    Jan 23, 2014
    Posts:
    69
    Hi all, after few hours of cleaning stuff, here some example project so far, fell free to modifying it for best result.
    I'll also trying to fixing some "unfinished" code, like the starfield (currently didn't move as expected).

    This file size big because of 2 huge starmap texture.

    https://drive.google.com/file/d/0B-9lh65U4CIrT2FySFlzVHpjVWM/view?usp=sharing

    Sorry for Uncleaned Script, maybe later i'll clean it :p
    maybe I'll get some spare time "again" to expanding, after im abandoned this:D
     
    Last edited: Nov 2, 2015
    mliukka likes this.
  11. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    looks nice, if i can get my head around your calculations i'll try implementing it.
     
  12. Ysgramor

    Ysgramor

    Joined:
    Jan 23, 2014
    Posts:
    69
    Sun and moon calculations are based on link i gave earlier, detailed calculation things explained there, im just convert it into C# :)
    Im ready for some help if ure feel lost in my script :D
     
  13. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Ok so i finished the editor window for now.

    To do:
    - add soundlist to editor window.
    - implement Sun/Moon position based on Latitude and longitude.
    - add moving clouds.
    - add moving starts.

    if you guys would be so nice and try to break it :)

    edit: lolz i see i still have the title for my class as the menu :)
     

    Attached Files:

    Ysgramor likes this.
  14. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Added the possibility to fade a list of sounds in and out at a certain timestamp.

    next is moving clouds and stars :)
     

    Attached Files:

    Kellyrayj likes this.
  15. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Added clouds, not moving yet and not sure if they need to move...
    you can set overall cloud density for the sky, min-max size and min-max height for the clouds to randomly generate individual size and height for a cloud.

    20:43:26.png
    20:43:44.png
    20:45:0.png
     
  16. Rombie

    Rombie

    Joined:
    Feb 18, 2015
    Posts:
    282
    This is looking good so far; I will definitely give it a test run. Been looking for a good cycle system.

    Keep up the great work; can't wait to see how this develops into down the road.
     
  17. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    well decided to make them moving anyway.
    I'll work it into the editor and upload a new package tomorrow.
     
  18. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Made the cloud movement parallax.
     
  19. RonnyDance

    RonnyDance

    Joined:
    Aug 17, 2015
    Posts:
    557
    Wow for a free System this looks pretty awesome!
    Respect that you are sharing this. Can you also show a video to storm / tempest skyes when you increase the cloud ratio? Since I am working on a dark horror game I am pretty interested to see how I can use your asset for creating dark / foggy nights. Fog Ratio and options are possible to set?

    Keep up the good work!
    Ronny
     
  20. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    there is no change in cloud texture (yet?) if you increase or decrease cloud density. see video.
    I was thinking of adding in a weather system... but thats for an other time... would be cool though, generating lightning, rain, clustering clouds and such :)

    as you can see in the screenshot of the editor, you can set fog color and fog density, didn't think it necessary to adjust fog mode.



    and here is the package:
     

    Attached Files:

  21. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    noticed some bugs, and change the popping in and out of the clouds, when adjusting density, to fading.

    ps: anyone knows a good particle shader with bump map? or rim lighting?
     

    Attached Files:

    Last edited: Nov 5, 2015
    KingLlama likes this.
  22. ThemSellech

    ThemSellech

    Joined:
    Sep 28, 2013
    Posts:
    3
    Good job
     
  23. KingLlama

    KingLlama

    Joined:
    Jul 18, 2015
    Posts:
    199
    Whoa, Thank you so much! I can't wait to test this in the morning!
     
  24. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    :) no problem, glad people can use it.
    let me know if you encounter bugz.
     
  25. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Updated with moving stars, they move parallax now, but i think it looks kind of odd... so i made it optional.
     

    Attached Files:

    RonnyDance and KingLlama like this.
  26. KingLlama

    KingLlama

    Joined:
    Jul 18, 2015
    Posts:
    199
    Would you be able to create a tutorial on this? I got the sun moving but the moon doesn't come up. I have no idea what I am doing.
     
  27. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    I'll make a video tutorial when finished.
    if you mean you don't see a moon like in the video's, that because i dropped a moon sprite inside the moon directional light the system creates.
    I could automate that... i didn't think it necessary because i though people would want to use their own moon.
    it might just as well be a sphere or a model.
    Edit:
    Made a moon object slot to add a moon model.
    Edit2: grr bugz, uploaded new package
     

    Attached Files:

    Last edited: Nov 6, 2015
    KingLlama likes this.
  28. KingLlama

    KingLlama

    Joined:
    Jul 18, 2015
    Posts:
    199
    Understanding how to use it more and more each time. It doesn't work correctly with Suimonos ocean due to the sun still being on when it rotates and it causes the ocean to stay lit up all day and night.



    The problem with the ocean though.
     
  29. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    here's a 10min vid for the main setup.
    i think light probes could fix your water problem.

    as you can see/hear in the video, i still need to change some things and get rid of some null references...

     
  30. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    Ok got rid off the null references and i think it's finished for now. also my time to work on this at the moment is up so
    if anyone who tried it encountered nasty bug let me know than i'll look into them otherwise I'm moving on to a showcase thread and upload it to the asset store, if i can... updated the first post with my final build.
     
  31. KingLlama

    KingLlama

    Joined:
    Jul 18, 2015
    Posts:
    199
    It seems the build in the first post has alot of problems. http://i.imgur.com/aVAZg2R.png I imported it to my current project and just tried to open it and just instantly stopped working. I'm going to try the other download.
     
  32. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    hmm yes, well it's having trouble finding you sun and moon. I'll look into it.
    to fix it just rename your sunLight to Sun and make sure there is a Moon directional light, if there isn't make one with the DayNightCycle window.
     
  33. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    changed it so it looks for the more intense directional to take as the sun. so it's not name based anymore :) which was rather stupid... damn and i uploaded it like this to the assets store for review... ah well.
     

    Attached Files:

  34. Whiteleaf

    Whiteleaf

    Joined:
    Jul 1, 2014
    Posts:
    728
    Wow! This looks amazing! It being free is a bonus, too. :)
     
  35. wakuwalt7

    wakuwalt7

    Joined:
    Oct 19, 2015
    Posts:
    6
    This is really interesting! Good job!
     
  36. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    resubmitted to the asset store, keeping fingers crossed I did everything right this time :)
     
    KingLlama, RonnyDance and Rombie like this.
  37. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    rejected again, hmmm and they don't say why... :-(
    First time i thought it was because i didn't have documentation nor a read me nor a demo scene, so i added all that.
    could be I'm submitting it wrong. I used some standard assets and an Editor folder, which i all put inside of 1 folder together with all the rest and mark that as the main asset folder...

    I don't really have more time to spend on this as a free asset...
    I'll go over it one more time, else If anyone wants to have a look at it as to why it gets rejected please let me know...
    or does anyone know where one can ask unity for the reasons of rejection?

    this is the package i tried to submit...
    https://dl.dropboxusercontent.com/u/32556668/DayNightCyclePackage.unitypackage

    could my code not being commented be a reason?
     
    Last edited: Feb 6, 2016
  38. RonnyDance

    RonnyDance

    Joined:
    Aug 17, 2015
    Posts:
    557
    I did not check your code / package but it's also important that you have some informations about yourself as a constributor if you commit an asset in your publisher informations. Unity rejects assets if you don't write some lines about yourself. So something like this:
    "Game development generalist working on free time on Unity5 assets." should be fine.

    And of course your package should be work and imported without errors. Commented and clean code is also important.

    If I have free time I will check your package. Standart Assets should be kept in the same standart asset folder unity creates if you import them by yourself.

    Cheers
     
  39. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    thx RonnyDance, now i at least have some idea what to do before submitting again.
    indeed i didn't write anything about myself.
    this is what i have as description:
    this is the preview link...
    https://www.assetstore.unity3d.com/en/#!/preview/49671/149429

    anyway i won't give up, commenting all my code atm ;-)
     
  40. Rombie

    Rombie

    Joined:
    Feb 18, 2015
    Posts:
    282
    I've seen assetstore packages rejected because the publisher doesn't have a website. So if you don't have one it might be worth it to add a quick one as well. Develop the site to pretty much just be a 'support' site for products which you publish and Unity should be good with it.
     
    jister likes this.
  41. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    website check :)
    http://rottchild.wix.com/rottchildinc
     
    Rombie likes this.
  42. Rombie

    Rombie

    Joined:
    Feb 18, 2015
    Posts:
    282
    Site looks good; should do the job. Good work. Best of luck when you submit the asset package again.
     
    jister likes this.
  43. manpower13

    manpower13

    Joined:
    Dec 22, 2013
    Posts:
    140
    Hi @jister

    First of all thank you for sharing this for free.
    I downloaded this, because I made my own system for my game and was not very happy with it. Your pictures look very great, so I was wondering if I could achieve the same for my game.
    The editor window looks great and has a very good layout.
    There are some bugs:
    - The moon object gets renamed to sun and is acting a little strange. I need to delete it every time and recreate the moon.

    It would be nice if there was a button to delete all stars and clouds the user has made in the editor. That way creating new clouds and stars is easier.

    I think you should deactivate the sun when night starts. That way users will never encounter a light from outside the ground.

    Overall a great system, but has some small things to be noticed.
    It would also be great if you used your own namespace, that way you will never interfere with someone else his code.

    Thank you for your time :)
     
  44. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    @manpower13 Thanks for the feedback,

    I looked into the moon bug you said, but can't seem to replicate the behaviour, could you show me your set up.
    only reason i can think of is your moon being as intense, or intenser then you sun. the system looks for the brightest light in the scene and takes that as the sun.
    could you see if it still happens if you set the moonlight's intensity less than your sunlight's?

    here's is the code that handle your sun, when you focus the editor window.
    Code (CSharp):
    1. GameObject g = GameObject.Find("Sun");
    2.         if(g == null)
    3.         {
    4.             Light[] lights = FindObjectsOfType<Light>();
    5.             List<Light> lightsList = new List<Light>(lights);
    6.             lightsList.OrderBy(o=>o.intensity).ToList();
    7.  
    8.             for(int i=lightsList.Count-1; i>=0; i--)
    9.             {
    10.                 if(lightsList[0].type != LightType.Directional)
    11.                     lightsList.Remove(lightsList[0]);
    12.                 else if(lightsList[0].type == LightType.Directional)
    13.                 {
    14.                     sun = lightsList[0];
    15.                     sun.gameObject.name ="Sun";
    16.                 }
    17.             }
    18.         }
    19.         else
    20.         {
    21.             sun = g.GetComponent<Light>();
    22.             if(sun.transform.eulerAngles != new Vector3(0,90,0))
    23.                 sun.transform.eulerAngles = new Vector3(0,90,0);
    24.         }
    for the star/cloud removal i'll add a button, for now just delete the "SkyClouds" and SkyStarts" gameobjects if you want to remove them. they get regenerate automatically if you focus on the AddClouds or AddStars editor windows.

    and will do for the namespace.

    if i've the time all will be added and fix after this weekend.
     
    manpower13 likes this.
  45. jister

    jister

    Joined:
    Oct 9, 2009
    Posts:
    1,749
    @manpower13
    Ok implemented all the tips you gave, except for disabling the sun during the night, because unity already makes sure you can't see the sun or light when it's below the horizon...

    about not getting accepted in the asset store (didn't try again yet) could it be because I didn't implement Undo function?(submission guidelines says:" Undo for all editor steps is mandatory.")
    because i didn't really see a need to undo in this system...

    a hell i guess I better do it before trying to submit again... :-(

    If it gets accepted I'll start expanding this with a weather system, volumetric clouds with shading, more night effects (falling stars, northern light, ...) but i doubt if i can keep it free then. Anyway if so, I'll keep it cheap.

    UPLOADED THE NEW PACKAGE, so all links should get you the latest one.
     
    manpower13, Rombie and RonnyDance like this.