Exemplo n.º 1
0
        private void PrepareVoxelDraw(DeviceContext context, Matrix viewProjection)
        {
            if (DrawStaticInstanced)
            {
                var focusMatrix = Matrix.Translation(_camManager.ActiveCamera.WorldPosition.ValueInterp.AsVector3());
                focusMatrix.Invert();

                _voxelModelInstancedEffect.Begin(context);
                _voxelModelInstancedEffect.CBPerFrame.Values.SunVector           = Skydome.LightDirection;
                _voxelModelInstancedEffect.CBPerFrame.Values.ViewProjection      = Matrix.Transpose(viewProjection);
                _voxelModelInstancedEffect.CBPerFrame.Values.UseShadowMap        = ClientSettings.Current.Settings.GraphicalParameters.ShadowMap;
                _voxelModelInstancedEffect.CBPerFrame.Values.LightViewProjection = Matrix.Transpose(ShadowMap.LightViewProjection);
                _voxelModelInstancedEffect.CBPerFrame.Values.ShadowMapVars       = new Vector3(0.001f, 0.0002f, 0.004f);
                _voxelModelInstancedEffect.CBPerFrame.Values.Focus = Matrix.Transpose(focusMatrix);
                _voxelModelInstancedEffect.CBPerFrame.IsDirty      = true;

                if (ShadowMap.ShadowMap != null)
                {
                    //Depth Shadow Mapping !
                    _voxelModelInstancedEffect.ShadowMap.Value   = ShadowMap.ShadowMap.DepthMap;
                    _voxelModelInstancedEffect.ShadowMap.IsDirty = true;
                }
            }
            else
            {
                _voxelModelEffect.Begin(context);
                _voxelModelEffect.CBPerFrame.Values.LightDirection = Skydome.LightDirection;
                _voxelModelEffect.CBPerFrame.Values.ViewProjection = Matrix.Transpose(viewProjection);
                _voxelModelEffect.CBPerFrame.IsDirty = true;
            }
        }
        private void DrawingTool(DeviceContext context)
        {
            if (_renderingType == ToolRenderingType.Voxel && (_toolVoxelInstance == null || _toolVoxelModel == null || !_toolVoxelModel.Initialized))
            {
                return;
            }

            context.ClearDepthStencilView(_d3dEngine.DepthStencilTarget, DepthStencilClearFlags.Depth, 1.0f, 0);
            RenderStatesRepo.ApplyStates(context, DXStates.Rasters.Default, DXStates.Blenders.Disabled, DXStates.DepthStencils.DepthReadWriteEnabled);

            float scale;

            if (_renderingType == ToolRenderingType.Cube)
            {
                scale = 0.75f;
            }
            else
            {
                var voxelBB = _toolVoxelInstance.State.BoundingBox.GetSize();
                scale  = MathHelper.Min(1.0f, 16 / MathHelper.Max(MathHelper.Max(voxelBB.X, voxelBB.Y), voxelBB.Z));
                scale *= 1.20f;
            }

            var screenPosition = Matrix.RotationQuaternion(_animationRotation) * Matrix.RotationX(MathHelper.Pi / 8) * Matrix.Scaling(scale) *
                                 Matrix.Translation(new Vector3(1.0f, -1, -0.2f) + _animationOffset) *
                                 Matrix.Invert(_camManager.ActiveCamera.View_focused) *
                                 Matrix.Translation(_camManager.ActiveCamera.LookAt.ValueInterp * 1.8f);

            if (_renderingType == ToolRenderingType.Cube)
            {
                _cubeRenderer.Render(context,
                                     Matrix.Translation(0.3f, 0.3f, 0.3f) * screenPosition,
                                     _camManager.ActiveCamera.ViewProjection3D_focused,
                                     _lightColor.ValueInterp
                                     );
            }
            if (_renderingType == ToolRenderingType.Voxel && _toolVoxelModel != null)
            {
                _voxelModelEffect.Begin(context);
                _voxelModelEffect.CBPerFrame.Values.ViewProjection = Matrix.Transpose(_camManager.ActiveCamera.ViewProjection3D_focused);
                _voxelModelEffect.CBPerFrame.IsDirty = true;
                _toolVoxelInstance.World             = Matrix.Scaling(1f / 16) * screenPosition;
                _toolVoxelInstance.LightColor        = _lightColor.ValueInterp;

                _toolVoxelModel.Draw(context, _voxelModelEffect, _toolVoxelInstance);
            }
        }
Exemplo n.º 3
0
        public override void Draw(SharpDX.Direct3D11.DeviceContext context, int index)
        {
            if (Display && Transform.HasValue && _toolVoxelModel != null)
            {
                RenderStatesRepo.ApplyStates(context, DXStates.Rasters.Default, DXStates.Blenders.Enabled, DXStates.DepthStencils.DepthReadEnabled);

                _voxelModelEffect.Begin(context);
                _voxelModelEffect.CBPerFrame.Values.ViewProjection = Matrix.Transpose(_cameraManager.ActiveCamera.ViewProjection3D);
                _voxelModelEffect.CBPerFrame.IsDirty = true;

                _toolVoxelInstance.World      = Matrix.Scaling(1f / 16) * Transform.Value;
                _toolVoxelInstance.LightColor = new Color3(0.0f, 0.0f, 1f);
                _toolVoxelInstance.Alpha      = _alpha;

                _toolVoxelModel.Draw(context, _voxelModelEffect, _toolVoxelInstance);
            }
        }
Exemplo n.º 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);
        }
Exemplo n.º 5
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);
                        }
                    }
                }
            }
        }