Search code examples
rotationtransformcalayercatransform3d

Place CALayer in SubLayer Hierarchy


Im trying to make an advanced 3D cube with CALayers based on this project:

https://github.com/joericioppo/3D-Layers/tree/master

I have such CALayer hierarchy:

self.layer
|
|
 rootLayer (CALayer) <--- in this point the cube is rotated by changing the rootLayer.sublayerTransform property
    |
    |
    transrormLayer(CATransformLayer)
        |
        |
        frontSide (CALayer)
        backSide  (CALayer)
        ...
        bottomSide(CALayer)

I want to insert a CALayer as sublayer in self.layer in the same 3D "position" as one of the cube sides. My idea was to multiply its transform by all CALayer hierarchy transrforms like that:

testLayer.transform = CATransform3DConcat(rootLayer.sublayerTransform, testLayer.transform);
testLayer.transform = CATransform3DConcat(transformLayer.sublayerTransform, testLayer.transform);
testLayer.transform = CATransform3DConcat(bottomSide.transform, testLayer.transform);

I recieve only crap - the layer is not placed in the same 3D "position" as bottomSide - it simply disappears. But placing the layer in frontSide or backSide is successful(only this 2 sides). Can someone tell me what im doing wrong?


Solution

  • I deleted transrormLayer. And changed the operation order.

    CATransform3D t = CATransform3DIdentity;
    t = CATransform3DConcat(bottomSide.transform, t); // cube side position
    t = CATransform3DConcat(rootLayer.sublayerTransform, t); // cube rotation and perspective
    testLayer.transform = t;
    

    Now it works fine.