| 35 | } |
| 36 | |
| 37 | bool TerrainChunk::PrepareDraw(const RenderContext& renderContext) |
| 38 | { |
| 39 | // Calculate LOD |
| 40 | int32 lod; |
| 41 | const int32 forcedLod = _patch->_terrain->_forcedLod; |
| 42 | const int32 lodCount = _patch->Heightmap.Get()->StreamingTexture()->TotalMipLevels(); |
| 43 | const int32 minStreamedLod = lodCount - _patch->Heightmap.Get()->GetTexture()->ResidentMipLevels(); |
| 44 | if (forcedLod >= 0) |
| 45 | { |
| 46 | lod = forcedLod; |
| 47 | } |
| 48 | else |
| 49 | { |
| 50 | const int32 lodBias = _patch->_terrain->_lodBias; |
| 51 | const float lodDistribution = _patch->_terrain->_lodDistribution; |
| 52 | const float chunkEdgeSize = (_patch->_terrain->_chunkSize * TERRAIN_UNITS_PER_VERTEX); |
| 53 | |
| 54 | // Calculate chunk distance to view |
| 55 | const auto lodView = (renderContext.LodProxyView ? renderContext.LodProxyView : &renderContext.View); |
| 56 | const float distance = Float3::Distance(_sphere.Center - lodView->Origin, lodView->Position); |
| 57 | lod = (int32)Math::Pow(distance / chunkEdgeSize, lodDistribution); |
| 58 | lod += lodBias; |
| 59 | |
| 60 | //lod = 0; |
| 61 | //lod = 10; |
| 62 | //lod = (_x + _z + Terrain::ChunksCountEdge * (_patch->_x + _patch->_z)); |
| 63 | //lod = (int32)Vector2::Distance(Vector2(2, 2), Vector2(_patch->_x, _patch->_z) * Terrain::ChunksCountEdge + Vector2(_x, _z)); |
| 64 | //lod = (int32)(Vector3::Distance(_bounds.GetCenter(), view.Position) / 10000.0f); |
| 65 | } |
| 66 | lod = Math::Clamp(lod, minStreamedLod, lodCount - 1); |
| 67 | |
| 68 | // Pick a material |
| 69 | auto material = OverrideMaterial.Get(); |
| 70 | if (!material || !material->IsLoaded()) |
| 71 | { |
| 72 | material = _patch->_terrain->Material.Get(); |
| 73 | if (!material || !material->IsLoaded()) |
| 74 | material = TerrainManager::GetDefaultTerrainMaterial(); |
| 75 | } |
| 76 | if (!material || !material->IsReady() || !material->IsTerrain()) |
| 77 | return false; |
| 78 | |
| 79 | // Cache data |
| 80 | _cachedDrawLOD = lod; |
| 81 | _cachedDrawMaterial = material; |
| 82 | return true; |
| 83 | } |
| 84 | |
| 85 | void TerrainChunk::Draw(const RenderContext& renderContext) const |
| 86 | { |
no test coverage detected