Skip to content

Instantly share code, notes, and snippets.

Created February 10, 2013 23:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/4751603 to your computer and use it in GitHub Desktop.
Save anonymous/4751603 to your computer and use it in GitHub Desktop.
A specular "conductivity" shader, with cubemap. Shaderlab + Cg / HLSL for Unity3D.
Shader "Rog/SpecConductCubed" {
Properties {
_Color ("Main Color", Color) = (1,1,1,1)
_FakeLight ("Light Tint", Color) = (1,1,1,1)
_SpecIntensity ("Specular Intensity", Range (0.01,6.0)) = 1.0
_SpecPower ("Specular Power", Range (0.01,1.0)) = 0.078125
_Conduct ("Specular Conductivity", Range (0.0,1.0)) = 0.01
_CubeInf ("CubeMap Influence", Range (0.0,1.0)) = 0.01
_MainTex ("Diffuse (RGB) SpecMap (A)", 2D) = "white" {}
_Cube ("Cubemap", CUBE) = "" {}
}
SubShader {
Tags { "RenderType"="Opaque" }
// LOD 300 // In our game, LOD is disregarded since we're using small enclosed areas.
CGPROGRAM
#pragma surface surf BlinnPhong
#pragma target 3.0
float4 _Color;
float4 _FakeLight;
half _SpecIntensity;
half _SpecPower;
half _Conduct;
half _CubeInf;
sampler2D _MainTex;
samplerCUBE _Cube;
struct Input {
float2 uv_MainTex;
float3 worldRefl;
};
// getAbsColor provided by Teck Lee Tan:
// "This essentially returns the input color at the same saturation and hue, but at maximum value (hsv, v = 1)"
inline float4 getAbsColor(float4 c) {
c += 0.000001; // to prevent divide by zero, and save the need for conditionals
float vMax = max(c.r, max(c.g, c.b));
c = (c / vMax);
return c;
}
void surf (Input IN, inout SurfaceOutput o) {
half4 tex = tex2D(_MainTex, IN.uv_MainTex);
float4 diffcol = tex * _Color; // Combine the diffuse with the main color.
float4 conductcol = lerp(_FakeLight, diffcol, _Conduct);
float4 speccol = lerp(conductcol, getAbsColor(conductcol), _SpecIntensity); // Note: Specularity lerp value can boost above the "absolute" color since intensity goes to 6.
float4 cubetex = texCUBE(_Cube, IN.worldRefl);
float4 cubecol = lerp(cubetex, Luminance(cubetex), _Conduct / 2) * _SpecIntensity; // Conductivity desaturates the cubemap.
_SpecColor = lerp(speccol, cubecol, _CubeInf);
o.Albedo = tex.rgb * _Color.rgb;
o.Gloss = tex.a; // Alpha channel for the gloss map.
o.Specular = _SpecPower;
}
ENDCG
}
FallBack "Diffuse"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment