Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bad performance using CATransform3D

I'm trying to create a 3D user interface using CALayer 3d transforms. I have figured out the basic technique for achieving this, but my real-time performance is pretty bad. Specifically, the user should be able to "pan" the interface "scene" interactively.

In the following snippet, "model" is the layer I'm trying to animate. "camera" is a CATransform3D matrix that I'm continuously updating by applying touch translations.

The approach works, but panning is very sluggish. If I uncomment the part with the CGAffineTransform, I'm getting fast and responsive panning - but I'm losing the change in perspective that should occur when panning.

- (void)didPan:(UIPanGestureRecognizer*)pan
{
    if (pan.state==UIGestureRecognizerStateChanged) 
    {
        CGPoint p = [pan translationInView:self.view.window];

        camera = CATransform3DTranslate(camera, p.y, 0, -p.x);
        model.transform = CATransform3DConcat(camera, modelView);

        // CGAffineTransform tA = self.view.transform;
        // tA = CGAffineTransformTranslate(tA, p.x, p.y);
        // self.view.transform = tA;

        [pan setTranslation:CGPointZero inView:self.view.window];
    }
}

How can I improve rendering performance?

like image 998
karstux Avatar asked May 04 '26 14:05

karstux


1 Answers

Okay, I solved it myself: the problem were implicit animations. Every model.transform = ... call triggered an implicit animation, resulting in a very sluggish behavior. Disabling implicit animations for the transform key, when creating the "model" layer, solved the problem:

model.actions = [NSDictionary dictionaryWithObjectsAndKeys:
                   [NSNull null], @"transform",
                   nil];

Now everything is snappy and fast.

like image 145
karstux Avatar answered May 07 '26 16:05

karstux