Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Shader "Car3man/Color Replacer" {
- Properties{
- _Color("Main Color", Color) = (1,1,1,1)
- _SpecColor("Spec Color", Color) = (1,1,1,0)
- _Emission("Emissive Color", Color) = (0,0,0,0)
- _Shininess("Shininess", Range(0.1, 1)) = 0.7
- _MainTex("Base (RGB) Trans (A)", 2D) = "white" {}
- _ColorToReplace("Color To Replace", Color) = (1.0, 0.0, 0.0, 1.0)
- _ReplacementColor("Replacement Color", Color) = (0.0, 0.0, 1.0, 1.0)
- _ColorError("Color Error", Range(0,1)) = 0.1
- _Foil("Foil", Range(0,10)) = 0.1
- }
- SubShader
- {
- Tags{ "Queue" = "Geometry" "IgnoreProjector" = "True" }
- LOD 100
- ZWrite On
- Pass{
- Tags{ LightMode = Vertex }
- CGPROGRAM
- #pragma vertex vert
- #pragma fragment frag
- #include "UnityCG.cginc"
- #define ADD_SPECULAR
- fixed4 _Color;
- fixed4 _SpecColor;
- fixed4 _Emission;
- half _Shininess;
- sampler2D _MainTex;
- float4 _MainTex_ST;
- uniform fixed4 _ColorToReplace;
- uniform fixed4 _ReplacementColor;
- uniform float _ColorError;
- uniform float _Foil;
- struct v2f {
- float4 pos : SV_POSITION;
- float2 uv_MainTex : TEXCOORD0;
- fixed3 diff : COLOR;
- #ifdef ADD_SPECULAR
- fixed3 spec : TEXCOORD1;
- #endif
- };
- v2f vert(appdata_full v)
- {
- v2f o;
- o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
- o.uv_MainTex = TRANSFORM_TEX(v.texcoord, _MainTex);
- float3 viewpos = mul(UNITY_MATRIX_MV, v.vertex).xyz;
- o.diff = UNITY_LIGHTMODEL_AMBIENT.xyz;
- #ifdef ADD_SPECULAR
- o.spec = 0;
- fixed3 viewDirObj = normalize(ObjSpaceViewDir(v.vertex));
- #endif
- //All calculations are in object space
- for (int i = 0; i < 4; i++) {
- half3 toLight = unity_LightPosition[i].xyz - viewpos.xyz * unity_LightPosition[i].w;
- half lengthSq = dot(toLight, toLight);
- half atten = 1.0 / (1.0 + lengthSq * unity_LightAtten[i].z);
- fixed3 lightDirObj = mul((float3x3)UNITY_MATRIX_T_MV, toLight); //View => model
- lightDirObj = normalize(lightDirObj);
- fixed diff = max(0, dot(v.normal, lightDirObj));
- o.diff += unity_LightColor[i].rgb * (diff * atten);
- #ifdef ADD_SPECULAR
- fixed3 h = normalize(viewDirObj + lightDirObj);
- fixed nh = max(0, dot(v.normal, h));
- fixed spec = pow(nh, _Shininess * 128.0) * 0.5;
- o.spec += spec * unity_LightColor[i].rgb * atten;
- #endif
- }
- o.diff = (o.diff * _Color + _Emission.rgb) * 2;
- #ifdef ADD_SPECULAR
- o.spec *= 2 * _SpecColor;
- #endif
- return o;
- }
- fixed4 frag(v2f i) : COLOR{
- fixed4 cc;
- fixed4 mainTex = tex2D(_MainTex, i.uv_MainTex);
- #ifdef ADD_SPECULAR
- cc.rgb = (mainTex.rgb * i.diff + i.spec);
- #else
- cc.rgb = (mainTex.rgb * i.diff);
- #endif
- cc.a = 1;
- float4 c = tex2D(_MainTex, i.uv_MainTex);
- if ((c.r >= _ColorToReplace.r - _ColorError / 2 && c.r <= _ColorToReplace.r + _ColorError / 2)
- && (c.g >= _ColorToReplace.g - _ColorError / 2 && c.g <= _ColorToReplace.g + _ColorError / 2)
- && (c.b >= _ColorToReplace.b - _ColorError / 2 && c.b <= _ColorToReplace.b + _ColorError / 2)) {
- c = (c - _ColorToReplace) * _Foil + _ReplacementColor;
- c.rgb = c.rgb * i.diff + i.spec;
- return c;
- }
- else
- return cc;
- }
- ENDCG
- }
- }
- Fallback "VertexLit"
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement