Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Now this is weird... (Problem with ObjSpaceLightDir)

Discussion in 'Shaders' started by FuzzyQuills, Sep 11, 2014.

  1. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Hi.

    I have something strang going on with a shader I wrote specifically to hard-code all the lighting code, rather than using a surface shader, as surface shaders, I have found, are optimized, but I wish to make it even simpler and only add the relevant bits for my project.

    My problem with this? ObjSpaceLightDir COMPLETELY SCREWS UP on DX9, but on DX11, it works flawlessly!

    In DX9, light direction completely screws up:
    WeirdDX9Lighting.png
    But in DX11 renderer, (actual video card only supports DX10, but it will do...) it's correct light direction dot product:
    CorrectDX11Lighting.png
    Anyone know of a workaround/solution? One of my workarounds was to pre-compute the lighting in the vertex shader instead, of which works with both renderers, but per-pixel lighting I prefer as it's smoother.
     
  2. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
  3. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Show your shader because seems it's work fine.
     
  4. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    Oh yes...

    Here:
    Code (csharp):
    1.  
    2. Shader "Custom/LightProbe_BRDF" {
    3.  
    4.     Properties {
    5.         _MainTex ("Base (RGB)", 2D) = "white" {}
    6.         _Probe ("Light Probe Cubemap", CUBE) = "" {}
    7.     }
    8.    
    9.     SubShader {
    10.         Pass {
    11.         tags { "RenderType" = "Opaque" "LightMode" = "ForwardBase" }
    12.         LOD 200
    13.             //lighting surface pass - fwdbase
    14.             CGPROGRAM
    15.             #pragma vertex vertSurf
    16.             #pragma fragment fragColor
    17.             #pragma multi_compile_fwdbase
    18.             #include "UnityCG.cginc"
    19.             #include "AutoLight.cginc"
    20.            
    21.             struct v2f {
    22.                 fixed4 pos : POSITION;
    23.                 fixed2 uv : TEXCOORD0;
    24.                 float3 lightDir : TEXCOORD1;
    25.                 fixed3 normal : COLOR;
    26.                 fixed3 wn : TEXCOORD2;
    27.                 fixed3 viewDir : COLOR1;
    28.                 LIGHTING_COORDS(3,4)
    29.             };
    30.            
    31.             v2f vertSurf ( appdata_full v ) {
    32.                 v2f o;
    33.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    34.                 o.uv = v.texcoord.xy;
    35.                 o.lightDir = ObjSpaceLightDir(v.vertex);
    36.                 o.normal = v.normal;
    37.                 o.wn = mul((float3x3)_Object2World, v.normal.xyz);
    38.                 o.viewDir = normalize(ObjSpaceViewDir(v.vertex));
    39.                 TRANSFER_VERTEX_TO_FRAGMENT(o)
    40.                 return o;
    41.             }
    42.            
    43.             sampler2D _MainTex;
    44.             samplerCUBE _Probe;
    45.             fixed4 fragColor (v2f i) : COLOR {
    46.                 fixed4 light = dot(i.lightDir,i.normal);
    47.                 fixed4 viewlight = dot(i.viewDir, i.normal);
    48.                 fixed4 color = tex2D(_MainTex, i.uv);
    49.                 fixed shadowTerm = LIGHT_ATTENUATION(i);
    50.                 fixed4 finalLight = lerp(texCUBE(_Probe, i.wn)*0.6, 1, light) * shadowTerm + (viewlight*0.5+0.5);
    51.                 return finalLight * (color*2);
    52.             }
    53.             ENDCG
    54.         }
    55.         Pass {
    56.         tags { "RenderType" = "Opaque" "LightMode" = "ForwardAdd" }
    57.         Blend One One
    58.         LOD 200
    59.             //lighting surface pass - fwdbase
    60.             CGPROGRAM
    61.             #pragma vertex vertSurf
    62.             #pragma fragment fragColor
    63.             #pragma multi_compile_fwdadd
    64.             #include "UnityCG.cginc"
    65.             #include "AutoLight.cginc"
    66.            
    67.             struct v2f {
    68.                 fixed4 pos : POSITION;
    69.                 fixed2 uv : TEXCOORD0;
    70.                 fixed3 lightDir : TEXCOORD1;
    71.                 fixed3 normal : COLOR;
    72.                 fixed3 wn : TEXCOORD2;
    73.                 fixed3 viewDir : COLOR1;
    74.                 LIGHTING_COORDS(3,4)
    75.             };
    76.            
    77.             v2f vertSurf ( appdata_full v ) {
    78.                 v2f o;
    79.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    80.                 o.uv = v.texcoord.xy;
    81.                 o.lightDir = ObjSpaceLightDir(v.vertex);
    82.                 o.normal = v.normal;
    83.                 o.wn = mul((float3x3)_Object2World, v.normal.xyz);
    84.                 o.viewDir = normalize(ObjSpaceViewDir(v.vertex));
    85.                 TRANSFER_VERTEX_TO_FRAGMENT(o)
    86.                 return o;
    87.             }
    88.            
    89.             sampler2D _MainTex;
    90.             samplerCUBE _Probe;
    91.             fixed4 fragColor (v2f i) : COLOR {
    92.                 fixed4 light = dot(i.lightDir,i.normal) ;
    93.                 fixed4 viewlight = dot(i.viewDir, i.normal);
    94.                 fixed4 color = tex2D(_MainTex, i.uv);
    95.                 fixed4 finalLight = lerp(texCUBE(_Probe, i.wn)*0.6, 0.9, light) * LIGHT_ATTENUATION(i) + (viewlight*0.5+0.5);
    96.                 return finalLight * (color*2) * 0.7;
    97.             }
    98.             ENDCG
    99.         }
    100.     }
    101.    
    102.     FallBack "Diffuse"
    103.    
    104. }
     
  5. mouurusai

    mouurusai

    Joined:
    Dec 2, 2011
    Posts:
    350
    Replace
    fixed3 normal : COLOR;
    with
    fixed3 normal : TEXCOORD5;.

    <<FirstRule Of Optimization. Don't do it!>>:D
    Why you used this semantic?
     
    Last edited: Sep 15, 2014
  6. FuzzyQuills

    FuzzyQuills

    Joined:
    Jun 8, 2013
    Posts:
    2,871
    I have no idea. for some reason, I thought TEXCOORD5 semantic wasn't available in DX9, so I didn't map it. (And incidentally mapped COLOR instead!) Will give this a try this afternoon. I also tried mapping normal with semantic NORMAL, but DX9 throws an error when I do that.

    EDIT: It fixed it! thank you! in future, I won't use COLOR for normals.
     
    Last edited: Sep 15, 2014