top of page

Bitmasked States and application in Shattered

Writer's picture: Sean BraithwaiteSean Braithwaite

I needed a system that allowed for the shattering of meshes. Research showed there are a number of visually-polished and exciting ways to achieve this, but short of buying an asset package or authoring complicated scripts, I couldn't find anything simple. So I set out to make a simple system which I could use to achieve our mesh shattering effect.


In addition to being simple, there were several other requirements I identified for the shattering scripts:


  1. Work with animating meshes and static meshes

  2. Support multiple states of shattered mesh (missing left/ right arm)

  3. Support congruent states of shattered mesh (can go from missing left arm to missing left AND right arm with ONLY the right arm playing shatter effects)

Our animator provided a skinned mesh in whole and pre-cut into shattered pieces. This meant that we could use the submeshes of the 'shattered' mesh to create the pieces we needed. These submeshes had skinned mesh renderer components in the editor, allowing us to bake the deformed mesh when the shatter effect was required. This left the following challenge regarding the individual submesh objects:


  1. Identify the pieces and release them from the skin as rigidbody props

The Result

After some experimentation and research, the challenge was met by a solution which provided the following features:


I refer to bitwise masks here, which are type int used as a bitwise I/O mask


  1. Shatterable.cs, exposing an int 'bitwise' field and public method Shatter();

  2. A bitwise 'shatter' state system composed of:

  3. ShatterState.cs, exposing an int 'bitwise mask' field (container for shatterable)

  4. ShatterStateController.cs, which stores a bit mask of states passed and calls 'Shatter();' on Shatterable.cs not included in its mask


How it Works

Each ShatterStateController.cs' children contain one or more ShatterState.cs. Each ShatterState.cs' children contain one or more Shatterable.cs.


The ShatterStateController transitions ShatterStates by a bitwise mask signature. When a ShatterState is used, its bitwise mask's true bits are imprinted on the ShatterStateControllers' bitwise mask. If the ShatterStateController is told to transition to a state whose bitwise identity is already enabled, it doesn't transition. This prevents returning to a previous type of shattering effect. Otherwise, when the ShatterStateController transitions to a new state, it calls Shatter(); on that state's Shatterable children whose Shatterable mask bitwise OR's false in the ShatterStateControllers' bitwise mask. What this results in is a system where a character can have the following steps happen:


A.

  1. Full Mesh

  2. Left-arm is shattered

  3. No Left-Arm Mesh

  4. Left-arm creates shattered objects

B.

  1. Full mesh

  2. Left and Right arms are shattered

  3. No arms mesh

  4. Left and right arms create shattered objects

C.

  1. Full Mesh

  2. Left-arm is shattered

  3. No left-arm mesh

  4. Left-arm creates shattered objects

  5. Right-arm is shattered

  6. No arms mesh

  7. Right-arm creates shattered objects

In the C example, only the right arm creates shattered objects on the last step as the left arm was previously broken, despite going to the same no-arms mesh as in example B. This allows for us to reuse the states and meshes while respecting the sub meshes that have already been shattered. This is critical as we don't want an effect to fire twice, and for shattering effects to only happen once per ShatterStateController.


Resetting the ShatterStateController is simple too, as the bitwise mask is merely cleared and the initial state returned to.

Wrap Up

The resultant code was less than a couple hundred lines and simplified the implementation and integration of pre-cut meshes and shattering effects. Notable, this system was later used on environmental props and static meshes which extended its functionality for our team. As a result of associating shattered submesh pieces with a bitwise mask, they could be easily identified from the bitwise state system.

コメント


コメント機能がオフになっています。
bottom of page