Broken Mirror URP

Planar reflections for URP

Techniques / Implementation

It use a Render Texture. So the scene need to be rendered twice, performance depends on the complexity of the scene, render texture size and camera configuration (mostly post-rendering).

Near clip of the reflection camera is adjusted to the plane position (oblique projection) so anything behind the plane could be rendered.

Culling mask of the camera can be used if you need to remove something from the reflection.

Orthogonal and Perspective camera are supported.

URP support.

HDRP support, but It’s not our purpose to replace unity built’n.

Auto mode, so you don’t need to worry about anything (just 3 things in the inspector) or manually, so you can tweak performance as much as you need.

Shaders / Effects / Example Scenes

Mirror Reflection

Just reflection, the most basic example.

Scene: ./BrokenMirror/Scenes/1 - Mirror Reflection.unity

Shader: Reflection

Broken glass, Embossed Glass: Mirror with normal distortion

With this shader you can distord the reflection.

Also notice the use of two different post-processing volumes.

Scene: ./BrokenMirror/Scenes/2 - Broken Mirror Reflection.unity

Shader: ReflectionWithDistortion / TransparentReflectionWithDistortion

Decal Reflection

Blend the reflection with a texture by a factor

Scene: ./BrokenMirror/Scenes/3 - Decal Reflection.unity

Shader: ReflectionWithTexture

Reflection Mask

Texture RGB goes directly to Albedo, alpha is used to blend the reflection texture and include normal map for distortion that only affect the reflection.

Scene: ./BrokenMirror/Scenes/4 - Window Reflection.unity

Shader MaskedReflectionWithDistortion

5 - Glass Reflection

Translucent glass.

Scene: ./BrokenMirror/Scenes/5 - Glass Reflection.unity

Shader: TransparentReflection

Glass Reflection 2

Translucent glass with texture blend by a factor.

So you wil see three images, reflection, given texture, and the object behind, a bit messy :)

Scene: ./BrokenMirror/Scenes/6 - Glass Reflection 2.unity

Shader: TransparentReflection

Rain shader

Beatiful rain shader.

it’s a migration this tutorial to unity:

Scene: ./BrokenMirror/Scenes/7 - Rain shader.unity

Shader: Rain

Manual

Example to setup everything manually, so you can tweak everything!

In this scene, render texture is created prior execution and also the camera.

It run faster than Mirror Scene because the lower render texture resolution and disabled post-processing.

Scene: ./BrokenMirror/Scenes/8 - Manual.unity

Override post processing

Example of how to override post processing in created camera.

This time, the main camera don’t have post processing while reflection does.

Scene: ./BrokenMirror/Scenes/9 - Override post processing.unity

Exclude From Reflection

Example of how to exclude staff from reflection.

Scene: ./BrokenMirror/Scenes/10 - Exclude From Reflection.unity

Subgraphs (./BrokenMirror/Shaders/Subgraphs)

There are, maybe, endless combinations you can achieve. We include some subgraphs to easy your task.

Reflection

Just the reflection part.

ReflectionWithDistortion

Reflection plus distortion using a normal map as input

ReflectionWithDistortionN

Reflection plus distortion using a normal map vector.

You need to calculate normal map in the shader.

Shaders (./BrokenMirror/Subgraphs)

Reflection

Reflection, Opaque.

TransparentReflection

Reflection with alpha.

ReflectionWithTexture

Blend an Albedo texture and the reflection by a factor.

TransparentReflection

Blend an Albedo texture and the reflection by a factor with alpha.

ReflectionWithDistortion

Reflection with distortion using a normal map, Opaque

TransparentReflectionWithDistortion

Reflection with distortion using a normal map with alpha.

Planar Reflection Manager (MonoBehaviour)

This is the main componet of the asset.

You just need to configure the first two things: Main Camera and Reflection plane, we take care of the rest.

Or could have finer grain configuring Reflection Camera and Render Texture

Main Camera

Select here your main camera, the one that see the mirror.

Reflection plane

Game object with a Renderer that represents the Reflection Plane.

Transform is used to know the orientation of the plane/mirror/glass.

Reflection Camera

Automatic (None)

We can create a camera based on your main camera.

We will asssign render texture to the one given or an temporary one.

Manual

You choose all the camera options except, render texture.

Give you all the fine grain for performance. For example you can disable post-processing.

Options

Culling mask (Automatic only)

Use it to exclude objects from reflection.

render texture

Automatic (None)

We can use your render texture or create a temporary one base on the current resolucion and color setting of the camera: HDR / Normal.

We will assign it to all materials in the Reflection Plane.

Manual

You create the render texture with the options you desire.

We wont assign it to anything this time, you have to do it yourself.

Options

Scale

Render texture Scale based on the current screen resolution.

Half should be OK, less will need some distortion.

Shader property

Name of the shader property for Texture2D reflection. By default: _ReflectionTex

Errors / Troubleshooting

Reflection plane must have a Renderer to set temporary render texture to materials that need it.

Just like the error says. Reflection plane require a Renderer in itself (a Mesh Renderer for example)

Could not find any material with a property named: XXX

The parameter sent at: Shader property is not a valid property of any material in the Reflection plane. Enable debug to see all shader parameter names in the console and choose the right one. The most common issue is that Shader graph give an auto-generated-unique-name.

I see the reflection laggin

Most likely you are updating Main Camera after PlanarReflectionManager.

Change executionOrder accordingly

I don’t see the reflection in Reflection Probe

Baked Reflection Probes are not supported as Reflection is a realtime effect.

Your only option is to use realtime Reflection Probes.

I can see Reflections only from one side

Edit your shader, display Graph Inspector -> Graph settings -> enable: Two sided

Relfection colors are wrong / darker / brighter

Most likely double post-processing (double tonemapping/vignette?)

For example: tonemapping in reflection and then again for the entire scene.

Automatic mode: Use Reflection Camera Volume Mask to select the desired layer for reflection.

In Manual mode edit Reflection Camera: post processing.

Advanced

Performance tweaking

The most important aspect to performance are:

Note that some post-processing are reduntant and you dont have to do it twice, like tonemapping.

Those tweaks are “Inspector-manual”.

Update frequency on the contrary, require knowledge of your scene. We update every frame, because we don’t know if there is something non-static in your scene. But… if you do… you should disable the camera GameObject to skip the rendering process. Also if the camera is slow you can just frameskip odd/even frames.

But the most important of all. If your reflective plane is not visible, disable the Reflection Camera.

Last words

RenderTexture is required, so your device must support them.

VR and AR working in unity but we didn’t tested in a real device.

Resources

List of all external resources used and packed.

Cracked_S.jpg

Cracked_N.jpg

WoodenWindows-03

https://skirmish.io/posts/688-seamless-stained-glass-texture/

FrustumViewer.cs

Texel Density Texture 1

Rain textures from: * https://deepspacebanana.github.io/blog/shader/art/unreal%20engine/Rainy-Surface-Shader-Part-1 * https://deepspacebanana.github.io/blog/shader/art/unreal%20engine/Rainy-Surface-Shader-Part-2

keywords

single pass reflection URP

TODO / Future plans

Recursive mirrors

There is a basic / buggy implementation already in the asset but it’s still a work in progress.

backport to Unity 2019

it’s wip

Credits

www.pixelsize.me

Contact

info@pixelsize.me