float SampleCaves(float x, float y, float z, PerlinNoise perlin) { //The creates the noise used for the caves. It uses domain warping like the moiuntains //to creat long twisting caves. float w = perlin.FractalNoise3D(x, y, z, 1, 40.0f, 32.0f); //The last vaule is the cave amp and defines the maximum cave diameter. A larger value will create //larger caves (A higher frequency will also create larger caves). It is unitless, 1 != 1m return(Mathf.Abs(perlin.FractalNoise3D(x + w, y * 2.0f + w, z + w, 2, 40.0f, 2.0f))); }
void DeformMesh(Mesh mesh, PolygonCollider2D collider, int mult, int seed) { Vector3[] verts = mesh.vertices; Vector3[] normals = mesh.normals; PerlinNoise pnoise = new PerlinNoise(seed); Vector2[] colPoints = new Vector2[14]; int j = 0; int k = 0; for (int i = 0; i < verts.Length; i++) { Vector3 v = normals[i]; v.z = 0; verts[i] = verts[i] + v * (pnoise.FractalNoise3D(verts[i].x, verts[i].y, verts[i].z, (int)octaves / 2, frequency * 2, amplitude / 2 * mult)); if (verts[i].z > -0.01) { if (k == 0) { colPoints[j++ % 14] = verts[i]; } k = (k + 1) % 3; } } mesh.vertices = verts; collider.points = colPoints; mesh.RecalculateNormals(); mesh.RecalculateBounds(); }
Vector3 getPerlin3D(Vector3 pos) { float f1 = perlinX.FractalNoise3D(pos.x, pos.y, pos.z, 1, 1 / frequency, amplitude); //Mathf.PerlinNoise(pos.x,pos.y) -.5f; float f2 = perlinY.FractalNoise3D(pos.x, pos.y, pos.z, 1, 1 / frequency, amplitude); //Mathf.PerlinNoise(pos.x,pos.y) -.5f; float f3 = perlinZ.FractalNoise3D(pos.x, pos.y, pos.z, 1, 1 / frequency, amplitude); //Mathf.PerlinNoise(pos.x,pos.y) -.5f; return(new Vector3(f1, f2, f3)); }
// Update is called once per frame void Update() { // Update the perlin index to lookup the perlin value m_PerlinIndex += m_PerlinIncrement * Time.deltaTime; Vector3 pos = transform.position; Vector3 perl; //m_PerlNoise perl.x = m_PerlNoise.FractalNoise3D(m_PerlinIndex, m_PerlinIndex + 5, m_PerlinIndex + 10, m_OctNum, 1, 1); perl.y = m_PerlNoise.FractalNoise3D(m_PerlinIndex + 5, m_PerlinIndex + 10, m_PerlinIndex, m_OctNum, 1, 1); perl.z = m_PerlNoise.FractalNoise3D(m_PerlinIndex + 10, m_PerlinIndex + 5, m_PerlinIndex, m_OctNum, 1, 1); //perl.x = m_PerlNoise.FractalNoise3D( m_PerlinIndex, m_PerlinIndex, m_PerlinIndex, m_OctNum, m_Freq, m_Amp ); //perl.y = 0; //perl.z = 0; //print ( perl ); //print ( perl ); // Set the transform offset based on the perlin value perl.Scale(m_PerlinPositioning); m_TransformManager.m_OffsetPosition += perl; }
void AddNoiseToVoxels() { if (noiseAmount < 0f) return; PerlinNoise noise = new PerlinNoise (Random.seed); int v = voxelCountPerChunk * chunkStride; for (int i = 0; i < v; i++) { for (int j = 0; j < v; j++) { for (int k = 0; k < v; k++) { if (voxels[i, j, k] == 0f) continue; voxels[i, j, k] += noise.FractalNoise3D(i, j, k, octNum, noiseFreq, 1f) * noiseAmount; voxels[i, j, k] = Mathf.Clamp01(voxels[i, j, k]); } } } }
void GenerateTerrain(float time) { PerlinNoise noiseGen = new PerlinNoise(0); for (int i = 0; i < _width; i++) { for (int j = 0; j < _height; j++) { float x = (float)i / (float)_width; float y = (float)j / (float)_height; float noise = noiseGen.FractalNoise3D(x, y, time, 2, _mapScale + 0.01f, 1.75f); _tileMap[i, j] = NoiseToTile(noise); } } }
public void Build() { float[,,] voxels = new float[size + 1, size + 1, size + 1]; int height; float hFactor; for (int z = 0; z < size + 1; z++) { for (int y = 0; y < size + 1; y++) { height = yi + y; hFactor = (float)(maxHeight - 2 * height) / (float)maxHeight; for (int x = 0; x < size + 1; x++) { voxels[x, y, z] = -hFactor + m_perlin.FractalNoise3D((float)(xi + x), (float)(yi + y), (float)(zi + z), 3, freq, 1.0f); } } } mesh = MarchingCubes.CreateMesh(voxels); //UV MAPPING Vector3[] vertices = mesh.vertices; Vector2[] uvs = new Vector2[mesh.vertices.Length]; for (int i = 0; i < uvs.Length; i++) { uvs[i] = new Vector2(vertices[i].x, vertices[i].z); } mesh.uv = uvs; //NORMALS mesh.RecalculateNormals(); //TANGENTS TangentSolver.Solve(mesh); mesh.Optimize(); AssetDatabase.CreateAsset(mesh, "Assets/Resources/meshes/mesh" + xi + yi + zi + ".asset"); //GAMEOBJECT SETUP m_mesh.GetComponent <MeshFilter>().mesh = mesh; m_mesh.transform.localPosition = gPos; m_mesh.AddComponent <MeshCollider>(); }
public void GenerateEmptySurface(float[] field, Vector3 pos) { int rS = size + 1; for (int x = 0; x < rS; x++) { for (int y = 0; y < rS; y++) { for (int z = 0; z < rS; z++) { Vector3 p = pos + new Vector3(x, y, z); float d = (c - p).magnitude; float t = 1f - 1f / (1f + d); float n = noise.FractalNoise3D(pos.x + x, pos.y + y, pos.z + z, oct, frq, amp); globalHeightFunction = AnimationCurve.Linear(0, startHeight + n, 1, 0); field[x + y * rS + z * rS * rS] = globalHeightFunction.Evaluate(t); } } } }
void GenerateGenericTexture(Texture2D tex, Texture2D normal, Color asteroidColor, int seed) { float _x, _y, _z, noise; int width = tex.width; int height = tex.height; Color[] pix = new Color[width * height]; Color[] pixn = new Color[width * height]; noise = 0; Gradient[] gradients = MakeGenericGradients(asteroidColor); PerlinNoise pnoise = new PerlinNoise(seed); for (float y = 0; y < width; y++) { for (float x = 0; x < height; x++) { _x = (x / width - 0.5f) * 2; _y = (y / height - 0.5f) * 2; _z = Mathf.Sqrt(1 - Mathf.Pow(_x, 2) - Mathf.Pow(_y, 2)); noise = 0; if (_z >= 0) { noise = (pnoise.FractalNoise3D(_x, _y, _z, (int)octaves, frequency, amplitude)); } pix[(int)y * tex.width + (int)x] = gradients[0].Evaluate(1 - Mathf.Abs(noise)) * (1 - Mathf.Abs(noise)); pixn[(int)y * tex.width + (int)x] = gradients[1].Evaluate(1 - Mathf.Abs(noise)) * (1 - Mathf.Abs(noise) * 4); } } tex.SetPixels(pix); normal.SetPixels(HeightToNormal(pixn)); }
float SampleCaves(float x, float y, float z, PerlinNoise perlin) { //The creates the noise used for the caves. It uses domain warping like the moiuntains //to creat long twisting caves. float w = perlin.FractalNoise3D(x, y, z, 1, 40.0f, 32.0f); //The last vaule is the cave amp and defines the maximum cave diameter. A larger value will create //larger caves (A higher frequency will also create larger caves). It is unitless, 1 != 1m return Mathf.Abs(perlin.FractalNoise3D(x+w, y*2.0f+w, z+w, 2, 40.0f, 2.0f)); }
private void marchingCubesV2(Vector3 pos) { int vert, idx; cubeVerts = new float[8]; int flagIndex = 0; float offset = 0.0f; edgeVertex = new Vector3[12]; //This is getting the noise value at each corner //Possible bottleneck is the noise generation, just got that code off the internet and went with it. for (int i = 0; i < 8; i++) { //The argument for noise constructor is just a seed, feel free to change....Should make that an argument for marching constructor //PerlinNoise noise = new PerlinNoise(100); cubeVerts[i] = noise.FractalNoise3D(pos.x + vertexOffset[i, 0] * scale, pos.y + vertexOffset[i, 1] * scale, pos.z + vertexOffset[i, 2] * scale, 3, 40, 1); } //the target value is the value that determines if noise is solid or air. //In the case we say negative is air and positive is solid //If solid we do a bitwise shift on flagIndex for (int i = 0; i < 8; i++) { if (cubeVerts[i] >= target) { flagIndex |= 1 << i; } } //Nifty ass lookup table powers at work //Finds edges intersecting the surface. int edgeFlags = cubeEdgeFlags[flagIndex]; //This cube is either entirely solid (edgeFlags == 255) //or entirely air so we don't do shit (edgeFlags == 0) //or it is outside the chunk we want generated (pos.x >= max || pos.y >= max || pos.z >= max) //So we don't do shit here if (edgeFlags == 0 || edgeFlags == 255 || pos.x > xmax || pos.y > ymax || pos.z > zmax || pos.x < xmin || pos.y < ymin || pos.z < zmin) { return; } /*else{ * GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube); * cube.transform.position = new Vector3(pos.x + scale/2, pos.y + scale/2, pos.z + scale/2); * cube.transform.localScale = new Vector3(scale,scale,scale); * }*/ //Find the point of intersection of the surface with each edge //Then find the normal to the surface at those points for (int i = 0; i < 12; i++) { if ((edgeFlags & (1 << i)) != 0) { offset = getOffset(cubeVerts[edgeConnection[i, 0]], cubeVerts[edgeConnection[i, 1]]); edgeVertex[i].x = pos.x + (vertexOffset[edgeConnection[i, 0], 0] + offset * edgeDirection[i, 0]); edgeVertex[i].y = pos.y + (vertexOffset[edgeConnection[i, 0], 1] + offset * edgeDirection[i, 1]); edgeVertex[i].z = pos.z + (vertexOffset[edgeConnection[i, 0], 2] + offset * edgeDirection[i, 2]); } } //Draw the triangles..Up to 5 per cube for (int i = 0; i < 5; i++) { if (triangleConnectionTable[flagIndex, 3 * i] < 0) { break; } idx = vertList.Count; for (int j = 0; j < 3; j++) { //Here we are making the lists to create a mesh. Could also try straight up drawing the triangles at some point vert = triangleConnectionTable[flagIndex, 3 * i + j]; indexList.Add(idx + windingOrder[j]); vertList.Add(edgeVertex[vert]); } } //March to the neighbors. Will be the six cubes that coincide with faces of this cube //Should actually attempt to put every new vector in the used hashset here before making the recusrive call at all... for (int i = 0; i < 6; i++) { if (used.Add(pos + neighbors[i] * scale)) { marchingCubesV2(pos + neighbors[i] * scale); } } }
void DeformMesh (Mesh mesh, PolygonCollider2D collider, int mult, int seed) { Vector3[] verts = mesh.vertices; Vector3[] normals = mesh.normals; PerlinNoise pnoise = new PerlinNoise(seed); Vector2[] colPoints = new Vector2[14]; int j = 0; int k = 0; for(int i = 0; i < verts.Length;i++) { Vector3 v = normals[i]; v.z = 0; verts[i] = verts[i]+v*(pnoise.FractalNoise3D(verts[i].x, verts[i].y, verts[i].z, (int)octaves/2, frequency*2, amplitude/2*mult)); if (verts[i].z > -0.01) { if (k == 0) colPoints[j++%14] = verts[i]; k = (k+1)%3; } } mesh.vertices = verts; collider.points = colPoints; mesh.RecalculateNormals(); mesh.RecalculateBounds(); }
void GenerateGenericTexture(Texture2D tex, Texture2D normal, Color asteroidColor, int seed) { float _x, _y, _z, noise; int width = tex.width; int height = tex.height; Color[] pix = new Color[width*height]; Color[] pixn = new Color[width*height]; noise = 0; Gradient[] gradients = MakeGenericGradients(asteroidColor); PerlinNoise pnoise = new PerlinNoise(seed); for(float y = 0; y < width;y++) { for(float x = 0; x < height;x++) { _x = (x/width-0.5f)*2; _y = (y/height-0.5f)*2; _z = Mathf.Sqrt (1 - Mathf.Pow (_x, 2) - Mathf.Pow (_y,2)); noise = 0; if (_z>=0) noise = (pnoise.FractalNoise3D(_x, _y, _z, (int)octaves, frequency, amplitude)); pix[(int)y*tex.width+(int)x] = gradients[0].Evaluate (1-Mathf.Abs (noise))*(1-Mathf.Abs (noise)); pixn[(int)y*tex.width+(int)x] = gradients[1].Evaluate (1-Mathf.Abs (noise))*(1-Mathf.Abs (noise)*4); } } tex.SetPixels(pix); normal.SetPixels (HeightToNormal (pixn)); }
void GenerateTerrain(float time) { PerlinNoise noiseGen = new PerlinNoise (0); for (int i = 0; i < _width; i++) { for (int j = 0; j < _height; j++) { float x = (float)i / (float)_width; float y = (float)j / (float)_height; float noise = noiseGen.FractalNoise3D(x, y, time, 2, _mapScale + 0.01f, 1.75f); _tileMap[i,j] = NoiseToTile (noise); } } }