public void ConstructMesh() { //array of vertices = r^2 vertices = new Vector3[resolution * resolution]; //total number of meshes (3 connected vertecies) = (r-1)^2 * 2 //total number of meshes * (vertecies each mesh uses) = ((r-1)^2 * 2) * 3 int[] triangles = new int[(resolution - 1) * (resolution - 1) * 2 * 3]; int triIndex = 0; Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length]; for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { //index counter int i = x + y * resolution; //it varies between 0-1 //starts with 0, each time added by a fraction Vector2 percent = new Vector2(x, y) / (resolution - 1); //the percent usage is very tricky and nice Vector3 pointOnUnitCube = localUp //point position on the facing axis + (percent.x - 0.5f) * 2 * axisA //point position on the "relative" +x + (percent.y - 0.5f) * 2 * axisB; //point position on the "relative" -y Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; //vertices[i] = pointOnUnitCube; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); uv[i].y = unscaledElevation; //I can not create any mesh on the edge if (x != resolution - 1 && y != resolution - 1) { //first triangle triangles[triIndex + 0] = i; triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; //second triangle triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; triIndex += 6; } } } mesh.Clear(); //clear previous data mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.uv = uv; }
// Generate the mesh of the face. public void ConstructMesh() { // Array of vertices (points on the plane) Vector3[] vertices = new Vector3[resolution * resolution]; // Calculate the amount of triangles within the plane given the amount of vertices. int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6]; int triIndex = 0; // The current index (point) we are drawing the triangle from. Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length]; // Ensure UVs are the correct size so it is consistent with planet resolution. for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { // Calculates the current index (which triangle we are creating). int i = x + y * resolution; // Keeps track of how complete the mesh is, used to define where the vertex should be on the face. Vector2 percent = new Vector2(x, y) / (resolution - 1); // Places a vertex on the cube using the percent value above; the axes, which tells us how far along each axis we are; and the current shape settings. Vector3 pointOnUnitCube = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB; Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; // Create a sphere by making all vertices the same distance away from the center. float unscaledElevation = shapeGenerator.CalculateUnscaledGeneration(pointOnUnitSphere); vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); // Get UV data. uv[i].y = unscaledElevation; // We divide the squares in the mesh into two triangles using the following formulas: // i, i + r (resolution, or number of vertices per line) + 1, i + r to determine the points of the first triangle. // i, i + 1, i + r + 1 for the second triangle. if (x != resolution - 1 && y != resolution - 1) { // first triangle triangles[triIndex] = i; // first vertex of first triangle triangles[triIndex + 1] = i + resolution + 1; // second vertex of first triangle triangles[triIndex + 2] = i + resolution; // third vertex of first triangle // second triangle triangles[triIndex + 3] = i; // first vertex of second triangle triangles[triIndex + 4] = i + 1; // second vertex of second triangle triangles[triIndex + 5] = i + resolution + 1; // third vertex of second triangle triIndex += 6; // added six vertices, so we increment the current index by six. } } } // Clears current mesh data just in case we are viewing the face at a lower resolution, so nonexistent indeces are no longer referenced. mesh.Clear(); // Assign data to the mesh. Ensures that data is not lost upon regeneration. mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.uv = uv; }
public void ConstructMesh() { Vector3[] vertices = new Vector3[resolution * resolution]; int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6]; int triIndex = 0; // getting each vertex of triangles in mesh for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { //same as i++ if i outside of for loops int i = x + y * resolution; Vector2 percent = new Vector2(x, y) / (resolution - 1); Vector3 pointOnUnitCube = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB; //same distance to the center so the cube becomes a sphere Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; pointsOnUnitSphere[i] = pointOnUnitSphere; //vertices[i] = pointOnUnitCube; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); // 0 or vertex value vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); if (x != resolution - 1 && y != resolution - 1) { triangles[triIndex] = i; triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; triIndex += 6; } } } corners[0] = 0; corners[1] = 0 + resolution - 1; corners[2] = vertices.Length - 1; corners[3] = vertices.Length - resolution; Vector3[] normals = new Vector3[vertices.Length]; for (int i = 0; i < normals.Length; i++) { normals[i] = (vertices[i] - origin).normalized; } mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; // mesh.RecalculateNormals(); mesh.normals = normals; }
// ********************************************************************** // public void ConstructMesh() { // Make a mesh out of lots of small triangles defined over a grid of resolution size 'resolution' Vector3[] vertices = new Vector3[resolution * resolution]; int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6]; // this many triangles as a function of number of vertices on each dimension (i.e. resolution). int triIndex = 0; Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length]; // if uv is correct length then all good, otherwise make a new one for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { int i = x + y * resolution; // this is a counter, same as setting int i=0 outside y loop and incrementing on inner loop Vector2 percent = new Vector2(x, y) / (resolution - 1); Vector3 pointOnUnitCube = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB; // geometry magic from youtube tutorial //vertices[i] = pointOnUnitCube; // if you want a cube use this // Turn the cube into a sphere Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); uv[i].y = unscaledElevation; // Note that vertices are labelled as: e.g. 0 1 2 // 3 4 5 // 6 7 8 // and so triangles making up each square in this grid are indexed as e.g. 0-4-3; 0-1-4; 1-5-4; etc if ((x != resolution - 1) && (y != resolution - 1)) { // first triangle triangles[triIndex] = i; triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; triIndex += 6; } } } mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.uv = uv; }
//Creamos las mallas que representan cada cara public void CreateMesh() { Vector3[] vertices = new Vector3[resolution * resolution]; int[] triangles = new int[(resolution - 1) * (resolution - 1) * 2 * 3]; Vector2[] uvHeight = (mesh.uv.Length == vertices.Length)? mesh.uv : new Vector2[vertices.Length]; Vector2[] uvTex = (mesh.uv2.Length == vertices.Length) ? mesh.uv2 : new Vector2[vertices.Length]; for (int vertIndex = 0, triIndex = 0, y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { //Creamos un cubo con el numero de poligonos dependiente de la resolucion Vector2 percent = new Vector2(x, y) / (resolution - 1); uvTex[vertIndex] = percent; //Una unidad en el eje y Percent pasa de 0-1 a -1-1 y lo multiplicamos por la unidad del eje x y eje z Vector3 pointOnUnitCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB; //Normalizamos cada punto para pasar de un cubo a una esfera Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; //Calculamos su altura float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); vertices[vertIndex] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); uvHeight[vertIndex].y = unscaledElevation; //Creamos la matriz de triangulos if (x != (resolution - 1) && y != (resolution - 1)) { triangles[triIndex] = vertIndex; triangles[triIndex + 1] = vertIndex + resolution + 1; triangles[triIndex + 2] = vertIndex + resolution; triangles[triIndex + 3] = vertIndex; triangles[triIndex + 4] = vertIndex + 1; triangles[triIndex + 5] = vertIndex + resolution + 1; triIndex += 6; } vertIndex++; } } //Actualizamos la malla mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.uv = uvHeight; mesh.uv2 = uvTex; }
public void ConstructMesh() { Vector3[] vertices = new Vector3[resolution * resolution]; //-> 6 = number of triangles to make a square * number of vertices to make a triangle (2*3) int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6]; int i = 0; int triIndex = 0; Vector2[] UV = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length]; for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { // how close to complete each of these loops is Vector2 percent = new Vector2(x, y) / (resolution - 1); Vector3 pointOnUnitCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB; // transform the current planar cube as a spherical/round plane Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); // using the y axis for the biomes UV[i].y = unscaledElevation; // make a square for this mesh if (x != resolution - 1 && y != resolution - 1) { // first triangle triangles[triIndex] = i; triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; // second triangle triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; // we added 6 vertices, update the index count triIndex += 6; } ++i; } } // clear all the data from the mesh to prevent // adding to the previous lower resolution mesh mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.uv = UV; }
public void ConstructMesh() { Vector3[] vertices = new Vector3[resolution * resolution]; //Number of vertices along a single edge of a face(resolution squared) int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6]; int triIndex = 0; //Keeps a track of whicch point of the square is where //we want to make sure that uvs are thye right size in case resolution is changed, aso we add an ifd statement Vector2[] uv = (mesh.uv.Length == vertices.Length)? mesh.uv : new Vector2[vertices.Length]; //This is added so when we rebuild the mesh we don't want to lose the uv data. //This method uses the same technique we used in Physics to crerate the Physics Flag for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { int i = x + y * resolution; Vector2 percent = new Vector2(x, y) / (resolution - 1); Vector3 pointOnUnitCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB; Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; //vertices[i] = pointOnUnitCube; //Displays a cube instead of a sphere //vertices[i] = pointOnUnitSphere; //Just to infllate cube to Shhere //vertices[i] = shapeGenerator.CalclulatePointOnPlanet(pointOnUnitSphere); //Before altering CalculatePointOnPlanet to add ocean depth float unscaledElevation = shapeGenerator.CalculateUncsaledElevation(pointOnUnitSphere); vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); uv[i].y = unscaledElevation; //X axis is used for biomes if (x != resolution - 1 && y != resolution - 1) { triangles[triIndex] = i; triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; triIndex += 6; } } } //If we update the mesh with a lower resolution version we will be referencing indeices that don't ewxist, so we clear out the mesh and reassign it. mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.normals = vertices; //Fixes the seams temporarily instead of recalculating the nmormals. also saves computiung power //mesh.RecalculateNormals(); //works but seams are visible if (mesh.uv.Length == uv.Length) { mesh.uv = uv; } }
public void ConstructMesh() { Vector3[] vertices = new Vector3[resolution * resolution]; int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6]; // number of triangles for a square mesh with resolution vertices per side, times 3 for the number of vertex triplets int triIndex = 0; Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length]; // save uv before resetting mesh, to avoid recalculating for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { int i = x + y * resolution; // number of total iterations Vector2 percent = new Vector2(x, y) / (resolution - 1); // percent on the x-axis and y-axis, between 0 and 1 Vector3 pointOnUnitCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB; // sum of base vectors Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); uv[i].y = unscaledElevation; // Create the triangles, if not on right or bottom edges if (x != resolution - 1 && y != resolution - 1) { // Create triangle South-East triangles[triIndex] = i; triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; // Create triangle East-South triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; // Increment triIndex triIndex += 6; } } } // Refresh mesh mesh.Clear(); // clear all data to avoid errors when downgrading resolution mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.uv = uv; // reassigned saved uv }
public void ConstructMesh() { Vector3[] vertices = new Vector3[resolution * resolution]; int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6]; int triIndex = 0; Vector2[] uv; if (mesh.uv.Length == vertices.Length) { uv = mesh.uv; } else { uv = new Vector2[vertices.Length]; } for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { int i = x + y * resolution; Vector2 percent = new Vector2(x, y) / (resolution - 1); Vector3 pointOnUnitCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB; Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); uv[i].y = unscaledElevation; if (x != resolution - 1 && y != resolution - 1) { triangles[triIndex] = i; triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; triIndex += 6; } } } mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.uv = uv; meshCollider.sharedMesh = mesh; }
public void ConstructMesh() { Vector3[] _vertices = new Vector3[resolution * resolution]; int[] _triangles = new int[(resolution - 1) * (resolution - 1) * 6]; int _triangleIndex = 0; Vector2[] _uvData = (mesh.uv.Length == _vertices.Length) ? mesh.uv : new Vector2[_vertices.Length]; for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { int _index = y * resolution + x; Vector2 _percent = new Vector2(x, y) / (resolution - 1); Vector3 _pointOnUnitCube = localUp + (_percent.x - 0.5f) * 2 * axisA + (_percent.y - 0.5f) * 2 * axisB; Vector3 _pointOnUnitSphere = _pointOnUnitCube.normalized; float _unscaledElevation = shapeGenerator.CalculateUnscaledElevation(_pointOnUnitSphere); _vertices[_index] = _pointOnUnitSphere * shapeGenerator.GetScaledElevation(_unscaledElevation); _uvData[_index].y = _unscaledElevation; if (x != resolution - 1 && y != resolution - 1) { // first triangle in square _triangles[_triangleIndex] = _index; _triangles[_triangleIndex + 1] = _index + resolution + 1; _triangles[_triangleIndex + 2] = _index + resolution; // second triangle in square _triangles[_triangleIndex + 3] = _index; _triangles[_triangleIndex + 4] = _index + 1; _triangles[_triangleIndex + 5] = _index + resolution + 1; _triangleIndex += 6; } } } mesh.Clear(); mesh.vertices = _vertices; mesh.triangles = _triangles; mesh.RecalculateNormals(); mesh.uv = _uvData; }
public void ConstructMesh() { Vector3[] vertices = new Vector3[_resolution * _resolution]; int[] triangles = new int[(_resolution - 1) * (_resolution - 1) * 6]; int triangleIndex = 0; Vector2[] uv = (_mesh.uv.Length == vertices.Length) ? _mesh.uv : new Vector2[vertices.Length]; for (int y = 0; y < _resolution; y++) { for (int x = 0; x < _resolution; x++) { Vector2 percent = new Vector2(x, y) / (_resolution - 1); Vector3 pointOnUnitCube = _localUp + (percent.x - .5f) * 2 * _xAxis + (percent.y - .5f) * 2 * _yAxis; Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; float unscaledElevation = _shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); int i = x + y * _resolution; vertices[i] = pointOnUnitSphere * _shapeGenerator.GetScaledElevation(unscaledElevation); uv[i].y = unscaledElevation; if (x != _resolution - 1 && y != _resolution - 1) { triangles[triangleIndex] = i; triangles[triangleIndex + 1] = i + _resolution + 1; triangles[triangleIndex + 2] = i + _resolution; triangles[triangleIndex + 3] = i; triangles[triangleIndex + 4] = i + 1; triangles[triangleIndex + 5] = i + _resolution + 1; triangleIndex += 6; } } } _mesh.Clear(); _mesh.vertices = vertices; _mesh.triangles = triangles; _mesh.RecalculateNormals(); _mesh.uv = uv; }
public void ConstructMesh() { var vertices = new Vector3[resolution * resolution]; var triangles = new int[(resolution - 1) * (resolution - 1) * 6]; var triIndex = 0; var uv = mesh.uv.Length == vertices.Length ? mesh.uv : new Vector2[vertices.Length]; for (var y = 0; y < resolution; y++) { for (var x = 0; x < resolution; x++) { var i = x + y * resolution; var percent = new Vector2(x, y) / (resolution - 1); var pointOnCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB; var pointOnSphere = pointOnCube.normalized; var unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnSphere); vertices[i] = pointOnSphere * shapeGenerator.GetScaledElevation(unscaledElevation); uv[i].y = unscaledElevation; if (x == resolution - 1 || y == resolution - 1) { continue; } triangles[triIndex] = i; triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; triIndex += 6; } } mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.uv = uv; }
public void ConstructMesh() { Vector3[] vertices = new Vector3[resolution * resolution]; int[] triangles = new int[(resolution - 1) * (resolution - 1) * 2 * 3]; // number of squares multiplied by two triangles in a square, by 3 vertices in a triangle int triIndex = 0; Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length]; for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { int i = x + (y * resolution); Vector2 percent = new Vector2(x, y) / (resolution - 1); Vector3 pointOnCube = localUp + ((percent.x - .5f) * 2 * axisA) + ((percent.y - .5f) * 2 * axisB); Vector3 pointOnSphere = pointOnCube.normalized; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnSphere); vertices[i] = pointOnSphere * shapeGenerator.GetScaledElevation(unscaledElevation); uv[i].y = unscaledElevation; //todo same code if (x != resolution - 1 && y != resolution - 1) { triangles[triIndex] = i; triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; triIndex += 6; } } } mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.uv = uv; }
public void ConstructMesh() { Vector3[] vertices = new Vector3[resolution * resolution]; int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6]; // https://prnt.sc/r6if91 int triIndex = 0; Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length]; for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { int i = x + y * resolution; Vector2 percent = new Vector2(x, y) / (resolution - 1); Vector3 pointOnUnitCube = axisY + (percent.x - 0.5f) * 2 * axisX + (percent.y - 0.5f) * 2 * axisZ; Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); uv[i].y = unscaledElevation; // do not construct for the border of the mesh if (x != resolution - 1 && y != resolution - 1) { triangles[triIndex] = i; triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; triIndex += 6; } } } mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.uv = uv; }
public void ConstructMesh() { Vector3[] vertices = new Vector3[resolution * resolution]; int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6]; int triIndex = 0; Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length]; for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { int i = x + y * resolution; // the same as the center is 0 and corner is -1 to 1 Vector2 percent = new Vector2(x, y) / (resolution - 1); Vector3 pointOnUnitCube = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB; Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); uv[i].y = unscaledElevation; if (x != resolution - 1 && y != resolution - 1) { triangles[triIndex] = i; triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; triIndex += 6; } } } mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.uv = uv; //ffGenerator.PopulateTerrain(vertices, index, center); }
public void ConstructMesh() { Vector3[] vertices = new Vector3[resolution * resolution]; //Store all vertices int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6]; //Int array to indeces of all triangles. size = resolution-1 squared(number of squares) * 2 (number of triangles per square) * 3 (number of vertices per triangle) int triIndex = 0; //Debug.Log(mesh); //mesh is setto null for some reason... Vector2[] uv = (mesh.uv.Length == vertices.Length)?mesh.uv:new Vector2[vertices.Length]; for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) //Iterate over entire face { int i = x + y * resolution; Vector2 percent = new Vector2(x, y) / (resolution - 1); //Tells us how far through the loop we are Vector3 pointOnUnitCube = localUp + (percent.x - .5f) * 2 * axisA + (percent.y - .5f) * 2 * axisB; Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; //used to go from a cube to a sphere float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); uv[i].y = unscaledElevation; if (x != resolution - 1 && y != resolution - 1) //vertex is not along the right or bottom edges { triangles[triIndex] = i; //Build the two triangles that make up a square by defining their 6 points (3 per triangle) triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; triIndex += 6; } } } mesh.Clear(); //If we update with a lower resolution, then when we set mesh.vertices, mesh.triangles will reference vertices that no longer exist and will throw an error. Therefore we need to clear the mesh before setting the new vertices mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.uv = uv; }
public void GenerateMesh() { var vertices = new Vector3[resolution * resolution]; var triangles = new int[(resolution - 1) * (resolution - 1) * 6]; Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length]; int triIndex = 0; for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { int i = x + y * resolution; var percent = new Vector2(x, y) / (resolution - 1); var pointOnUnitCube = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB; var pointOnUnitSphere = pointOnUnitCube.normalized; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); uv[i].y = unscaledElevation; if (x != resolution - 1 && y != resolution - 1) { triangles[triIndex] = i; triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; triIndex += 6; } } } mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.uv = uv; }
public void ConstructMesh() { Vector3[] vertices = new Vector3[resolution * resolution]; int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6]; int triIndex = 0; Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length]; for (int y = 0, i = 0; y < resolution; ++y) { for (int x = 0; x < resolution; ++x, ++i) { Vector2 percent = new Vector2(x, y) / (resolution - 1); Vector3 pointOnUnitCube = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB; Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); uv[i].y = unscaledElevation; if (x != resolution - 1 && y != resolution - 1) { triangles[triIndex++] = i; triangles[triIndex++] = i + resolution + 1; triangles[triIndex++] = i + resolution; triangles[triIndex++] = i; triangles[triIndex++] = i + 1; triangles[triIndex++] = i + resolution + 1; } } } mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); mesh.uv = uv; }
public void ConstructMesh(int levelOfDetail) { int simplificationIncriment = levelOfDetail; //levelOfDetail==0?1:levelOfDetail*2; int verticesPerLine = (resolution - 1) / simplificationIncriment + 1; //LOD MeshData meshData = new MeshData(verticesPerLine, verticesPerLine); int vertexIndex = 0; Vector3[] vertices = new Vector3[resolution * resolution]; int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6]; Vector2[] uv = (mesh.uv.Length == vertices.Length)?mesh.uv:new Vector2[vertices.Length]; for (int y = 0; y < resolution; y += simplificationIncriment) { for (int x = 0; x < resolution; x += simplificationIncriment) { //LOD //vertexIndex=x + y * resolution; Vector2 percent = new Vector2(x, y) / (resolution - 1); Vector3 pointOnUnitCube = localUp + (percent.x - .5f * subfaceNumber + (subfaceIndex % subfaceNumber)) * (2f / subfaceNumber) * axisA + (percent.y - .5f * subfaceNumber + (subfaceIndex / subfaceNumber)) * (2f / subfaceNumber) * axisB; Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); //LOD meshData.vertices[vertexIndex] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); meshData.uvs[vertexIndex].y = unscaledElevation; if (x < resolution - 1 && y < resolution - 1) { meshData.AddTriangle(vertexIndex, vertexIndex + verticesPerLine + 1, vertexIndex + verticesPerLine); meshData.AddTriangle(vertexIndex, vertexIndex + 1, vertexIndex + verticesPerLine + 1); } vertexIndex++; } } mesh.Clear(); mesh = meshData.CreateMesh(mesh); }
public void ConstructMesh(int xMesh, int yMesh, int divisions, Vector3 center) { Vector3[] vertices = new Vector3[resolution * resolution]; int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6]; int[] realTriangles = new int[(resolution - 1) * (resolution - 1) * 6]; int triIndex = 0; Vector2[] uv = (mesh.uv.Length == vertices.Length)?mesh.uv:new Vector2[vertices.Length]; Vector3[] norms = new Vector3[resolution * resolution]; for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { int i = x + y * resolution; Vector2 percent = new Vector2(x - 0, y - 0) / (resolution - 1 - 0); Vector3 pointOnUnitCube = localUp + ((float)xMesh / divisions + percent.x / divisions - .5f) * 2 * axisA + ((float)yMesh / divisions + percent.y / divisions - .5f) * 2 * axisB; Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); vertices[i] = pointOnUnitSphere * shapeGenerator.GetScaledElevation(unscaledElevation); uv[i].x = (unscaledElevation > 0) ? unscaledElevation : unscaledElevation * 1; if (unscaledElevation > 0.2f) { //Debug.Log(unscaledElevation); } if (x < resolution - 1 && y < resolution - 1) { triangles[triIndex] = i; triangles[triIndex + 1] = i + resolution + 1; triangles[triIndex + 2] = i + resolution; triangles[triIndex + 3] = i; triangles[triIndex + 4] = i + 1; triangles[triIndex + 5] = i + resolution + 1; if (x < hiddenResolution - 1 && y < hiddenResolution - 1) { realTriangles[triIndex] = i; realTriangles[triIndex + 1] = i + resolution + 1; realTriangles[triIndex + 2] = i + resolution; realTriangles[triIndex + 3] = i; realTriangles[triIndex + 4] = i + 1; realTriangles[triIndex + 5] = i + resolution + 1; } triIndex += 6; } } } mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.RecalculateNormals(); //mesh.triangles = realTriangles; mesh.uv = uv; norms = mesh.normals; for (int y = 0; y < resolution; y++) { for (int x = 0; x < resolution; x++) { if (x == 0 || x == (resolution - 1) || y == 0 || y == (resolution - 1)) { int i = x + y * resolution; Vector2 percent = new Vector2(x - 0, y - 0) / (resolution - 1 - 0); Vector3 pointOnUnitCube = localUp + ((float)xMesh / divisions + percent.x / divisions - .5f) * 2 * axisA + ((float)yMesh / divisions + percent.y / divisions - .5f) * 2 * axisB; Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; norms[i] = pointOnUnitSphere; } } } mesh.normals = norms; }
public void ConstructMesh() { Vector3[] vertices = new Vector3[resolution * resolution]; int[] triangles = new int[(resolution - 1) * (resolution - 1) * 6]; Vector3[] borderVertices = new Vector3[resolution * 4 + 4]; int[] borderTriangles = new int[24 * resolution]; int triIndex = 0; int borderTriIndex = 0; Vector2[] uv = (mesh.uv.Length == vertices.Length) ? mesh.uv : new Vector2[vertices.Length]; int[,] vertexIndicesMap = new int[borderedResolution, borderedResolution]; int meshVertexIndex = 0; int borderVertexIndex = -1; for (int y = 0; y < borderedResolution; y++) { for (int x = 0; x < borderedResolution; x++) { bool isBorderVertex = y == 0 || y == borderedResolution - 1 || x == 0 || x == borderedResolution - 1; if (isBorderVertex) { vertexIndicesMap[x, y] = borderVertexIndex; borderVertexIndex--; } else { vertexIndicesMap[x, y] = meshVertexIndex; meshVertexIndex++; } } } for (int y = 0; y < borderedResolution; y++) { for (int x = 0; x < borderedResolution; x++) { int i = vertexIndicesMap[x, y]; Vector2 percent = new Vector2(x - 1, y - 1) / (resolution - 1); Vector3 pointOnUnitCube = localUp + (percent.x - 0.5f) * 2 * axisA + (percent.y - 0.5f) * 2 * axisB; Vector3 pointOnUnitSphere = pointOnUnitCube.normalized; float unscaledElevation = shapeGenerator.CalculateUnscaledElevation(pointOnUnitSphere); float scaledElevation = shapeGenerator.GetScaledElevation(unscaledElevation); // shapeGenerator.elevationMinMax.AddValue(scaledElevation); Vector3 vertexPosition = pointOnUnitSphere * scaledElevation; float vertexUV = unscaledElevation; AddVertex(vertexPosition, vertexUV, i, ref vertices, ref uv, ref borderVertices); if (x != borderedResolution - 1 && y != borderedResolution - 1) { int a = vertexIndicesMap[x, y]; int b = vertexIndicesMap[x + 1, y]; int c = vertexIndicesMap[x, y + 1]; int d = vertexIndicesMap[x + 1, y + 1]; AddTriangle(a, d, c, ref triIndex, ref borderTriIndex, ref triangles, ref borderTriangles); AddTriangle(d, a, b, ref triIndex, ref borderTriIndex, ref triangles, ref borderTriangles); } } } mesh.Clear(); mesh.vertices = vertices; mesh.triangles = triangles; mesh.normals = CalculateNormals(ref triangles, ref borderTriangles, ref vertices, ref borderVertices); mesh.uv = uv; }
// Update is called once per frame void Update() { origin = originController.GetOrigin(); Vector3 clamp; ShapeSettings settings = originController.objects[0].GetComponent <Planet>().shapeSettings; ShapeGenerator temp = new ShapeGenerator(); temp.UpdateSettings(settings); for (int i = 0; i < 25; i++) { Vector3 offset = Quaternion.LookRotation(-transform.position, transform.up) * new Vector3((i % size) * 40, Mathf.Floor(i / size) * 40, 0); buildings[i].transform.position = origin + transform.position * 10000 + offset; clamp = origin + (Quaternion.LookRotation(origin - buildings[i].transform.position) * new Vector3(0, 0, -100010)) * temp.GetScaledElevation(temp.CalculateUnscaledElevation(Quaternion.LookRotation(origin - buildings[i].transform.position) * new Vector3(0, 0, -1))); buildings[i].transform.position = clamp; } }