ARKit平面可视化

ARKit plane visualization

我希望能够可视化我的 ARKit 应用检测到的平面。我该怎么做?

This is what I want to be able to do

使用 SceneKit 和 Obj-C 在 Xcode 中创建一个新的 AR 项目,然后将它们添加到 ViewController.m:

//as a class or global variable:
NSMapTable *planes;

//add to viewWillAppear:
configuration.planeDetection = ARPlaneDetectionHorizontal;

//to viewDidLoad:
planes = [NSMapTable mapTableWithKeyOptions:NSMapTableStrongMemory
                               valueOptions:NSMapTableWeakMemory];

//new functions: 
- (void)renderer:(id<SCNSceneRenderer>)renderer didAddNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor {
if( [anchor isKindOfClass:[ARPlaneAnchor class]] ){
    [planes setObject:anchor forKey:node];
    ARPlaneAnchor *pa = anchor;

    SCNNode *pn = [SCNNode node];
    [node addChildNode:pn];
    pn.geometry = [SCNPlane planeWithWidth:pa.extent.x height:pa.extent.z];
    SCNMaterial *m = [SCNMaterial material];
    m.emission.contents = UIColor.blueColor;
    m.transparency = 0.1;
    pn.geometry.materials = @[m];
    pn.position = SCNVector3Make(pa.center.x, -0.002, pa.center.z);
    pn.transform = SCNMatrix4MakeRotation(-M_PI / 2.0, 1, 0, 0);
  }
}

- (void)renderer:(id<SCNSceneRenderer>)renderer didUpdateNode:(SCNNode *)node forAnchor:(ARAnchor *)anchor {
if( [anchor isKindOfClass:[ARPlaneAnchor class]] ){
    [planes setObject:anchor forKey:node];

    ARPlaneAnchor *pa = anchor;
    SCNNode *pn = [node childNodes][0];
    SCNPlane *pg = pn.geometry;
    pg.width = pa.extent.x;
    pg.height = pa.extent.z;
    pn.position = SCNVector3Make(pa.center.x, -0.002, pa.center.z);
  }
}

- (void)renderer:(id<SCNSceneRenderer>)renderer didRemoveNode:(nonnull SCNNode *)node forAnchor:(nonnull ARAnchor *)anchor{
[planes removeObjectForKey:node];
}

你会看到半透明的平面,如果你觉得这样,给m.emission.contents一个纹理。

或者获取 Example App from Apple in Swift