public void CollectMeshInfo(int maxLodLevel) { lodMeshInfos = new LodMeshInfo[maxLodLevel + 1]; for (int lod = 0; lod <= maxLodLevel; lod++) { lodMeshInfos[lod] = new LodMeshInfo(); if (layerType == LayerType.SingleMesh_Max4Layer) { lodMeshInfos[lod].layerIndices = new int[1]; lodMeshInfos[lod].layerIndices[0] = 0; } else { lodMeshInfos[lod].layerIndices = new int[trees[0].validNums.Length]; int maxIndex = trees[0].GetMaxValidNumIndex(); for (int i = 0; i < lodMeshInfos[lod].layerIndices.Length; i++) { lodMeshInfos[lod].layerIndices[i == maxIndex ? 0 : (i < maxIndex ? i + 1 : i)] = i; } } lodMeshInfos[lod].intVertices = TerrainToMeshTool.GetIntVertices(trees[lod]); lodMeshInfos[lod].vecNormals = TerrainToMeshTool.GetNormals(lodMeshInfos[lod].intVertices, terrainData); lodMeshInfos[lod].indices = TerrainToMeshTool.GetIndices(trees[lod], lodMeshInfos[lod].layerIndices); } }
//创建某个级别的LOD的网格 public List <Renderer> CreateRenderersForOneLodLevel(int lod) { List <Renderer> lodRenderers = new List <Renderer>(10); if (lod >= 0 && lod < lodMeshInfos.Length) { lodRenderers.Clear(); if (lod < 2) { bool[] layerValid = new bool[lodMeshInfos[lod].layerIndices.Length]; if (layerType == LayerType.SubMesh) { for (int i = 0; i < lodMeshInfos[lod].layerIndices.Length; i++) { if (lodMeshInfos[lod].indices[lodMeshInfos[lod].layerIndices[i]].Length > 0) { layerValid[i] = true; } } Mesh mesh = TerrainToMeshTool.CreateMesh(terrainData, heights, lodMeshInfos[lod].intVertices, lodMeshInfos[lod].vecNormals, lodMeshInfos[lod].indices, -1); MeshRenderer renderer = CreateMeshRenderer_SubMesh(mesh, layerValid, lodMeshInfos[lod].layerIndices, lod); if (renderer) { lodRenderers.Add(renderer); } } else if (layerType == LayerType.SingleMesh_Max4Layer) { Mesh mesh = TerrainToMeshTool.CreateMesh(terrainData, heights, lodMeshInfos[lod].intVertices, lodMeshInfos[lod].vecNormals, lodMeshInfos[lod].indices, 0); MeshRenderer renderer = CreateMeshRenderer_SingleMesh(mesh, lod); if (renderer) { lodRenderers.Add(renderer); } } else if (layerType == LayerType.SingleMesh_LayerIndexTexture) { Mesh mesh = TerrainToMeshTool.CreateMesh(terrainData, heights, lodMeshInfos[lod].intVertices, lodMeshInfos[lod].vecNormals, lodMeshInfos[lod].indices, 0); MeshRenderer renderer = CreateMeshRenderer_SingleMesh_TextureIndex(mesh, lod); if (renderer) { lodRenderers.Add(renderer); } } else { for (int i = 0; i < lodMeshInfos[lod].layerIndices.Length; i++) { Mesh mesh = TerrainToMeshTool.CreateMesh(terrainData, heights, lodMeshInfos[lod].intVertices, lodMeshInfos[lod].vecNormals, lodMeshInfos[lod].indices, i); MeshRenderer renderer = CreateMeshRenderer_OneOfMultiMesh(mesh, lodMeshInfos[lod].layerIndices[i], i == 0, lod); if (renderer) { lodRenderers.Add(renderer); } } } } else { List <TerrainToMeshTool.VecInt3> intVertices = TerrainToMeshTool.GetIntVertices(trees[lod]); List <TerrainToMeshTool.VecNormal> vecNormals = TerrainToMeshTool.GetNormals(intVertices, terrainData); List <int[]> indices = TerrainToMeshTool.GetIndices(trees[lod], new int[] { 0 }); Mesh mesh = TerrainToMeshTool.CreateMesh(terrainData, heights, intVertices, vecNormals, indices, 0); GameObject obj = new GameObject("base_lod_" + lod); MeshRenderer renderer = obj.AddComponent <MeshRenderer>(); MeshFilter filter = obj.AddComponent <MeshFilter>(); renderer.sharedMaterial = matBase; filter.sharedMesh = mesh; obj.transform.SetParent(transform, false); lodRenderers.Add(renderer); } } return(lodRenderers); }