Last active
February 21, 2025 17:26
-
-
Save agrancini-sc/84bd84563a2f929306e4097c6b19f2c1 to your computer and use it in GitHub Desktop.
CameraDoubleEye
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import NativeLogger from "SpectaclesInteractionKit/Utils/NativeLogger"; | |
const log = new NativeLogger("CameraAPIBothLogger"); | |
@component | |
export class CameraDoubleye extends BaseScriptComponent { | |
private cameraModule: CameraModule = require("LensStudio:CameraModule"); | |
private cameraRequestLeft: CameraModule.CameraRequest | undefined; | |
private cameraTextureLeft: Texture | undefined; | |
private cameraTextureProviderLeft: CameraTextureProvider | undefined; | |
private cameraRequestRight: CameraModule.CameraRequest | undefined; | |
private cameraTextureRight: Texture | undefined; | |
private cameraTextureProviderRight: CameraTextureProvider | undefined; | |
@input | |
@hint("The left image in the scene that will be showing the captured frame.") | |
uiImageLeft: Image | undefined; | |
@input | |
@hint("The right image in the scene that will be showing the captured frame.") | |
uiImageRight: Image | undefined; | |
onAwake() { | |
log.d("Script is waking up."); | |
print("Script is waking up."); | |
// Set up an OnStartEvent to ensure the script initializes after awakening | |
this.createEvent("OnStartEvent").bind(this.onStart.bind(this)); | |
} | |
onStart() { | |
log.d("Script has started."); | |
print("Script has started."); | |
try { | |
// Initialize and start the left camera request | |
if (this.uiImageLeft) { | |
log.d("Initializing left camera."); | |
print("Initializing left camera."); | |
this.cameraRequestLeft = CameraModule.createCameraRequest(); | |
this.cameraRequestLeft.cameraId = CameraModule.CameraId.Left_Color; | |
this.cameraTextureLeft = this.cameraModule.requestCamera( | |
this.cameraRequestLeft | |
); | |
this.cameraTextureProviderLeft = this.cameraTextureLeft | |
.control as CameraTextureProvider; | |
// Add listener for new frames | |
if (this.cameraTextureProviderLeft) { | |
log.d("Setting up left camera frame listener."); | |
print("Setting up left camera frame listener."); | |
this.cameraTextureProviderLeft.onNewFrame.add( | |
this.handleNewFrameLeft.bind(this) | |
); | |
} else { | |
log.e("CameraTextureProviderLeft creation failed."); | |
print("CameraTextureProviderLeft creation failed."); | |
} | |
} | |
// Initialize and start the right camera request | |
if (this.uiImageRight) { | |
log.d("Initializing right camera."); | |
print("Initializing right camera."); | |
this.cameraRequestRight = CameraModule.createCameraRequest(); | |
this.cameraRequestRight.cameraId = CameraModule.CameraId.Right_Color; | |
this.cameraTextureRight = this.cameraModule.requestCamera( | |
this.cameraRequestRight | |
); | |
this.cameraTextureProviderRight = this.cameraTextureRight | |
.control as CameraTextureProvider; | |
// Add listener for new frames | |
if (this.cameraTextureProviderRight) { | |
log.d("Setting up right camera frame listener."); | |
print("Setting up right camera frame listener."); | |
this.cameraTextureProviderRight.onNewFrame.add( | |
this.handleNewFrameRight.bind(this) | |
); | |
} else { | |
log.e("CameraTextureProviderRight creation failed."); | |
print("CameraTextureProviderRight creation failed."); | |
} | |
} | |
} catch (error) { | |
log.e("Error during camera setup: " + error); | |
print("Error during camera setup: " + error); | |
} | |
} | |
handleNewFrameLeft(cameraFrame) { | |
if (this.uiImageLeft && this.cameraTextureLeft) { | |
log.d("New frame received from left camera."); | |
print("New frame received from left camera."); | |
this.uiImageLeft.mainPass.baseTex = this.cameraTextureLeft; | |
} | |
} | |
handleNewFrameRight(cameraFrame) { | |
if (this.uiImageRight && this.cameraTextureRight) { | |
log.d("New frame received from right camera."); | |
print("New frame received from right camera."); | |
this.uiImageRight.mainPass.baseTex = this.cameraTextureRight; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment