Add the native UI slider to your Meta Spark Studio effect to let people gradually control something in the scene, for example transparency or color.
The on-screen slider will not be visible in the Simulator as you build your effect. To view and play with the slider, you’ll need to preview your effect in the Meta Spark Player app.
In this tutorial, you’ll learn how to create an effect that enables a user to adjust the slider on a device screen to change the opacity of a material with an emoji texture applied to it.
We’ll explain how to create this effect using both the Patch Editor and Scripting.
Download the sample content to follow along. It includes the finished and unfinished version of the project created with the Patch Editor.
In the unfinished project, we’ve added:
We’ll first explain how to use the Patch Editor to adjust the opacity of the angry texture. Adjusting this property in the Patch Editor instead of via the Inspector will allow you to add further logic so users can change the opacity for themselves using an on-screen slider.
Adjusting the opacity of the angry texture
You’ll need to use a Multiply patch to edit the RGBA channels of the angry texture and then apply the adjusted texture to the material, to decrease or increase opacity.
Creating the patches
You'll need to create:
Connecting the patches
You'll need to connect:
If you wanted to adjust the opacity to a specific value, you could simply edit the value in the Multiply patch.
For example, to create 50% opacity, you would change the value from 0 to 0.5:
In order to enable users of this effect to dynamically change the opacity for themselves, you’ll need to add the Slider UI patch.
Adding and connecting the Slider UI patch
You'll need to:
Your graph will look like this:
Editing the Slider UI patch:
Edit the Slider UI patch to make the opacity of the angry emoji 50% and to display an interactive slider.
To do so:
This is how your final effect will look:
You can also achieve this effect with scripting. If you're new to scripting, you might want to take a look at our guide to scripting and open a new project to try it out yourself.
Add the capability
The Slider capability needs to added to your project. To do this:
Native UI Control will now be listed as a Capability. Next add the Slider capability. To do this:
Not all capabilities are added to projects automatically. If you need other capabilities you may need to add them manually.
First create a new script, and open it.
Include the NativeUI
module so that you can use its API:
const NativeUI = require('NativeUI');
Create a reference to the slider object:
const slider = NativeUI.slider;
Set the slider's visible
property to true. This property is false
by default and needs to be set to true
in order to show the slider:
slider.visible = true;
Monitor changes to the slider's value
property by subscribing to the EventSource
returned by calling the monitor()
method on value
.
slider.value.monitor().subscribe(function(val) { // Get the slider's new value with 'val.newValue' // 'val.newValue' will be a scalar signal between 0 and 1 });
The example below shows you how to use the slider to set the opacity of a particular material, like in the example project.
// Load in the required modules const NativeUI = require('NativeUI'); const Materials = require('Materials'); (async function () { // Enables async/await in JS [part 1] // Locate the material in the project const material = await Materials.findFirst('material0'); // Create a reference to the NativeUI's slider object // and display it const slider = NativeUI.slider; slider.visible = true; // Subcribe to changes to the slider's value property slider.value.monitor().subscribe(function(val) { // Use the new slider value to update the material's // opacity material.opacity = val.newValue; }); })(); // Enables async/await in JS [part 2]