home *** CD-ROM | disk | FTP | other *** search
/ DarkBasic Professional / DarkBasicPro.iso / data1.cab / Lang_Files_(English) / Compiler / effects / bump.fx next >
Encoding:
Text File  |  2004-09-22  |  7.9 KB  |  344 lines

  1. //
  2. // Bump2
  3. //
  4.  
  5. // Constants
  6. float4 lhtDir < string UIDirectional = "Light Direction"; >; 
  7. float4 lhtPos < string UIPosition = "Light Position"; >;
  8. vector vCPS : CameraPosition;  
  9. matrix mTot : WorldViewProjection;
  10. matrix mWldInv : WorldIT;
  11. matrix mWld :  World; 
  12.  
  13. // Model and Texture Names
  14. string XFile = "default.x";
  15. texture tBase < string name = "base.tga"; >;
  16. texture tBump < string name = "bump.dds"; >;
  17.  
  18. struct VS_INPUT
  19. {
  20.     float4  Pos     : POSITION;
  21.     float3  Normal  : NORMAL;
  22.     float3  Tex0    : TEXCOORD0;
  23.     float3  Tangent : TANGENT;    
  24. };
  25.  
  26. struct VS_OUTPUT
  27. {
  28.     float4  Pos           : POSITION;
  29.     float3  tLight        : COLOR;
  30.     float3  tNormal       : TEXCOORD0;   
  31.     float3  depRead       : TEXCOORD1;    
  32.     float3  tH            : TEXCOORD2; 
  33.     float3  Tex0          : TEXCOORD3;    
  34. };
  35.  
  36. VS_OUTPUT VShade(VS_INPUT i, uniform int ShadeMethod)
  37. {
  38.     VS_OUTPUT   o;
  39.     float3      wNormal, wTangent, wBinorm, tLight;
  40.     
  41.     //tranform position    
  42.     o.Pos = mul( i.Pos, mTot); 
  43.            
  44.     //transform normal
  45.     //and tangent
  46.     wNormal = mul( i.Normal, mWld );
  47.     wTangent = mul( i.Tangent, mWld );
  48.  
  49.     //lee, normalize to defeat scaled world matrices
  50.     wNormal = normalize(wNormal);
  51.     wTangent = normalize(wTangent);
  52.  
  53.     //Cross product to create BiNormal
  54.     wBinorm = cross( wTangent, wNormal );
  55.  
  56.     // The first method is diffuse dot3 bumpmap
  57.     // transform the light vector into Tangent Space   
  58.     float3x3 tangentMatrix = {wTangent, wBinorm, wNormal};
  59.     tangentMatrix = transpose( tangentMatrix);
  60.     tLight = mul(-lhtDir, tangentMatrix);
  61.    
  62.     // all other methods are specular dot3 bump 
  63.     // and therefore use the half angle instead of the light       
  64.     // compute the half angle vector    
  65.     float3 wPos, H, tH;
  66.         
  67.     wPos = mul( i.Pos, mWld); 
  68.        
  69.     H = vCPS - wPos;
  70.     H = normalize(H);
  71.       
  72.     H = H -lhtDir.xyz;
  73.     H = normalize(H);
  74.  
  75.     //transform the half angle vector into tangent space
  76.     tH = mul(H, tangentMatrix);
  77.     o.tH = tH;
  78.     o.depRead = tH;    
  79.  
  80.     if (ShadeMethod==1)
  81.        tLight = tH;
  82.  
  83.     //mutiply by a half to bias, then add half
  84.     o.tLight = 0.5f + tLight * 0.5f;
  85.    
  86.     // Copy UVs
  87.     o.Tex0 = i.Tex0;                  
  88.     o.tNormal = i.Tex0;                  
  89.     
  90.     return o;
  91. }    
  92.  
  93. uniform sampler   sampler0 = 
  94. sampler_state 
  95. {
  96.     texture = <tBase>;
  97.     MinFilter = Linear;
  98.     MagFilter = Linear;
  99.     MipFilter = Linear;
  100.     AddressU = Wrap;
  101.     AddressV = Wrap;
  102.     AddressW = Wrap;
  103. };
  104.  
  105. uniform sampler   sampler1 = 
  106. sampler_state 
  107. {
  108.     texture = <tBump>;
  109.     MinFilter = Linear;
  110.     MagFilter = Linear;
  111.     MipFilter = Linear;
  112.     AddressU = Wrap;
  113.     AddressV = Wrap;
  114.     AddressW = Wrap;
  115. };
  116.  
  117. struct PS_INPUT
  118. {
  119.     float3  tLight        : COLOR;
  120.     float3  tNormal       : TEXCOORD0;    
  121.     float3  depRead       : TEXCOORD1;    
  122.     float3  tH            : TEXCOORD2;    
  123.     float3  Tex0          : TEXCOORD3;    
  124. };
  125.  
  126. struct PS_OUTPUT
  127. {
  128.     float4  Col        : COLOR;
  129. };
  130.               
  131. PS_OUTPUT PShade(PS_INPUT i, uniform int ShadeMethod)
  132. {
  133.     PS_OUTPUT o;
  134.     float4    cosang, t0, t1;
  135.     float3    tLight;
  136.     
  137.     // Sample diffuse texture and Normal map    
  138.     t0 = tex2D( sampler0, i.Tex0);
  139.     // _bx2 = 2 * source - 1
  140.     t1 = 2 * tex2D( sampler1, i.tNormal) - 1;
  141.     tLight = 2 * i.tLight - 1;
  142.        
  143.     // DP Lighting in tangent space (where normal map is based)
  144.     cosang = dot( t1, tLight );    
  145.     if (ShadeMethod==1) 
  146.     {
  147.           cosang = cosang * cosang;
  148.           cosang = cosang * cosang;
  149.           cosang = cosang * cosang;
  150.           cosang = cosang * cosang;                              
  151.     }        
  152.  
  153.     // Modulate with diffuse texture    
  154.     o.Col = t0 * cosang;
  155.    
  156.     return o;
  157. }
  158.  
  159. PS_OUTPUT PShade2(PS_INPUT i)
  160. {
  161.     PS_OUTPUT o;
  162.     float4    cosang, tDiffuse, tNormal, col;
  163.     float3    tLight;
  164.     
  165.     // Sample diffuse texture and Normal map    
  166.     tDiffuse = tex2D( sampler0, i.Tex0 );    
  167.  
  168.     // sample tLight
  169.     // _bx2 = 2 * source - 1
  170.     tNormal = 2 * tex2D( sampler1, i.tNormal) - 1;
  171.     tLight = 2 * i.tLight - 1;
  172.       
  173.     // DP Lighting in tangent space (where normal map is based)
  174.     col = dot( tNormal, tLight ) * tDiffuse;    
  175.     
  176.     cosang = dot( tNormal,i.tH );    
  177.     cosang = pow( cosang, 32);    
  178.  
  179.     // Modulate with diffuse texture    
  180.     col = col  + cosang;
  181.  
  182.     o.Col = col;
  183.    
  184.     return o;
  185. }
  186.  
  187. technique tec0
  188.     pass p0
  189.     {
  190.         VertexShaderConstant[0] = <mTot>; 
  191.         VertexShaderConstant[4] = <mWldInv>;
  192.         VertexShaderConstant[8] = <lhtPos>;
  193.         VertexShaderConstant[9] = <vCPS>;
  194.         VertexShaderConstant[10] = { 0, 0.5, 1, 2 };
  195.  
  196.         PixelShaderConstant[0] = { 0, 0.5, 1, -0.2 };
  197.         PixelShaderConstant[1] = { 66, 66, 66, 66 };
  198.         PixelShaderConstant[2] = { 0.985, 1, 0.945, 1 };
  199.         PixelShaderConstant[3] = { 1, 1, 1, 1 };
  200.         PixelShaderConstant[4] = { 2, 0.5, 4, 8 };
  201.  
  202.         Texture[0]   = <tBase>;
  203.         Texture[1]   = <tBump>;
  204.  
  205.         AddressU[0] = wrap;
  206.         AddressU[1] = wrap;
  207.  
  208.         MinFilter[0] = Linear;
  209.         MagFilter[0] = Linear;
  210.         MipFilter[0] = Linear;
  211.         MinFilter[1] = Linear;
  212.         MagFilter[1] = Linear;
  213.         MipFilter[1] = Linear;
  214.  
  215.         VertexShader = 
  216.         asm
  217.         {
  218.     vs.1.1
  219.  
  220.     dcl_position   v0
  221.     dcl_normal     v1
  222.     dcl_texcoord   v2
  223.     dcl_tangent    v3
  224.     dcl_binormal   v4
  225.  
  226.     ; xform and output vertex
  227.     m4x4 oPos, v0, c0
  228.  
  229.     ; base map and bump map texture coordinates
  230.     mov  oT0, v2
  231.     mov  oT1, v2
  232.  
  233.     ; find light position in object space 
  234.     mov r1, c8
  235.     m4x4 r0, r1, c4
  236.  
  237.     ; find light vector in object space
  238.     sub r0, r0, v0
  239.  
  240.     ; normalize light vector
  241.     dp3 r1.x, r0, r0
  242.     rsq r1.x, r1.x
  243.     mul r0, r0.xyz, r1.xxx
  244.  
  245.     ; move light vector into tangent space
  246.     dp3 r1.x, r0, v3  ; tangent
  247.     dp3 r1.y, r0, v4  ; binormal
  248.     dp3 r1.z, r0, v1  ; normal
  249.  
  250.     mov oT2, r1  ; light vector into tex coord2 (to index normalizer cubemap)
  251.     mov r5, r1   ; cache light vector in r5 for later use
  252.  
  253.     ; find eye position in object space 
  254.     mov r1, c9
  255.     m4x4 r0, r1, c4
  256.  
  257.     ; find view vector in object space
  258.     sub r0, r0, v0
  259.  
  260.     ; normalize view vector
  261.     dp3 r1.x, r0, r0
  262.     rsq r1.x, r1.x
  263.     mul r0, r0.xyz, r1.xxx
  264.  
  265.     ; move view vector into tangent space
  266.     dp3 r1.x, r0, v3  ; tangent
  267.     dp3 r1.y, r0, v4  ; binormal
  268.     dp3 r1.z, r0, v1  ; normal
  269.  
  270.     ; find half angle vector (L + V) / 2
  271.     add r2, r1, r5
  272.  
  273.     ; normalize half angle vector
  274.     dp3 r1.x, r2, r2
  275.     rsq r1.x, r1.x
  276.     mul r2, r2.xyz, r1.xxx
  277.  
  278.     mov oT3, r2  ; half angle vector into tex coord3 (to index normalizer cubemap)
  279.         };
  280.  
  281.  
  282.         PixelShader = 
  283.         asm
  284.         {
  285.        ps.2.0
  286.  
  287.        dcl t0
  288.        dcl t1
  289.        dcl t2
  290.        dcl t3
  291.  
  292.        dcl_2d   s0
  293.        dcl_2d   s1
  294.  
  295.        texld r0, t0, s0 ; base map
  296.        texld r1, t1, s1 ; bump map
  297.  
  298.        ; normalize L
  299.        dp3 r2.x, t2, t2
  300.        rsq r2.x, r2.x
  301.        mul r2, t2, r2.x
  302.  
  303.        ; normalize H
  304.        dp3 r3.x, t3, t3
  305.        rsq r3.x, r3.x
  306.        mul r3, t3, r3.x
  307.  
  308.        ; scale and bias N
  309.        sub r1, r1, c4.y
  310.        mul r1, r1, c4.x
  311.  
  312.        ; N.L
  313.        dp3_sat r2, r1, r2
  314.  
  315.        ; N.L * diffuse_light_color
  316.        mul r2, r2, c2
  317.    
  318.        ; (N.H)
  319.        dp3_sat r1, r1, r3
  320.  
  321.        ; (N.H)^k
  322.        pow r1, r1.x, c1.x
  323.  
  324.        ; (N.H)^k * specular_light_color
  325.         mul r1, r1, c3
  326.  
  327.        ; [(N.L) * base] + (N.H)^k
  328.        mad_sat r0, r0, r0, r1
  329.    
  330.        mov oC0, r0
  331.         };
  332.     }
  333. }
  334.  
  335. technique tec1
  336.     pass p0
  337.     {      
  338.         VertexShader = compile vs_1_1 VShade(0);
  339.         PixelShader = compile ps_1_1 PShade(0);
  340.     }
  341. }
  342.