Skip to content

Commit

Permalink
Improved support for runtime component/game object enabled/disabled s…
Browse files Browse the repository at this point in the history
…tate changes.
  • Loading branch information
nmalg committed Sep 20, 2022
1 parent 73f2d98 commit 7167e7f
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 52 deletions.
29 changes: 18 additions & 11 deletions AGXUnity/Cable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -374,15 +374,14 @@ protected override bool Initialize()

try {
if ( Route.NumNodes < 2 )
throw new Exception( "Invalid number of nodes. Minimum number of route nodes is two." );
throw new Exception( $"{GetType().FullName} ERROR: Invalid number of nodes. Minimum number of route nodes is two." );

agxCable.Cable cable = null;
if ( RouteAlgorithm == RouteType.Segmenting ) {
var result = SynchronizeRoutePointCurve();
if ( !result.Successful )
throw new Exception( "Invalid cable route. Unable to initialize cable with " +
Route.NumNodes +
" nodes and resolution/length = " + ResolutionPerUnitLength + "." );
throw new Exception( $"{GetType().FullName} ERROR: Invalid cable route. Unable to initialize cable with " +
$" {Route.NumNodes} nodes and resolution/length = {ResolutionPerUnitLength}." );

cable = CreateNative( result.NumSegments / Route.TotalLength );

Expand Down Expand Up @@ -410,21 +409,29 @@ protected override bool Initialize()
}
if ( !cable.add( routeNode.GetInitialized<CableRouteNode>().Native ) )
throw new Exception( "Unable to add node to cable." );
throw new Exception( $"{GetType().FullName} ERROR: Unable to add node to cable." );
} );

if ( !success )
throw new Exception( string.Format( "Invalid route - unable to find segment length given resolution/length = {0}",
ResolutionPerUnitLength ) );
throw new Exception( $"{GetType().FullName} ERROR: Invalid route - unable to find segment length given resolution/length = {ResolutionPerUnitLength}." );
}
else {
cable = CreateNative( ResolutionPerUnitLength );
foreach ( var node in Route ) {
if ( !cable.add( node.GetInitialized<CableRouteNode>().Native ) )
throw new Exception( "Unable to add node to cable." );
throw new Exception( $"{GetType().FullName} ERROR: Unable to add node to cable." );
}
}

cable.setName( name );

// Adding the cable to the simulation independent of if this
// cable is enabled or not, only to initialize it. If this
// component/game object is disabled, remove it later.
GetSimulation().add( cable );
if ( cable.getInitializationReport().getNumSegments() == 0 )
throw new Exception( $"{GetType().FullName} ERROR: Initialization failed. Check route and/or resolution." );

Native = cable;
}
catch ( Exception e ) {
Expand All @@ -433,9 +440,9 @@ protected override bool Initialize()
return false;
}

Native.setName( name );

GetSimulation().add( Native );
// Remove if this cable is inactive/disabled (the cable has been added above).
if ( !isActiveAndEnabled )
GetSimulation().remove( Native );

if ( Properties != null )
Properties.GetInitialized<CableProperties>();
Expand Down
29 changes: 13 additions & 16 deletions AGXUnity/Collide/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,20 +150,17 @@ public bool IsEnabledInHierarchy
{
get
{
RigidBody rb = RigidBody;
return enabled && gameObject.activeInHierarchy && ( rb == null || rb.enabled );
}
}

/// <summary>
/// True if the game object this active and this component is enabled.
/// </summary>
[HideInInspector]
public bool IsEnabled
{
get
{
return gameObject.activeSelf && enabled;
// If this component is disabled or our game object or any of its
// parent(s) game object(s) are disabled, this shape is disabled.
if ( !isActiveAndEnabled )
return false;

// Assuming shapes are children of rigid bodies, which is per definition.
// 'isActiveAndEnabled' above will catch the case where the rigid body
// game object is inactive. We only have to check rigid body component
// enabled state.
var rb = RigidBody;
return rb == null || rb.enabled;
}
}

Expand Down Expand Up @@ -253,7 +250,7 @@ public void SetRigidBody( RigidBody rb )
if ( !rb.gameObject.HasChild( gameObject ) )
throw new Exception( "RigidBody not parent to Shape." );

m_geometry.setEnable( IsEnabled );
m_geometry.setEnable( isActiveAndEnabled );

rb.Native.add( m_geometry, GetNativeRigidBodyOffset( rb ) );

Expand Down Expand Up @@ -318,7 +315,7 @@ protected override bool Initialize()
return false;

m_geometry.setName( name );
m_geometry.setEnable( IsEnabled );
m_geometry.setEnable( isActiveAndEnabled );

if ( Material != null )
m_geometry.setMaterial( m_material.GetInitialized<ShapeMaterial>().Native );
Expand Down
24 changes: 16 additions & 8 deletions AGXUnity/Model/DeformableTerrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,7 @@ public void PatchTerrainData()

protected override void OnEnable()
{
if ( Native != null && !Native.getEnable() ) {
Native.setEnable( true );
Native.getGeometry().setEnable( true );
}
SetNativeEnable( true );
}

protected override bool Initialize()
Expand All @@ -288,15 +285,14 @@ protected override bool Initialize()
if ( Simulation.Instance.SolverSettings != null )
GetSimulation().getSolver().setNumPPGSRestingIterations( (ulong)Simulation.Instance.SolverSettings.PpgsRestingIterations );

SetNativeEnable( isActiveAndEnabled );

return true;
}

protected override void OnDisable()
{
if ( Native != null && Native.getEnable() ) {
Native.setEnable( false );
Native.getGeometry().setEnable( false );
}
SetNativeEnable( false );
}

protected override void OnDestroy()
Expand All @@ -318,6 +314,18 @@ protected override void OnDestroy()
base.OnDestroy();
}

private void SetNativeEnable( bool enable )
{
if ( Native == null )
return;

if ( Native.getEnable() == enable )
return;

Native.setEnable( enable );
Native.getGeometry().setEnable( enable );
}

private void InitializeNative()
{
var nativeHeightData = WriteTerrainDataOffset( Terrain, MaximumDepth );
Expand Down
9 changes: 7 additions & 2 deletions AGXUnity/Rendering/CableRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ private void Render()
}
return;
}
else if ( !m_segmentSpawner.IsValid )
InitializeRenderer( true );

var it = native.begin();
var endIt = native.end();
Expand Down Expand Up @@ -142,8 +144,11 @@ private void OnEnableDisable( bool enable )
Simulation.Instance.StepCallbacks.PostStepForward += Render;
}
else {
m_segmentSpawner.Destroy();
m_segmentSpawner = null;
if ( m_segmentSpawner != null ) {
m_segmentSpawner.Destroy();
m_segmentSpawner = null;
}

if ( Simulation.HasInstance && Application.isPlaying )
Simulation.Instance.StepCallbacks.PostStepForward -= Render;
}
Expand Down
2 changes: 2 additions & 0 deletions AGXUnity/Rendering/SegmentSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public Material DefaultMaterial
}
}

public bool IsValid => m_parentComponent != null;

public SegmentSpawner( ScriptComponent parentComponent, string prefabObjectPath, string separateFirstObjectPath = "" )
{
m_parentComponent = parentComponent;
Expand Down
15 changes: 4 additions & 11 deletions AGXUnity/RigidBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -219,12 +219,6 @@ public MassProperties MassProperties
}
}

/// <summary>
/// True if the game object is active in hierarchy and this component is enabled.
/// </summary>
[HideInInspector]
public bool IsEnabled { get { return gameObject.activeInHierarchy && enabled; } }

/// <summary>
/// Array of shapes belonging to this rigid body instance.
/// </summary>
Expand Down Expand Up @@ -332,7 +326,7 @@ public void PeekTemporaryNativeOrGetNative( Action<agx.RigidBody, bool> callback
if ( geometry == null )
continue;

geometry.setEnable( shape.IsEnabled );
geometry.setEnable( shape.isActiveAndEnabled );
if ( shape.Material != null )
geometry.setMaterial( shape.Material.CreateTemporaryNative() );

Expand Down Expand Up @@ -366,7 +360,7 @@ public static agx.RigidBody InstantiateTemplate( RigidBody template, Shape[] sha
foreach ( var shape in shapes ) {
var geometry = shape.CreateTemporaryNative();

geometry.setEnable( shape.IsEnabled );
geometry.setEnable( shape.isActiveAndEnabled );
if ( shape.Material != null )
geometry.setMaterial( shape.Material.GetInitialized<ShapeMaterial>().Native );
native.add( geometry, shape.GetNativeRigidBodyOffset( template ) );
Expand Down Expand Up @@ -440,7 +434,7 @@ protected override bool Initialize()

m_rb = new agx.RigidBody();
m_rb.setName( name );
m_rb.setEnable( IsEnabled );
m_rb.setEnable( isActiveAndEnabled );
m_rb.getMassProperties().setAutoGenerateMask( 0u );

SyncNativeTransform( m_rb );
Expand All @@ -451,8 +445,7 @@ protected override bool Initialize()

UpdateMassProperties();

if ( IsEnabled )
HandleUpdateCallbacks( true );
HandleUpdateCallbacks( isActiveAndEnabled );

return true;
}
Expand Down
11 changes: 8 additions & 3 deletions AGXUnity/Wire.cs
Original file line number Diff line number Diff line change
Expand Up @@ -237,9 +237,6 @@ public void RestoreLocalDataFrom( agxWire.Wire native )
Radius = System.Convert.ToSingle( native.getRadius() );
ResolutionPerUnitLength = System.Convert.ToSingle( native.getResolutionPerUnitLength() );
ScaleConstant = System.Convert.ToSingle( native.getParameterController().getScaleConstant() );

// Is the wire enabled/active?
gameObject.SetActive(native.isEnabled());
}

protected override void OnEnable()
Expand Down Expand Up @@ -300,7 +297,15 @@ protected override bool Initialize()

Native.setName( name );

// Wires doesn't have setEnable( true/false ) but supports
// re-adding to a simulation. I.e., the state of the previously
// removed wire will be recovered when the wire is added again.
// Initialize the wire (by adding it) and remove the wire if this
// component isn't active.
GetSimulation().add( Native );
if ( !isActiveAndEnabled )
GetSimulation().remove( Native );

Simulation.Instance.StepCallbacks.PostStepForward += OnPostStepForward;
}
catch ( System.Exception e ) {
Expand Down
2 changes: 1 addition & 1 deletion Editor/AGXUnityEditor/Utils/DrawGizmoCallbackHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ private static void HandleSelectedGameObject( GameObject selected, ObjectsGizmoC
Shape shape = null;
MeshFilter filter = null;
if ( ( rb = selected.GetComponent<RigidBody>() ) != null ) {
if ( rb.IsEnabled )
if ( rb.isActiveAndEnabled )
m_colorHandler.Highlight( rb, selectionType );
}
else if ( ( shape = selected.GetComponent<Shape>() ) != null ) {
Expand Down

0 comments on commit 7167e7f

Please sign in to comment.