public void UpdateTerrainSpawn() { if (terrianDataReceived) { float terrianToViewer = Mathf.Sqrt(bounds.SqrDistance(viewerPosition)); bool visible = terrianToViewer <= maxVisibleDistance; if (visible) { int detailLevelIndex = 0; for (int i = 0; i < detailLevels.Length - 1; i++) { if (terrianToViewer > detailLevels[i].visibleDstThreshold) { detailLevelIndex = i + 1; } else { break; } } if (detailLevelIndex != previousDetailLevelIndex) { MeshDetailLevel meshDetailLevelObj = meshDetailLevel[detailLevelIndex]; if (meshDetailLevelObj.hasMesh) { previousDetailLevelIndex = detailLevelIndex; meshFilter.mesh = meshDetailLevelObj.mesh; meshCollider.sharedMesh = meshDetailLevelObj.mesh; } else if (!meshDetailLevelObj.hasRequestedMesh) { meshDetailLevelObj.RequestMesh(terrianData); GenerateModels(terrianData, meshDetailLevelObj.mesh); } GenerateModels(terrianData, meshDetailLevelObj.mesh); } terrainChunksVisibleLastUpdate.Add(this); } SetVisible(visible); } }
public SpawnTerrian(Vector2 terrianCoord, int terrianSize, LODInfo[] detailLevels, Transform parent, Material material) { this.models = FindObjectOfType <Models>(); this.detailLevels = detailLevels; terrianPosition = terrianCoord * terrianSize; bounds = new Bounds(terrianPosition, Vector2.one * terrianSize); Vector3 worldPosition = new Vector3(terrianPosition.x, 0, terrianPosition.y); meshObject = new GameObject("Terrain"); meshRenderer = meshObject.AddComponent <MeshRenderer>(); meshFilter = meshObject.AddComponent <MeshFilter>(); meshCollider = meshObject.AddComponent <MeshCollider>(); meshRenderer.material = material; meshObject.transform.position = worldPosition; meshObject.transform.parent = parent; meshObject.transform.localScale = Vector3.one; SetVisible(false); meshDetailLevel = new MeshDetailLevel[detailLevels.Length]; for (int i = 0; i < detailLevels.Length; i++) { meshDetailLevel[i] = new MeshDetailLevel(detailLevels[i].lod, UpdateTerrainSpawn, terrianGenerator); } terrianGenerator.RequestTerrianData(terrianPosition, OnTerrianDataReceived); }