簡體   English   中英

如何在Scenekit中的3D模型上添加單詞(注釋)?

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

我使用Scenekit在我的應用程序中顯示了3D模型。 該模型可以通過手勢旋轉,縮放,移動。 現在,我需要在屏幕上添加一些注釋,以解釋模型的每個部分是什么(例如心臟模塊,包括心肌,血管...)。 注釋應始終跟隨模型的一部分,當模型轉換時, 單詞的大小不會改變,始終面對User 但是我不知道該如何實現。

我的想法是使3D世界坐標變為屏幕坐標 ,並在SCNView上添加UILabels,在模型轉換時,更改UILabels框架。 但是我不知道如何進行坐標轉換。 當然,這可能是更好的方法 ,我不知道。 謝謝。

該摘錄摘自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