2022年2月11日 星期五

RealityKit 判斷手指觸摸物件

  1. 用Reality Composer編輯物件『屬性』,勾選『物理效果』『加入』
  2. 將ARFrame.capturedImage轉成CVPixelBuffer

    func session(_ session: ARSession, didUpdate frame: ARFrame) {

            guard let arView = arView else {return}

            // Do not enqueue other buffers for processing while another Vision task is still running.

            // The camera stream has only a finite amount of buffers available; holding too many buffers for analysis would starve the camera.

            guard currentBuffer == nil, case .normal = frame.camera.trackingState else {

                return

            }

            // Retain the image buffer for Vision processing.

            let currentBuffer = frame.capturedImage

    }

  3. 利用Vision偵測食指

    let requestHandler = VNImageRequestHandler(cvPixelBuffer: currentBuffer, orientation: orientation)

    visionQueue.async {

            do {

                    // Perform VNDetectHumanHandPoseRequest

                    try requestHandler.perform([self.handPoseRequest])

                    // Continue only when a hand was detected in the frame.

                    // Since we set the maximumHandCount property of the request to 1, there will be at most one observation.

                    guard let observation = self.handPoseRequest.results?.first else {

                        return

                    }

                    // Get points for index finger.

                    let indexFingerPoints = try observation.recognizedPoints(.indexFinger)

                    // Look for tip points.

                    guard let indexTipPoint = indexFingerPoints[.indexTip] else {

                        return

                    }

                    // Ignore low confidence points.

                    guard indexTipPoint.confidence > 0.3 else {

                        return

                    }

                    

                    

            } catch {

                    print("Error: Vision request failed with error \"\(error)\"")

            }

    }

  4. 將Vision的座標轉成ARView的座標

    // Convert points from Vision coordinates to ARView coordinates.

    let point = indexTipPoint.location

    let tip = CGPoint(x: point.x * width, y: (1 - point.y) * height)


sample code

2022年2月10日 星期四

RealityKit 判斷物件點擊方法2

  1. 用Reality Composer編輯物件『屬性』,勾選『物理效果』『加入』
  2. 替arView加上手勢功能

    arView.addGestureRecognizer(

          UITapGestureRecognizer(

                target: xxxx,

                action: #selector(XXXX.handleTap(recognizer:))

        )

    )


  3. @objc func handleTap(recognizer: UITapGestureRecognizer) {

            let tapLocation = recognizer.location(in: arView)

            let entity = arView.entity(at: tapLocation)

            guard entity == boxAnchor?.steelBox else { return }

            // entity 即點擊物件

    }

RealityKit 判斷物件點擊方法1

 直接用Reality Composer編輯物件行為

  1. 選取物件
  2. 顯示行為(⌥⌘B)
  3. 加入行為『點按與反轉』

2021年8月25日 星期三

2019年5月2日 星期四

在Heroku上發佈MochiWeb

注意:
  1. 官方https://github.com/heroku/heroku-buildpack-erlang.git過舊(OTP和cebar),所以需要替代方案
  2. Heroku的port是亂數產生,所以需要動態拿到port
參考

2018年7月20日 星期五

2018年6月7日 星期四

iOS hot reloading

1. 安裝 InjectionIII 並啟動
2. 打開 XCode 和 專案
3. 在appDelegate加入

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.z
        #if DEBUG
        #if targetEnvironment(simulator)
        #if os(iOS)
        Bundle(path: "/Applications/InjectionIII.app/Contents/Resources/iOSInjection.bundle")?.load()
        #elseif os(tvOS)
        Bundle(path: "/Applications/InjectionIII.app/Contents/Resources/tvOSInjection.bundle")?.load()
        #endif
        #elseif os(macOS)
        Bundle(path: "/Applications/InjectionIII.app/Contents/Resources/macOSInjection.bundle")?.load()
        #endif
        #endif
        return true
    }


PS.
已經秀出來的view並未改變。但是,新present出的view確實已經改變

如需要改變已秀出來的view,需在每個controller上實作
@objc func injected() {
    // 可馬上更新的code

}

建議的懶人法

    @objc func injected() {
        // 移除全部vc/view
        if self.childViewControllers.count > 0{
            let viewControllers:[UIViewController] = self.childViewControllers
            for viewContoller in viewControllers{
                viewContoller.willMove(toParentViewController: nil)
                viewContoller.view.removeFromSuperview()
                viewContoller.removeFromParentViewController()
            }
        }
        
        configureView()
    }
    
    func configureView() {
        
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        configureView()
    }