home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the 3D Game Programming Gurus / gurus.iso / DirectX / dx9sdkcp.exe / SDK (C++) / Samples / Media / MotionBlur.fx < prev    next >
Encoding:
Text File  |  2002-11-12  |  3.3 KB  |  120 lines

  1. //
  2. // Shaders used by the MotionBlur D3D sample
  3. //
  4. // Note: This effect file does not work with EffectEdit.
  5. //
  6.  
  7. VertexShader MotionBlur = asm
  8. {
  9.     vs.1.1 
  10.  
  11.     dcl_position0 v0   // v0 = position
  12.     dcl_normal0 v1   // v1 = normal
  13.  
  14.     // The following constants are set externally
  15.     // c0 ...c3  = world matrix, previous frame
  16.     // c4 ...c7  = world matrix, current frame
  17.     // c8 ...c11 = rotation matrix (from Arcball)
  18.     // c12...c15 = view matrix
  19.     // c16...c19 = projection matrix
  20.     // c20.x     = trail length
  21.     // c20.y     = 0
  22.     // c20.z     = 1
  23.  
  24.     //-------------------------------------------------------------------------
  25.     // POSITION
  26.     //-------------------------------------------------------------------------
  27.  
  28.     m4x4 r0, v0, c0 
  29.     // r0.xyz = POSITION of previous frame
  30.  
  31.     m4x4 r1, v0, c4 
  32.     // r1.xyz = POSITION of current frame
  33.  
  34.     m3x3 r2.xyz, v1, c4 
  35.     // r2.xyz = N = NORMAL of current frame 
  36.  
  37.     sub r3, r1, r0 
  38.     dp3 r4.x, r3.xyz, r3.xyz 
  39.     rsq r4.x, r4.x 
  40.     mul r3, r3, r4.x 
  41.     dp3 r4.x, r2.xyz, r3.xyz 
  42.     // r3.xyz = M = normalized moving vector
  43.     // r4.x = (N dot M)
  44.  
  45.     slt r4.y, r4.x, c20.y 
  46.     mul r4.y, r4.y, c20.x 
  47.     mad r1.xyz, -r3, r4.y, r1 
  48.     // if N points toward M, leave current POSITION unchanged
  49.     // else                , offset current POSITION with (-M * trail length)
  50.  
  51.     m4x4 r5, r1, c8 
  52.     m4x4 r6, r5, c12 
  53.     m4x4 oPos, r6, c16 
  54.     // POSITION in clipping space
  55.  
  56.     //-------------------------------------------------------------------------
  57.     // DIFFUSE
  58.     //-------------------------------------------------------------------------
  59.  
  60.     m3x3 r5.xyz, r2, c8 
  61.     m3x3 r6.xyz, r5, c12 
  62.     mov oD0.xyz, -r6.z 
  63.     // oD0.xyz = DIFFUSE color, assuming fixed light direction (0, 0, -1)
  64.  
  65.     sge oD0.w, r4.x, c20.y  // oD0.w = (r4.x >= 0 ? 1 : 0 )
  66.     // if (N dot M) < 0, oD0.w = 0
  67.     // else            , oD0.w = 1
  68. };
  69.  
  70. VertexShader NoMotionBlur = asm
  71. {
  72.     vs.1.1 
  73.  
  74.     dcl_position0 v0   // v0 = position
  75.     dcl_normal0   v1   // v1 = normal
  76.  
  77.     // The following constants are set externally
  78.     // c4 ...c7  = world matrix, current frame
  79.     // c8 ...c11 = rotation matrix (from Arcball)
  80.     // c12...c15 = view matrix
  81.     // c16...c19 = projection matrix
  82.     // c20.z     = 1
  83.  
  84.     //-------------------------------------------------------------------------
  85.     // POSITION
  86.     //-------------------------------------------------------------------------
  87.  
  88.     m4x4 r1, v0, c4 
  89.     m4x4 r2, r1, c8 
  90.     m4x4 r3, r2, c12 
  91.     m4x4 oPos, r3, c16 
  92.  
  93.     //-------------------------------------------------------------------------
  94.     // DIFFUSE
  95.     //-------------------------------------------------------------------------
  96.  
  97.     m3x3 r0.xyz, v1, c4 
  98.     m3x3 r1.xyz, r0, c8 
  99.     m3x3 r2.xyz, r1, c12 
  100.     mov oD0.xyz, -r2.z 
  101.     mov oD0.w, c20.z 
  102.     // oD0.xyz = DIFFUSE color, assuming fixed light direction (0, 0, -1)
  103.     // oD0.w   = 1.0f = alpha (opaque)
  104. };
  105.  
  106. technique MotionBlur
  107. {
  108.     pass p0
  109.     {
  110.         VertexShader = (NoMotionBlur);
  111.     }
  112.     pass p1
  113.     {
  114.         VertexShader = (MotionBlur);
  115.         AlphaBlendEnable = True;
  116.         SrcBlend = SrcAlpha;
  117.         DestBlend = InvSrcAlpha;
  118.     }
  119. }
  120.