home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the 3D Game Programming Gurus / gurus.iso / DirectX / dx9sdkcp.exe / SDK (C++) / Samples / Media / EffectEdit / Wood.fx < prev   
Encoding:
Text File  |  2002-11-21  |  4.5 KB  |  168 lines

  1. //
  2. // Wood Shader
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. //
  5. // Note: This effect file works with EffectEdit.
  6. //
  7.  
  8. string XFile = "teapot.x";
  9. int    BCLR  = 0xff202080;
  10.  
  11. // transformations
  12. float4x3 WorldView  : WORLDVIEW;
  13. float4x4 Projection : PROJECTION;
  14.  
  15. // light direction (view space)
  16. float3 lightDir <  string UIDirectional = "Light Direction"; > = {0.577, -0.577, 0.577};
  17.  
  18. // light intensity
  19. float4 I_a = { 0.3f, 0.3f, 0.3f, 1.0f };    // ambient
  20. float4 I_d = { 1.0f, 1.0f, 1.0f, 1.0f };    // diffuse
  21. float4 I_s = { 0.6f, 0.6f, 0.6f, 1.0f };    // specular
  22.  
  23. // material reflectivity
  24. float4 k_a = { 0.2f, 0.2f, 0.2f, 1.0f };    // ambient
  25. float4 k_d = { 1.0f, 0.7f, 0.2f, 1.0f };    // diffuse
  26. float4 k_s = { 1.0f, 1.0f, 1.0f, 1.0f };    // specular
  27. int    n   = 64;                            // power
  28.  
  29. #define DARK    0.3f
  30. #define LIGHT   1.0f
  31.  
  32. // model dependent wood parameters
  33. #define RINGSCALE   10.0f
  34. #define POINTSCALE  2.0f
  35. #define TURBULENCE  1.0f
  36. #define VOLUME_SIZE 32
  37. // textures
  38.  
  39. texture LinearTex < string function = "Linear"; int width = 16; int height = 16; >;
  40. texture NoiseTex  < string type = "VOLUME"; string function = "GenerateNoise"; int width = VOLUME_SIZE, height = VOLUME_SIZE, depth = VOLUME_SIZE; >;
  41.  
  42. float4 Linear(float2 Pos : POSITION) : COLOR
  43. {
  44.     return float4(Pos, Pos);
  45. }
  46.  
  47. // function used to fill the volume noise texture
  48. float4 GenerateNoise(float3 Pos : POSITION) : COLOR
  49. {
  50.     float4 Noise = (float4)0;
  51.  
  52.     for (int i = 1; i < 256; i += i)
  53.     {
  54.         Noise.r += abs(noise(Pos * 500 * i)) / i;
  55.         Noise.g += abs(noise((Pos + 1)* 500 * i)) / i;
  56.         Noise.b += abs(noise((Pos + 2) * 500 * i)) / i;
  57.         Noise.a += abs(noise((Pos + 3) * 500 * i)) / i;
  58.     }
  59.  
  60.     return Noise;
  61. }
  62.  
  63. struct VS_OUTPUT
  64. {
  65.     float4 Pos  : POSITION;
  66.     float3 Diff : COLOR0;
  67.     float3 Spec : COLOR1;
  68.     float3 Tex0 : TEXCOORD0;               
  69.     float3 Tex1 : TEXCOORD1;               
  70.     float2 Tex2 : TEXCOORD2;               
  71. };
  72.  
  73. VS_OUTPUT VS(    
  74.     float3 Pos  : POSITION,
  75.     float3 Norm : NORMAL)
  76. {
  77.     VS_OUTPUT Out = (VS_OUTPUT)0;
  78.  
  79.     float3 L = -lightDir;
  80.     float3 P = mul(float4(Pos, 1), (float4x3)WorldView);    // position (view space)
  81.     float3 N = normalize(mul(Norm, (float3x3)WorldView));   // normal (view space)
  82.     float3 R = normalize(2 * dot(N, L) * N - L);            // reflection vector (view space)
  83.     float3 V = -normalize(P);                               // view direction (view space)
  84.  
  85.     Out.Pos  = mul(float4(P, 1), Projection);             // position (projected)
  86.     Out.Diff = I_a * k_a + I_d * k_d * max(0, dot(N, L)); // diffuse + ambient
  87.     Out.Spec = I_s * k_s * pow(max(0, dot(R, V)), n/4);   // specular
  88.     Out.Tex1 = 0.5 * Pos * POINTSCALE; 
  89.     Out.Tex0 = Out.Tex1 * TURBULENCE;        
  90.     Out.Tex2 = RINGSCALE * length(Out.Tex1.xz);
  91.  
  92.     return Out;
  93. }
  94.  
  95. // samplers
  96. sampler NoiseSamp = sampler_state 
  97. {
  98.     texture = <NoiseTex>;
  99.     AddressU  = WRAP;        
  100.     AddressV  = WRAP;
  101.     AddressW  = WRAP;
  102.     MIPFILTER = LINEAR;
  103.     MINFILTER = LINEAR;
  104.     MAGFILTER = LINEAR;
  105. };
  106.  
  107. sampler LinearSamp = sampler_state 
  108. {
  109.     texture = <LinearTex>;
  110.     AddressU  = WRAP;        
  111.     AddressV  = WRAP;
  112.     AddressW  = WRAP;
  113.     MIPFILTER = LINEAR;
  114.     MINFILTER = LINEAR;
  115.     MAGFILTER = LINEAR;
  116. };
  117.  
  118. // PS11 version is a gross approximation
  119. float4 PS11(VS_OUTPUT In) : COLOR
  120. {   
  121.     float4 Color;
  122.     float r;
  123.  
  124.     // note the use of a linear texture with wrapped texcoords to emulate 'frac'
  125.     r = tex2D(LinearSamp, In.Tex2) +  0.1 * (tex3D(NoiseSamp, In.Tex1) - 0.5); 
  126.  
  127.     Color.rgb = In.Diff * lerp(DARK, LIGHT, saturate(0.8 - 0.6 * r))
  128.               + In.Spec;
  129.     Color.w   = 1;
  130.  
  131.     return Color;
  132. }  
  133.  
  134. // PS20 is much better than PS11 because of dependent texture reads
  135. float4 PS20(VS_OUTPUT In) : COLOR
  136. {   
  137.     float4 Color;
  138.     float3 PP = In.Tex1 + 0.1 * (tex3D(NoiseSamp, frac(In.Tex1)) - 0.5);
  139.     float r;
  140.  
  141.     r = RINGSCALE * length(PP.xz);
  142.     r += 0.1 * tex3D(NoiseSamp, r);
  143.     r = frac(r);
  144.  
  145.     Color.rgb = In.Diff * lerp(DARK, LIGHT, saturate(0.8 - 0.6 * r))
  146.               + In.Spec;
  147.     Color.w   = 1;
  148.  
  149.     return Color;
  150. }  
  151.  
  152. technique TWood_PS_1_1
  153. {
  154.     pass P0
  155.     {
  156.         VertexShader = compile vs_1_1 VS();
  157.         PixelShader  = compile ps_1_1 PS11();
  158.     }
  159. }
  160.  
  161. technique TWood_PS_2_0
  162. {
  163.     pass P0
  164.     {
  165.         VertexShader = compile vs_1_1 VS();
  166.         PixelShader  = compile ps_2_0 PS20();
  167.     }
  168. }