NPR-Hatching素描渲染

来源:互联网 发布:forge软件 编辑:程序博客网 时间:2024/06/02 12:58

Hatching(素描)渲染——通过光照的强弱,用不同深浅的纹理,实现一种类似素描效果。

 

 

Hatching的实现原理是:通过N.L计算点受光的强弱,将该值分成不同的段,不同的光照强度对应不同深浅的素描贴图

 

算法对应的VS如下:

[cpp] view plaincopy
  1. VS_OUTPUT vs_main( VS_INPUT Input )  
  2. {  
  3.    VS_OUTPUT Output;  
  4.   
  5.    Output.Position = mul( Input.Position, matViewProjection );  
  6.    Output.Texcoord = Input.Texcoord;  
  7.      
  8.    float3 normalW = mul( Input.Normal, matView);  
  9.    float diffuse = min( 1, max(0, dot( vecLightDir, normalW)));  
  10.   
  11. //把diffuse平方两次是为了,把亮度降低点,把整体亮度向暗方向偏移,以得到更好的视觉效果。  
  12.    diffuse = diffuse * diffuse;  
  13.    diffuse = diffuse * diffuse;  
  14.      
  15.    float factor = diffuse * 6.0;  
  16. // 通过两个weight,来记录权值,减少传入ps的变量数。  
  17.    float3 weight0 = 0.0;  
  18.    float3 weight1 = 0.0;  
  19.      
  20. // 不同光照强度,对应不同的权值,及不同的纹理贴图  
  21.    if( factor > 5.0)  
  22.    {  
  23.       weight0.x = 1;  
  24.    }  
  25.    else if( factor > 4.0)  
  26.    {  
  27.       weight0.x = factor - 4.0;  
  28.       weight0.y = 1.0 - weight0.x;  
  29.    }  
  30.    else if( factor > 3.0)  
  31.    {  
  32.       weight0.y = factor - 3.0;  
  33.       weight0.z = 1.0 - weight0.y;  
  34.    }  
  35.    else if( factor > 2.0)  
  36.    {  
  37.       weight0.z = factor - 2.0;  
  38.       weight1.x = 1.0 - weight0.z;  
  39.    }  
  40.    else if( factor > 1.0)  
  41.    {  
  42.       weight1.x = factor - 1.0;  
  43.       weight1.y = 1.0 - weight1.x;   
  44.    }  
  45.    else if( factor > 0.0)  
  46.    {  
  47.       weight1.y = factor;  
  48.       weight1.z = 1.0 - weight1.y;  
  49.    }  
  50.    Output.Weight0 = weight0;  
  51.    Output.Weight1 = weight1;  
  52.      
  53.    return( Output );  
  54.      
  55. }  

PS为:

[c-sharp] view plaincopy
  1. struct PS_INPUT  
  2. {  
  3.    float2 tex     : TEXCOORD0;  
  4.    float3 weight0 : TEXCOORD1;  
  5.    float3 weight1 : TEXCOORD2;  
  6. };  
  7.   
  8. float4 ps_main( PS_INPUT input) : COLOR0  
  9. {    
  10.    float4 hattch0 = tex2D(hattch0Map, input.tex)*input.weight0.x;  
  11.    float4 hattch1 = tex2D(hattch1Map, input.tex)*input.weight0.y;  
  12.    float4 hattch2 = tex2D(hattch2Map, input.tex)*input.weight0.z;  
  13.    float4 hattch3 = tex2D(hattch3Map, input.tex)*input.weight1.x;  
  14.    float4 hattch4 = tex2D(hattch4Map, input.tex)*input.weight1.y;  
  15.    float4 hattch5 = tex2D(hattch5Map, input.tex)*input.weight1.z;  
  16.      
  17.    return hattch0 + hattch1 + hattch2  
  18.          + hattch3 + hattch4 + hattch5;  
  19.      
  20. }  

 

最终效果

0 0