Hue shift shader with point filtering?

Hi,

We’ve just tried getting a hue shift shader to work for our game, and we found something that does this pretty much exactly like we want it, but something seems to mess up our textures.
After applying the shader to a cube we saw that besides changing the hue, it also seems to blend the colors together. Changing to default diffuse makes everything sharp again, so I doubt its a texture issue.

We use 2x2 px textures (or more in some cases, but the same still applies) to make sure everything is always sharp, no matter the scale. For this we need a point filtered shader, or the colors don’t work correctly.

Is there some sort of tag that defines whether or not a texture is rendered using point filtering or something?

    Shader "Hue Shift" {
        Properties {
          _MainTex ("Texture", 2D) = "white" {}
          _HueShift("Hue", Range(0,.99)) = 0
        }
        SubShader {
          Tags { "RenderType" = "Opaque" }
          CGPROGRAM
          #pragma surface surf Lambert
          #pragma target 3.0        
     
            float3 hsv_to_rgb(float3 HSV)
            {
                    float3 RGB = HSV.z;
               
                       float var_h = HSV.x * 6;
                       float var_i = floor(var_h);
                       float var_1 = HSV.z * (1.0 - HSV.y);
                       float var_2 = HSV.z * (1.0 - HSV.y * (var_h-var_i));
                       float var_3 = HSV.z * (1.0 - HSV.y * (1-(var_h-var_i)));
                       if      (var_i == 0) { RGB = float3(HSV.z, var_3, var_1); }
                       else if (var_i == 1) { RGB = float3(var_2, HSV.z, var_1); }
                       else if (var_i == 2) { RGB = float3(var_1, HSV.z, var_3); }
                       else if (var_i == 3) { RGB = float3(var_1, var_2, HSV.z); }
                       else if (var_i == 4) { RGB = float3(var_3, var_1, HSV.z); }
                       else                 { RGB = float3(HSV.z, var_1, var_2); }
               
               return (RGB);
            }
     
          struct Input {
              float2 uv_MainTex;
          };
         
          float _HueShift;
         
          void surf (Input IN, inout SurfaceOutput o)
          {
              float3 hsv = float3(_HueShift, IN.uv_MainTex.x, IN.uv_MainTex.y);
              o.Albedo = half3(hsv_to_rgb(hsv));
          }
         
          ENDCG
        }
        Fallback "Diffuse"
      }

Clicking on the texture in the Project tab in the editor will bring up its inspector. This will allow you to change filter sampling.

Yes, I’m aware of that. I still get blending with this shader only tho. As I said I switched to default diffuse and the texture looked sharp as it should be. I think it has something to do with the shader itself, as the texture shows perfectly well with other shaders.

EDIT: here’s whats happening right now:


As you can see the black and white on the texture also seems to be wrong, but changing maintex x and y on line 39 fixed that. Only for that specific texture tho, which is weird. Another texture with the black and white parts swapped seems to give exactly the same result. Does anybody know what’s really going on in this shader?

Nothing? Does nobody know why this shader behaves like this?

About what kind of a filtering you say, if you do not look into texture at all :?

Does the shader code need some specific code for the texture to be rendered with point filtering?
Because the texture I’m using has point filtering set. The texture shown in the image above is a 2x2 px texture with point filtering.

The only thing I changed was the shader used in both images, yet default diffuse shader looks very different( it’s being rendered with point filtering, leaving the 2x2 texture sharp as it should be ) to how the hue shift shader looks( blurry, much like it would look without point filtering. I’ve got point filtering set in the texture tho ). I can’t seem to figure out why tho…

If there’s some code involved in getting a shader to actually use point filtering, I’d like to know what this code is, and where I can find more info about it.

Use tex2D to read from the texture.

hmm but isn’t the surface function reading _mainTex and using it to generate the output in the hsv_to_rgb thing?
Anyways, I found a different approach that worked for me here, using andeee’s script and the one on the unify page. Still curious about why this behaves like it does tho.

Set the texture to be point filtered from its inspector.

Here’s the relevant page in the manual.

The surface function is reading IN.uv_MainTex, which you can see on line 32 is a two-dimensional float–a UV coordinate. To sample a texture, you need to use tex2D(texture, uv) like Mouurusai says.

You can find examples of simple surface shaders that include texture sampling in the manual.