Skip to content

Commit

Permalink
Merge pull request #10 from Algoryx/fix/NativePODTypes
Browse files Browse the repository at this point in the history
Fix/native pod types
  • Loading branch information
vrguru authored Oct 1, 2018
2 parents a51bcb7 + 7fdec62 commit 40231cf
Show file tree
Hide file tree
Showing 18 changed files with 643 additions and 99 deletions.
4 changes: 2 additions & 2 deletions AGXUnity/CableRouteNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,9 +115,9 @@ protected override bool Initialize()
Rotation.ToHandedQuat();

if ( Type == Cable.NodeType.BodyFixedNode )
Native = new agxCable.BodyFixedNode( rb != null ? rb.Native : null, new agx.AffineMatrix4x4( rotation, position ) );
Native = new agxCable.CableBodyFixedNode( rb != null ? rb.Native : null, new agx.AffineMatrix4x4( rotation, position ) );
else if ( Type == Cable.NodeType.FreeNode ) {
Native = new agxCable.FreeNode( position );
Native = new agxCable.CableFreeNode( position );
Native.getRigidBody().setRotation( Rotation.ToHandedQuat() );
}
else
Expand Down
3 changes: 1 addition & 2 deletions AGXUnity/Collide/HeightField.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ public override UnityEngine.Vector3 GetScale()
/// <returns>Shape transform to be used between native geometry and shape.</returns>
public override agx.AffineMatrix4x4 GetNativeGeometryOffset()
{
return agx.AffineMatrix4x4.rotate( agx.Vec3.Z_AXIS(), agx.Vec3.Y_AXIS() ).Multiply(
agx.AffineMatrix4x4.translate( transform.position.ToHandedVec3() + new Vector3( 0.5f * GetWidth(), 0, 0.5f * GetHeight() ).ToHandedVec3() ) );
return agx.AffineMatrix4x4.rotate( agx.Vec3.Z_AXIS(), agx.Vec3.Y_AXIS() ) * agx.AffineMatrix4x4.translate( transform.position.ToHandedVec3() + new Vector3( 0.5f * GetWidth(), 0, 0.5f * GetHeight() ).ToHandedVec3() );
}

/// <summary>
Expand Down
40 changes: 36 additions & 4 deletions AGXUnity/Collide/Shape.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,29 @@ public bool CollisionsEnabled
}
}


/// <summary>
/// Is Shape a sensor?
/// </summary>
[SerializeField]
private bool m_isSensor = false;

/// <summary>
/// Specify if this shape is a sensor or not
/// </summary>
public bool IsSensor
{
get { return m_isSensor; }
set
{
m_isSensor = value;
if (NativeGeometry != null)
NativeGeometry.setSensor(m_isSensor);
}
}



/// <summary>
/// Shape material instance paired with property Material.
/// </summary>
Expand Down Expand Up @@ -160,7 +183,7 @@ public virtual agxCollide.Shape CreateTemporaryNative()
/// <returns>Relative transform geometry -> shape.</returns>
public virtual agx.AffineMatrix4x4 GetNativeGeometryOffset()
{
return new agx.AffineMatrix4x4();
return agx.AffineMatrix4x4.identity();
}

/// <summary>
Expand All @@ -172,12 +195,12 @@ public agx.AffineMatrix4x4 GetNativeRigidBodyOffset( RigidBody rb )
// If we're on the same level as the rigid body we have by
// definition no offset to the body.
if ( rb == null || rb.gameObject == gameObject )
return new agx.AffineMatrix4x4();
return agx.AffineMatrix4x4.identity();

// Using the world position of the shape - which includes scaling etc.
agx.AffineMatrix4x4 shapeInWorld = new agx.AffineMatrix4x4( transform.rotation.ToHandedQuat(), transform.position.ToHandedVec3() );
agx.AffineMatrix4x4 rbInWorld = new agx.AffineMatrix4x4( rb.transform.rotation.ToHandedQuat(), rb.transform.position.ToHandedVec3() );
return shapeInWorld.Multiply( rbInWorld.inverse() );
return shapeInWorld * rbInWorld.inverse();
}

/// <summary>
Expand Down Expand Up @@ -344,8 +367,17 @@ private void OnPostSynchronizeTransformsCallback()
{
SyncUnityTransform();

// If DebugRenderManager is disabled, we should NOT try to update anything.
// the m_geometry.getRigidBody will allocate memory for a RigidBodyInstance, so we
// want to avoid that.
bool isInstanced = Rendering.DebugRenderManager.IsActiveForSynchronize;
bool enabled = false;

if (isInstanced)
enabled = Rendering.DebugRenderManager.Instance.isActiveAndEnabled;

// If we have a body the debug rendering synchronization is made from that body.
if ( m_geometry != null && m_geometry.getRigidBody() == null )
if (enabled && m_geometry != null && m_geometry.getRigidBody() == null )
Rendering.DebugRenderManager.OnPostSynchronizeTransforms( this );
}

Expand Down
23 changes: 17 additions & 6 deletions AGXUnity/Constraint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,8 @@ public ESolveType SolveType
[SerializeField]
private bool m_drawGizmosEnable = true;

private bool m_isAnimated = false;

/// <summary>
/// Enable/disable gizmos drawing of this constraint. Enabled by default.
/// </summary>
Expand Down Expand Up @@ -745,18 +747,31 @@ protected override bool Initialize()
bool valid = added && Native.getValid();
Simulation.Instance.StepCallbacks.PreSynchronizeTransforms += OnPreStepForwardUpdate;

// It's not possible to check which properties an animator
// is controlling, for now we update all properties in the
// controllers if we have an animator.
m_isAnimated = GetComponent<Animator>() != null;

return valid;
}
catch ( System.Exception e ) {
Debug.LogException( e, gameObject );
return false;
}


}

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


// It's not possible to check which properties an animator
// is controlling, for now we update all properties in the
// controllers if we have an animator.
m_isAnimated = GetComponent<Animator>() != null;
}

protected override void OnDisable()
Expand Down Expand Up @@ -784,12 +799,8 @@ private void OnPreStepForwardUpdate()

SynchronizeNativeFramesWithAttachmentPair();

// It's not possible to check which properties an animator
// is controlling, for now we update all properties in the
// controllers if we have an animator. This could probably
// be a flag (IsAnimated).
var isAnimated = GetComponent<Animator>() != null;
if ( isAnimated ) {

if (m_isAnimated) {
var controllers = GetElementaryConstraintControllers();
for ( int i = 0; i < controllers.Length; ++i )
PropertySynchronizer.Synchronize( controllers[ i ] );
Expand Down
30 changes: 17 additions & 13 deletions AGXUnity/Rendering/DebugRenderManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ protected void Update()
}

private static bool m_isActiveForSynchronize = false;
private static bool IsActiveForSynchronize { get { return m_isActiveForSynchronize; } }
public static bool IsActiveForSynchronize { get { return m_isActiveForSynchronize; } }

private bool UpdateIsActiveForSynchronize()
{
Expand Down Expand Up @@ -371,20 +371,24 @@ private void OnSimulationPostStep( agxSDK.Simulation simulation )
if ( simulation == null )
return;

var gcs = simulation.getSpace().getGeometryContacts();
m_contactList.Clear();
m_contactList.Capacity = 4 * gcs.Count;
for ( int i = 0; i < gcs.Count; ++i ) {
var gc = gcs[ i ];
if ( !gc.isEnabled() )
continue;

for ( uint j = 0; j < gc.points().size(); ++j ) {
var p = gc.points().at( j );
if ( !p.enabled )
// Only collect data for contacts if they are enabled
if (m_renderContacts)
{
var gcs = simulation.getSpace().getGeometryContacts();
m_contactList.Clear();
m_contactList.Capacity = 4 * gcs.Count;
for ( int i = 0; i < gcs.Count; ++i ) {
var gc = gcs[ i ];
if ( !gc.isEnabled() )
continue;

m_contactList.Add( new ContactData() { Point = p.point.ToHandedVector3(), Normal = p.normal.ToHandedVector3() } );
for ( uint j = 0; j < gc.points().size(); ++j ) {
var p = gc.points().at( j );
if ( !p.enabled )
continue;

m_contactList.Add( new ContactData() { Point = p.point.ToHandedVector3(), Normal = p.normal.ToHandedVector3() } );
}
}
}
}
Expand Down
38 changes: 32 additions & 6 deletions AGXUnity/Rendering/SegmentSpawner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@ public class SegmentSpawner

[SerializeField]
private Material m_material = null;

// Use this instead of new every time we need one
private static Vector3 s_y_vec = new Vector3( 0, 1, 0 );

// Set this and use = operator instead of new every timestep
private Vector3 m_vec = new Vector3(-20, -20, -20);

public Material Material
{
get { return m_material ?? DefaultMaterial; }
Expand Down Expand Up @@ -129,21 +136,40 @@ public GameObject CreateSegment( Vector3 start, Vector3 end, float width, float
Transform main = instance.transform.GetChild( 1 );
Transform bottom = instance.transform.GetChild( 2 );

main.localScale = new Vector3( width, length, height );
top.localScale = bottom.localScale = new Vector3( width, width, height );
//main.localScale = new Vector3( width, length, height );
m_vec.Set(width, length, height);
main.localScale = m_vec;

//top.localScale = bottom.localScale = new Vector3( width, width, height );
//top.localScale = bottom.localScale = m_vec;
top.localScale = m_vec;
m_vec.Set(width, length, height);
bottom.localScale = new Vector3(width, width, height);


top.transform.localPosition = 0.5f * length * Vector3.up;
bottom.transform.localPosition = -0.5f * length * Vector3.up;
}
else {
Transform main = instance.transform.GetChild( 0 );
Transform top = instance.transform.GetChild( 1 );

main.localScale = new Vector3( width, length, height );
top.localScale = new Vector3( width, width, height );
top.transform.localPosition = new Vector3( 0, 0.5f * length, 0 );

// main.localScale = new Vector3( width, length, height );
// top.localScale = new Vector3( width, width, height );
// top.transform.localPosition = new Vector3( 0, 0.5f * length, 0 );

m_vec.Set(width, length, height);
top.localScale = main.localScale = m_vec;

//top.transform.localPosition = new Vector3(0, 0.5f * length, 0);

m_vec.Set(0, 0.5f * length, 0);
top.transform.localPosition = m_vec;

}

instance.transform.rotation = Quaternion.FromToRotation( new Vector3( 0, 1, 0 ), startToEnd );
instance.transform.rotation = Quaternion.FromToRotation( s_y_vec, startToEnd );
instance.transform.position = start + 0.5f * length * startToEnd;

return instance;
Expand Down
53 changes: 35 additions & 18 deletions AGXUnity/Rendering/WireRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ public class WireRenderer : ScriptComponent

[SerializeField]
private Material m_material = null;

private List<Vector3> m_positions;

public Material Material
{
get { return m_material ?? m_segmentSpawner.DefaultMaterial; }
Expand Down Expand Up @@ -107,36 +110,50 @@ private void Render( Wire wire )
return;
}

List<Vector3> positions = new List<Vector3>();
positions.Capacity = 256;
agxWire.RenderIterator it = wire.Native.getRenderBeginIterator();
if (m_positions == null)
{
m_positions = new List<Vector3>();
m_positions.Capacity = 256;
}

//UnityEngine.Profiling.Profiler.BeginSample("CollectingWirePoints");
m_positions.Clear();

agxWire.RenderIterator it = wire.Native.getRenderBeginIterator();
agxWire.RenderIterator endIt = wire.Native.getRenderEndIterator();
while ( !it.EqualWith( endIt ) ) {
positions.Add( it.get().getWorldPosition().ToHandedVector3() );
while (!it.EqualWith(endIt))
{
m_positions.Add(it.getWorldPosition().ToHandedVector3());
it.inc();
}

//UnityEngine.Profiling.Profiler.EndSample();
m_segmentSpawner.Begin();

try {
for ( int i = 0; i < positions.Count - 1; ++i ) {
Vector3 curr = positions[ i ];
Vector3 next = positions[ i + 1 ];
try
{
//UnityEngine.Profiling.Profiler.BeginSample("GeneratingSegments");
for (int i = 0; i < m_positions.Count - 1; ++i)
{
Vector3 curr = m_positions[i];
Vector3 next = m_positions[i + 1];
Vector3 currToNext = next - curr;
float distance = currToNext.magnitude;
currToNext /= distance;
int numSegments = Convert.ToInt32( distance * NumberOfSegmentsPerMeter + 0.5f );
float dl = distance / numSegments;
for ( int j = 0; j < numSegments; ++j ) {
float distance = currToNext.magnitude;
currToNext /= distance;
int numSegments = Convert.ToInt32(distance * NumberOfSegmentsPerMeter + 0.5f);
float dl = distance / numSegments;
for (int j = 0; j < numSegments; ++j)
{
next = curr + dl * currToNext;

m_segmentSpawner.CreateSegment( curr, next, wire.Radius );
m_segmentSpawner.CreateSegment(curr, next, wire.Radius);
curr = next;
}
}
//UnityEngine.Profiling.Profiler.EndSample();
}
catch ( System.Exception e ) {
Debug.LogException( e );
catch (System.Exception e)
{
Debug.LogException(e);
}

m_segmentSpawner.End();
Expand Down
9 changes: 8 additions & 1 deletion AGXUnity/RigidBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,14 @@ private void OnPostSynchronizeTransformsCallback()
SyncUnityTransform();
SyncProperties();

Rendering.DebugRenderManager.OnPostSynchronizeTransforms( this );
bool isInstanced = Rendering.DebugRenderManager.IsActiveForSynchronize;
bool enabled = false;

if (isInstanced)
enabled = Rendering.DebugRenderManager.Instance.isActiveAndEnabled;

if (enabled)
Rendering.DebugRenderManager.OnPostSynchronizeTransforms( this );
}
#endregion

Expand Down
Loading

0 comments on commit 40231cf

Please sign in to comment.