Environment in Godot – Complete Guide

Creating immersive and lifelike virtual worlds is an essential aspect of modern game development, and mastering environmental rendering is key to achieving this goal. In Godot 4, the Environment class opens the door to a myriad of options that can transform any bare-bones scene into a dynamic and engaging world, complete with atmospheric effects and realistic lighting. Whether you’re a beginner taking first steps into game creation, or an experienced coder seeking to refine your skills, understanding the Environment class will significantly elevate the realism and aesthetic appeal of your projects.

What is the Environment Class in Godot 4?

The Environment class in Godot 4 is a powerful resource tied to environmental nodes such as WorldEnvironment, designed to define and manage multiple rendering options for your scenes. Through this class, you can craft engaging backgrounds, control ambient lighting, implement fog, depth-of-field, and more to create the desired atmosphere for your game.

The Role of Environmental Effects

Environmental effects bring life to your virtual creations by simulating conditions we experience in the real world. A well-crafted environment can evoke emotions, provide gameplay cues, and ensure a cohesive game experience. With Godot 4’s Environment class, you can achieve just that, by customizing a plethora of parameters to fit the narrative and style of your game.

Why Should I Learn About the Environment Class?

Game development is not only about functionality but also about the experience. Learning how to manipulate environmental effects through the Environment class is crucial for creating rich, interactive, and visually appealing games. It gives you the power to control the mood and atmosphere, ultimately elevating player immersion. By mastering these skills, you open up a world of creative possibilities that can set your games apart in a crowded marketplace.

CTA Small Image
FREE COURSES AT ZENVA
LEARN GAME DEVELOPMENT, PYTHON AND MORE
ACCESS FOR FREE
AVAILABLE FOR A LIMITED TIME ONLY

Setting Up Basic Environment Properties

Let’s start by setting up the most basic properties of the Environment class. We will see how to create an environment and adjust the clear color, ambiance, and sky properties to set the stage for our scene.

var env = Environment.new()
env.background_color = Color(0.1, 0.1, 0.1)
env.ambient_light_energy = 0.5
env.ambient_light_sky_contribution = 0.5
get_tree().root.add_child(env)

This snippet creates a new environment setting a dark grey clear color, a medium ambient light, and it makes the sky contribute to this ambient light. Then we add the environment to the scene tree.

Next, we’ll add a simple procedural sky to our environment. Procedural skies are great for quick prototyping and testing lighting configurations.

var sky = ProceduralSky.new()
env.background_sky = sky

The above code sets up a procedural sky resource and assigns it to our environment. The scene will now have a dynamic sky as background.

Adjusting Lighting and Shadow

Proper lighting can make or break the visual quality of your game. We’ll set up the sun (directional light) and tweak its properties for better shadows.

var sun = DirectionalLight.new()
sun.direction = Vector3(-1, -1, -1)
sun.shadow_enabled = true
sun.shadow_normal_bias = 1.1
sun.shadow_contact = 0.2
add_child(sun)

This code creates a directional light cast from a sun-like source, enabling shadows and adjusting bias and contact settings to reduce artifacts.

To fine-tune the lighting even further, we’ll modify the environment’s tonemap settings to achieve more balanced exposures.

env.tonemap_mode = Environment.TONE_MAP_MODE_ACES
env.tonemap_exposure = 1.0
env.tonemap_white = 4.5

The ACES tonemapping provides a more filmic look to the rendered scene with better handling of bright spots. The exposure is set to a neutral level while the white point is slightly elevated for contrast enhancement.

Creating Atmosphere with Fog

Fog can add depth to your scenes and create atmospheric effects. We’ll add fog to our environment and adjust its parameters accordingly.

env.fog_enabled = true
env.fog_color = Color(0.7, 0.7, 0.8)
env.fog_begin = 25.0
env.fog_end = 100.0
env.fog_height_min = 0.0
env.fog_height_max = 40.0

With the fog enabled, we set a light blue color and control how the fog begins and ends in the game world, giving us a gradient of visibility. The height parameters allow the fog to affect only a certain vertical range of the scene.

For a more dynamic effect, we can also add fog depth cueing based on the camera angle to simulate a more realistic atmospheric perspective.

env.fog_depth_enabled = true
env.fog_depth_begin = 20.0
env.fog_depth_end = 50.0
env.fog_depth_curve = 1.0

This configuration ensures that fog density increases with depth, mimicking how light scatters in the real world. We’ve just tweaked the beginning and end distances for the effect and the bias with the curve.

Enhancing Scenes with Depth of Field and Glow

Last but not least, we’ll add post-processing effects such as depth of field, which simulates camera focus, and glow, which adds a bloom effect around bright areas.

env.dof_blur_far_enabled = true
env.dof_blur_far_distance = 75.0
env.dof_blur_far_transition = 10.0
env.dof_blur_far_amount = 0.1

This snippet enables a depth of field effect for distant objects and sets up the simulation of a camera losing focus as objects move further away.

Now, let’s bring some sparkle to our lights with glow:

env.glow_enabled = true
env.glow_level_1 = true
env.glow_intensity = 0.8
env.glow_strength = 1.0
env.glow_bloom = 0.0

By activating glow, we’re allowing certain elements in our scene to pop with added brightness and a soft halo. We’re controlling the intensity and the strength, and keeping bloom to a minimum for a more subtle effect.

These basics form the building blocks of the Environment class within Godot 4. Understanding and experimenting with these examples will set you up for success as you move on to create even more complex and engaging environmental setups in your games.

Let’s delve deeper into the Environment class and explore additional properties and code examples that can further refine the ambiance and mood of our scenes in Godot 4.

Simulating Weather with Environment Settings

Weather plays a crucial role in setting the right atmosphere in games. We can simulate weather effects such as rain or snow by adjusting the environment’s particles and other visual elements. Here, we’ll focus on adding rain to our environment.

var rain_particles = CPUParticles.new()
rain_particles.emitting = true
rain_particles.amount = 1000
rain_particles.speed_scale = 80
rain_particles.gravity = Vector3(0, -98, 0)
add_child(rain_particles)

This code snippet creates a new particle system that simulates rain by emitting a large number of particles that fall downward, affected by gravity to mimic the motion of raindrops.

Utilizing Screen Space Reflections (SSR)

Screen Space Reflections add realism to reflective surfaces by simulating the way light bounces off them. This can make your environments look more dynamic and immersive. Here’s how we enable and configure SSR in Godot 4:

env.ss_reflections_enabled = true
env.ss_reflections_scale = 1.0
env.ss_reflections_depth_tolerance = 0.02
env.ss_reflections_max_steps = 64

Enabling SSR is straightforward, and the additional parameters allow us to tweak the quality of the reflections, their precision and the computational effort required to render them.

Improving Visual Clarity with Ambient Occlusion

Ambient Occlusion (AO) is a shading method used to add depth to scenes by calculating how exposed each point in a scene is to ambient lighting. Thus, it creates softer shadows in the corners and crevices. Here’s how to enable and set up Ambient Occlusion in our environment:

env.ao_enabled = true
env.ao_intensity = 1.0
env.ao_radius = 1.0
env.ao_quality = 1

By tweaking the intensity and radius parameters, you can control the strength and reach of the ambient occlusion effect. Adjusting quality can be a trade-off between performance and visual fidelity.

Enhancing Color with Color Correction

Color correction is a final touch that can greatly influence the mood and visual style of a game. Using an adjustment curve, we can remap color values to alter the appearance of our entire scene. This can be done in the Environment class as follows:

env.adjustment_enabled = true
env.adjustment_brightness = 0.8
env.adjustment_contrast = 1.2
env.adjustment_saturation = 1.1

Enabling the adjustment property and tweaking brightness, contrast, and saturation allows us to create a specific visual tone, from a bleak, post-apocalyptic landscape to a vibrant, surreal fantasy world.

Dynamic Sky with Physical Sky

If we wish to step up from Procedural Sky and introduce a more sophisticated and dynamic sky setting, Godot 4’s Physical Sky provides an advanced, physically-based sky model that includes sun, moon, and stars. Here is how you can set up a Physical Sky:

var physical_sky = PhysicalSky.new()
env.background_sky = physical_sky
env.background_sky_custom_fov = 50.0
env.background_sky_rotation_degrees = Vector3(0, 0, 0)

With the Physical Sky resource attached to our environment, we can also customize its field of view and rotation to position the celestial bodies exactly where we want them in the sky according to our scene needs.

These advanced features available in the Environment class enable you to fine-tune every visual aspect of your scene, thereby creating a compelling and authentic gaming experience. Knowing how to use these properties effectively is crucial for aspiring game developers seeking to create professional-level games with Godot 4.

As we further explore the Environment class in Godot 4, we’ll focus on integrating advanced rendering features that push the boundaries of visual fidelity in our games. These include the use of volumetric fog, auto-exposure, and reflection probes, among others.

Volumetric fog adds a three-dimensional aspect to our environments, where light scatters within the fog to give a sense of density and depth. This is perfect for creating dynamic lighting scenes such as light shafts (also known as God rays).

env.volumetric_fog_enabled = true
env.volumetric_fog_height_min = 0.0
env.volumetric_fog_height_max = 50.0
env.volumetric_fog_albedo = Color(0.8, 0.8, 0.9)
env.volumetric_fog_light_energy = 0.5

By switching on volumetric fog, we can adjust the minimum and maximum heights to control how the fog appears over various elevations. Altering the albedo influences the fog’s color, thereby increasing the drama and moodiness of lighting within the fog.

Another important aspect of game visuals is ensuring that the exposure of various scenes maintains a consistent level of visibility. Auto-exposure automatically adjusts the brightness of the scene based on the average luminance, much like a camera’s exposure setting.

env.auto_exposure_enabled = true
env.auto_exposure_speed = 0.5
env.auto_exposure_scale = 1.0
env.auto_exposure_max_luminance = 2.0
env.auto_exposure_min_luminance = 0.2
env.auto_exposure_grey = 0.4

When enabled, auto-exposure settings dictate how quickly and to what extent the scene adapts to brightness changes. The max and min luminance set the exposure limits to prevent overly bright or dark scenes.

Reflection probes are used to create realistic reflections on shiny or metallic surfaces. They capture the surroundings and project them onto reflective materials.

var probe = ReflectionProbe.new()
probe.extents = Vector3(10, 10, 10)
probe.updates_mode = ReflectionProbe.UPDATES_REALTIME
probe.intensity = 0.8
probe.max_distance = 1000
add_child(probe)

The above code instantiates a Reflection Probe with a defined size, sets it to update reflections in real-time, controls the strength of reflections with intensity, and sets how far its effects reach with max_distance.

Light baking can significantly improve performance in complex scenes by pre-calculating the lighting information. In Godot 4, you can set up light baking within the environment to ensure static objects receive consistent, pre-computed lighting.

env.gi_probe_data = GIProbeData.new()
env.gi_probe_data.bias = 0.03
env.gi_probe_data.normal_bias = 0.05
env.gi_probe_data.compress = true

Here, we create a new GI Probe Data resource, adjust bias values for accuracy, and enable compression for performance benefits.

Lastly, Sub-surface Scattering (SSS) is part of the Environment class that particularly enhances the rendering of skin, wax, marble, and other materials where light penetrates the surface, bounces around, and exits at a different point.

env.sss_scale = 0.75
env.sss_depth_scale = 0.01
env.sss_sharpness = 0.8

Adjusting the SSS parameters will change the way light interacts with the material, defining qualities such as translucency and softness of shadows within the material itself.

Combining these features gives us extensive control over how our game environments look and feel. It allows us to create scenes that can range from photorealistic to stylized, all within the same powerful engine. The creative possibilities are virtually limitless with Godot 4’s Environment class.

Where to Go Next with Your Godot Learning Journey

The world of Godot Engine is vast, and your journey in game development has just begun. To keep your skills sharpening and to delve deeper into the endless possibilities that Godot 4 presents, consider taking the next step with our Godot Game Development Mini-Degree. Through this comprehensive collection of courses, you will build cross-platform games and explore different genres, deepening your understanding of 2D and 3D game development concepts.

Whether you are a complete novice or have some experience under your belt, our Mini-Degree is designed to be accessible, flexible, and tailored to your learning pace. You can access the courses on any device, making it convenient to learn anytime, anywhere. By completing the curriculum, not only will you gain practical, hands-on experience with real Godot projects, but you’ll also cultivate the technical prowess needed for a successful career in game development.

If you’re eager to explore an even broader horizon, our full suite of Godot courses caters to a variety of interests and levels. Dive into the resources we offer to continue forging your path as a game developer. Remember, with Zenva, you can take your passion from hobbyist to professional. We’re thrilled to be a part of your learning adventure!

Conclusion

In the expansive universe of game development, mastering tools like the Environment class in Godot 4 can significantly enhance the visual narrative of your games. The skills you acquire through our Godot Game Development Mini-Degree will not only sharpen your technical abilities but also empower you to weave captivating digital tales. At Zenva, our mission is to provide you with the knowledge and resources to transform your creative visions into interactive realities. So why wait? Embark on this exciting journey with us, and let’s turn the virtual worlds of your imagination into the games of tomorrow!

Remember, every great game begins with the first line of code. By choosing to learn and grow with the Godot Engine and our carefully curated courses, you are taking the crucial steps towards becoming a proficient game developer. Your commitment to learning holds the key to unlocking new realms of innovation and entertainment. Together, let’s push the boundaries of game development and create experiences that enchant, challenge, and inspire gamers around the globe.

FREE COURSES
Python Blog Image

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