Search Unity

MatCap shader imitation of blender 2.8s matcap

Discussion in 'Shaders' started by mr-hog, May 24, 2020.

  1. mr-hog

    mr-hog

    Joined:
    Nov 21, 2013
    Posts:
    2
    Hello I'm trying to make a blender2.8 matcap imitation so far its fairly good , the error is in the corners (hopefully i have the image attached correctly , black with white edges) , i don't really know how to fix my mistake the idea is to use a dot product on a surface to eye vector with a tangent of the normals.
    thanks.

    Code (CSharp):
    1. Shader "Unlit/matcatallsurface_vf"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex ("Texture", 2D) = "white" {}
    6.         _MatCap ("matcap",2D) = "black" {}
    7.     }
    8.     SubShader
    9.     {
    10.         Tags { "RenderType"="Opaque" }
    11.         LOD 100
    12.  
    13.         Pass
    14.         {
    15.         Tags { "LightMode" = "Always" }
    16.             CGPROGRAM
    17.             #pragma vertex vert
    18.             #pragma fragment frag
    19.            
    20.  
    21.             #include "UnityCG.cginc"
    22.  
    23.             struct appdata
    24.             {
    25.                 float4 vertex : POSITION;
    26.                 float2 uv : TEXCOORD0;
    27.                 float3 normal : NORMAL;
    28.        
    29.             };
    30.  
    31.             struct v2f
    32.             {
    33.                 float2 uv : TEXCOORD0;
    34.                 UNITY_FOG_COORDS(1)
    35.                 float4 vertex : SV_POSITION;
    36.                 float2 mat : TEXCOORD1;
    37.                
    38.             };
    39.  
    40.             sampler2D _MainTex , _MatCap;
    41.             float4 _MainTex_ST;
    42.            
    43.             v2f vert (appdata v)
    44.             {
    45.                 v2f o;
    46.  
    47.                 float3 worldPos = mul(unity_ObjectToWorld, v.vertex);
    48.                 float3 worldNormal = mul(unity_ObjectToWorld, v.normal);
    49.                 float3 toCam = normalize(_WorldSpaceCameraPos.xyz - worldPos);
    50.  
    51.                 float3 cx = normalize( cross(-worldNormal, UNITY_MATRIX_V[1]) );
    52.                 float3 cy = normalize( cross(-worldNormal, UNITY_MATRIX_V[0]) );
    53.  
    54.                 o.mat = float2( dot(toCam, cx) , dot(toCam, -cy) )*.5+.5;
    55.  
    56.                 o.vertex = UnityObjectToClipPos(v.vertex);
    57.                 o.uv = TRANSFORM_TEX(v.uv, _MainTex);
    58.            
    59.                 return o;
    60.             }
    61.            
    62.             fixed4 frag (v2f i) : SV_Target
    63.             {
    64.                
    65.                 fixed4 col = tex2D(_MainTex, i.uv);
    66.                 fixed4 matcap = tex2D(_MatCap, i.mat.xy);
    67.                 return col *2*matcap;
    68.             }
    69.             ENDCG
    70.         }
    71.     }
    72. }
     

    Attached Files:

  2. mr-hog

    mr-hog

    Joined:
    Nov 21, 2013
    Posts:
    2
    Ok my main problem seems to be in normalizing the cross product instead of getting something like (1,1) getting (.7,.7) for a length of 1,the above code has mat removed form appdata by accident trying to clean up post .
    the odd problem now is serious curvature when looking at a similar surfaces of the viewing angle.
    _Unityshader5.PNG