Search Unity

  1. New Unity Live Help updates. Check them out here!

    Dismiss Notice

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

Discussion in 'Shaders' started by Odyssee, May 2, 2016.

  1. Odyssee

    Odyssee

    Joined:
    Sep 14, 2014
    Posts:
    3
    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)

    shader bug2.png

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

    shader bug1.png

    For these results, I tried to mix these two shaders together
    http://docs.unity3d.com/Manual/SL-SurfaceShaderLightingExamples.html - The Toon Ramp one
    http://forum.unity3d.com/threads/temperature-map-shader.190030/ - The proposition of Acegikmo

    The result is this :
    Code (CSharp):
    1.     Shader "Heat shader" {
    2.         Properties {
    3.             _MainTex ("Texture", 2D) = "white" {}
    4.             _Ramp ("Texture rampe", 2D) = "white" {}
    5.         }
    6.         SubShader {
    7.             Tags { "RenderType" = "Opaque" }
    8.             CGPROGRAM
    9.             #pragma surface surf Ramp
    10.  
    11.             sampler2D _Ramp;
    12.  
    13.             half4 LightingRamp (SurfaceOutput s, half3 lightDir, half atten) {
    14.                 half NdotL = dot (s.Normal, lightDir);
    15.                 half diff = NdotL * 0.5 + 0.5;
    16.                 half3 ramp = tex2D (_Ramp, float2(diff, diff) ).rgb;
    17.  
    18.                 half4 c;
    19.                 c.rgb = s.Albedo * ramp;
    20.                 c.a = s.Alpha;
    21.                 return c;
    22.             }
    23.  
    24.             struct Input {
    25.                 float2 uv_MainTex;
    26.             };
    27.      
    28.             sampler2D _MainTex;
    29.      
    30.             void surf (Input IN, inout SurfaceOutput o) {
    31.                 fixed tempData = tex2D(_MainTex, IN.uv_MainTex.xy);
    32.                 fixed2 rampUV = fixed2(tempData, 0);
    33.                 fixed3 heatColor = tex2D(_Ramp, rampUV);
    34.                 o.Albedo = heatColor;
    35.             }
    36.             ENDCG
    37.         }
    38.         Fallback "Diffuse"
    39.     }

    For test I use these images
    Ramp :
    ramp_heat_04.png

    Gradient :
    gradient.png

    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?
     
  2. smd863

    smd863

    Joined:
    Jan 26, 2014
    Posts:
    293
    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.
     
  3. Odyssee

    Odyssee

    Joined:
    Sep 14, 2014
    Posts:
    3
    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

    shader bug_blender1.png
    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).

    shader bug_blender3_possibleAnswer.png

    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.
     
    Last edited: May 3, 2016
  4. Odyssee

    Odyssee

    Joined:
    Sep 14, 2014
    Posts:
    3
    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

    shader bug_solved.png

    I post here the working shader;

    Code (CSharp):
    1. Shader "LeDucSAS/Heat shader" {
    2.     Properties {
    3.         _MainTex ("Texture", 2D) = "white" {}
    4.         _Ramp ("Texture rampe", 2D) = "white" {}
    5.         _Brightness ("Brightness", Float) = 0.05
    6.         _Contrast ("Contrast", Float) = 0.95
    7.     }
    8.     SubShader {
    9.         Tags { "RenderType" = "Opaque" }
    10.         CGPROGRAM
    11.         #pragma surface surf Ramp
    12.  
    13.         sampler2D _Ramp;
    14.  
    15.         half4 LightingRamp (SurfaceOutput s, half3 lightDir, half atten) {
    16.             half NdotL = dot (s.Normal, lightDir);
    17.             half diff = NdotL * 0.5 + 0.5;
    18.             half3 ramp = tex2D (_Ramp, float2(diff, diff) ).rgb;
    19.  
    20.             half4 c;
    21.             c.rgb = s.Albedo ;
    22.             return c;
    23.         }
    24.  
    25.         struct Input {
    26.             float2 uv_MainTex;
    27.         };
    28.    
    29.         sampler2D _MainTex;
    30.         float _Brightness;
    31.         float _Contrast;
    32.    
    33.         void surf (Input IN, inout SurfaceOutput o) {
    34.             fixed tempData = tex2D(_MainTex, IN.uv_MainTex.xy);
    35.             // Shader don't work with pure black and white
    36.             // Add brightness to remove pure black and make it very dark greyish
    37.             tempData += _Brightness; // 0.05; // Best val, may depend
    38.             // Add Contrast to remove pure white and make it very light greyish
    39.             tempData *= _Contrast; // 0.95; // Best val, may depend
    40.             fixed2 rampUV = fixed2(tempData, 0);
    41.             fixed3 heatColor = tex2D(_Ramp, rampUV);
    42.             o.Albedo = heatColor.rgb;
    43.         }
    44.         ENDCG
    45.     }
    46.     Fallback "Diffuse"
    47. }

    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 ^_^
     
    Last edited: May 3, 2016
unityunity