Rendering a scene with soft shadows using Percentage-Closer Soft Shadows (PCSS).
Overview
This tutorial upgrades the hard shadow mapping of Tutorial_502_HardShadow to soft shadows. The program structure is identical — subclass Viewer and install RenderSettings in init() — only the shadow softness changes from 0 to a positive value. The entry point loads the same indoor scene (room.obj) with a neutral gray material; all shadow logic lives in the TutorialSoftShadow class (shown after this file).
The technique
Real light sources have a finite area, so shadow boundaries are not sharp: near a shadow edge there is a penumbra where the receiver sees only part of the light. Percentage-Closer Soft Shadows (Randima Fernando, GPU Gems, 2005) approximates this on top of a standard shadow map in three steps per pixel: (1) blocker search — sample a neighborhood of the shadow map to estimate the average depth of the occluders between the light and the receiver; (2) penumbra estimation — from similar triangles, the farther the receiver is from the blocker, the wider the penumbra, which sets the filter kernel size; (3) Percentage- Closer Filtering (PCF) — average many binary depth comparisons over that kernel to get a fractional visibility. The result is shadows that are crisp where an object touches the floor and grow softer with distance ("contact hardening"), which is a strong realism cue. Easy3D exposes a softness parameter (scales the simulated light size) in RenderSettings and chooses an appropriate sample pattern for the active rendering device. See the renderer's shadow pass for the implementation and links to the original NVIDIA paper.
Easy3D API
- Viewer: the interactive viewer; subclass it, install RenderSettings in init(), and override key_press_event() to control the effect by replacing those settings.
- RenderSettings::shadow: the declarative shadow settings. A positive softness selects the PCSS path; virtual_background/virtual_background_color enable and color the virtual plane.
- DrawableTriangles: the surface drawable; set_uniform_coloring() assigns a constant material color and set_smooth_shading(true) interpolates normals, so the penumbra gradient is easy to read.
Running it
Press 'space' to toggle shadowing on/off. Look at where objects meet the floor: the shadow is sharp at contact and softens with distance — compare with the uniformly hard edges of Tutorial_502_HardShadow. Standard Viewer navigation (mouse drag, wheel) applies.
28#include <easy3d/core/model.h>
29#include <easy3d/renderer/drawable_triangles.h>
30#include <easy3d/renderer/renderer.h>
31#include <easy3d/util/resource.h>
32#include <easy3d/util/initializer.h>
90int main(
int argc,
char **argv) {
96 const std::string file = resource::directory() +
"/data/room.obj";
99 TutorialSoftShadow viewer(EXAMPLE_TITLE);
103 auto model = viewer.add_model(file,
true);
105 std::cerr <<
"failed to load model. Please make sure the file exists and format is correct" << std::endl;
112 auto drawable = model->renderer()->get_triangles_drawable(
"faces");
113 drawable->set_uniform_coloring(
vec4(0.9f, 0.9f, 0.9f, 1.0f));
114 drawable->set_smooth_shading(
true);
Definition collider.cpp:182
The header file of the viewer class:
27#ifndef EASY3D_TUTORIAL_SOFT_SHADOW_H
28#define EASY3D_TUTORIAL_SOFT_SHADOW_H
30#include <easy3d/viewer/viewer.h>
45 explicit TutorialSoftShadow(
const std::string &title =
"");
51 bool key_press_event(
int key,
int modifiers)
override;
The default viewer implementation for both OpenGL and Vulkan backends.
Definition viewer.h:143
The source file of the viewer class:
28#include <easy3d/core/surface_mesh.h>
29#include <easy3d/renderer/camera.h>
30#include <easy3d/renderer/render_settings.h>
38TutorialSoftShadow::TutorialSoftShadow(
const std::string &title) :
Viewer(title) {
40 camera()->set_up_vector(
vec3(0, 1, 0));
41 camera()->set_view_direction(
vec3(0, 0, 1));
44 hint_ =
"Press 'space' to switch on/off shadowing";
47void TutorialSoftShadow::init() {
53 settings.shadow.enabled =
true;
54 settings.shadow.softness = 0.5f;
57 settings.shadow.virtual_background =
true;
58 settings.shadow.virtual_background_color = background_color();
59 set_render_settings(settings);
64bool TutorialSoftShadow::key_press_event(
int key,
int modifiers) {
65 if (key == KEY_SPACE) {
68 settings.shadow.enabled = !settings.shadow.enabled;
69 set_render_settings(settings);
73 return Viewer::key_press_event(key, modifiers);
Declarative effect settings shared by all rendering hosts.
Definition render_settings.h:80
- See also
- RenderSettings, Viewer, DrawableTriangles, Tutorial_502_HardShadow