Spark AR v85 Code Examples

Spark AR Studio v85+ Script Examples

Spark AR Studio v85 updates are here! The biggest changes include an asynchronous API update that will require developers to update their script file code.

“APIs will be deprecated after 6 months in existing live effects, and 3 months in resubmitted projects.”

Spark AR Scripting Asynchronous API Changes (v85+)

Native UI Picker

This common script has been updated in version 85+ of Spark AR. There updated documentation and an example here.


Promise

To prepare textures, materials and other scene objects for your effect script; start your script using the Promise.all method.

const Scene = require('Scene');
const Textures = require('Textures');
const Materials = require('Materials');

Promise.all([

    // Find Texture
    Textures.findFirst('textureName'),
    // Find Material
    Materials.findFirst('materialName'),
    // Find Scene Object
    Scene.root.findFirst('sceneObject'),

]).then(function (results) {

    // Define variable names for items we found
    const theTexture = results[0];
    const theMaterial = results[1];
    const theSceneObject= results[2];

    // Change the theMaterial texture to theTexture
    theMaterial.diffuse = theTexture;

    // Execute other code and functions here
});

Script to Patch Bridging

Sending values from the Patch Editor the the script and visa versa has changed. Here is the official documentation; and here are some examples. I will continue to add to this list.

To Script

Number: getScalar

Patches.outputs.getScalar('ToScriptNumber').then(event=> {
     event.monitor().subscribe(function (values) {
          Diagnostics.log(values.newValue);
     });
});

Text: getString

Patches.outputs.getString('ToScriptText').then(event => {
     Diagnostics.log(event.pinLastValue());
});

Boolean: getBoolean

Patches.outputs.getBoolean('ToScriptBoolean').then(event => {
     event.monitor().subscribe(function (values) {
          Diagnostics.log(values.newValue);
          Diagnostics.log(values.oldValue);
     });
});

Pulse: getPulse

// Declare variable to keep track of this pulse
var pulsed;

Patches.outputs.getPulse('ToScriptPulse').then(event => {
     pulsed = event.subscribe(function () {
          // code to execute when pulse happens
          // "pulsed" variable will be available throughout the script
     });
});

Vector 2: getPoint2D

// Declare variable with x and y properties
var vector2 = {x: 0, y: 0};

//Vector 2
Patches.outputs.getPoint2D('ToScriptVector2').then(pixelPointSignal => {

     // Pin
     vector2.x = pixelPointSignal.x.pinLastValue();
     vector2.y = pixelPointSignal.y.pinLastValue();

     // Monitor
     pixelPointSignal.x.monitor({ fireOnInitialValue: true }).subscribe(function (xSignal) {
          vector2.x = xSignal.newValue;
     });
     pixelPointSignal.y.monitor({ fireOnInitialValue: true }).subscribe(function (ySignal) {
          vector2.y = ySignal.newValue;
      });

});

Vector 3: getPoint

// Declare variable with x, y, z properties
var vector3 = { x: 0, y: 0, z: 0 };

// Vector 3
Patches.outputs.getPoint('ToScriptVector3').then(pointSignal => {

     // Pin
     vector3.x = pointSignal.x.pinLastValue();
     vector3.y = pointSignal.y.pinLastValue();
     vector3.z = pointSignal.z.pinLastValue();

     // Monitor
     pointSignal.x.monitor({ fireOnInitialValue: true }).subscribe(function (xSignal) {
          vector3.x = xSignal.newValue;
      });
     pointSignal.y.monitor({ fireOnInitialValue: true }).subscribe(function (ySignal) {
          vector3.y = ySignal.newValue;
      });
     pointSignal.z.monitor({ fireOnInitialValue: true }).subscribe(function (zSignal) {
          vector3.z = zSignal.newValue;
      });
});

Vector 4: getColor

// Declare variable with x, y, z, w properties
var vector4 = {x: 0, y: 0, z: 0, w: 0};

// Vector 4
// Cannot monitor this signal without crashing program.
Patches.outputs.getColor('ToScriptVector4').then(colorSignal => {
     vector4.x = colorSignal.red.pinLastValue();
     vector4.y = colorSignal.green.pinLastValue();
     vector4.z = colorSignal.blue.pinLastValue();
     vector4.w = colorSignal.alpha.pinLastValue();
});

From Script

Number: setScalar

// Example number to send to Patch Editor from script
var scriptNumber;

Patches.inputs.setScalar('FromScriptNumber', scriptNumber);

Text: setString

// Example string (text) to send to Patch Editor from script
var scriptString = "Example Text";
var scriptNumber = 3;

Patches.inputs.setString('FromScriptText', scriptString);
Patches.inputs.setString('FromScriptNumberAsText', scriptNumber.toString());

Boolean: setBoolean

Patches.inputs.setBoolean('FromScriptBoolean', true);

Pulse: setPulse

// Call this function to pulse 'FromScriptPulse'
// Reactive.once() will send an event pulse
// If it's not in a function, it will fire once on load

function sendPulse() {
     Patches.inputs.setPulse('FromScriptPulse', Reactive.once());
}

Vector 2: setPoint2D

// Declare variable to send 2 values
var pixelSignal = Reactive.point2d(10, 20);

// Set the vector 2
Patches.inputs.setPoint2D('FromScriptVector2', pixelSignal);

Vector 3: setPoint

// Declare variable to send 3 values
var pointSignal = Reactive.point(10, 20, 30);

// Set the vector 3
Patches.inputs.setPoint('FromScriptVector3', pointSignal)

Thanks for visiting. If you have more questions about this particular topic, feel free to send me a message or email.

You may also like...

Popular Posts

4 Comments

  1. Dude thank you! You’ve filled the massive void in the official spark documentation, I’m shocked every time they introduce a new methodology that I’m still looking at docs from a year ago.

    1. Glad it is helpful! I will continue to add and update snippets on this post.

  2. I’m wondering how can I set Vectror4 from script to Patch. How to declare vector variable in the script and set it to ‘vector4 patch’? The Internet answers nothing :/

    1. Newcolor Studio says:

      Set is currently unavailable, according to the documentation for the RgbaSignal class, but Get is available using .getColor(); just don’t throttle it too hard or the program crashes.

Leave a Reply

Your email address will not be published. Required fields are marked *