- Home /
Unity Shader: Specular reflection as Alpha Value.
Hello, I am currently trying to learn shader programming. I wanted to test out several shaders by their functionality only. I want to give my object several materials with one functionality only. For example: 1) Diffuse(Base) 2) Specularity only (+BumMap) 3) Rim-Shader
I am not sure if this is more expensive than normal materials, but it allows a greater flexibility. Plus in case of learning shaders itd be a great opportunity if i knew how to access the reflected specular information.
I need to somehow get access to the lightDir and viewDir, multiply it with the specular map, specular color and light color and then store it in a texture to apply it to the o.Alpha. Thats what I think. Now I need your help.
How do I store the spec variable into a 2D texture to use as alpha map. All I found was this part where the light and viewDir were used to calc the specularity...
inline half4 LightingColoredSpecular (MySurfaceOutput 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, 32.0);
half3 specCol = spec * s.GlossColor;
half4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * specCol) * (atten * 2);
c.a = s.Alpha;
return c;
}
Here is my current working code without the working alpha specularity: How can I use the specular reflection as alpha values???
////////////////////////
Shader "Mine/Specular/BumpedSpecular" {
Properties {
_MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {}
_BumpMap ("Normalmap", 2D) = "bump" {}
_SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
_Intensity ("Intensity", Range (0.01, 3)) = 1
}
SubShader {
Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
LOD 600
CGPROGRAM
#pragma surface surf BlinnPhong alpha
#pragma exclude_renderers flash
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _AlphaMap;
half _Shininess;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
};
struct MySurfaceOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half Specular;
half3 GlossColor;
half Alpha;
};
float _Intensity;
void surf (Input IN, inout SurfaceOutput o) {
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
o.Gloss = pow( (tex.r + tex.g + tex.b)/3 , _Intensity )*_Intensity ;
o.Specular = _Shininess;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
o.Alpha = o.Gloss;
}
ENDCG
}
FallBack "Diffuse"
}
Answer by Katatafisch · Aug 03, 2012 at 04:56 PM
Hey Scroodge, thanks so much for your reply.
Maybe I was a but too vague or confusing with what I want.
I want a shader which shows ONLY the specularity. Where there is no light reflected, the alpha should be 0. Where there is light reflected, the alpha should be between 0 and 1 according to the strength of the specular reflection.
Therefore I think I do not need to apply the alpha to the specular. I think I need to apply the specular information to the alpha. But none of my tests worked well. I think I somehow need to get the information of the lightDir and viewDir to know where there is specular reflection on the surface and where not.
Like in this image on the third image in the top-row. Everything else is transparent and where there is reflection/specularity its not transparent.
PS if you need ONLY specular power like at top-right image, shader can be cheaper a lot. i just improved your one.
Wow, thanks alot for your time! Did this work for you? I applied your shader, but nothing shows. I am trying to figure a way to get a simple and cheap shader that draws only the specularity. But it seems i do not understand shaders well enough to come up with a proper script. Do you have an idea how to write such a shader?
EDIT: I changed your shader a bit and replaced this line: specPower = _LightColor0.a _SpecColor.a spec (atten); by this specPower = _LightColor0.rgb _SpecColor.rgb spec (atten*2);
Now it does what I wanted! Thanks.
If we now find a way to make it cheaper, it'd be great!
describe exactly what you need on this shader output? RGB=0 and A=calculated specular intensity at point?
Answer by ScroodgeM · Aug 02, 2012 at 09:45 PM
Shader "Mine/Specular/BumpedSpecular" { Properties { _MainTex ("Base (RGB) TransGloss (A)", 2D) = "white" {} _BumpMap ("Normalmap", 2D) = "bump" {} _SpecColor ("Specular Color", Color) = (0.5, 0.5, 0.5, 0) _Shininess ("Shininess", Range (0.01, 1)) = 0.078125 _Intensity ("Intensity", Range (0.01, 3)) = 1 } SubShader { Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"} LOD 600
CGPROGRAM
#pragma surface surf BlinnPhong2 alpha finalcolor:mycolor
#pragma exclude_renderers flash
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _AlphaMap;
half _Shininess;
struct Input
{
float2 uv_MainTex;
float2 uv_BumpMap;
};
struct MySurfaceOutput
{
half3 Albedo;
half3 Normal;
half3 Emission;
half Specular;
half3 GlossColor;
half Alpha;
};
float _Intensity;
void surf (Input IN, inout SurfaceOutput o)
{
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
o.Gloss = pow( (tex.r + tex.g + tex.b)/3 , _Intensity )*_Intensity ;
o.Specular = _Shininess;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
o.Alpha = o.Gloss;
}
float specPower;
inline fixed4 LightingBlinnPhong2 (SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
{
half3 h = normalize (lightDir + viewDir);
fixed diff = max (0, dot (s.Normal, lightDir));
float nh = max (0, dot (s.Normal, h));
float spec = pow (nh, s.Specular*128.0) * s.Gloss;
fixed4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten * 2);
specPower = _LightColor0.a * _SpecColor.a * spec * atten;
c.a = s.Alpha + specPower;
return c;
}
void mycolor (Input IN, SurfaceOutput o, inout fixed4 color)
{
color.a = specPower;
return;
}
ENDCG
}
FallBack "Diffuse"
}
Your answer
Follow this Question
Related Questions
How to make a specular shader fully transparent 0 Answers
Glass Shader with Strumpy Shader Editor 1 Answer
About shader! 0 Answers
Shader alpha setting being ignored? 1 Answer
specular shader alpha not working? 0 Answers