纳金网

标题: 深度格子Shader [打印本页]

作者: 王者再临    时间: 2014-2-23 01:48
标题: 深度格子Shader
  1. Shader "quad_depth_relief_mapping"
  2. {
  3. Properties
  4. {
  5. depth ("Depth Factor", Float) = 0.1
  6. tile ("Tile Factor", Float) = 1

  7. ambient_color ("Ambient",  Color) = (0.2, 0.2, 0.2)
  8. diffuse_color ("Diffuse",  Color) = (1, 1, 1)
  9. specular_color ("Specular", Color) = (0.75,0.75,0.75)

  10. shine ("Shine", Float) = 128

  11. lightpos ("Light Position (view space)", Vector) = (100.0, -50.0, 50.0)

  12. color_map ("Color Map" ,2D) = "white" {}
  13. quad_depth_map ("Quad Depth Map",2D) = "white" {}
  14. normal_map_x ("Normal Map X" ,2D) = "white" {}
  15. normal_map_y ("Normal Map Y" ,2D) = "white" {}

  16. }
  17. SubShader
  18. {
  19. Pass
  20. {
  21. Cull off

  22. CGPROGRAM //-----------

  23. #pragma target 3.0
  24. #pragma vertex view_space
  25. #pragma fragment relief_map_quad_depth
  26. #pragma profileoption MaxTexIndirections=64

  27. //----- uniform
  28. float depth;
  29. float tile;
  30. float3 ambient_color;
  31. float3 diffuse_color;
  32. float3 specular_color;
  33. float shine;
  34. float3 lightpos : POSITION;

  35. //----- application data

  36. struct a2v
  37. {
  38.     float4 vertex       : POSITION; // float4 pos
  39.     float3 normal     : NORMAL;
  40.     float2 texcoord   : TEXCOORD0;
  41.     float4 tangent   : TANGENT0; // float3 tangent
  42.     //float3 binormal   : BINORMAL0; // Not found in unity3d
  43. };

  44. struct v2f
  45. {
  46. float4 hpos : POSITION;
  47. float3 eye : TEXCOORD0;
  48. float3 light : TEXCOORD1;
  49. float2 texcoord : TEXCOORD2;
  50. };

  51. //----- vetrex shader
  52. v2f view_space(a2v IN)
  53. {

  54. v2f OUT;

  55. // vertex position in object space
  56. float4 pos=float4(IN.vertex.x,IN.vertex.y,IN.vertex.z,1.0);

  57. // vertex position IN clip space
  58. OUT.hpos = mul(glstate.matrix.mvp, pos);

  59. // copy color and texture coordinates
  60. OUT.texcoord=IN.texcoord.xy*tile;

  61. // compute modelview rotation only part
  62. float3x3 modelviewrot =float3x3(glstate.matrix.modelview[0]);

  63. // tangent vectors IN view space
  64. float3 IN_bINormal = cross( IN.normal, IN.tangent.xyz )*IN.tangent.w;  
  65. float3 tangent = mul(modelviewrot, IN.tangent.xyz);
  66. float3 bINormal = mul(modelviewrot, IN_bINormal.xyz); // IN.biNormal
  67. float3 normal = mul(modelviewrot, IN.normal);
  68. float3x3 tangentspace = float3x3(tangent, bINormal,normal);

  69. // vertex position IN view space (with model transformations)
  70. float3 vpos=mul(glstate.matrix.modelview[0],pos).xyz;

  71. // view in tangent space
  72. float3 eye=mul(tangentspace,vpos);
  73. eye.z=-eye.z;
  74. OUT.eye=eye;

  75. // light position in tangent space
  76. OUT.light=mul(tangentspace,lightpos -vpos);
  77. //OUT.light=mul(tangentspace,glstate.light[0].position.xyz -vpos);

  78. return OUT;
  79. }


  80. // ray intersect quad depth map with linear search
  81. void ray_intersect_rmqd_linear(
  82.       in sampler2D quad_depth_map,
  83.       inout float3 s,
  84.       inout float3 ds)
  85. {
  86.    const int linear_search_steps=30;  //15
  87.    
  88.    ds/=linear_search_steps;
  89.    
  90.    // search front to back for first point inside object
  91.    for( int i=0;i<linear_search_steps-1;i++ )
  92.    {
  93. float4 t=tex2D(quad_depth_map,s.xy);

  94. float4 d=s.z-t; // compute distances to each layer
  95. d.xy*=d.zw; d.x*=d.y; // x=(x*y)*(z*w)

  96. if (d.x>0) // if ouside object move forward
  97. s+=ds;
  98.    }
  99. }

  100. // ray intersect quad depth map with binary search
  101. void ray_intersect_rmqd_binary(
  102.       in sampler2D quad_depth_map,
  103.       inout float3 s,
  104.       inout float3 ds)
  105. {
  106.    const int binary_search_steps=5;
  107.    
  108.    float3 ss=sign(ds.z);

  109.    // recurse around first point for closest match
  110.    for( int i=0;i<binary_search_steps;i++ )
  111.    {
  112. ds*=0.5; // half size at each step

  113. float4 t=tex2D(quad_depth_map,s.xy);

  114. float4 d=s.z-t; // compute distances to each layer
  115. d.xy*=d.zw; d.x*=d.y; // x=(x*y)*(z*w)

  116. if (d.x<0) // if inside
  117. {
  118. ss=s; // store good return position
  119. s-=2*ds; // move backward
  120. }
  121. s+=ds; // else move forward
  122.    }
  123.    
  124.    s=ss;
  125. }

  126. float4 relief_map_quad_depth(
  127. v2f IN,
  128. uniform sampler2D quad_depth_map,
  129. uniform sampler2D color_map,
  130. uniform sampler2D normal_map_x,
  131. uniform sampler2D normal_map_y ) : COLOR
  132. {
  133. // view vector in tangent space
  134. float3 v=normalize(IN.eye);

  135. // serach start position
  136. float3 s=float3(IN.texcoord,0);

  137. // separate direction (front or back face)
  138. float dir=v.z;
  139. v.z=abs(v.z);

  140. // depth bias (1-(1-d)*(1-d))
  141. float d=depth*(2*v.z-v.z*v.z);

  142. // compute serach vector
  143. v/=v.z;
  144. v.xy*=d;
  145. s.xy-=v.xy*0.5;

  146. // if viewing from backface
  147. if (dir<0)
  148. {
  149. s.z=0.996; // search from back to front
  150. v.z=-v.z;
  151. }

  152. // ray intersect quad depth map
  153. ray_intersect_rmqd_linear(quad_depth_map,s,v);
  154. ray_intersect_rmqd_binary(quad_depth_map,s,v);

  155. // discard if no intersection is found
  156. if (s.z>0.997) discard;
  157. if (s.z<0.003) discard;

  158. // DEBUG: return depth
  159. //return float4(s.zzz,1);

  160. // get quad depth and color at intersection
  161. float4 t=tex2D(quad_depth_map,s.xy);
  162. float4 c=tex2D(color_map,s.xy);

  163. // get normal components X and Y
  164. float4 nx=tex2D(normal_map_x,s.xy);
  165. float4 ny=tex2D(normal_map_y,s.xy);

  166. // compute normal
  167. float4 z=abs(s.z-t);
  168. int m=0; // find min component
  169. float zm = z.x;
  170. if (z.y<zm) { m=1; zm = z.y; }
  171. if (z.z<zm) { m=2; zm = z.z; }
  172. if (z.w<zm) { m=3; }

  173. float3 n; // get normal at min component layer
  174. if ( m == 0) { n.x=nx[0]; n.y=1-ny[0]; } //n.x=nx[m]; n.y=1-ny[m];
  175. if ( m == 1) { n.x=nx[1]; n.y=1-ny[1]; }
  176. if ( m == 2) { n.x=nx[2]; n.y=1-ny[2]; }
  177. if ( m == 3) { n.x=nx[3]; n.y=1-ny[3]; }

  178. n.xy=n.xy*2-1; // expand to [-1,1] range
  179. n.z=sqrt(max(0,1.0-dot(n.xy,n.xy))); // recompute z
  180. if (m==1||m==3) // invert normal z if in backface
  181. n.z=-n.z;

  182. // DEBUG: return normal
  183. // return float4(n*0.5+0.5,1);

  184. // compute light vector in view space
  185. float3 l=normalize(IN.light);

  186. // restore view direction z component
  187. v=normalize(IN.eye);
  188. v.z=-v.z;

  189. // compute diffuse and specular terms
  190. float ldotn=saturate(dot(l,n));
  191. float ndoth=saturate(dot(n,normalize(l-v)));

  192. // attenuation factor
  193. float att=1.0-max(0,l.z); att=1.0-att*att;

  194. // return final color with lighting
  195. float4 finalcolor;
  196. finalcolor.xyz = c.xyz*ambient_color +
  197. att*(c.xyz*diffuse_color*ldotn +
  198. c.w*specular_color.xyz*pow(ndoth,shine));
  199. finalcolor.w=1;

  200. return finalcolor;
  201. }

  202. ENDCG //----------
  203. } // Pass
  204. } // SubShader
  205. } // Shader
复制代码

作者: Chubbaz    时间: 2014-2-23 07:04
謝謝你的分享!教程
作者: 蜗牛    时间: 2014-2-23 11:26
谢谢 共享 。。。。
作者: kikkik7000    时间: 2014-2-23 18:32
Thanks for sharing !
作者: BlueGem    时间: 2014-2-24 16:19
感谢分享
作者: 川流不息    时间: 2014-3-3 12:47
感谢楼主的无私分享
作者: vega_lin    时间: 2014-3-3 14:11
感谢无私分享.~
作者: gracezrxx    时间: 2014-3-6 15:01
4个报错 28 29 71
作者: Eureka    时间: 2014-3-28 16:19
感谢无私分享.~
作者: smg169    时间: 2014-4-25 15:09


感谢楼主分享啦~~~




欢迎光临 纳金网 (http://c-www.narkii.com/club/) Powered by Discuz! X2.5