Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rotating camera experiment #81

Draft
wants to merge 1 commit into
base: moving-camera
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions RealityMixer/Capture/Misc/CameraPoseSender.swift
Original file line number Diff line number Diff line change
Expand Up @@ -236,3 +236,61 @@ final class TemporaryCalibrationStorage {
self.calibration = calibration
}
}

final class RotatingCamera {

private weak var client: TCPClient?

private let radiansPerSecond = .pi/12.0
private let radius = 3.0
private let height = 2.0
private let center = Vector3(x: 0, y: 1.5, z: 0)

private var currentAngle = 0.0

private var currentPosition: Vector3 {
.init(x: cos(currentAngle) * radius, y: height, z: sin(currentAngle) * radius)
}

init(client: TCPClient) {
self.client = client
}

private func sendCameraUpdate(pose: Pose) {
_ = client?.send(data: CameraPositionPayload(position: pose.position).data)
_ = client?.send(data: CameraRotationPayload(rotation: pose.rotation).data)
}

private func lookAt(_ cameraPosition: Vector3, center: Vector3, up: Vector3 = Vector3(x: 0, y: 1, z: 0)) -> SCNMatrix4 {
let forward = (center - cameraPosition).normalized
let right = forward.cross(up).normalized
let updatedUp = right.cross(forward)

return SCNMatrix4(
m11: Float(right.x),
m12: Float(right.y),
m13: Float(right.z),
m14: 0.0,
m21: Float(updatedUp.x),
m22: Float(updatedUp.y),
m23: Float(updatedUp.z),
m24: 0.0,
m31: Float(-forward.x),
m32: Float(-forward.y),
m33: Float(-forward.z),
m34: 0.0,
m41: 0.0,
m42: 0.0,
m43: 0.0,
m44: 1.0
)
}

func update(elapsedTime: TimeInterval) {
currentAngle += elapsedTime * radiansPerSecond
let currentPosition = self.currentPosition
sendCameraUpdate(
pose: .init(position: currentPosition, rotation: Quaternion(rotationMatrix: lookAt(currentPosition, center: center)))
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ final class MixedRealityViewController: UIViewController {
true
}

private let cameraPoseSender: CameraPoseSender?
// private let cameraPoseSender: CameraPoseSender?
private let rotatingCamera: RotatingCamera

init(
client: TCPClient,
Expand All @@ -48,7 +49,8 @@ final class MixedRealityViewController: UIViewController {
self.client = client
self.configuration = configuration
self.factory = ARConfigurationFactory(mrConfiguration: configuration)
self.cameraPoseSender = CameraPoseSender(client: client)
self.rotatingCamera = RotatingCamera(client: client)
// self.cameraPoseSender = CameraPoseSender(client: client)
super.init(nibName: String(describing: type(of: self)), bundle: Bundle(for: type(of: self)))
}

Expand Down Expand Up @@ -255,6 +257,8 @@ final class MixedRealityViewController: UIViewController {
}

@objc func update(with sender: CADisplayLink) {
let interval = sender.targetTimestamp - sender.timestamp
rotatingCamera.update(elapsedTime: interval)
receiveData()
oculusMRC?.update()
}
Expand Down Expand Up @@ -363,7 +367,7 @@ extension MixedRealityViewController: ARSessionDelegate {
configureForeground(with: frame)
first = false
} else {
cameraPoseSender?.didUpdate(frame: frame)
// cameraPoseSender?.didUpdate(frame: frame)
}
}

Expand Down