Skip to content

Instantly share code, notes, and snippets.

@gsabran
Created January 24, 2017 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsabran/dc1dfa276f8650907b7d38ea16a79652 to your computer and use it in GitHub Desktop.
Save gsabran/dc1dfa276f8650907b7d38ea16a79652 to your computer and use it in GitHub Desktop.
import DDDKit
import GLMatrix
import GLKit
import AVFoundation
class DDD360VideoViewController: DDDViewController {
fileprivate var videoNode: DDDNode!
// DDDViewController handles a few things, including when to render the scene.
// It already has a DDDScene object built in
override func viewDidLoad() {
super.viewDidLoad()
let url = URL(string: "https://s3.amazonaws.com/mettavr/adhoc-uploads/k2LytGGex5.qt")!
let player = AVPlayer(url: url)
player.play()
do {
videoNode = DDDNode()
videoNode.geometry = DDDGeometry.Sphere(radius: 20.0, orientation: .inward)
scene?.add(node: videoNode)
// I quite like putting the sphere forward so that it’s not too zoomed in:
videoNode.position = Vec3(v: (0, 0, -30))
let fragment = try DDDFragmentShader(fromResource: "fragmentShader", withExtention: "txt")
// let program = try DDDShaderProgram(fragment: fragment)
let program = try DDDShaderProgram(fragment: fragment, shaderModifiers: [
.fragment: "gl_FragColor = vec4(vec3(gl_FragColor.x + gl_FragColor.y + gl_FragColor.z) / 3.0, 1.0);",
])
videoNode.material.shaderProgram = program
let videoTexture = DDDVideoTexture(player: player)
videoNode.material.set(property: videoTexture, for: "SamplerY", and: "SamplerUV")
// Note that SamplerY and SampleUV match the variables names in the shader code
setupGestureRecognizer()
} catch {
print(error)
}
}
private func setupGestureRecognizer() {
let panGesture = UIPanGestureRecognizer(
target: self,
action: #selector(didPan(sender:))
)
panGesture.maximumNumberOfTouches = 1
view.addGestureRecognizer(panGesture)
}
private var hAngle: CGFloat = 0.0
private var vAngle: CGFloat = 0.0
@objc private func didPan(sender: UIPanGestureRecognizer) {
guard let view = sender.view else { return }
let vector = sender.translation(in: view)
hAngle += -CGFloat(vector.x / view.frame.width / 5)
vAngle += CGFloat(vector.y / view.frame.height / 10)
let q = GLKQuaternionInvert(GLKQuaternion(right: hAngle, top: vAngle)).q
videoNode.rotation = Quat(x: q.0, y: q.1, z: q.2, w: q.3)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment