Skip to content

Implementing fixes for Unity 2021.3 LTS #4

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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
9 changes: 5 additions & 4 deletions Runtime/ChunkGraph/ChunkNode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -264,17 +264,18 @@ private void OnDrawGizmos()
var connectionsCount = connections.Count;
if (connectionsCount == 0)
return;
var points = new Vector3[2* connectionsCount];
var points = new Vector3[2 * connectionsCount];
int i = 0;
Gizmos.color = Color.blue;
foreach(var connectedNode in connections.Keys)
{
var otherCenterOfMass = connectedNode.Rigidbody.worldCenterOfMass;
points[i++] = centerOfMass;
points[i++] = otherCenterOfMass;
}

Gizmos.color = Color.blue;
Gizmos.DrawLineList(points);
//Draw gizmo line for connections
Gizmos.DrawLine(points[i], points[i++]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a problem with index increment in this loop. Calling i++ will already increment the index after the instruction is completed.

// Here i == 0
points[i++] = centerOfMass;
// Here i == 1
points[i++] = otherCenterOfMass;
// Here i == 2
Gizmos.DrawLine(points[i], points[i++]);

Here is the problem. We should link with a line points[0] and point[1], but index is already been incremented so we are probably skipping array elements. The following index management should be more robust.

points[i] = centerOfMass;  
points[i + 1] = otherCenterOfMass; 
Gizmos.DrawLine(points[i], points[i + 1]); 
i += 2;

By the way, I used DrawLineList since it's declared to be a more optimized method to draw multiple lines in a raw, so I think we should keep it for newer versions of Unity, and add this fix as an optional code with a condition at compile time checking for the Unity version:

#if UNITY_2023_1_OR_NEWER
// We can use the version with DrawLineList
#else
// We can use the one with simple DrawLine
#endif

}
}

#endregion
Expand Down
6 changes: 3 additions & 3 deletions Runtime/LibreFracture.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using UnityEngine;
using Unity.VisualScripting;
using NvBlast;


Expand Down Expand Up @@ -162,7 +161,7 @@ public static GameObject FinalizeFracturedPreview(GameObject fracturedObject, Fr
chunkDistancePreview.UpdatePreview(0);
GameObject.DestroyImmediate(chunkDistancePreview);
}
chunk.GetOrAddComponent<ChunkNode>();
chunk.gameObject.AddComponent<ChunkNode>();

CreateColliderForChunk(chunk.gameObject);
}
Expand All @@ -173,7 +172,8 @@ public static GameObject FinalizeFracturedPreview(GameObject fracturedObject, Fr
if(!fracturedObject.TryGetComponent<Collider>(out _))
fracturedObject.AddComponent<MeshCollider>();

var fracture = fracturedObject.GetOrAddComponent<ChunkGraphManager>();
var fracture = fracturedObject.GetComponent<ChunkGraphManager>();
if (fracture == null) fracture = fracturedObject.AddComponent<ChunkGraphManager>();
fracture.jointBreakForce = parameters.jointBreakForce;
fracture.totalMass = parameters.totalObjectMass;

Expand Down