How to set Physics in face filters

Physics example

The physics property makes 3D models react to use head movements and behave by the law of physics. It allows for a more realistic and entertaining experience with AR effects.

Physics is usually applied in dynamic face filters featuring virtual hair or jewelry, to animate the movement of items with respect to gravity, head shakes or tilts.

image

Bone system

To enable physics simulation, you need to create a bone system in your 3D editor and attach objects to it or set up the skin.

image

Each object or system of objects should have its chain of bones. The chain must have at least two bones as each bone is affected by the next one in the hierarchy.

For the proper physics functioning, you need to assign a weight to each bone. You can also set gravity, constrain, and collision.

If you assign a unique gravity or collision to a unique chain of bones, you need to export this chain and its objects from a 3D scene as a separate FBX file.

Parameters and methods explanation

The physics property are enabled with the following methods:

Api.meshfxMsg("spawn", 1, 0, "mesh.bsm2") - initiates 3d mesh for a specific instance id

It has 4 required arguments:

  1. String with the message name ("spawn") sent to the render
  2. Mesh id (int).
  3. 0, no options.
  4. Name of .bsm2 file with mesh to spawn.

!glfx_FACE can be used instead of file name. It is face mesh from FRX.

Api.meshfxMsg("dynGravity", 1, 0, "0 -1000 0") - to apply gravity to mesh

It has 4 required arguments:

  1. String with the message name ("dynGravity") sent to the render
  2. Mesh id (int) spawned with Api.meshfxMsg("spawn")
  3. 0, no options.
  4. Gravitation coefficient โ€” the string "0 -1000 0", where 0 is x-axis coefficient, -1000 is y-axis coefficient, 0 is z-axis coefficient.

Api.meshfxMsg("dynImass", 0, 0, "bone1") - to add mass to mesh

It has 4 required arguments:

  1. String with the message name ("dynImass") sent to the render
  2. Mesh id (int) spawned with Api.meshfxMsg("spawn")
  3. Invert bone mass (float) (the range must be [0; 100]; common value is 5 - 10), invert means that 0 - infinity mass, 100 is no mass.
  4. String with the bone name.

Api.meshfxMsg("dynDamping", 0, 95) - to apply friction to your mask

It has 3 required arguments:

  1. String with the message name ("dynDamping") sent to the render
  2. Mesh id (int) specified in 2 argument of Api.meshfxMsg("spawn")
  3. Friction coefficient (int) โ€” the value that shows the relationship between your mesh and air (i.e. how air friction will influence your mesh movement when physics is applied).

Api.meshfxMsg("dynConstraint", 0, 100, "bone_3 bone_5") - to apply constraint to your mask. The parameter binds 2 bones and connects them as a rope.

It has 4 required arguments:

  1. String with the message name ("dynConstraint") sent to the render
  2. Mesh id (int) spawned with Api.meshfxMsg("spawn")
  3. Binding length - length of the rope.
  4. String with bones, which will be connected.

Api.meshfxMsg("dynSphere", 0, 0, "0 0 0 100") - to create a sphere that will interact with the objects.

It has 4 required arguments:

  1. String with the message name ("dynSphere") sent to the render
  2. Mesh id (int) spawned with Api.meshfxMsg("spawn")
  3. Sphere id, starts from 0. You can add several of them to one mesh.
  4. String with 4 parameters separated with spaces: x y z coordinates of sphere and its radius

Steps to apply physics

  1. Create a bone system for your .fbx model using a bone modelling tool in your 3D graphical editor. Convert your model using fbx โ†’ bsm2 converter with given arguments: -hierarchy -last_bones PATH_TO_FBX_FILE.

  2. Go to the effect directory and replace .bsm2 file with the converted one. Open config.js file and find the given snippet: Api.meshfxMsg("spawn", MESH_INT_ID, 0, "YOUR_FILE_NAME.bsm2");

Save the value in MESH_INT_ID to your clipboard or just remember it (_VALUE).

  1. Set mass to your bones using the snippet: Api.meshfxMsg("dynImass", MESH_INT_ID_VALUE, MASS_VALUE, "BONE_NAME"); where: MESH_INT_ID_VALUE: the value saved within Step 2. MASS_VALUE: invert mass (0..100), where 0 - infinite mass, 100 - no mass (typically value is 10), BONE_NAME: name of your bone. Please note, all root bones should have MASS_VALUE 0.

  2. Apply gravitation using the snippet: Api.meshfxMsg("dynGravity", MESH_INT_ID_VALUE, 0, GRAVITATION_COEFFICIENTS); GRAVITATION_COEFFICIENTS: is string "0 -1000 0", where 0 is x-axis coefficient, -1000 is y-axis coefficient, 0 is z-axis coefficient.

  3. Change all .vert files in cfg.toml file on physics.vert on all materials where physics is applied.

Example of config.js file with physics:

function Effect() {
var self = this;
this.init = function() {
Api.meshfxMsg("spawn", 1, 0, "mesh.bsm2"); // 1 is your saved value
Api.meshfxMsg("dynImass", 1, 0, "root"); // 0 is infinity mass
Api.meshfxMsg("dynImass", 1, 10, "bone_1");
Api.meshfxMsg("dynImass", 1, 10, "bone_2");
Api.meshfxMsg("dynGravity", 1, 0, "0 -1000 0"); //gravitation will be directed down by y axis
};
this.restart = function() {
Api.meshfxReset();
self.init();
};
this.faceActions = [];
this.noFaceActions = [];
this.videoRecordStartActions = [];
this.videoRecordFinishActions = [];
this.videoRecordDiscardActions = [this.restart];
}
Download example of Face filter with Physics
Last updated on