Bad performance using CATransform3D

I am trying to create a 3D user interface using CALayer 3d transforms. I figured out a basic technique to achieve this, but my real-time performance is pretty poor. In particular, the user should be able to "pan" the "scene" interface interactively.

In the next snippet, "model" is the layer I'm trying to animate. "camera" is a CATransform3D matrix that I am constantly updating with touch translations.

The approach works, but panning is very sluggish. If I uncomment the CGAffineTransform part, I get a fast and responsive panning, but I lose the perspective change that needs to happen 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 to improve rendering performance?

+3


source to share


2 answers


Ok, I solved it myself: the problem was the implicit animation. Each call to model.transform = ... caused an implicit animation, which resulted in very slow 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 fast and fast.

+4


source


I also figured out a cleaner way to apply camera transform, for example:

camera = CATransform3DTranslate(camera, p.x, p.y, 0);  
mainLayer.sublayerTransform = CATransform3DConcat(camera, CATransform3DPerspective);

      

where mainLayer

is my viewport and CATransform3DPerspective

is a CATransform3D constant defined like this:



CATransform3D const CATransform3DPerspective = {
    1.0, 0.0, 0.0, 0.0,
    0.0, 1.0, 0.0, 0.0,
    0.0, 0.0, 1.0, -1.0/500.0,
    0.0, 0.0, 0.0, 1.0
};

      

This way I no longer need to disable implicit animations - it just works.

+3


source







All Articles