Search Unity

Toonshader ramp problem

Discussion in 'Shaders' started by and1i, Mar 2, 2017.

  1. and1i

    and1i

    Joined:
    Mar 2, 2017
    Posts:
    4
    Hey. Iam learning shaders and now Iam stuck on this problem.
    Iam trying to make ramp effect, but still I get this smoothed lighting.

    I tried to look answers from this forum but cant find any help. Maybe it is Linux? If you spot what is wrong in this, please let me know. :)

    Thank you!


    Unity version 5.4.2

    Ramp texture:
    Wrap mode = Clamp
    Filter mode = Point

    And my shader:
    Code (CSharp):
    1. // Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'
    2.  
    3. Shader "Unlit/test2"
    4. {
    5.     Properties
    6.     {
    7.         _Color ("Color", Color) = (1,1,1,1)
    8.         _Float ("Value", Range(-100,100)) = 0
    9.         _MainTex ("Texture", 2D) = "white" {}
    10.         _RampTex ("Ramp", 2D) = "white" {}
    11.     }
    12.     SubShader
    13.     {
    14.         pass{
    15.             Tags{"LightMode" = "ForwardBase"}
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.  
    20.             float4 _Color;
    21.             float _Float;
    22.             sampler2D _RampTex;
    23.             sampler2D _MainTex;
    24.  
    25.             float4 _LightColor0;
    26.  
    27.             struct vertexInput{
    28.                 float4 vertex : POSITION;
    29.                 float3 normal : NORMAL;
    30.                 float2 texcoord : TEXCOORD0;
    31.  
    32.             };
    33.  
    34.             struct vertexOutput{
    35.                 float4 pos : SV_POSITION;
    36.                 float4 col : COLOR;
    37.                 float2 texcoord : TEXCOORD0;
    38.             };
    39.  
    40.             vertexOutput vert(vertexInput v){
    41.                 vertexOutput o;
    42.  
    43.                 float3 normalDirection = normalize(mul(float4(v.normal, 1), unity_WorldToObject).xyz);
    44.                 float3 lightDirection;
    45.                 float atten = 1.0;
    46.                 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
    47.  
    48.                 float3 diffuseReflection = atten * _LightColor0.xyz * _Color.rgb * max(0.0,dot(normalDirection, lightDirection));
    49.                 float3 lightFinal = diffuseReflection + UNITY_LIGHTMODEL_AMBIENT.xyz;
    50.                 lightFinal = tex2D(_RampTex, float2(lightFinal)).rgb * _LightColor0.xyz;
    51.  
    52.                 o.texcoord = v.texcoord;
    53.                 o.col = float4((lightFinal), 1),
    54.                 o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
    55.  
    56.                 return o;
    57.  
    58.  
    59.             }
    60.             float4 frag(vertexOutput i) : COLOR{
    61.                 fixed4 texColor = tex2D(_MainTex, i.texcoord);
    62.                 return texColor * i.col;
    63.             }
    64.  
    65.  
    66.             ENDCG
    67.  
    68.         }
    69.     }
    70. }
    71.  
     

    Attached Files:

    • help.png
      help.png
      File size:
      328.1 KB
      Views:
      755
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    You're using a texture with bilinear sampling (Filter Mode: Bilinear on the texture asset's import settings), try using Point, or a higher resolution ramp.
     
  3. and1i

    and1i

    Joined:
    Mar 2, 2017
    Posts:
    4
    I was using Point filter mode on my texure.
     
  4. and1i

    and1i

    Joined:
    Mar 2, 2017
    Posts:
    4
    Now I changed to windows and get error:

    Shader error in 'Unlit/test2': incorrect number of arguments to numeric-type constructor at line 50 (on d3d11)

    Compiling Vertex program
    Platform defines: UNITY_ENABLE_REFLECTION_BUFFERS UNITY_PBS_USE_BRDF1 UNITY_SPECCUBE_BOX_PROJECTION UNITY_SPECCUBE_BLENDING SHADER_API_DESKTOP


    I have no idea what is happening.
     
  5. brownboot67

    brownboot67

    Joined:
    Jan 5, 2013
    Posts:
    375
    You have a lot of miscast types in your code.

    The error is for this one
    Code (csharp):
    1. lightFinal = tex2D(_RampTex, float2(lightFinal)).rgb * _LightColor0.xyz;
    lightFinal is a float3, tex2D wants uv's that are a float2.

    Lines 49, and 48 will probably also give you trouble depending on target platform.
     
  6. and1i

    and1i

    Joined:
    Mar 2, 2017
    Posts:
    4
    Yeah I saw there was many errors. It was odd Ubuntu version did not told about them and did run my code.
     
  7. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,342
    Different platforms / graphics drivers have different levels of error handling. Nvidia on Windows is very leanent and even some of the most messily written shaders will still work and usually even with as expected. AMD and Intel on Windows are usually a bit more strict, but sometimes won't throw an error for something that it won't render correctly at all. Mac OSX, iOS and consoles tend to be the most strict. Linux is kind of random since there are so many versions and graphics drivers to choose from and they all behave a little differently.

    As for the original problem I realized you're doing your lighting calculations entirely in the vertex shader. That means the lighting can only be as sharp as the distance between vertices. You need to do your lighting per pixel (in the fragment shader) to get the sharp transitions you want.