From 334e2b2f500dec35181cb2663054480f3326df83 Mon Sep 17 00:00:00 2001 From: smitdylan2001 Date: Sun, 11 Dec 2022 16:17:41 +0100 Subject: [PATCH] Minor performance improvements Done by using SetPositionAndRotation, TryGetComponent, removing duplicate call, and marking BinSort as static --- CHANGELOG.md | 4 ++++ Runtime/Scripts/Fracture.cs | 3 +-- Runtime/Scripts/Fragment/UnfreezeFragment.cs | 1 - Runtime/Scripts/Prefracture.cs | 3 +-- Runtime/Scripts/Slice.cs | 3 +-- Runtime/Scripts/Slicers/PlaneSlicer.cs | 5 ++--- Runtime/Scripts/Utilities/BinSort.cs | 2 +- 7 files changed, 10 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1e2662c..77c8b20 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,9 @@ # Changelog +## 1.0.3 +### Features +- Improved CPU performance + ## 1.0.2 ### Features - Added `onFracture` to editor diff --git a/Runtime/Scripts/Fracture.cs b/Runtime/Scripts/Fracture.cs index 787de24..c27407f 100644 --- a/Runtime/Scripts/Fracture.cs +++ b/Runtime/Scripts/Fracture.cs @@ -134,8 +134,7 @@ private void ComputeFracture() this.fragmentRoot.transform.SetParent(this.transform.parent); // Each fragment will handle its own scale - this.fragmentRoot.transform.position = this.transform.position; - this.fragmentRoot.transform.rotation = this.transform.rotation; + this.fragmentRoot.transform.SetPositionAndRotation(this.transform.position, this.transform.rotation); this.fragmentRoot.transform.localScale = Vector3.one; } diff --git a/Runtime/Scripts/Fragment/UnfreezeFragment.cs b/Runtime/Scripts/Fragment/UnfreezeFragment.cs index 41dda43..5dc0f79 100644 --- a/Runtime/Scripts/Fragment/UnfreezeFragment.cs +++ b/Runtime/Scripts/Fragment/UnfreezeFragment.cs @@ -48,7 +48,6 @@ void OnTriggerEnter(Collider collider) return; } - bool tagAllowed = triggerOptions.IsTagAllowed(collider.gameObject.tag); if (!triggerOptions.filterCollisionsByTag || triggerOptions.IsTagAllowed(collider.gameObject.tag)) { this.Unfreeze(); diff --git a/Runtime/Scripts/Prefracture.cs b/Runtime/Scripts/Prefracture.cs index 62371a2..4b70ba0 100644 --- a/Runtime/Scripts/Prefracture.cs +++ b/Runtime/Scripts/Prefracture.cs @@ -55,8 +55,7 @@ public void ComputeFracture() this.fragmentRoot.transform.SetParent(this.transform.parent); // Each fragment will handle its own scale - this.fragmentRoot.transform.position = this.transform.position; - this.fragmentRoot.transform.rotation = this.transform.rotation; + this.fragmentRoot.transform.SetPositionAndRotation(this.transform.position, this.transform.rotation); this.fragmentRoot.transform.localScale = Vector3.one; } diff --git a/Runtime/Scripts/Slice.cs b/Runtime/Scripts/Slice.cs index 70586d8..c303f14 100644 --- a/Runtime/Scripts/Slice.cs +++ b/Runtime/Scripts/Slice.cs @@ -38,8 +38,7 @@ public void ComputeSlice(Vector3 sliceNormalWorld, Vector3 sliceOriginWorld) this.fragmentRoot.transform.SetParent(this.transform.parent); // Each fragment will handle its own scale - this.fragmentRoot.transform.position = this.transform.position; - this.fragmentRoot.transform.rotation = this.transform.rotation; + this.fragmentRoot.transform.SetPositionAndRotation(this.transform.position, this.transform.rotation); this.fragmentRoot.transform.localScale = Vector3.one; } diff --git a/Runtime/Scripts/Slicers/PlaneSlicer.cs b/Runtime/Scripts/Slicers/PlaneSlicer.cs index a038530..8d76dce 100644 --- a/Runtime/Scripts/Slicers/PlaneSlicer.cs +++ b/Runtime/Scripts/Slicers/PlaneSlicer.cs @@ -53,9 +53,8 @@ void Update() foreach(RaycastHit hit in hits) { var obj = hit.collider.gameObject; - var sliceObj = obj.GetComponent(); - - if (sliceObj != null) + + if (obj.TryGetComponent(out var sliceObj)) { sliceObj.GetComponent()?.material.SetVector("CutPlaneOrigin", Vector3.positiveInfinity); sliceObj.ComputeSlice(this.transform.up, this.transform.position); diff --git a/Runtime/Scripts/Utilities/BinSort.cs b/Runtime/Scripts/Utilities/BinSort.cs index b963b59..b86ae68 100644 --- a/Runtime/Scripts/Utilities/BinSort.cs +++ b/Runtime/Scripts/Utilities/BinSort.cs @@ -23,7 +23,7 @@ public interface IBinSortable /// |_____|_____|_____| /// /// -public class BinSort +public static class BinSort { /// /// Computes the bin number for the set of grid coordinates