public static START_FUNCTION_TIMER ( string functionName ) : void | ||
functionName | string | |
return | void |
private void CreateGeodesicSphere(int resolution) { var container = new GameObject { name = "GeodesicSphere_" + resolution + "_Subdivisions" }; container.transform.parent = World; var pos = Vector3.zero; if (CreateEveryVersion) { pos.x = 2.5f * (resolution % 4); pos.y = 2.5f - (2 * resolution / 8f) * 2.5f; } container.transform.position = pos; var isSingleMesh = resolution <= MaxResolutionWithSingleMesh; _nbVertices = NbIcosahedronFaces * 3 * (int)Mathf.Pow(9, resolution); // 20 triangles on an icosahedron, 3 vertices per triangle, each triangle when subdivided yields 4 triangles var verticesPerFace = _nbVertices / NbIcosahedronFaces; var meshSubdivisionDepth = (int)Mathf.Pow(4, Mathf.Max(0, resolution - MaxResolutionBeforeMeshSubdivision)); var nbMeshSubdivisions = isSingleMesh ? 1 : NbIcosahedronFaces * meshSubdivisionDepth; var verticesPerMesh = isSingleMesh ? _nbVertices : verticesPerFace / meshSubdivisionDepth; Debug.Log("Total Verts [" + _nbVertices + "]"); Debug.Log("Verts per Face [" + verticesPerFace + "]"); Debug.Log("Verts per Mesh [" + verticesPerMesh + "]"); Debug.Log("Total Mesh Subdivisions [" + nbMeshSubdivisions + "]"); _spherePoints = new List <Vector3>(_nbVertices); _sphereUvs = new List <Vector2>(_nbVertices); _sphereTris = new List <int>(verticesPerMesh); _cells = new Dictionary <Vector2Int, Cell>(); InitTriangles(resolution); FunctionTimer.START_FUNCTION_TIMER("Subdivision_Total_" + resolution); for (var i = 0; i < NbIcosahedronFaces; i++) { SubdivideGeo(IcosahedronVertices[IcosahedronTriangles[i][0]], IcosahedronVertices[IcosahedronTriangles[i][1]], IcosahedronVertices[IcosahedronTriangles[i][2]], IcosahedronTriangleUvs[i][0], IcosahedronTriangleUvs[i][1], IcosahedronTriangleUvs[i][2], _triangles[i], resolution); //i == 0 || isSingleMesh); } InitCells(); FunctionTimer.STOP_FUNCTION_TIMER("Subdivision_Total_" + resolution); // Go through each cell, and adjust the center point to create a flat surface foreach (var cell in _cells.Values) { cell.Flatten(_spherePoints); } foreach (var cell in _cells.Values) { var canvas = Instantiate(_cellPrefab, cell.Normal * 1.01f, Quaternion.identity); canvas.Pos.text = cell.XY.ToString(); canvas.transform.forward = -cell.Normal; } FunctionTimer.START_FUNCTION_TIMER("MeshCreation_Total_" + resolution); for (var i = 0; i < nbMeshSubdivisions; i++) { var startIdx = isSingleMesh ? 0 : i * verticesPerMesh; var mesh = new Mesh { name = isSingleMesh ? "GeodesicSphere_Mesh" : "GeodesicSphere_Face_" + i + "_Mesh", vertices = _spherePoints.GetRange(startIdx, verticesPerMesh).ToArray(), uv = _sphereUvs.GetRange(startIdx, verticesPerMesh).ToArray(), triangles = _sphereTris.ToArray() }; mesh.RecalculateNormals(SmoothingAngle); mesh.RecalculateBounds(); CalculateMeshTangents(mesh); var obj = new GameObject { name = isSingleMesh ? "GeodesicSphere" : "GeodesicSphere_Face" + i }; var objRenderer = obj.AddComponent <MeshRenderer>(); var objFilter = obj.AddComponent <MeshFilter>(); objFilter.mesh = mesh; objRenderer.sharedMaterial = Material; obj.transform.SetParent(container.transform, false); } FunctionTimer.STOP_FUNCTION_TIMER("MeshCreation_Total_" + resolution); }
private void CreateGeodesicSphere(int resolution) { var container = new GameObject { name = "GeodesicSphere_" + resolution + "_Subdivisions" }; container.transform.parent = World; var pos = Vector3.zero; pos.x = 2.5f * (resolution % 4); pos.y = 2.5f - (2 * resolution / 8) * 2.5f; container.transform.position = pos; var isSingleMesh = resolution <= MaxResolutionWithSingleMesh; _nbVertices = NbIcosahedronFaces * 3 * (int)Mathf.Pow(4, resolution); // 20 triangles on an icosahedron, 3 vertices per triangle, each triangle when subdivided yields 4 triangles var verticesPerFace = _nbVertices / NbIcosahedronFaces; var meshSubdivisionDepth = (int)Mathf.Pow(4, Mathf.Max(0, resolution - MaxResolutionBeforeMeshSubdivision)); var nbMeshSubdivisions = isSingleMesh ? 1 : NbIcosahedronFaces * meshSubdivisionDepth; var verticesPerMesh = isSingleMesh ? _nbVertices : verticesPerFace / meshSubdivisionDepth; Debug.Log("Total Verts [" + _nbVertices + "]"); Debug.Log("Verts per Face [" + verticesPerFace + "]"); Debug.Log("Verts per Mesh [" + verticesPerMesh + "]"); Debug.Log("Total Mesh Subdivisions [" + nbMeshSubdivisions + "]"); _spherePoints = new List <Vector3>(_nbVertices); _sphereUvs = new List <Vector2>(_nbVertices); _sphereTris = new List <int>(verticesPerMesh); FunctionTimer.START_FUNCTION_TIMER("Subdivision_Total_" + resolution); for (var i = 0; i < NbIcosahedronFaces; i++) { Subdivide(IsocahedronVertices[IsocahedronTriangles[i][0]], IsocahedronVertices[IsocahedronTriangles[i][1]], IsocahedronVertices[IsocahedronTriangles[i][2]], IsocahedronTriangleUvs[i][0], IsocahedronTriangleUvs[i][1], IsocahedronTriangleUvs[i][2], resolution, i == 0 || isSingleMesh); } FunctionTimer.STOP_FUNCTION_TIMER("Subdivision_Total_" + resolution); FunctionTimer.START_FUNCTION_TIMER("MeshCreation_Total_" + resolution); for (var i = 0; i < nbMeshSubdivisions; i++) { var startIdx = isSingleMesh ? 0 : i * verticesPerMesh; var mesh = new Mesh { name = isSingleMesh ? "GeodesicSphere_Mesh" : "GeodesicSphere_Face_" + i + "_Mesh", vertices = _spherePoints.GetRange(startIdx, verticesPerMesh).ToArray(), uv = _sphereUvs.GetRange(startIdx, verticesPerMesh).ToArray(), triangles = _sphereTris.ToArray() }; mesh.RecalculateNormals(SmoothingAngle); mesh.RecalculateBounds(); CalculateMeshTangents(mesh); mesh.Optimize(); var obj = new GameObject { name = isSingleMesh ? "GeodesicSphere" : "GeodesicSphere_Face" + i }; var objRenderer = obj.AddComponent <MeshRenderer>(); var objFilter = obj.AddComponent <MeshFilter>(); objFilter.mesh = mesh; objRenderer.sharedMaterial = Material; obj.transform.SetParent(container.transform, false); } FunctionTimer.STOP_FUNCTION_TIMER("MeshCreation_Total_" + resolution); }