简体   繁体   English

如何在Scenekit中的3D模型上添加单词(注释)?

[英]How to add words(comment) on 3D model in Scenekit ?

I showed a 3D model in my app with Scenekit. 我使用Scenekit在我的应用程序中显示了3D模型。 The model could rotate, scale, move by gesture. 该模型可以通过手势旋转,缩放,移动。 Now I need to add some comments on screen to explain every part of the model is what(such as a cardiac module, includes cardiac muscle, blood vessel...). 现在,我需要在屏幕上添加一些注释,以解释模型的每个部分是什么(例如心脏模块,包括心肌,血管...)。 The comments should always follow the part of model, when the model transform, and word's size will not change, always face to User . 注释应始终跟随模型的一部分,当模型转换时, 单词的大小不会改变,始终面对User But I do not know how to achieved that. 但是我不知道该如何实现。

My idea is to make 3D world coordinate to screen coordinate , and add UILabels on SCNView, when model transforms, change UILabels frame. 我的想法是使3D世界坐标变为屏幕坐标 ,并在SCNView上添加UILabels,在模型转换时,更改UILabels框架。 But I don't know how to make coordinate convert. 但是我不知道如何进行坐标转换。 Of course, it could be a better way , I don't know. 当然,这可能是更好的方法 ,我不知道。 Thanks. 谢谢。

This snippet is taken from an ARKit project, but with respect to having a text label attached to an SCNNode and always facing the user, this is what you need - 该摘录摘自ARKit项目,但是相对于将文本标签附加到SCNNode并始终面向用户而言,这就是您需要的-

func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
        guard let featureAnchor = anchor as? FeatureAnchor else {
            return nil
        }
        let feature = featureAnchor.feature

        let billboardConstraint = SCNBillboardConstraint()
        billboardConstraint.freeAxes = SCNBillboardAxis.Y

        let sphere = SCNSphere(radius: Global.dotRadius)
        sphere.firstMaterial?.transparency = Global.annotationTransparency
        sphere.firstMaterial?.diffuse.contents = UIColor.yellow
        sphere.firstMaterial?.specular.contents = UIColor.white
        let node = SCNNode(geometry: sphere)
        node.constraints = [billboardConstraint]

        let text = SCNText(string: feature.description, extrusionDepth: Global.textDepth)
        text.chamferRadius = Global.textDepth
        text.firstMaterial?.transparency = Global.annotationTransparency
        text.firstMaterial?.diffuse.contents = UIColor.green
        text.firstMaterial?.specular.contents = UIColor.white
        text.firstMaterial?.isDoubleSided = true
        text.font = Global.textFont
        text.alignmentMode = kCAAlignmentCenter

        let (min, max) = text.boundingBox
        let dx: Float = (max.x - min.x) / 2.0 // x = center
        let dy: Float = min.y // y = bottom
        let dz: Float = Float(Global.textDepth) / 2.0 // z = center

        let textNode = SCNNode(geometry: text)
        textNode.pivot = SCNMatrix4MakeTranslation(dx, dy, dz)
        textNode.scale = SCNVector3Make(Global.textScale, Global.textScale, Global.textScale)

        node.addChildNode(textNode)

        return node
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM