Quantcast
Channel: GameDev Academy
Viewing all articles
Browse latest Browse all 1620

Sky in Godot – Complete Guide

$
0
0

When it comes to creating visually stunning games, the sky is literally the limit! And if you’re diving into game development using Godot 4, you’ll find that the Sky class is pivotal for setting the perfect scene. From the soft glow of a morning sunrise to the spectacular colors of a setting sun, a sky is not just a backdrop but a vital element that adds depth, mood, and immersion to your game environments.

Our journey through the Sky class will explore how you can leverage its features to elevate the aesthetic appeal of your games. Understanding and mastering this component of Godot 4 will arm you with the skills to create dynamic and engaging skies that react to gameplay and contribute to your game’s overall feel.

What is the Sky Class?

The Sky class in Godot 4 is a powerful and flexible Resource designed to render skies using a variety of Materials. It forms the canvas for celestial bodies, atmospheric effects, and can influence the lighting of your entire scene through its reflection and radiance capabilities.

What is it Used For?

Beyond its visual contribution, the Sky class dynamically updates reflection/radiance cubemaps. This is crucial, as the cubemaps are instrumental in determining how light interacts with the surfaces in your game, affecting everything from the glow on a character’s armor to the shimmer of a distant ocean.

Why Should I Learn to Use the Sky Class?

Creating a compelling game environment is about more than just gameplay mechanics; it’s about crafting an experience. By learning to use the Sky class, you can:

– Develop custom skies that fit the narrative and aesthetics of your game.
– Control in-game lighting naturally, making transitions from day to night seamless.
– Enhance the visual quality of your scenes with reflections that are pivotal for realistic environmental effects.

Stay tuned as we delve into the world of Godot 4’s Sky class and discover how to make your game worlds come alive with awe-inspiring skies.

CTA Small Image

FREE COURSES AT ZENVA

LEARN GAME DEVELOPMENT, PYTHON AND MORE

AVAILABLE FOR A LIMITED TIME ONLY

Creating a Basic Sky

To get started with using the Sky class in Godot 4, let’s create a simple daytime sky for our scene. First, we’ll need to create a new Sky resource.

var sky = Sky.new()

Next, we will assign this Sky resource to the environment of our scene to make it visible.

var environment = Environment.new()
environment.background_mode = Environment.BG_MODE_SKY
environment.sky = sky
$Camera.environment = environment

With these lines, we’ve told our scene to use the new Sky resource as the background and attached it to the camera’s environment.

Customizing the Sky Color

Now, we want our sky to have a color gradient that resembles a clear day. You must set the top color and the horizon color to achieve this.

var sky_material = ProceduralSkyMaterial.new()
sky_material.sky_top_color = Color(0.4, 0.6, 0.9)
sky_material.sky_horizon_color = Color(0.0, 0.3, 0.7)
sky.set_sky_material(sky_material)

The above code creates a `ProceduralSkyMaterial` and sets the colors for the top and horizon. Then it applies this material to our Sky resource.

Adding a Sun

The sky often isn’t complete without a sun. We’ll use the `ProceduralSkyMaterial` to add a sun to our sky.

sky_material.sun_enabled = true
sky_material.sun_color = Color(1.0, 0.9, 0.6)
sky_material.sun_size = 0.02

The code snippet above enables the sun in the material and sets the sun’s color and size. A smaller `sun_size` value will make the sun appear bigger in the skybox.

Transitioning from Day to Night

Another key aspect of the Sky class is the ability to transition from day to night. This involves interpolating the colors over time. Let’s create a simple function to transition from day to night.

func update_sky(time_of_day : float):
    var color_day = Color(0.4, 0.6, 0.9)
    var color_night = Color(0.1, 0.1, 0.3)
    var color_current = color_day.linear_interpolate(color_night, time_of_day)
    sky_material.sky_top_color = color_current
    sky_material.sky_horizon_color = color_current

This `update_sky` function will interpolate between `color_day` and `color_night` based on the `time_of_day` parameter you pass. Call this function with a value between 0.0 (day) and 1.0 (night) to transition the sky colors.

Reflecting the Sky Changes in Game

Once you’ve made changes to your Sky or its material, you’ll want these updates to reflect in-game. Ensure that the changes take effect with the following line.

sky.set_sky_material(sky_material)

This command ensures that the sky in your game scene updates in real-time when you modify its material properties.

By following the above examples, you now have the foundation to start manipulating the Sky class in Godot 4 and can begin experimenting with more complex sky dynamics and visuals. Remember to continuously test your changes to see how they behave in the context of your game. Each adjustment will get you one step closer to crafting the perfect atmospheric effect for your project.Understanding and utilizing the full potential of the Sky class is akin to having a deep conversation with the environment of your game. Each subtle change can convey a different mood, time of day, or even foreshadow events in your game’s narrative. Below, we’ll explore advanced customization options, like working with skyboxes and adjusting the ambient light to match your sky.

Working with Skyboxes

In some cases, you may want to use a custom skybox for a more stylized look. For instance, if you have a panoramic image that you want to use as your background, you can set it up as follows.

var skybox_material = PanoramaSkyMaterial.new()
skybox_material.panorama = preload("res://path_to_your_image.jpg")
sky.set_sky_material(skybox_material)

With a `PanoramaSkyMaterial`, you can easily set a panoramic image (which needs to be an HDR or sRGB image) as your environment’s background.

Adjusting Ambient Light

The ambient light in your scene should be consistent with the light from your sky. You can alter ambient light properties like color and energy to reflect the current time of day in your sky.

environment.ambient_light_color = Color(1, 0.9, 0.8, 1)
environment.ambient_light_energy = 0.6
$Camera.environment = environment

Here, we set the color and energy of ambient light. The color is generally a soft shade close to white, and the energy determines the intensity of the ambient light.

Animating Clouds

For dynamic skies, you may want to animate the clouds. This can be done by modifying the UVs of the clouds over time in the ProceduralSkyMaterial:

func _process(delta):
   var cloud_uv = sky_material.cloud_uv_offset
   cloud_uv += Vector2(delta * 0.01, 0) # scrolls clouds horizontally
   sky_material.cloud_uv_offset = cloud_uv

The above function will move the cloud UVs slowly across the sky, creating the effect of drifting clouds.

Using PhysicalSkyMaterial for Realism

Godot 4 also offers a `PhysicalSkyMaterial`, which can be used for photorealistic skies and atmospheric effects.

var physical_sky_mat = PhysicalSkyMaterial.new()
sky.set_sky_material(physical_sky_mat)

The PhysicalSkyMaterial comes with a range of properties that simulate the scattering of light in the atmosphere, allowing for realistic sunrise, sunset, and deep blue skies.

Simulating Weather Changes

To simulate weather changes, you can adjust properties like cloudiness, color variation, and more. For example, to create an overcast sky, you may increase the cloudiness and adjust the colors to be grayer.

func set_overcast(weather_intensity: float):
    sky_material.cloudiness = weather_intensity
    sky_material.sky_top_color = Color(0.5, 0.5, 0.5).linear_interpolate(Color(0.2, 0.2, 0.2), weather_intensity)
    sky_material.sky_horizon_color = sky_material.sky_top_color

By calling this `set_overcast` function with a value between 0.0 and 1.0, you dynamically alter the cloudiness and sky color.

Reflecting Weather and Time Changes on Environmental Lighting

With changing weather and time of day, the environmental lighting should also change. Here’s how you can ensure that the updates on the Sky and its material are reflected in the environmental lighting.

environment.sky = sky
environment.sky_custom_fov = 50
environment.sky_orientation = Vector3(0, 0, 0)
# Adjust environmental lighting based on time of day or weather
environment.ambient_light_color = your_ambient_light_color
environment.ambient_light_energy = your_ambient_light_energy
$Camera.environment = environment

This set of commands will set the Sky resource in the environment and update ambient light properties to match the sky’s appearance.

By integrating these examples into your Godot 4 projects, you’ll start to see how dynamic and responsive your game environments can become. Whether you aim for photorealism or a more artistic representation, understanding the ins and outs of the Sky class will be a game-changer in creating immersive and dynamic scenes.As we delve deeper into Godot’s Sky class capabilities, we’re going to explore advanced environmental effects, creating a more immersive and dynamic game experience. Let’s start by fine-tuning the sun’s position and dealing with moon and star settings for nighttime scenes.

Sun Position and Animation

To accurately position the sun, Godot’s Sky allows you to specify its azimuth and elevation:

sky_material.sun_azimuth = 0.5
sky_material.sun_elevation = 0.3

With the above properties, you can move the sun across the sky, either for setting your scene or for real-time day-night cycles. For animation, you would increment these values through a function that runs periodically within the game loop.

func _process(delta):
    sky_material.sun_azimuth += delta * sun_speed
    sky_material.sun_elevation += delta * sun_elevation_change_rate
    sky.set_sky_material(sky_material)

The `sun_speed` and `sun_elevation_change_rate` determine the pace at which the sun moves through the sky and changes its height above the horizon.

Moon and Stars at Night

To enhance your night scenes, you can also add a moon and stars using the sky materials:

sky_material.moon_enabled = true
sky_material.moon_texture = preload("res://path_to_your_moon_texture.png")
sky_material.star_density = 0.5

The `moon_enabled` boolean toggles the moon on and off, `moon_texture` allows you to set a custom texture for the moon, and `star_density` controls how many stars will appear in the night sky.

Creating a Weather System

Another way to make your environment dynamic is to integrate a weather system using sky parameters. You can set up your weather variables such as `rain_intensity`, `fog_thickness`, etc., and based on those, adjust the sky:

func update_weather_system():
    if rain_intensity > 0.5:
        sky_material.cloud_height = 1 - rain_intensity
        sky_material.cloud_opacity = rain_intensity 
    else:
        # Update fog settings
        environment.fog_enabled = true
        environment.fog_height_min = 0
        environment.fog_height_max = 100
        environment.fog_density = fog_thickness

You can call `update_weather_system` at regular intervals or at momentous instances within your game logic to smoothly transition between different weather conditions.

Adjusting the Color of the Sky Throughout the Day

For a more dynamic sky, you can simulate how the color of the clear sky changes from dawn to dusk:

func update_sky_color(hour):
    var color_morning = Color(0.8, 0.7, 0.5)
    var color_midday = Color(0.4, 0.6, 0.9)
    var color_evening = Color(0.9, 0.5, 0.3)

    var color_current = color_morning
    if hour > 12 and hour = 18:
        color_current = color_evening

    sky_material.sky_top_color = color_current
    sky_material.sky_horizon_color = color_current.darkened(0.3)

By using this `update_sky_color` function, you can transition the sky from morning to evening colors, by calling it with the current hour of your game world’s virtual day.

Managing Performance

Finally, optimizing your game for performance is vital, especially when dealing with dynamic environmental effects:

func adjust_sky_quality(is_high_quality):
    if is_high_quality:
        sky_material.radiance_size = SkyMaterial.RADIANCE_SIZE_1024
        sky_material.subdiv = SkyMaterial.SUBDIV_128
    else:
        sky_material.radiance_size = SkyMaterial.RADIANCE_SIZE_32
        sky_material.subdiv = SkyMaterial.SUBDIV_4
    sky.set_sky_material(sky_material)

With `adjust_sky_quality`, you can switch between high and low quality for sky rendering, which can be especially useful if you are targeting a wide range of hardware.

Remember that the Sky class is one of many tools in your Godot 4 arsenal. By mastering it, you can curate the atmospheric ambiance that best suits the narrative, design, and performance needs of your game. Experiment with these properties and functions to find the perfect balance that brings your virtual world to life.

Continue Your Godot Mastery

Your journey through the Godot 4 Sky class is just touching the horizon of what you can achieve with this versatile game engine. To keep expanding your knowledge and skills, we invite you to explore our Godot Game Development Mini-Degree. This comprehensive series of courses will provide you with a robust understanding of various aspects of game development using Godot. Whether you’re crafting riveting 2D platformers or engaging 3D adventures, the Mini-Degree is designed to take your skills from beginner to professional.

Dive deeper into a well-rounded curriculum that offers project-based learning with a flexible online format catering to both new and more experienced developers. If Godot has sparked your interest in game development, Zenva is ready to support your growth. Our 250+ courses are geared towards helping you learn coding, create games, and even earn certificates.

For those who wish to explore a broader range of topics, we also have a full collection of Godot courses addressing different areas within the engine. These selections aim to complement your learning experience, ensuring that you have the tools and knowledge to bring your game ideas to life. Join us at Zenva to continue your educational journey where the sky is not the limit, but just the beginning.

Conclusion

Embarking on the adventure of game development is a continuous process of learning and growth, and mastering elements like the Sky class is a testament to the evolving nature of your skills. At Zenva, we are committed to helping you reach the zenith of your creative potential. Through our Godot Game Development Mini-Degree, we ensure that you’re not just learning to code, but also transforming those codes into captivating experiences.

Whether the skies you create in Godot 4 reflect the calm of daybreak, the mystery of twilight, or the unpredictable dance of ever-changing weather, remember that with each line of code, you’re painting worlds of endless possibilities. Let Zenva be your guide in this creative endeavor. Join us to start crafting your dream games today, for in the realm of game development, you are the creator of universes.

FREE COURSES

Python Blog Image

FINAL DAYS: Unlock coding courses in Unity, Unreal, Python, Godot and more.


Viewing all articles
Browse latest Browse all 1620

Trending Articles