- Home /
Specular from alpha in custom shader
Hi, I've been learning about writing my own shaders, particularly from Kenneth Lemmers youtube tutorials which are great. I currently have a fake BRDF shader with diffuse and normal map included. But I'm having trouble adding specular from the diffuse texture's alpha. I know how to do it using the BlinnPhong surface but unsure with the current code I have here. I've removed any lines that attempt to use specular in the hopes to hopefully prevent confusion. :)
Shader "Custom/PP_BRDF"
{
Properties
{
_ColorTint ("Colour Tint", Color) = (125,125,125,1)
_MainTex ("Albedo", 2D) = "white" {}
_BumpMap ("Normal Map", 2D) = "bump"{}
_Ramp2D ("BRDF Ramp", 2D) = "gray" {}
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 200
CGPROGRAM
#pragma surface surf Ramp
#pragma target 3.0
float4 _ColorTint;
sampler2D _MainTex;
sampler2D _BumpMap;
sampler2D _Ramp2D;
struct Input
{
float4 color : Color;
float2 uv_MainTex;
float2 uv_BumpMap;
};
half4 LightingRamp (SurfaceOutput s,half3 lightDir, half3 viewDir, half atten)
{
float NdotL = dot(s.Normal, lightDir);
float NdotE = dot(s.Normal, viewDir);
//Diffuse Wrap
float diff = (NdotL * 0.5) + 0.5;
float2 brdfUV = float2(NdotE, diff);
float3 BRDF = tex2D(_Ramp2D, brdfUV.xy).rgb;
float4 c;
c.rgb = BRDF;
c.a = s.Alpha;
return c;
}
void surf (Input IN, inout SurfaceOutput o)
{
half4 c = float4(0.5,0.5,0.5,1)/ tex2D (_MainTex, IN.uv_MainTex)/2;
IN.color = _ColorTint;
o.Albedo = tex2D(_MainTex, IN.uv_MainTex) * IN.color/c.rgb * 2;
o.Alpha = c.a;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
}
ENDCG
}
FallBack "Diffuse"
}
Answer by nsxdavid · Feb 25, 2013 at 07:23 AM
Roughly, I think what you want is this:
half4 texColor = tex2D(_MainTex,IN.uv_MainTex);
o.Specular = texColor.a
Hey nsxdavid, thanks for giving it a shot. Unfortunately that alone only works if using a BlinnPhong Surface. Lighting.cginc has the code the inbuilt BlinnPhong reads, I'm unsure how to transfer that into the custom shader.
Your answer
Follow this Question
Related Questions
How do i remove specular/fresnel effect?,How do I remove fresnel or specular effect? 0 Answers
Why does the 3D Text shader show specular at certain angles? 2 Answers
How to make an unlit shader use spec and normal maps? 2 Answers
Custom specular reflection cubemap Unity script throwing errors. 0 Answers
Specular problem 1 Answer