CharaOutline.cg 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. // Outline shader
  3. // Material parameters
  4. float4 _Color;
  5. float4 _LightColor0;
  6. float _EdgeThickness = 1.0;
  7. float4 _MainTex_ST;
  8. // Textures
  9. sampler2D _MainTex;
  10. // Structure from vertex shader to fragment shader
  11. struct v2f
  12. {
  13. float4 pos : SV_POSITION;
  14. float2 uv : TEXCOORD0;
  15. };
  16. // Float types
  17. #define float_t half
  18. #define float2_t half2
  19. #define float3_t half3
  20. #define float4_t half4
  21. // Outline thickness multiplier
  22. #define INV_EDGE_THICKNESS_DIVISOR 0.00285
  23. // Outline color parameters
  24. #define SATURATION_FACTOR 0.6
  25. #define BRIGHTNESS_FACTOR 0.8
  26. // Vertex shader
  27. v2f vert( appdata_base v )
  28. {
  29. v2f o;
  30. o.uv = TRANSFORM_TEX( v.texcoord.xy, _MainTex );
  31. half4 projSpacePos = UnityObjectToClipPos( v.vertex );
  32. half4 projSpaceNormal = normalize( UnityObjectToClipPos( half4( v.normal, 0 ) ) );
  33. half4 scaledNormal = _EdgeThickness * INV_EDGE_THICKNESS_DIVISOR * projSpaceNormal; // * projSpacePos.w;
  34. scaledNormal.z += 0.00001;
  35. o.pos = projSpacePos + scaledNormal;
  36. return o;
  37. }
  38. // Fragment shader
  39. float4 frag( v2f i ) : COLOR
  40. {
  41. float4_t diffuseMapColor = tex2D( _MainTex, i.uv );
  42. float_t maxChan = max( max( diffuseMapColor.r, diffuseMapColor.g ), diffuseMapColor.b );
  43. float4_t newMapColor = diffuseMapColor;
  44. maxChan -= ( 1.0 / 255.0 );
  45. float3_t lerpVals = saturate( ( newMapColor.rgb - float3( maxChan, maxChan, maxChan ) ) * 255.0 );
  46. newMapColor.rgb = lerp( SATURATION_FACTOR * newMapColor.rgb, newMapColor.rgb, lerpVals );
  47. return float4( BRIGHTNESS_FACTOR * newMapColor.rgb * diffuseMapColor.rgb, diffuseMapColor.a ) * _Color * _LightColor0;
  48. }