Skip to content

Instantly share code, notes, and snippets.

@glaurent
Created December 11, 2014 22:10
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save glaurent/aad82c4185f3c92f21dc to your computer and use it in GitHub Desktop.
Save glaurent/aad82c4185f3c92f21dc to your computer and use it in GitHub Desktop.
adding a video in a SceneKit scene using a SpriteKit SKVideoNode
let spriteKitScene = SKScene(size: CGSize(width: 1276.0 / 2.0, height: 712.0 / 2.0))
var videoSpriteKitNode:SKVideoNode?
let videoNode = SCNNode()
videoNode.geometry = SCNPlane(width: videoNodeWidth, height: videoNodeHeight)
// using SpriteKit scene with videonode inside
//
spriteKitScene.scaleMode = .AspectFit
videoSpriteKitNode?.position = CGPoint(x: spriteKitScene.size.width / 2.0, y: spriteKitScene.size.height / 2.0)
videoSpriteKitNode?.size = spriteKitScene.size
spriteKitScene.addChild(videoSpriteKitNode!)
videoNode.geometry?.firstMaterial?.diffuse.contents = spriteKitScene
videoNode.geometry?.firstMaterial?.doubleSided = true
// flip video upside down, so that it's shown in the right position
//
var transform = SCNMatrix4MakeRotation(Float(M_PI), 0.0, 0.0, 1.0)
transform = SCNMatrix4Translate(transform, 1.0, 1.0, 0)
videoNode.geometry?.firstMaterial?.diffuse.contentsTransform = transform
videoNode.position = SCNVector3(x: 0, y: 30, z: 7)
aSceneKitNode.addChildNode(videoNode)
videoSpriteKitNode?.play()
@guoye-zhang
Copy link

Or you can just use:

videoSpriteKitNode?.yScale = -1

@frowing
Copy link

frowing commented Jan 5, 2018

Any idea why is it shown upside down?

@chrisl777
Copy link

I was doing something similar and getting upside down labels. Try this:

videoNode.geometry?.firstMaterial?.diffuse.contentsTransform = SCNMatrix4Translate(SCNMatrix4MakeScale(1, -1, 1), 0, 1, 0)

(From this post: https://stackoverflow.com/questions/33284781/scenekit-flip-direction-of-scnmaterial)

@mortenjust
Copy link

@guoye-zhang's suggestion using videoSpriteKitNode?.yScale = -1 works great for me

@JaydenIrwin
Copy link

contentsTransform = SCNMatrix4Translate(SCNMatrix4MakeScale(1, -1, 1), 0, 1, 0) doesn't seem to do anything on iOS 13.4

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment