Outline shaders for spine-unity

February 10th, 2020

Outline Shader Demo

We are happy to announce outline rendering support for all shaders in our spine-unity runtime! This makes it easy to show colored selection outlines around characters, animated Spine UI elements, and much more.

Outline Shader Enable/Disable

Each shader that comes with the spine-unity runtime now provides an additional Outline section. When you enable the Outline parameter of a material, the shader will automatically be switched from Spine/shader_name to its new Spine/Outline/shader_name shader variant, and back when you disable it.

The separate outline shader ensures that no render passes are wasted where no outline is needed and keeps draw call batching optimal.

Creating and switching to outline materials

Outline Shader Duplicate Existing Material

To create an outline material, first duplicate the original material.

Note: This is the *_Material next to your skeleton asset when using a normal SkeletonRenderer, SkeletonAnimation or SkeletonMecanim. For SkeletonGraphic it is SkeletonGraphicDefault.

Select the new material and enable the Outline property via the material inspector. This will automatically switch the material to the respective outline shader variant.

Now that the outline material is prepared, you can assign it to your SkeletonRenderer and SkeletonGraphic components as usual.

Apply via SkeletonRendererCustomMaterials

  • For SkeletonRenderer, this can be via done via SkeletonRenderer.CustomMaterialOverride or by adding a SkeletonRendererCustomMaterials component.
  • For SkeletonGraphic, you can directly set the SkeletonGraphic.material property.

If you want to see an example of outlines appearing when hovering the mouse over a skeleton, please have a look at the new example scene described in section Outline Shaders example scene at the end of this blog post.

Requirements for meshes and atlas textures

How the outline shader works - neighbourhood sampling

Normally, transparent pixels within a RegionAttachment or MeshAttachment do not contribute to the frame buffer. In our outline shaders, each transparent pixel will now test whether there are opaque pixels in its neighbourhood at a certain distance. If there are, the outline color will be drawn.

Note: This requires an additional render pass of the affected skeleton to maintain correct draw order.

Image and atlas textures - add padding or whitespace

Outline Bleeding across Images

When you see outline color appearing at borders of attachments as shown above, it is most likely the outlines of nearby atlas image regions, growing into your mesh.

You can add more padding via the atlas export settings to leave enough space for the neighbour outline. You may also consider disabling whitespace stripping instead of increasing the padding.

Export Settings for Outlines

Alternatively, you can reduce outline width until this problem no longer occurs.

Mesh attachments - leave space to not cut off the outline

Outline Cut by Mesh

The outline is only drawn within the mesh borders. When you see parts of the outline cut away as shown above, your mesh borders are likely being too tightly wrapped around the opaque pixels of your images.

To fix this problem, move the mesh vertices further out to provide enough space (shown in green below). You may need to add more whitespace around the borders of your images.

Outline Move Vertices Outwards

Alternatively, you can reduce outline width until this problem no longer occurs.

Outline shaders example scene

The spine-unity runtime package now comes with an additional example scene, Spine Examples/Other Examples/Outline Shaders. This scene shows how you can switch to prepared outline material variants of existing materials on mouse over. The example scene uses EventTrigger components of the Unity event system to react to mouse pointer enter and exit events.

Conclusion

These additions to our spine-unity runtime make it easy to add colored outlines to your skeletons. Don't hesitate to share your thoughts and creations on the Spine forum!

Mesh binding tutorial

January 21st, 2020

Many characters rigged in Spine require additional images for some of their parts during their animations, for example to add perspective or visual effects. When these new parts are meshes, it can be tricky to bind them to bones and setup weights because the pose where they are needed is in an animation, not the setup pose. This post covers how to do exactly that!

Binding in animate mode

Consider this bird's wings:

We'd like to have a "flapping" animation that uses a separate mesh for the downward and upward wings, to add visual variation to the bird. We don't want to add additional bones to do it, which would clutter the skeleton and make it difficult for the two meses to behave as the same wign. Instead, the wing bone should control both meshes.

For our bird, the setup pose looks like the left side in the image above. The downward wing is bound to the body and wing bones. When the wing bone moves up and down, the mesh deforms to look like the wing is flapping.

The upward wing mesh is tricky: we want the wing bone to control the tip of the upward mesh, as it does the downward mesh, but in the setup pose the wing bone is not positioned at the tip of the upward mesh. If we bind the upward mesh to the wing bone in the setup pose, it will make controlling the mesh difficult.

The solution is to bind the bones in animate mode (note that Spine 3.8.77 or later is required). You can download the bird project if you'd like to try doing it yourself. Open bird-before.spine and go to animate mode. Move the timeline position to frame 5, which is when the wing bone is at its highest. Next, we need to place the upward wing in the right place with the Translate tool:

If Auto Key is enabled, moving the mesh in animate mode will create a deform key, as in the image above. That is fine, the following steps will work either way.

Next, click Bind in the Weights view and choose the body and wing bones. Use the Weights tool to set the top 2 vertices 100% for the wing and the bottom 2 vertices 100% for the body:

Once that is done, both wing meshes are controlled by the wing bone, making it very easy to control when switching between them:

If you show the upward wing in setup mode, you'll find that it looks odd:

This happens because it was bound to the wing bone when it was moved up in the animation, but the wing bone is moved down in the setup pose. It is normal for a mesh to look strange in setup mode when it is intended for use in a pose from an animation, not in the setup pose.

Update bindings

The important part is that the bones are in the correct position when they are bound to the mesh, which is called the "bind pose". When the bones are moved from the bind pose, the mesh will be deformed based on the mesh's vertex weights. Let's look at what happens if bones are bound in the wrong position and how that can be fixed.

Start over with the bird-before.spine project, but this time in setup mode show the upward wing and move it into position with the Translate tool. Bind it to the body and wing bones, then show the downward wing.

Next go to animate mode and move the timeline position to frame 5. The upward wing only moves with the body bone because we haven't set weights for the wing bone yet. However, if we set the weights as we did before, the result is not what we want:

Whenever the vertices move when you are adjusting weights, it means that the bones have been moved since the bones were bound to the mesh. In this case, the wing bone has been moved up from where it was bound, so the vertices will move up when weighted to the wing bone.

The Update Bindings button in the Weights view can help in this situation! When clicked, it resets the bind pose for the bones using the current bone positions and mesh vertex positions. Afterward, when weights are changed the vertices will not move.

Back in Spine, on frame 5 in animate mode, click Update Bindings, then set the weights for the wing bone. You'll find the vertices don't move and the end result is the same as if we had bound the bones in this position.

Tips and fixing mistakes

You may need to adjust the pose of bones which are bound to a mesh, without affecting the mesh. To do so, enable image compensation and adjust the bones.

You may need to redo all the weights for a mesh. To do so, click Reset in the mesh properties at the bottom of the tree. This will remove all weights and reset the mesh so it is not deformed. If you want to keep the current vertex positions, you can remove the bones in the Weights view instead of using Reset.

It can be helpful to create an animation that keys the bind pose for the bones bound to a mesh. This allows you to get back to the bind pose for adding new bones or redoing the weights for a mesh. In the bird project we can do this by going to frame 5, but this may not be easy to remember and later the bones on frame 5 might have be moved.

Conclusion

Binding bones and setting weights can be a little tricky, but is a powerful feature. Showing different meshes in the middle of animations is very common and the workflows described above make it quite easy.

We hope you found this article interesting and we'd love to continue discussion on the Spine forum.

2D and 3D physics for spine-unity

December 2nd, 2019

Hinge Chain Demo

We are happy to announce improved support for HingeJoint and HingeJoint2D chains in our spine-unity runtime! This makes it easy to add realistic cape physics to your character, have it drag heavy objects around, swing a morning star, or much more.

HingeJoint and HingeJoint2D components are used to connect a Rigidbody or Rigidbody2D to one another, constraining them to move like they are connected by a hinge. It is perfect for doors, but can also be used to model chains, pendulums, and similar objects. When set up correctly, Unity's physics will apply momentum and collision response to all the chain elements. An important requirement is that chain elements must be laid out flat on the same hierarchy level, instead of forming a parent-child hierarchy.

Previous functionality

You might note that this functionality existed in previous spine-unity versions. You may have given it a try, but likely were unhappy with the result, no matter how you tweaked the Rigidbody parameters. The reason was that gravity was being applied, but it was not able to capture movement of the skeleton or parent bones, thus lacking momentum. Starting in the spine-unity 3.8, the physics rigging respects momentum and works correctly when flipping the skeleton.

Creating hinge chains

2D and 3D hinge chains are created from an existing SkeletonUtilityBone hierarchy.

Hinge Chain Setup

Select the first SkeletonUtilityBone chain element and in the Inspector choose Create 3D Hinge Chain or Create 2D Hinge Chain to generate the physics rig. The selected element and all its SkeletonUtilityBone children are turned into a hinge chain. Adjust the Rigidbody drag and mass parameters to tweak the result to your liking. Raising the drag value will make the Rigidbody move slower and create the effect of being heavy or interacting with air.

Note that the chain root node is no longer parented to bones of the skeleton, but placed at the top hierarchy level of the scene. This is a requirement in Unity to have momentum applied properly. Do not reparent the chain root to bones of your skeleton, or chain elements will no longer be affected by skeleton movement!

2D hinge chains

  1. Create your SkeletonUtilityBone hierarchy as usual.
  2. Select the first chain element in the Scene panel and in the Inspector choose Create 2D Hinge Chain to create the 2D hinge chain rig. Create Hinge Chain 2D
  3. This will remove the GameObject chain from the previous parent (cape-root in the example) and place a new HingeChain Parent GameObject at the top level of the scene. As mentioned above, do not reparent this GameObject to the skeleton! Hinge Chain 2D Hierarchy
  4. Adjust the chain element Rigidbody drag and mass parameters to tweak the result to your liking.

Note that this GameObject contains two child objects, named Hinge Chain and Hinge Chain FlippedX. When the skeleton is flipped, these GameObject instances will automatically be activated and deactivated to enable the respective hinge chain.

3D hinge chains

  1. Create your SkeletonUtilityBone hierarchy as usual.
  2. Select the first chain element in the Scene panel and in the Inspector choose Create 3D Hinge Chain to create the 3D hinge chain rig. Create Hinge Chain 3D
  3. This will remove the GameObject chain from the previous parent (cape-root in the example) and place a new HingeChain Parent GameObject at the top level of the scene. As mentioned above, do not reparent this GameObject to the skeleton! Hinge Chain 3D Hierarchy
  4. Adjust the chain element Rigidbody drag and mass parameters to tweak the result to your liking.

When the skeleton is flipped, the HingeChain Parent GameObject will automatically be rotated by 180 degrees to adjust the hinge chain to the flipped bone locations.

These additions to our spine-unity runtime make it easy for physics to affect your skeletons for more dynamic behavior. Don't hesitate to share your thoughts and creations on the Spine forum!

Holding a two-handed weapon

November 22nd, 2019

final result

Ever wondered how to create a setup in Spine that would allow your character to hold a weapon, but also throw it away if needed? Wonder no more! In this blog post we'll explain step-by-step how to do exactly that, expanding on one of our tips.

Spine Professional is required to follow this tutorial as we will use IK constraints and transform constraints to achieve our goals.

Skeleton setup

initial skeleton setup

We start out with a basic body rig and create a bone representing our weapon. We'll omit image and mesh attachments in this tutorial so we can more easily focus on the skeleton structure.

For the weapon bone to be able to move freely, it must not be a descendant of the body bone. A good place to parent it to is the root bone of our skeleton.

The above image illustrates this initial setup. The weapon is represented by the green bone called weapon, while the rest of the body follows a pretty standard humanoid bone hierarchy setup.

In addition to the weapon and body bones, we also created 2 bones called hand-on-weapon-l and hand-on-weapon-r. These will become IK constraints targets that help us to keep the hands of the body rig on the weapon.

Constraints setup

Once all the bones are in place, and with the weapon and the body being separate within the bone hierarchy, we can connect the two via constraints.

Creating the IK constraints

ik setup

First, we want to make the arms follow the left and right hand attachment points on the weapon. We start with the left arm by selecting the two arm bones arm-l-up and arm-l-down and creating an IK constraint with hand-on-weapon-l as the target. We repeat this process for the right arm.

The two arms will immediately try to point to their respective target bones. We want to disable this behavior in setup mode so we can more easily animate poses and movements without the weapon. For each of the two IK constraints, change the Mix from 100 to 0. This way the arm will use its original pose instead, allowing us to animate it as we please.

Creating the transform constraints

transform setup

As a final touch we want to ensure that the hands will match the weapon rotation, so they look as if they are actually holding the weapon instead of following their respective parent's rotation. We can achieve this by selecting hand-l and creating a new transform constraint with the bone weapon as the target. The same needs to be done for the other hand bone. To make things look right, we set the offset rotation to 90. We keep all the mixes at 0, so the hands don't rotate with the weapon in setup mode, again making it easier to animate weaponless movements and poses.

Creating animations following the weapon

When creating an animation where the arms and hands should follow the weapon, we simply key the mixes of the IK and transform constraints to a value of 100. Voila, the hands and arms will follow the weapon!

ik animate

transform animate

If everything went well, your final setup in animate mode could look like this:

final skeleton setup

Variations

It is possible to constrain both hands to the weapon using a single transform constraint. In general, fewer constraints is better because it makes the rig simpler. However, this may be an inconvenience if the character needs to detach only one hand from the weapon.

The character could use IK constraints on the arms when not holding a weapon. In that case, the mix for those IK constraints can be set to 0 when the mixes for the constraints to hold the weapon are set to 100.

Conclusion

final result

The character can hold the weapon with one or both hands, throw away the weapon, do tricks with it, and so on. Since it can be turned on or off, multiple similar setups can be used for different weapon types, such as a pistol, shotgun, machine gun, spear, etc. The constraint mixes can be set in animations or by code at runtime.

You can download an example of this setup here.

We hope you'll find this little setup useful for your projects! Feel free to discuss this post on the forum.

Older