public void Draw(TriangleDrawer g, gl.Transform info, Vector3 Camera, bool onlyborders, Size screensize, float pixelprecision, int MaxResolution, Shader shader, float zoom) { int maxsize = screensize.Width > screensize.Height ? screensize.Width : screensize.Height; float precision = maxsize / pixelprecision * shapeSettings.PlanetRadius; for (int i = 0; i < 6; i++) { TerrainFaces[i].DrawMesh(info, Camera, g, shader, onlyborders, zoom, precision, 10); } }
public void GenerateRow_4padding10items() { //arrange var triangleDrawer = new TriangleDrawer(); //act var row = triangleDrawer.GenerateRow(4, 10); //assert StringAssert.AreEqualIgnoringCase(" * * * * * * * * * * ", row); }
public void GenerateRows_Size1_OneRow() { //arrange var triangleDrawer = new TriangleDrawer(); //act var rows = triangleDrawer.GenerateRows(0, 1); //assert Assert.AreEqual(1, rows.Count); StringAssert.AreEqualIgnoringCase("* ", rows[0]); }
void Draw() { Shader shader = Shade ? new Shader { ShadeB = 0.05f, ShadeG = 0.03f, ShadeR = 0.03f, LightB = 1f, LightG = 1f, LigtR = 1f, Enabled = true } : new Shader(); int w = (int)SystemParameters.PrimaryScreenWidth; int h = (int)SystemParameters.PrimaryScreenHeight; using (TriangleDrawer g = new TriangleDrawer(w, h)) { openview.Size size = new openview.Size(w, h); g.Lock(); gl.Matrix matrix = transform.Matrix; for (int i = 0; i < planets.Count; i++) { ShapeSettings shapeSettings = planets[i].shapeSettings; Vector3 v = matrix.ApplyTo(shapeSettings.position - transform.position); float x = v.x, y = v.y, z = v.z; float r = shapeSettings.PlanetRadius + shapeSettings.MaxDifference; y += r; bool xCorrect = x > 0 ? x - r <y : x + r> -y; bool zCorrect = z > 0 ? z - r <y : z + r> -y; gl.Transform Applied = transform; Applied.position = Applied.position - shapeSettings.position; //Applied.position = shapeSettings.position- Applied.position; if (y > 0 && xCorrect && zCorrect) { planets[i].Draw(g, Applied, transform.position, DrawOnlyBorders, size, precision, 1000 / precision, shader, zoom); } } if (DrawCockpit) { g.DrawImage(cockpit, 255); } if (MotionBlur) { if (LastDraw != null) { g.DrawImage(LastDraw, 128); } LastDraw = g.Unlock(); Background = new ImageBrush(LastDraw); } else { Background = new ImageBrush(g.Unlock()); } Title = g.elapsed.ToString(); } }
public void GenerateRows_Size3_3Rows() { //arrange var triangleDrawer = new TriangleDrawer(); //act var rows = triangleDrawer.GenerateRows(0, 2); //assert Assert.AreEqual(2, rows.Count); var expectedRow1 = @" * "; var expectedRow2 = @"* * "; StringAssert.AreEqualIgnoringCase(expectedRow1, rows[0]); StringAssert.AreEqualIgnoringCase(expectedRow2, rows[1]); }
void Start() { // BUILDING --- --- BUILDING --- --- BUILDING System.DateTime previousTime = System.DateTime.Now; var points = new List <Vec3>(); int n = seed; RandGen.Init(seed); switch (usedGenerator) { case Generator.Halton: points = SphereSampler.Halton(pointNumber, seed, bases[0], bases[1]).ToList(); break; case Generator.Uniform: for (int i = 0; i < pointNumber; i++) { double phi = 2.0 * System.Math.PI * RandGen.NextDouble(); double theta = System.Math.Acos(2.0 * RandGen.NextDouble() - 1.0); points.Add(Geometry.SphericalToEuclidean(phi, theta)); n++; } break; case Generator.Fibonnaci: // points = SphereSampler.Fibonnaci(pointNumber, 0.8).ToList(); points = SphereSampler.Fibonnaci(pointNumber).ToList(); break; case Generator.Poisson: points = SphereSampler.Poisson(pointNumber, 0.1f).ToList(); Debug.Log(points.Count); break; } System.TimeSpan delta = System.DateTime.Now - previousTime; Debug.Log(string.Format("BUILDING *** {0} secondes OU {1} milliseconds *** BUILDING", delta.TotalSeconds, delta.TotalMilliseconds)); Debug.Log("Total generated points: " + points.Count); if (triangulate) { // INIT --- --- INIT --- --- INIT previousTime = System.DateTime.Now; sphereMeshUsed = new SphericalMesh <int, int>(points.Select(x => Geometry.StereographicProjection(x)).ToArray(), false); delta = System.DateTime.Now - previousTime; Debug.Log("***"); Debug.Log(string.Format("*** INIT *** {0} secondes OU {1} milliseconds *** INIT", delta.TotalSeconds, delta.TotalMilliseconds)); // TRIANGULATION --- --- TRIANGULATION --- --- TRIANGULATION previousTime = System.DateTime.Now; sphereMeshUsed.Construct(); delta = System.DateTime.Now - previousTime; Debug.Log("***"); Debug.Log(string.Format("*** TRIANGULATION *** {0} secondes OU {1} milliseconds *** TRIANGULATION", delta.TotalSeconds, delta.TotalMilliseconds)); // // START DEBUGTOOLS // // START DEBUGTOOLS // // START DEBUGTOOLS // var test = radius * sphereMeshUsed.RightMostEdge.Rot.Origin; // if (triangulationOnSphere) // { // test = radius * Geometry.InvStereographicProjection(sphereMeshUsed.RightMostEdge.Rot.Origin); // } // var newGo = GameObject.Instantiate(shape); // newGo.transform.SetParent(transform); // newGo.transform.position = test.AsVector3(); // newGo.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); // // Color // var meshR = newGo.GetComponent<MeshRenderer>(); // if (meshR != null) // { // meshR.materials[0].color = Color.blue; // } // var test2 = radius * sphereMeshUsed.RightMostEdge.Destination; // if (triangulationOnSphere) // { // test2 = radius * Geometry.InvStereographicProjection(sphereMeshUsed.RightMostEdge.Destination); // } // var newGo2 = GameObject.Instantiate(shape); // newGo2.transform.SetParent(transform); // newGo2.transform.position = test2.AsVector3(); // newGo2.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); // // Color // var meshR2 = newGo2.GetComponent<MeshRenderer>(); // if (meshR2 != null) // { // meshR2.materials[0].color = Color.red; // } // int i = 0; // foreach (QuadEdge<int> edge in baseEdge.RightEdges()) // { // var test3 = radius * edge.Destination; // if (triangulationOnSphere) // { // test3 = radius * Geometry.InvStereographicProjection(edge.Destination); // } // var newGo3 = GameObject.Instantiate(shape); // newGo3.transform.SetParent(transform); // newGo3.transform.position = test3.AsVector3(); // newGo3.transform.localScale = new Vector3(4.0f, 4.0f, 4.0f); // // Color // var meshR3 = newGo3.GetComponent<MeshRenderer>(); // if (meshR3 != null) // { // meshR3.materials[0].color = Color.yellow; // } // if (i >= 0) // { // break; // } // i++; // } // // END DEBUGTOOLS // // END DEBUGTOOLS // // END DEBUGTOOLS // DRAWING --- --- DRAWING --- --- DRAWING List <Vec3> triangles; if (triangulationOnSphere) { triangles = sphereMeshUsed.Triangles().ForEach(x => Geometry.InvStereographicProjection(x) * radius).ToList(); } else { // Remove first triangles triangles = sphereMeshUsed.Triangles().ForEach(x => x * radius).ToList(); } TriangleDrawer.DrawLine(triangles, transform, mat, Color.black, lineScale); TriangleDrawer.DrawFace(triangles, transform, mat, gradient); TriangleDrawer.DrawPoints(triangles, transform, shape, Color.red, scale); // Remove first face (reconstructed hole) // List<Face<int, int>> faces = sphereMeshUsed.Faces(FaceConfig.Voronoi, radius).Collection().Skip(1).ToList(); List <Face <int, int> > faces = sphereMeshUsed.Faces(FaceConfig.Voronoi, radius).ToList(); // // START DEBUGTOOLS // // START DEBUGTOOLS // // START DEBUGTOOLS // int i = 0; // foreach (QuadEdge<int> edge in sphereMeshUsed.RightMostEdge.Oprev.Sym.Lnext.Oprev.FaceLeftEdges()) // // foreach (QuadEdge<int> edge in sphereMeshUsed.RightMostEdge.Oprev.Rprev.EdgesFrom()) // { // var test3 = edge.Destination; // // test3 = radius * test3; // // if (triangulationOnSphere) // // { // // test3 = radius * Geometry.InvStereographicProjection(edge.Destination); // // } // var newGo3 = GameObject.Instantiate(shape); // newGo3.transform.SetParent(transform); // newGo3.transform.position = test3.AsVector3(); // newGo3.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); // // Color // var meshR3 = newGo3.GetComponent<MeshRenderer>(); // if (meshR3 != null) // { // meshR3.materials[0].color = Color.yellow; // } // if (i >= 1) // { // // Add fake point created // break; // } // else // { // test3 = Geometry.CircumCenter3D(Geometry.InvStereographicProjection(edge.RotSym.Origin), // Geometry.InvStereographicProjection(edge.RotSym.Destination), // Geometry.InvStereographicProjection(edge.RotSym.Oprev.Destination)); // Vec3 a = Geometry.InvStereographicProjection(edge.RotSym.Origin); // Vec3 b = Geometry.InvStereographicProjection(edge.RotSym.Destination); // Vec3 c = Geometry.InvStereographicProjection(edge.RotSym.Oprev.Destination); // Debug.Log(a); // Debug.Log(b); // Debug.Log(c); // Vec3 ca = c - a; // Vec3 ba = b - a; // Vec3 baca = Vec3.Cross(ba, ca); // Debug.Log(baca.SquaredMagnitude); // Debug.Log(test3); // var newGo31 = GameObject.Instantiate(shape); // newGo31.transform.SetParent(transform); // newGo31.transform.position = (radius * Geometry.InvStereographicProjection(edge.RotSym.Origin)).AsVector3(); // newGo31.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); // // Color // var meshR31 = newGo31.GetComponent<MeshRenderer>(); // if (meshR31 != null) // { // meshR31.materials[0].color = Color.grey; // } // var newGo32 = GameObject.Instantiate(shape); // newGo32.transform.SetParent(transform); // newGo32.transform.position = (radius * Geometry.InvStereographicProjection(edge.RotSym.Destination)).AsVector3(); // newGo32.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); // // Color // var meshR32 = newGo32.GetComponent<MeshRenderer>(); // if (meshR32 != null) // { // meshR32.materials[0].color = Color.grey; // } // var newGo33 = GameObject.Instantiate(shape); // newGo33.transform.SetParent(transform); // newGo33.transform.position = (radius * Geometry.InvStereographicProjection(edge.RotSym.Oprev.Destination)).AsVector3(); // newGo33.transform.localScale = new Vector3(1.0f, 1.0f, 1.0f); // // Color // var meshR33 = newGo33.GetComponent<MeshRenderer>(); // if (meshR33 != null) // { // meshR33.materials[0].color = Color.grey; // } // double invDistanceScaled = radius / test3.Magnitude; // test3 *= invDistanceScaled; // var newGo34 = GameObject.Instantiate(shape); // newGo34.transform.SetParent(transform); // newGo34.transform.position = test3.AsVector3(); // newGo34.transform.localScale = new Vector3(2.0f, 2.0f, 2.0f); // // Color // var meshR34 = newGo34.GetComponent<MeshRenderer>(); // if (meshR34 != null) // { // meshR34.materials[0].color = Color.green; // } // } // // if (!Geometry.Ccw(edge.Destination, edge.Lprev.Destination, edge.Lprev.Origin)) // // { // // Debug.Log("FOUND"); // // if (meshR3 != null) // // { // // meshR3.materials[0].color = Color.magenta; // // } // // } // i++; // } // // END DEBUGTOOLS // // END DEBUGTOOLS // // END DEBUGTOOLS if (triangulationOnSphere) { float nbCells = (float)faces.Count; int indcolor2 = 0; foreach (Face <int, int> face in faces) { var color = gradient.Evaluate(indcolor2 / nbCells); face.DrawFace(transform, mat, color, scale: radius); face.DrawLine(transform, mat, Color.white, lineScale, loop: true); face.DrawPoints(transform, shape, mat, Color.blue, 0.6f * scale); indcolor2++; // if (indcolor2 > 11) // { // break; // } } } } else { // DRAWING --- --- DRAWING --- --- DRAWING points = points.Select(x => x * radius).ToList(); int ptId = 0; foreach (Vec3 point in points) { var newGo = GameObject.Instantiate(shape); newGo.name = string.Format("Fibonnaci Sphere {0}", ptId.ToString()); newGo.transform.SetParent(transform); newGo.transform.position = point.AsVector3(); newGo.transform.localScale = new Vector3(scale, scale, scale); // Color var meshR = newGo.GetComponent <MeshRenderer>(); if (meshR != null) { meshR.materials[0].color = Color.black; } ptId++; } } }
public static void CreateTriangles() { //var points = new List<Vector3>(); //Enumerable.Range(0, 10).ToList().ForEach(i => //{ // var x = UnityEngine.Random.Range(-5f, 5f); // var y = UnityEngine.Random.Range(-5f, 5f); // var pos = new Vector3(x, y, 0); // points.Add(pos); //}); //var drs = new Drawer(); //var triangles = DelaunayTriangulator.Triangulate(points); //foreach (var t in triangles) //{ // var triangleProp = new TriangleDrawProperty() // { // VertexA = t.A.Position, // VertexB = t.B.Position, // VertexC = t.C.Position, // VertexAColor = Color.cyan, // VertexBColor = Color.cyan, // VertexCColor = Color.cyan, // EdgeABColor = Color.green, // EdgeBCColor = Color.green, // EdgeCAColor = Color.green, // }; // var triangleDrawer = new TriangleDrawer(triangleProp); // triangleDrawer.BatchDraw = true; // drs.Drawers.Add(triangleDrawer); //} //DrawerManager.Instance.AddDrawer("delaunay", drs); //return; //var cube = GameObject.CreatePrimitive(PrimitiveType.Sphere); //var mesh = cube.GetComponent<MeshFilter>().sharedMesh; //var data = new DrawData(); //var index = 1; //for (int i = 0; i < mesh.triangles.Length;) //{ // var a = mesh.vertices[mesh.triangles[i++]]; // var b = mesh.vertices[mesh.triangles[i++]]; // var c = mesh.vertices[mesh.triangles[i++]]; // var triangleProp = new TriangleDrawerProperty() // { // A = a, // B = b, // C = c, // Color = Color.yellow, // Delay = 1 * (index++), // }; // var triangleDraer = new TriangleDrawer(triangleProp); // data.Draweres.Add(triangleDraer); //} //DrawerManager.Instance.Record("test", data); var drawer = new Drawer(); var cube = GameObject.CreatePrimitive(PrimitiveType.Cube); var mesh = cube.GetComponent <MeshFilter>().sharedMesh; for (int i = 0; i < mesh.triangles.Length;) { var a = mesh.vertices[mesh.triangles[i++]]; var b = mesh.vertices[mesh.triangles[i++]]; var c = mesh.vertices[mesh.triangles[i++]]; var triangleProp = new TriangleDrawProperty() { VertexA = a, VertexB = b, VertexC = c, VertexAColor = Color.cyan, VertexBColor = Color.cyan, VertexCColor = Color.cyan, EdgeABColor = Color.green, EdgeBCColor = Color.green, EdgeCAColor = Color.green, }; var triangleDrawer = new TriangleDrawer(triangleProp); drawer.Drawers.Add(triangleDrawer); } drawer.BatchDraw = true; DrawerManager.Instance.AddDrawer("cube", drawer); GameObject.DestroyImmediate(cube); { var drawer2 = new Drawer(); var cube2 = GameObject.CreatePrimitive(PrimitiveType.Cube); var mesh2 = cube2.GetComponent <MeshFilter>().sharedMesh; for (int i = 0; i < mesh2.triangles.Length;) { var a = mesh2.vertices[mesh2.triangles[i++]]; var b = mesh2.vertices[mesh2.triangles[i++]]; var c = mesh2.vertices[mesh2.triangles[i++]]; var triangleProp = new TriangleDrawProperty() { VertexA = a, VertexB = b, VertexC = c, VertexAColor = Color.cyan, VertexBColor = Color.cyan, VertexCColor = Color.cyan, EdgeABColor = Color.green, EdgeBCColor = Color.green, EdgeCAColor = Color.green, }; var triangleDrawer = new TriangleDrawer(triangleProp); triangleDrawer.BatchDraw = true; drawer2.Drawers.Add(triangleDrawer); } DrawerManager.Instance.AddDrawer("cube_batch_1", drawer2); GameObject.DestroyImmediate(cube2); } { var drawer2 = new Drawer(); var cube2 = GameObject.CreatePrimitive(PrimitiveType.Cube); var mesh2 = cube2.GetComponent <MeshFilter>().sharedMesh; for (int i = 0; i < mesh2.triangles.Length;) { var a = mesh2.vertices[mesh2.triangles[i++]]; var b = mesh2.vertices[mesh2.triangles[i++]]; var c = mesh2.vertices[mesh2.triangles[i++]]; var triangleProp = new TriangleDrawProperty() { VertexA = a, VertexB = b, VertexC = c, VertexAColor = Color.cyan, VertexBColor = Color.cyan, VertexCColor = Color.cyan, EdgeABColor = Color.green, EdgeBCColor = Color.green, EdgeCAColor = Color.green, }; var triangleDrawer = new TriangleDrawer(triangleProp); triangleDrawer.BatchDraw = true; drawer2.Drawers.Add(triangleDrawer); } drawer2.BatchDraw = true; DrawerManager.Instance.AddDrawer("cube_batch_2", drawer2); GameObject.DestroyImmediate(cube2); } var circlesDrawer = new Drawer(); for (int i = 0; i <= 36; i++) { var circleDrawerProp = new CircleDrawProperty(); circleDrawerProp.Center = UnityEngine.Vector3.zero; circleDrawerProp.Angle = 10 * i; circleDrawerProp.Color = Color.yellow; var d = i / 70f; circleDrawerProp.From = new Vector3(0.1f + d, 0.1f + d, 0.1f + d); circleDrawerProp.Radius = Vector3.Distance(circleDrawerProp.Center, circleDrawerProp.From); var vec1 = circleDrawerProp.From - circleDrawerProp.Center; var vec2 = new Vector3(-0.1f - d, -0.1f - d, 0.0f) - circleDrawerProp.Center; var normal = Vector3.Cross(vec1, vec2).normalized; circleDrawerProp.Normal = normal; var circleDrawer = new CircleDrawer(circleDrawerProp); circlesDrawer.Drawers.Add(circleDrawer); } DrawerManager.Instance.AddDrawer("circle", circlesDrawer); //{ // var drawer2 = new Drawer(); // var cube2 = GameObject.CreatePrimitive(PrimitiveType.Cylinder); // var mesh2 = cube2.GetComponent<MeshFilter>().sharedMesh; // for (int i = 0; i < mesh2.triangles.Length;) // { // var a = mesh2.vertices[mesh2.triangles[i++]]; // var b = mesh2.vertices[mesh2.triangles[i++]]; // var c = mesh2.vertices[mesh2.triangles[i++]]; // var triangleProp = new TriangleDrawProperty() // { // VertexA = a, // VertexB = b, // VertexC = c, // VertexAColor = Color.cyan, // VertexBColor = Color.cyan, // VertexCColor = Color.cyan, // EdgeABColor = Color.green, // EdgeBCColor = Color.green, // EdgeCAColor = Color.green, // }; // var triangleDrawer = new TriangleDrawer(triangleProp); // drawer2.Drawers.Add(triangleDrawer); // } // DrawerManager.Instance.AddDrawer("cylinder", drawer2); // GameObject.DestroyImmediate(cube2); //} //{ // var drawer2 = new Drawer(); // var cube2 = GameObject.CreatePrimitive(PrimitiveType.Sphere); // var mesh2 = cube2.GetComponent<MeshFilter>().sharedMesh; // for (int i = 0; i < mesh2.triangles.Length;) // { // var a = mesh2.vertices[mesh2.triangles[i++]]; // var b = mesh2.vertices[mesh2.triangles[i++]]; // var c = mesh2.vertices[mesh2.triangles[i++]]; // var triangleProp = new TriangleDrawProperty() // { // VertexA = a, // VertexB = b, // VertexC = c, // VertexAColor = Color.cyan, // VertexBColor = Color.cyan, // VertexCColor = Color.cyan, // EdgeABColor = Color.green, // EdgeBCColor = Color.green, // EdgeCAColor = Color.green, // }; // var triangleDrawer = new TriangleDrawer(triangleProp); // triangleDrawer.BatchDraw = true; // drawer2.Drawers.Add(triangleDrawer); // } // drawer2.BatchDraw = true; // DrawerManager.Instance.AddDrawer("sphere", drawer2); // GameObject.DestroyImmediate(cube2); //} //for (int i = 0; i < 50; i++) //{ // var circleDrawerProp = new CircleDrawProperty(); // circleDrawerProp.Center = UnityEngine.Vector3.zero; // circleDrawerProp.Angle = 280f; // circleDrawerProp.Color = Color.cyan; // circleDrawerProp.From = new UnityEngine.Vector3(1.0f, 1.0f, 1.0f); // circleDrawerProp.Radius = UnityEngine.Vector3.Distance(circleDrawerProp.Center, circleDrawerProp.From); // var vec1 = new UnityEngine.Vector3(1.0f, 1.0f, 1.0f) - circleDrawerProp.Center; // var vec2 = new UnityEngine.Vector3(-1.0f, -1.0f, -0.0f) - circleDrawerProp.Center; // var normal = UnityEngine.Vector3.Cross(vec1, vec2).normalized; // circleDrawerProp.Normal = normal; // var circleDrawer = new CircleDrawer(circleDrawerProp); // DrawerManager.Instance.AddDrawer("circle" + i.ToString(), circleDrawer); //} }
// Use this for initialization void Start() { // INIT --- --- INIT --- --- INIT System.DateTime previousTime = System.DateTime.Now; meshUsed = new Mesh2D <int, int>(points.ToArray(), false); System.TimeSpan delta = System.DateTime.Now - previousTime; Debug.Log("***"); Debug.Log(string.Format("*** INIT *** {0} secondes OU {1} milliseconds *** INIT", delta.TotalSeconds, delta.TotalMilliseconds)); // TRIANGULATION --- --- TRIANGULATION --- --- TRIANGULATION previousTime = System.DateTime.Now; meshUsed.Construct(); delta = System.DateTime.Now - previousTime; Debug.Log("***"); Debug.Log(string.Format("*** TRIANGULATION *** {0} secondes OU {1} milliseconds *** TRIANGULATION", delta.TotalSeconds, delta.TotalMilliseconds)); Vec3 pos; // Avoid infinite because safe not used some Insert method (needed for testing) if (points.Count > 10) { // LOCATE --- --- LOCATE --- --- LOCATE // points >= 10 | seed 154 pos = new Vec3(0.72 * boundaries[0], 0.546 * boundaries[1], 0.0); // var newGo = GameObject.Instantiate(shapes[0]); // newGo.name = "PointLocated"; // newGo.transform.SetParent(transform); // newGo.transform.position = pos.AsVector3(); // newGo.transform.localScale = new Vector3(5.0f, 5.0f, 5.0f); // // Color // var meshR = newGo.GetComponent<MeshRenderer>(); // if (meshR != null) // { // meshR.materials[0].color = Color.green; // } // Start locate previousTime = System.DateTime.Now; var edge = meshUsed.Locate(pos, safe: true); delta = System.DateTime.Now - previousTime; Debug.Log("***"); Debug.Log(string.Format("*** LOCATE *** {0} secondes OU {1} milliseconds *** LOCATE", delta.TotalSeconds, delta.TotalMilliseconds)); Debug.Log("Point is inside: " + meshUsed.InsideConvexHull(pos)); Debug.Log("Edge origin is: " + edge.Origin); // INSERT --- --- INSERT --- --- INSERT // points >= 10 | seed 154 previousTime = System.DateTime.Now; pos = new Vec3(1.1 * boundaries[0], 0.8 * boundaries[1], 0.0); bool result = meshUsed.Insert(pos, safe: true); Debug.Log("Site outside --> Not added: " + !result); pos = new Vec3(0.72265633333 * boundaries[0], 0.54732506667 * boundaries[1], 0.0); result = meshUsed.Insert(pos); Debug.Log("Site already existing --> Not added: " + !result); pos = new Vec3(0.76666666666667 * boundaries[0], pos.Y, pos.Z); result = meshUsed.Insert(pos); Debug.Log("Inside convex Hull --> Added: " + result); delta = System.DateTime.Now - previousTime; Debug.Log("***"); Debug.Log(string.Format("*** INSERT *** {0} secondes OU {1} milliseconds *** INSERT", delta.TotalSeconds, delta.TotalMilliseconds)); } // DRAWING --- --- DRAWING --- --- DRAWING previousTime = System.DateTime.Now; // Draw Delaunay var triangles = meshUsed.Triangles().ToList(); if (D_faces) { TriangleDrawer.DrawFace(triangles, transform, mat, gradient); } if (D_lines) { TriangleDrawer.DrawLine(triangles, transform, mat, Color.black, lineScale); } if (D_points) { TriangleDrawer.DrawPoints(triangles, transform, shapes[0], Color.red, 1.1f * scale); } // Get faces List <Face <int, int> > faces = meshUsed.Faces(celltype, Mathf.Max(boundaries) * 5.0, true) // .InsideHull() // .FiniteBounds() // .Finite() // .Bounds() // .AtInfinity() // .CenterCloseTo(new Vec3(boundaries[0] / 2.0, boundaries[1] / 2.0, 0.0), 50.0) // .CloseTo(new Vec3(boundaries[0] / 2.0, boundaries[1] / 2.0, 0.0), 50.0) // .Inside(Vec3.Zero, new Vec3(boundaries[0] * 0.25, boundaries[1] * 1.0, 1.0)) // .Inside(Vec3.Zero, new Vec3(boundaries[0] * 0.25, boundaries[1] * 0.5, 1.0)) .ToList(); float nbCells = (float)faces.Count; int indcolor2 = 0; foreach (Face <int, int> face in faces) { var color = gradient.Evaluate(indcolor2 / nbCells); if (V_faces) { face.DrawFace(transform, mat, color); } if (V_lines) { face.DrawLine(transform, mat, Color.white, lineScale, loop: true); } if (V_points) { face.DrawPoints(transform, shapes[1], mat, Color.blue, 0.8f * scale); } // if (V_circles) // { // face.DrawCircumCercle(transform, mat, color); // } indcolor2++; } delta = System.DateTime.Now - previousTime; Debug.Log("***"); Debug.Log(string.Format("*** DRAWING *** {0} secondes OU {1} milliseconds *** DRAWING", delta.TotalSeconds, delta.TotalMilliseconds)); Debug.Log("Points count : " + points.Count); Debug.Log("Triangle count : " + triangles.Count / 3); Debug.Log("Face count : " + nbCells); }
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 void DrawChildMeshes(int res, float minx, float miny, float width, gl.Transform transform, Vector3 Camera, TriangleDrawer graphics, Shader shader, bool onlyborders, float zoom, float precision, int maxprecision) { const int Spread = 4, sq = Spread * Spread; res /= Spread; float step = width / Spread; MeshHolder[] childs = Childs; if (childs == null) { childs = new MeshHolder[sq]; int k = 0; for (int j = 0; j < Spread; j++) { float mny = miny + step * j; for (int i = 0; i < Spread; i++) { float mnx = minx + step * i; childs[k] = new MeshHolder(Parent = this, shapeGenerator, LocalUp, AxisA, AxisB, mnx, mny, step); k++; } } Childs = childs; } for (int i = 0; i < sq; i++) { childs[i].DrawMesh(transform, Camera, graphics, shader, onlyborders, zoom, precision, maxprecision); } }