private Node CreateNode(Box3D bounds, int level, EntityManager entityManager) { var mesh = MeshCreator.CreateXZGrid(10, 10); var staticMesh = new StaticMesh { Color = new Vector4(0f, 0f, 1f, 1f), ModelMatrix = Matrix4.Identity, }; var size = bounds.Max - bounds.Min; var mesh3V3N = mesh.Transformed(Matrix4.CreateScale((float)size.X, 1, (float)size.Z) * Matrix4.CreateTranslation((Vector3)bounds.Center)); var improvedPerlinNoise = new ImprovedPerlinNoise(4711); for (int i = 0; i < mesh3V3N.Vertices.Length; i++) { var vertex = mesh3V3N.Vertices[i]; var height = improvedPerlinNoise.Noise(vertex.Position.X, vertex.Position.Z) * 0.2; mesh3V3N.Vertices[i] = new Vertex3V3N { Normal = new Vector3(0, 1, 0), Position = new Vector3(vertex.Position.X, (float)height, vertex.Position.Z) }; } staticMesh.Update(mesh3V3N); var entity = new Entity(Guid.NewGuid().ToString()); entityManager.Add(entity); entityManager.AddComponentToEntity(entity, staticMesh); if (level == 0) { return(new Node(bounds, new Node[] { }, entity, 1)); } var min = bounds.Min; var max = bounds.Max; var center = bounds.Center; return(new Node(bounds, new[] { CreateNode(new Box3D(bounds.Min, center), level - 1, entityManager), CreateNode(new Box3D(new Vector3d(center.X, 0, min.Z), new Vector3d(max.X, 0, center.Z)), level - 1, entityManager), CreateNode(new Box3D(new Vector3d(min.X, 0, center.Z), new Vector3d(center.X, 0, max.Z)), level - 1, entityManager), CreateNode(new Box3D(center, max), level - 1, entityManager) }, entity, Math.Pow(2, level))); }
//public void PerlinNoise2D() //{ // int terrainWidth = TerrainData.heightmapResolution; // int terrainHeight = TerrainData.heightmapResolution; // float[,] heightMap = TerrainData.GetHeights(0, 0, terrainWidth, terrainHeight); // for (int x = 0; x < terrainHeight; x++) // { // for (int y = 0; y < terrainWidth; y++) // { // float inputX = (x + PerlinOffsetX) * PerlinScaleX; // float inputY = (y + PerlinOffsetY) * PerlinScaleY; // heightMap[x, y] = Utils.FBM(inputX, inputY, PerlinOctaves, PerlinPersistance) * PerlinHeightScale; // } // } // SaveToFile(heightMap, "./PerlinNoise.csv"); // TerrainData.SetHeights(0, 0, heightMap); //} public void ImprovetPerlinNoise() { int terrainWidth = TerrainData.heightmapResolution; int terrainHeight = TerrainData.heightmapResolution; var perlin = new ImprovedPerlinNoise(); float[,] heightMap = TerrainData.GetHeights(0, 0, terrainWidth, terrainHeight); for (int x = 0; x < terrainHeight; x++) { for (int y = 0; y < terrainWidth; y++) { double inputX = (x + PerlinOffsetX) * PerlinScaleX; double inputY = (y + PerlinOffsetY) * PerlinScaleY; heightMap[x, y] += (float)perlin.Noise(inputX, inputY, 0) * PerlinHeightScale; } } //SaveToFile(heightMap, "./OwnPerlinNoise.csv"); TerrainData.SetHeights(0, 0, heightMap); }