Skip to content

Improve performance #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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
23 changes: 22 additions & 1 deletion Plugins/BulletUnity/BulletSharp/SoftBody/AlignedNodeArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,12 @@ public void Reset()
public class AlignedNodeArray : IList<Node>
{
private IntPtr _native;
private Dictionary<long, Node> _nodeObjects;

internal AlignedNodeArray(IntPtr native)
{
_native = native;
_nodeObjects = new Dictionary<long, Node>();
}

public int IndexOf(Node item)
Expand All @@ -103,14 +105,33 @@ public Node this[int index]
{
throw new ArgumentOutOfRangeException("index");
}
return new Node(btAlignedSoftBodyNodeArray_at(_native, index));

IntPtr nodePtr = btAlignedSoftBodyNodeArray_at(_native, index);
long nodePtrInt = nodePtr.ToInt64();
Node node;
if (!_nodeObjects.TryGetValue(nodePtrInt, out node))
{
node = new Node(nodePtr);
_nodeObjects.Add(nodePtrInt, node);
}

return node;
}
set
{
throw new NotImplementedException();
}
}

public IntPtr GetNodePointer(int index)
{
if ((uint)index >= (uint)Count)
{
throw new ArgumentOutOfRangeException("index");
}
return btAlignedSoftBodyNodeArray_at(_native, index);
}

public void Add(Node item)
{
btAlignedSoftBodyNodeArray_push_back(_native, item._native);
Expand Down
4 changes: 2 additions & 2 deletions Plugins/BulletUnity/BulletSharp/SoftBody/SoftBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2335,13 +2335,13 @@ public Vector3 Velocity
[DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity]
static extern IntPtr btSoftBody_Node_getLeaf(IntPtr obj);
[DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity]
static extern void btSoftBody_Node_getN(IntPtr obj, [Out] out Vector3 value);
public static extern void btSoftBody_Node_getN(IntPtr obj, [Out] out Vector3 value);
[DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity]
static extern void btSoftBody_Node_getQ(IntPtr obj, [Out] out Vector3 value);
[DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity]
static extern void btSoftBody_Node_getV(IntPtr obj, [Out] out Vector3 value);
[DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity]
static extern void btSoftBody_Node_getX(IntPtr obj, [Out] out Vector3 value);
public static extern void btSoftBody_Node_getX(IntPtr obj, [Out] out Vector3 value);
[DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity]
static extern void btSoftBody_Node_setArea(IntPtr obj, float value);
[DllImport(Native.Dll, CallingConvention = Native.Conv), SuppressUnmanagedCodeSecurity]
Expand Down
18 changes: 12 additions & 6 deletions Scripts/SoftBody/BSoftBody.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,24 @@ public void DumpDataFromBullet()
if (isInWorld)
{
SoftBody m_BSoftBody = (SoftBody)m_collisionObject;
if (verts.Length != m_BSoftBody.Nodes.Count)
int nodeCount = m_BSoftBody.Nodes.Count;
if (verts.Length != nodeCount)
{
verts = new Vector3[m_BSoftBody.Nodes.Count];
verts = new Vector3[nodeCount];
}
if (norms.Length != verts.Length)
{
norms = new Vector3[m_BSoftBody.Nodes.Count];
norms = new Vector3[nodeCount];
}
for (int i = 0; i < m_BSoftBody.Nodes.Count; i++)
for (int i = 0; i < nodeCount; i++)
{
verts[i] = m_BSoftBody.Nodes[i].Position.ToUnity();
norms[i] = m_BSoftBody.Nodes[i].Normal.ToUnity();
BulletSharp.Math.Vector3 pos;
Node.btSoftBody_Node_getX(m_BSoftBody.Nodes.GetNodePointer(i), out pos);
verts[i] = pos.ToUnity();

BulletSharp.Math.Vector3 normal;
Node.btSoftBody_Node_getN(m_BSoftBody.Nodes.GetNodePointer(i), out normal);
norms[i] = normal.ToUnity();
}
}
}
Expand Down