【发布时间】:2015-12-24 00:39:06
【问题描述】:
我正在尝试使用 Scenekit 将视频作为纹理映射到 VR 项目的原始圆柱体:嵌入在 SKScene 中的 SKVideoNode 作为 SceneKit SCNTube 对象的纹理,但我无法显示视频就像静止图像一样。下面的 PLayground 代码应该生成映射到圆柱体的移动视频,但映射不起作用:
编辑:在列表末尾添加单行进行修复。下面的代码应该可以工作
import UIKit
import SceneKit // for 3D mapping
import SpriteKit // for SKVideoNode
import QuartzCore // for basic animation
import XCPlayground // for live preview
import AVFoundation // for video playback engine
// create a scene view with an empty scene
var sceneView = SCNView(frame: CGRect(x: 0, y: 0, width: 300, height: 300))
var scene = SCNScene()
sceneView.scene = scene
// start a live preview of that view
XCPShowView("The Scene View", view: sceneView)
// default lighting
sceneView.autoenablesDefaultLighting = true
// a geometry object
var tube = SCNTube(innerRadius: 1.99, outerRadius: 2, height: 3)
var tubeNode = SCNNode(geometry: tube)
scene.rootNode.addChildNode(tubeNode)
// video scene
let urlStr = NSBundle.mainBundle().pathForResource("sample", ofType: "mp4")
let url = NSURL(fileURLWithPath: urlStr!)
let asset = AVURLAsset(URL: url, options: nil)
let playerItem = AVPlayerItem(asset: asset)
let player = AVPlayer(playerItem: playerItem)
let videoNode = SKVideoNode(AVPlayer: player)
let spritescene = SKScene(size: CGSize(width: 1211, height: 431))
videoNode.size.width=spritescene.size.width
videoNode.size.height=spritescene.size.height
spritescene.addChild(videoNode)
// configure the geometry object
var myImage = UIImage.init(named: "BandImage.jpeg")
tube.firstMaterial?.diffuse.contents = spritescene
// set a rotation axis (no angle) to be able to
// use a nicer keypath below and avoid needing
// to wrap it in an NSValue
tubeNode.rotation = SCNVector4(x: 0.0, y: 1.0, z: 0.0, w: 0.0)
// animate the rotation of the torus
var spin = CABasicAnimation(keyPath: "rotation.w") // only animate the angle
spin.toValue = 2.0*M_PI
spin.duration = 3
spin.repeatCount = HUGE // for infinity
tubeNode.addAnimation(spin, forKey: "spin around")
// starts the video, solving the issue
sceneView.playing = true
【问题讨论】:
-
“映射不起作用”?-> 全黑?您是否尝试将 SKScene 的宽度设置为 1024?您是否尝试强制使用 GL 渲染器(请参阅 SCNView 界面构建器设置)?
-
全黑,即使 skscene 设置为 1024。音频播放,所以我知道电影正在播放,但没有纹理映射到表面
-
界面生成器?我正在代码中从头开始生成视图。
-
Similar SKVideoNode -> SceneKit 材质代码适用于我的设备...但不适用于模拟器。你在设备上试过了吗?
标签: ios sprite-kit avfoundation scenekit virtual-reality