Clear
Back

As a result of the coronavirus (COVID-19) outbreak, we have a reduced capacity to review effects and delays in publishing are expected. Learn more.

Using the Counter and If Then Else Patches

You can use the Patch Editor to add logic and interactivity to your effects. In this guide, you'll make a color in your effect change in response to different interactions using the If Then Else and Counter patches:

For these examples we're going to use a Screen Tap patch, which detects a tap of someone's finger on the device's screen and outputs a boolean signal. You could use any other patch that outputs a boolean signal. To see what kind of signal a patch outputs, check the menu in the Patch Editor.

This guide uses Counter and If Then Else to show different colors. They can be used for other types of effect, for example to play different animations or make objects become visible in the scene.

Switching between 2 options using If Then Else

You can use the If Then Else and Switch patches to switch between 2 options in response to an interaction from the user. In this example, we'll use these patches to change the color of a material each time a tap is detected.

Create the patches

Start by opening the Patch Editor. Then, right-click in the Patch Editor and select:

  1. A Screen Tap patch.
  2. An If Then Else patch.
  3. A Switch patch.

You'll also need a to create a patch representing the property you want these patches to change. For this example:

  1. Create a material for an object in your scene.
  2. Click the arrow next to Texture in the Inspector to create a patch representing this property.


Change the data type

If you select the If Then Else patch and expand the blue box at the bottom of the patch, you'll see it can output different types of data. Select Color.



The option you'll select here depends on the type of effect you're creating. If you were using these patches to switch between two animations, you'd select Animation Data instead.

Edit the patch

Next to Then and Else, click the color symbols on the patch to choose new colors for the material:

Connecting the patches

Connect:

  1. The Screen Tap patch to the Switch patch.
  2. The On/Off port in Switch to the Condition input port in If Then Else.
  3. The output of If Then Else to the Diffuse Texture port in the material patch.

Your graph should look like this.



As a result of connecting these patches, each time the screen is tapped, a switch is turned on or off. When it's on, the material will take the color added next to Then in the If Then Else patch. When it's off, it'll change to the other color.

You can test this in the Simulator, by selecting Simulate Touch from the menu and clicking anywhere in the Simulator:

Counting between more than 2 options

To count between more than 2 options in response to an interaction, you can use the Counter and Option Picker patches.

Right-click in the Patch Editor and select:

  • A Screen Tap patch.
  • A Counter patch.
  • An Option Picker patch.

You'll also need a patch representing the property in the scene object that you want to change. Like before, for this example we'll use the Diffuse Texture property in a material, to change the color.

The Counter patch can be use to count through different options, which we'll set in the Option Picker patch.

You can use the ports and adjust the values in the Counter patch to count through options in different orders, or jump to a specific option.

The Option Picker can accept and output different data types - like animation, boolean signals and color. For this example, select Color. To set color options in the Option Picker, click the value next to each port. For this example, set a different color in each one.

Connect the patches, so your graph looks like this:



Make sure the Maximum Count is set to match the number of options you've set in the Option Picker. We've set the Maximum Count to 4, to show 4 colors.

Randomizing the output

To show a random option in your effect, all you need to do is replace the Counter patch with a Random patch. Make sure the End Value in the Random patch matches the number of options you've set in the Option Picker patch.

Join the Spark AR Creator's community

Find inspiration, see examples, get support, and share your work with a network of creators.

Join Community

Join the Spark AR Creator's community

Find inspiration, see examples, get support, and share your work with a network of creators.

Join Community

Frequently asked questions

Have a specific question? Maybe it's been answered.


Read FAQs
Was this document helpful?