Search Unity

Internal-DeferredShading.shader -> getting alpha value in CalculateLight?

Discussion in 'Shaders' started by Quatum1000, Sep 6, 2021.

  1. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    Hi,

    In half4 CalculateLight(unity_v2f_deferred i) I'd like to receive the alpha value of a martial in the
    Internal-DeferredShading.shader.Would it be possible?

    1. half4 gbuffer0 = tex2D (_CameraGBufferTexture0, uv); // Diffuse RGB, Occlusion A
    2. half4 gbuffer1 = tex2D (_CameraGBufferTexture1, uv); // Specular RGB, Smoothness A
    3. half4 gbuffer2 = tex2D (_CameraGBufferTexture2, uv); // Normal RGB
    I need a custom information in one pixel and thought about to store it in the alpha channel.
    But it seems I can't get the alpha channel in CalculateLight() ?

    In the end I want to use the custom information to access a different light map.
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    Nope. Deferred rendering is all about using as little data as possible. That means excluding data you don’t need. Deferred doesn’t work with transparent surfaces, so there’s no point in keeping the alpha value. The only channel that’s not used is the alpha of the normal gbuffer, which is an R10G10B10A2 format, which means it can only store the alpha values 0.0, 0.33, 0.67, and 1.0, and nothing in between.

    Some assets on the store are designed either as partial or whole replacements of Unity’s deferred renderer will change what all or some of the values in the gbuffer hold. Some work interchangeably with Unity’s standard shader by using that 4 bit alpha as a mask to change which light model / gbuffer layout is used. A common example would be to take the specular gbuffer and use it as a metallic gbuffer instead so you get two more channels to play with.
     
    Quatum1000 likes this.
  3. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    Thank you. Great, I will use metallic gbuffer instead.

    If I pass the light map inside of the half4 CalculateLight(unity_v2f_deferred i) function at:

    Code (CSharp):
    1.  ...
    2. half4 res = UNITY_BRDF_PBS(data.diffuseColor, data.specularColor, oneMinusReflectivity, data.smoothness, data.normalWorld, -eyeVec, light, ind);
    3.  
    4. half3  LM_sample = 0;
    5. #if defined (DIRECTIONAL) || defined (DIRECTIONAL_COOKIE)
    6.   LM_sample = LM_Compute(data.diffuseColor, LM_tex2DGrad);
    7. #endif
    8. return res += LM_sample;
    Then the reflections probes did not see the extra light mapping unfortunately on bake or real time.
    Would it possible to add the LM in the same way to the reflection probes?
     
  4. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,336
    Reflection probe rendering is all done in the black box of Unity's C++ code, so ... *shrug* ... no idea.
     
    Agent0023 and Quatum1000 like this.
  5. Quatum1000

    Quatum1000

    Joined:
    Oct 5, 2014
    Posts:
    889
    A bit of research uncovers unity use different shaders than deferred BDRF to render a reflection probe. In half4 CalculateLight(), if you return 0; and set data.diffuse = 0 the camera display the whole scene in black.
    But the reflection probe displays all lights and scene correctly with lights. Perhaps I'll find where it's going on.

    Another ugly thing is the material gloss/specular is rendered into reflection probes. I wish I could disable mat spec and/gloss in a reflection probe. The extra light sources rendered into a RP are great.