Scripting
Lens Studio provides a scripting engine for creating rich interactive experiences. With scripts, your Lenses can respond to touch input, play animation and audio, modify Scene Objects, and more.
worldAabbMax or worldAabbMin should provide the bounds of an object.
You can add the following script anywhere you would like to get stacktrace
print(`${new Error().stack}`);
You can use requireAsset
to reference assets such as Textures, Materials, or
Meshes within your script without needing to use //@input
. Please follow the
guidelines
on properly using requireAsset
by file path.
@input myMesh:
MaterialMeshVisual let myTexture = requireAsset("./TextureFolder/myTexture.jpg");
this.myMesh.mainPass.baseTex = myTexture;
Persistent storage limit on Spectacles is 125MB. It's using GeneralDataStore for storing and retrieving data based on keys.
Once the image has been fetched, Base64 can be used to handle encoding and decoding, converting the images into a texture that can be applied to materials.
@component
export class FetchImageToTexture extends BaseScriptComponent {
// Using InternetModule to fetch images and turn it into a texture
@input
internetModule: InternetModule;
// Using RenderMeshVisual to display the texture
@input
renderMeshVisual: RenderMeshVisual;
// Method called when the script is awake
async onAwake() {
let request = new Request(
'https://developers.snap.com/img/spectacles/spectacles-2024-hero.png',
{ method: 'GET' }
);
let response = await this.internetModule.fetch(request);
print('Response: ' + response);
if (response.status === 200) {
let bytes = await response.bytes();
// Convert bytes to a base64 string
let base64 = Base64.encode(bytes);
// Decode the base64 string to a texture
this.decodeBase64ToTexture(base64).then(this.displayTexture.bind(this));
}
}
decodeBase64ToTexture(base64: string) {
// Decode the base64 string to a texture using Base64.decodeTextureAsync
return new Promise((resolve, reject) => {
Base64.decodeTextureAsync(base64, resolve, reject);
});
}
displayTexture(texture: Texture) {
if (this.renderMeshVisual) {
// Set the texture to the main material of the RenderMeshVisual
this.renderMeshVisual.mainMaterial.mainPass.baseTex = texture;
}
}
}
Script Components in Lens Studio are able to reference one another whether they have been created with JavaScript, TypeScript or Custom Component. Please take a look at Accessing Components for various examples.