예제 #1
0
        protected override void OnUpdate(GameTime gameTime)
        {
            var args = new UpdateArgs()
            {
                Camera         = World.Camera,
                GraphicsDevice = Graphics,
                GameTime       = gameTime
            };

            //	if (Alex.IsActive)
            {
                var newAspectRatio = Graphics.Viewport.AspectRatio;
                if (AspectRatio != newAspectRatio)
                {
                    World.Camera.UpdateAspectRatio(newAspectRatio);
                    AspectRatio = newAspectRatio;
                }

                UpdateRayTracer(Alex.GraphicsDevice, World);

                if (!_playingHud.Chat.Focused)
                {
                    World.Player.Controller.CheckInput = Alex.IsActive;
                    CheckInput(gameTime);
                }
                else
                {
                    World.Player.Controller.CheckInput = false;
                }

                SkyRenderer.Update(args);
                World.Update(args, SkyRenderer);

                var now = DateTime.UtcNow;
                if (now - _previousMemUpdate > TimeSpan.FromSeconds(5))
                {
                    _previousMemUpdate = now;

                    //Task.Run(() =>
                    {
                        _ramUsage = Environment.WorkingSet;

                        ThreadPool.GetMaxThreads(out int maxThreads, out int maxCompletionPorts);
                        ThreadPool.GetAvailableThreads(out int availableThreads, out int availableComplPorts);
                        _threadsUsed   = maxThreads - availableThreads;
                        _complPortUsed = maxCompletionPorts - availableComplPorts;

                        _maxThreads    = maxThreads;
                        _maxComplPorts = maxCompletionPorts;

                        var pos     = World.Player.KnownPosition.GetCoordinates3D();
                        var biomeId = World.GetBiome(pos.X, pos.Y, pos.Z);
                        var biome   = BiomeUtils.GetBiomeById(biomeId);
                        _currentBiomeId = biomeId;
                        _currentBiome   = biome;
                    }                    //);
                }
            }
            base.OnUpdate(gameTime);
        }
예제 #2
0
        protected override void OnUpdate(GameTime gameTime)
        {
            MiniMap.PlayerLocation = World.Player.KnownPosition;

            var args = new UpdateArgs()
            {
                Camera = World.Camera, GraphicsDevice = Graphics, GameTime = gameTime
            };

            _playingHud.CheckInput = Alex.GuiManager.ActiveDialog == null;

            //	if (Alex.IsActive)

            if (Math.Abs(AspectRatio - Graphics.Viewport.AspectRatio) > 0f)
            {
                World.Camera.UpdateAspectRatio(Graphics.Viewport.AspectRatio);
                AspectRatio = Graphics.Viewport.AspectRatio;
            }

            if (!_playingHud.Chat.Focused && Alex.GameStateManager.GetActiveState() is PlayingState)
            {
                World.Player.Controller.CheckMovementInput = Alex.IsActive && Alex.GuiManager.ActiveDialog == null;
                World.Player.Controller.CheckInput         = Alex.IsActive;

                if (Alex.GuiManager.ActiveDialog == null)
                {
                    CheckInput(gameTime);
                }
            }
            else
            {
                World.Player.Controller.CheckInput = false;
            }

            World.Update(args);

            var now = DateTime.UtcNow;

            if (now - _previousMemUpdate > TimeSpan.FromSeconds(5))
            {
                _previousMemUpdate = now;


                _ramUsage = Environment.WorkingSet;

                ThreadPool.GetMaxThreads(out int maxThreads, out _);
                ThreadPool.GetAvailableThreads(out int availableThreads, out _);
                _threadsUsed = maxThreads - availableThreads;

                _maxThreads = maxThreads;

                var pos     = World.Player.KnownPosition.GetCoordinates3D();
                var biomeId = World.GetBiome(pos.X, pos.Y, pos.Z);
                var biome   = BiomeUtils.GetBiomeById(biomeId);
                _currentBiomeId = biomeId;
                _currentBiome   = biome;
            }

            base.OnUpdate(gameTime);
        }
예제 #3
0
        protected void GetVertices(IBlockAccess world, ChunkData chunkBuilder, BlockCoordinates blockCoordinates,
                                   Vector3 position, Block baseBlock,
                                   BlockStateModel[] models)
        {
            {
                var biome = world == null?BiomeUtils.GetBiomeById(0) : world.GetBiome(position);

                if (UseRandomizer)
                {
                    BlockStateModel selectedModel = null;
                    var             rnd           = MathF.Abs(NoiseGenerator.GetValue(position.X * position.Y, position.Z * position.X)) * WeightSum;

                    for (var index = 0; index < models.Length; index++)
                    {
                        var model = models[index];
                        rnd -= model.Weight;

                        if (rnd < 0)
                        {
                            selectedModel = model;

                            break;
                        }
                    }

                    CalculateModel(
                        world, blockCoordinates, chunkBuilder, position, baseBlock, selectedModel, biome);
                }
                else
                {
                    for (var bsModelIndex = 0; bsModelIndex < models.Length; bsModelIndex++)
                    {
                        var bsModel = models[bsModelIndex];

                        if (bsModel.Model == null)
                        {
                            continue;
                        }

                        CalculateModel(
                            world, blockCoordinates, chunkBuilder, position, baseBlock, bsModel,
                            biome);
                    }
                }
            }
        }
예제 #4
0
        protected VerticesResult GetVertices(IBlockAccess world,
                                             Vector3 position, Block baseBlock,
                                             BlockStateModel[] models)
        {
            var verts = new List <BlockShaderVertex>();
            {
                var indexResult         = new List <int>(24 * models.Length);
                var animatedIndexResult = new List <int>();

                int biomeId = 0;                //world == null ? 0 : world.GetBiome((int) position.X, 0, (int) position.Z);
                var biome   = BiomeUtils.GetBiomeById(biomeId);

                if (UseRandomizer)
                {
                    //var rndIndex = FastRandom.Next() % Models.Length;
                    CalculateModel(
                        world, position, baseBlock, models[0], verts, indexResult, animatedIndexResult, biomeId, biome);
                }
                else
                {
                    for (var bsModelIndex = 0; bsModelIndex < models.Length; bsModelIndex++)
                    {
                        var bsModel = models[bsModelIndex];

                        if (bsModel.Model == null)
                        {
                            continue;
                        }

                        CalculateModel(
                            world, position, baseBlock, bsModel, verts, indexResult, animatedIndexResult, biomeId,
                            biome);
                    }
                }

                return(new VerticesResult(verts.ToArray(), indexResult.ToArray(), animatedIndexResult.Count > 0 ? animatedIndexResult.ToArray() : null));
            }
        }
예제 #5
0
        public override void GetVertices(IBlockAccess world, ChunkData chunkBuilder, BlockCoordinates blockCoordinates, Vector3 position, Block baseBlock)
        {
            var biome = world == null?BiomeUtils.GetBiomeById(0) : world.GetBiome(position);

            if (UseRandomizer)
            {
                BlockStateModelWrapper selectedModel = null;

                var rnd = MathF.Abs(NoiseGenerator.GetValue(position.X * position.Y, position.Z * position.X))
                          * WeightSum;

                for (var index = 0; index < Models.Length; index++)
                {
                    var model = Models[index];
                    rnd -= model.BlockStateModel.Weight;

                    if (rnd < 0)
                    {
                        selectedModel = model;

                        break;
                    }
                }

                CalculateModel(world, blockCoordinates, chunkBuilder, position, baseBlock, selectedModel.BlockStateModel, selectedModel.BlockModel, biome);
            }
            else
            {
                for (var bsModelIndex = 0; bsModelIndex < Models.Length; bsModelIndex++)
                {
                    var bsModel = Models[bsModelIndex];

                    //	if (string.IsNullOrWhiteSpace(bsModel.ModelName)) continue;

                    CalculateModel(world, blockCoordinates, chunkBuilder, position, baseBlock, bsModel.BlockStateModel, bsModel.BlockModel, biome);
                }
            }
        }
예제 #6
0
        protected (VertexPositionNormalTextureColor[] vertices, int[] indexes) GetVertices(IBlockAccess world,
                                                                                           Vector3 position, Block baseBlock,
                                                                                           BlockStateModel[] models)
        {
            using (var verts = new PooledList <VertexPositionNormalTextureColor>(ClearMode.Auto))
            {
                var indexResult = new List <int>(24 * models.Length);

                int biomeId = 0;                //world == null ? 0 : world.GetBiome((int) position.X, 0, (int) position.Z);
                var biome   = BiomeUtils.GetBiomeById(biomeId);

                if (UseRandomizer)
                {
                    //var rndIndex = FastRandom.Next() % Models.Length;
                    CalculateModel(
                        world, position, baseBlock, models[0], verts, indexResult, biomeId, biome);
                }
                else
                {
                    for (var bsModelIndex = 0; bsModelIndex < models.Length; bsModelIndex++)
                    {
                        var bsModel = models[bsModelIndex];

                        if (bsModel.Model == null)
                        {
                            continue;
                        }

                        CalculateModel(
                            world, position, baseBlock, bsModel, verts, indexResult, biomeId,
                            biome);
                    }
                }

                return(verts.ToArray(), indexResult.ToArray());
            }
        }
예제 #7
0
        protected (VertexPositionNormalTextureColor[] vertices, int[] indexes) GetVertices(IWorld world, Vector3 position, IBlock baseBlock,
                                                                                           BlockStateModel[] models, IDictionary <string, FaceCache> faceCache)
        {
            var verts       = new List <VertexPositionNormalTextureColor>(36);
            var indexResult = new List <int>();

            int biomeId = world.GetBiome((int)position.X, 0, (int)position.Z);
            var biome   = BiomeUtils.GetBiomeById(biomeId);

            for (var bsModelIndex = 0; bsModelIndex < models.Length; bsModelIndex++)
            {
                var bsModel = models[bsModelIndex];
                if (bsModel.Model == null)
                {
                    continue;
                }

                var model         = bsModel.Model;
                var modelElements = model.Elements;
                for (var i = 0; i < modelElements.Length; i++)
                {
                    FaceCache elementCache;
                    if (!faceCache.TryGetValue($"{bsModel.ModelName}:{bsModelIndex}:{i}", out elementCache))
                    {
                        Log.Warn($"Element cache is null!");
                        continue;
                    }

                    var element = modelElements[i];

                    foreach (var faceElement in element.Faces)
                    {
                        var facing = faceElement.Key;

                        GetCullFaceValues(faceElement.Value.CullFace, facing, out var cullFace);

                        var originalCullFace = cullFace;

                        if (bsModel.X > 0f)
                        {
                            var offset = (-bsModel.X) / 90;
                            cullFace = RotateDirection(cullFace, offset, FACE_ROTATION_X, INVALID_FACE_ROTATION_X);
                            facing   = RotateDirection(facing, offset, FACE_ROTATION_X, INVALID_FACE_ROTATION_X);
                        }

                        if (bsModel.Y > 0f)
                        {
                            var offset = (-bsModel.Y) / 90;
                            cullFace = RotateDirection(cullFace, offset, FACE_ROTATION, INVALID_FACE_ROTATION);
                            facing   = RotateDirection(facing, offset, FACE_ROTATION, INVALID_FACE_ROTATION);
                        }

                        if (originalCullFace != BlockFace.None && !ShouldRenderFace(world, facing, position, baseBlock))
                        {
                            continue;
                        }


                        FaceData faceVertices;
                        if (!elementCache.TryGet(faceElement.Key, out faceVertices) || faceVertices.Vertices.Length == 0 || faceVertices.Indexes.Length == 0)
                        {
                            //Log.Debug($"No vertices cached for face {faceElement.Key} in model {bsModel.ModelName}");
                            continue;
                        }

                        Color faceColor = faceVertices.Vertices[0].Color;

                        if (faceElement.Value.TintIndex >= 0)
                        {
                            if (biomeId != -1)
                            {
                                if (baseBlock.Name.Equals("grass_block", StringComparison.InvariantCultureIgnoreCase))
                                {
                                    faceColor = Resources.ResourcePack.GetGrassColor(biome.Temperature, biome.Downfall,
                                                                                     (int)position.Y);
                                }
                                else
                                {
                                    faceColor = Resources.ResourcePack.GetFoliageColor(biome.Temperature,
                                                                                       biome.Downfall, (int)position.Y);
                                }
                            }
                        }

                        faceColor = AdjustColor(faceColor, facing,
                                                GetLight(world, position + cullFace.GetVector3(),
                                                         false), element.Shade);

                        var initialIndex = verts.Count;
                        for (var index = 0; index < faceVertices.Vertices.Length; index++)
                        {
                            var vertex = faceVertices.Vertices[index];
                            vertex.Color    = faceColor;
                            vertex.Position = position + vertex.Position;

                            verts.Add(vertex);
                        }

                        for (var index = 0; index < faceVertices.Indexes.Length; index++)
                        {
                            var idx = faceVertices.Indexes[index];
                            indexResult.Add(initialIndex + idx);
                        }
                    }
                }
            }


            return(verts.ToArray(), indexResult.ToArray());
        }
예제 #8
0
        public override void GetVertices(IBlockAccess world,
                                         ChunkData chunkBuilder,
                                         BlockCoordinates blockCoordinates,
                                         Vector3 position,
                                         BlockState baseBlock)
        {
            var biome = world == null?BiomeUtils.GetBiomeById(0) : world.GetBiome(position);


            if (baseBlock.ModelData == null)
            {
                return;
            }

            if (baseBlock.VariantMapper.IsMultiPart)
            {
                foreach (var model in baseBlock.ModelData)
                {
                    if (Resources.BlockModelRegistry.TryGet(model.ModelName, out var registryEntry))
                    {
                        CalculateModel(
                            world, blockCoordinates, chunkBuilder, position, baseBlock.Block, model,
                            registryEntry.Value, biome);
                    }
                }

                return;
            }

            //	if (UseRandomizer)
            {
                BlockStateModel selectedModel = null;

                if (baseBlock.ModelData.Count > 1)
                {
                    var weightSum = baseBlock.ModelData.Sum(x => x.Weight);

                    var rnd = ((baseBlock.ModelData.Count == 1) ? 1f : MathF.Abs(
                                   NoiseGenerator.GetValue(position.X * position.Y, position.Z * position.X))) * weightSum;

                    //for (var index = 0; index < Models.Length; index++)
                    foreach (var model in baseBlock.ModelData)
                    {
                        //	var model = Models[index];
                        rnd -= model.Weight;

                        if (rnd < 0)
                        {
                            selectedModel = model;

                            break;
                        }
                    }
                }
                else
                {
                    selectedModel = baseBlock.ModelData.FirstOrDefault();
                }

                if (selectedModel == null)
                {
                    return;
                }

                if (Resources.BlockModelRegistry.TryGet(selectedModel.ModelName, out var registryEntry))
                {
                    CalculateModel(
                        world, blockCoordinates, chunkBuilder, position, baseBlock.Block, selectedModel,
                        registryEntry.Value, biome);
                }

                //	CalculateModel(world, blockCoordinates, chunkBuilder, position, baseBlock, selectedModel.BlockStateModel, selectedModel.BlockModel, biome);
            }
            //else
            //{
            //for (var bsModelIndex = 0; bsModelIndex < Models.Length; bsModelIndex++)
            //{
            //var bsModel = Models[bsModelIndex];

            //	if (string.IsNullOrWhiteSpace(bsModel.ModelName)) continue;

            //}
            //}
        }