MatCap_Plain_Bumped.shader 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. // MatCap Shader, (c) 2015-2017 Jean Moreno
  3. Shader "MatCap/Bumped/Plain"
  4. {
  5. Properties
  6. {
  7. _Color ("Main Color", Color) = (0.5,0.5,0.5,1)
  8. _BumpMap ("Normal Map", 2D) = "bump" {}
  9. _MatCap ("MatCap (RGB)", 2D) = "white" {}
  10. [Toggle(MATCAP_ACCURATE)] _MatCapAccurate ("Accurate Calculation", Int) = 0
  11. }
  12. Subshader
  13. {
  14. Tags { "RenderType"="Opaque" }
  15. Pass
  16. {
  17. Tags { "LightMode" = "Always" }
  18. CGPROGRAM
  19. #pragma vertex vert
  20. #pragma fragment frag
  21. #pragma fragmentoption ARB_precision_hint_fastest
  22. #pragma shader_feature MATCAP_ACCURATE
  23. #pragma multi_compile_fog
  24. #include "UnityCG.cginc"
  25. struct v2f
  26. {
  27. float4 pos : SV_POSITION;
  28. float2 uv_bump : TEXCOORD0;
  29. #if MATCAP_ACCURATE
  30. fixed3 tSpace0 : TEXCOORD1;
  31. fixed3 tSpace1 : TEXCOORD2;
  32. fixed3 tSpace2 : TEXCOORD3;
  33. UNITY_FOG_COORDS(4)
  34. #else
  35. float3 c0 : TEXCOORD1;
  36. float3 c1 : TEXCOORD2;
  37. UNITY_FOG_COORDS(3)
  38. #endif
  39. };
  40. uniform float4 _BumpMap_ST;
  41. v2f vert (appdata_tan v)
  42. {
  43. v2f o;
  44. o.pos = UnityObjectToClipPos (v.vertex);
  45. o.uv_bump = TRANSFORM_TEX(v.texcoord,_BumpMap);
  46. #if MATCAP_ACCURATE
  47. //Accurate bump calculation: calculate tangent space matrix and pass it to fragment shader
  48. fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
  49. fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
  50. fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
  51. o.tSpace0 = fixed3(worldTangent.x, worldBinormal.x, worldNormal.x);
  52. o.tSpace1 = fixed3(worldTangent.y, worldBinormal.y, worldNormal.y);
  53. o.tSpace2 = fixed3(worldTangent.z, worldBinormal.z, worldNormal.z);
  54. #else
  55. //Faster but less accurate method (especially on non-uniform scaling)
  56. v.normal = normalize(v.normal);
  57. v.tangent = normalize(v.tangent);
  58. TANGENT_SPACE_ROTATION;
  59. o.c0 = mul(rotation, normalize(UNITY_MATRIX_IT_MV[0].xyz));
  60. o.c1 = mul(rotation, normalize(UNITY_MATRIX_IT_MV[1].xyz));
  61. #endif
  62. UNITY_TRANSFER_FOG(o, o.pos);
  63. return o;
  64. }
  65. uniform float4 _Color;
  66. uniform sampler2D _MatCap;
  67. uniform sampler2D _BumpMap;
  68. float4 frag (v2f i) : COLOR
  69. {
  70. fixed3 normals = UnpackNormal(tex2D(_BumpMap, i.uv_bump));
  71. #if MATCAP_ACCURATE
  72. //Rotate normals from tangent space to world space
  73. float3 worldNorm;
  74. worldNorm.x = dot(i.tSpace0.xyz, normals);
  75. worldNorm.y = dot(i.tSpace1.xyz, normals);
  76. worldNorm.z = dot(i.tSpace2.xyz, normals);
  77. worldNorm = mul((float3x3)UNITY_MATRIX_V, worldNorm);
  78. float4 mc = tex2D(_MatCap, worldNorm.xy * 0.5 + 0.5) * _Color * 2.0;
  79. #else
  80. half2 capCoord = half2(dot(i.c0, normals), dot(i.c1, normals));
  81. float4 mc = tex2D(_MatCap, capCoord*0.5+0.5) * _Color * 2.0;
  82. #endif
  83. UNITY_APPLY_FOG(i.fogCoord, mc);
  84. return mc;
  85. }
  86. ENDCG
  87. }
  88. }
  89. Fallback "VertexLit"
  90. }