MCPcopy Create free account
hub / github.com/FlaxEngine/FlaxEngine / PrepareDraw

Method PrepareDraw

Source/Engine/Terrain/TerrainChunk.cpp:37–83  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

35}
36
37bool 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
85void TerrainChunk::Draw(const RenderContext& renderContext) const
86{

Callers 1

DrawImplMethod · 0.80

Calls 8

DistanceFunction · 0.85
ClampFunction · 0.85
StreamingTextureMethod · 0.80
GetTextureMethod · 0.80
PowFunction · 0.50
GetMethod · 0.45
IsLoadedMethod · 0.45
IsReadyMethod · 0.45

Tested by

no test coverage detected