home *** CD-ROM | disk | FTP | other *** search
/ DarkBasic Professional / DarkBasicPro.iso / data1.cab / Lang_Files_(English) / Help / examples / basic3d / bubble.fx < prev    next >
Encoding:
Text File  |  2004-09-22  |  3.9 KB  |  162 lines

  1. //
  2. // Bubble
  3. //
  4.  
  5. matrix worldi : WorldIT;
  6. matrix wvp : WorldViewProjection;
  7. matrix mworld : World;
  8. matrix viewInv : ViewIT;
  9. float4 lightPos : Position < string UIPosition = "Light Position"; >;
  10. float ticks : Time;
  11.  
  12. string XFile = "sphere.x";
  13. texture colorTexture < string Name = "blue.tga"; >;
  14.  
  15. pixelshader pNIL;
  16.  
  17. struct VS_INPUT
  18. {
  19.     float3 Position     : POSITION;
  20.     float4 UV           : TEXCOORD0;
  21.     float3 Normal       : NORMAL;
  22. };
  23.  
  24. struct VS_OUTPUT
  25. {
  26.     float4 HPosition    : POSITION;
  27.     float4 TexCoord0    : TEXCOORD0;
  28.     float4 diffCol      : COLOR;
  29.     float4 specCol      : SPECULAR;
  30. };
  31.  
  32. float4 ambiColor={0.2f, 0.2f, 0.2f, 1.0f};
  33. float4 surfColor={0.8f, 0.8f, 0.8f, 1.0f};
  34. float4 liteColor={1.0f, 1.0f, 1.0f, 1.0f};
  35. float specExpon=22.0;
  36. float horizontal=0.2;
  37. float vertical=3.0;
  38. float timeScale=0.005;
  39.  
  40. VS_OUTPUT VShade(VS_INPUT IN)
  41. {
  42.     VS_OUTPUT   OUT;
  43.  
  44.     // world normal
  45.     float3 worldNormal = mul(IN.Normal, mworld).xyz;
  46.     worldNormal = normalize(worldNormal);
  47.     float timeNow = ((ticks/100.0)/timeScale);
  48.  
  49.     //build float4
  50.     float4 tempPos;
  51.     tempPos.xyz = IN.Position.xyz;
  52.     tempPos.w   = 1.0;
  53.  
  54.     float iny = tempPos.y * vertical + timeNow;
  55.     float wiggleX = sin(iny) * horizontal;
  56.     float wiggleY = cos(iny) * horizontal; // deriv
  57.     worldNormal.y = worldNormal.y + wiggleY;
  58.     worldNormal = normalize(worldNormal);
  59.     tempPos.x = tempPos.x + wiggleX;
  60.  
  61.     //compute worldspace position
  62.     float3 worldSpacePos = mul(tempPos,mworld).xyz;
  63.     float3 LightVec = normalize(lightPos - worldSpacePos);
  64.     float ldn = dot(LightVec,worldNormal);
  65.  
  66.     float diffComp = max(0,ldn);
  67.     float4 diffContrib = surfColor * ( diffComp * liteColor + ambiColor);
  68.     OUT.diffCol = diffContrib;
  69.     OUT.diffCol.w = 1.0;
  70.     OUT.TexCoord0 = IN.UV;
  71.  
  72.     float3 EyePos = viewInv[3].xyz;
  73.     float3 vertToEye = normalize(EyePos - worldSpacePos);
  74.     float3 halfAngle = normalize(vertToEye + LightVec);
  75.  
  76.     float hdn = pow(max(0,dot(halfAngle,worldNormal)),specExpon);
  77.     OUT.specCol = hdn * liteColor;
  78.  
  79.     // transform into homogeneous-clip space
  80.     OUT.HPosition = mul(tempPos, wvp);
  81.  
  82.     return OUT;
  83. }
  84.  
  85. uniform sampler   sampler0 = 
  86. sampler_state 
  87. {
  88.     texture = <colorTexture>;
  89.     MinFilter = Linear;
  90.     MagFilter = Linear;
  91.     MipFilter = Linear;
  92.     AddressU = Wrap;
  93.     AddressV = Wrap;
  94.     AddressW = Wrap;
  95. };
  96.  
  97. struct PS_INPUT
  98. {
  99.     float4 TexCoord0    : TEXCOORD0;
  100.     float4 diffCol      : COLOR;
  101.     float4 specCol      : SPECULAR;
  102. };
  103.  
  104. struct pixelOutput
  105. {
  106.   float4 col : COLOR;
  107. };
  108.  
  109. pixelOutput MrWigglePS_t(PS_INPUT IN)
  110. {
  111.     // Lookup the colorMap texture
  112.     pixelOutput OUT; 
  113.     float4 result = IN.diffCol * tex2D( sampler0, IN.TexCoord0) + IN.specCol;
  114.     OUT.col = result;
  115.     return OUT;
  116. }
  117.  
  118. //////////////////////////////////////
  119. // Techniques specs follow
  120. //////////////////////////////////////
  121. technique t0
  122. {
  123.     pass p0
  124.     {
  125.         VertexShader = compile vs_2_0 VShade();
  126.         PixelShader = compile ps_1_1 MrWigglePS_t();
  127.  
  128.         // Z-buffering not to be used
  129.         ZEnable      = false;
  130.         ZWriteEnable = false;
  131.         CullMode     = CW;
  132.  
  133.         // enable alpha blending
  134.         AlphaBlendEnable = TRUE;
  135.         SrcBlend         = SRCALPHA;
  136.         DestBlend        = INVSRCALPHA;
  137.  
  138.         MinFilter[0] = Linear;
  139.         MagFilter[0] = Linear;
  140.         MipFilter[0] = Linear;
  141.     }
  142.     pass p1
  143.     {
  144.         VertexShader = compile vs_2_0 VShade();
  145.         PixelShader = compile ps_1_1 MrWigglePS_t();
  146.  
  147.         // Z-buffering not to be used
  148.         ZEnable      = false;
  149.         ZWriteEnable = false;
  150.         CullMode     = CCW;
  151.  
  152.         // enable alpha blending
  153.         AlphaBlendEnable = TRUE;
  154.         SrcBlend         = SRCALPHA;
  155.         DestBlend        = INVSRCALPHA;
  156.  
  157.         MinFilter[0] = Linear;
  158.         MagFilter[0] = Linear;
  159.         MipFilter[0] = Linear;
  160.     }
  161. }
  162.