Advertisement
Guest User

Untitled

a guest
Feb 24th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.54 KB | None | 0 0
  1. using Urho;
  2.  
  3. namespace AppUrhoGame1
  4. {
  5.     public class CAnimatedModel_OLD : Component
  6.     {
  7.         Node node;
  8.         AnimatedModel model;
  9.  
  10.         /**
  11.         lataa animoidun modelin ja materiaalin
  12.         name            : modelin nimi
  13.         materialName    : materiaalin nimi
  14.  
  15.         jos ei anneta materialNamea, luetaan fileName.txt:stä materiaalin nimi ja käytetään sitä.
  16.  
  17.         */
  18.         public static CAnimatedModel_OLD Load(Scene scene, string name, string materialName = "")
  19.         {
  20.             CAnimatedModel_OLD m = new CAnimatedModel_OLD();
  21.             m._Load(scene, name, materialName);
  22.             return m;
  23.         }
  24.  
  25.         void _Load(Scene scene, string name, string materialName)
  26.         {
  27.             // luo node ja sille model
  28.             node = scene.CreateChild();
  29.             model = node.CreateComponent<AnimatedModel>();
  30.  
  31.             // lataa model
  32.             var cache = Globals.ResourceCache;
  33.             model.Model = cache.GetModel("Models/" + name);
  34.  
  35.             // jos materialName ei ole "", ladataan materialName.xml (muuten name.txt:stä löytyvä materiaali)
  36.             if (materialName != "")
  37.             {
  38.                 model.SetMaterial(cache.GetMaterial("Materials/" + materialName));
  39.             }
  40.             else
  41.             {
  42.                 string txtPath = "Models/" + name.Substring(0, name.LastIndexOf(".")) + ".txt";
  43.                 model.SetMaterial(cache.GetMaterial(Globals.ReadTxt(txtPath)));
  44.             }
  45.  
  46.             node.AddComponent(new AnimationController());
  47.  
  48.             model.CastShadows = Globals.CastShadows;
  49.         }
  50.  
  51.         public void Clear()
  52.         {
  53.             if (node != null)
  54.             {
  55.                 node.Remove();
  56.                 node = null;
  57.             }
  58.             if (model != null)
  59.             {
  60.                 model.Remove();
  61.                 model = null;
  62.             }
  63.         }
  64.  
  65.         public Node GetNode()
  66.         {
  67.             return node;
  68.         }
  69.  
  70.         public void LoadAnimation(string name, float weight)
  71.         {
  72.             var cache = Globals.ResourceCache;
  73.             Animation animation = cache.GetAnimation("Models/" + name);
  74.             AnimationState state = model.AddAnimationState(animation);
  75.  
  76.             // The state would fail to create (return null) if the animation was not found
  77.             if (state != null)
  78.             {
  79.                 state.Weight = weight;
  80.                 state.Looped = true;
  81.             }
  82.  
  83.         }
  84.  
  85.         public void SetWeight(uint animNum, float weight)
  86.         {
  87.             AnimationState state = model.GetAnimationState(animNum);
  88.             if (state != null)
  89.             {
  90.                 state.Weight = weight;
  91.             }
  92.         }
  93.         public void AddWeight(uint animNum, float weight)
  94.         {
  95.             AnimationState state = model.GetAnimationState(animNum);
  96.             if (state != null)
  97.             {
  98.                 float w = state.Weight + weight;
  99.                 if (w < 0) w = 0;
  100.                 if (w > 1) w = 1;
  101.                 state.Weight = w;
  102.             }
  103.         }
  104.  
  105.         int lastAnim = -1;
  106.         public void Update(uint animNum, float timeStep)
  107.         {
  108.             AnimationState state = model.GetAnimationState(animNum);
  109.             if (state != null)
  110.             {
  111.                 if (lastAnim != animNum)
  112.                 {
  113.                     //state.Time = 0;
  114.                     lastAnim = (int)animNum;
  115.                 }
  116.  
  117.                 state.AddTime(timeStep);
  118.             }
  119.         }
  120.  
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement