public void DrawMesh(gl.Transform transform, Vector3 Camera, TriangleDrawer graphics, Shader shader, bool onlyborders, float zoom, float precision, int maxprecision) { float minx = this.minx, miny = this.miny, width = this.width; maxprecision--; ShapeGenerator shapeGenerator = this.shapeGenerator; ShapeSettings shapeSettings = shapeGenerator.settings; NoiseSettings noiseSettings = shapeSettings.NoiseSettings; ElevationSettings settings = noiseSettings.settings; Vector3 PlanetCentre = shapeSettings.position; //Vector3 pos = transform.position - PlanetCentre; float radius = shapeSettings.PlanetRadius; float r = settings.WaterHeight * shapeSettings.MaxDifference + radius; float Middleelev = shapeGenerator.noiseFilter.Evaluate(MiddlePointOnUnitSphere); Vector3 MiddlePoint = MiddlePoint = MiddlePointOnUnitSphere * (radius + Middleelev * shapeSettings.MaxDifference) + PlanetCentre; float distance = Vector3.Distance(Camera, MiddlePoint); if (distance == 0) { return; } if (minx > 0) { } int resolution = Mathf.FloorToPower((int)(precision / distance * width), 2); //resolution will be >0 //#if false if (maxprecision > 0 && resolution > 16)//max 16x16=256 { DrawChildMeshes(resolution, minx, miny, width, transform, Camera, graphics, shader, onlyborders, zoom, precision, maxprecision); return; } //#endif else if (maxprecision == 0) { } Vector3 Max00 = (LocalUp * .5f + (minx - .5f) * AxisA + (miny - .5f) * AxisB).normalized * r + PlanetCentre; Vector3 Max01 = (LocalUp * .5f + (minx - .5f) * AxisA + ((miny + width) - .5f) * AxisB).normalized * r + PlanetCentre; Vector3 Max10 = (LocalUp * .5f + ((minx + width) - .5f) * AxisA + (miny - .5f) * AxisB).normalized * r + PlanetCentre; Vector3 Max11 = (LocalUp * .5f + ((minx + width) - .5f) * AxisA + ((miny + width) - .5f) * AxisB).normalized * r + PlanetCentre; Vector3 nearest = Vector3.Nearest(Camera, Max00, Max01, Max10, Max11); //int intersections = Vector3.FindIntersections(PlanetCentre, r - Mathf.FloatEpsilon, nearest, Camera - nearest, out Vector3 nearestpoint, out Vector3 farthestpoint); //if (intersections > 0) return;//intersects with sphere if (LastResolution == resolution) { OpenGl.Draw(graphics, LastMesh, transform, onlyborders, shader, zoom); return; } #region initialisation int resolution1 = resolution + 1; Vector3[] vertices = new Vector3[resolution1 * resolution1]; int cnt = resolution * resolution * 6; int[] triangles = new int[cnt]; byte[] colors = new byte[cnt]; int triIndex = 0; int i = 0; float wh = settings.WaterHeight; Color watercolor = settings.WaterColor; Elevation[] Elevations = settings.Elevations.ToArray(); int elevcount = Elevations.Length; float Scale = width / resolution; #endregion for (int y = 0; y < resolution1; y++) { for (int x = 0; x < resolution1; x++) { #region Calculate Vertices Vector2 percent = new Vector2(x * Scale + minx, y * Scale + miny); Vector3 pointOnUnitCube = LocalUp * .5f + (percent.x - .5f) * AxisA + (percent.y - .5f) * AxisB; Vector3 PointOnUnitSphere = pointOnUnitCube.normalized; float elev = shapeGenerator.noiseFilter.Evaluate(PointOnUnitSphere); vertices[i] = PointOnUnitSphere * (radius + elev * shapeSettings.MaxDifference);// + PlanetCentre; #endregion if (x != resolution && y != resolution) { Color color = watercolor; if (elev > wh + Mathf.FloatEpsilon) { for (int j = 0; j < elevcount; j++) { if (elev < Elevations[j].height + Mathf.FloatEpsilon) { color = Elevations[j].color; break; } } } #region Calculate Colors colors[triIndex] = color.R; colors[triIndex + 1] = color.G; colors[triIndex + 2] = color.B; colors[triIndex + 3] = color.R; colors[triIndex + 4] = color.G; colors[triIndex + 5] = color.B; #endregion #region Calculate Triangles triangles[triIndex] = i; triangles[triIndex + 1] = i + resolution1 + 1; triangles[triIndex + 2] = i + resolution1; triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution1 + 1; #endregion triIndex += 6; } i++; } } //mesh.Clear(); LastMesh = new Mesh(vertices, triangles, colors); OpenGl.Draw(graphics, LastMesh, transform, onlyborders, shader, zoom); LastResolution = resolution; //mesh.shader = shader; //mesh.RecalculateNormals(); }
public TerrainFace(ShapeGenerator shapeGenerator, Vector3 localup) : base(null, shapeGenerator, localup, new Vector3(localup.y, localup.z, localup.x), Vector3.Cross(localup, new Vector3(localup.y, localup.z, localup.x)), 0, 0, 1) { }