コード例 #1
0
        /// <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);
            }
        }
コード例 #2
0
        //The tool has been changed !
        private void ToolChange()
        {
            //Is it a CubeResource ?
            if (_tool is CubeResource)
            {
                _renderingType = ToolRenderingType.Cube;
                _cubeRenderer.PrepareCubeRendering((CubeResource)_tool);
            }
            else if (_tool is IVoxelEntity) //A voxel Entity ?
            {
                logger.Info("Voxel Entity tool equipped : {0}", _tool.Name);

                var voxelEntity = _tool as IVoxelEntity;
                _renderingType  = ToolRenderingType.Voxel;
                _toolVoxelModel = _voxelModelManager.GetModel(voxelEntity.ModelName);

                if (_toolVoxelModel != null)
                {
                    if (!_toolVoxelModel.Initialized)
                    {
                        _toolVoxelModel.BuildMesh();
                    }

                    _toolVoxelInstance = _toolVoxelModel.VoxelModel.CreateInstance();
                    _toolVoxelInstance.SetState(_toolVoxelModel.VoxelModel.GetMainState());
                }
                else
                {
                    logger.Info("Unable to display the voxel model");
                }
            }
        }
コード例 #3
0
        private void RotateHead(VoxelModelPartState partState, VoxelModelInstance instance, BoundingBox bb, out Matrix result)
        {
            var move = (bb.Maximum - bb.Minimum) / 2;

            var partTransform = partState.GetTransformation();

            // get the point of the head center
            move = Vector3.TransformCoordinate(move, partTransform * Matrix.RotationQuaternion(instance.Rotation));

            result = partTransform *
                     Matrix.RotationQuaternion(instance.Rotation) *
                     Matrix.Translation(-move) *
                     Matrix.RotationQuaternion(Quaternion.Invert(instance.Rotation)) *
                     Matrix.RotationQuaternion(instance.HeadRotation) *
                     Matrix.Translation(move);
        }
コード例 #4
0
        protected override void OnInstanceChanged(VoxelModelInstance prev)
        {
            if (prev != null)
            {
                prev.StateChanged -= ModelInstanceOnStateChanged;
            }

            if (ModelInstance != null)
            {
                var newState = IsOpen ? OpenedState : ClosedState;
                if (!string.IsNullOrEmpty(newState))
                {
                    ModelInstance.SetState(newState);
                }

                ModelInstance.StateChanged += ModelInstanceOnStateChanged;
            }
        }
コード例 #5
0
 protected override void OnInstanceChanged(VoxelModelInstance prev)
 {
     if (ModelInstance != null)
     {
         if (base.Locked)
         {
             if (!string.IsNullOrEmpty(OpenedState))
             {
                 ModelInstance.SetState(OpenedState);
             }
         }
         else
         {
             if (!string.IsNullOrEmpty(ClosedState))
             {
                 ModelInstance.SetState(ClosedState);
             }
         }
     }
 }
コード例 #6
0
        private void PrepareModel()
        {
            var voxelEntity = (IVoxelEntity)Tool;

            if (voxelEntity == null || string.IsNullOrEmpty(voxelEntity.ModelName))
            {
                _toolVoxelModel = null;
                return;
            }

            _toolVoxelModel = _voxelModelManager.GetModel(voxelEntity.ModelName);

            if (_toolVoxelModel != null)
            {
                if (!_toolVoxelModel.Initialized)
                {
                    _toolVoxelModel.BuildMesh();
                }

                _toolVoxelInstance = _toolVoxelModel.VoxelModel.CreateInstance();
                _toolVoxelInstance.SetState(_toolVoxelModel.VoxelModel.GetMainState());
            }
        }
コード例 #7
0
 /// <summary>
 /// Executed when the model instance is changed
 /// Allows to initialize the instance
 /// </summary>
 protected virtual void OnInstanceChanged(VoxelModelInstance previousInstance)
 {
 }
コード例 #8
0
        /// <summary>
        /// Add an entity to the dynamic entity collection, all visual part of the entity (voxel) will be created here
        /// </summary>
        /// <param name="entity">the entity</param>
        /// <param name="withNetworkInterpolation">the entity movement will be interpolated between each move/rotate changes</param>
        public void AddEntity(ICharacterEntity entity, bool withNetworkInterpolation)
        {
            string entityVoxelName = entity.ModelName;

            if (entity.HealthState == DynamicEntityHealthState.Dead)
            {
                entityVoxelName = "Ghost";
            }

            //Do we already have this entity ??
            if (_dynamicEntitiesDico.ContainsKey(entity.DynamicId) == false)
            {
                //subscribe to entity state/afllication changes
                entity.HealthStateChanged     += EntityHealthStateChanged;
                entity.HealthChanged          += entity_HealthChanged;
                entity.AfflictionStateChanged += EntityAfflictionStateChanged;

                ModelAndInstances modelWithInstances;
                //Does the entity model exist in the collection ?
                //If yes, will send back the instance Ids registered to this Model
                if (!_models.TryGetValue(entityVoxelName, out modelWithInstances))
                {
                    //Model not existing ====
                    // load a new model
                    modelWithInstances = new ModelAndInstances
                    {
                        VisualModel = _voxelModelManager.GetModel(entityVoxelName), //Get the model from the VoxelModelManager
                        Instances   = new Dictionary <uint, VoxelModelInstance>()   //Create a new dico of modelInstance
                    };

                    //If the voxel model was send back by the manager, create the Mesh from it (Vx and idx buffers)
                    if (modelWithInstances.VisualModel != null)
                    {
                        // todo: probably do this in another thread
                        modelWithInstances.VisualModel.BuildMesh();
                    }
                    _models.Add(entityVoxelName, modelWithInstances);
                }

                VoxelModelInstance instance;

                //If the Model has a Voxel Model (Search by Name)
                if (modelWithInstances.VisualModel != null)
                {
                    //Create a new Instance of the Model
                    instance             = new VoxelModelInstance(modelWithInstances.VisualModel.VoxelModel);
                    entity.ModelInstance = instance;
                    modelWithInstances.Instances.Add(entity.DynamicId, instance);
                }
                else
                {
                    //Add a new instance for the model, but without Voxel Body (=null instance)
                    modelWithInstances.Instances.Add(entity.DynamicId, null);
                }

                VisualDynamicEntity newEntity = CreateVisualEntity(entity);
                newEntity.WithNetworkInterpolation = withNetworkInterpolation;
                _dynamicEntitiesDico.Add(entity.DynamicId, newEntity);
                DynamicEntities.Add(newEntity);

                OnEntityAdded(new DynamicEntityEventArgs {
                    Entity = entity
                });
            }
        }
コード例 #9
0
 public void SetInstance(VoxelModelInstance instance)
 {
     _instance = instance;
 }