Skip to content

Commit

Permalink
WriteTerrainDataOffset now only writes to terrain data once
Browse files Browse the repository at this point in the history
  • Loading branch information
FilipAlg committed Dec 5, 2023
1 parent f1c1803 commit da19121
Showing 1 changed file with 11 additions and 22 deletions.
33 changes: 11 additions & 22 deletions AGXUnity/Utils/TerrainUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,27 +83,28 @@ public static NativeHeights WriteTerrainDataOffset( Terrain terrain, float offse
{
var terrainData = terrain.terrainData;
var nativeHeightData = FindHeights( terrainData );
var tmp = new float[,] { { 0.0f } };
var dataMaxHeight = terrainData.size.y;
var maxClampedHeight = -1.0f;
var resolution = TerrainDataResolution( terrainData );
var resolutionX = nativeHeightData.ResolutionX;
var resolutionY = nativeHeightData.ResolutionY;
var scale = terrainData.heightmapScale.y;

var modHeights = new float[resolutionY, resolutionX];
for ( int i = 0; i < nativeHeightData.Heights.Count; ++i ) {

var newHeight = nativeHeightData.Heights[ i ] += offset;

var vertexX = i % nativeHeightData.ResolutionX;
var vertexY = i / nativeHeightData.ResolutionY;
var vertexX = i % resolutionX;
var vertexY = i / resolutionY;

modHeights[ resolutionY - vertexY - 1, resolutionX - vertexX - 1 ] = (float)newHeight / scale;

tmp[ 0, 0 ] = (float)newHeight / scale;
if ( newHeight > dataMaxHeight )
maxClampedHeight = System.Math.Max( maxClampedHeight, (float)newHeight );

terrainData.SetHeightsDelayLOD( resolution - vertexX - 1,
resolution - vertexY - 1,
tmp );
}

terrainData.SetHeights( 0, 0, modHeights );

if ( maxClampedHeight > 0.0f ) {
Debug.LogWarning( "Terrain heights were clamped: UnityEngine.TerrainData max height = " +
dataMaxHeight +
Expand All @@ -112,12 +113,6 @@ public static NativeHeights WriteTerrainDataOffset( Terrain terrain, float offse
". Resolve this by increasing max height and lower the terrain or decrease Maximum Depth.", terrain );
}

#if UNITY_2019_1_OR_NEWER
terrainData.SyncHeightmap();
#else
terrain.ApplyDelayedHeightmapModification();
#endif

return nativeHeightData;
}

Expand All @@ -143,13 +138,7 @@ public static NativeHeights WriteTerrainDataOffset( Terrain terrain, float offse
}
}

terrainData.SetHeightsDelayLOD( 0, 0, data );

#if UNITY_2019_1_OR_NEWER
terrainData.SyncHeightmap();
#else
terrain.ApplyDelayedHeightmapModification();
#endif
terrainData.SetHeights( 0, 0, data );

if ( maxClampedHeight > 0.0f ) {
Debug.LogWarning( "Terrain heights were clamped: UnityEngine.TerrainData max height = " +
Expand Down

0 comments on commit da19121

Please sign in to comment.