Let's Break It
Let’s Break It is a template that has scripts that help you bring 3d object-breaking physics into your lens. Break a 3d Object into multiple small pieces only when it collides with particular objects. The models can only break when the name of the scene object is in the filter script. Controlled collision of 3d Convex Physics body.
The Let's Break It Physics Template was created and provided by Kavin Kumar
Template Walkthrough
The template comes with two sets of examples. This template has several setups to showcase how you can create a lens with 3d objects breaking.
- Broken 3D Model: Create the broken 3d model and apply physics.
- Break on Collision: Break into small pieces when there is a collision.
- Controlled Break: Break objects with control.
- World Mesh Spawner: Break objects using World Mesh.
Broken 3D Model
Preparing the model
We need a base model that we are going to use. Next we need to create a new model using the base model.
Using any 3d Modeling software of your choice, break the model into multiple pieces. This will be the model we will use to show the breaking down.
Note: You can break down the model into any number of pieces. We will use a script to apply the physics body.
Setup in Lens Studio
Import both the broken model and regular model into the Lens Studio software. Set up a simple Physics scene by creating a floor by clicking + icon -> Physics -> Box body
.
Resize the box to make it a floor for the physics world. The physics box comes with a collider that will help us on physics set up. Convert the mass of the cube to 0 so it does not fall down into void. Now bring in the broken 3d model into the Scene Hierarchy
panel and place it on top of the newly created floor.
Now it's time to apply the physics body script to the broken model. Find the script ConvexMeshHelper
and apply that script on the broken model’s parent.
Make sure all the children of the bottom have mesh in them. As the script needs the Render Mesh Visual component to work.
You will see the broken model fall down on the floor. Yay!! The physics works now. You can toggle the debug mesh to see how the collider is being applied to the model.
You might see the models keep breaking automatically. This is where the next stage comes in. Where we break the model only on collision.
Break on Collision
Import the main model and position it the same as the broken model and apply the Physics Body component. In the physics body component’s select the shape type as mesh. Use the same mesh as the render mesh visual for the mesh tab.
Select the mesh on the render mesh visual component and right click. It will show an option to highlight. When clicked it will show the mesh from the asset panel you can then bring the mesh and use it.
You might see the bottles having wired glitch. It's normal. As both objects are overlapping in the same 3d space. We are going to solve it now.You can turn off the debug mode.
Now it's time to turn the visibility of the broken model and make it a child of the main model.
Now it's time to apply the onCollisionBreak
Script. Drag it and apply to the main model. You will see an option on the script called is Breaking
. If it's turned off the model will not break. Only when the option is turned on will the model break. This gives you the ability to break them only when you want.
Controlled Break
Now the objects keep breaking when a collision happens. What if you want it to happen only when a certain object touches the model. Like a moving ball touching the bottle, the bottle should break. This is where the second property of the script comes in. You might notice there is an option called Filter the collision
. This is where the name of the scene objects goes. What this does is that, when a collision happens it looks for the scene object's name. And when the name exists in the list of the names then the object breaks.
Here we have set up a simple object that moves when you tap on the screen using a tween. This ball is going to be our main object that's going to create the break.
Now let's set up the filter to only work. When we turn on the filter the object does not break. As it needs a list of names in order to work.
We can move the ball a little down and place it on the floor. When the filter is turned on and we tap on the screen to see if the bottle breaks using the ball. As shown when the bottle’s filter is turned on and it does not have the ball object's name. So when the ball interacts with the bottle it flies away, but it does not break. Once we add the ball in the filter list and repeat the same. We can see the bottle is broken.
We can add any number of name’s on the filter list.
Using this setup we can add multiple objects into the scene. Here is the difference between objects with filter and without filter. It shows how even colliding with the floor can cause the break to happen.
World Mesh Breaker
Allows physics interaction with the world mesh. Break 3d objects when they touch the real environment using World Mesh physics.
You can push the Lens to your device as you would with any other Lens, but your device must support World Mesh. You can preview how it would look on a supported device in Lens Studio using the Interactive Preview mode in the Preview panel.
World Mesh Collider
You can find the World Mesh
object in this example. Use the interactive panel to move around. You can visualize the World Mesh Collider by enabling the Show Collider
field in the Physics Collider
component.
Spawn Breakable Object
With Dropper
helper script, you can spawn breakable objects based on the camera position. Tap on the screen to spawn new bottles and see the breakable bottles colliding with the world mesh.
Previewing Your Lens
You’re now ready to preview your Lens! To preview your Lens in Snapchat, follow the Pairing to Snapchat guide.