Skip to content

Commit 07f034d

Browse files
committed
Initial support for refactored shovels
1 parent b4fd0f4 commit 07f034d

9 files changed

+85
-370
lines changed

Diff for: AGXUnity/Model/DeformableTerrain.cs

-132
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,7 @@
22
using AGXUnity.Collide;
33
using AGXUnity.Utils;
44
using System;
5-
using System.Collections.Generic;
65
using UnityEngine;
7-
using GUI = AGXUnity.Utils.GUI;
86

97
namespace AGXUnity.Model
108
{
@@ -42,37 +40,6 @@ public Terrain Terrain
4240
[HideInInspector]
4341
public int TerrainDataResolution { get { return TerrainUtils.TerrainDataResolution( TerrainData ); } }
4442

45-
[SerializeField]
46-
private List<DeformableTerrainShovel> m_shovels = new List<DeformableTerrainShovel>();
47-
48-
[SerializeField]
49-
private bool m_tempDisplayShovelForces = false;
50-
51-
[HideInInspector]
52-
public bool TempDisplayShovelForces
53-
{
54-
get { return m_tempDisplayShovelForces; }
55-
set
56-
{
57-
m_tempDisplayShovelForces = value;
58-
59-
if ( !Application.isPlaying )
60-
return;
61-
62-
if ( m_tempDisplayShovelForces &&
63-
Shovels.Length > 0 &&
64-
GUIWindowHandler.Instance.GetWindowData( ShowForces ) == null ) {
65-
var windowSize = new Vector2( 750, 125 );
66-
GUIWindowHandler.Instance.Show( ShowForces,
67-
windowSize,
68-
new Vector2( Screen.width - windowSize.x - 20, 20 ),
69-
"Shovel forces" );
70-
}
71-
else if ( !m_tempDisplayShovelForces && GUIWindowHandler.HasInstance )
72-
GUIWindowHandler.Instance.Close( ShowForces );
73-
}
74-
}
75-
7643
/// <summary>
7744
/// Resets heights of the Unity terrain and recreate native instance.
7845
/// </summary>
@@ -104,8 +71,6 @@ protected override bool Initialize()
10471
// Only printing the errors if something is wrong.
10572
LicenseManager.LicenseInfo.HasModuleLogError( LicenseInfo.Module.AGXTerrain | LicenseInfo.Module.AGXGranular, this );
10673

107-
RemoveInvalidShovels( true, true );
108-
10974
m_initialHeights = TerrainData.GetHeights( 0, 0, TerrainDataResolution, TerrainDataResolution );
11075

11176
InitializeNative();
@@ -135,9 +100,6 @@ protected override void OnDestroy()
135100
}
136101
Native = null;
137102

138-
if ( GUIWindowHandler.HasInstance )
139-
GUIWindowHandler.Instance.Close( ShowForces );
140-
141103
base.OnDestroy();
142104
}
143105

@@ -156,10 +118,6 @@ private void InitializeNative()
156118

157119
Native.setTransform( Utils.TerrainUtils.CalculateNativeOffset( transform, TerrainData ) );
158120

159-
160-
foreach ( var shovel in Shovels )
161-
Native.add( shovel.GetInitialized<DeformableTerrainShovel>()?.Native );
162-
163121
GetSimulation().add( Native );
164122
}
165123

@@ -209,108 +167,18 @@ private void UpdateHeights( agxTerrain.ModifiedVerticesVector modifiedVertices )
209167
TerrainData.SyncHeightmap();
210168
}
211169

212-
private GUIStyle m_textLabelStyle = null;
213-
private void ShowForces( EventType eventType )
214-
{
215-
if ( m_textLabelStyle == null ) {
216-
m_textLabelStyle = new GUIStyle( GUI.Skin.label );
217-
m_textLabelStyle.alignment = TextAnchor.MiddleLeft;
218-
219-
var fonts = Font.GetOSInstalledFontNames();
220-
foreach ( var font in fonts ) {
221-
if ( font == "Consolas" ) {
222-
m_textLabelStyle.font = Font.CreateDynamicFontFromOSFont( font, 24 );
223-
break;
224-
}
225-
}
226-
}
227-
228-
var textColor = Color.Lerp( Color.black, Color.white, 1.0f );
229-
var valueColor = Color.Lerp( Color.green, Color.white, 0.45f );
230-
Func<string, agx.Vec3, GUIContent> Vec3Content = ( name, v ) =>
231-
{
232-
return GUI.MakeLabel( string.Format( "{0} [{1}, {2}, {3}] kN",
233-
GUI.AddColorTag( name, textColor ),
234-
GUI.AddColorTag( v.x.ToString( "0.00" ).PadLeft( 7, ' ' ), valueColor ),
235-
GUI.AddColorTag( v.y.ToString( "0.00" ).PadLeft( 7, ' ' ), valueColor ),
236-
GUI.AddColorTag( v.z.ToString( "0.00" ).PadLeft( 7, ' ' ), valueColor ) ) );
237-
};
238-
239-
var shovel = m_shovels[ 0 ].Native;
240-
var penetrationForce = new agx.Vec3();
241-
var penetrationTorque = new agx.Vec3();
242-
Native.getPenetrationForce( shovel, ref penetrationForce, ref penetrationTorque );
243-
var separationForce = -Native.getSeparationContactForce( shovel );
244-
var deformerForce = -Native.getDeformationContactForce( shovel );
245-
var contactForce = -Native.getContactForce( shovel );
246-
247-
GUILayout.Label( Vec3Content( "Penetration force:", 1.0E-3 * penetrationForce ), m_textLabelStyle );
248-
GUILayout.Space( 4 );
249-
GUILayout.Label( Vec3Content( "Separation force: ", 1.0E-3 * separationForce ), m_textLabelStyle );
250-
GUILayout.Space( 4 );
251-
GUILayout.Label( Vec3Content( "Deformer force: ", 1.0E-3 * deformerForce ), m_textLabelStyle );
252-
GUILayout.Space( 4 );
253-
GUILayout.Label( Vec3Content( "Contact force: ", 1.0E-3 * contactForce ), m_textLabelStyle );
254-
}
255-
256170
private Terrain m_terrain = null;
257171
private float[,] m_initialHeights = null;
258172

259173
// -----------------------------------------------------------------------------------------------------------
260174
// ------------------------------- Implementation of DeformableTerrainBase -----------------------------------
261175
// -----------------------------------------------------------------------------------------------------------
262176
public override float ElementSize => TerrainData.size.x / ( TerrainDataResolution - 1 );
263-
public override DeformableTerrainShovel[] Shovels => m_shovels.ToArray();
264177
public override agx.GranularBodyPtrArray GetParticles() { return Native?.getSoilSimulationInterface()?.getSoilParticles(); }
265178
public override Uuid GetParticleMaterialUuid() => Native?.getMaterial( agxTerrain.Terrain.MaterialType.PARTICLE ).getUuid();
266179
public override agxTerrain.SoilSimulationInterface GetSoilSimulationInterface() { return Native?.getSoilSimulationInterface(); }
267180
public override agxTerrain.TerrainProperties GetProperties() { return Native?.getProperties(); }
268181

269-
public override bool Add( DeformableTerrainShovel shovel )
270-
{
271-
if ( shovel == null || m_shovels.Contains( shovel ) )
272-
return false;
273-
274-
m_shovels.Add( shovel );
275-
276-
// Initialize shovel if we're initialized.
277-
if ( Native != null )
278-
Native.add( shovel.GetInitialized<DeformableTerrainShovel>().Native );
279-
280-
return true;
281-
}
282-
283-
public override bool Remove( DeformableTerrainShovel shovel )
284-
{
285-
if ( shovel == null || !m_shovels.Contains( shovel ) )
286-
return false;
287-
288-
if ( Native != null )
289-
Native.remove( shovel.Native );
290-
291-
return m_shovels.Remove( shovel );
292-
}
293-
294-
public override bool Contains( DeformableTerrainShovel shovel )
295-
{
296-
return shovel != null && m_shovels.Contains( shovel );
297-
}
298-
299-
public override void RemoveInvalidShovels( bool removeDisabled = false, bool warn = false )
300-
{
301-
m_shovels.RemoveAll( shovel => shovel == null );
302-
if ( removeDisabled ) {
303-
int removed = m_shovels.RemoveAll( shovel => !shovel.isActiveAndEnabled );
304-
if ( removed > 0 ) {
305-
if ( warn )
306-
Debug.LogWarning( $"Removed {removed} disabled shovels from terrain {gameObject.name}." +
307-
" Disabled shovels should not be added to the terrain on play and should instead be added manually when enabled during runtime." +
308-
" To fix this warning, please remove any disabled shovels from the terrain." );
309-
else
310-
Debug.Log( $"Removed {removed} disabled shovels from terrain {gameObject.name}." );
311-
}
312-
}
313-
}
314182
public override void ConvertToDynamicMassInShape( Shape failureVolume )
315183
{
316184
if ( !IsNativeNull() )

Diff for: AGXUnity/Model/DeformableTerrainBase.cs

-33
Original file line numberDiff line numberDiff line change
@@ -140,12 +140,6 @@ public float MaximumDepth
140140
/// </summary>
141141
abstract public float ElementSize { get; }
142142

143-
/// <summary>
144-
/// Shovels associated to this terrain.
145-
/// </summary>
146-
[HideInInspector]
147-
abstract public DeformableTerrainShovel[] Shovels { get; }
148-
149143
protected override void OnEnable()
150144
{
151145
SetEnable( true );
@@ -183,33 +177,6 @@ protected override void OnDisable()
183177
/// </summary>
184178
virtual public void OnPropertiesUpdated() { }
185179

186-
/// <summary>
187-
/// Associate shovel instance to this terrain.
188-
/// </summary>
189-
/// <param name="shovel">Shovel instance to add.</param>
190-
/// <returns>True if added, false if null or already added.</returns>
191-
abstract public bool Add( DeformableTerrainShovel shovel );
192-
193-
/// <summary>
194-
/// Disassociate shovel instance to this terrain.
195-
/// </summary>
196-
/// <param name="shovel">Shovel instance to remove.</param>
197-
/// <returns>True if removed, false if null or not associated to this terrain.</returns>
198-
abstract public bool Remove( DeformableTerrainShovel shovel );
199-
200-
/// <summary>
201-
/// Find if shovel has been associated to this terrain.
202-
/// </summary>
203-
/// <param name="shovel">Shovel instance to check.</param>
204-
/// <returns>True if associated, otherwise false.</returns>
205-
abstract public bool Contains( DeformableTerrainShovel shovel );
206-
207-
/// <summary>
208-
/// Verifies so that all added shovels still exists. Shovels that
209-
/// has been deleted are removed.
210-
/// </summary>
211-
abstract public void RemoveInvalidShovels( bool removeDisabled = false, bool warn = false );
212-
213180
/// <summary>
214181
/// Converts any part of the terrain that overlaps the provided shape into dynamic mass
215182
/// </summary>

Diff for: AGXUnity/Model/DeformableTerrainPager.cs

+26-44
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ public class DeformableTerrainPager : DeformableTerrainBase
3838
[SerializeField]
3939
private List<PagingBody<DeformableTerrainShovel>> m_shovels = new List<PagingBody<DeformableTerrainShovel>>();
4040

41+
4142
/// <summary>
4243
/// Shovels along with their respective load radii that are associated with this terrainPager
4344
/// </summary>
@@ -47,6 +48,12 @@ public class DeformableTerrainPager : DeformableTerrainBase
4748
[HideInInspector]
4849
public PagingBody<DeformableTerrainShovel>[] PagingShovels { get { return m_shovels.ToArray(); } }
4950

51+
/// <summary>
52+
/// Rigidbodies associated to this terrain.
53+
/// </summary>
54+
[HideInInspector]
55+
public DeformableTerrainShovel[] Shovels { get { return m_shovels.Select( rb => rb.Body ).ToArray(); } }
56+
5057
[SerializeField]
5158
private List<PagingBody<RigidBody>> m_rigidbodies = new List<PagingBody<RigidBody>>();
5259

@@ -178,6 +185,23 @@ public bool Add( DeformableTerrainShovel shovel, float requiredRadius = 5, float
178185
return true;
179186
}
180187

188+
/// <summary>
189+
/// Disassociate shovel instance to this terrain.
190+
/// </summary>
191+
/// <param name="shovel">Shovel instance to remove.</param>
192+
/// <returns>True if removed, false if null or not associated to this terrain.</returns>
193+
public bool Remove( DeformableTerrainShovel shovel )
194+
{
195+
if ( shovel == null || m_shovels.Find( pagingRigidBody => pagingRigidBody.Body == shovel ) == null )
196+
return false;
197+
198+
if ( Native != null )
199+
Native.remove( shovel.Native );
200+
201+
m_shovels.RemoveAt( m_shovels.FindIndex( pagingRigidBody => pagingRigidBody.Body == shovel ) );
202+
return true;
203+
}
204+
181205
/// <summary>
182206
/// Associates the given rigidbody instance to this terrain.
183207
/// </summary>
@@ -316,8 +340,6 @@ protected override bool Initialize()
316340
if ( AutoTileOnPlay )
317341
RecalculateParameters();
318342

319-
RemoveInvalidShovels( true );
320-
321343
// Create a new adapter using the terrain attached to this gameobject as the root
322344
// This attaches DeformableTerrainConnector components to each connected Unity terrain which must be done before InitializeNative is called
323345
m_terrainDataSource = new UnityTerrainAdapter( Terrain, MaximumDepth );
@@ -370,6 +392,8 @@ private void InitializeNative()
370392
Native.setTerrainDataSource( m_terrainDataSource );
371393
Native.setShouldStoreCompaction( m_compactionStoreDepth.UseOverride, (uint)m_compactionStoreDepth.OverrideValue );
372394

395+
GetSimulation().add( Native );
396+
373397
// Add Rigidbodies and shovels to pager
374398
foreach ( var shovel in m_shovels )
375399
Native.add( shovel.Body.GetInitialized<DeformableTerrainShovel>().Native, shovel.requiredRadius, shovel.preloadRadius );
@@ -378,9 +402,6 @@ private void InitializeNative()
378402

379403
if ( MaterialPatches.Length != 0 )
380404
Debug.LogWarning( "Nonhomogenous terrain is not yet supported for DeformableTerrainPager.", this );
381-
382-
383-
GetSimulation().add( Native );
384405
}
385406

386407
protected override void OnDestroy()
@@ -590,50 +611,11 @@ public static bool IsInteger( float v )
590611
// -----------------------------------------------------------------------------------------------------------
591612

592613
public override float ElementSize => TerrainData.size.x / ( TerrainDataResolution - 1 );
593-
public override DeformableTerrainShovel[] Shovels => m_shovels.Select( shovel => shovel.Body ).ToArray();
594614
public override agx.GranularBodyPtrArray GetParticles() { return Native?.getSoilSimulationInterface()?.getSoilParticles(); }
595615
public override agx.Uuid GetParticleMaterialUuid() => Native?.getTemplateTerrain()?.getMaterial( agxTerrain.Terrain.MaterialType.PARTICLE ).getUuid();
596616
public override agxTerrain.TerrainProperties GetProperties() { return Native?.getTemplateTerrain()?.getProperties(); }
597617
public override agxTerrain.SoilSimulationInterface GetSoilSimulationInterface() { return Native?.getSoilSimulationInterface(); }
598618
public override void OnPropertiesUpdated() { Native?.applyChangesToTemplateTerrain(); }
599-
public override bool Add( DeformableTerrainShovel shovel )
600-
{
601-
return Add( shovel, requiredRadius: default, preloadRadius: default );
602-
}
603-
public override bool Remove( DeformableTerrainShovel shovel )
604-
{
605-
if ( shovel == null || m_shovels.Find( pagingShovel => pagingShovel.Body == shovel ) == null )
606-
return false;
607-
608-
if ( Native != null )
609-
Native.remove( shovel.Native );
610-
611-
m_shovels.RemoveAt( m_shovels.FindIndex( pagingShovel => pagingShovel.Body == shovel ) );
612-
return true;
613-
}
614-
public override bool Contains( DeformableTerrainShovel shovel )
615-
{
616-
return m_shovels.Find( s => s.Body == shovel ) != null;
617-
}
618-
public override void RemoveInvalidShovels( bool removeDisabled = false, bool warn = false )
619-
{
620-
m_shovels.RemoveAll( shovel => shovel.Body == null );
621-
m_rigidbodies.RemoveAll( rb => rb.Body == null );
622-
623-
if ( removeDisabled ) {
624-
int remShovels = m_shovels.RemoveAll( shovel => !shovel.Body.isActiveAndEnabled );
625-
int remRBs = m_rigidbodies.RemoveAll( rb => !rb.Body.isActiveAndEnabled );
626-
if ( remShovels + remRBs > 0 ) {
627-
if ( warn )
628-
Debug.LogWarning( $"Removed {remShovels} disabled shovels and {remRBs} disabled rigid bodies from terrain {gameObject.name}." +
629-
" Disabled objects should not be added to the terrain on play and should instead be added manually when enabled during runtime." +
630-
" To fix this warning, please remove any disabled objects from the terrain." );
631-
else
632-
Debug.Log( $"Removed {remShovels} disabled shovels and {remRBs} disabled rigid bodies from terrain {gameObject.name}." );
633-
634-
}
635-
}
636-
}
637619
public override void ConvertToDynamicMassInShape( Shape failureVolume )
638620
{
639621
if ( Native != null ) {

0 commit comments

Comments
 (0)