import UIKit
import RealityKit
import ARKit
class ViewController: UIViewController, ARSessionDelegate {
var arView: ARView!
override func viewDidLoad() {
super.viewDidLoad()
arView = ARView(frame: .zero)
let config = ARImageTrackingConfiguration()
let trackedImagesLib01 = ARReferenceImage(UIImage(named: "anchor_img_01")!.cgImage!, orientation: .up, physicalWidth: 0.05)
// 通过命名可以在获取锚点时确定是什么内容
trackedImagesLib01.name = "fox"
let trackedImagesLib02 = ARReferenceImage(UIImage(named: "anchor_img_02")!.cgImage!, orientation: .up, physicalWidth: 0.05)
trackedImagesLib02.name = "toy_biplane"
config.trackingImages = [trackedImagesLib01, trackedImagesLib02]
config.maximumNumberOfTrackedImages = 2
self.view.addSubview(arView)
arView.session.delegate = self
arView.session.run(config, options: [])
}
override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)
arView.frame = self.view.frame
}
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(animated)
arView.session.pause()
}
func session(_ session: ARSession, didAdd anchors: [ARAnchor]) {
guard let imageAnchor = anchors[0] as? ARImageAnchor else {
return
}
// 图片识别加载模型
DispatchQueue.main.async {
do {
let myModelEntity = try ModelEntity.load(named: imageAnchor.name!)
let objectAnchorEntity = AnchorEntity(anchor:imageAnchor)
objectAnchorEntity.addChild(myModelEntity)
objectAnchorEntity.scale = [0.1, 0.1, 0.1]
self.arView.scene.addAnchor(objectAnchorEntity)
myModelEntity.playAnimation(myModelEntity.availableAnimations[0].repeat())
} catch {
print("无法加载模型")
}
}
}
}
ARKit实现2D图片识别跟踪
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 1安装 npm install qrcodejs2 --save npm install --save html2...
- 上一篇实现了图片CNN单标签分类(猫狗图片分类任务)(地址://www.greatytc.com/p/4...
- 小程序实现长按图片弹出保存图片、发送给朋友、识别图中码菜单 加上show-menu-by-longpress 为t...
- 我们在平常制作2d小游戏的时候一般有一个分数的显示。对于用UGUI的童鞋来说,我们一般用text这个组件来实时...
- 用UIGraphics进行2D图像渲染 不要用UIGraphicsBeginImageContext(size);...