Skip to content

Commit

Permalink
Merge pull request #167 from Algoryx/feature/pager-tiling-helpers
Browse files Browse the repository at this point in the history
Add helper functions related to the pager's tiling
  • Loading branch information
FilipAlg authored Oct 1, 2024
2 parents 7832000 + 56de501 commit f5a3449
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 3 deletions.
7 changes: 5 additions & 2 deletions AGXUnity/Model/DeformableTerrainConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,16 @@ public Vector3 GetOffsetPosition()
return transform.position;
}

public float[,] WriteTerrainDataOffset()
public float[,] WriteTerrainDataOffset(bool needsReturnData = true)
{
var resolution = TerrainUtils.TerrainDataResolution(Terrain.terrainData);
if ( InitialHeights != null )
return needsReturnData ? Terrain.terrainData.GetHeights( 0, 0, resolution, resolution ) : null;

if ( float.IsNaN( MaximumDepth ) ) {
Debug.LogError( "Writing terrain offset without first setting depth!" );
MaximumDepth = 0;
}
var resolution = TerrainUtils.TerrainDataResolution(Terrain.terrainData);
InitialHeights = Terrain.terrainData.GetHeights( 0, 0, resolution, resolution );
transform.position += MaximumDepth * Vector3.down;
return TerrainUtils.WriteTerrainDataOffsetRaw( Terrain, MaximumDepth );
Expand Down
41 changes: 40 additions & 1 deletion AGXUnity/Model/DeformableTerrainPager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -407,6 +407,7 @@ private void UpdateHeights()
UpdateTerrain( tiles[ i ] );
foreach ( var terr in m_updatedTerrains )
terr.terrainData.SyncHeightmap();
m_updatedTerrains.Clear();
}

private void UpdateTerrain( agxTerrain.TerrainPager.TileAttachments tile )
Expand Down Expand Up @@ -465,12 +466,50 @@ private Vector2Int GetGlobalIndexInternal( Vector2Int tileIndex, agx.Vec2i index
return tileIndex;
}

private Vector2Int GetGlobalIndex( agxTerrain.Terrain terrain, agx.Vec2i index )
/// <summary>
/// Converts a tile local index to a global index used to index specific cells over the entirety of the pager.
/// </summary>
/// <param name="terrain">The local agx terrain that the index is relative to</param>
/// <param name="index">The local index of the cell</param>
/// <returns>A global index referencing the specific local cell</returns>
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public Vector2Int GetGlobalIndex( agxTerrain.Terrain terrain, agx.Vec2i index )
{
var tileIndex = GetTileIndex( terrain );
return GetGlobalIndexInternal( tileIndex, index );
}

/// <summary>
/// Converts a given global cell index to the corresponding index of the Unity terrain containing that cell.
/// </summary>
/// <param name="globalIndex">The global cell index to convert</param>
/// <returns>The Unity terrain tile index</returns>
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public Vector2Int GetUnityTerrainIndex( Vector2Int globalIndex ) => m_terrainDataSource.GlobalToUnityIndex( globalIndex );
/// <summary>
/// Converts a given local cell index to the corresponding index of the Unity terrain containing that cell.
/// </summary>
/// <param name="terrain">The local agx terrain that the index is relative to</param>
/// <param name="index">The local index of the cell</param>
/// <returns>The Unity terrain tile index</returns>
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public Vector2Int GetUnityTerrainIndex( agxTerrain.Terrain terrain, agx.Vec2i index ) => m_terrainDataSource.GlobalToUnityIndex( GetGlobalIndex( terrain, index ) );
/// <summary>
/// Gets the Unity terrain at the specified Unity tile index.
/// </summary>
/// <param name="terrainIndex">The Unity terrain tile index</param>
/// <returns>The Unity terrain at the specified tile index</returns>
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public Terrain GetUnityTerrain( Vector2Int terrainIndex ) => m_terrainDataSource.GetTerrainAtTerrainIndex( terrainIndex );

/// <summary>
/// Whether or not the pager has fetched data from the terrain at the given index.
/// </summary>
/// <param name="terrainIndex">The terrain index to check</param>
/// <returns>True if data has been fetched for the specified index, false otherwise.</returns>
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public bool IsDataFetchedFromTerrain( Vector2Int terrainIndex ) => m_terrainDataSource.IsDataFetchedFromTerrain( terrainIndex );

public void RecalculateParameters()
{
if ( ValidateParameters() )
Expand Down
18 changes: 18 additions & 0 deletions AGXUnity/Model/UnityTerrainAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using UnityEngine;

namespace AGXUnity.Model
Expand Down Expand Up @@ -225,12 +226,29 @@ public override agx.RealVector fetchTerrainTile( agxTerrain.TileSpecification ts
/// </summary>
/// <param name="globalIndex">The global index to convert to a unity tile index</param>
/// <returns>The unity tile index for the given global index</returns>
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public Vector2Int GlobalToUnityIndex( Vector2Int globalIndex )
{
return new Vector2Int( (int)Mathf.Floor( (float)globalIndex.x / ( m_tileResolution - 1 ) ),
(int)Mathf.Floor( (float)globalIndex.y / ( m_tileResolution - 1 ) ) );
}

/// <summary>
/// Returns the connected Unity terrain at the specified Unity terrain tile index
/// </summary>
/// <param name="terrainIndex">The Unity terrain tile index to fetch the terrain for.</param>
/// <returns>The Unity terrain at the specified tile index</returns>
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public Terrain GetTerrainAtTerrainIndex( Vector2Int terrainIndex ) => m_unityTiles.GetValueOrDefault(terrainIndex,null);

/// <summary>
/// Whether or not the pager has fetched data from the terrain at the given index.
/// </summary>
/// <param name="terrainIndex">The terrain index to check</param>
/// <returns>True if data has been fetched for the specified index, false otherwise.</returns>
[MethodImpl( MethodImplOptions.AggressiveInlining )]
public bool IsDataFetchedFromTerrain( Vector2Int terrainIndex ) => m_unityData.ContainsKey(terrainIndex);

// Checks if unity tile data is loaded for the tile at the given index and queues the tile to be loaded if it is not
private bool VerifyAndQueueTileData( Vector2Int unityIndex )
{
Expand Down

0 comments on commit f5a3449

Please sign in to comment.