Skip to content

Instantly share code, notes, and snippets.

View positlabs's full-sized avatar
🌶️
Focusing

Josh Beckwith positlabs

🌶️
Focusing
View GitHub Profile
@positlabs
positlabs / PlaceObjectOnSurface.js
Last active April 14, 2023 15:05
Object placement for Lens Studio
/*
REQUIRED: Create a ground plane with a collider
so we can hit test against it to place the object on the surface.
*/
/* API */
script.api.enablePlacement = function (bool) { placementEnabled = bool }
script.api.setScreenPoint = function (pt) { screenPoint = pt }
@positlabs
positlabs / snap-debounce.js
Created March 22, 2023 17:32
Debounce function for Lens Studio. Also provides setTimeout and clearTimeout functions.
/*
Debouncing functions is used for delaying a function call until a certain amount of time has passed.
In the example below, it's used to hide a slider after 3 seconds if the user isn't interacting with it.
*/
/**
* Polyfill for browser function setTimeout
* @param {function} callback - function to trigger after timeout is completed
@positlabs
positlabs / Enable4FrontCam.js
Last active April 12, 2023 21:10
Lens studio script to enable an object only on the front camera
//@input SceneObject obj
/** @type {SceneObject} */
var obj = script.obj
script.createEvent('CameraFrontEvent').bind(function(){
if(obj) obj.enabled = true
})
script.createEvent('CameraBackEvent').bind(function(){
if(obj) obj.enabled = false
@positlabs
positlabs / FrameUpdateMonitor.ts
Created December 16, 2022 15:01
Register a callback for when a video frame is updated
/*
Register a callback for when a video frame is updated
Since video playback generally isn't 60fps, this can be used as a render optimization
new FrameUpdateMonitor(videoElement, callback)
*/
export class FrameUpdateMonitor {
@positlabs
positlabs / html2Element.js
Created December 16, 2021 20:52
Converts a string into an html element
/**
* @param {String} HTML representing a single element
* @return {Element}
*/
export function html2Element(html) {
var template = document.createElement('template');
html = html.trim(); // Never return a text node of whitespace as the result
template.innerHTML = html;
return template.content.firstChild;
}
@positlabs
positlabs / LensStudioDebug.js
Last active January 25, 2023 09:41
Script debugging functions for Lens Studio
/*
Include this script at the top of the scene to enable global commands
Usage:
global.showProps(object)
global.showScene(scene)
global.showSceneObject(sceneObject)
global.showComponent(component)
Original by @robertlugg
https://gist.github.com/robertlugg/a5161200998092ddf69cb7393f7efcc0
@positlabs
positlabs / longPress.js
Created September 30, 2021 14:51
Long press for lens studio. WIP
var elapsed = 0
var updateEvent = script.createEvent('UpdateEvent')
updateEvent.enabled = false
updateEvent.bind(function(e){
elapsed += e.getDeltaTime()
if(elapsed > .5){
updateEvent.enabled = false
reset()
}
})
@positlabs
positlabs / encode-lens-studio.sh
Last active August 5, 2021 19:33
Lens Studio video asset encoder. Max dimension of 1280 using H.264 / AVC / MP4
#!/bin/bash
# Lens Studio video asset encoder. Max dimension of 1280 using H.264 / AVC / MP4
# https://lensstudio.snapchat.com/guides/2d/video/
# USAGE: ./encode-lens-studio.sh myvideo.mov
ffmpeg -i $1 \
-vf "scale='if(gt(iw, ih), min(1280, iw-mod(iw, 16)), -16)':'ifnot(gt(iw, ih), min(1280, ih-mod(ih, 16)), -16)'" \
-c:v libx264 \
-crf 23 \
-preset veryslow \
-c:a copy \
@positlabs
positlabs / spark-export.sh
Created July 16, 2021 19:15
Exports all arproj files in a directory
#!/bin/bash
for project in **/*.arproj;
do /Applications/Spark\ AR\ Studio.app/Contents/MacOS/sparkTerminalAppleMac export $project;
done;
@positlabs
positlabs / spark-ambient-color.js
Created June 23, 2021 15:10
Setting the color of an ambient light source in script
const Scene = require('Scene')
const {log} = require('Diagnostics')
const {RGBA} = require('Reactive')
;(async function () {
const light = await Scene.root.findFirst('ambientLight0')
log(light)
light.color = new RGBA(1, 1, 179/255, 1)