Search Unity

  1. Looking for a job or to hire someone for a project? Check out the re-opened job forums.
    Dismiss Notice
  2. Unity 2020.2 has been released.
    Dismiss Notice
  3. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Bug Project specific bugs (tex2Dlod in vertex program, garbled mouse cursor)

Discussion in 'Shaders' started by KayH, Jul 9, 2020.

  1. KayH


    Jan 6, 2017
    I'm putting this in the shader category because the more important of the two problems fits it. Basically I'm encountering two strange behaviors:

    1) When I move my mouse over the game view, the cursor turns to a larger rectangle with garbled pixels (probably rendering some random data as a graphic).

    2) When I use tex2Dlod in the vertex function of a shader, the values I get aren't the expected ones. If an RGBA value is exactly 1 or 0, it is the correct value. For anything in between I get a smaller value than it is supposed to.

    I reproduced the issue in a simplified shader for demonstration purposes. But when I use this shader in a new project, it works as intended. Also, the garbled mouse cursor appears in three different Unity Editor versions, but only in this one project. Not in the new one I made to test the demo shader.

    The code for the demo shader:

    Code (CSharp):
    1. Shader "Debug/WrongSampleData"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("MainTex (RGBA)", 2D) = "black" {}
    6.     }
    8.     SubShader
    9.     {
    10.         CGPROGRAM
    12.         #pragma surface surf Standard vertex:vert
    13.         #include "UnityCG.cginc"
    15.         sampler _MainTex;
    17.         struct Input
    18.         {
    19.             float4 color;
    20.         };
    22.         void vert(inout appdata_full v, out Input o) {
    23.             float4 data = tex2Dlod(_MainTex, float4(0, 0, 0, 0));
    24.             if (data.x > 0.85)
    25.                 o.color = float4(1,0,0,1);
    26.             else
    27.                 o.color = float4(0, 0, 1, 1);
    28.         }
    30.         void surf (Input IN, inout SurfaceOutputStandard o)
    31.         {
    32.             o.Albedo = IN.color.rgb;
    33.         }
    35.         ENDCG
    36.     }
    37.     FallBack "Diffuse"
    38. }
    I use a texture with one pixel that has the color rgba(0.9, 0,0.9,1). So it should render as red, because 0.9 is bigger than 0.85. But it gives me blue, which means the value coming back from tex2Dlod is equal or smaller than 0.85.

    When I use another texture that has rgba(1, 0, 1, 1), it renders red as expected:

    In my completely fresh and new project, when I copy over this shader and the textures, both render red as expected:


    So my question is, what could cause these bugs in my project? What could change the mouse cursor? What could affect the tex2Dlod function (which I assume is a native function)?

    The textures are attached to this post.


    Attached Files:

  2. KayH


    Jan 6, 2017
    Okay, I was able to reproduce both problems in my fresh project by copying over files from the "corrupted" project.

    The unexpected shader behavior was due to a player setting. I had color space set to linear, setting it to gamma fixed the problem.

    The garbled mouse cursor seems to be caused by some file in my project. Since it isn't breaking anything in the build (I hope) I'm probably not going to spend time finding it (for now), but if anyone has a hunch what might be causing this please let me know.
  3. KayH


    Jan 6, 2017
    I remembered that the reason I set it to linear is because Oculus says to do so:

    Is there a way to make my data texture shader work with the linear setting? I guess I could do my own gamma correction on the values.
    Last edited: Jul 9, 2020
  4. KayH


    Jan 6, 2017
    Removing the tick on sRGB (Color Texture) in the texture setting makes it linear. Values seem to be reliable now.