Skip to content

Instantly share code, notes, and snippets.

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 smkplus/17fea90de9215b1f8e1085912d9e11dc to your computer and use it in GitHub Desktop.
Save smkplus/17fea90de9215b1f8e1085912d9e11dc to your computer and use it in GitHub Desktop.
Shader "Custom/Cook-Torrance" {
Properties {
_Color ("Color", Color) = (1,1,1,1)
_MainTex ("Albedo (RGB)", 2D) = "white" {}
_BumpMap("Normal Map", 2D) = "bump" {}
_Metallic("Metallic", Range(0,1)) = 0.5
_MetallicTex("Metallic", 2D) = "white" {}
_Roughness("Roughness", Range(0.000000001,1)) = 0.5
_RoughnessTex("Roughness", 2D) = "white" {}
_Fresnel("Fresnel Value", Float) = 0.028
_Cutoff("Alpha Cut-Off Threshold", Range(0,1)) = 0.5
}
SubShader {
Tags { "RenderType"="TransparentCutout" }
LOD 200
CGPROGRAM
// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf CookTorrance exclude_path:prepass nolightmap nodirlightmap fullforwardshadows
// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0
sampler2D _MainTex;
sampler2D _MetallicTex;
sampler2D _RoughnessTex;
sampler2D _BumpMap;
struct Input {
float2 uv_MainTex;
float2 uv_BumpMap;
};
struct SurfaceOutputCookTorrance {
half3 Albedo;
half3 Normal;
half3 Emission;
half Metallic;
half Roughness;
half Alpha;
};
half4 _Color;
float _Fresnel, _Cutoff, _Metallic, _Roughness;
half4 LightingCookTorrance(SurfaceOutputCookTorrance s, half3 lightDir, half3 viewDir, half atten)
{
clip(s.Alpha - _Cutoff);
float roughnessValue = s.Roughness;
float F0 = _Fresnel;
float k = s.Metallic;
half3 lightColor = _LightColor0.rgb;
half3 lightDirection = normalize(lightDir);
half NdotL = max(dot(s.Normal, lightDirection), 0.0);
half specular = 0.0;
if(NdotL > 0.0)
{
half3 eyeDir = normalize(viewDir);
// intermediary values
half3 halfVector = normalize(lightDirection + eyeDir);
half NdotH = max(dot(s.Normal, halfVector), 0.0);
half NdotV = max(dot(s.Normal, eyeDir), 0.0);
half VdotH = max(dot(eyeDir, halfVector), 0.0);
half mSquared = roughnessValue * roughnessValue;
// geometric attenuation
half NH2 = 2.0 * NdotH;
half g1 = (NH2 * NdotV) / VdotH;
half g2 = (NH2 * NdotL) / VdotH;
half geoAtt = min(1.0, min(g1, g2));
// roughness (or: microfacet distribution function)
// beckmann distribution function
half r1 = 1.0 / ( 4.0 * mSquared * pow(NdotH, 4.0));
half r2 = (NdotH * NdotH - 1.0) / (mSquared * NdotH * NdotH);
half roughness = r1 * exp(r2);
// fresnel
// Schlick approximation
half fresnel = pow(1.0 - VdotH, 5.0);
fresnel *= (1.0 - F0);
fresnel += F0;
specular = (fresnel * geoAtt * roughness) / (NdotV * NdotL * 3.14);
}
half3 finalValue = lightColor * NdotL * (k + specular * (1.0 - k));
return float4(finalValue, 1.0);
}
void surf (Input IN, inout SurfaceOutputCookTorrance o) {
// Albedo comes from a texture tinted by color
half4 albedo = tex2D(_MainTex, IN.uv_MainTex) * _Color;
o.Albedo = albedo.rgb;
// Metallic and smoothness come from slider variables
o.Roughness = tex2D(_RoughnessTex, IN.uv_MainTex).r * _Roughness;
o.Metallic = tex2D(_MetallicTex, IN.uv_MainTex).r * _Metallic;
o.Normal = UnpackNormal(tex2D(_BumpMap, IN.uv_BumpMap));
o.Alpha = albedo.a;
}
ENDCG
}
FallBack "Transparent/Cutout/VertexLit"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment