- Home /
Question by
AdamRV · Dec 27, 2012 at 08:55 AM ·
shadermaterialtransparencytransparentshading
Adding Transparency to a Diffuse / Spec / Normal Shader
Hello. I'm not very experienced with shaders, but I've been stuck trying to simply allow a mesh to be transparent while allowing Diffuse, Specular and Normal textures attached. Any help would be appreciated. Here's the code of the shader I'm working with. Thank you.
Shader "NormalMap"
{
Properties
{
_Color ("Main Color", Color) = (1,1,1,1)
_Diffuse("_Diffuse", 2D) = "gray" {}
_Normal("_Normal", 2D) = "bump" {}
_Specular("_Specular", 2D) = "gray" {}
_Shininess ("Shininess", Range (0.01, 1)) = 0.01
_Brightness ("Brightness", Range (0.01, 1)) = 0.01
}
SubShader
{
Tags
{
"Queue"="Geometry"
"IgnoreProjector"="False"
"RenderType"="Opaque"
}
Cull Back
ZWrite On
ZTest LEqual
ColorMask RGBA
Fog{
}
CGPROGRAM
// Upgrade NOTE: excluded shader from Xbox360 and OpenGL ES 2.0 because it uses unsized arrays
#pragma exclude_renderers xbox360 gles
#pragma surface surf BlinnPhongEditor vertex:vert
#pragma target 2.0
sampler2D _Diffuse;
sampler2D _Normal;
sampler2D _Specular;
float4 _Color;
float _Shininess;
float _Brightness;
struct EditorSurfaceOutput {
half3 Albedo;
half3 Normal;
half3 Emission;
half3 Gloss;
half Specular;
half Alpha;
half4 Custom;
};
inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light)
{
half3 spec = light.a * s.Gloss;
half4 c;
c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
c.a = s.Alpha;
return c;
}
inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten)
{
s.Alpha = _Color.a;
half3 h = normalize (lightDir + viewDir);
half diff = max (0, dot ( lightDir, s.Normal ));
float nh = max (0, dot (s.Normal, h));
float spec = pow (nh, s.Specular*128.0);
half4 res;
res.rgb = _LightColor0.rgb * diff * _Color;
res.a = _Color.a;
res.w = spec * Luminance (_LightColor0.rgb);
res *= atten * 2.0;
return LightingBlinnPhongEditor_PrePass( s, res );
}
inline half4 LightingBlinnPhongEditor_DirLightmap (EditorSurfaceOutput s, fixed4 color, fixed4 scale, half3 viewDir, bool surfFuncWritesNormal, out half3 specColor)
{
UNITY_DIRBASIS
half3 scalePerBasisVector;
half3 lm = DirLightmapDiffuse (unity_DirBasis, color, scale, s.Normal, surfFuncWritesNormal, scalePerBasisVector);
half3 lightDir = normalize (scalePerBasisVector.x * unity_DirBasis[0] + scalePerBasisVector.y * unity_DirBasis[1] + scalePerBasisVector.z * unity_DirBasis[2]);
half3 h = normalize (lightDir + viewDir);
float nh = max (0, dot (s.Normal, h));
float spec = pow (nh, s.Specular * 128.0);
// specColor used outside in the forward path, compiled out in prepass
specColor = lm * _SpecColor.rgb * s.Gloss * spec;
// spec from the alpha component is used to calculate specular
// in the Lighting*_Prepass function, it's not used in forward
return half4(lm, spec);
}
struct Input {
float2 uv_Diffuse;
float2 uv_Normal;
float2 uv_Specular;
};
void vert (inout appdata_full v, out Input o) {
float4 VertexOutputMaster0_0_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_1_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_2_NoInput = float4(0,0,0,0);
float4 VertexOutputMaster0_3_NoInput = float4(0,0,0,0);
}
void surf (Input IN, inout EditorSurfaceOutput o) {
o.Normal = float3(0.0,0.0,1.0);
o.Alpha = _Color.a;
o.Albedo = 0.0;
o.Emission = _Brightness;
o.Gloss = 1.0;
o.Specular = _Shininess;
o.Custom = 0.0;
float4 Tex2D1=tex2D(_Diffuse,(IN.uv_Diffuse.xyxy).xy);
float4 Tex2D0=tex2D(_Normal,(IN.uv_Normal.xyxy).xy);
float4 UnpackNormal0=float4(UnpackNormal(Tex2D0).xyz, 1.0);
float4 Tex2D2=tex2D(_Specular,(IN.uv_Specular.xyxy).xy);
float4 Master0_2_NoInput = float4(0,0,0,0);
float4 Master0_3_NoInput = float4(0,0,0,0);
float4 Master0_5_NoInput = float4(1,1,1,1);
float4 Master0_7_NoInput = float4(0,0,0,0);
float4 Master0_6_NoInput = float4(1,1,1,1);
o.Albedo = Tex2D1.rgba * _Color;
o.Normal = UnpackNormal0;
o.Gloss = Tex2D2;
o.Normal = normalize(o.Normal);
}
ENDCG
}
Fallback "Diffuse"
}
Comment