SceneKit - SCNText centering incorrectly

前端 未结 4 1596
醉话见心
醉话见心 2020-12-31 23:13

I have tried to make a text string (SCNText) fit inside a box (SCNBox) in the code below. The size of the box looks correct but the text is not in the right center of the bo

4条回答
  •  天命终不由人
    2020-12-31 23:57

    The difference of SCNText from other geometries is that SCNText origin point positioned at bottom left corner. In other geometries, it is a bottom center.

    To fix text position in parent node you can set its pivotPoint.x to half of width:

    SCNVector3 min, max;
    [textNode getBoundingBoxMin:&min max:&max];
    textNode.pivot = SCNMatrix4MakeTranslation((max.x - min.x) / 2, 0, 0);
    

    To fix subnodes position, you should set their position to half of width plus min:

    SCNVector3 min, max;
    [textNode getBoundingBoxMin:&min max:&max];
    subnode.position = SCNVector3Make((max.x - min.x) / 2 + min.x, (max.y - min.y) / 2 + min.y, 0);
    

提交回复
热议问题