Last active
June 6, 2022 05:46
-
-
Save Heilum/cc85c83c793595ddf7fbe7af87c1ce58 to your computer and use it in GitHub Desktop.
Play video using SKVideoNode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private func resolutionForVideo(url: URL) -> CGSize? { | |
guard let track = AVURLAsset(url: url).tracks(withMediaType: AVMediaType.video).first else { return nil } | |
let size = track.naturalSize.applying(track.preferredTransform) | |
return CGSize(width: abs(size.width), height: abs(size.height)) | |
} | |
@objc func tapped(recognizer :UIGestureRecognizer) { | |
guard let currentFrame = self.sceneView.session.currentFrame else { | |
return | |
} | |
if let tvPlaneNode = self.tvPlaneNode{ | |
tvPlaneNode.removeFromParentNode(); | |
} | |
let url = URL(string: self.videoURL)! | |
let size = resolutionForVideo(url: url)!; | |
let videoNode = SKVideoNode(url: url) | |
videoNode.play() | |
//let size = CGSize(width: 452, height: 256); | |
let skScene = SKScene(size:size) | |
skScene.addChild(videoNode) | |
videoNode.position = CGPoint(x: skScene.size.width/2, y: skScene.size.height/2) | |
videoNode.size = skScene.size | |
let tvPlane = SCNPlane(width: 1.0, height: 1.0 * size.height / size.width) | |
tvPlane.firstMaterial?.diffuse.contents = skScene | |
tvPlane.firstMaterial?.isDoubleSided = true | |
let tvPlaneNode = SCNNode(geometry: tvPlane) | |
//放置摄像头前1m | |
var translation = matrix_identity_float4x4 | |
translation.columns.3.z = -1.0 | |
tvPlaneNode.simdTransform = matrix_multiply(currentFrame.camera.transform, translation) | |
//绕z轴旋转-90度 | |
var zAxis = SIMD3<Float>(0, 0, 1) | |
var rotationMatrix = float4x4(simd_quatf(angle: -Float(Double.pi) / 2, axis: zAxis)); | |
tvPlaneNode.simdTransform *= rotationMatrix | |
//左右镜像 | |
zAxis = SIMD3<Float>(0, 1, 0) | |
rotationMatrix = float4x4(simd_quatf(angle: Float(Double.pi), axis: zAxis)); | |
tvPlaneNode.simdTransform *= rotationMatrix | |
//tvPlaneNode.eulerAngles = SCNVector3(0,0,0) | |
self.sceneView.scene.rootNode.addChildNode(tvPlaneNode) | |
self.tvPlaneNode = tvPlaneNode; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment