Someone asked how I made the performance gains from my previous post, so Iâll share it here for others.
Hope this helps.
WindZone
In PhysicalBoneManager.cs
Thereâs a GetComponent<WindZone>
in the update loop. Move to it to start. Itâs not necessary to get component every frame.
== null checks
The biggest performance gain can be made here.
In PhysicalBone.cs, PhysicalBoneBase.cs & PhysicalBoneCollider.cs.
Replace every == null
check with:
bool isNull = ReferenceEquals(someVariable, null);
Manual vector3 operations
In PhysicalBone.cs, PhysicalBoneBase.cs & PhysicalBoneCollider.cs.
The most tedious, but you can also gain quite a number of fps here.
For every addition, multiplication etc2
Instead of vectorA + vectorB
do
vectorA.x += vectorB.x;
vectorA.y += vectorB.y;
vectorA.z += vectorB.z;
For hundreds of colliders
This changes the usage workflow, so thinks if itâs really necessary.
PhysicalBoneCollider.cs loops through all the colliders, at all times. So youâre wasting CPU time here on bones that doesnât even collide.
I made every child in Bone Attributes have its own collider. Use sphere collider because itâs what PhysicalBoneCollider uses.
I also made PhysicalBoneCollider have Unity colliders + kinematic rigidbody + no gravity.
Do a collision check in OnTriggerStay & OnTriggerExit for the custom unity collider.
In the for loops, I made sure that the PhysicalBoneCollider only checks if any custom bone collider is colliding with it based on layer specified.
Now you can have hundreds of colliders with no performance loss.
Editor Performance
PhysicalBone.cs uses custom inspector for BoneAttributes. Itâs not performant as in every redraw if does a whole lot of loops & variable referencing that is very resource heavy. (I think it uses .Net reflection? Might be wrong.)
I moved BoneAttributes into a custom class and just uses the default drawing behavior for the inspector.
Now your editor wonât freeze every time. You can also do multiple component edits like this, very useful if you have hundreds of bones and you need to freeze certain child.
Referencing x4000 review on the asset store:
Implementing my fixes above, I can manage to just use a single PhysicalBoneManager.
In Start, do a find components for PhysicalBone & global PhysicalBoneCollider.
Voila, all bones share the same colliders with no performance loss.