private void CreateGrid(gridVertexMethod method) { currentResolution = resolution; mesh.Clear(); vertices = new Vector3[(resolution + 1) * (resolution + 1)]; colors = new Color[vertices.Length]; normals = new Vector3[vertices.Length]; Vector2[] uv = new Vector2[vertices.Length]; float stepSize = 1f / resolution; for (int v = 0, z = 0; z <= resolution; z++) { for (int x = 0; x <= resolution; x++, v++) { Vector3 cubeCoords = (method(x, z, stepSize) + rootPosition) * 2; //new Vector3(x * stepSize - 0.5f, 0f, z * stepSize - 0.5f); cubeToSphere(ref cubeCoords); vertices[v] = cubeCoords; colors[v] = Color.black; normals[v] = Vector3.up; uv[v] = new Vector2(x * stepSize, z * stepSize); float fx = x / (float)resolution; float fz = z / (float)resolution; Vector2 uvCoords = toMinusOneToOne(new Vector2(fx, fz)); uvCoords = squareToCircle(uvCoords); uvCoords = toZeroToOne(uvCoords); uv[v] = uvCoords; } } mesh.vertices = vertices; mesh.colors = colors; mesh.normals = normals; mesh.uv = uv; int[] triangles = new int[resolution * resolution * 6]; for (int t = 0, v = 0, y = 0; y < resolution; y++, v++) { for (int x = 0; x < resolution; x++, v++, t += 6) { triangles[t] = v; triangles[t + 1] = v + resolution + 1; triangles[t + 2] = v + 1; triangles[t + 3] = v + 1; triangles[t + 4] = v + resolution + 1; triangles[t + 5] = v + resolution + 2; } } mesh.triangles = triangles; }
public void Initialize(PlanetChunk parent, ChunckPosition position, gridVertexMethod method) { if (parent != null) { this.parent = parent; lodLevel = parent.lodLevel + 1; rootPosition = Vector3.right * 10; } else { lodLevel = 0; rootPosition = Vector3.zero; } //TODO on a encore des problems ac les uv ya de la distortion dans les coins... //set one of the main plane to test subdivision here CreateGrid(method); }