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

Method ModifyHeightMap

Source/Engine/Terrain/TerrainPatch.cpp:1393–1486  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

1391}
1392
1393bool TerrainPatch::ModifyHeightMap(const float* samples, const Int2& modifiedOffset, const Int2& modifiedSize)
1394{
1395 // Validate input samples range
1396 TerrainDataUpdateInfo info(this);
1397 if (samples == nullptr)
1398 {
1399 LOG(Warning, "Missing heightmap samples data.");
1400 return true;
1401 }
1402 if (modifiedOffset.X < 0 || modifiedOffset.Y < 0 ||
1403 modifiedSize.X <= 0 || modifiedSize.Y <= 0 ||
1404 modifiedOffset.X + modifiedSize.X > info.HeightmapSize ||
1405 modifiedOffset.Y + modifiedSize.Y > info.HeightmapSize)
1406 {
1407 LOG(Warning, "Invalid heightmap samples range.");
1408 return true;
1409 }
1410 PROFILE_CPU_NAMED("Terrain.ModifyHeightMap");
1411 PROFILE_MEM(LevelTerrain);
1412
1413 // Check if has no heightmap
1414 if (Heightmap == nullptr)
1415 {
1416 // Initialize with flat heightmap data
1417 if (InitializeHeightMap())
1418 {
1419 LOG(Error, "Failed to initialize patch heightmap for modification.");
1420 return true;
1421 }
1422 }
1423
1424 // Get the current data to modify it
1425 float* heightMap = GetHeightmapData();
1426 if (samples == heightMap)
1427 {
1428 LOG(Warning, "Updating terrain with its own data. Oh god xD");
1429 }
1430
1431 // Modify heightmap data
1432 {
1433 PROFILE_CPU_NAMED("Terrain.WrtieCache");
1434 for (int32 z = 0; z < modifiedSize.Y; z++)
1435 {
1436 // TODO: use batches row mem copy
1437
1438 for (int32 x = 0; x < modifiedSize.X; x++)
1439 {
1440 heightMap[(z + modifiedOffset.Y) * info.HeightmapSize + (x + modifiedOffset.X)] = samples[z * modifiedSize.X + x];
1441 }
1442 }
1443 }
1444
1445 // Process heightmap to get per-patch height normalization values
1446 float chunkOffsets[Terrain::ChunksCount];
1447 float chunkHeights[Terrain::ChunksCount];
1448 CalculateHeightmapRange(_terrain, info, heightMap, chunkOffsets, chunkHeights);
1449 // TODO: maybe calculate chunk ranges for only modified chunks
1450 const bool wasHeightRangeChanged = Math::NotNearEqual(_yOffset, info.PatchOffset) || Math::NotNearEqual(_yHeight, info.PatchHeight);

Callers

nothing calls this directly

Calls 7

CalculateHeightmapRangeFunction · 0.85
UpdateHeightMapFunction · 0.85
UpdateNormalsAndHolesFunction · 0.85
NotNearEqualFunction · 0.50
GetMethod · 0.45
UpdateTransformMethod · 0.45
UpdateBoundsMethod · 0.45

Tested by

no test coverage detected