3d对象的静态图像(Swift AR)

时间:2018-01-29 16:03:01

标签: swift xcode scenekit augmented-reality

我只想知道如何让我的SceneKit对象像普通的2D图像一样静止,而不是让它像球体一样形状。我对AR很新,所以我真的不知道如何编程。谢谢!

import UIKit
import SceneKit

class Ship: SCNNode {
    let radius : CGFloat = 0.11
    override init() {
        super.init()
        self.geometry = SCNSphere(radius: radius)
        self.physicsBody = SCNPhysicsBody(type: .dynamic, shape: nil)
        self.physicsBody?.isAffectedByGravity = false

        self.physicsBody?.categoryBitMask = CollisionCategory.ship.rawValue
        self.physicsBody?.contactTestBitMask = CollisionCategory.bullets.rawValue

        // add texture
        let material = SCNMaterial()
        material.diffuse.contents = UIImage(named: "innierevised")
        self.geometry?.materials  = [material, material, material, material, material, material]
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

}

enter image description here enter image description here

1 个答案:

答案 0 :(得分:2)

您必须创建一个SCNPlane并使用其材质属性为其指定图像:

// Compute Material
let material = SCNMaterial()
material.isDoubleSided = false
material.diffuse.contents = UIImage(named: "innierevised") 
material.diffuse.contentsTransform = 
SCNMatrix4Translate(SCNMatrix4MakeScale(-1, 1, 1), 1, 0, 0)
// This is if your image is flipped vertically (as SceneKit coordinate system is different from UIKit one)

然后:

// Create Plane
let newPoi = SCNPlane(width: radius, height: radius)
newPoi.materials = [material]
let newPoiNode = SCNNode(geometry: newPoi)
newPoiNode.position = // your SCNVector3 expected position
newPoiNode.constraints = [SCNBillboardConstraint()] // Look towards camera