Esempio n. 1
0
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            QuadTreeNode.CalculateIndexData(GraphicsDevice);

            _oldMouseState    = Mouse.GetState();
            _oldKeyboardState = Keyboard.GetState();

            int            worldSeed               = 1;
            NoiseProvider  mainNoiseProvider       = new NoiseProvider(worldSeed, 4, 0.3f, 6000);
            NoiseProvider  modulationNoiseProvider = new NoiseProvider(worldSeed + 1, 2, 0.2f, 50000);
            DirectionLight light      = new DirectionLight(Vector3.Normalize(new Vector3(0, -0.3f, 1)), 1, 0.1f);
            Atmosphere     atmosphere = new Atmosphere(150000, new Color(0.3f, 0.5f, 0.9f));

            _world = new World(GraphicsDevice, new TerrainHeightProvider(mainNoiseProvider, modulationNoiseProvider), 1737000, light, new Random(worldSeed), atmosphere);

            Matrix cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60), GraphicsDevice.Viewport.AspectRatio, CentredCamera.NearPlaneDistance, CentredCamera.FarPlaneDistance);

            _camera = new CentredCamera()
            {
                Position         = new Vector3Double(0, _world.Radius + 3000, 0),
                Orientation      = Matrix.Identity,
                ProjectionMatrix = cameraProjectionMatrix,
                MovementSpeed    = 1000
            };

            _starTexture = new Texture2D(GraphicsDevice, 1, 1);
            _starTexture.SetData(new Color[] { Color.White });

            _spriteBatch = new SpriteBatch(GraphicsDevice);

            base.Initialize();
        }
Esempio n. 2
0
    /// <summary>
    /// Applies the textures.
    /// </summary>
    /// <param name="terrainData">Terrain data.</param>
    private void    ApplyTextures(TerrainData terrainData)
    {
        List <SplatPrototype> splats = new List <SplatPrototype>();

        for (int i = 0; i < Setting.Textures.Count; i++)
        {
            SplatPrototype splat = new SplatPrototype();
            splat.texture = Setting.Textures[i];
            splats.Add(splat);
        }

        terrainData.splatPrototypes = splats.ToArray();
        terrainData.RefreshPrototypes();

        Texture2D alphamap = NoiseProvider.GetAlphamap();
        // proc alphamap
        var splatMap = new float[terrainData.alphamapResolution, terrainData.alphamapResolution, splats.Count];

        for (var zRes = 0; zRes < terrainData.alphamapHeight; zRes++)
        {
            for (var xRes = 0; xRes < terrainData.alphamapWidth; xRes++)
            {
                Color clr = alphamap.GetPixel(zRes, xRes);

                splatMap[zRes, xRes, 0] = clr.r;
                splatMap[zRes, xRes, 1] = clr.g;
                splatMap[zRes, xRes, 2] = clr.b;
                splatMap[zRes, xRes, 3] = clr.a;
            }
        }

        terrainData.SetAlphamaps(0, 0, splatMap);
    }
Esempio n. 3
0
    private Vector3 Perturb(Vector3 position)
    {
        var sample = NoiseProvider.SampleNoise(position);

        position.x += (sample.x * 2f - 1f) * HexMetrics.cellPerturbStrength;
        position.z += (sample.z * 2f - 1f) * HexMetrics.cellPerturbStrength;
        return(position);
    }
Esempio n. 4
0
    public ZNoise(Vector2i positionKey, NoiseProvider noise, int range, int sizeToGenerate)
    {
        //Debug.Log("Omg!");
        Positionkey    = positionKey;
        SizeToGenerate = sizeToGenerate;
        int Seed = Positionkey.GetHashCode();

        Noise = noise;
        //Debug.Log(PerlinNoise.Seed);
        Range = range;
    }
Esempio n. 5
0
    private void Awake()
    {
        cellCountX = chunkCountX * HexMetrics.chunkSizeX;
        cellCountZ = chunkCountZ * HexMetrics.chunkSizeZ;
        cells      = new HexCell[cellCountZ * cellCountX];

        NoiseProvider.Init(mapNoise);

        CreateChunks();
        CreateCells();
    }
Esempio n. 6
0
        public World(GraphicsDevice graphicsDevice, HeightProvider heightProvider, DirectionLight light, NoiseProvider decorNoiseProvider)
        {
            _graphicsDevice = graphicsDevice;

            _heightProvider = heightProvider;

            _decorNoiseProvider = decorNoiseProvider;

            _newTerrainChunks = new List <TerrainChunk>();

            TerrainChunks = new List <TerrainChunk>();
            Light         = light;
        }
Esempio n. 7
0
 // Use this for
 void Start()
 {
     Cache               = new ChunkCache(this);
     Cache.Resolution    = Resolution;
     Cache.StartSeed     = Seed;
     Cache.Steepness     = Steepness;
     Cache.MaxOverhang   = MaxOverhang;
     Cache.OverhangRatio = OverhangRatio;
     Cache.Patchamount   = PatchAmount;
     Noise               = new NoiseProvider();
     //PerlinNoise = new Perlin(Frequency, Lacunarity, Persistence, Octaves, Seed, QualityMode.Medium);
     //PerlinNoise = new Perlin();
     //float a = (float)(PerlinNoise.GetValue(1, 0, 1) / 2f) + 0.5f;
     //Debug.Log("outside");
     //Debug.Log(a);
 }
        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {
            TerrainChunk.CalculateIndices();

            _defaultRasterizerState = GraphicsDevice.RasterizerState;
            _treeRasterizerState    = new RasterizerState()
            {
                CullMode = CullMode.None
            };

            _oldMouseState    = Mouse.GetState();
            _oldKeyboardState = Keyboard.GetState();

            int            worldSeed               = 0;
            NoiseProvider  mainNoiseProvider       = new NoiseProvider(worldSeed, 4, 0.2f, 200);
            NoiseProvider  modulationNoiseProvider = new NoiseProvider(worldSeed + 1, 2, 0.2f, 600);
            DirectionLight light = new DirectionLight(Vector3.Normalize(new Vector3(0, -0.3f, 1)), 1, 0.1f, 64);
            NoiseProvider  decorNoiseProvider = new NoiseProvider(worldSeed + 2, 2, 0.2f, 200);

            _world = new World(GraphicsDevice, new TerrainHeightProvider(mainNoiseProvider, modulationNoiseProvider), light, decorNoiseProvider);

            Matrix cameraProjectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(60), GraphicsDevice.Viewport.AspectRatio, 1, World.ChunkRenderDistance);

            _camera = new Camera()
            {
                ProjectionMatrix = cameraProjectionMatrix
            };

            _spriteBatch = new SpriteBatch(GraphicsDevice);

            _waterTileScaleMatrix = Matrix.CreateScale(TerrainChunk.EdgeLength);

            _mouseLookActive = true;

            _waterRefractionMapRenderTarget = new RenderTarget2D(GraphicsDevice, WaterMapSize, WaterMapSize, false, SurfaceFormat.Color, DepthFormat.Depth24);
            _waterReflectionMapRenderTarget = new RenderTarget2D(GraphicsDevice, WaterMapSize, WaterMapSize, false, SurfaceFormat.Color, DepthFormat.Depth24);

            _terrainChunkIndexBuffer = new IndexBuffer(GraphicsDevice, typeof(ushort), TerrainChunk.Indices.Length, BufferUsage.WriteOnly);
            _terrainChunkIndexBuffer.SetData(TerrainChunk.Indices);

            base.Initialize();
        }
Esempio n. 9
0
    /// <summary>
    /// Generates the heightmap thread.
    /// </summary>
    private void    GenerateHeightmapThread()
    {
        lock (HeightmapThread)
        {
            Heightmap = NoiseProvider.GetHeightmapData(Setting.HeightmapResolution, Setting.HeightmapResolution, Position.X, Position.Z);

            if (Setting.Trees.Count > 0)
            {
                for (var zRes = 0; zRes < Setting.HeightmapResolution; zRes++)
                {
                    for (var xRes = 0; xRes < Setting.HeightmapResolution; xRes++)
                    {
                        int height = Mathf.FloorToInt(Heightmap[zRes, xRes] * 100);
                        if (height > 30 && zRes % 20 == 0 && xRes % 20 == 0)
                        {
                            var xCoordinate = (float)xRes / (Setting.HeightmapResolution - 1);
                            var zCoordinate = (float)zRes / (Setting.HeightmapResolution - 1);

                            Vector3 point = new Vector3(xCoordinate, zCoordinate, height);
                            TreePoint.Add(point);
                        }
                        else
                        {
                            if (height == 0 && zRes % 10 == 0 && xRes % 10 == 0)
                            {
                                var xCoordinate = (float)xRes / (Setting.HeightmapResolution - 1);
                                var zCoordinate = (float)zRes / (Setting.HeightmapResolution - 1);

                                Vector3 point = new Vector3(xCoordinate, zCoordinate, height);
                                ChopPoint.Add(point);
                            }
                        }
                    }
                }
            }
        }
    }
Esempio n. 10
0
        public static IUnityContainer AddAudioServices(this IUnityContainer services)
        {
            services.RegisterInstance(WaveFormat.CreateIeeeFloatWaveFormat(44100, 2));
            services.RegisterWavePlayer();
            services.RegisterWaveIn();
            var format = services.Resolve <WaveFormat>();
            var mixer  = new MixingSampleProvider(format)
            {
                ReadFully = true
            };

            services.RegisterInstance(mixer, new SingletonLifetimeManager());

            var player = services.Resolve <SamplePlayer>();

            player.Add(mixer);
            var noise = new NoiseProvider(format);

            player.Add(noise);
            services.RegisterInstance <IGlobalNoiseController>(noise);
            services.RegisterInstance <ISamplePlayer>(player, new SingletonLifetimeManager());
            services.RegisterSingleton <ToneProvider>();
            return(services);
        }
Esempio n. 11
0
        private void CalculateContents(GraphicsDevice graphicsDevice, HeightProvider heightProvider, NoiseProvider decorNoiseProvider)
        {
            float centreXWorld = GridCoordinates.X * EdgeLength;
            float centreZWorld = GridCoordinates.Y * EdgeLength;

            Vector3 min = new Vector3(float.MaxValue, float.MaxValue, float.MaxValue);
            Vector3 max = new Vector3(float.MinValue, float.MinValue, float.MinValue);

            List <TerrainVertex> resultVertices = new List <TerrainVertex>();

            for (int z = 0; z < VerticesPerEdge; z++)
            {
                for (int x = 0; x < VerticesPerEdge; x++)
                {
                    float positionXLocal = x * VertexSpacing - HalfEdgeLength;
                    float positionZLocal = z * VertexSpacing - HalfEdgeLength;

                    float positionXWorld = positionXLocal + centreXWorld;
                    float positionZWorld = positionZLocal + centreZWorld;

                    Vector3 position = new Vector3(positionXWorld, heightProvider.GetHeight(positionXWorld, positionZWorld), positionZWorld);
                    Vector3 normal   = heightProvider.GetNormalFromFiniteOffset(positionXWorld, positionZWorld);

                    Vector2 textureCoordinates       = new Vector2(positionXLocal, positionZLocal) / HalfEdgeLength;
                    Vector2 textureCoordinatesDetail = new Vector2(positionXLocal, positionZLocal) * 2 / HalfEdgeLength;

                    TerrainVertex vertex = new TerrainVertex(position, normal, textureCoordinates, textureCoordinatesDetail);
                    resultVertices.Add(vertex);

                    min = Vector3.Min(min, position);
                    max = Vector3.Max(max, position);
                }
            }

            TerrainVertex[] resultVerticesData = resultVertices.ToArray();

            VertexBuffer = new VertexBuffer(graphicsDevice, TerrainVertex.VertexDeclaration, resultVerticesData.Length, BufferUsage.WriteOnly);
            VertexBuffer.SetData(resultVerticesData);

            // Populate grid triangles
            _gridTriangles.Clear();

            for (int z = 0; z < VerticesPerEdge - 1; z++)
            {
                for (int x = 0; x < VerticesPerEdge - 1; x++)
                {
                    int baseIndexPosition = (x + z * (VerticesPerEdge - 1)) * 6;
                    _gridTriangles.Add(new GridTriangle(resultVerticesData[Indices[baseIndexPosition]].Position, resultVerticesData[Indices[baseIndexPosition + 1]].Position, resultVerticesData[Indices[baseIndexPosition + 2]].Position));
                    _gridTriangles.Add(new GridTriangle(resultVerticesData[Indices[baseIndexPosition + 3]].Position, resultVerticesData[Indices[baseIndexPosition + 4]].Position, resultVerticesData[Indices[baseIndexPosition + 5]].Position));
                }
            }

            // Include water tile vertices in bounding box
            for (int z = -1; z <= 1; z += 2)
            {
                for (int x = -1; x <= 1; x += 2)
                {
                    Vector3 waterTileVertexPosition = new Vector3(centreXWorld + x * HalfEdgeLength, 0, centreZWorld + z * HalfEdgeLength);

                    min = Vector3.Min(min, waterTileVertexPosition);
                    max = Vector3.Max(max, waterTileVertexPosition);
                }
            }

            BoundingBox = new BoundingBox(min, max);

            // Fill decor according to procedural inputs
            _decorRandom = new Random(GridCoordinates.GetHashCode());

            DecorItems = new List <DecorItem>();
            for (int z = 0; z < DecorGridSize; z++)
            {
                for (int x = 0; x < DecorGridSize; x++)
                {
                    float decorCentreXWorld = centreXWorld - HalfEdgeLength + HalfDecorTileEdgeLength + x * DecorTileEdgeLength + (float)(2 * _decorRandom.NextDouble() - 1) * HalfDecorTileEdgeLength;
                    float decorCentreZWorld = centreZWorld - HalfEdgeLength + HalfDecorTileEdgeLength + z * DecorTileEdgeLength + (float)(2 * _decorRandom.NextDouble() - 1) * HalfDecorTileEdgeLength;

                    float?decorHeight = GetGridHeight(new Vector3(decorCentreXWorld, 0, decorCentreZWorld));
                    if (decorHeight.HasValue && decorHeight.Value > 5)
                    {
                        if (decorNoiseProvider.GetValue(decorCentreXWorld, decorCentreZWorld) > 0.9f)
                        {
                            DecorItems.Add(new DecorItem(new Vector3(decorCentreXWorld, decorHeight.Value, decorCentreZWorld), (float)(_decorRandom.NextDouble() * 2 * Math.PI)));
                        }
                    }
                }
            }
        }
Esempio n. 12
0
        public TerrainChunk(GraphicsDevice graphicsDevice, HeightProvider heightProvider, Vector2 gridCoordinates, NoiseProvider decorNoiseProvider)
        {
            _gridTriangles = new List <GridTriangle>();

            GridCoordinates = gridCoordinates;

            CalculateContents(graphicsDevice, heightProvider, decorNoiseProvider);
        }
Esempio n. 13
0
 public TerrainHeightProvider(NoiseProvider mainNoiseProvider, NoiseProvider modulationNoiseProvider)
 {
     MainNoiseProvider       = mainNoiseProvider;
     ModulationNoiseProvider = modulationNoiseProvider;
 }
Esempio n. 14
0
 private void OnEnable()
 {
     NoiseProvider.Init(mapNoise);
 }