public GetValue ( float x, float y, float z ) : float | ||
x | float | The input coordinate on the x-axis. |
y | float | The input coordinate on the y-axis. |
z | float | The input coordinate on the z-axis. |
Результат | float |
// Use this for initialization void Start () { Debug.Log("Tutorial conversion for http://libnoise.sourceforge.net/tutorials/tutorial2.html"); var perlin = new Perlin(); Debug.Log(string.Format("First value: {0}", perlin.GetValue(_firstValue))); Debug.Log(string.Format("First value, displaced: {0}", perlin.GetValue(_firstValue + _displacement))); Debug.Log(string.Format("Second value: {0}", perlin.GetValue(_secondValue))); }
//通过方块的世界坐标获取它的方块类型 public static byte GetTerrainBlock(Vector3i worldPosition) { //LibNoise噪音对象 Perlin noise = new LibNoise.Generator.Perlin(1f, 1f, 1f, 8, SeedManager.randomSeed, QualityMode.High); //为随机数指定种子,这样每次随机的都是同样的值 Random.InitState(SeedManager.randomSeed); //因为柏林噪音在(0,0)点是上下左右对称的,所以我们设置一个很远很远的地方作为新的(0,0)点 Vector3 offset = new Vector3(Random.value * 100000, Random.value * 100000, Random.value * 100000); float noiseX = Mathf.Abs((worldPosition.x + offset.x) / 20); float noiseY = Mathf.Abs((worldPosition.y + offset.y) / 20); float noiseZ = Mathf.Abs((worldPosition.z + offset.z) / 20); double noiseValue = noise.GetValue(noiseX, noiseY, noiseZ); noiseValue += (20 - worldPosition.y) / 15f; noiseValue /= worldPosition.y / 5f; if (noiseValue > 0.5f) { return(1); } return(0); }
//通过世界坐标获取他方块的类型 public static byte GetTerrainBlock(Vector3i worldPosition) { //libNooise噪音对象 Perlin noise = new LibNoise.Generator.Perlin(1f, 1f, 1f, 8, GameManager.randomSeed, QualityMode.High); //为随机数指定种子,每次随机都为同样值 Random.InitState(GameManager.randomSeed); //因为柏林噪音在(0,0)点是上下左右对称的,所以我们设置一个很远很远的地方作为新的(0,0)点 Vector3 offset = new Vector3(Random.value * 100000, Random.value * 100000, Random.value * 100000); float noiseX = Mathf.Abs((worldPosition.x + offset.x) / 20); //改变地形的平缓程度,值越大越平缓 float noiseY = Mathf.Abs((worldPosition.y + offset.y) / 20); //不要小于20 float noiseZ = Mathf.Abs((worldPosition.z + offset.z) / 20); double noiseValue = noise.GetValue(noiseX, noiseY, noiseZ); //没有此两行就为洞窟地貌 noiseValue += (20 - worldPosition.y) / 15f;//30改变修正山高度,也可以为将整体噪音图像向上平移 noiseValue /= worldPosition.y / 5f; if (noiseValue > 0.5f) { return(1); } return(0); }
private float noise(Vector3 point, int octaves, float lucanarity = 2.0f, float gain = 0.5f, float warp = 0.25f) { float sum = 0.0f, freq = 1.0f, amp = 1.0f; for (int o = 0; o < octaves; o++) { sum += amp * (float)planes.GetValue(point); freq *= lucanarity; amp *= gain; } sum *= (float)mountains.GetValue(point * freq) * warp * octaves; return(sum); }
//Copied public Vector2 PerlinNoise3D(Transform tr, float radius, float scaler, int octaves = 2, float lucanarity = 2f, float gain = 0.1f, float warp = 0.1f, float testing = 10) { LibNoise.Generator.Perlin planes = GameObject.FindGameObjectWithTag("Setting").GetComponent <TestongThings>().planes; LibNoise.Generator.RidgedMultifractal mountains = GameObject.FindGameObjectWithTag("Setting").GetComponent <TestongThings>().mountains; float highest = 0; float lowest = 0; for (var i = 0; i < vertices.Length; i++) { Vector3 point = tr.TransformPoint(vertices[i]); //Vector3 point = vertices[i]; float sum = 0.0f, freq = 1.0f, amp = 1.0f; for (int j = 0; j < octaves; j++) { sum += amp * (float)planes.GetValue(point); freq *= lucanarity; amp *= gain; } sum *= (float)mountains.GetValue(point * freq) * octaves * testing; sum = Mathf.Max(-1, sum / 2); vertices[i] = vertices[i].normalized * (radius * scaler + sum); if (sum > highest) { highest = sum; } if (sum < lowest && sum > 0) { lowest = sum; } } return(new Vector2(lowest, highest)); }
public void setColor(int index, Color32 color) { Polygon poly = GetPolygon(index); if (poly != null) { #region Set Selection Color Mesh m_Mesh = m_PlanetMesh.GetComponent <MeshFilter>().mesh; Color32[] colors = m_Mesh.colors32; if (s_Polygon != null) { int j = 0; foreach (int ind in s_Polygon.indices) { colors[ind + 0] = s_Color32[j++]; colors[ind + 1] = s_Color32[j++]; colors[ind + 2] = s_Color32[j++]; } } s_Color32 = new List <Color32>(); foreach (int ind in poly.indices) { s_Color32.Add(colors[ind + 0]); s_Color32.Add(colors[ind + 1]); s_Color32.Add(colors[ind + 2]); colors[ind + 0] = color; colors[ind + 1] = color; colors[ind + 2] = color; } s_Polygon = poly; m_PlanetMesh.GetComponent <MeshFilter>().mesh.colors32 = colors; #endregion #region Set Display Area (If Exists) if (s_Display != null) { Mesh mesh = m_PlanetMesh.GetComponent <MeshFilter>().mesh; Texture2D texture = new Texture2D(s_MapResolution, s_MapResolution); Vector3 f = mesh.vertices[poly.t_Vertices[3]] - mesh.vertices[poly.t_Vertices[2]]; Vector3 g = mesh.vertices[poly.t_Vertices[3]] - mesh.vertices[poly.t_Vertices[0]]; Vector3 P = mesh.vertices[poly.t_Vertices[3]]; Vector3 Q = mesh.vertices[poly.t_Vertices[0]]; Vector3 R = mesh.vertices[poly.t_Vertices[2]]; Vector3 p = new Vector3(0, 0, 0); float stepX = FindStep(mesh.vertices[poly.t_Vertices[3]].x, mesh.vertices[poly.t_Vertices[2]].x); float stepY = FindStep(mesh.vertices[poly.t_Vertices[3]].y, mesh.vertices[poly.t_Vertices[0]].y); float denom = (f.x * g.y) - (f.y * g.x); float alpha = ((p.x * g.y) - (p.y * g.x)) / denom; float beta = ((p.y * g.x) - (p.x * g.y)) / denom; Vector3 stepVector = new Vector3(stepX, stepY, 0); float stepAlpha = ((stepVector.x * g.y) - (stepVector.y * g.x)) / denom; float stepBeta = ((stepVector.y * g.x) - (stepVector.x * g.y)) / denom; float goalAlpha = ((R.x * g.y) - (R.y * g.x)) / denom; float goalBeta = ((Q.x * g.y) - (Q.y * g.x)) / denom; float currAlpha = alpha; for (int y = 0; y < texture.width; y++) { for (int x = 0; x < texture.height; x++) { Vector3 P_prime = (currAlpha * f) + (beta * g) + P; double value = Mathf.Abs((float)Perlin.GetValue(P_prime.x, P_prime.y, P_prime.z)); Color32 polyColor = p_TerrainType[p_TerrainType.Count - 1]; for (int range = 0; range < p_TerrainHeight.Count; range++) { if (value <= p_TerrainHeight[range]) { polyColor = p_TerrainType[range]; break; } } texture.SetPixel(x, y, polyColor); currAlpha += stepAlpha; } beta += stepBeta; currAlpha = alpha; } s_Display.GetComponent <Renderer>().material.mainTexture = texture; texture.Apply(); } #endregion } }
/// <summary> /// Returns the output value for the given input coordinates. /// </summary> /// <param name="x">The input coordinate on the x-axis.</param> /// <param name="y">The input coordinate on the y-axis.</param> /// <param name="z">The input coordinate on the z-axis.</param> /// <returns>The resulting output value.</returns> public override double GetValue(double x, double y, double z) { float signal; float value; int curOctave; x *= Frequency; y *= Frequency; z *= Frequency; // Initialize value, fBM starts with 0 value = 0; ModuleBase tmpperl = new Perlin(Frequency,Lacunarity,0.5,OctaveCount,Seed,QualityMode.Medium); // Inner loop of spectral construction, where the fractal is built for(curOctave = 0; curOctave < OctaveCount; curOctave++) { // Get the coherent-noise value. signal = (float)tmpperl.GetValue(x, y, z) * (float)m_weights[curOctave]; // Add the signal to the output value. value += signal; // Go to the next octave. x *= Lacunarity; y *= Lacunarity; z *= Lacunarity; }//end for //take care of remainder in _octaveCount float remainder = OctaveCount - (int)OctaveCount; if(remainder > 0.0f) { value += remainder * (float)tmpperl.GetValue(x, y, z) * (float)m_weights[curOctave]; }//end if return value; }
private static float getAxis(LibNoise.Generator.Perlin perlin) { return((float)perlin.GetValue(0, 0, 1) * 10); }
private static GameObject CreateGameObject(Primitive primitive, LibNoise.Generator.Perlin perlin) { #region Create Planet Object GameObject planet = new GameObject("Planet"); planet.AddComponent <Planet>(); planet.GetComponent <Planet>().m_Polygons = primitive.m_Polygons; planet.GetComponent <Planet>().Perlin = perlin; MeshCollider meshCollider = planet.AddComponent <MeshCollider>(); MeshFilter meshFilter = planet.AddComponent <MeshFilter>(); MeshRenderer meshRenderer = planet.AddComponent <MeshRenderer>(); meshRenderer.material = new Material(Shader.Find("Particles/Standard Surface")); #endregion #region Generate Terrain List <float> p_TerrainHeight = new List <float>(); List <Color32> p_TerrainType = new List <Color32>(); GenerateTerrain(p_TerrainHeight, p_TerrainType); planet.GetComponent <Planet>().p_TerrainHeight = p_TerrainHeight; planet.GetComponent <Planet>().p_TerrainType = p_TerrainType; #endregion #region Local Variables int vertexCount = (primitive.m_Polygons[0].type == 0) ? primitive.m_Polygons.Count * 6 : primitive.m_Polygons.Count * 3; int[] indices = new int[vertexCount]; Vector3[] vertices = new Vector3[vertexCount]; Color32[] colors = new Color32[vertexCount]; #endregion #region Create Mesh Data for (int i = 0; i < primitive.m_Polygons.Count; i++) { #region Get/Set Polygon Data var poly = primitive.m_Polygons[i]; int index = i; if (poly.type == 0) { index = index * 6; primitive.m_Polygons[i].indices.Add(index); primitive.m_Polygons[i].indices.Add(index + 3); primitive.m_Polygons[i].t_Vertices.Add(index + 0); primitive.m_Polygons[i].t_Vertices.Add(index + 2); primitive.m_Polygons[i].t_Vertices.Add(index + 1); primitive.m_Polygons[i].t_Vertices.Add(index + 4); } else { index = index * 3; primitive.m_Polygons[i].indices.Add(index); primitive.m_Polygons[i].t_Vertices.Add(index + 0); primitive.m_Polygons[i].t_Vertices.Add(index + 2); primitive.m_Polygons[i].t_Vertices.Add(index + 1); } #endregion #region Set Mesh Indices indices[index + 0] = index + 0; indices[index + 1] = index + 1; indices[index + 2] = index + 2; if (poly.type == 0) { indices[index + 3] = index + 3; indices[index + 4] = index + 4; indices[index + 5] = index + 5; } #endregion #region Set Mesh Vertices vertices[index + 0] = primitive.m_Vertices[poly.m_Vertices[0]]; vertices[index + 1] = primitive.m_Vertices[poly.m_Vertices[2]]; vertices[index + 2] = primitive.m_Vertices[poly.m_Vertices[1]]; if (poly.type == 0) { vertices[index + 3] = primitive.m_Vertices[poly.m_Vertices[0]]; vertices[index + 4] = primitive.m_Vertices[poly.m_Vertices[3]]; vertices[index + 5] = primitive.m_Vertices[poly.m_Vertices[2]]; } #endregion #region Get Polgon Color (Legacy) /* * double value0 = Mathf.Abs((float)perlin.GetValue(vertices[index].x, vertices[index].y, vertices[index].z)); * double value1 = Mathf.Abs((float)perlin.GetValue(vertices[index + 1].x, vertices[index + 1].y, vertices[index + 1].z)); * double value2 = Mathf.Abs((float)perlin.GetValue(vertices[index + 2].x, vertices[index + 2].y, vertices[index + 2].z)); * double value3 = Mathf.Abs((float)perlin.GetValue(vertices[index + 3].x, vertices[index + 3].y, vertices[index + 3].z)); * double value = (value1 + value2 + value3 + value0) / 4; * Color32 polyColor = p_TerrainType[p_TerrainType.Count - 1]; * for (int range = 0; range < p_TerrainHeight.Count; range++) * { * if (value <= p_TerrainHeight[range]) * { * polyColor = p_TerrainType[range]; * break; * } * } */ #endregion #region Set Colors (Legacy) /* * colors[index] = polyColor; * colors[index + 1] = polyColor; * colors[index + 2] = polyColor; * * if (poly.type == 0) * { * colors[index + 3] = polyColor; * colors[index + 4] = polyColor; * colors[index + 5] = polyColor; * } */ #endregion #region Set Color int j_max = (primitive.m_Polygons[i].type == 0) ? 6 : 4; for (int j = 0; j < j_max; j++) { double value = Mathf.Abs((float)perlin.GetValue(vertices[index + j].x, vertices[index + j].y, vertices[index + j].z)); colors[index + j] = p_TerrainType[p_TerrainType.Count - 1]; for (int range = 0; range < p_TerrainHeight.Count; range++) { if (value <= p_TerrainHeight[range]) { colors[index + j] = p_TerrainType[range]; break; } } } #endregion } #endregion #region Create Mesh Mesh mesh = new Mesh(); mesh.vertices = vertices; mesh.colors32 = colors; mesh.SetTriangles(indices, 0); meshFilter.mesh = mesh; meshFilter.mesh.RecalculateNormals(); meshCollider.sharedMesh = mesh; #endregion return(planet); }
/// <summary> /// Returns the output value for the given input coordinates. /// </summary> /// <param name="x">The input coordinate on the x-axis.</param> /// <param name="y">The input coordinate on the y-axis.</param> /// <param name="z">The input coordinate on the z-axis.</param> /// <returns>The resulting output value.</returns> public override double GetValue(double x, double y, double z) { float signal; float value; int curOctave; ModuleBase tmpperl = new Perlin(Frequency,Lacunarity,0.5,OctaveCount,Seed,QualityMode.Medium); x *= this.m_frequency; y *= this.m_frequency; z *= this.m_frequency; // Initialize value : first unscaled octave of function; later octaves are scaled value = (float)m_offset + (float)tmpperl.GetValue(x, y, z); x *= this.m_lacunarity; y *= this.m_lacunarity; z *= this.m_lacunarity; // inner loop of spectral construction, where the fractal is built for(curOctave = 1; curOctave < m_octaveCount; curOctave++) { // obtain displaced noise value. signal = (float)m_offset + (float)Utils.GradientCoherentNoise3D(x, y, z, m_seed, this.m_quality);; //scale amplitude appropriately for this frequency signal *= (float)m_weights[curOctave]; // scale increment by current altitude of function signal *= value; // Add the signal to the output value. value += signal; // Go to the next octave. x *= m_lacunarity; y *= m_lacunarity; z *= m_lacunarity; }//end for //take care of remainder in _octaveCount float remainder = m_octaveCount - (int)m_octaveCount; if(remainder > 0.0f) { signal = (float)m_offset + (float)tmpperl.GetValue(x, y, z); signal *= (float)m_weights[curOctave]; signal *= value; signal *= remainder; value += signal; }//end if return value; }
/// <summary> /// /// </summary> /// <param name="x"></param> /// <param name="z"></param> /// <returns></returns> public override float GetValue(float x, float z) { return((float)(perlinNoiseGenerator.GetValue(x, 0, z) / 2f) + 0.5f); }