From 679e31eaabb76ef914470c15dac562eae72c890c Mon Sep 17 00:00:00 2001 From: Filip Henningsson Date: Tue, 24 Sep 2024 15:22:14 +0200 Subject: [PATCH 1/2] Exposed helper methods for interacting with the pager tiling --- AGXUnity/Model/DeformableTerrainConnector.cs | 7 +++-- AGXUnity/Model/DeformableTerrainPager.cs | 33 +++++++++++++++++++- AGXUnity/Model/UnityTerrainAdapter.cs | 10 ++++++ 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/AGXUnity/Model/DeformableTerrainConnector.cs b/AGXUnity/Model/DeformableTerrainConnector.cs index e3b3a984..77c5c67a 100644 --- a/AGXUnity/Model/DeformableTerrainConnector.cs +++ b/AGXUnity/Model/DeformableTerrainConnector.cs @@ -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 ); diff --git a/AGXUnity/Model/DeformableTerrainPager.cs b/AGXUnity/Model/DeformableTerrainPager.cs index 4c5d0324..58f0aad7 100644 --- a/AGXUnity/Model/DeformableTerrainPager.cs +++ b/AGXUnity/Model/DeformableTerrainPager.cs @@ -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 ) @@ -465,12 +466,42 @@ private Vector2Int GetGlobalIndexInternal( Vector2Int tileIndex, agx.Vec2i index return tileIndex; } - private Vector2Int GetGlobalIndex( agxTerrain.Terrain terrain, agx.Vec2i index ) + /// + /// Converts a tile local index to a global index used to index specific cells over the entirety of the pager. + /// + /// The local agx terrain that the index is relative to + /// The local index of the cell + /// A global index referencing the specific local cell + [MethodImpl( MethodImplOptions.AggressiveInlining )] + public Vector2Int GetGlobalIndex( agxTerrain.Terrain terrain, agx.Vec2i index ) { var tileIndex = GetTileIndex( terrain ); return GetGlobalIndexInternal( tileIndex, index ); } + /// + /// Converts a given global cell index to the corresponding index of the Unity terrain containing that cell. + /// + /// The global cell index to convert + /// The Unity terrain tile index + [MethodImpl( MethodImplOptions.AggressiveInlining )] + public Vector2Int GetUnityTerrainIndex( Vector2Int globalIndex ) => m_terrainDataSource.GlobalToUnityIndex( globalIndex ); + /// + /// Converts a given local cell index to the corresponding index of the Unity terrain containing that cell. + /// + /// The local agx terrain that the index is relative to + /// The local index of the cell + /// The Unity terrain tile index + [MethodImpl( MethodImplOptions.AggressiveInlining )] + public Vector2Int GetUnityTerrainIndex( agxTerrain.Terrain terrain, agx.Vec2i index ) => m_terrainDataSource.GlobalToUnityIndex( GetGlobalIndex( terrain, index ) ); + /// + /// Gets the Unity terrain at the specified Unity tile index. + /// + /// The Unity terrain tile index + /// The Unity terrain at the specified tile index + [MethodImpl( MethodImplOptions.AggressiveInlining )] + public Terrain GetUnityTerrain( Vector2Int terrainIndex ) => m_terrainDataSource.GetTerrainAtTerrainIndex( terrainIndex ); + public void RecalculateParameters() { if ( ValidateParameters() ) diff --git a/AGXUnity/Model/UnityTerrainAdapter.cs b/AGXUnity/Model/UnityTerrainAdapter.cs index d3377e2b..4eeb5032 100644 --- a/AGXUnity/Model/UnityTerrainAdapter.cs +++ b/AGXUnity/Model/UnityTerrainAdapter.cs @@ -2,6 +2,7 @@ using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using UnityEngine; namespace AGXUnity.Model @@ -225,12 +226,21 @@ public override agx.RealVector fetchTerrainTile( agxTerrain.TileSpecification ts /// /// The global index to convert to a unity tile index /// The unity tile index for the given global index + [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 ) ) ); } + /// + /// Returns the connected Unity terrain at the specified Unity terrain tile index + /// + /// The Unity terrain tile index to fetch the terrain for. + /// The Unity terrain at the specified tile index + [MethodImpl( MethodImplOptions.AggressiveInlining )] + public Terrain GetTerrainAtTerrainIndex( Vector2Int terrainIndex ) => m_unityTiles.GetValueOrDefault(terrainIndex,null); + // 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 ) { From 56de5018096876e56d5f9f405852c6657eb96d1d Mon Sep 17 00:00:00 2001 From: Filip Henningsson Date: Mon, 30 Sep 2024 14:53:11 +0200 Subject: [PATCH 2/2] Added method to query whether pager has fetched data from a given unity terrain --- AGXUnity/Model/DeformableTerrainPager.cs | 8 ++++++++ AGXUnity/Model/UnityTerrainAdapter.cs | 8 ++++++++ 2 files changed, 16 insertions(+) diff --git a/AGXUnity/Model/DeformableTerrainPager.cs b/AGXUnity/Model/DeformableTerrainPager.cs index 58f0aad7..bbd656b6 100644 --- a/AGXUnity/Model/DeformableTerrainPager.cs +++ b/AGXUnity/Model/DeformableTerrainPager.cs @@ -502,6 +502,14 @@ public Vector2Int GetGlobalIndex( agxTerrain.Terrain terrain, agx.Vec2i index ) [MethodImpl( MethodImplOptions.AggressiveInlining )] public Terrain GetUnityTerrain( Vector2Int terrainIndex ) => m_terrainDataSource.GetTerrainAtTerrainIndex( terrainIndex ); + /// + /// Whether or not the pager has fetched data from the terrain at the given index. + /// + /// The terrain index to check + /// True if data has been fetched for the specified index, false otherwise. + [MethodImpl( MethodImplOptions.AggressiveInlining )] + public bool IsDataFetchedFromTerrain( Vector2Int terrainIndex ) => m_terrainDataSource.IsDataFetchedFromTerrain( terrainIndex ); + public void RecalculateParameters() { if ( ValidateParameters() ) diff --git a/AGXUnity/Model/UnityTerrainAdapter.cs b/AGXUnity/Model/UnityTerrainAdapter.cs index 4eeb5032..d0ffd480 100644 --- a/AGXUnity/Model/UnityTerrainAdapter.cs +++ b/AGXUnity/Model/UnityTerrainAdapter.cs @@ -241,6 +241,14 @@ public Vector2Int GlobalToUnityIndex( Vector2Int globalIndex ) [MethodImpl( MethodImplOptions.AggressiveInlining )] public Terrain GetTerrainAtTerrainIndex( Vector2Int terrainIndex ) => m_unityTiles.GetValueOrDefault(terrainIndex,null); + /// + /// Whether or not the pager has fetched data from the terrain at the given index. + /// + /// The terrain index to check + /// True if data has been fetched for the specified index, false otherwise. + [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 ) {