Search Unity

Unity Surface shader - Lightmapping viewDir

Discussion in 'Shaders' started by MaT227, Jan 19, 2015.

  1. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Unity offers the possibility to access viewDir inside the lightmapping lighting functions like explaine in the documentation : Custom Lighting models in Surface Shaders

    Code (CSharp):
    1. half4 LightingCustom_SingleLightmap (SurfaceOutput s, fixed4 color, half3 viewDir)
    2. half4 LightingCustom_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade, half3 viewDir)
    3. half4 LightingCustom_DirLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, half3 viewDir, bool surfFuncWritesNormal, out half3 specColor)
    But it doesn't seem to work... and it gives this error.
    Is there a workaround or maybe I am missing something.
     
    Last edited: Jan 22, 2015
  2. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    It seems that viewDir is not working... So I tried to transfer it from the Surface function to the lighting function using a custom SurfaceOutput but the result is quite funny...
    Code (CSharp):
    1. struct Input {
    2.     // ...
    3.     float3    viewDir;
    4.     INTERNAL_DATA
    5. };
    6.  
    7. struct SurfaceCustom {
    8.     // ...
    9.     half3    MyViewDir;
    10. };
    11.  
    12. void surf(Input IN, inout SurfaceCustom o) {
    13.     // ...
    14.     o.MyViewDir = IN.viewDir;
    15. }
    16.  
    17. half4 LightingCustom_SingleLightmap (SurfaceCustom s, fixed4 color) {
    18.     // ...
    19.     c.rgb = s.MyViewDir;
    20.     return c;
    21. }
    I only get a black object... BUT, if I use Albedo to transfer my viewDir, it works...
    To be honest I am quite bored by this as I don't see any solution... and there is no answer from the Unity devs.
     
  3. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    @Aras I take the liberty (again) to remember you this question as it might be a problem for some other developers according to this thread : Custom lightmapping + ViewDir seems to be broken
    • Is there a workaround or any solution concerning lightmapping and viewDir ?
    • Why is there nothing in MyViewDir variable ?
    Thank you very much.

     
  4. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    The only solution that comes in my mind is to build a full vertex/fragment shader that handles all types of passes for deferred and forward but I would prefer not doing it and use a surface shader.

    The full vertex/fragment might be a bigger source of errors and incompatibility and there is no example of a standard Blinn-Phong shader that handles all the Unity defines and rendering.

    Anybody has an idea ? :(
     
  5. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Here is a small post to sum up the issues I am facing.

    I am using currently using Unity Pro 4.6.0f3 in forward or deferred rendering.

    I've built a surface shader with a custom lighting function using the view direction vector and everything is working like a charm.
    Code (CSharp):
    1. half4 Lighting<Name> (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten);
    I am now trying to handle lightmapping inside my surface shader but here are the different problems I am facing.

    1. I tried to handle single lightmap lighting but this does not compile and gives an error.
    Code (CSharp):
    1. half4 Lighting<Name>_SingleLightmap (SurfaceOutput s, fixed4 color, half3 viewDir)
    I would like to know if there a way to send the view direction vector to the single lightmap lighting function ? Or maybe there is a workaround.

    2. I tried to handle dual lightmaps lighting but it seems that this lighting function is simply not handled and not called. Instead, the single lightmaps lighting function is called.
    Code (CSharp):
    1. half4 LightingSnow_DualLightmap (SurfaceOutput s, fixed4 totalColor, fixed4 indirectOnlyColor, half indirectFade, half3 viewDir)
    How can I handle dual lightmap lighting with the view direction vector ?

    3. I tried to handle directional lightmaps lighting but again I am facing a new issue. The function is well called but it seems that the view direction value is empty and outputs only black.
    Code (CSharp):
    1. half4 LightingSnow_DirLightmap (SurfaceOutput s, fixed4 color, fixed4 scale, half3 viewDir, bool surfFuncWritesNormal, out half3 specColor)
    How can I handle directionnal lightmaps lighting with view direction vector ?

    4. I also would like to know if it's possible to handle view direction vector with the deferred prepass lighting function.
    Code (CSharp):
    1. half4 Lighting<Name>_PrePass (SurfaceOutput s, half4 light);
    Is there a workaround for all those issues or maybe I am missing something. I would like to use the view direction vector inside all the lighting functions (forward, deferred, lightmap).

    I've thought about building my own vertex/fragment shader without having to use a surface shader but I would need to handle all types of passes and defines for deferred and forward.
    I would prefer not doing it and use a surface shader unless there is any sources of a simple Blinn-Phong shader handling everything (lightprobes, lightmaps, deferred, soft and hard shadows, etc.) with all defines and passes included. Maybe some kind of shader example or sample.

    I hope somebody will be able to help me solving my problems because those issues are blocking my project. Of course, if you need more informations feel free to ask.
    Thank you very much.
     
  6. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Anyone ? :(