轉載請註明出處:http://blog.csdn.net/tianhai110
Hatching(素描)渲染——通過光照的強弱,用不同深淺的紋理,實現一種類似素描效果。
Hatching的實現原理是:通過N.L計算點受光的強弱,將該值分成不同的段,不同的光照強度對應不同深淺的素描貼圖
演算法對應的VS如下:
VS_OUTPUT vs_main( VS_INPUT Input )<br />{<br /> VS_OUTPUT Output;</p><p> Output.Position = mul( Input.Position, matViewProjection );<br /> Output.Texcoord = Input.Texcoord;</p><p> float3 normalW = mul( Input.Normal, matView);<br /> float diffuse = min( 1, max(0, dot( vecLightDir, normalW)));</p><p>//把diffuse平方兩次是為了,把亮度降低點,把整體亮度向暗方向位移,以得到更好的視覺效果。<br /> diffuse = diffuse * diffuse;<br /> diffuse = diffuse * diffuse;</p><p> float factor = diffuse * 6.0;<br />// 通過兩個weight,來記錄權值,減少傳入ps的變數數。<br /> float3 weight0 = 0.0;<br /> float3 weight1 = 0.0;</p><p>// 不同光照強度,對應不同的權值,及不同的紋理貼圖<br /> if( factor > 5.0)<br /> {<br /> weight0.x = 1;<br /> }<br /> else if( factor > 4.0)<br /> {<br /> weight0.x = factor - 4.0;<br /> weight0.y = 1.0 - weight0.x;<br /> }<br /> else if( factor > 3.0)<br /> {<br /> weight0.y = factor - 3.0;<br /> weight0.z = 1.0 - weight0.y;<br /> }<br /> else if( factor > 2.0)<br /> {<br /> weight0.z = factor - 2.0;<br /> weight1.x = 1.0 - weight0.z;<br /> }<br /> else if( factor > 1.0)<br /> {<br /> weight1.x = factor - 1.0;<br /> weight1.y = 1.0 - weight1.x;<br /> }<br /> else if( factor > 0.0)<br /> {<br /> weight1.y = factor;<br /> weight1.z = 1.0 - weight1.y;<br /> }<br /> Output.Weight0 = weight0;<br /> Output.Weight1 = weight1;</p><p> return( Output );</p><p>}<br />
PS為:
struct PS_INPUT<br />{<br /> float2 tex : TEXCOORD0;<br /> float3 weight0 : TEXCOORD1;<br /> float3 weight1 : TEXCOORD2;<br />};</p><p>float4 ps_main( PS_INPUT input) : COLOR0<br />{<br /> float4 hattch0 = tex2D(hattch0Map, input.tex)*input.weight0.x;<br /> float4 hattch1 = tex2D(hattch1Map, input.tex)*input.weight0.y;<br /> float4 hattch2 = tex2D(hattch2Map, input.tex)*input.weight0.z;<br /> float4 hattch3 = tex2D(hattch3Map, input.tex)*input.weight1.x;<br /> float4 hattch4 = tex2D(hattch4Map, input.tex)*input.weight1.y;<br /> float4 hattch5 = tex2D(hattch5Map, input.tex)*input.weight1.z;</p><p> return hattch0 + hattch1 + hattch2<br /> + hattch3 + hattch4 + hattch5;</p><p>}<br />
最終效果