Giter Site home page Giter Site logo

realitykit-import-mp4's Introduction

RealityKit-import-mp4

RealityKit and SwiftUI

The first part

IMG_0158

The second part

Code file 1

import SwiftUI
@main
struct MyApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Code file 2

import SwiftUI
import RealityKit
import AVFoundation
struct ContentView: View {
    var body: some View {
        return ARViewContainer().edgesIgnoringSafeArea(.all)
    }
}
struct ARViewContainer: UIViewRepresentable {
    func makeUIView(context: Context) -> ARView {
        let arView = ARView(frame: .zero)
        let anchor = AnchorEntity(plane: .any)
        let dimensions: SIMD3<Float> = [1.2,0.04,0.8]
        let screenMesh = MeshResource.generatePlane(width: dimensions.x, depth: dimensions.z)
        let screenMat = SimpleMaterial(color: .black,roughness: 0.2, isMetallic: false)
        let scrennEntity = ModelEntity(mesh: screenMesh, materials: [screenMat])
        anchor.addChild(scrennEntity)
        arView.scene.addAnchor(anchor)
        arView.tapGesture()
        scrennEntity.generateCollisionShapes(recursive: true)
        arView.installGestures([.translation, .scale], for: scrennEntity)
        return arView
    }
    func updateUIView(_ uiView: ARView, context: Context) {}
}
extension ARView{
    func tapGesture() {
        let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTap(recognize:)))
        self.addGestureRecognizer(tapGestureRecognizer)
    }
    @objc func handleTap(recognize:UITapGestureRecognizer){
        let  tapLocation = recognize.location(in: self)
        if let entity  = self.entity(at: tapLocation) as? ModelEntity {
            let videoURL = Bundle.main.url(forResource: "TVShow", withExtension: "mp4")!
            let playerItem = AVPlayerItem(asset: AVAsset(url: videoURL))
            let player = AVPlayer(url: videoURL)
            entity.model?.materials = [VideoMaterial(avPlayer: player)]
            player.replaceCurrentItem(with: playerItem)
            player.play()
        }
    }
}

realitykit-import-mp4's People

Contributors

s-way520 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.