码迷,mamicode.com
首页 > 编程语言 > 详细

Unity Shader: Cook-Torrance

时间:2015-08-15 13:26:49      阅读:178      评论:0      收藏:0      [点我收藏+]

标签:

// 原公式来自于 GPU 编程与CG 语言之阳春白雪下里巴人 的 114页的(10-11)。公式中的分母少了个 (N·L),具体看 113 页的公式(10-3)。

1
Shader "Custom/Cook-Torrance" { 2 Properties { 3 _MainTex("Main Tex", 2D) = "white" {} 4 _AmbiColor ("Main Color", Color) = (1, 1, 1, 1) 5 _Ak ("Ambient Coef", float) = 1 6 _DiffColor ("Diff Color", Color) = (1, 1, 1, 1) 7 _Dk ("Diff Coef", float) = 1 8 _SpecColor ("Spec Color", Color) = (1, 1, 1, 1) 9 _Sk ("Sk", float) = 1 10 // _Sp ("Sp", Range(0, 5)) = 1 11 _m ("m", Range(0, 1)) = 0.1 12 _f0 ("f0", Range(0, 1)) = 0.5 13 } 14 15 SubShader { 16 Tags { "RenderType"="Opaque" } 17 LOD 200 18 Pass { 19 CGPROGRAM 20 #include "UnityCG.cginc" 21 #pragma vertex vert 22 #pragma fragment frag 23 24 float4 _AmbiColor; 25 float _Ak; 26 float4 _DiffColor; 27 float _Dk; 28 float4 _SpecColor; 29 float _Sk; 30 // float _Sp; 31 float _m; 32 float _f0; 33 34 sampler2D _MainTex; 35 36 struct v2f { 37 float4 pos : POSITION; 38 float2 uv : TEXCOORD0; 39 float3 light : TEXCOORD1; 40 float3 view : TEXCOORD2; 41 float3 normal : TEXCOORD3; 42 }; 43 44 v2f vert(appdata_base v) { 45 v2f o; 46 o.pos = mul(UNITY_MATRIX_MVP, v.vertex); 47 o.normal = v.normal; 48 o.light = ObjSpaceLightDir(v.vertex); 49 o.view = ObjSpaceViewDir(v.vertex); 50 o.uv = v.texcoord; 51 52 return o; 53 } 54 55 float4 frag(v2f i) : COLOR { 56 float3 l = normalize(i.light); 57 float3 v = normalize(i.view); 58 float3 n = normalize(i.normal); 59 float3 h = normalize(l + v); 60 61 float vh = dot(v, h); 62 float nh = dot(n, h); 63 float nl = dot(n, l); 64 float nv = dot(n, v); 65 66 float4 t = tex2D(_MainTex, i.uv); 67 68 float4 ambi = _AmbiColor * _Ak; 69 float4 diff = _DiffColor * _Dk * saturate(nl); 70 71 if (nh <= 0 || nl <= 0) 72 return t * ambi + diff; 73 74 float F = _f0 + (1 - _f0) * (1 - pow(vh, 5)); 75 float D = exp((nh * nh - 1) / (_m * _m * nh * nh)) / (_m * _m * pow(nh, 4)); 76 float G = min(1, min(2 * nh * nl / vh, 2 * nh * nv / vh)); 77 78 float R = F * D * G / (nv * nl); 79 80 float spec = _SpecColor * _Sk * R * nl; 81 82 return t * ambi + diff + spec; 83 } 84 85 ENDCG 86 } 87 } 88 FallBack "Diffuse" 89 }

 效果如下:

技术分享

Unity Shader: Cook-Torrance

标签:

原文地址:http://www.cnblogs.com/foxianmo/p/4732054.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!