Search Unity

Custom lightmapping + ViewDir seems to be broken

Discussion in 'Shaders' started by avadlamani, Jun 12, 2014.

  1. avadlamani

    avadlamani

    Joined:
    Sep 29, 2013
    Posts:
    104
    The shader below is a 2d ramp shader (x = diffuse,y = fresnel). Recently however I tried porting this shader to a lightmapped version (cause free lighting is kinda awful, I'm looking at you forward rendering!), but as I need the View Direction to calculate fresnel, I used,
    Code (CSharp):
    1. half4 LightingDiff_SingleLightmap (SurfaceOutput s, fixed4 color, half3 viewDir) {
    2.          
    3.             half diff = Luminance(DecodeLightmap(color)) * 0.5 + 0.5;
    4.             half fres = 1-saturate(dot (viewDir,s.Normal));
    5.             half3 bl = tex2D(_BDRFTexture,half2(diff,fres));
    6.             half4 c;
    7.             c.rgb = s.Albedo * _LightColor0.rgb * (bl * atten * 2);
    8.             c.a = s.Alpha;
    9.             return c;
    10.            }
    However this produces the error message

    Code (CSharp):
    1. Shader error in 'Toon/Opaque/Lightmap': expression left of ."viewDir" is not a struct or array at line 179
    I have produced a bug report for this. Case No. 613315, I'm just posting to see if I've made a stupid error.

    Also here is the full shader

    Code (CSharp):
    1. Shader "Toon/Opaque/Lightmap" {
    2.     Properties {
    3.         _MainTex ("Base (RGB)", 2D) = "white" {}
    4.      
    5.         _BDRFTexture("BDRFDiffTex",2D) = "white"{}
    6.      
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType"="Opaque" }
    10.         LOD 200
    11.      
    12.         CGPROGRAM
    13.         #pragma surface surf Diff
    14.         #pragma debug
    15.         sampler2D _BDRFTexture;
    16.          half4 LightingDiff (SurfaceOutput s, half3 lightDir,half3 viewDir, half atten) {
    17.            half NdotL = dot (s.Normal, lightDir);
    18.            half diff = NdotL * 0.5 + 0.5;
    19.            half fres = 1-saturate(dot (viewDir,s.Normal));
    20.             half3 bl = tex2D(_BDRFTexture,half2(diff,fres));
    21.             half4 c;
    22.             c.rgb = s.Albedo * _LightColor0.rgb * (bl * atten * 2);
    23.             c.a = s.Alpha;
    24.             return c;
    25.            }
    26.          
    27.            half4 LightingDiff_SingleLightmap (SurfaceOutput s, fixed4 color, half3 viewDir) {
    28.          
    29.             half diff = Luminance(DecodeLightmap(color)) * 0.5 + 0.5;
    30.             half fres = 1-saturate(dot (viewDir,s.Normal));
    31.             half3 bl = tex2D(_BDRFTexture,half2(diff,fres));
    32.             half4 c;
    33.             c.rgb = s.Albedo * _LightColor0.rgb * (bl * atten * 2);
    34.             c.a = s.Alpha;
    35.             return c;
    36.            }
    37.          
    38.  
    39.         sampler2D _MainTex;
    40.      
    41.         struct Input {
    42.             half2 uv_MainTex;
    43.      
    44.         };
    45.  
    46.         void surf (Input IN, inout SurfaceOutput o) {
    47.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    48.          
    49.             o.Albedo = c.rgb;
    50.      
    51.             o.Alpha = c.a;
    52.         }
    53.         ENDCG
    54.     }
    55.     FallBack "Diffuse"
    56. }
    57.  
    EDIT: used the insert > code option
     
    Last edited: Jun 13, 2014
  2. avadlamani

    avadlamani

    Joined:
    Sep 29, 2013
    Posts:
    104
    Does no one have a solution for this cause I can't see what I'm doing wrong
     
  3. avadlamani

    avadlamani

    Joined:
    Sep 29, 2013
    Posts:
    104
    Not fixed just silently fails
     
  4. RC-1290

    RC-1290

    Joined:
    Jul 2, 2012
    Posts:
    639
    I was about to reply to this post when you decided to bump it. Please don't bump it so quickly after posting. It hasn't even been 24 hours
    Is the code the same as in your shader? Because you're missing several spaces. For example, after fixed4 and half3.

    [Edit] And another one... well at least you fixed your problem.
     
  5. avadlamani

    avadlamani

    Joined:
    Sep 29, 2013
    Posts:
    104
    nope that was because of copy-paste. Also very sorry for bumping so soon.
     
  6. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054

    Firstly if you are going to ask for help the least you can do is take the time and make sure your post comes through correctly and doesn't have silly copy & paste errors as well as not correctly using tags. i.e. your shader as an extra [/code] tag at the end and quite how random spaces get removed I have no idea, but it made it a real pain to even try and debug.

    Anyway after spending far too much time on this it appears the main problem is that using viewDir is not supported or just broken in pre Unity 4.5.0f6.

    Moving the shader from 4.3.4 to 4.5 and fixing the obvious error with your SingleLightmap() function ( return c when c doesn't exist etc) and it compiles without errors. Whether it works correctly I don't know as you don't supply textures or a test scene to compare in.

    Quite why it errors out in 4.3.4 I'm not sure, I can see no obvious differences in the debug compiled code between versions. I had assumed the error you saw meant that the IN struct didn't contain viewDir, but again I couldn't see why it wouldn't when looking through the compiled code, but perhaps I just missed something.
     
    RC-1290 likes this.
  7. avadlamani

    avadlamani

    Joined:
    Sep 29, 2013
    Posts:
    104
    But the thing is, I'm on unity 4.5.0f6.
     
  8. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Ah, the new joy of Unity's optimised shader generation and another reason why people need to start providing small test projects with their questions. You're right once I spent the time creating a scene with lightmapping the old error came back.


    Well this is a strange one, nothing that you are doing should cause an error, so I'm tempted to call it a bug. However I create a new shader using the tonemapLightmap example from this page http://docs.unity3d.com/Manual/SL-SurfaceShaderLightingExamples.html and then added viewDir and can't get the shader to error out at all.

    Oh that tonemap example has bugs in the documentation, a '>' sign is 'escaped' to become '>' and I strongly suspect that 'LightingTonemapped_StandardLightmap' should be 'LightingTonemapped_DirLightmap'

    That suggests there might not be a bug but still something wrong with your code.

    Where did you write this code? The fact it gets so mangled when copy & pasted here worries me that something else is wrong with it that i'm just not seeing or is hidden from view. Then again I wouldn't expect such issues to manifest as not having a viewDir.

    I'll keep digging, but at this point it might be worth making a bug report from it, unless someone from Unity stumbles into this thread.

    In the meantime here is a unmangled version of your code, though anyone testing will have to create themselves a lightmapped test scene to see the error.

    Code (CSharp):
    1. Shader "Toon/Opaque/Lightmap22"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex         ("Base (RGB)",  2D) = "white" {}
    6.         _BDRFTexture    ("BDRFDiffTex", 2D) = "white" {}
    7.     }
    8.  
    9.       SubShader
    10.       {
    11.           Tags { "RenderType" = "Opaque" }
    12.    
    13.         CGPROGRAM
    14.         #pragma surface surf Diff
    15.     //    #pragma target 3.0
    16.         //    #pragma debug
    17.        
    18.         sampler2D    _MainTex;
    19.           sampler2D    _BDRFTexture;
    20.        
    21.  
    22.         half4 LightingDiff (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
    23.         {
    24.             half NdotL = dot (s.Normal, lightDir);
    25.             half diff     = NdotL * 0.5 + 0.5;
    26.             half fres     = 1-saturate(dot (viewDir, s.Normal));
    27.             half3 bl     = tex2D(_BDRFTexture, half2(diff,fres));
    28.             half4 c;
    29.             c.rgb = s.Albedo * _LightColor0.rgb * (bl * atten * 2);
    30.             c.a = s.Alpha;
    31.             return c;
    32.         }
    33.      
    34.         inline fixed4 LightingDiff_SingleLightmap (SurfaceOutput s, fixed4 color, half3 viewDir) //this is the evil line
    35.         {
    36.             half3 diff = DecodeLightmap(color);
    37.             return fixed4(diff, 0); //c;
    38.         }
    39.  
    40.          
    41.         struct Input
    42.         {
    43.             half2 uv_MainTex;
    44.         };
    45.        
    46.         void surf (Input IN, inout SurfaceOutput o)
    47.         {
    48.             half4 c = tex2D (_MainTex, IN.uv_MainTex);
    49.            
    50.             o.Albedo = c.rgb;      
    51.             o.Alpha = c.a;
    52.         }
    53.      
    54.         ENDCG
    55.     }
    56.  
    57.     FallBack "Diffuse"
    58. }
     
  9. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Ok, I'm rapidly reaching the conclusion that there is a bug somewhere in trying to use viewDir in lightmap functions.

    Unfortuantely debugging in Unity 4.5 with its optimised compilation of shaders is making it really awkward to test. For example I tried the tonemapping example from the page I linked to earlier, that worked fine. I added viewDir into the lightmap functions and it appeared to work fine, but actually I don't think it recompiled as later on I cuased it to recompile and the same error as from your shader occurred.

    At this point I just think adding viewDir is broken in surface shaders, unless anyone else can find a problem with your source. I would therefore suggest creating a simple test scene and uploading it to Unity as a bug report.
     
  10. Aras

    Aras

    Unity Technologies

    Joined:
    Nov 7, 2005
    Posts:
    4,770
    Sounds like a bug with view direction indeed. Could you file a bug report so it doesn't get forgotten?

    @noisecrime: the "show all variants" in shader inspector does exactly the "compile all platforms & variants" thing (which 4.3 used to do on each and every import).
     
  11. avadlamani

    avadlamani

    Joined:
    Sep 29, 2013
    Posts:
    104
    I did, it's case no. 613315.

    Also, @Aras, do the normals of meshes get stripped from it when the object uses a light-mapped path?
     
    Last edited: Jun 13, 2014
  12. Noisecrime

    Noisecrime

    Joined:
    Apr 7, 2010
    Posts:
    2,054
    Hey Aras,

    Thanks for the useful information about 'Show all' to get it to compile all platforms and variants, that is very useful to know for the future.

    I've created a test project and have uploaded it as a bug report, case 613476.

    In the meantime for anyone else i've attached the test project here too.
     

    Attached Files:

    Last edited: Jun 13, 2014
  13. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Is this issue fixed ? It doesn't seems to be unfortunately :(
     
  14. MaT227

    MaT227

    Joined:
    Jul 3, 2012
    Posts:
    628
    Any news about this ?
     
    Flurgle likes this.