/// <summary>
        /// Draws one model using its instance data
        /// </summary>
        /// <param name="context"></param>
        /// <param name="effect"></param>
        /// <param name="instance"></param>
        public void Draw(DeviceContext context, HLSLVoxelModel effect, VoxelModelInstance instance)
        {
            if (!_initialized)
            {
                return;
            }

            var state = instance.State;

            if (_model.ColorMapping != null)
            {
                effect.CBPerModel.Values.ColorMapping = _model.ColorMapping.BlockColors;
            }

            effect.CBPerModel.Values.World      = Matrix.Transpose(instance.World);
            effect.CBPerModel.Values.LightColor = instance.LightColor;
            effect.CBPerModel.Values.Alpha      = instance.Alpha;
            effect.CBPerModel.IsDirty           = true;

            // draw each part of the model
            for (int i = 0; i < state.PartsStates.Count; i++)
            {
                var voxelModelPartState = state.PartsStates[i];

                if (voxelModelPartState.ActiveFrame == byte.MaxValue)
                {
                    continue;
                }

                var vb = _visualFrames[voxelModelPartState.ActiveFrame].VertexBuffer;
                var ib = _visualFrames[voxelModelPartState.ActiveFrame].IndexBuffer;

                vb.SetToDevice(context, 0);
                ib.SetToDevice(context, 0);

                var frame = _model.Frames[voxelModelPartState.ActiveFrame];

                if (frame.ColorMapping != null)
                {
                    effect.CBPerModel.Values.ColorMapping = frame.ColorMapping.BlockColors;
                    effect.CBPerModel.IsDirty             = true;
                }

                if (_model.Parts[i].IsHead)
                {
                    var bb = _visualFrames[voxelModelPartState.ActiveFrame].BoundingBox;
                    RotateHead(voxelModelPartState, instance, bb, out effect.CBPerPart.Values.Transform);
                    effect.CBPerPart.Values.Transform = Matrix.Transpose(effect.CBPerPart.Values.Transform);
                }
                else
                {
                    effect.CBPerPart.Values.Transform = Matrix.Transpose(voxelModelPartState.GetTransformation() * Matrix.RotationQuaternion(instance.Rotation));
                }

                effect.CBPerPart.IsDirty = true;
                effect.Apply(context);

                context.DrawIndexed(ib.IndicesCount, 0, 0);
            }
        }
        private void DrawingArm(DeviceContext context)
        {
            //Prepare DirectX rendering pipeline
            context.ClearDepthStencilView(_d3dEngine.DepthStencilTarget, DepthStencilClearFlags.Depth, 1.0f, 0);
            RenderStatesRepo.ApplyStates(context, DXStates.Rasters.Default, DXStates.Blenders.Disabled, DXStates.DepthStencils.DepthReadWriteEnabled);

            //Compute a "world matrix" for displaying the Arm
            var screenPosition =
                Matrix.RotationX(MathHelper.Pi * 1.3f) * Matrix.RotationY(MathHelper.Pi * 0.01f) * //Some rotations
                Matrix.Scaling(1.7f) *                                                             //Adjusting scale
                Matrix.Translation(0.1f, -1, 0) *                                                  //Translation
                Matrix.Invert(_camManager.ActiveCamera.View_focused) *                             //Keep the Arm On screen
                Matrix.Translation(_camManager.ActiveCamera.LookAt.ValueInterp * 2.5f);            //Project the arm in the lookat Direction = Inside the screen

            //Prepare Effect
            _voxelModelEffect.Begin(context);
            _voxelModelEffect.CBPerFrame.Values.ViewProjection = Matrix.Transpose(_camManager.ActiveCamera.ViewProjection3D_focused);
            _voxelModelEffect.CBPerFrame.IsDirty = true;

            //Assign model variables values
            _voxelModelEffect.CBPerModel.Values.World      = Matrix.Transpose(Matrix.Scaling(1f / 16) * screenPosition);
            _voxelModelEffect.CBPerModel.Values.LightColor = _lightColor.ValueInterp;
            _voxelModelEffect.CBPerModel.IsDirty           = true;

            //Don't use the GetTransform of the Part because its linked to the entity body, here we have only the arm to display.
            _voxelModelEffect.CBPerPart.Values.Transform = Matrix.Transpose(Matrix.Identity);
            _voxelModelEffect.CBPerPart.IsDirty          = true;

            //Assign color mappings
            var colorMapping = _player.ModelInstance.VoxelModel.Frames[_handState.ActiveFrame].ColorMapping;

            if (colorMapping != null)
            {
                _voxelModelEffect.CBPerModel.Values.ColorMapping = colorMapping.BlockColors;
            }
            else
            {
                _voxelModelEffect.CBPerModel.Values.ColorMapping = _player.ModelInstance.VoxelModel.ColorMapping.BlockColors;
            }

            _voxelModelEffect.CBPerModel.IsDirty = true;

            //Assign buffers
            var vb = _handVisualVoxelModel.VertexBuffer;
            var ib = _handVisualVoxelModel.IndexBuffer;

            vb.SetToDevice(context, 0);
            ib.SetToDevice(context, 0);

            //Push Effect
            _voxelModelEffect.Apply(context);

            //Draw !
            context.DrawIndexed(ib.IndicesCount, 0, 0);
        }
        public void VoxelDraw(DeviceContext context, Matrix viewProjection)
        {
            //Applying Correct Render States
            RenderStatesRepo.ApplyStates(context, DXStates.Rasters.Default, DXStates.Blenders.Disabled, DXStates.DepthStencils.DepthReadWriteEnabled);
            _voxelModelEffect.Begin(context);
            _voxelModelEffect.CBPerFrame.Values.SunVector      = _skyDome.LightDirection;
            _voxelModelEffect.CBPerFrame.Values.ViewProjection = Matrix.Transpose(viewProjection);
            _voxelModelEffect.CBPerFrame.IsDirty = true;
            _voxelModelEffect.Apply(context);

            //Draw each buffered Models =====================================
            foreach (var modelAndInstances in _models)
            {
                //For each instance of the model that have received a body
                foreach (var pairs in modelAndInstances.Value.Instances.Where(x => x.Value != null))
                {
                    var entityToRender = _dynamicEntitiesDico[pairs.Key];
                    var modelInstance  = pairs.Value;
                    //Draw only the entities that are in Client view range
                    //if (_visualWorldParameters.WorldRange.Contains(entityToRender.VisualEntity.Position.ToCubePosition()))
                    bool isInRenderingRange = MVector3.Distance2D(entityToRender.VoxelEntity.Position, _camManager.ActiveCamera.WorldPosition.ValueInterp) <= _staticEntityViewRange;
                    //Can only see dead persons if your are dead yourself or if its your own body !
                    bool showCharacters = entityToRender.DynamicEntity.HealthState != DynamicEntityHealthState.Dead || IsLocalPlayer(entityToRender.DynamicEntity.DynamicId) || _playerEntityManager.Player.HealthState == DynamicEntityHealthState.Dead;
                    if (isInRenderingRange && showCharacters)
                    {
                        modelInstance.World      = Matrix.Scaling(1f / 16) * Matrix.Translation(entityToRender.WorldPosition.ValueInterp.AsVector3());
                        modelInstance.LightColor = entityToRender.ModelLight.ValueInterp;
                    }
                    else
                    {
                        modelInstance.World = Matrix.Zero;
                    }
                }

                if (modelAndInstances.Value.VisualModel != null && modelAndInstances.Value.Instances != null)
                {
                    var instancesToDraw = modelAndInstances.Value.Instances.Values.Where(x => x.World != Matrix.Zero).ToList(); //Draw only those where the matrix is different than Zero
                    modelAndInstances.Value.VisualModel.DrawInstanced(_d3DEngine.ImmediateContext, _voxelModelEffect, instancesToDraw);
                }
            }

            _voxelToolEffect.Begin(context);
            _voxelToolEffect.CBPerFrame.Values.LightDirection = _skyDome.LightDirection;
            _voxelToolEffect.CBPerFrame.Values.ViewProjection = Matrix.Transpose(viewProjection);
            _voxelToolEffect.CBPerFrame.IsDirty = true;
            _voxelToolEffect.Apply(context);

            // draw tools ================
            foreach (var pair in _dynamicEntitiesDico)
            {
                var charEntity = pair.Value.DynamicEntity as CharacterEntity;
                if (charEntity != null && pair.Value.ModelInstance != null && pair.Value.ModelInstance.World != Matrix.Zero)
                {
                    //Take the Tool entity equiped in character Right hand
                    if (charEntity.Equipment.RightTool is CubeResource)
                    {
                        DrawCube(context, (CubeResource)charEntity.Equipment.RightTool, charEntity);
                    }
                    else if (charEntity.Equipment.RightTool is IVoxelEntity)
                    {
                        var voxelItem = charEntity.Equipment.RightTool as IVoxelEntity;
                        if (!string.IsNullOrEmpty(voxelItem.ModelName)) //Check if a voxel model is associated with the entity
                        {
                            DrawTool(voxelItem, charEntity);
                        }
                    }
                }
            }
        }