- Home /
Car shader problem
Hi, I've written my first custom shader (Unity 4.0.0f5, DX11)
Shader "Custom/FerrariShader" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_SpecColor ("Specular Color", Color) = (0.5,0.5,0.5,1)
_Shininess ("Shininess", Range (0.01, 1)) = 0.078125
//_ReflectColor ("Reflection Color", Color) = (1,1,1,0.5)
_MainTex ("Base (RGB) RefStrGloss (A)", 2D) = "white" {}
_Cube ("Reflection Cubemap", Cube) = "" { TexGen CubeReflect }
_BumpMap ("Normalmap", 2D) = "bump" {}
_SpecularMap ("Spec(R) Gloss(G) Reflec(B)", 2D) = "specular" {}
_RimPower ("Rim Power", Range(0.005,8.0)) = 1.0
}
SubShader {
Tags
{
"Queue"="Geometry"
"IgnoreProjector"="False"
"RenderType"="Opaque"
}
LOD 400
Cull Back
ZWrite On
ZTest LEqual
ColorMask RGBA
Fog{
}
CGPROGRAM
#pragma surface surf BlinnPhong
#pragma target 5.0
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _SpecularMap;
samplerCUBE _Cube;
float _RimPower;
fixed4 _Color;
fixed4 _ReflectColor;
half _Shininess;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
float3 worldRefl;
float3 viewDir;
INTERNAL_DATA
};
void surf (Input IN, inout SurfaceOutput o) {
fixed4 tex = tex2D(_MainTex, IN.uv_MainTex);
fixed4 texSpec = tex2D(_SpecularMap, IN.uv_MainTex);
fixed4 c = tex * _Color;
float diffusePower=.3;
float reflectPower=.7;
o.Albedo = c.rgb*diffusePower;
//o.Gloss = texSpec.rgb;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
//o.Specular = texSpec.r*_Shininess;
float3 worldRefl = WorldReflectionVector (IN, o.Normal);
fixed4 reflcol = texCUBE (_Cube, worldRefl);
half rim = 1.0-saturate (dot (normalize(IN.viewDir), o.Normal));
o.Emission = reflcol.rgb* pow(rim,_RimPower/3)*texSpec.b*reflectPower;//* pow(_ReflectColor.rgb,_RimPower);
//o.Emission = Lerp0.rgb;
o.Normal = normalize(o.Normal);
}
ENDCG
}
FallBack "Diffuse"
}
I'm quite satisfied, but when car parts aren't thrown by direction light, car seems to me too dark.
Please give me feedbacks and suggestions !!! Thanks
Answer by hvilela · Nov 10, 2012 at 06:56 PM
You can icrease your ambient ligth (and reduce your directional light to compensate) or you can write your own light model.
Answer by trumanita · Nov 10, 2012 at 07:23 PM
thanks, you pointed me in the right direction: you mean that I should make something like this:
half4 LightingFerrariPhong (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 = .9*s.Albedo+(.1 * _LightColor0.rgb * diff + _LightColor0.rgb * spec) * (atten * 2);
c.a = s.Alpha;
return c;
}
Yes, now it's up to you how to tune the light to active what you expect.
PS: Do not forget to make the shader use your light model.
#pragma surface surf LightingFerrariPhong
Answer by trumanita · Nov 10, 2012 at 07:45 PM
yes, I already added
#pragma surface surf LightingFerrariPhong
darkness problem is solved, I have to tune light thanks!!!!!!!!!!!