CandelaSSRR in Unity 5

CandelaSSRR works out-of-the-box in Unity 5 ( at least in deferred mode ), but all surfaces are at maximum reflectivity regardless of the alpha parameter. How to make it work with the standard shader ?

The reason why everything is shiny is that the output alpha buffer is always all white (as can be seen in the frame debugger and the alpha of the scene view).

This is due to the way Unity resolves the GBuffers : UNITY_BRDF_PBS() always outputs 1 to the alpha channel.

Steps to fix this :

  • Download the Unity 5 shaders that match your beta version (mine is 21)

  • Copy DefaultResourcesExtra/StandardSpecular.shader, CGIncludes/UnityStandardCore.unity and DefaultResourcesExtra/Internal-DeferredShading.cginc anywhere in your Assets/ folder.

  • Rename all of them with a “_Custom” prefix (not sure if really needed)

  • Make the deferred renderer output the alpha channel : at the very end of UnityStandardCore_Custom.cginc, remplace

    outDiffuse = half4(s.diffColor, 0.0);

with

outDiffuse = half4(s.diffColor, s.alpha)
  • Make your Custom Standard Shader use this function instead of the built-in one : in StandardShader_Custom.shader, remplace

    Shader “Standard (Specular setup)”

with

Shader "Standard (Specular setup), Set Alpha"

and

#include "UnityStandardCore.cginc" // There are 5 occurrences of this line, all must be replaced

with

#include "UnityStandardCore_Custom.cginc"
  • Make Unity use your own copy of Internal-DeferredShading.inc : Render settings → Deferred → Custom → Pick your custom .shader

  • Replace

    Blend [_SrcBlend] [_DstBlend] // Line 16 in beta 21

with

Blend [_SrcBlend] [_DstBlend], Zero SrcAlpha

and

half4 res = UNITY_BRDF_PBS (baseColor, specColor, oneMinusReflectivity, oneMinusRoughness, normalWorld, -eyeVec, light, ind); // Line 66 in beta 21
return res;

with

half4 res = UNITY_BRDF_PBS (baseColor, specColor, oneMinusReflectivity, oneMinusRoughness, normalWorld, -eyeVec, light, ind);
res.a = gbuffer0.a;
return res;
  • You should now be able to change the reflectivity of the material by changing the diffuse color’s alpha; just make sure it uses your new custom standard shader.

A few notes

  • This may or may not interfere with occlusion, I’m not sure yet
  • I’m also not sure how it works with the transparent standard shader
  • In Unity 5 we now have access to the GBuffers, so Candela can skip doing their own pass and spare the fillrate (at least in deferred), so performance-wise, more modifications are needed.