Created
February 9, 2013 04:47
-
-
Save anonymous/4743878 to your computer and use it in GitHub Desktop.
Another small update to my specular "conductivity" shader for Unity3D. Still not too complex, but it's getting closer to doing what it's supposed to do.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Shader "Rog/SpecConduct" { | |
Properties { | |
_Color ("Main Color", Color) = (1,1,1,1) | |
_FakeLight ("Fake Light", 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 | |
_MainTex ("Diffuse (RGB) SpecMap (A)", 2D) = "white" {} | |
} | |
SubShader { | |
Tags { "RenderType"="Opaque" } | |
LOD 300 | |
CGPROGRAM | |
#pragma surface surf BlinnPhong | |
#pragma target 3.0 | |
float4 _Color; | |
float4 _FakeLight; | |
half _SpecIntensity; | |
half _SpecPower; | |
half _Conduct; | |
sampler2D _MainTex; | |
struct Input { | |
float2 uv_MainTex; | |
}; | |
// 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; | |
} | |
// Conductivity ranges from no tinting of the incoming light, to a full tint from the diffuse: | |
inline float4 getConductColor(half a, float4 c, float4 d) { | |
c = lerp(d, c, a); | |
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 = getConductColor(_Conduct, diffcol, _FakeLight); | |
_SpecColor = lerp(conductcol, getAbsColor(conductcol), _SpecIntensity); // Note: Specularity lerp value can boost above the "absolute" color. | |
o.Albedo = tex.rgb * _Color.rgb; | |
o.Gloss = 1; | |
o.Specular = _SpecPower; | |
} | |
ENDCG | |
} | |
FallBack "Diffuse" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment