Skip to content

Instantly share code, notes, and snippets.

@skang2-sc
Last active March 19, 2025 21:26
Show Gist options
  • Save skang2-sc/390b1e1de99993de8ef58b7d5dcee197 to your computer and use it in GitHub Desktop.
Save skang2-sc/390b1e1de99993de8ef58b7d5dcee197 to your computer and use it in GitHub Desktop.
Camera Crop
@component
export class CameraCrop extends BaseScriptComponent {
@input
nonCropImage: Image;
@input
croppedImage: Image;
//Generate this through Asset Browser > Screen Crop Texture
@input
screenCropCameraTexture: Texture;
private cameraTexture: Texture;
private cameraTextureControl: CameraTextureProvider;
private cameraModule: CameraModule = require("LensStudio:CameraModule");
onAwake() {
//Requesting Camera needs to be done in OnStartEvent
this.createEvent("OnStartEvent").bind(() => {
// Create a camera request to get the default color camera (i.e. Left vs Right camera)
let imageRequest = CameraModule.createCameraRequest();
imageRequest.cameraId = CameraModule.CameraId.Default_Color;
// Request the camera and get the camera texture
this.cameraTexture = this.cameraModule.requestCamera(imageRequest);
//Need to call onNewFrame to start camera streaming remain streaming
this.cameraTextureControl = this.cameraTexture
.control as CameraTextureProvider;
this.cameraTextureControl.onNewFrame.add(() => {});
//Setting crop texture to use the camera texture
let screenCropTextureControl = this.screenCropCameraTexture
.control as CropTextureProvider;
screenCropTextureControl.inputTexture = this.cameraTexture;
this.croppedImage.mainPass.baseTex = this.screenCropCameraTexture;
// Set the non-cropped image to use the camera texture
this.nonCropImage.mainPass.baseTex = this.cameraTexture;
});
}
}
@skang2-sc
Copy link
Author

Result:
Screenshot 2025-03-19 at 11 03 17 AM

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