Example #1
0
        private ModelBone ProcessBone(
            EntityModel source,
            EntityModelBone bone,
            ref List <VertexPositionColorTexture> vertices,
            Vector2 textureSize,
            Dictionary <string, ModelBone> modelBones)
        {
            ModelBone modelBone;

            int startIndex   = vertices.Count;
            int elementCount = 0;

            if (bone.Cubes != null)
            {
                foreach (var cube in bone.Cubes)
                {
                    if (cube == null)
                    {
                        Log.Warn("Cube was null!");

                        continue;
                    }

                    var inflation = (float)(cube.Inflate ?? bone.Inflate);
                    var mirror    = cube.Mirror ?? bone.Mirror;

                    var origin = cube.InflatedOrigin(inflation);

                    MCMatrix matrix = MCMatrix.CreateTranslation(origin);
                    if (cube.Rotation.HasValue)
                    {
                        var rotation = cube.Rotation.Value;

                        Vector3 pivot = origin + (cube.InflatedSize(inflation) / 2f);

                        if (cube.Pivot.HasValue)
                        {
                            pivot = cube.InflatedPivot(inflation);                            // cube.Pivot.Value;
                        }

                        matrix =
                            MCMatrix.CreateTranslation(origin)
                            * MCMatrix.CreateTranslation((-pivot))
                            * MCMatrix.CreateRotationDegrees(rotation)
                            * MCMatrix.CreateTranslation(pivot);
                    }

                    Cube built = new Cube(cube, textureSize, mirror, inflation);
                    ModifyCubeIndexes(ref vertices, built.Front, origin, matrix);
                    ModifyCubeIndexes(ref vertices, built.Back, origin, matrix);
                    ModifyCubeIndexes(ref vertices, built.Top, origin, matrix);
                    ModifyCubeIndexes(ref vertices, built.Bottom, origin, matrix);
                    ModifyCubeIndexes(ref vertices, built.Left, origin, matrix);
                    ModifyCubeIndexes(ref vertices, built.Right, origin, matrix);
                }
            }

            elementCount = vertices.Count - startIndex;

            modelBone = new ModelBone(bone, startIndex, elementCount);

            if (bone.Rotation.HasValue)
            {
                var r = bone.Rotation.Value;
                modelBone.BindingRotation = new Vector3(r.X, r.Y, r.Z);
            }

            if (bone.BindPoseRotation.HasValue)
            {
                var r = bone.BindPoseRotation.Value;
                modelBone.BindingRotation += new Vector3(r.X, r.Y, r.Z);
            }

            foreach (var childBone in source.Bones.Where(
                         x => x.Parent != null && string.Equals(x.Parent, bone.Name, StringComparison.OrdinalIgnoreCase)))
            {
                if (childBone.Parent != null && childBone.Parent.Equals(childBone.Name))
                {
                    continue;
                }

                if (string.IsNullOrWhiteSpace(childBone.Name))
                {
                    childBone.Name = Guid.NewGuid().ToString();
                }

                var child = ProcessBone(source, childBone, ref vertices, textureSize, modelBones);
                //child.Parent = modelBone;

                modelBone.AddChild(child);

                if (!modelBones.TryAdd(childBone.Name, child))
                {
                    Log.Warn($"Failed to add bone! {childBone.Name}");
                    break;
                }
            }

            return(modelBone);
        }
Example #2
0
            public void Update(IUpdateArgs args,
                               MCMatrix characterMatrix)
            {
                if (_disposed)
                {
                    return;
                }

                //if (!Monitor.TryEnter(_disposeLock, 0))
                //	return;

                try
                {
                    //var device = args.GraphicsDevice;

                    if (CurrentAnim == null && Animations.TryDequeue(out var animation))
                    {
                        animation.Setup();
                        animation.Start();

                        CurrentAnim = animation;
                    }

                    if (CurrentAnim != null)
                    {
                        CurrentAnim.Update(args.GameTime);

                        if (CurrentAnim.IsFinished())
                        {
                            CurrentAnim.Reset();
                            CurrentAnim = null;
                        }
                    }


                    if (Definition.Pivot.HasValue)
                    {
                        var pivot = (Definition.Pivot ?? Vector3.Zero);
                        WorldMatrix = MCMatrix.CreateTranslation(-pivot)
                                      * MCMatrix.CreateRotationDegrees(BindingRotation + Rotation)
                                      * MCMatrix.CreateTranslation(pivot)
                                      * characterMatrix;
                    }
                    else
                    {
                        WorldMatrix = MCMatrix.CreateRotationDegrees(BindingRotation + Rotation)
                                      * characterMatrix;
                    }

                    var children = Children.ToArray();

                    if (children.Length > 0)
                    {
                        foreach (var child in children)
                        {
                            child.Update(args, WorldMatrix);
                        }
                    }
                }
                finally
                {
                    //	Monitor.Exit(_disposeLock);
                }
            }