public void Update() { if (mapDataReceived) { float viewerDstFromEdge = Mathf.Sqrt(bounds.SqrDistance(_viewerPos)); bool visible = viewerDstFromEdge <= maxViewDst; if (visible) { int lodIndex = 0; for (int i = 0; i < detailLevels.Length - 1; i++) { if (viewerDstFromEdge > detailLevels[i].visibleDstThreshold) { lodIndex++; } else { break; } } if (lodIndex != previousLODIndex) { LODMesh lodMesh = lodMeshes[lodIndex]; if (lodMesh.hasMesh) { meshFilter.mesh = lodMesh.mesh; previousLODIndex = lodIndex; } else if (!lodMesh.hasRequestedMesh) { lodMesh.requestMesh(mapData); } } if (lodIndex == 0) { if (collisionLODMesh.hasMesh) { meshCollider.sharedMesh = collisionLODMesh.mesh; if (onColliderRecived != null) { onColliderRecived(false, this); } } else if (!collisionLODMesh.hasRequestedMesh) { collisionLODMesh.requestMesh(mapData); } } chunksVisibleLastUpdate.Add(this); } setVisible(visible); } }
public void UpdateTerrainChunk() { if (mapDataReceived) { float viewerNearestViewDst = Mathf.Sqrt(bounds.SqrDistance(viewPosition)); bool wasVisible = IsVisible(); bool isVisible = viewerNearestViewDst <= MaxViewDest; if (isVisible) { int indLevel = 0; for (int i = 0; i < detaillevels.Length - 1; i++) { if (viewerNearestViewDst > detaillevels[i].visibleDstThreshold) { indLevel = i + 1; } else { break; } } if (indLevel != previousLODIndex) { LODMesh lodMesh = lodMeshes[indLevel]; if (lodMesh.hasMesh) { previousLODIndex = indLevel; meshFilter.mesh = lodMesh.mesh; //meshCollider.sharedMesh = lodMesh.mesh; } else if (!lodMesh.hasRequestedMesh) { lodMesh.requestMesh(mapData); } } } if (wasVisible != isVisible) { if (isVisible) { terrainBlocksVisibleListUpdate.Add(this); } else { terrainBlocksVisibleListUpdate.Remove(this); } SetVisible(isVisible); } } }
public void updateTerrainChunk() { if (heightMapReceived) { float viewerDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition)); bool wasVisible = isVisible(); bool visible = viewerDstFromNearestEdge <= maxViewDst; if (visible) { int lodIndex = 0; for (int i = 0; i < detailLevels.Length - 1; i++) { if (viewerDstFromNearestEdge > detailLevels[i].visibleDstThreshold) { lodIndex = i + 1; } else { break; } } if (lodIndex != previousLODIndex) { LODMesh lodMesh = lodMeshes[lodIndex]; if (lodMesh.hasMesh) { previousLODIndex = lodIndex; meshFilter.mesh = lodMesh.mesh; } else if (!lodMesh.hasRequestedMesh) { lodMesh.requestMesh(heightmap, meshSettings, heightMapSettings); } } } if (wasVisible != visible) { setVisible(visible); if (onVisibilityChanged != null) { onVisibilityChanged(this, visible); } } } }
public void UpdateTerrainChunkVisibility() { if (mapDataReceived) { float viewerDstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(viewerPosition)); bool visible = viewerDstFromNearestEdge <= maxViewDistance; if (visible) { int lodIndex = 0; for (int i = 0; i < detailLevels.Length - 1; i++) { if (viewerDstFromNearestEdge >= detailLevels [i].visibleDistThreshold) { lodIndex = i + 1; } else { break; } } if (lodIndex != previousLODIndex) { LODMesh lodMesh = lodMeshes [lodIndex]; if (lodMesh.hasMesh) { previousLODIndex = lodIndex; meshFilter.mesh = lodMesh.mesh; } else if (!lodMesh.hasRequestedMesh) { lodMesh.requestMesh(mapData); } } terrainChunksVisibleLastUpdate.Add(this); } SetVisible(visible); } }
public void UpdateTerainChunk() { if (recievedMapdata) { float dstFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(playerPos)); bool visible = dstFromNearestEdge <= maxViewDist; if (visible) { int LODIndex = 0; for (int i = 0; i < detailLevels.Length - 1; i++) { if (dstFromNearestEdge > detailLevels[i].distFromPlayer) { LODIndex = i + 1; } else { break; } } if (LODIndex != prevLODIndex) { LODMesh lodMesh = lodMeshes[LODIndex]; if (lodMesh.hasMesh) { mFilter.mesh = lodMesh.mesh; prevLODIndex = LODIndex; mCollider.sharedMesh = lodMesh.mesh; } else if (lodMesh.hasReq == false) { lodMesh.requestMesh(mapData); } } prevChunks.Add(this); } setVisible(visible); } }
/// <summary> /// This function handles the switching of lod for the mesh of the chunk as the player moves around /// </summary> public void updateTerrainChunk() { //We can't update the terrain if we don't have any mapData to work off if (!mapDataReceived) { return; } //Get the distance of the player from the bounding box of the chunk float viewerDistanceFromNearestEdge = Mathf.Sqrt(bounds.SqrDistance(playerPosition)); //The chunk is visible if the chunk is not too far away from the player bool visible = viewerDistanceFromNearestEdge <= maxViewDistance; //Change the visibility of the chunk based off of whether it is in range setVisible(visible); //If the chunk is in range of the player, then we need to figure out what it's mesh looks like if (visible) { //Start out assuming that we are at the highest detail level, then iterate over all subsequent detail levels, until we find the correct distance that reflects our lod int lodIndex = 0; for (int i = 0; i < detailLevels.Length - 1; i++) { //We use the lod for which our distance is less than the given distance for the lod if (viewerDistanceFromNearestEdge > detailLevels[i].visibleDistanceThreshold) { //So if we are too far away we increment our lod lodIndex = i + 1; } else { //otherwise we are in range and we can stop iterating over the lods break; } } //If our lod index has changed since the player moved last if (lodIndex != previousLODIndex) { //Get the lod for this index LODMesh lodMesh = lodMeshes[lodIndex]; //If there is a mesh for this given lod: if (lodMesh.hasReceivedMesh) { //Reassign our current index of lod previousLODIndex = lodIndex; //Assign the mesh from the lodMesh meshFilter.mesh = lodMesh.mesh; //Otherwise if the mesh hasn't received a mesh it could just be aiting for it to be generated in another thread, however if the mesh generation hasn't even //been requested, then we get it to request a mesh } else if (!lodMesh.hasRequestedMesh) { lodMesh.requestMesh(mapData, mapGenerator); } } //If we are at the highest lod if (lodIndex == 0) { //If the mesh collider has been generated, we add a collider to the mesh if (collisionLODMesh.hasReceivedMesh) { meshCollider.sharedMesh = collisionLODMesh.mesh; //Otherwise we get the collider to generate it's mesh } else if (!collisionLODMesh.hasRequestedMesh) { collisionLODMesh.requestMesh(mapData, mapGenerator); } } //Add this terrain chunk to the list of all chunks that were visible last update terrainChunksVisibleLastUpdate.Add(this); } }
public void updateChunk() { if (landDataReceived) { float viewerDist = Mathf.Sqrt(bounds.SqrDistance(viewerPosition)); bool visible = viewerDist <= maxViewDist; if (visible) { int lodIndex = 0; for (int i = 0; i < detailLevels.Length - 1; i++) { if (viewerDist > detailLevels[i].visibleDistThreshold) { lodIndex = i + 1; } else { break; } } if (lodIndex != prevLOD) { LODMesh lodMesh = lodMeshes[lodIndex]; if (lodMesh.hasMesh) { prevLOD = lodIndex; filter.mesh = lodMesh.mesh; if (lodIndex == 0) { col.sharedMesh = colLODMesh.mesh; } } else if (!lodMesh.hasRequestedMesh) { lodMesh.requestMesh(landData); if (lodIndex == 0) { colLODMesh.requestMesh(landData); } } } /*Actual video * * if(lodIndex == 0) { * if(colLODMesh.hasMesh) { * col.sharedMesh = colLODMesh.mesh; * } else if(!colLODMesh.hasRequestedMesh) { * colLODMesh.requestMesh(landData); * } * } * */ } setVisible(visible); } }