Creating and replaying snapshots of 2000+ Transforms

Hi,

I need to create snapshots of a complex Transform hierarchy. About 2000 Transform components. After I changed the positions/orientations/scales of some of them, I want to save a snapshot. Then I want to scrub through a timeline of snapshots with a slider, and I need to be able to change the order of the snapshots.

The problem is that setting the transforms of 2000 objects takes a lot of time. Saving seems to work instantly.

Does anyone have an idea for an efficient way for making and replaying snapshots?

Thanks in advance,

Wavy

Yeah use Unity 2017, has it built in.

How are you setting the restored transform data?

Basically, writing into Transform.localPosition, Transform.localRotation and Transform.localScale should be faster than writing into Transform.position/rotation.

Is that also true for things at top level in the hierarchy? Or does that get optimized automatically?

My suggestion was a guess which is very likely to be a correct. I haven’t verified it.

There shouldn’t be any noticeable overhead for root transforms when accessing Transform.position comapred to accessing Transform.localPosition.

Basically, if you write into “position”, you’re specifying coordinates in world space, and parent transform needs to be known. Then position/rotation will have to be transformed into parent space. This will have some overhead (because you’ll need either multiplication by inverted matrix or an equivalent operation). If the transform is a root transform, there’s no parent, and no need to transform anything.

Also see: https://docs.unity3d.com/ScriptReference/Transform-hasChanged.html

2 Likes

Thanks all!

I’m using LINQ to filter those transforms that are different to the previous transforms. That speeds it up a lot.

Traditional advice is that a hand crafted loop can outperform LINQ. Normally I wouldn’t bother to mention it. But since you are having performance issues, its worth considering.

Yeah, I was going to say, this is like the fist time I heard “speeds up” in t he context of LINQ doing anything.

Its the filtering that’s producing the speed up. The OP is now doing the work on 100 transforms instead of 2000. Which is a good optimization.

Making the filter more efficient might be useful, or it may be a waste of time. Its entirely possible the performance gain is minimal compared to not using a filter at all. But it is something worth checking in the profiler.

I also had rigidbodies attached to maybe 10 percent of the GameObjects, all set to isKinematic = true. After I removed them, it becomes even faster.