- Home /
Need Help adding Main Color Property to a complicated Shader
Good day!
I have gotten my hands on a great hair shader, the best i've ever seen so far. Now I need to change the main color, but I don't have a clue how this can be done. I googled a lot and tried alot but it seems I'm not capable to get the magic done within this beast of a script.
Shader "Custom/Hair Soft Edge Surface" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_MainTex ("Diffuse (RGB) Alpha (A)", 2D) = "black" {}
_SpecularTex ("Specular (R) Gloss (G) Null (B)", 2D) = "gray" {}
_BumpMap ("Normal (Normal)", 2D) = "bump" {}
_AnisoDirection ("Anisotropic Direction (RGB) Anisotropic Mask (A)", 2D) = "bump" {}
_AnisoOffset ("Anisotropic Highlight Offset", Range(-0.5,0.5)) = -0.2
_Cutoff ("Alpha Cut-Off Threshold", Range(0,1)) = 0.5
_Fresnel ("Fresnel Value", Float) = 0.028
}
SubShader {
Tags { "RenderType" = "TransparentCutout" }
Cull Off
CGPROGRAM
#pragma surface surf ExplorationSoftHair fullforwardshadows exclude_path:prepass nolightmap nodirlightmap
#pragma target 3.0
struct SurfaceOutputHair {
fixed3 Albedo;
fixed Alpha;
fixed3 AnisoDir;
fixed3 Normal;
fixed2 Specular;
fixed3 Emission;
};
struct Input
{
float2 uv_MainTex;
};
sampler2D _MainTex, _SpecularTex, _BumpMap, _AnisoDirection;
float _Cutoff, _AnisoOffset, _Fresnel;
void surf (Input IN, inout SurfaceOutputHair o)
{
float4 albedo = tex2D(_MainTex, IN.uv_MainTex);
clip(albedo.a - _Cutoff);
o.Albedo = albedo.rgb;
o.Alpha = albedo.a;
o.AnisoDir = tex2D(_AnisoDirection, IN.uv_MainTex).rgb * 2 - 1;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
o.Specular = tex2D(_SpecularTex, IN.uv_MainTex).rg;
// Stop DX11 complaining.
o.Emission = fixed3(0.0,0.0,0.0);
}
inline fixed4 LightingExplorationSoftHair (SurfaceOutputHair s, fixed3 lightDir, fixed3 viewDir, fixed atten)
{
viewDir = normalize(viewDir);
lightDir = normalize(lightDir);
s.Normal = normalize(s.Normal);
float NdotL = dot(s.Normal, lightDir);
float3 h = normalize(lightDir + viewDir);
float VdotH = dot( viewDir, h );
float fresnel = pow( 1.0 - VdotH, 5.0 );
fresnel += _Fresnel * ( 1.0 - fresnel );
float aniso = max(0, sin(radians( (dot(normalize(s.Normal + s.AnisoDir), h) + _AnisoOffset) * 180 ) ));
float spec = pow( aniso, s.Specular.g * 128 ) * s.Specular.r * fresnel;
fixed4 c;
c.rgb = (s.Albedo * saturate(NdotL) * atten * _LightColor0.rgb + (spec * atten * _LightColor0.rgb) ) * 2;
c.a = s.Alpha;
return c;
}
ENDCG
ZWrite Off
CGPROGRAM
#pragma surface surf ExplorationSoftHair fullforwardshadows exclude_path:prepass nolightmap nodirlightmap decal:blend
#pragma target 3.0
struct SurfaceOutputHair {
fixed3 Albedo;
fixed Alpha;
fixed3 AnisoDir;
fixed3 Normal;
fixed2 Specular;
fixed3 Emission;
};
struct Input
{
float2 uv_MainTex;
};
sampler2D _MainTex, _SpecularTex, _BumpMap, _AnisoDirection;
float _Cutoff, _AnisoOffset, _Fresnel;
void surf (Input IN, inout SurfaceOutputHair o)
{
float4 albedo = tex2D(_MainTex, IN.uv_MainTex);
clip(-(albedo.a - _Cutoff));
o.Albedo = albedo.rgb;
o.Alpha = albedo.a;
o.AnisoDir = tex2D(_AnisoDirection, IN.uv_MainTex).rgb * 2 - 1;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_MainTex));
o.Specular = tex2D(_SpecularTex, IN.uv_MainTex).rg;
// Stop DX11 complaining.
o.Emission = fixed3(0.0,0.0,0.0);
}
inline fixed4 LightingExplorationSoftHair (SurfaceOutputHair s, fixed3 lightDir, fixed3 viewDir, fixed atten)
{
viewDir = normalize(viewDir);
lightDir = normalize(lightDir);
s.Normal = normalize(s.Normal);
float NdotL = dot(s.Normal, lightDir);
float3 h = normalize(lightDir + viewDir);
float VdotH = dot( viewDir, h );
float fresnel = pow( 1.0 - VdotH, 5.0 );
fresnel += _Fresnel * ( 1.0 - fresnel );
float aniso = max(0, sin(radians( (dot(normalize(s.Normal + s.AnisoDir), h) + _AnisoOffset) * 180 ) ));
float spec = pow( aniso, s.Specular.g * 128 ) * s.Specular.r * fresnel;
fixed4 c;
c.rgb = (s.Albedo * saturate(NdotL) * atten * _LightColor0.rgb + (spec * atten * _LightColor0.rgb) ) * 2;
c.a = s.Alpha;
return c;
}
ENDCG
}
FallBack "Transparent/Cutout/VertexLit"
}
There is already a variable (Main Color) that should alter the main color in the inspector, but it does nothing at all.
Any advice would be most welcome!
Answer by RafalDorsz · Sep 11, 2014 at 11:15 AM
Well,the _Color property is there probably just so the Fallback shader works, because it probably needs it there. What do you want the color to do? If you want it to tint the main texture, you have to add a variable _Color to the shader, something like this, in your variables:
sampler2D _MainTex, _SpecularTex, _BumpMap, _AnisoDirection;
float _Cutoff, _AnisoOffset, _Fresnel;
uniform float4 _Color; // <<<<--- add this
And then in the surface shader, change
float4 albedo = tex2D(_MainTex, IN.uv_MainTex);
into:
float4 albedo = tex2D(_MainTex, IN.uv_MainTex) * _Color;
I didn't have time to figure out exactly what this shader does and where, but this should get you started. Basically add a variable to use the shader property, and then use that variable however you want in the code.
Also, this might help you, if something's not clear: Link!
Hope it helps, good luck
Your answer
Follow this Question
Related Questions
Shader that does per Vert alpha with co 0 Answers
Strange artifacts on Vertex Color Shader 0 Answers
Rewrite simple alphamask fixed function shader to surface or cg shader 2 Answers
Soft edge shader - Issues when objects using same material overlap 0 Answers
Materials are colored differently on rotated objects 1 Answer