浏览代码

Add a copy of animated Model and import it with GPU Animation Importer (#2)

Nikos Kastellanos 8 年之前
父节点
当前提交
208a7c2b2d

+ 42 - 11
Samples/Animation/Game1.cs

@@ -8,13 +8,23 @@ using tainicom.Aether.Animation;
 
 namespace Samples.Animation
 {
+    enum DrawMode : int
+    {
+        CPU,
+        GPU,
+    }
+
     public class Game1 : Microsoft.Xna.Framework.Game
     {
         GraphicsDeviceManager graphics;
         SpriteBatch spriteBatch;
         SpriteFont font;
-        Model _model;
+        Model _model_CPU;
+        Model _model_GPU;
         Animations _animations;
+        DrawMode drawMode = DrawMode.CPU;
+
+        KeyboardState prevKeyboardState;
 
         public Game1()
         {
@@ -32,9 +42,10 @@ namespace Samples.Animation
             spriteBatch = new SpriteBatch(GraphicsDevice);
             font = Content.Load<SpriteFont>("font");
             
-            _model = Content.Load<Model>("Dude/dude");
+            _model_CPU = Content.Load<Model>("Dude/dude");
+            _model_GPU = Content.Load<Model>("Dude/dude_GPU");
 
-            _animations = _model.GetAnimations();
+            _animations = _model_CPU.GetAnimations(); // Animation Data are the same between the two models
             var clip = _animations.Clips["Take 001"];
             _animations.SetClip(clip);
         }
@@ -45,10 +56,21 @@ namespace Samples.Animation
 
         protected override void Update(GameTime gameTime)
         {
+            var keyboardState = Keyboard.GetState();
+            var gamePadState = GamePad.GetState(PlayerIndex.One);
+
             // Allows the game to exit
-            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
+            if (keyboardState.IsKeyDown(Keys.Escape) || gamePadState.Buttons.Back == ButtonState.Pressed)
                 this.Exit();
 
+            if ((keyboardState.IsKeyDown(Keys.Space) && prevKeyboardState.IsKeyUp(Keys.Space)) || gamePadState.Buttons.A == ButtonState.Pressed)
+            {
+                int drawModesCount = Enum.GetValues(drawMode.GetType()).Length;
+                drawMode = (DrawMode)(((int)drawMode + 1) % drawModesCount);
+            }
+
+            prevKeyboardState = keyboardState;
+
             _animations.Update(gameTime.ElapsedGameTime, true, Matrix.Identity);
 
             base.Update(gameTime);
@@ -77,7 +99,11 @@ namespace Samples.Animation
             GraphicsDevice.DepthStencilState = DepthStencilState.Default;
             GraphicsDevice.SamplerStates[0] = SamplerState.LinearWrap;
 
-            var m = _model;
+            Model m = _model_CPU;
+            if (drawMode == DrawMode.CPU)
+                m = _model_CPU;
+            else if (drawMode == DrawMode.GPU)
+                m = _model_GPU;
 
             float aspectRatio = graphics.GraphicsDevice.Viewport.AspectRatio;
             Matrix[] transforms = new Matrix[m.Bones.Count];
@@ -94,12 +120,17 @@ namespace Samples.Animation
             {
                 foreach (var part in mesh.MeshParts)
                 {
-                    ((BasicEffect)part.Effect).SpecularColor = Vector3.Zero;
+                    if (drawMode == DrawMode.CPU)
+                        ((BasicEffect)part.Effect).SpecularColor = Vector3.Zero;
+                    else if (drawMode == DrawMode.GPU)
+                        ((SkinnedEffect)part.Effect).SpecularColor = Vector3.Zero;                        
                     ConfigureEffectMatrices((IEffectMatrices)part.Effect, Matrix.Identity, view, projection);
                     ConfigureEffectLighting((IEffectLights)part.Effect);
-
-                    // animate vertices on CPU
-                    part.UpdateVertices(_animations.AnimationTransforms);
+                    
+                    if (drawMode == DrawMode.CPU)
+                        part.UpdateVertices(_animations.AnimationTransforms); // animate vertices on CPU
+                    else if (drawMode == DrawMode.GPU)
+                        ((SkinnedEffect)part.Effect).SetBoneTransforms(_animations.AnimationTransforms);// animate vertices on GPU
                 }
                 mesh.Draw();
             }
@@ -117,13 +148,13 @@ namespace Samples.Animation
             }
 
             spriteBatch.Begin();
+            spriteBatch.DrawString(font, "Draw Mode: " + drawMode, new Vector2(32, 32), Color.White);
             spriteBatch.DrawString(font, msec.ToString("#0.000",CultureInfo.InvariantCulture) + "ms", new Vector2(32, GraphicsDevice.Viewport.Height - 130), Color.White);
             spriteBatch.DrawString(font, avg.ToString("#0.000",CultureInfo.InvariantCulture) + "ms (avg)", new Vector2(32, GraphicsDevice.Viewport.Height - 100), Color.White);
             spriteBatch.DrawString(font, msecMin.ToString("#0.000",CultureInfo.InvariantCulture) + "ms (min)", new Vector2(32, GraphicsDevice.Viewport.Height - 70), Color.White);
             spriteBatch.DrawString(font, msecMax.ToString("#0.000",CultureInfo.InvariantCulture) + "ms (max)", new Vector2(32, GraphicsDevice.Viewport.Height - 40), Color.White);
             spriteBatch.End();
-
-
+            
             base.Draw(gameTime);
         }
 

+ 3 - 0
Samples/AnimationContent/Content.mgcb

@@ -25,6 +25,9 @@
 #begin Dude/dude.xnb
 /copy:Dude/dude.xnb
 
+#begin Dude/dude_GPU.xnb
+/copy:Dude/dude_GPU.xnb
+
 #begin Dude/head_0.xnb
 /copy:Dude/head_0.xnb
 

文件差异内容过多而无法显示
+ 510 - 0
Samples/AnimationContent/Dude/dude_GPU.fbx


二进制
Samples/AnimationContent/Dude/dude_GPU.xnb


+ 7 - 0
Samples/AnimationContent/Samples.AnimationContent.contentproj

@@ -51,6 +51,13 @@
       <Name>GraphicsImporters.WINDOWS.XNA</Name>
     </ProjectReference>
   </ItemGroup>
+  <ItemGroup>
+    <Compile Include="Dude\dude_GPU.fbx">
+      <Name>dude_GPU</Name>
+      <Importer>FbxImporter</Importer>
+      <Processor>GpuAnimatedModelProcessor</Processor>
+    </Compile>
+  </ItemGroup>
   <Import Project="$(MSBuildExtensionsPath)\Microsoft\XNA Game Studio\$(XnaFrameworkVersion)\Microsoft.Xna.GameStudio.ContentPipeline.targets" />
   <!--  To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.

部分文件因为文件数量过多而无法显示