AnchorEntity方便init(plane: classification: minimumBounds)使用

AnchorEntity convenience init(plane: classification: minimumBounds) use

// add in viewWillAppear
let anchor = AnchorEntity(plane: [.horizontal, .vertical], 
                 classification: [.any], 
                  minimumBounds: [0.01, 0.01])
anchor.name = "loongAnchor"
let boxModel = QDVirtualEntity.box(size: 0.1, corner: 0, color: .red)
anchor.addChild(boxModel)
arView.scene.anchors.append(anchor)

QDVirtualEntity 函数:

public static func box(size: Float, corner: Float, color: UIColor = .orange) -> ModelEntity {
    let entity = ModelEntity(mesh: .generateBox(size: size, cornerRadius: corner), 
                        materials: [SimpleMaterial(color: color, isMetallic: false)])
    entity.position.y = size / 2.0
    return entity
}

运行后,我找不到添加的框了。这是为什么?这个方法是如何工作的。

您的代码运行良好。唯一的问题可能是 minimumBounds 设置为 [0.01, 0.01]。太小了。将其设置为 [0.2, 0.2],这样您检测到的平面将具有稳健的尺寸。还可以较慢地跟踪周围环境,一旦检测到飞机,您就会在房间里看到您的小盒子。

class ViewController: UIViewController {
    
    @IBOutlet var arView: ARView!
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)

        let anchor = AnchorEntity(plane: [.horizontal, .vertical], 
                         classification: [.any], 
                          minimumBounds: [0.2, 0.2])
        let boxModel = QDVirtualEntity.box(size: 0.1, corner: 0, color: .green)
        anchor.addChild(boxModel)
        arView.scene.anchors.append(anchor)
    }
}

class QDVirtualEntity {
    
    public static func box(size: Float, corner: Float, color: UIColor) -> ModelEntity {
        let entity = ModelEntity(mesh: .generateBox(size: size, cornerRadius: corner),
                            materials: [SimpleMaterial(color: color,
                                                  isMetallic: false)])
        entity.position.y = size / 2.0
        return entity
    }
}

1:可以设置(默认)

 arView.automaticallyConfigureSession = true

如上添加代码后,检测到飞机时会自动添加

2:如果你set automaticallyConfigureSession = false

/// Defaults to ARPlaneDetectionNone.
configuration.planeDetection = .horizontal  or .vertical