[Solved] Thermal shader ramp work with grayscale but not pure black or white

Hello everybody,

I’m new into shading. I tried to create a shader to view heat (it take greyscale texture and a color ramp). But it work only if color is not black and white, it only accept grey.

On the next image, you see the color gradient on left and right is light blue, not what is wanted (should be left dark blue, and right be white)

Here the map in black should be dark blue on the ocean, but it is not

For these results, I tried to mix these two shaders together
Unity - Manual: Surface Shader lighting examples - The Toon Ramp one
Temperature map shader - Unity Engine - Unity Discussions - The proposition of Acegikmo

The result is this :

    Shader "Heat shader" {
        Properties {
            _MainTex ("Texture", 2D) = "white" {}
            _Ramp ("Texture rampe", 2D) = "white" {}
        }
        SubShader {
            Tags { "RenderType" = "Opaque" }
            CGPROGRAM
            #pragma surface surf Ramp

            sampler2D _Ramp;

            half4 LightingRamp (SurfaceOutput s, half3 lightDir, half atten) {
                half NdotL = dot (s.Normal, lightDir);
                half diff = NdotL * 0.5 + 0.5;
                half3 ramp = tex2D (_Ramp, float2(diff, diff) ).rgb;

                half4 c;
                c.rgb = s.Albedo * ramp;
                c.a = s.Alpha;
                return c;
            }

            struct Input {
                float2 uv_MainTex;
            };
      
            sampler2D _MainTex;
      
            void surf (Input IN, inout SurfaceOutput o) {
                fixed tempData = tex2D(_MainTex, IN.uv_MainTex.xy);
                fixed2 rampUV = fixed2(tempData, 0);
                fixed3 heatColor = tex2D(_Ramp, rampUV);
                o.Albedo = heatColor;
            }
            ENDCG
        }
        Fallback "Diffuse"
    }

For test I use these images
Ramp :
2619065--183853--ramp_heat_04.png

Gradient :

I have mixed opinion. Or I messed up everything (surely), or it’s a bug about texture rendering and compression (i don’t think so, i changed texture compression and no change), or even something else (yet, not convinced). I’ll try to investigate further.

Have you any idea of what is happening?

Are your textures set to clamp? If they are set to wrap, it will blend between the opposite edges like that. Useful for seamless textures but that is not what you want here.

It’s a track (edit: confirmed answer xD) for a problem I had (when zooming to some edges on some textures, so thanks ^^) but not the solution of this current problem

I tried to rebuild the shader inside Blender, and I faced the same problem of colors

2620014--183930--shader bug_blender1.png
2620014--183928--shader bug_blender2.png

I think I will first try to make it working into Blender, then try to export the logic to Unity

EDIT:

I tried to simulate color levels (brightness and contrast) http://blenderartists.org/forum/archive/index.php/t-235247.html?s=087a4258fc5df2ca1e1592ea56dd41d4 . If black is @!#{xD} my colors, then I should remove black. I used a Math.Add() node in Blender for brightness (make black grey) and Math.Multiply for contrast (here at 1.0 so it doesn’t change, but I prefer have it there to not forget i can use it).

2620014--183932--shader bug_blender3_possibleAnswer.jpg

I’ll still have to check about white light. I’ll have to work on something else for now but I’ll investigate further later.

Understood, in fact I also had to play
with the brightness to remove black
AND
with the contrast to remove white.
Each images are differents. Different values maybe required

I post here the working shader;

Shader "LeDucSAS/Heat shader" {
    Properties {
        _MainTex ("Texture", 2D) = "white" {}
        _Ramp ("Texture rampe", 2D) = "white" {}
        _Brightness ("Brightness", Float) = 0.05
        _Contrast ("Contrast", Float) = 0.95
    }
    SubShader {
        Tags { "RenderType" = "Opaque" }
        CGPROGRAM
        #pragma surface surf Ramp

        sampler2D _Ramp;

        half4 LightingRamp (SurfaceOutput s, half3 lightDir, half atten) {
            half NdotL = dot (s.Normal, lightDir);
            half diff = NdotL * 0.5 + 0.5;
            half3 ramp = tex2D (_Ramp, float2(diff, diff) ).rgb;

            half4 c;
            c.rgb = s.Albedo ;
            return c;
        }

        struct Input {
            float2 uv_MainTex;
        };
   
        sampler2D _MainTex;
        float _Brightness;
        float _Contrast;
   
        void surf (Input IN, inout SurfaceOutput o) {
            fixed tempData = tex2D(_MainTex, IN.uv_MainTex.xy);
            // Shader don't work with pure black and white
            // Add brightness to remove pure black and make it very dark greyish
            tempData += _Brightness; // 0.05; // Best val, may depend
            // Add Contrast to remove pure white and make it very light greyish
            tempData *= _Contrast; // 0.95; // Best val, may depend
            fixed2 rampUV = fixed2(tempData, 0);
            fixed3 heatColor = tex2D(_Ramp, rampUV);
            o.Albedo = heatColor.rgb;
        }
        ENDCG
    }
    Fallback "Diffuse"
}

I had a ugly border due to texture wrap mode. And I thought it was due to my shader, but in fact not. So thanks to you Captain Science, I set it to clamp and problem was solved. This information was useful to me :slight_smile: