private void SetTriangles(RTBranchContainer branch, int vertCount, int initIndex, int branchIndex) { int initRound = 0; int endRound = Mathf.Min(branch.branchPoints.Count - 2, ((branch.branchPoints.Count - initIndex) - 2)); for (int round = initRound; round < endRound; round++) { for (int i = 0; i < ivyParameters.sides; i++) { int offset = vertCount - lastVertCount; int v0 = (i + (round * (ivyParameters.sides + 1))) + offset; int v1 = (i + (round * (ivyParameters.sides + 1)) + 1) + offset; int v2 = (i + (round * (ivyParameters.sides + 1)) + ivyParameters.sides + 1) + offset; int v3 = (i + (round * (ivyParameters.sides + 1)) + 1) + offset; int v4 = (i + (round * (ivyParameters.sides + 1)) + ivyParameters.sides + 2) + offset; int v5 = (i + (round * (ivyParameters.sides + 1)) + ivyParameters.sides + 1) + offset; //int firstIndexTriangle = lastTriangleIndexPerBranch[branchIndex]; buildingMeshData.AddTriangle(0, v0); buildingMeshData.AddTriangle(0, v1); buildingMeshData.AddTriangle(0, v2); buildingMeshData.AddTriangle(0, v3); buildingMeshData.AddTriangle(0, v4); buildingMeshData.AddTriangle(0, v5); triCount += 6; } } for (int t = 0, c = 0; t < ivyParameters.sides * 3; t += 3, c++) { buildingMeshData.AddTriangle(0, vertCount - 1); buildingMeshData.AddTriangle(0, vertCount - 3 - c); buildingMeshData.AddTriangle(0, vertCount - 2 - c); triCount += 3; } lastTriangleIndexPerBranch[branchIndex] = vertCount - 1; }
public void CopyToFixedMesh(int branchIndex, int initSegmentIdx, int endSegmentIdx, RTBranchContainer branchContainer, RTBranchContainer bakedBranchContainer) { int numVerticesPerLoop = ivyParameters.sides + 1; int numTrianglesPerLoop = ivyParameters.sides * 6; int numLoopsToProcess = 1; int onlyBranchVertices = (vertCountsPerBranch[branchIndex] - vertCountLeavesPerBranch[branchIndex]); int vertexOffset = 0; for (int i = 1; i <= branchIndex; i++) { vertexOffset += vertCountsPerBranch[branchIndex]; } if (processedBranchesVerticesIndicesPerBranch[branchIndex].Count <= 0) { numLoopsToProcess = 2; } else { numLoopsToProcess = 1; vertexOffset += numVerticesPerLoop; } for (int i = numLoopsToProcess - 1; i >= 0; i--) { int index = branchContainer.branchPoints.Count - backtrackingPoints - i; RTBranchPoint rtBranchPoint = branchContainer.branchPoints[index]; for (int j = 0; j < rtBranchPoint.verticesLoop.Length; j++) { RTVertexData vertexData = rtBranchPoint.verticesLoop[j]; processedMeshData.AddVertex(vertexData.vertex, vertexData.normal, vertexData.uv, vertexData.color); processedBranchesVerticesIndicesPerBranch[branchIndex].Add(processedMeshData.VertexCount() - 1); } } int triangleIndexOffset = 0; if (branchIndex > 0) { triangleIndexOffset = lastTriangleIndexPerBranch[branchIndex]; } if (processedBranchesVerticesIndicesPerBranch[branchIndex].Count >= numVerticesPerLoop * 2) { int initIdx = processedBranchesVerticesIndicesPerBranch[branchIndex].Count - (numVerticesPerLoop * 2); for (int i = 0; i < ivyParameters.sides; i++) { int v0 = processedBranchesVerticesIndicesPerBranch[branchIndex][i + initIdx]; int v1 = processedBranchesVerticesIndicesPerBranch[branchIndex][i + 1 + initIdx]; int v2 = processedBranchesVerticesIndicesPerBranch[branchIndex][i + ivyParameters.sides + 1 + initIdx]; int v3 = processedBranchesVerticesIndicesPerBranch[branchIndex][i + 1 + initIdx]; int v4 = processedBranchesVerticesIndicesPerBranch[branchIndex][i + ivyParameters.sides + 2 + initIdx]; int v5 = processedBranchesVerticesIndicesPerBranch[branchIndex][i + ivyParameters.sides + 1 + initIdx]; processedMeshData.AddTriangle(0, v0); processedMeshData.AddTriangle(0, v1); processedMeshData.AddTriangle(0, v2); processedMeshData.AddTriangle(0, v3); processedMeshData.AddTriangle(0, v4); processedMeshData.AddTriangle(0, v5); } } if (ivyParameters.generateLeaves) { int lastVertexLeafProcessed = processedMeshData.VertexCount(); int numLeavesProcessed = 0; for (int i = initSegmentIdx; i < endSegmentIdx; i++) { RTLeafPoint[] leaves = bakedBranchContainer.leavesOrderedByInitSegment[i]; for (int j = 0; j < leaves.Length; j++) { RTLeafPoint currentLeaf = leaves[j]; if (currentLeaf == null) { continue; } RTMeshData chosenLeaveMeshData = leavesMeshesByChosenLeaf[currentLeaf.chosenLeave]; int submesh = submeshByChoseLeaf[currentLeaf.chosenLeave]; for (int t = 0; t < chosenLeaveMeshData.triangles[0].Length; t++) { int triangleValue = chosenLeaveMeshData.triangles[0][t] + lastVertexLeafProcessed; processedMeshData.AddTriangle(submesh, triangleValue); } for (int v = 0; v < currentLeaf.vertices.Length; v++) { RTVertexData vertexData = currentLeaf.vertices[v]; processedMeshData.AddVertex(vertexData.vertex, vertexData.normal, vertexData.uv, vertexData.color); processedVerticesIndicesPerBranch[branchIndex].Add(processedMeshData.VertexCount() - 1); lastVertexLeafProcessed++; } numLeavesProcessed++; } } } }