Scripting
Frequently asked questions about scripting in SPECS Lenses including TypeScript, lifecycle events, and common coding patterns.
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(`${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.
- TypeScript
- JavaScript
let material = requireAsset('../Materials/Dartboard.mat') as Material;
let texture = requireAsset('../Textures/Dartboard.png') as Texture;
let mesh = requireAsset('../Meshes/Dartboard.mesh') as RenderMesh;
this.renderMeshVisual.mainMaterial = material;
this.renderMeshVisual.mainPass.baseTex = texture;
this.renderMeshVisual.mesh = mesh;
const material = requireAsset('../Materials/Dartboard.mat');
const texture = requireAsset('../Textures/Dartboard.png');
const mesh = requireAsset('../Meshes/Dartboard.mesh');
script.renderMeshVisual.mainMaterial = material;
script.renderMeshVisual.mainPass.baseTex = texture;
script.renderMeshVisual.mesh = mesh;
On SPECS, a Lens's persistent storage holds up to about 100 KB (102,400 bytes). Persistent
storage uses a GeneralDataStore to store and retrieve data by key. To
read the limit your Lens has, call
getMaxSizeInBytes() on
global.persistentStorageSystem.store. To store more data, use
Snap Cloud.
To load an image from a URL, create a resource from the URL with makeResourceFromUrl(), then load it as a texture with loadResourceAsImageTexture(). See Download an Image from a URL.
If you already have the image's bytes, for example from a Fetch request or an API that returns them, Base64 can encode them and decode the result into a texture that you can apply to materials:
- TypeScript
- JavaScript
@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/misc/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;
}
}
}
// @input Component.ScriptComponent internetModule {"hint": "InternetModule"}
// @input Component.RenderMeshVisual renderMeshVisual
async function onAwake() {
const request = new Request(
'https://developers.snap.com/img/spectacles/misc/spectacles-2024-hero.png',
{ method: 'GET' }
);
const response = await script.internetModule.fetch(request);
print('Response: ' + response);
if (response.status === 200) {
const bytes = await response.bytes();
// Convert bytes to a base64 string
const base64 = Base64.encode(bytes);
// Decode the base64 string to a texture
decodeBase64ToTexture(base64).then(displayTexture);
}
}
function decodeBase64ToTexture(base64) {
// Decode the base64 string to a texture using Base64.decodeTextureAsync
return new Promise(function (resolve, reject) {
Base64.decodeTextureAsync(base64, resolve, reject);
});
}
function displayTexture(texture) {
if (script.renderMeshVisual) {
// Set the texture to the main material of the RenderMeshVisual
script.renderMeshVisual.mainMaterial.mainPass.baseTex = texture;
}
}
// Initialize
onAwake();
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.