Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Anchor Override only works for transparent materials in deferred?

Discussion in 'General Graphics' started by Warrior1424, Feb 22, 2016.

  1. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
    So I have certain objects that need to not be affected by the fancy per-pixel reflection probes that come with deferred rendering.

    Luckily Unity has the feature "Anchor Override".
    I put an empty transform far under my map, so it picks up the reflection of only the skybox, then set the anchor override of my object to that transform.
    Problem is, it doesn't work in every situation.
    To be honest, I'm surprised it works at all, given that the reflections are rendered per-pixel.
    (I'm using the standard shader btw)
    It only works when the shader's rendering mode is marked "Transparent" or "Fade".
    If it's marked "Transparent" or "Fade" I can move the anchor override around and see it changing reflections based on its position.
    When the shader's rendering mode is marked "Opaque", it ignores the anchor override completely.

    Is this a bug, or is there an explanation as to why it works this way?
    I'm really hoping it's a bug, because it's a really nice feature that would be nice to use in deferred for Opaque objects.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    Some of the options mesh renderers have only work when forward rendered; reflection probe type, anchor override and receive shadows. This isn't a bug, this is the intended behavior for deferred rendering for anything opaque using the standard shader. Transparent objects are always forward rendered, deferred rendering only works with opaque objects, so those options still work, as will any opaque materials using custom shading models as these will also be forward rendered.

    To understand why you should understand how deferred rendering works. You can read up on some of it here:
    http://docs.unity3d.com/Manual/RenderTech-DeferredShading.html

    The short version is deferred objects are rendered into textures to store the information needed for shading. For performance and memory usage this should be as few textures as possible. That means uncommonly used features like those listed above are omitted, especially as to support anchor override would require being able to optionally store the position for sampling the reflection probe for every pixel on the screen which would be very wasteful as for most pixels that position is already know.

    Now you can work around this with some custom shaders. You can create a copy of the standard shader with the deferred passes removed to force it rendering as a forward pass for example.
     
    Last edited: Feb 22, 2016
    theANMATOR2b likes this.
  3. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
    I never knew that, thanks!

    When you say a copy, do you mean making a surface shader that uses things from the standard shader (roughness, normal map, etc) or do you mean taking the source code of the standard shader, copying it, and modifying that?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    Either, though you would have to take the surface shader's generated code and delete the deferred pass, or use exclude_path:deferred
     
  5. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
    Thanks!
    Although, I tried the latter, and all it seemed to do was make the shader not use any reflections at all.
    Here's what my top line looks like:
    Code (CSharp):
    1. #pragma surface surf StandardSpecular fullforwardshadows exclude_path:deferred
    [exclude_path: prepass] seems to work just fine, and leaving the exclude deferred bit of allows for the deferred reflections to show up. Any ideas?
    (Also, I put a space before "prepass" to keep it from making the stupid :p emoji)
     
  6. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,209
    Excluding prepass is disabling the legacy deferred rendering path, it has nothing to do with the new deferred path that has per pixel reflections. If excluding deferred is disabling reflections entirely I this might be a bug and you may try reporting it to Unity. If you can post the whole shader here I can also see if there's anything obvious going wrong.

    Actually there's one more simple thing you can try. You can force force a material to be rendered as part of the transparent queue which will force them to be forward rendered regardless of if they're transparent or not.

    The part in transparent or alpha test shaders that have "Queue"="Transparent" is what actually changes if they're considered part of the opaque (and could be deferred) or transparent (and must be forward).
    http://docs.unity3d.com/Manual/SL-SubShaderTags.html

    Since you said your transparent objects are working properly, then we just need to set the material's custom queue to 2501 or higher. To do that select a material, right click on the inspector tab and select debug, and set the custom queue to 2501. Note that the custom editor for the standard shader itself changes this value as well, so it might override this if you change anything else in the material after you've made this change, it'll also get overrides any time a material's shader is changed in the editor.
     
  7. Warrior1424

    Warrior1424

    Joined:
    Sep 30, 2010
    Posts:
    984
    I tried what you suggested, and the result was still the same.
    That's when it hit me.
    I took the anchor override off the object, and a reflection appeared.
    I went back down to the reflection probe that surrounded my anchor, and hit bake.
    I reassigned the anchor to my game object and sure enough, the correct reflection appeared.
    For some odd reason, that reflection probe specifically was not baked. ALL the other probes were baked, but not that one.

    Thank you @bgolus for all of your help, and sorry for wasting your time with that last part.
     
    theANMATOR2b and bgolus like this.