Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Dynamic Weather // Day night Cycle

Discussion in 'Made With Unity' started by ikke998, Jul 21, 2010.

  1. ikke998

    ikke998

    Joined:
    Jan 7, 2010
    Posts:
    79
    Update I'm also working on a customizable Day/Night cycle that will work with the time of day.
    here is the --> WIP <--

    It works with a 24h system that can be sped up or down.
    Differnt tints can be added at different points of the day.
    The clouds are not part of the skybox!

    This package will be released to be used in your Unity Projects as a free drag and drop prefab when it is done.


    I tried to make a dynamic weather system for First Person Games in Unity.

    This is bad Weather:
    Weplayer here
    The slider controls the weather


    I can't seem to find anywhere if you can control the wind settings for trees and grass with scripting, anyone want to help on that?
     

    Attached Files:

  2. gamenut30111

    gamenut30111

    Joined:
    Nov 7, 2009
    Posts:
    399
    I looked into it and found nothing, maybe something like this

    Code (csharp):
    1. var terrain : GameObject;
    2. var other;
    3. other = terrain.GetComponent("Terrain");
    4. other.speed = 1;
    5.  
    Just an idea, since nothing appears in the documentation, maybe as a mod.
     
  3. Alex Mat

    Alex Mat

    Joined:
    May 14, 2010
    Posts:
    177
    There must be a way but it isn't documented, so unless someone already found it OR you try guessing/reflecting OR if a Unity dev will tell you, I don't think you'll find the way to change wind speed :p
     
  4. MegadethRocks

    MegadethRocks

    Joined:
    Dec 12, 2009
    Posts:
    162
    IntelliSense came up with some grass stuff for me, but I didn't see anything for trees.

    Terrain.terrainData.wavingGrassAmount
    Terrain.terrainData.wavingGrassSpeed
    Terrain.terrainData.wavingGrassStrength
    Terrain.terrainData.wavingGrassTint

    Not sure if they'll work, but maybe you can give them a try.
     
  5. ikke998

    ikke998

    Joined:
    Jan 7, 2010
    Posts:
    79
    I tried to catch the value of Terrain.terrainData.wavingGrassSpeed inl a variable but Unity editor crashes on me when I run the script.
    I hope this will be included in Unity 3.
     
  6. Slem

    Slem

    Joined:
    Jan 28, 2009
    Posts:
    191
    Unity 3 includes wind zones :)
     
  7. TwiiK

    TwiiK

    Joined:
    Oct 23, 2007
    Posts:
    1,656
    Weird that no one has said this, but that looks and sounds fantastic. :)

    I love the transition.

    One thing that would make it better was if you had an animated skybox or something as well.
     
  8. ikke998

    ikke998

    Joined:
    Jan 7, 2010
    Posts:
    79
    I'm working on a Day/Night cycle right now :D
     
  9. Nick3d

    Nick3d

    Joined:
    Jan 28, 2010
    Posts:
    100
    Wonderful! dy/night cycle is something that I would like to do too, please show use when it's done, I'm really curious :D
     
  10. ikke998

    ikke998

    Joined:
    Jan 7, 2010
    Posts:
    79
    This is what I have so far

    edit: Oh and I can't get the directional light to work with grass :?. Does anyone know how to dynamically light the grass on a terrain?
     

    Attached Files:

  11. Alex Mat

    Alex Mat

    Joined:
    May 14, 2010
    Posts:
    177
  12. Nick3d

    Nick3d

    Joined:
    Jan 28, 2010
    Posts:
    100
    Did you script everything? I mean, it is difficult to setup an animation for the sun in order to do a cycle?
    Sorry but has been a while since I opened Unity, now I'm building a huge set in Softimage and I haven't tried anything, so any help will be appreciate :D
     
  13. ikke998

    ikke998

    Joined:
    Jan 7, 2010
    Posts:
    79
    I can get the shader to work, but it looks crap with direct lighting.
    An ideal shader would be a grass shader that adapts to the light but can shade the grass on both sides.
    A bit like the vegetation-leaves shader.

    @Nick3D the scripting wasn't that hard. I think most of the scripting is finished.

    I now added a 24H cycle to the demo!
     
  14. gamenut30111

    gamenut30111

    Joined:
    Nov 7, 2009
    Posts:
    399
    just wondering if this is finished, i really could use it
     
  15. coin-god

    coin-god

    Joined:
    Jan 21, 2010
    Posts:
    325
    This looks so beatiful. Great work dude.

    I will surelly use it!
     
  16. DisTrash

    DisTrash

    Joined:
    Feb 2, 2009
    Posts:
    166
    Hello,

    This is great !! ;)

    How to use it if it is possible, can i download a unitypackage or other and can i use it even in a commercial way ?

    Thanks a lot for your work and sharing !! ;)

    ++
    :)
     
  17. ikke998

    ikke998

    Joined:
    Jan 7, 2010
    Posts:
    79
    Here is a package.
    If you want to add the TOD to your scene just drag the prefab in your scene and assign skyboxmaterial1 as your skybox.

    Mind this is a work in progress, I haven't had the time to make it very user friendly.
    With all of the experience from making this ToD I'm thinking of redoing it and making it a lot better.
     

    Attached Files:

  18. brendonnelly

    brendonnelly

    Joined:
    Aug 5, 2010
    Posts:
    39
    am i doing something wrong? when I drag it onto my scene, i get a null reference exception
     
  19. Simian

    Simian

    Joined:
    Jun 19, 2010
    Posts:
    8
    This works great, good job! Thanks for your hard work!
     
  20. BHS

    BHS

    Joined:
    Dec 21, 2009
    Posts:
    4,218
    This is awesome! Good job

    But 2 things how come when you move the light fades faster like it speeds up the process?

    And how can I slow it to where it's a little more realistic. It takes like 25 seconds to cycle through. Something like 10 or 20 mins would be awesome.

    Is there a way to do this?
     
  21. Argien

    Argien

    Joined:
    Aug 1, 2010
    Posts:
    18
    This is great, thanks for sharing :D
     
  22. BHS

    BHS

    Joined:
    Dec 21, 2009
    Posts:
    4,218
    Alright I figured out how to slow the time, I didn't see the deltatime in the script the first time.

    Another suggestion you should make the day time longer then the morning and evening. It seems like there all pretty equal when in reality morning and evening are brief.

    How did you get it so dark at night? I can't figure it out, do you have pro? Or am I just missing something.
     
  23. ikke998

    ikke998

    Joined:
    Jan 7, 2010
    Posts:
    79
    :D Glad you like it!
    I think I put the night tint really dark to make it dark at night!
    there are things that could be improved upon in this script but I don't have the time to remake this right now!
    You can always try to figure out the code and adjust some things yourself.
     
  24. BHS

    BHS

    Joined:
    Dec 21, 2009
    Posts:
    4,218
    Thanks for the reply.

    Yeah I've been tweaking everything and I put the tint all the way down along with the light and fog and the grass is the only thing that's still bright. If I light map everything then it won't change when the sun rises and sets it just keeps its shaded areas the same regardless of the position of the sun. Any ideas?
     
  25. BHS

    BHS

    Joined:
    Dec 21, 2009
    Posts:
    4,218
    Are you still working on this? I'm willing to help make improvements..
     
  26. Vert

    Vert

    Joined:
    Mar 23, 2010
    Posts:
    1,093
    THIS IS AMAZING! I was going to design a system like this for my game but it seems you have saved me much work! THANK YOU!
     
  27. Hogfather

    Hogfather

    Joined:
    Aug 17, 2010
    Posts:
    39
    Simply amazing...

    A million thanks to you sir.
     
  28. ikke998

    ikke998

    Joined:
    Jan 7, 2010
    Posts:
    79
    I'm still working on this, Download the new version here:

    it includes:
    - longer night and daytime
    - up to 3 skyboxes for night/morning or evening/day
    - improved sun
    - very high quality 16 bit skyboxes for very nice gradients

    I used the crysis day/night cycle as a guideline.
    You can still tweek everything yourself and customize it with tints or your own skyboxes!

    if you want to support me and to keep my packages free you can always consider donating 8)
     
  29. ikke998

    ikke998

    Joined:
    Jan 7, 2010
    Posts:
    79
    and a demo of what you can do with the v2 package
     
  30. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    The demo looks very nice, how did you do the animated clouds?
     
  31. fallingbrickwork

    fallingbrickwork

    Joined:
    Mar 16, 2009
    Posts:
    1,072
    Looks really great, nice work.
     
  32. ikke998

    ikke998

    Joined:
    Jan 7, 2010
    Posts:
    79
    I used a dome with a script that scrolls the cloudtexture.
    The shader is a transparent 2sided shader (the back is lit the same as the front) and it ignores fog.
     
  33. bigkahuna

    bigkahuna

    Joined:
    Apr 30, 2006
    Posts:
    5,435
    If I might make a suggestion, you might want to move all functionality that isn't used in the GUI system to an Update() function. OnGUI updates 2x per frame while Update only updates 1x frame. Runs smooth as greased lightning for me, but it never hurts to optimize where you can.

    Here's my edited script for TOD.js:

    Code (csharp):
    1. var slider : float;
    2. var slider2 : float;
    3. var Hour : float;
    4. private var Tod: float;
    5. var sun: Light;
    6. var NightFogColor : Color;
    7. var DuskFogColor : Color;
    8. var MorningFogColor : Color;
    9. var MiddayFogColor : Color;
    10. var NightAmbientLight : Color;
    11. var DuskAmbientLight : Color;
    12. var MorningAmbientLight : Color;
    13. var MiddayAmbientLight : Color;
    14. var NightTint : Color;
    15. var DuskTint : Color;
    16. var MorningTint : Color;
    17. var MiddayTint : Color;
    18. var SkyBoxMaterial1 : Material;
    19. var SkyBoxMaterial2 : Material;
    20. var SunNight : Color;
    21. var SunDay : Color;
    22.  
    23. function OnGUI () {
    24.     slider= GUI.HorizontalSlider( Rect(20,20,200,30), slider, 0,1.0);
    25.     Hour= slider*24;
    26.     Tod= slider2*24;
    27.     sun.transform.localEulerAngles.x= (slider*360)-90;
    28.     slider = slider +Time.deltaTime/300;
    29.     sun.color = Color.Lerp (SunNight, SunDay, slider*2);
    30.     if(slider<0.5){
    31.         slider2= slider;
    32.     }
    33.     if(slider>0.5){
    34.         slider2= (1-slider);
    35.     }
    36.     sun.intensity = (slider2-0.2)*2;   
    37. }
    38.  
    39. function Update () {   
    40.     //it is Night
    41.     if(Tod<4){
    42.         RenderSettings.skybox=SkyBoxMaterial1;
    43.         RenderSettings.skybox.SetFloat("_Blend", 0);
    44.         SkyBoxMaterial1.SetColor ("_Tint", NightTint);
    45.         RenderSettings.ambientLight = NightAmbientLight;
    46.         RenderSettings.fogColor = NightFogColor;
    47.     }
    48.     //it is Dusk
    49.     if(Tod>4&Tod<6){
    50.         RenderSettings.skybox=SkyBoxMaterial1;
    51.         RenderSettings.skybox.SetFloat("_Blend", 0);
    52.         RenderSettings.skybox.SetFloat("_Blend", (Tod/2)-2);
    53.         SkyBoxMaterial1.SetColor ("_Tint", Color.Lerp (NightTint, DuskTint, (Tod/2)-2) );
    54.         RenderSettings.ambientLight = Color.Lerp (NightAmbientLight, DuskAmbientLight, (Tod/2)-2);
    55.         RenderSettings.fogColor = Color.Lerp (NightFogColor,DuskFogColor, (Tod/2)-2);
    56.     }
    57.     //it is Morning
    58.     if(Tod>6&Tod<8){
    59.         RenderSettings.skybox=SkyBoxMaterial2;
    60.         RenderSettings.skybox.SetFloat("_Blend", 0);
    61.         RenderSettings.skybox.SetFloat("_Blend", (Tod/2)-3);
    62.         SkyBoxMaterial2.SetColor ("_Tint", Color.Lerp (DuskTint,MorningTint,  (Tod/2)-3) );
    63.         RenderSettings.ambientLight = Color.Lerp (DuskAmbientLight, MorningAmbientLight, (Tod/2)-3);
    64.         RenderSettings.fogColor = Color.Lerp (DuskFogColor,MorningFogColor, (Tod/2)-3);
    65.     }
    66.     //it is getting Midday
    67.     if(Tod>8&Tod<10){
    68.         RenderSettings.ambientLight = MiddayAmbientLight;
    69.         RenderSettings.skybox=SkyBoxMaterial2;
    70.         RenderSettings.skybox.SetFloat("_Blend", 1);
    71.         SkyBoxMaterial2.SetColor ("_Tint", Color.Lerp (MorningTint,MiddayTint,  (Tod/2)-4) );
    72.         RenderSettings.ambientLight = Color.Lerp (MorningAmbientLight, MiddayAmbientLight, (Tod/2)-4);
    73.         RenderSettings.fogColor = Color.Lerp (MorningFogColor,MiddayFogColor, (Tod/2)-4);
    74.     }
    75. }
    This could probably be optimized even more, but this will give you an idea what I mean.
     
  34. Vert

    Vert

    Joined:
    Mar 23, 2010
    Posts:
    1,093
    WOW The second demo is simply fantastic! THANK YOU SO MUCH!!!! This will save me so much time and effort! Thank you!
     
  35. ikke998

    ikke998

    Joined:
    Jan 7, 2010
    Posts:
    79
    Some pretty pictures from my Tod system
    I've got some grass working with the Day/Night cycle!
     

    Attached Files:

  36. Argien

    Argien

    Joined:
    Aug 1, 2010
    Posts:
    18
    Wow, you rock :)
    How did you manage to light the grass finally ?
     
  37. ikke998

    ikke998

    Joined:
    Jan 7, 2010
    Posts:
    79
    this topic (2nd reply) has a grass shader that works with a directional light:
    http://forum.unity3d.com/viewtopic.php?t=28053

    My grass is a detail mesh not the terrain grass.
     
  38. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    5,664
    vertexlit on grass looks ok as far as you don’t have dark [=unlit] slopes where it is placed.
    the original grass shader takes a look at the terrain lightmap to calculate an appropriate lighting whereas vertexlit only takes the angle between vertex normal and the light source into account. the result looks like shown in the image below.
    even using real time shadows doesn’t produce better lighting on small meshes which are very close to the ground for the terrain doesn’t cast very accurate shadows.
    but unity 3 will give you much appropriate real time lighting on grass. so just stay tuned i guess.

    lars
     

    Attached Files:

  39. ikke998

    ikke998

    Joined:
    Jan 7, 2010
    Posts:
    79
    You are right about the lightmapped grass looking better.
    But using the vertex lit shader is the best dynamic solution until Unity 3 comes out.
    So we can only wait for now!
     
  40. Pulov

    Pulov

    Joined:
    Feb 20, 2010
    Posts:
    824
  41. Greg-Bassett

    Greg-Bassett

    Joined:
    Jul 28, 2009
    Posts:
    618
    I see the title of this topic is Dynamic Weather/Day night Cycle, but it is all about the day night cycle, which I must say is very impressive, nice work! :D

    I did find the link to your Bad Weather demo in your first post, and its also awesome!
    http://kristof.dedene.com/Unity/DynamicWeather/DynamicWeather.html

    Are you considering making this available to download as a UnityPackage as well?

    Keep up the great work!
     
  42. msborat

    msborat

    Joined:
    Jun 17, 2010
    Posts:
    89
    How to import the package to unity after I extract the file. So it is a lot of folders
     
  43. larsbertram1

    larsbertram1

    Joined:
    Oct 7, 2008
    Posts:
    5,664
    i have just imported your script to my testing scene in unity 3 beta.
    due to the new grass and tree leaves shaders day and night cycles will look really nice:

    http://vimeo.com/14632222

    lars
     

    Attached Files:

  44. Argien

    Argien

    Joined:
    Aug 1, 2010
    Posts:
    18
    Good news, and thanks for the demo :)
    It should work with the free version too, I suppose ?
     
  45. ikke998

    ikke998

    Joined:
    Jan 7, 2010
    Posts:
    79
    Awesome that video looks very nice!
    I don't have unity pro so I have not been able to see the D/N cycle with shadows and it looks so awesome!
     
  46. loock_

    loock_

    Joined:
    Aug 4, 2010
    Posts:
    18
    @ikke998: The Lars' grass shader from here http://forum.unity3d.com/viewtopic.php?t=28053 doesn't seem to work for me.. At night, grass is still lit and visible (as detail mesh). Could you please explain step by step how to setup grass in your day/night cycle properly?

    Also, referring to your screenshots, did you use any billboarding or everything is a detail mesh?
     
  47. Sypress

    Sypress

    Joined:
    Sep 15, 2010
    Posts:
    2
    I got the package going - but it seems the presence of the "directional light" causes all of my textures to disappear which is odd. My original scene never had a sun directional light as it was just night in a forest with ambient light. Any ideas?
     
    Last edited: Oct 5, 2010
  48. xomg

    xomg

    Joined:
    Sep 27, 2010
    Posts:
    330
    Just dumped this into an empty project in Unity 3, and if the camera is set to "Clear Flags: Skybox" it corrupts the terrain and everything else in the scene. Everything renders okay if I set it to clear to a solid colour, but then I (obviously) don't see the skybox.

    Haven't looked into why yet, just thought I'd let you know!
     
  49. Wildpepper

    Wildpepper

    Joined:
    Dec 15, 2009
    Posts:
    30
    Hey there !
    loaded in Unity3 on a complete scene, and same problem here, curiously it's rendering just the FX/Glass/Stained BumpDistort shaded objects.
    oh, and of course, impressive job :)
     
  50. lycaeonia

    lycaeonia

    Joined:
    Oct 7, 2010
    Posts:
    1
    @Xomg: Hey I found the problem with the skybox. I was looking at the prefab and noticed it uses specific materials for the day/night cycles. All that is needed is skybox made and a small change in the script. unless you know a way to do a skybox masking another skybox
     
unityunity