protected override bool CalculateHeight(out float height, int x, int z) { float noiseValue = fractalNoise.Sample2D(offset.x + x / (float)width, offset.y + z / (float)length); height = noiseValue; return(true); }
protected override bool CalculateVoxel(Voxel voxel, int x, int y, int z) { float noiseValue = fractal.Sample2D(x / (float)width, z / (float)length); float normalizedNoise = 1 - (noiseValue + 1) / 2; voxel.Data = y / (float)height > normalizedNoise ? 0.0f : 1.0f; return(true); }
void FillHeights(float[,] htmap, int tileX, int tileZ) { float ratio = (float)m_terrainSize / (float)m_heightMapSize; for (int x = 0; x < m_heightMapSize; x++) { for (int z = 0; z < m_heightMapSize; z++) { float worldPosX = (x + tileX * (m_heightMapSize - 1)) * ratio; float worldPosZ = (z + tileZ * (m_heightMapSize - 1)) * ratio; float distance = new Vector3(worldPosX - m_terrainSize * m_tilesX / 2, 0, worldPosZ - m_terrainSize * m_tilesZ / 2).magnitude; htmap[z, x] = noiseBig.Amplitude + noiseSmall.Sample2D(worldPosX, worldPosZ); htmap[z, x] += noiseSmall.Amplitude + noiseSmall.Sample2D(worldPosX, worldPosZ); htmap[z, x] *= Mathf.Clamp01((radial - distance) / radialFalloff); } } }
public float SampleValue(float x, float z) { if (!IsEnabled) { return(0); } //return RaiseHeight + (fracNoise.Sample2D(x, z) + fracNoise.Amplitude * fracNoise.Noises.Max(n => n.Amplitude)); return(RaiseHeight + fracNoise.Sample2D(x, z)); }
void FillHeights(float[,] htmap, int tileX, int tileZ) { float ratio = (float)m_terrainSize / (float)m_heightMapSize; for (int x = 0; x < m_heightMapSize; x++) { for (int z = 0; z < m_heightMapSize; z++) { float worldPosX = (x + tileX * (m_heightMapSize - 1)) * ratio; float worldPosZ = (z + tileZ * (m_heightMapSize - 1)) * ratio; htmap[z, x] = Mathf.Max(0.0f, m_mountainNoise.Sample2D(worldPosX, worldPosZ)); } } }
void FillHeights(float[,] htmap, int tileX, int tileZ) { float ratio = (float)m_terrainSize / (float)m_heightMapSize; for (int x = 0; x < m_heightMapSize; x++) { for (int z = 0; z < m_heightMapSize; z++) { float worldPosX = (x + tileX * (m_heightMapSize - 1)) * ratio; float worldPosZ = (z + tileZ * (m_heightMapSize - 1)) * ratio; htmap[z, x] = m_groundNoise.Amplitude + m_groundNoise.Sample2D(worldPosX, worldPosZ); } } }
void FillHeights(float[,] htmap, int tileX, int tileZ) { float ratio = m_terrainSize * 1.0f / m_heightMapSize; float relativeHeight = m_terrainAltitude / m_terrainHeight; for (int x = 0; x < m_heightMapSize; x++) { for (int z = 0; z < m_heightMapSize; z++) { float worldPosX = (x + tileX * (m_heightMapSize - 1)) * ratio; float worldPosZ = (z + tileZ * (m_heightMapSize - 1)) * ratio; float value = m_groundNoise.Amplitude + m_groundNoise.Sample2D(worldPosX, worldPosZ); htmap[z, x] = Mathf.Clamp01(relativeHeight + value); } } }
void Start() { foreach (GameObject o in meshes) { o.transform.parent = null; Destroy(o); } meshes = new List <GameObject>(); INoise perlin = new PerlinNoise(seed, 2.0f); FractalNoise fractal = new FractalNoise(perlin, 3, 1.0f); //Set the mode used to create the mesh. //Cubes is faster and creates less verts, tetrahedrons is slower and creates more verts but better represents the mesh surface. Marching marching = null; if (mode == MARCHING_MODE.TETRAHEDRON) { marching = new MarchingTertrahedron(); } else { marching = new MarchingCubes(); } //Surface is the value that represents the surface of mesh //For example the perlin noise has a range of -1 to 1 so the mid point is where we want the surface to cut through. //The target value does not have to be the mid point it can be any value with in the range. marching.Surface = 0.0f; //The size of voxel array. int width = 32 * resolution; int height = 32 * resolution; int length = 32 * resolution; float[] voxels = new float[width * height * length]; //Fill voxels with values. Im using perlin noise but any method to create voxels will work. for (int x = 0; x < width; x++) { for (int z = 0; z < length; z++) { float fx = x / (width - 1.0f); float fz = z / (length - 1.0f); float value = fractal.Sample2D(fx, fz); for (int y = 0; y < height; y++) { int idx = x + y * width + z * width * height; voxels[idx] = y < (value + 1.0f) / 2.0f * height ? 1 : -1; } } } List <Vector3> verts = new List <Vector3>(); List <int> indices = new List <int>(); List <Vector3> normals = new List <Vector3>(); //The mesh produced is not optimal. There is one vert for each index. //Would need to weld vertices for better quality mesh. marching.Generate(voxels, width, height, length, verts, indices, normals); //A mesh in unity can only be made up of 65000 verts. //Need to split the verts between multiple meshes. int maxVertsPerMesh = 30000; //must be divisible by 3, ie 3 verts == 1 triangle int numMeshes = verts.Count / maxVertsPerMesh + 1; for (int i = 0; i < numMeshes; i++) { List <Vector3> splitVerts = new List <Vector3>(); List <int> splitIndices = new List <int>(); List <Vector3> splitNormals = new List <Vector3>(); for (int j = 0; j < maxVertsPerMesh; j++) { int idx = i * maxVertsPerMesh + j; if (idx < verts.Count) { splitVerts.Add(verts[idx]); splitNormals.Add(normals[idx]); splitIndices.Add(j); } } if (splitVerts.Count == 0) { continue; } Mesh mesh = new Mesh(); mesh.SetVertices(splitVerts); mesh.SetTriangles(splitIndices, 0); mesh.SetNormals(splitNormals); mesh.RecalculateBounds(); GameObject go = new GameObject("Mesh"); go.transform.parent = transform; go.AddComponent <MeshFilter>(); go.AddComponent <MeshRenderer>(); go.GetComponent <Renderer>().material = m_material; go.GetComponent <MeshFilter>().mesh = mesh; go.transform.localPosition = new Vector3(-width / resolution / 2, -height / resolution / 2, -length / resolution / 2); go.transform.localScale = new Vector3(1.0f / resolution, 1.0f / resolution, 1.0f / resolution); meshes.Add(go); } }