Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

screen-space-reflections

Package Overview
Dependencies
Maintainers
1
Versions
22
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

screen-space-reflections

Screen Space Reflections implementation in three.js

  • 2.1.2
  • npm
  • Socket score

Version published
Weekly downloads
8.3K
increased by2.6%
Maintainers
1
Weekly downloads
 
Created
Source

three.js Screen Space Reflections

Implements performant Screen Space Reflections in three.js.





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.

Basic usage:

Install the package first:

npm i screen-space-reflections

Then add it to your code like so:

import { SSREffect } from "screen-space-reflections"

const composer = new POSTPROCESSING.EffectComposer(renderer)

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

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

composer.addPass(ssrPass)

Options

Default values of the optional "options" parameter
const options = {
	temporalResolve: true,
	temporalResolveMix: 0.9,
	temporalResolveCorrection: 1,
	resolutionScale: 1,
	velocityResolutionScale: 1,
	width: typeof window !== "undefined" ? window.innerWidth : 1000,
	height: typeof window !== "undefined" ? window.innerHeight : 1000,
	blurMix: 0.5,
	blurSharpness: 10,
	blurKernelSize: 1,
	rayDistance: 10,
	intensity: 1,
	colorExponent: 1,
	maxRoughness: 1,
	jitter: 0,
	jitterSpread: 0,
	jitterRough: 0,
	roughnessFadeOut: 1,
	rayFadeOut: 0,
	MAX_STEPS: 20,
	NUM_BINARY_SEARCH_STEPS: 5,
	maxDepthDifference: 10,
	thickness: 10,
	ior: 1.45,
	CLAMP_RADIUS: 1,
	ALLOW_MISSED_RAYS: true,
	USE_MRT: true,
	USE_NORMALMAP: true,
	USE_ROUGHNESSMAP: true
}
Description of the properties
NameTypeDescription
temporalResolvebooleanwhether you want to use Temporal Resolving to re-use reflections from the last frames; this will reduce noise tremendously but may result in "smearing"
resolutionScaleNumberresolution of the SSR effect, a resolution of 0.5 means the effect will be rendered at half resolution
velocityResolutionScaleNumberresolution of the velocity buffer, a resolution of 0.5 means velocity will be rendered at half resolution
widthNumberwidth of the SSREffect
heightNumberheight of the SSREffect
blurMixNumberhow much the blurred reflections should be mixed with the raw reflections
blurSharpnessNumberexponent of the Box Blur filter; higher values will result in more sharpness
blurKernelSizeNumberkernel size of the Box Blur Filter; higher kernel sizes will result in blurrier reflections with more artifacts
rayDistanceNumbermaximum distance a reflection ray can travel to find what it reflects
intensityNumberintensity of the reflections
colorExponentNumberexponent 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
maxRoughnessNumbermaximum roughness a texel can have to have reflections calculated for it
jitterNumberhow intense jittering should be
jitterSpreadNumberhow much the jittered rays should be spread; higher values will give a rougher look regarding the reflections but are more expensive to compute with
jitterRoughNumberhow intense jittering should be in relation to a material's roughness
roughnessFadeOutNumberhow intense reflections should be on rough spots; a higher value will make reflections fade out quicker on rough spots
rayFadeOutNumberhow much reflections will fade out by distance
MAX_STEPSNumbernumber of steps a reflection ray can maximally do to find an object it intersected (and thus reflects)
NUM_BINARY_SEARCH_STEPSNumberonce 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
maxDepthDifferenceNumbermaximum 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
thicknessNumbermaximum 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
iorNumberIndex 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
CLAMP_RADIUSbooleanhow 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
ALLOW_MISSED_RAYSbooleanif 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
USE_MRTbooleanWebGL2 only - whether to use multiple render targets when rendering the G-buffers (normals, depth and roughness); using them can improve performance as they will render all information to multiple buffers for each fragment in one run; this setting can't be changed during run-time
USE_NORMALMAPbooleanif roughness maps should be taken account of when calculating reflections
USE_ROUGHNESSMAPbooleanif normal maps should be taken account of when calculating reflections

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 rayDistance 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:

  • First install the npm package of the module used for the GUI:
npm i tweakpane
  • then just copy the SSRDebugGUI.js to your project and initialize it like so in your scene:
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

  • Temporal Reprojection to re-use the last frame and thus reduce noise
  • Jittering and blurring reflections to approximate rough reflections
  • Using three.js' WebGLMultipleRenderTarget (WebGL2 only) to improve performance when rendering scene normals, depth and roughness
  • Early out cases to compute only possible reflections and boost performance
  • Box Blur to reduce noise

What's new in v2

  • Introduced Temporal Reprojection to reduce noise for the reflections when moving the camera by reprojecting the last frame's reflections into the current one
  • Implemented accumulative sampling by saving and re-using the last frame's reflections to accumulate especially jittered reflections over frames
  • Made all SSR-related options (e.g. thickness, ior, rayDistance,...) reactive so that you now just need to set ssrEffect.rayDistance = value for example to update values
  • Fixed jittering so that it's actually correct from all angles (it used to be less intense the higher you were looking down at a reflection)
  • Changed the SSR implementation from a pass to an effect to improve performance
  • Optimizations regarding computation of required buffers and reflections

Tips

Expand to view tips

Getting rid of artifacts

If you are getting artifacts, for example:


Then try the following:

  • increase thickness
  • increase maxDepthDifference
  • decrease rayDistance and increase MAX_STEPS if reflections are cutting off now
  • increase NUM_BINARY_SEARCH_STEPS

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

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.
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)

Todos

  • Proper upsampling to still get quality reflections when using half-res buffers

Credits

Resources

Screen Space Reflections in general

Temporal Reprojection

Keywords

FAQs

Package last updated on 07 Aug 2022

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc