ios - SKVideoNode(嵌入在 SKScene 中)作为 Scene Kit 节点的纹理不起作用

标签 ios sprite-kit avfoundation scenekit virtual-reality

我正在尝试使用 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

最佳答案

我已经在 github 上发布了我的代码(和一些示例全景内容),供任何想要工作示例代码或有兴趣在开源全景视频播放器上进行协作的人使用:

https://github.com/jglasse/OSVR

事实证明,模拟器(和 Playground )似乎不支持此功能。将上面的代码移动到一个项目并在设备上运行,我终于让它工作了。

所以故事的寓意是 - 如果您使用 SKVideoNodes 作为 Scenekit 的纹理,请使用实际设备进行测试。

关于ios - SKVideoNode(嵌入在 SKScene 中)作为 Scene Kit 节点的纹理不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32794733/

相关文章:

ios - 如何以编程方式将带有 zPosition 的子项添加到使用场景编辑器创建的场景中

ios - AVCameraInput - 将相机从前切换到后时崩溃

ios - 在layoutSubviews中设置 subview 框架不会持续存在

ios - 适用于 iOS 的自定义粒子系统

android - React Native 输出是 .class(对于 android)和 .c(对于 iOS)

ios - 如何使用 Sprite Kit 减慢运行 SKAction followPath 的 SKSpriteNode 以获得慢动作效果?

ios - AVAssetWriter:如何多次写入单个文件

ios - AVAssetExportSession 为零 iPhone 7 - Plus 模拟器

ios - XIB Control 单击以编程方式引用 socket iAD

ios - Chartboost Swift 实现 : CoreData: warning: Unable to load class named 'CBHTTPRequestEntity' for entity 'CBHTTPRequestEntity'