Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2017
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.24 KB | None | 0 0
  1. Shader "Car3man/Color Replacer" {
  2. Properties{
  3. _Color("Main Color", Color) = (1,1,1,1)
  4. _SpecColor("Spec Color", Color) = (1,1,1,0)
  5. _Emission("Emissive Color", Color) = (0,0,0,0)
  6. _Shininess("Shininess", Range(0.1, 1)) = 0.7
  7. _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
  8. _ColorToReplace("Color To Replace", Color) = (1.0, 0.0, 0.0, 1.0)
  9. _ReplacementColor("Replacement Color", Color) = (0.0, 0.0, 1.0, 1.0)
  10. _ColorError("Color Error", Range(0,1)) = 0.1
  11. _Foil("Foil", Range(0,10)) = 0.1
  12. }
  13.  
  14. SubShader
  15. {
  16. Tags{ "Queue" = "Geometry" "IgnoreProjector" = "True" }
  17. LOD 100
  18.  
  19. ZWrite On
  20.  
  21. Pass{
  22. Tags{ LightMode = Vertex }
  23.  
  24. CGPROGRAM
  25.  
  26. #pragma vertex vert
  27. #pragma fragment frag
  28.  
  29. #include "UnityCG.cginc"
  30.  
  31. #define ADD_SPECULAR
  32.  
  33. fixed4 _Color;
  34. fixed4 _SpecColor;
  35. fixed4 _Emission;
  36.  
  37. half _Shininess;
  38.  
  39. sampler2D _MainTex;
  40. float4 _MainTex_ST;
  41. uniform fixed4 _ColorToReplace;
  42. uniform fixed4 _ReplacementColor;
  43. uniform float _ColorError;
  44. uniform float _Foil;
  45.  
  46. struct v2f {
  47. float4 pos : SV_POSITION;
  48. float2 uv_MainTex : TEXCOORD0;
  49. fixed3 diff : COLOR;
  50.  
  51. #ifdef ADD_SPECULAR
  52. fixed3 spec : TEXCOORD1;
  53. #endif
  54. };
  55.  
  56. v2f vert(appdata_full v)
  57. {
  58. v2f o;
  59. o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
  60. o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
  61.  
  62. float3 viewpos = mul(UNITY_MATRIX_MV, v.vertex).xyz;
  63.  
  64. o.diff = UNITY_LIGHTMODEL_AMBIENT.xyz;
  65.  
  66. #ifdef ADD_SPECULAR
  67. o.spec = 0;
  68. fixed3 viewDirObj = normalize(ObjSpaceViewDir(v.vertex));
  69. #endif
  70.  
  71. //All calculations are in object space
  72. for (int i = 0; i < 4; i++) {
  73. half3 toLight = unity_LightPosition[i].xyz - viewpos.xyz * unity_LightPosition[i].w;
  74. half lengthSq = dot(toLight, toLight);
  75. half atten = 1.0 / (1.0 + lengthSq * unity_LightAtten[i].z);
  76.  
  77. fixed3 lightDirObj = mul((float3x3)UNITY_MATRIX_T_MV, toLight); //View => model
  78.  
  79. lightDirObj = normalize(lightDirObj);
  80.  
  81. fixed diff = max(0, dot(v.normal, lightDirObj));
  82. o.diff += unity_LightColor[i].rgb * (diff * atten);
  83.  
  84. #ifdef ADD_SPECULAR
  85. fixed3 h = normalize(viewDirObj + lightDirObj);
  86. fixed nh = max(0, dot(v.normal, h));
  87.  
  88. fixed spec = pow(nh, _Shininess * 128.0) * 0.5;
  89. o.spec += spec * unity_LightColor[i].rgb * atten;
  90. #endif
  91.  
  92. }
  93.  
  94. o.diff = (o.diff * _Color + _Emission.rgb) * 2;
  95. #ifdef ADD_SPECULAR
  96. o.spec *= 2 * _SpecColor;
  97. #endif
  98.  
  99. return o;
  100. }
  101.  
  102. fixed4 frag(v2f i) : COLOR{
  103. fixed4 cc;
  104.  
  105. fixed4 mainTex = tex2D(_MainTex, i.uv_MainTex);
  106.  
  107. #ifdef ADD_SPECULAR
  108. cc.rgb = (mainTex.rgb * i.diff + i.spec);
  109. #else
  110. cc.rgb = (mainTex.rgb * i.diff);
  111. #endif
  112.  
  113. cc.a = 1;
  114.  
  115. float4 c = tex2D(_MainTex, i.uv_MainTex);
  116. if ((c.r >= _ColorToReplace.r - _ColorError / 2 && c.r <= _ColorToReplace.r + _ColorError / 2)
  117. && (c.g >= _ColorToReplace.g - _ColorError / 2 && c.g <= _ColorToReplace.g + _ColorError / 2)
  118. && (c.b >= _ColorToReplace.b - _ColorError / 2 && c.b <= _ColorToReplace.b + _ColorError / 2)) {
  119.  
  120. c = (c - _ColorToReplace) * _Foil + _ReplacementColor;
  121. c.rgb = c.rgb * i.diff + i.spec;
  122. return c;
  123. }
  124. else
  125. return cc;
  126. }
  127.  
  128. ENDCG
  129. }
  130. }
  131. Fallback "VertexLit"
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement