Home

Awesome

⚠️ NOTE: This repository was deprecated as all future development of it happens in the realism-effects repository. This version does not work anymore with the latest three.js version. If you'd like to use the advanced SSR implementation, follow the readme of the new SSR repository.

<br> <br> <br>

three.js Screen Space Reflections

Implements performant Screen Space Reflections in three.js. <br></br> <img src="https://raw.githubusercontent.com/0beqz/screen-space-reflections/screenshots/1.jpg"> <br></br> <img src="https://raw.githubusercontent.com/0beqz/screen-space-reflections/screenshots//2.jpg"> <br></br>

Demos

react-three-fiber demos:

Usage

If you are using react-three-fiber, you can also use the SSR component from react-postprocessing. Check out the react-three-fiber demos to see how it's used there. <br>

Basic usage:

This effect uses postprocessing.js. If you don't have it installed, install it like so:

npm i postprocessing

Then install this effect by running:

npm i screen-space-reflections

Then add it to your code like so:

import { SSREffect } from "screen-space-reflections"
import * as POSTPROCESSING from "postprocessing"

const composer = new POSTPROCESSING.EffectComposer(renderer)

const ssrEffect = new SSREffect(scene, camera, options?)

const ssrPass = new POSTPROCESSING.EffectPass(camera, ssrEffect)

composer.addPass(ssrPass)

Options

<details> <summary>Default values of the optional "options" parameter</summary>
const options = {
	intensity: 1,
	exponent: 1,
	distance: 10,
	fade: 0,
	roughnessFade: 1,
	thickness: 10,
	ior: 1.45,
	maxRoughness: 1,
	maxDepthDifference: 10,
	blend: 0.9,
	correction: 1,
	correctionRadius: 1,
	blur: 0.5,
	blurKernel: 1,
	blurSharpness: 10,
	jitter: 0,
	jitterRoughness: 0,
	steps: 20,
	refineSteps: 5,
	missedRays: true,
	useNormalMap: true,
	useRoughnessMap: true,
	resolutionScale: 1,
	velocityResolutionScale: 1
}
</details> <details> <summary>Description of the properties</summary>
NameTypeDescription
intensity<code>Number</code>intensity of the reflections
exponent<code>Number</code>exponent by which reflections will be potentiated when composing the current frame's reflections and the accumulated reflections into a final reflection; higher values will make reflections clearer by highlighting darker spots less
distance<code>Number</code>maximum distance a reflection ray can travel to find what it reflects
fade<code>Number</code>how much reflections will fade out by distance
roughnessFade<code>Number</code>how intense reflections should be on rough spots; a higher value will make reflections fade out quicker on rough spots
thickness<code>Number</code>maximum depth difference between a ray and the particular depth at its screen position before refining with binary search; higher values will result in better performance
ior<code>Number</code>Index of Refraction, used for calculating fresnel; reflections tend to be more intense the steeper the angle between them and the viewer is, the ior parameter sets how much the intensity varies
maxRoughness<code>Number</code>maximum roughness a texel can have to have reflections calculated for it
maxDepthDifference<code>Number</code>maximum depth difference between a ray and the particular depth at its screen position after refining with binary search; higher values will result in better performance
blend<code>Number</code>a value between 0 and 1 to set how much the last frame's reflections should be blended in; higher values will result in less noisy reflections when moving the camera but a more smeary look
correction<code>boolean</code>how much pixels should be corrected when doing temporal resolving; higher values will result in less smearing but more noise
correctionRadius<code>boolean</code>how many surrounding pixels will be used for neighborhood clamping; a higher value can reduce noise when moving the camera but will result in less performance
blur<code>Number</code>how much the blurred reflections should be mixed with the raw reflections
blurKernel<code>Number</code>kernel size of the Box Blur Filter; higher kernel sizes will result in blurrier reflections with more artifacts
blurSharpness<code>Number</code>exponent of the Box Blur filter; higher values will result in more sharpness
jitter<code>Number</code>how intense jittering should be
jitterRoughness<code>Number</code>how intense jittering should be in relation to a material's roughness
steps<code>Number</code>number of steps a reflection ray can maximally do to find an object it intersected (and thus reflects)
refineSteps<code>Number</code>once we had our ray intersect something, we need to find the exact point in space it intersected and thus it reflects; this can be done through binary search with the given number of maximum steps
missedRays<code>boolean</code>if there should still be reflections for rays for which a reflecting point couldn't be found; enabling this will result in stretched looking reflections which can look good or bad depending on the angle
useNormalMap<code>boolean</code>if roughness maps should be taken account of when calculating reflections
useRoughnessMap<code>boolean</code>if normal maps should be taken account of when calculating reflections
resolutionScale<code>Number</code>resolution of the SSR effect, a resolution of 0.5 means the effect will be rendered at half resolution
velocityResolutionScale<code>Number</code>resolution of the velocity buffer, a resolution of 0.5 means velocity will be rendered at half resolution
</details>

❗ Highly recommended: Use a GUI to tweak the options

Since the right options for an SSR effect depend a lot on the scene, it can happen that you don't seem to have an effect at all in your scene when you use the SSR effect for the first time in it without any configuration. This can have multiple causes such as `` being way too low for your scene for example. So to find out which SSR options are right for your scene, you should use a GUI to find the right values easily. The example already comes with a simple one-file GUI SSRDebugGUI.js that you can use in your project like so:

npm i tweakpane
import { SSRDebugGUI } from "./SSRDebugGUI"

const gui = new SSRDebugGUI(ssrEffect, options)

That's it, you should now have the GUI you can see in the example scene. The options parameter is optional for the SSRDebugGUI and will default to the default options if no options parameter is given.

Run Locally

If you'd like to test this project and run it locally, run these commands:

git clone https://github.com/0beqz/screen-space-reflections
cd screen-space-reflections/example
npm i --force
npm run dev

Features

Tips

<details> <summary>Expand to view tips</summary>

Getting rid of artifacts

If you are getting artifacts, for example:

<br> <img src="https://raw.githubusercontent.com/0beqz/screen-space-reflections/screenshots//artifacts.jpg" width="50%">

Then try the following:

Keep in mind that increasing these values will have an impact on performance. <br>

Hiding missing reflections

Since SSR only works with screen-space information, there'll be artifacts when there's no scene information for a reflection ray. This usually happens when another objects occludes a reflecting object behind it. <br> To make missing reflections less apparent, use an env-map that can then be used as a fallback when there is no reflection. Ideally use a box-projected env-map.

Here are two implementations for three.js and react-three-fiber:

Getting updated reflections for animated materials

By default, the SSR effect won't really update reflections if the camera is not moving and no mesh in the view is moving. However, it will check if a mesh's material's map is a VideoTexture and will keep its reflections updated each frame. If your material is not using a VideoTexture but is still animated (e.g. it's a custom animated shader material), then you can get updated reflections for it by setting mesh.material.userData.needsUpdatedReflections = true. This will make the SSR effect recalculate its reflections each frame.

Server Side Rendering and window being undefined

If you are using Server Side Rendering and don't have access to the window object then the SSR effect won't be able to set the correct width and height for its passes. So once you have access to the window object, set the correct width and height of the SSR effect using:

ssrEffect.setSize(window.innerWidth, window.innerHeight)
</details>

Sponsoring

If the project is useful for you and you'd like to sponsor my work:

"Buy Me A Coffee"

Credits

Resources

Screen Space Reflections in general

Temporal Reprojection