public override void LoadContent(DeviceContext context)
        {
            _cubeRenderer.LoadContent(context);

            _voxelModelEffect = ToDispose(new HLSLVoxelModel(context.Device, ClientSettings.EffectPack + @"Entities\VoxelModel.hlsl", VertexVoxel.VertexDeclaration));

            Tool = _player.Equipment.RightTool;

            _playerModel = _voxelModelManager.GetModel(_player.ModelName);
            if (_playerModel != null)
            {
                if (!_playerModel.Initialized)
                {
                    _playerModel.BuildMesh();
                }

                if (_player.ModelInstance == null)
                {
                    _player.ModelInstance = _playerModel.VoxelModel.CreateInstance();

                    //Get Voxel Arm of the character.
                    var armPartIndex = _player.ModelInstance.VoxelModel.GetArmIndex();
                    _handVoxelModel       = _player.ModelInstance.VoxelModel.Parts[armPartIndex];
                    _handState            = _player.ModelInstance.VoxelModel.GetMainState().PartsStates[armPartIndex];
                    _handVisualVoxelModel = _playerModel.VisualVoxelFrames[armPartIndex];
                }
            }
        }
        //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 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());
            }
        }
예제 #4
0
        public Texture2D CreateVoxelIcon(VisualVoxelModel visualVoxelModel, Size2 iconSize, VoxelModelState state = null, DeviceContext context = null, Matrix transform = default(Matrix))
        {
            if (context == null)
            {
                context = _d3DEngine.ImmediateContext;
            }

            //Create the render texture
            var texture = ToDispose(new RenderedTexture2D(_d3DEngine, iconSize.Width, iconSize.Height, Format.R8G8B8A8_UNorm)
            {
                BackGroundColor = new Color4(0, 0, 0, 0)
            });

            float  aspectRatio = IconSize / IconSize;
            Matrix projection;
            var    fov = (float)Math.PI / 3.6f;

            Matrix.PerspectiveFovLH(fov, aspectRatio, 0.5f, 100f, out projection);
            Matrix view = Matrix.LookAtLH(new Vector3(0, 0, -1.9f), Vector3.Zero, Vector3.UnitY);

            texture.Begin(context);

            RenderStatesRepo.ApplyStates(context, DXStates.Rasters.Default, DXStates.Blenders.Enabled, DXStates.DepthStencils.DepthReadWriteEnabled);

            _voxelEffect.Begin(context);

            _voxelEffect.CBPerFrame.Values.LightDirection = Vector3.Zero;
            _voxelEffect.CBPerFrame.Values.ViewProjection = Matrix.Transpose(view * projection);
            _voxelEffect.CBPerFrame.IsDirty = true;

            var instance = visualVoxelModel.VoxelModel.CreateInstance();

            if (state == null)
            {
                var iconState = visualVoxelModel.VoxelModel.States.FirstOrDefault(s => string.Equals(s.Name, "Icon", StringComparison.CurrentCultureIgnoreCase));
                state = iconState ?? visualVoxelModel.VoxelModel.GetMainState();
            }

            instance.SetState(state);

            var sphere = BoundingSphere.FromBox(state.BoundingBox);

            var rMax = 2f * Math.Sin(fov / 2);

            var size = state.BoundingBox.GetSize();

            var offset = -size / 2 - state.BoundingBox.Minimum;

            var scale = (float)rMax / sphere.Radius; // Math.Min(scaleFactor / size.X, Math.Min(scaleFactor / size.Y, scaleFactor / size.Z));

            if (transform == default(Matrix))
            {
                instance.World = Matrix.Translation(offset) * Matrix.Scaling(scale) * Matrix.RotationY(MathHelper.Pi + MathHelper.PiOver4) * Matrix.RotationX(-MathHelper.Pi / 5);
            }
            else
            {
                instance.World = transform;
            }

            visualVoxelModel.Draw(context, _voxelEffect, instance);

            texture.End(context, false);


            var tex2D = texture.CloneTexture(context, ResourceUsage.Default);

            tex2D = DrawOuterShadow(context, texture, tex2D, iconSize.Width);

            _d3DEngine.SetRenderTargetsAndViewPort(context);

            return(tex2D);
        }
예제 #5
0
        private void AddVoxelEntity(EntityCollectionEventArgs e)
        {
            var voxelEntity = e.Entity as IVoxelEntity;

            if (voxelEntity == null)
            {
                return; //My static entity is not a Voxel Entity => Not possible to render it so !!!
            }
            //Create the Voxel Model Instance for the Item
            VisualVoxelModel model = null;

            if (!string.IsNullOrEmpty(voxelEntity.ModelName))
            {
                model = _voxelModelManager.GetModel(voxelEntity.ModelName, false);
            }
            if (model != null && voxelEntity.ModelInstance == null) //The model blueprint is existing, and I need to create an instance of it !
            {
                var treeGrowing = e.Entity as TreeGrowingEntity;
                if (treeGrowing != null)
                {
                    if (treeGrowing.Scale > 0)
                    {
                        // we need to use generated voxel model
                        TreeBpSeed key;
                        key.TreeTypeId = treeGrowing.TreeTypeId;
                        key.TreeSeed   = treeGrowing.TreeRndSeed;

                        VisualVoxelModel treeModel;

                        if (_cachedTrees.TryGetValue(key, out treeModel))
                        {
                            model = treeModel;
                        }
                        else
                        {
                            var voxelModel = VoxelModel.GenerateTreeModel(treeGrowing.TreeRndSeed,
                                                                          _visualWorldParameters.WorldParameters.Configuration.TreeBluePrintsDico[
                                                                              treeGrowing.TreeTypeId]);

                            model = new VisualVoxelModel(voxelModel, _voxelModelManager.VoxelMeshFactory);
                            model.BuildMesh();

                            _cachedTrees.Add(key, model);
                        }
                    }
                }

                var treeSoul = e.Entity as TreeSoul;
                if (treeSoul != null)
                {
                    TreeBpSeed key;
                    key.TreeTypeId = treeSoul.TreeTypeId;
                    key.TreeSeed   = treeSoul.TreeRndSeed;

                    _cachedTrees.Remove(key);
                }


                voxelEntity.ModelInstance = new VoxelModelInstance(model.VoxelModel);

                //Assign state in case of growing entity !
                var growingEntity = e.Entity as PlantGrowingEntity;
                if (growingEntity != null)
                {
                    voxelEntity.ModelInstance.SetState(growingEntity.GrowLevels[growingEntity.CurrentGrowLevelIndex].ModelState);
                }

                var visualVoxelEntity = new VisualVoxelEntity(voxelEntity, _voxelModelManager);
                //Get default world translation
                Matrix instanceTranslation = Matrix.Translation(voxelEntity.Position.AsVector3());

                //Apply special rotation to the creation instance
                Quaternion instanceRotation = Quaternion.Identity;
                if (voxelEntity is IRndYRotation && ((IRndYRotation)voxelEntity).RndRotationAroundY)
                {
                    instanceRotation = Quaternion.RotationAxis(Vector3.UnitY, (float)(_rnd.NextDouble() * MathHelper.TwoPi));
                }
                else if (voxelEntity is IItem)
                {
                    var item = voxelEntity as IItem;
                    instanceRotation = item.Rotation;
                }

                //Apply special scaling to created entity (By default all blue print are 16 times too big.
                Matrix instanceScaling = Matrix.Scaling(1.0f / 16.0f);

                if (treeGrowing != null && treeGrowing.Scale > 0)
                {
                    instanceScaling = Matrix.Scaling(treeGrowing.Scale);
                }

                //Create the World transformation matrix for the instance.
                //We take the Model instance world matrix where we add a Rotation and scaling proper to the instance
                visualVoxelEntity.VoxelEntity.ModelInstance.World    = instanceScaling * instanceTranslation;
                visualVoxelEntity.VoxelEntity.ModelInstance.Rotation = instanceRotation;

                var result = GetCube(visualVoxelEntity.VoxelEntity.Position.ToCubePosition());


                if (result.IsValid)
                {
                    visualVoxelEntity.BlockLight = result.Cube.EmissiveColor;
                }
                else
                {
                    visualVoxelEntity.BlockLight = new ByteColor(255, 255, 255, 255);
                }


                if (visualVoxelEntity.VisualVoxelModel.Initialized == false)
                {
                    visualVoxelEntity.VisualVoxelModel.BuildMesh();
                }

                if (voxelEntity.ModelInstance.CanPlay("Idle"))
                {
                    voxelEntity.ModelInstance.Play("Idle", true);
                }

                lock (_syncRoot)
                {
                    List <VisualVoxelEntity> list;
                    if (_visualVoxelEntities.TryGetValue(voxelEntity.ModelName, out list))
                    {
                        list.Add(visualVoxelEntity);
                    }
                    else
                    {
                        _visualVoxelEntities.Add(voxelEntity.ModelName, new List <VisualVoxelEntity> {
                            visualVoxelEntity
                        });
                    }
                }

                var lightEntity = e.Entity as ILightEmitterEntity;
                if (e.AtChunkCreationTime == false && lightEntity != null)
                {
                    //Get the Cube where is located the entity
                    var entityWorldPosition = lightEntity.Position;
                    var entityBlockPosition = new Vector3I(MathHelper.Floor(entityWorldPosition.X),
                                                           MathHelper.Floor(entityWorldPosition.Y),
                                                           MathHelper.Floor(entityWorldPosition.Z));
                    //new TerraCubeWithPosition(entityBlockPosition, WorldConfiguration.CubeId.Air, _visualWorldParameters.WorldParameters.Configuration),
                    this.UpdateOrder = 1;
                    var cubeRange = new Range3I
                    {
                        Position = new Vector3I(entityBlockPosition.X, 0, entityBlockPosition.Z),
                        Size     = Vector3I.One
                    };
                    _chunkEntityImpactManager.CheckImpact(this, cubeRange);
                }
            }
        }