简体   繁体   English

使用Swift的SceneKit中视频纹理对象的布局坐标

[英]Layout coordinates for a video-texture object in SceneKit with Swift

I'm working on creating an SCNNode instance that consists of an SCNPlane - type geometry, with a video playing on the surface of the node. 我正在创建一个由SCNPlane类型几何组成的SCNNode实例,并在节点表面播放视频。 This is what I have so far: 这是我到目前为止的内容:

    let plane = SCNPlane(width: 5, height: 5)
    node = SCNNode(geometry: plane)
    GlobalScene.shared.rootNode.addChildNode(node!)

    let urlStr = Bundle.main.path(forResource: "test", ofType: "mov")
    videoURL = NSURL(fileURLWithPath: urlStr!)
    player = AVPlayer(playerItem: AVPlayerItem(url: videoURL! as URL))
    let videoNode = SKVideoNode(avPlayer: player!)
    player?.actionAtItemEnd = .none

    let spritescene = SKScene(size: CGSize(width: 122, height: 431))
    videoNode.size.width=spritescene.size.width
    videoNode.size.height=spritescene.size.height
    spritescene.addChild(videoNode)

    plane.firstMaterial?.diffuse.contents = spritescene

The overall functionality so far works great! 到目前为止,整体功能还不错! What I'm trying to figure out is, how to I get the node 's ENTIRE surface to be made up of the video ? 我要弄清楚的是,如何使node的整个表面由视频组成? So far, I've only gotten it to appear in the single corner shown below: 到目前为止,我只让它出现在下面显示的单个角上: 在此处输入图片说明

EDIT : it looks like the issue is that I'm only setting the first material of the plane node (rather than all of them...) which is why I'm seeing the other 3 "quadrants" as blank. 编辑 :看起来问题在于我只设置了平面节点的第一个材料(而不是全部...),这就是为什么我将其他3个“象限”视为空白的原因。

EDIT2 : That first conclusion may not be correct - if I set: EDIT2 :第一个结论可能不正确-如果我设置为:

    plane.firstMaterial?.diffuse.contents = NSColor.green

I get the following result: 我得到以下结果:

在此处输入图片说明

So...why won't that work when applying the contents of a SpriteKit scene? 那么...为什么在应用SpriteKit场景的内容时SpriteKit

Using a SKScene and a SKVideoNode is not necessary. SKScene使用SKSceneSKVideoNode You can directly set the AVPlayer as the contents of a SCNMaterialProperty instance. 您可以直接将AVPlayer设置为SCNMaterialProperty实例的内容。 This will allow for better performance and will avoid having to deal with scaling and positioning the SpriteKit elements. 这样可以提高性能,并且避免处理SpriteKit元素的缩放和定位。

With the help of this gist , I was able to solve my issue; 在这个要点的帮助下,我得以解决我的问题; which ended up involving both scaling the video node properly, as well as setting the position properly. 最终涉及正确缩放视频节点以及正确设置位置。 My working code is below: 我的工作代码如下:

    GlobalScene.shared.rootNode.addChildNode(node!)

    let urlStr = Bundle.main.path(forResource: "test", ofType: "mov")
    videoURL = NSURL(fileURLWithPath: urlStr!)
    player = AVPlayer(playerItem: AVPlayerItem(url: videoURL! as URL))
    videoSpriteKitNode = SKVideoNode(avPlayer: player!)
    player?.actionAtItemEnd = .none
    NotificationCenter.default.addObserver(
        self,
        selector: #selector(self.playerItemDidReachEnd),
        name: NSNotification.Name.AVPlayerItemDidPlayToEndTime,
        object: player?.currentItem)


    let spriteKitScene = SKScene(size: CGSize(width: 1276.0 / 2.0, height: 712.0 / 2.0))
    videoSpriteKitNode?.size.width=spriteKitScene.size.width
    videoSpriteKitNode?.size.height=spriteKitScene.size.height
    node?.geometry?.firstMaterial?.diffuse.contentsTransform = SCNMatrix4Translate(SCNMatrix4MakeScale(1, -1, 1), 0, 1, 0)

    videoSpriteKitNode?.position = CGPoint(x: spriteKitScene.size.width / 2.0, y: spriteKitScene.size.height / 2.0)

    videoSpriteKitNode?.size = spriteKitScene.size

    videoSpriteKitNode?.play()
    spriteKitScene.addChild(videoSpriteKitNode!)

    plane?.firstMaterial?.diffuse.contents = spriteKitScene

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM