- Templates
- Spectacles
- Spectacles Depth
Spectacles Depth
The Spectacles Depth Template allows you to dynamically generate objects in the world space and check whether the object has collided with a surface. It additionally allows you to trigger behaviors when the object spawns, when it collides, and when its lifecycle ends.
Tutorial
Guide
This template is driven by the Object Spawner
object which contains the ObjectSpawner
script. This object will spawn its children over the course of the Lens, duplicating its children as needed, and reusing old ones as they finish their lifecycle.
Spawning your Objects
To spawn your object, select the object you would like to spawn, and drag it under the ObjectSpawner
.

Modifying Object Spawner
Next, we can modify how the spawned objects behaves. To do this, select the Object Spawner [EDIT ME]
object in the Objects
panel, then, with the object selected, we can configure its settings in the Inspector
panel.
Modify the Count
property to specify how many objects will spawn during the Spectacles video loop.
Set object Lifetime
to set a spawned object’s lifetime in seconds (use -1 if you want the objects to stay alive to the end of the video/photo loop).
Set the Collision
checkbox if you want the object to react to surfaces in the video. This option uses the depth information stored in a Spectacles 3 video. Take a look at Lenses for Spectacles for more information.
Set the lifetimeAfterCollision
parameter to define how many seconds the spawned objects will stay alive after collision (0: disappear right after collision, -1: stay alive until the end of the video/photo loop).

You can click on the Spawn Source
field to select an object that you want the objects to be spawned from. By default, the Spawn Source is the camera, which means the objects are spawned wherever the camera is.
You can additionally adjust the spawn position relative to the Spawn Source by modifying the Offset
parameters.
You can modify the size of the cube by setting the Spawn Box Size.

Finally, if you enable the Apply Rotation checkbox, the spawned objects will be rotated towards the camera when they are spawned.
If you want objects to always look at camera simply add a LookAt Component to the original object.
Setting up Movement
Once your objects are spawned, you can have them move like a simple projectile by enabling the Move
checkbox.
The Start Speed
parameter allows you to set the object's movement speed relative to the spawn source. For example, if you set the Camera object as a Spawn Source
and set Start Speed
to (0, 0, -200), the spawned objects will fly away from the Camera at 200 units a second.
The World Force
parameter adds additional acceleration to the spawned objects. It is world-oriented. For example, if you want to simulate gravity simply set the World Force
's y value to -100.

If the Stop on Collide
checkbox is enabled, the spawned object's positions will not update after the moment they reach the Spectacles depth. This is helpful for having an object stick to a surface, rather than go through it.
Spawned Object Callbacks
You can set up some behavior to occur throughout the object's lifecycle: when the object is spawned, when it has collided, and when it is recycled at the end of the object’s lifetime.
To do this, add a script to the object under the Object Spawner object and expose your callback function as an API.
script.api.onSpawned = function () { // do something } script.api.onCollided = function () { // do something } script.api.onRecycled = function () { // do something }
- The OnSpawned function is called whenever an object is spawned
- The OnCollided function is called when the object reaches the depth distance of a depth surface if the Collide setting is enabled on ObjectSpawner script.
- The OnRecycled function is called at the end of the spawned object lifecycle. When the object is recycled, It is put back into the duplicate pool to be reused later. Use this function to return objects to their initial state (e.g. disable enabled child, stop 3d animation etc.)
Helper Scripts
This project has several example helper scripts that utilize the spawned object callbacks above to help add some basic behaviors to your spawned objects.
SetEnabledObject - Enables, disables or toggles an object

PlayStop3DAnimation - Plays 3D Animation, optionally plays another animation when complete, or stops a 3D animation

PlayParticles - Controls the External Time and Particle Count of a particle material in order to play or stop the particles. Additionally, it will create a duplicate of the particle material for each spawned object so that each spawned object instance can play their own particles.

External time checkbox should be enabled on the particle material.
Play2DAnimation - Plays a 2D animation on a baseTexture of a specified Mesh Visual.

Since all instances of an animated texture resource will play at the same time, this helper provides the ability to link another script which will create duplicate materials with duplicated animated textures to enable each object to play their own animated texture.
Without this material clone, you can see below that every time an object collides, all the animated textures in other objects are played as well.
By enabling Clone Material, you will be able to link an Animated Material Duplication Helper which will allow you to have each spawned object's animated texture play separately.

The Animated Material Duplication Helper has an array of animated texture duplicates, and a material to clone as an input. For each texture, the script will clone the material and apply the texture to it.

Note
The duplicated texture will continue to have one resource within the Lens, so duplicating the animated texture will not have an impact on your final Lens size.
TriggerBehaviorScript
If you need your own helper, you can write your own script based on ExampleHelper[EDIT_ME] script or by using TriggerBehaviorScript to trigger a Behavior.
For example, if you wanted to run a tween when an object is spawned, you can create a behavior that plays a tween.
Then, in the spawned object, add a TriggerBehaviorScript script, and add the Behavior script component to the list of values.

Note
There’s no need to set Trigger type on the Behavior script, it will be triggered directly by TriggerBehaviorScript
Depth Setter Component
Lastly, the template includes a DepthSetter Component, which uses the depth information from the Spectacles recording to occlude your 3D scene when it is behind a surface or a real objects in the video. You can enable or disable this feature by enabling or disabling the DepthSetter component on the Camera object.
Previewing Your Lens
You’re now ready to preview your Lens! To preview your Lens for Spectacles in Snapchat, follow the Lenses for Spectacles guide.
Related Guides
Please refer to the guides below for additional information:
Still Looking for help?
Visit Support