- Home /
Specular problem
Hi, I am having a little trouble with my shader. The problem is that my specular map (stored in the mainTex alpha) has no effect, and the shinyness slider also has no effect. I thought that it because the vertex overlay blend mode is a heavy effect, but I removed a lot of code and still got the same problem (you can see the simpler code below it)
If you have any advice at all it is much appreciated. I'm sure I am just missing something simple.
Here is the code: shader "Custom/constructTex" { Properties{ _MainTex("Diffuse Texture", 2D) = "white"{} _BumpMap("NormalSpec Texture", 2D) = "bump" {} _vertStrength("Vert Color Strength",Range(0.0,1.0)) = 1.0 _Shininess ("Shininess", Range (0.03, 1)) = 0.5 } SubShader{ Tags { "RenderType" = "Opaque"}
CGPROGRAM
#pragma surface surf SpecularLight vertex:vert
sampler2D _MainTex;
sampler2D _BumpMap;
half _vertStrength;
half _Shininess;
//**Lighting Model
half4 LightingSpecularLight (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten){
half3 h = normalize (lightDir + viewDir);
half diff = max (0, dot (s.Normal, lightDir));
float nh = max (0, dot (s.Normal, h));
float spec = pow (nh, 48.0);
half4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * (atten * 2);
c.a = s.Alpha;
return c;
}
struct Input{
float2 uv_MainTex;
float3 vertColors;
};
void vert (inout appdata_full v, out Input o) {
o.vertColors = v.color.rgb;
}
void surf (Input IN, inout SurfaceOutput o) {
half4 tex = tex2D (_MainTex, IN.uv_MainTex);
//**Here we multiply colors less than 0.5, add colors over, and colors in the middle are hue only. Sort of like a more advanced overlay blend.
if(IN.vertColors.r+IN.vertColors.g+IN.vertColors.b>1.5){
tex.rgb = lerp(tex.rgb,clamp(tex.rgb+(2*IN.vertColors.rgb)-1,0.0,1.0),_vertStrength);
}else{
tex.rgb = lerp(tex.rgb,tex.rgb*(2*IN.vertColors.rgb),_vertStrength);
}
half4 normalMap = tex2D (_BumpMap, IN.uv_MainTex);
o.Albedo = tex.rgb;
o.Normal = UnpackNormal (normalMap);
o.Specular = tex.rgb;
o.Gloss = tex.a;
}
ENDCG
}
Fallback "Diffuse"
}
//////////////////////////////////////////////////////////////////////////
//Simplified code
shader "Custom/constructTex"
{
Properties{
_MainTex("Diffuse Texture", 2D) = "white"{}
_BumpMap("NormalSpec Texture", 2D) = "bump" {}
_Shininess ("Shininess", Range (0.03, 1)) = 0.5
}
SubShader{
Tags { "RenderType" = "Opaque"}
CGPROGRAM
#pragma surface surf SimpleSpecular
sampler2D _MainTex;
sampler2D _BumpMap;
half _Shininess;
half4 LightingSimpleSpecular (SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
half3 h = normalize (lightDir + viewDir);
half diff = max (0, dot (s.Normal, lightDir));
float nh = max (0, dot (s.Normal, h));
float spec = pow (nh, 48.0);
half4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * (atten * 2);
c.a = s.Alpha;
return c;
}
struct Input{
float2 uv_MainTex;
};
void surf (Input IN, inout SurfaceOutput o) {
half4 tex = tex2D (_MainTex, IN.uv_MainTex);
half4 normalMap = tex2D (_BumpMap, IN.uv_MainTex);
o.Albedo = tex.rgb;
o.Normal = UnpackNormal (normalMap);
o.Specular = tex.rgb;
o.Gloss = tex.a;
}
ENDCG
}
Fallback "Diffuse"
}
//////////////////////////////////////
Anyone at all? I'm sure I've just missed a space or something but I can't see it :/
Answer by ohlin · Dec 10, 2014 at 01:05 PM
This is old. but you want a specular color in the properties. to go along with your bump map and specular strength slider.
_SpecColor ("Specular Color", Color) = (1,1,1,1)
_SpecPower ("Shinyness", Range(0,1))=0.5
and I just put this for gloss and specular and it worked for me (changed color with reaction to light sources) but Im not using a normal map...so that part idk.
o.Specular = _SpecPower;
o.Gloss = 1.0;
well i hope that was a helpful nudge to whom ever next finds this.