MatCap_TextureMult_Bumped.shader 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'
  2. // MatCap Shader, (c) 2015-2017 Jean Moreno
  3. Shader "MatCap/Bumped/Textured Multiply"
  4. {
  5. Properties
  6. {
  7. _MainTex ("Base (RGB)", 2D) = "white" {}
  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 : TEXCOORD0;
  29. float2 uv_bump : TEXCOORD1;
  30. #if MATCAP_ACCURATE
  31. fixed3 tSpace0 : TEXCOORD2;
  32. fixed3 tSpace1 : TEXCOORD3;
  33. fixed3 tSpace2 : TEXCOORD4;
  34. UNITY_FOG_COORDS(5)
  35. #else
  36. float3 c0 : TEXCOORD2;
  37. float3 c1 : TEXCOORD3;
  38. UNITY_FOG_COORDS(4)
  39. #endif
  40. };
  41. uniform float4 _MainTex_ST;
  42. uniform float4 _BumpMap_ST;
  43. v2f vert (appdata_tan v)
  44. {
  45. v2f o;
  46. o.pos = UnityObjectToClipPos (v.vertex);
  47. o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
  48. o.uv_bump = TRANSFORM_TEX(v.texcoord,_BumpMap);
  49. #if MATCAP_ACCURATE
  50. //Accurate bump calculation: calculate tangent space matrix and pass it to fragment shader
  51. fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
  52. fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
  53. fixed3 worldBinormal = cross(worldNormal, worldTangent) * v.tangent.w;
  54. o.tSpace0 = fixed3(worldTangent.x, worldBinormal.x, worldNormal.x);
  55. o.tSpace1 = fixed3(worldTangent.y, worldBinormal.y, worldNormal.y);
  56. o.tSpace2 = fixed3(worldTangent.z, worldBinormal.z, worldNormal.z);
  57. #else
  58. //Faster but less accurate method (especially on non-uniform scaling)
  59. v.normal = normalize(v.normal);
  60. v.tangent = normalize(v.tangent);
  61. TANGENT_SPACE_ROTATION;
  62. o.c0 = mul(rotation, normalize(UNITY_MATRIX_IT_MV[0].xyz));
  63. o.c1 = mul(rotation, normalize(UNITY_MATRIX_IT_MV[1].xyz));
  64. #endif
  65. UNITY_TRANSFER_FOG(o, o.pos);
  66. return o;
  67. }
  68. uniform sampler2D _MainTex;
  69. uniform sampler2D _BumpMap;
  70. uniform sampler2D _MatCap;
  71. fixed4 frag (v2f i) : COLOR
  72. {
  73. fixed4 tex = tex2D(_MainTex, i.uv);
  74. fixed3 normals = UnpackNormal(tex2D(_BumpMap, i.uv_bump));
  75. #if MATCAP_ACCURATE
  76. //Rotate normals from tangent space to world space
  77. float3 worldNorm;
  78. worldNorm.x = dot(i.tSpace0.xyz, normals);
  79. worldNorm.y = dot(i.tSpace1.xyz, normals);
  80. worldNorm.z = dot(i.tSpace2.xyz, normals);
  81. worldNorm = mul((float3x3)UNITY_MATRIX_V, worldNorm);
  82. float4 mc = tex2D(_MatCap, worldNorm.xy * 0.5 + 0.5) * tex * 2.0;
  83. #else
  84. half2 capCoord = half2(dot(i.c0, normals), dot(i.c1, normals));
  85. float4 mc = tex2D(_MatCap, capCoord*0.5+0.5) * tex * 2.0;
  86. #endif
  87. UNITY_APPLY_FOG(i.fogCoord, mc);
  88. return mc;
  89. }
  90. ENDCG
  91. }
  92. }
  93. Fallback "VertexLit"
  94. }