Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions AGXUnity/Collide/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,30 @@ public ShapeMaterial Material
}
}

[SerializeField]
private bool m_isPulley = false;

/// <summary>
/// When enabled, Wires will snap onto this geometry to simulate a sheave or pulley geometry without having to rely on contacts
/// </summary>
[Tooltip( "When enabled, Wires will snap onto this geometry to simulate a sheave or pulley geometry without having to rely on contacts" )]
[InspectorGroupBegin( Name = "Extra Properties" )]
[InspectorGroupEnd]
public bool IsPulley
{
get => m_isPulley;
set
{
m_isPulley = value;
if ( NativeGeometry != null ) {
if ( m_isPulley )
NativeGeometry.getPropertyContainer().addPropertyBool( "Pulley", true );
else
NativeGeometry.getPropertyContainer().removePropertyBool( "Pulley" );
}
}
}

/// <summary>
/// Native geometry object, if initialized.
/// </summary>
Expand Down
9 changes: 1 addition & 8 deletions AGXUnity/Model/DeformableTerrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected override bool Initialize()
LicenseManager.LicenseInfo.HasModuleLogError( LicenseInfo.Module.AGXTerrain | LicenseInfo.Module.AGXGranular, this );

m_initialHeights = TerrainData.GetHeights( 0, 0, TerrainDataResolution, TerrainDataResolution );

Terrain.terrainData = Instantiate( Terrain.terrainData );
InitializeNative();

Simulation.Instance.StepCallbacks.PostStepForward += OnPostStepForward;
Expand Down Expand Up @@ -144,13 +144,6 @@ private void ResetTerrainDataHeightsAndTransform()

TerrainData.SetHeights( 0, 0, m_initialHeights );
transform.position = transform.position + MaximumDepth * Vector3.up;

#if UNITY_EDITOR
// If the editor is closed during play the modified height
// data isn't saved, this resolves corrupt heights in such case.
UnityEditor.EditorUtility.SetDirty( TerrainData );
UnityEditor.AssetDatabase.SaveAssets();
#endif
}

private void OnPostStepForward()
Expand Down
8 changes: 2 additions & 6 deletions AGXUnity/Model/DeformableTerrainConnector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ public Vector3 GetOffsetPosition()
if ( InitialHeights != null )
return needsReturnData ? Terrain.terrainData.GetHeights( 0, 0, resolution, resolution ) : null;

Terrain.terrainData = Instantiate( Terrain.terrainData );

if ( float.IsNaN( MaximumDepth ) ) {
Debug.LogError( "Writing terrain offset without first setting depth!" );
MaximumDepth = 0;
Expand All @@ -41,12 +43,6 @@ internal void OnReset()
transform.position += MaximumDepth * Vector3.up;
Terrain.terrainData.SetHeights( 0, 0, InitialHeights );

#if UNITY_EDITOR
// If the editor is closed during play the modified height
// data isn't saved, this resolves corrupt heights in such case.
UnityEditor.EditorUtility.SetDirty( Terrain.terrainData );
UnityEditor.AssetDatabase.SaveAssets();
#endif
InitialHeights = null;
}
}
Expand Down
36 changes: 26 additions & 10 deletions AGXUnity/Model/DeformableTerrainPager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -411,8 +411,7 @@ private void InitializeNative()
foreach ( var rb in m_rigidbodies )
Native.add( rb.Body.GetInitialized<RigidBody>().Native, rb.requiredRadius, rb.preloadRadius );

if ( MaterialPatches.Length != 0 )
Debug.LogWarning( "Nonhomogenous terrain is not yet supported for DeformableTerrainPager.", this );
GetSimulation().add( Native );
}

protected override void OnDestroy()
Expand Down Expand Up @@ -444,7 +443,7 @@ private void UpdateHeights()

private void UpdateTerrain( agxTerrain.TerrainPager.TileAttachments tile )
{
var terrain = tile.m_terrainTile;
var terrain = tile.m_terrainTile.get();
var modifications = terrain.getModifiedVertices();
if ( modifications.Count == 0 )
return;
Expand All @@ -456,12 +455,12 @@ private void UpdateTerrain( agxTerrain.TerrainPager.TileAttachments tile )
var result = new float[,] { { 0.0f } };

agx.Vec2i index = new agx.Vec2i(0,0);
Vector2Int tileIndex = GetTileIndex(terrain.get());
Vector2Int tileIndex = GetTileIndex(terrain);

UnityTerrainAdapter.UnityModificationCallback modCallbackFn = ( Terrain tile, Vector2Int unityIndex ) =>
{
tile.terrainData.SetHeightsDelayLOD( unityIndex.x, unityIndex.y, result );
OnModification?.Invoke( terrain.get(), index, tile, unityIndex );
OnModification?.Invoke( terrain, index, tile, unityIndex );
m_updatedTerrains.Add( tile );
};

Expand Down Expand Up @@ -845,17 +844,33 @@ public override void TriggerModifyAllCells()

public override bool ReplaceTerrainMaterial( DeformableTerrainMaterial oldMat, DeformableTerrainMaterial newMat )
{
throw new NotImplementedException( "Terrain pager does not yet support Inhomogeneous terrain" );
if ( Native == null )
return true;

if ( oldMat == null || newMat == null )
return false;

var success = m_terrainDataSource.exchangeTerrainMaterial(oldMat.GetInitialized().Native, newMat.GetInitialized().Native);
return success;
}

public override void SetAssociatedMaterial( DeformableTerrainMaterial terrMat, ShapeMaterial shapeMat )
{
throw new NotImplementedException( "Terrain pager does not yet support Inhomogeneous terrain" );
if ( Native == null )
return;

m_terrainDataSource.setAssociatedMaterial( shapeMat.GetInitialized().Native, terrMat.GetInitialized().Native );
}

public override void AddTerrainMaterial( DeformableTerrainMaterial terrMat, Shape shape = null )
{
throw new NotImplementedException( "Terrain pager does not yet support Inhomogeneous terrain" );
if ( Native == null )
return;

if ( shape != null )
m_terrainDataSource.addTerrainMaterial( terrMat.GetInitialized().Native, shape.GetInitialized().NativeGeometry );
else
m_terrainDataSource.addTerrainMaterial( terrMat.GetInitialized().Native );
}

protected override bool IsNativeNull() { return Native == null; }
Expand All @@ -867,8 +882,9 @@ protected override void SetShapeMaterial( agx.Material material, agxTerrain.Terr

protected override void SetTerrainMaterial( agxTerrain.TerrainMaterial material )
{
Native?.getTemplateTerrain().setTerrainMaterial( material );
OnPropertiesUpdated();
m_terrainDataSource.setDefaultTerrainMaterial( material );
if ( Material != null )
m_terrainDataSource.setAssociatedMaterial( Material.Native, material );
}

protected override void SetEnable( bool enable )
Expand Down
9 changes: 9 additions & 0 deletions AGXUnity/Model/MovableTerrain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,15 @@ public override void EditorUpdate()
#endif
}

protected override bool PerformMigration()
{
if ( m_serializationVersion < 2 ) {
PlacementMode = Placement.Manual;
return true;
}
return false;
}

protected override bool Initialize()
{
// Only printing the errors if something is wrong.
Expand Down
9 changes: 7 additions & 2 deletions AGXUnity/Model/TerrainMaterialPatch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,11 @@ public DeformableTerrainMaterial TerrainMaterial
return;
}

if ( (bool)ParentTerrain?.ReplaceTerrainMaterial( m_terrainMaterial, value ) )
if ( m_terrainMaterial == null ) {
ParentTerrain?.AddTerrainMaterial( value );
m_terrainMaterial = value;
}
else if ( (bool)ParentTerrain?.ReplaceTerrainMaterial( m_terrainMaterial, value ) )
m_terrainMaterial = value;
}
}
Expand All @@ -51,7 +55,8 @@ public DeformableTerrainMaterial TerrainMaterial
public void AddShape( Shape shape )
{
shape.enabled &= !DisableShapes;
shape.Visual.GetComponent<MeshRenderer>().enabled &= !DisableVisuals;
if ( shape.Visual != null )
shape.Visual.GetComponent<MeshRenderer>().enabled &= !DisableVisuals;
ParentTerrain?.AddTerrainMaterial( m_terrainMaterial, shape.GetInitialized<Shape>() );
}

Expand Down
88 changes: 70 additions & 18 deletions AGXUnity/Model/Track.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using AGXUnity.Collide;
using AGXUnity.Utils;
using agxVehicle;
using System.Collections.Generic;
Expand Down Expand Up @@ -266,19 +267,7 @@ public ShapeMaterial Material
/// Registered track wheel instances.
/// </summary>
[HideInInspector]
public TrackWheel[] Wheels
{
get { return m_wheels.ToArray(); }
}

protected override bool PerformMigration()
{
if ( m_serializationVersion < 2 ) {
FullDoF = true;
return true;
}
return false;
}
public TrackWheel[] Wheels => m_wheels.ToArray();

/// <summary>
/// Associate track wheel instance to this track.
Expand Down Expand Up @@ -313,18 +302,77 @@ public bool Remove( TrackWheel wheel )
/// </summary>
/// <param name="wheel">Track wheel instance.</param>
/// <returns>True if <paramref name="wheel"/> is associated to this track.</returns>
public bool Contains( TrackWheel wheel )
public bool Contains( TrackWheel wheel ) => m_wheels.Contains( wheel );

/// <summary>
/// Verifies so that all added track wheels still exists. Wheels that
/// has been deleted are removed.
/// </summary>
public void RemoveInvalidWheels() => m_wheels.RemoveAll( wheel => wheel == null );

[SerializeField]
private List<Shape> m_supportGeometries = new List<Shape>();

/// <summary>
/// Registered support geometry instances.
/// </summary>
[HideInInspector]
public Shape[] SupportGeometries => m_supportGeometries.ToArray();

/// <summary>
/// Associate support geometry instance to this track.
/// </summary>
/// <param name="supportGeometry">support geometry instance to add.</param>
/// <returns>True if added, false if null or already added.</returns>
public bool Add( Shape supportGeometry )
{
return m_wheels.Contains( wheel );
if ( supportGeometry == null || m_supportGeometries.Contains( supportGeometry ) )
return false;

m_supportGeometries.Add( supportGeometry );

if ( Native != null )
Native.addSupportGroupId( supportGeometry.GetInitialized().NativeGeometry );

return true;
}

/// <summary>
/// Verifies so that all added track wheels still exists. Wheels that
/// Disassociate support geometry instance from this track.
/// </summary>
/// <param name="supportGeometry">Support geometry instance to remove.</param>
/// <returns>True if removed, false if null or not associated to this track.</returns>
public bool Remove( Shape supportGeometry )
{
if ( supportGeometry == null )
return false;

if ( Native != null )
Native.removeSupportGroupId( supportGeometry.GetInitialized().NativeGeometry );

return m_supportGeometries.Remove( supportGeometry );
}

/// <summary>
/// True if <paramref name="supportGeometry"/> is associated to this track.
/// </summary>
/// <param name="supportGeometry">Support geometry instance.</param>
/// <returns>True if <paramref name="supportGeometry"/> is associated to this track.</returns>
public bool Contains( Shape supportGeometry ) => m_supportGeometries.Contains( supportGeometry );

/// <summary>
/// Verifies so that all added support geometries still exists. Geometries that
/// has been deleted are removed.
/// </summary>
public void RemoveInvalidWheels()
public void RemoveInvalidSupportGeometries() => m_supportGeometries.RemoveAll( geom => geom == null );

protected override bool PerformMigration()
{
m_wheels.RemoveAll( wheel => wheel == null );
if ( m_serializationVersion < 2 ) {
FullDoF = true;
return true;
}
return false;
}

private class OnInitializeAdapter : TrackNodeOnInitializeCallback
Expand Down Expand Up @@ -354,6 +402,7 @@ protected override bool Initialize()
return false;

RemoveInvalidWheels();
RemoveInvalidSupportGeometries();

if ( m_wheels.Count == 0 ) {
Debug.LogError( "Component: Track requires at least one wheel to initialize.", this );
Expand Down Expand Up @@ -389,6 +438,9 @@ protected override bool Initialize()
foreach ( var wheel in Wheels )
Native.add( wheel.Native );

foreach ( var geom in SupportGeometries )
Native.addSupportGroupId( geom.GetInitialized().NativeGeometry );

if ( WidthVariation != null || ThicknessVariation != null )
Native.initialize( new OnInitializeAdapter( Width, Thickness, WidthVariation, ThicknessVariation ) );
else
Expand Down
2 changes: 2 additions & 0 deletions AGXUnity/Model/TrackProperties.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ protected override bool PerformMigration()
HingeStiffnessRotational = new Vector3( convertCompliance( m_hingeComplianceRotational.x ), convertCompliance( m_hingeComplianceRotational.y ), 100.0f );
HingeAttenuationRotational = new Vector3( convertDamping( m_hingeDampingRotational.x ), convertDamping( m_hingeDampingRotational.y ), 2.0f );

FullDoF = true;

return true;
}

Expand Down
Loading
Loading