Share Pastebin
Guest
Public paste!

Bryan

By: a guest | May 16th, 2008 | Syntax: C# | Size: 10.09 KB | Hits: 109 | Expires: Never
Copy text to clipboard
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. using Engine.Source.Client.Content;
  6. using Engine.Source.Client;
  7.  
  8. using Microsoft.Xna.Framework;
  9. using Microsoft.Xna.Framework.Graphics;
  10.  
  11. using Engine.Source.Client.BatchRendering;
  12. using Engine.Source.Client.Cubemaps;
  13. using Engine.Source.Client.Lighting;
  14.  
  15. using XSIXNARuntime;
  16.  
  17. namespace Engine.Source.Shared.Entities
  18. {
  19.     public class RenderEntityModule : IBatchRenderObject
  20.     {
  21.  
  22.         Model model;
  23.         public Model Model
  24.         {
  25.             get { return model; }
  26.         }
  27.  
  28.  
  29.  
  30.         List<XSIAnimationContent> Animations;
  31.         int AnimationIndex = 0;
  32.  
  33.         Matrix world;
  34.  
  35.         float animStart = 0.0f;
  36.         float nextAnimation = 0.0f;
  37.         float currentAnimLength;
  38.         float lastTime = 0.0f;
  39.  
  40.         Matrix[] transforms;
  41.         Matrix[] bones;
  42.  
  43.  
  44.         /// <summary>
  45.         ///
  46.         /// </summary>
  47.         public bool OverrideView = false;
  48.  
  49.  
  50.         public float animSpeed = 32f;
  51.  
  52.         public bool refreshWorld = false;
  53.  
  54.         Vector3 scale = Vector3.One;
  55.         Vector3 rotation = Vector3.Zero;
  56.         Vector3 translation = Vector3.Zero;
  57.  
  58.         IBatchRenderService BatchRender;
  59.         ICubeMapService CubeMaps;
  60.         ILightingService Lighting;
  61.  
  62.         Vector3 forceRenderPosition = Vector3.Zero;
  63.         Vector3 forceAmbient = Vector3.Zero;
  64.  
  65.         public Vector3 Scale
  66.         {
  67.             get { return scale; }
  68.             set { scale = value; refreshWorld = true; }
  69.         }
  70.  
  71.         public Vector3 Rotation
  72.         {
  73.             get { return rotation; }
  74.             set { rotation = value; refreshWorld = true; }
  75.         }
  76.  
  77.         public Vector3 Translation
  78.         {
  79.             get { return translation; }
  80.             set { translation = value; refreshWorld = true; }
  81.         }
  82.  
  83.  
  84.         public float Speed
  85.         {
  86.             get { return animSpeed; }
  87.             set { animSpeed = value; }
  88.         }
  89.  
  90.         public Matrix World
  91.         {
  92.             get { return world; }
  93.             set { world = value; }
  94.         }
  95.  
  96.         public void ForceRenderPosition(Vector3 pos)
  97.         {
  98.             forceRenderPosition = pos;
  99.         }
  100.         public void ForceAmbient(Vector3 ambientColor)
  101.         {
  102.             forceAmbient = ambientColor;
  103.         }
  104.  
  105.         public RenderEntityModule(string sz)
  106.             : this(ContentLoader.Load<Model>(sz))
  107.         {
  108.            
  109.         }
  110.  
  111.         public RenderEntityModule( Model m)
  112.         {
  113.             //model = ContentLoader.Load<Model>(sz);
  114.             model = m;
  115.            
  116.             Animations = new List<XSIAnimationContent>();
  117.  
  118.             XSIAnimationData l_Animations = model.Tag as XSIAnimationData;
  119.             if (l_Animations != null)
  120.             {
  121.                 foreach (KeyValuePair<String, XSIAnimationContent> AnimationClip in l_Animations.RuntimeAnimationContentDictionary)
  122.                 {
  123.                     AnimationClip.Value.BindModelBones(model);
  124.                     Animations.Add(AnimationClip.Value);
  125.                 }
  126.                
  127.  
  128.  
  129.  
  130.                
  131.                 //model.CopyAbsoluteBoneTransformsTo(transforms);
  132.                 //l_Animations.ComputeBoneTransforms(transforms);
  133.             }
  134.             l_Animations.ResolveBones(model);
  135.  
  136.             transforms = new Matrix[model.Bones.Count];
  137.  
  138.             BatchRender = (IBatchRenderService)(GameManager.Game.Services.GetService(typeof(IBatchRenderService)));
  139.             CubeMaps = (ICubeMapService)(GameManager.Game.Services.GetService(typeof(ICubeMapService)));
  140.             Lighting = (ILightingService)(GameManager.Game.Services.GetService(typeof(ILightingService)));
  141.         }
  142.  
  143.         public int GetBoneIndex(string sz)
  144.         {
  145.             return model.Bones[sz].Index;
  146.         }
  147.  
  148.         public Matrix GetTransform(int index)
  149.         {
  150.             return Matrix.CreateTranslation(this.transforms[index].Translation) * this.World;
  151.         }
  152.  
  153.         /// <summary>
  154.         /// Sets the animation for the model
  155.         /// </summary>
  156.         /// <param name="animationIndex">The index of the animation to play</param>
  157.         /// <param name="time">The length of the animation</param>
  158.         /// <param name="overrideAnim">If set to true, the new animation will play regardless.</param>
  159.         public void SetAnimation(int animationIndex, float time, bool overrideAnim)
  160.         {
  161.             if (overrideAnim || nextAnimation <= lastTime)
  162.             {
  163.                 AnimationIndex = animationIndex;
  164.                 Animations[AnimationIndex].Reset();
  165.                 currentAnimLength = time;
  166.                 nextAnimation = lastTime + time;
  167.                 animStart = lastTime;
  168.             }
  169.  
  170.             //Animations[AnimationIndex].
  171.         }
  172.  
  173.         public void StopAnimation(bool overrideAnim)
  174.         {
  175.             if (overrideAnim || nextAnimation <= lastTime)
  176.             {
  177.                 AnimationIndex = -1;
  178.             }
  179.         }
  180.  
  181.  
  182.  
  183.         public void Update(double gameTime)
  184.         {
  185.    
  186.             lastTime = (float)gameTime;
  187.  
  188.             if (CubeMaps == null)
  189.                 CubeMaps = (ICubeMapService)(GameManager.Game.Services.GetService(typeof(ICubeMapService)));
  190.  
  191.  
  192.  
  193.         }
  194.  
  195.         void AnimationUpdate()
  196.         {
  197.             if (AnimationIndex == -1)
  198.                 return;
  199.  
  200.             Animations[AnimationIndex].PlayBack(TimeSpan.FromSeconds((lastTime - animStart) / animSpeed
  201.     ), 1.0f);
  202.  
  203.  
  204.             if (Animations[AnimationIndex].Loop == true && nextAnimation < lastTime)
  205.             {
  206.                 Animations[AnimationIndex].Reset();
  207.                 nextAnimation = lastTime + currentAnimLength;
  208.                 animStart = lastTime;
  209.             }
  210.         }
  211.  
  212.  
  213.         public void RenderCallback(Effect effect, int i)
  214.         {
  215.             AnimationUpdate();
  216.  
  217.             XSIAnimationData l_Animations = model.Tag as XSIAnimationData;
  218.  
  219.             model.CopyAbsoluteBoneTransformsTo(transforms);
  220.             l_Animations.ComputeBoneTransforms(transforms);
  221.             bones = l_Animations.BoneTransforms;
  222.  
  223.             bool isSkinned = (bones.GetLength(0) > 0);
  224.             if ((effect.Parameters["Bones"] != null) && isSkinned)
  225.                 effect.Parameters["Bones"].SetValue(bones);
  226.  
  227.  
  228.  
  229.             //Matrix world = Matrix.CreateTranslation(Vector3.Zero) * Matrix.CreateScale(0.25f);
  230.             effect.Parameters["Model"].SetValue(transforms[model.Meshes[i].ParentBone.Index] * world);
  231.  
  232.             Vector3 renderPos = this.world.Translation;
  233.             if (forceRenderPosition != Vector3.Zero)
  234.                 renderPos = forceRenderPosition;
  235.          
  236.  
  237.             if (CubeMaps != null)
  238.             {
  239.                 TextureCube tc = CubeMaps.GetNearestCubeMap(renderPos);
  240.                 //tc = CubeMaps.GetCubeMapByIndex(0);
  241.                 if (tc != null)
  242.                     effect.Parameters["EnvironmentMap"].SetValue(tc);
  243.             }
  244.  
  245.             if (OverrideView)
  246.                 effect.Parameters["View"].SetValue(Matrix.Identity);
  247.  
  248.  
  249.             Vector4 lightPos0 = Vector4.Zero + Vector4.UnitY * 10.0f;
  250.             Vector4 lightPos1 = Vector4.UnitX * 10.0f;
  251.             Vector4 lightPos2 = Vector4.UnitZ * 10.0f;
  252.  
  253.             Vector4 Color1 = new Vector4(Vector3.One, 0.25f);
  254.             Vector4 Color2 = new Vector4(Vector3.One, 0.2f);
  255.             Vector4 Color0 = new Vector4(Vector3.One, 0.2f);
  256.  
  257.  
  258.             Vector3 ambientColor = new Vector3(0.5f);
  259.             if (Lighting.AmbientLighting != null)
  260.             {
  261.                 AmbientLightData ald = Lighting.AmbientLighting.GetAmbientLighting(renderPos);
  262.  
  263.                 ambientColor = ald.AmbientColor;
  264.                 if (forceAmbient != Vector3.Zero)
  265.                     ambientColor = forceAmbient;
  266.  
  267.                
  268.             }
  269.  
  270.             effect.Parameters["AmbientColor"].SetValue(ambientColor);
  271.  
  272.             //effect.Parameters["lightpos0"].SetValue(lightPos0);
  273.             //effect.Parameters["lightpos1"].SetValue(lightPos1);
  274.             //effect.Parameters["lightpos2"].SetValue(lightPos2);
  275.  
  276.             //effect.Parameters["lightcol0"].SetValue(Color0);
  277.             //effect.Parameters["lightcol1"].SetValue(Color1);
  278.             //effect.Parameters["lightcol2"].SetValue(Color2);
  279.             effect.CommitChanges();
  280.  
  281.             ModelMesh mesh = model.Meshes[i];
  282.             effect.GraphicsDevice.Indices = mesh.IndexBuffer;
  283.             for (int j = 0; j < mesh.MeshParts.Count; j++)
  284.             {
  285.                
  286.                 ModelMeshPart part = mesh.MeshParts[j];
  287.  
  288.                 effect.GraphicsDevice.Vertices[0].SetSource(mesh.VertexBuffer, part.StreamOffset, part.VertexStride);
  289.                 effect.GraphicsDevice.VertexDeclaration = part.VertexDeclaration;
  290.                 effect.GraphicsDevice.DrawIndexedPrimitives(PrimitiveType.TriangleList, part.BaseVertex, 0, part.NumVertices, part.StartIndex, part.PrimitiveCount);
  291.                
  292.             }
  293.         }
  294.  
  295.         public void Render()
  296.         {
  297.             if (refreshWorld)
  298.                 world = Matrix.CreateScale(scale) * Matrix.CreateFromYawPitchRoll(rotation.X, rotation.Y, rotation.Z)
  299.              * Matrix.CreateTranslation(translation);
  300.  
  301.             AnimationUpdate();
  302.  
  303.             XSIAnimationData l_Animations = model.Tag as XSIAnimationData;
  304.  
  305.             model.CopyAbsoluteBoneTransformsTo(transforms);
  306.             l_Animations.ComputeBoneTransforms(transforms);
  307.             bones = l_Animations.BoneTransforms;
  308.  
  309.             bool isSkinned = (bones.GetLength(0) > 0);
  310.  
  311.            
  312.  
  313.  
  314.             for(int i = 0; i < model.Meshes.Count; i++)
  315.             {
  316.                 foreach (Effect effect in model.Meshes[i].Effects)
  317.                 {
  318.                     EffectTechnique tech;
  319.                     if (isSkinned)
  320.                         tech = effect.Techniques["Skinned"];
  321.                     else
  322.                         tech = effect.Techniques["Static"];
  323.  
  324.                     BatchRender.BatchRender(effect, tech, i, this);
  325.                 }
  326.             }
  327.  
  328.  
  329.  
  330.         }
  331.  
  332.     }
  333. }