Skip to content

Error-mdl/UnityReflectionProbeArrays

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Reflection Probe Arrays For Unity's Built-in Render Pipeline

Unity's batching system is critical in most applications for reducing the draw call load on the cpu. However it has a number of limitations, including the inability to batch draw calls for meshes which require different data (other than the mesh data) to render. While this most obviously this means that meshes of different materials cannot be batched, it also means that if two meshes of the same material cannot be batched if they sample different light or reflection probes. One can avoid light probes breaking static batching by either using baked lightmaps or light probe proxy volumes, but for reflection probes this is seemingly unavoidable. Unity provides two reflection probe textures to each object, and if either of these differ from another object the two objects will not be able to be batched. In order to work around this issue, this tool replaces unity's default reflection probe system with one that takes advantage of cubemap arrays. If all reflection probes in a scene are of the same dimensions, it is possible to combine them into a single array texture. Then if each object's shader is then provided with the entire array and each object's verticies contain the information about which cubemaps to sample in the array, the data required to render each object will no longer differ and unity will be able to combine the meshes for batching.

How it works

This script goes through every active gameobject in the current scene and compiles a list of all reflection probes. It then combines their cubemaps into a single cubemap array, and generates a 2d texture array containing information about each probe. Afterwards, it goes through every gameobject in the scene again, and finds all objects marked as static batching. It determines if each object has a mesh filter, and if it does it makes a full duplicate of the mesh referenced by the filter and adds data to the 3rd and 4th uv channels. Into the third uv it combines the byte representation of 4 unsigned shorts representing the indicies of the four closest reflection probes and stores them as 2 floats in the first two channels of the uv, and into the rest of UV3 and all of UV4 it stores the world position of the minimum and maximum of the mesh's bounds. After modifying the mesh's data, the script then saves the mesh, assigns it to the mesh filter, assigns the two texture arrays to the first material on the mesh, and disables reflection probes on the mesh renderer.

In order to utilize the reflection probe arrays, a specialized shader must be used. A cginc with with several functions relating to sampling the probe array and unpacking data from the uvs is included, as well as a simple example shader showing how to utilize the functions and a couple of barebones standard-like shaders.

The actual sampling of the probes in the shader is different from unity's default implementation. Four probes are provided instead of two, and no weights are given for the probes. Instead, it is up to each pixel to determine the weights of the probes, which is done by determining the distance of the pixel from each probe's bounding box. Additionally, the bounds of each reflection probe is expanded to contain the bounds of the mesh so no ugly seams occur at the boundaries of a probe in the middle of a mesh. If the probe is a box projection probe, there is a factor which decreases the weight of a probe to 0 if the distance the reflected ray travels is close to 0. This prevents a common situation where the probe's bounds are expanded to match an object and a face of the object is now flat against a wall of the box projection, resulting the reflected ray travelling 0 distance and the reflected image looking like it is painted on to the surface.

WARNINGS

Because this replaces the references of all static batched meshes in the scene, it is unwise to run this tool on your main copy of the scene. Any edits to the source model files will no longer propigate to the instances of that mesh in the scene, as the mesh filters are now using a copy of the old version of the mesh. Additionally, one should unlink all prefabs containing static batching meshes before building, as unity will also include all the original mesh files referenced by the base prefab inflating the file size. In order to make sure you don't run the tool on your main scene and unlink all the prefabs by accident, you can and "_workingcopy" to the end of your scene's name and the tool will warn you and refuse to function.

Another important note is that because all 32 bits of each channel in UV's 3 and 4 are important, you must turn off vertex compression for texcoords 2 and 3 in the project settings. The tool should alert you if they are being compressed however, and will present you with a button to turn off compression for those channels.

How to use

I recommend only using this tool as a final step before building your project. First, assign a compatible shader to the materials used by all static batched objects in your scene. Then save the scene as a different file, open the conversion window under Window -> Reflection Probe Arrays. Then hit the big "Create Arrays and Convert All Static Batched Meshes" at the bottom of the window. After it is finished processing all the meshes, go through the scene and unlink any prefab containing static batched meshes (it really pays to have an organized scene!) so unity does not include the original meshes in addition to the edited ones when building.

Credits

d4rkpl4y3r - BRDF PBS Macro shader I based my standard replacements on

mykohsu - Fast array filling method I used for creating the data arrays in UV2 and 3 (https://github.com/mykohsu/Extensions/blob/master/ArrayExtensions.cs)

About

Utility for unity to generate arrays of reflection probes to preserve static batching.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published