Skip to content

Instantly share code, notes, and snippets.

@tgjones
Created December 22, 2010 13:44
Show Gist options
  • Save tgjones/751513 to your computer and use it in GitHub Desktop.
Save tgjones/751513 to your computer and use it in GitHub Desktop.
Surface shaders using StitchUp
// Normal-Diffuse.shader
surface Diffuse;
[params]
float4 Color : DIFFUSE_COLOR = float4(1, 1, 1, 1);
[textures]
Texture2D MainTex : DIFFUSE_TEXTURE;
[vertex]
float2 uv;
[interpolators]
float2 uv;
[lightingmodel Lambert]
[ps]
__hlsl__
void surface(INPUT input, inout LambertSurfaceOutput output)
{
float4 c = tex2D(MainTex, input.uv) * Color;
output.Diffuse = c.rgb;
output.Alpha = c.a;
}
__hlsl__
// LightingModels\Lambert.fragment
fragment Lambert;
[headercode]
__hlsl__
struct LambertSurfaceOutput
{
float3 Diffuse;
float3 Normal;
float Alpha;
};
float4 LightingLambert(LambertSurfaceOutput s, Light l, float attenuation, float3 viewDirection)
{
float diffuse = max(0, dot(s.Normal, l.DirectionToLight));
float4 c;
c.rgb = s.Diffuse * l.Color.rgb * (diffuse * attenuation * 2);
c.a = s.Alpha;
return c;
}
__hlsl__
// Normal-Specular.shader
surface Specular;
[params]
float4 DiffuseColor : DIFFUSE_COLOR = float4(1, 1, 1, 1);
float4 SpecularColor : SPECULAR_COLOR = float4(0.5, 0.5, 0.5, 1);
float SpecularPower : SPECULAR_POWER;
[textures]
Texture2D MainTex : DIFFUSE_TEXTURE;
[vertex]
float2 uv;
[interpolators]
float2 uv;
[lightingmodel BlinnPhong]
[ps]
__hlsl__
void surface(INPUT input, inout BlinnPhongSurfaceOutput output)
{
float4 c = tex2D(MainTex, input.uv) * DiffuseColor;
output.Diffuse = c.rgb;
output.Alpha = c.a;
output.Specular = SpecularColor;
output.SpecularPower = SpecularPower;
}
__hlsl__
// LightingModels\BlinnPhong.fragment
fragment BlinnPhong;
[headercode]
__hlsl__
struct BlinnPhongSurfaceOutput
{
float3 Diffuse;
float3 Normal;
float3 Specular;
float SpecularPower;
float Alpha;
};
float4 LightingBlinnPhong(BlinnPhongSurfaceOutput s, Light l, float attenuation, float3 viewDirection)
{
float3 half = normalize(l.DirectionToLight + viewDirection);
float diffuse = max(0, dot(s.Normal, l.DirectionToLight));
float normalHalf = max(0.0001f, dot(s.Normal, half));
float specular = pow(normalHalf, s.SpecularPower);
float4 c;
c.rgb = (l.Color * s.Diffuse * diffuse + l.Color.rgb * s.Specular.rgb * specular) * (attenuation * 2);
c.a = s.Alpha + l.Color.a * specular * attenuation;
return c;
}
__hlsl__
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment