Morph


One of the most powerful features of MegaFiers is the Morpher. This allows you to change a mesh or part of a mesh from one shape to another which allows you do such things as facial animation, lip syncing, avatar systems or apply damage to vehicles and buildings. One of the feature of the morphing system used in MegaFiers is it allows for any number of channels of morphing and for any number of progressive targets to make up a channel and when playing back the morphs you can mix any amount of channels together. The morpher also only morphs vertices that have morph data so if you are only using a few vertices on a mesh only that data is saved in memory and only those vertices are effected thus making morphs very memory efficient and fast.

The best way to use the morphing system is by making use of one of the custom exporters available for either 3DS Max, Maya, or Blender (there is also a 3rd party tool available for Lightwave), currently the exporter is available for PC Maya 2011, 2012, 2013, 2014, 2016 and 2017 32 and 64 bit and all versions of 3ds max from 2010 on wards both 64 and 32 bit and all versions of Blender since version 2.58. We do also have an exporter for the OSX version of Maya 2013 and 2014 64 bit. Using the exporter means you can get your complex morphs from your 3d package into Unity with just a couple of clicks and as well as giving you all the animation data you can also export the animations of the channel percents and have those play back. If you don’t have access to any of these 3d packages or use one that doesn’t have its own exporter you can still build your morphs up using the universal OBJ format and a guide on how to do that is given below.

Info

Life is much easier with the custom exporters for 3DS Max, Maya and Blender. If you want the exporter please contact us with the version you require. Note only newer versions of the exporter will be sent ie if you request Max 2011 and Max 2012 only 2012 will be sent, and if 32 and 64 bit versions are requested again only the 64 bit version will be sent.

Morph Param Description

Import Morph File

If you have exported a .mor file then click this to select the file for loading. Once loaded all the channels etc will be set up for you.

Animate

If the imported mor file has animation data then this will be set which will open up further params below it. If you have animation data attached to a morph and you don’t want it played back clear this.

AnimTime

The length of the animation data in the .mor file.

Loop Time

Time at which to loop the animation.

Speed

Speed to play back the animation usually this is 1 for normal playback but you can change this to speed it up or slow it down or even make it run backwards.

Repeat Mode

How the animation repeats, either loop, ping pong or clamp.

Load Mapping

Clicking this allows you to select an OBJ file to be used for generating the mapping of Unity vertices to original morphed vertices. This is needed due to Unity reordering and adding vertices during importing of FBX files. Note you only need to use this if you are building morphs from OBJ files. This button will be Red if no valid mapping is currently attached to the morph and will go Green is the mesh has been correctly mapped.

Add Channel

If you are building a morph by hand using OBJ files you can Add a new channel by clicking this button.

Hide Channels

Hides all the channel data in the inspector.

Channel Button

Each channel will have a button to open and close its inspector. The name on the button is the channel name and the number is the channel number, these can be handy to know if you want to script the morpher. If the channel is closed you will only see a Percent slider for it else you will see all the target info that makes up the channel (See below)

Percent

Amount of the channel being applied to the morph end result. 0 – 100.

Channel Col 1 & 2

The tint of the channel buttons in the inspector.

Memory

Amount of memory used by morph, this is outdated now and shows 0.

Channel Compressed

The amount of memory used by the morph, this replaces the above value as this shows the value for the new compressed data.

Channel Param Description

Name

Name of the channel.

Active

If you have channels that aren’t needed you can turn them off so they aren’t checked and added to the end result.

Percent

Amount of the channel being applied to the morph end result. 0 – 100.

Tension

If you have more than one target in the channel you can alter this value between 0 and 1 to change how linear the transition is, 0 is linear 1 is a full bezier interpolation, a value of 0.5 works well.

Use Limit

Whether or not the percent is limited. If it is the values below will be used to clamp the percent value.

Min

If limited this is the lowest amount.

Max

If limited this is the max amount

Load Targets

If you are building

Add Target

Create a new target slot.

Delete Channel

Delete the entire channel.

Targets

Click this to reveal the target params.

Name

Name of the target.

Percent

The position in the channel percent where this target will be the full contribution to the channel morph.

Load

Load an OBJ file to make a new target.

Delete

Delete the target.

Up

You may need to reorder your targets and you can move the target Up the list with this button.

Dn

You may need to reorder your targets and you can move the target Up the list with this button.

Morph Classes

public class MegaMorphChan
{
    public string       mName;
    public float        Percent;
    public bool         mActiveOverride;
    public bool         mUseLimit;
    public float        mSpinmax;
    public float        mSpinmin;
    public float        mCurvature;
}

public class MegaMorphBase : MegaModifier
{
    public List<MegaMorphChan>  chanBank;
}

public class MegaMorph : MegaMorphBase
{
    public bool             UseLimit;
    public float            Max;
    public float            Min;
    public bool             animate;
    public float            atime;
    public float            animtime;
    public float            looptime;
    public MegaRepeatMode   repeatMode;
    public float            speed;
}

Some methods are provided in the Morpher Class to control the morph via script and to help do custom animation solutions or make use of the morphing system in something like say a character customization system or avatar generator.

int NumChannels()

Returns how many channels are attached to the morph.

Returns int value with number of channels.

void SetPercent(int i, float percent)

Set the amount of morph for a channel.

Parameters
i – channel to set
percent – value between 0 and 100 for the percent of the morph

string[] GetChannelNames()

Returns a string array with all the channel names for the morph. Useful if you are doing a GUI for example.

MegaMorphChan GetChannel(string name)

Returns the channel for the given name or null if no channel found.

Parameters
name – Channel name to find and return

void SetPercent(int channel, float percent, float speed)

Will change the channel percent value to percent using the speed value.

Parameters
channel – index of the channel to change
percent – The value to move the channel towards
speed – How fast to change the value in percent per second, so to change from 0 percent to 100 percent in 5 seconds speed would be 20.

void ResetPercent(int[] channels, float speed)

Will reset all the channels in the channels array to 0 using the speed value.

Parameters
channels – An array of int values representing the channel indexes to be reset
speed – How fast to move the channels to 0 percent.

float GetPercent(int channel)

Get the current percent value of a channel.

Parameters
channel – The channel whose percent value will be returned.

void SetAnim(float t)

If the morph has animation data attached this will set the current position in that animation.

Parameters
t – Time value to set the animation to.

Simple Example

A very simple example to set the percent of all channels to 50%

void MorphChange(GameObject go)
{
    MegaMorph mr = (MegaMorph)go.GetComponent<MegaMorph>();

    if ( mr != null )
    {
        int num = mr.NumChannels();

        for ( int i = 0; i < num; i++ )
            mr.SetPercent(i, 50.0f);
    }
}

Adding Morph Data from OBJ files

The morph system uses OBJ format files to provide the morph data. If you are getting odd results from the morpher it will be because of OBJ files that are not valid. You need to make sure that the OBJ files have the same number of vertices in as the mapping file and that the vertex order has been preserved. It appears some tools, lightwave for example, have a different way of exporting Obj files that will reorder the vertices, usually due to removing or welding vertices.

To prepare your meshes for use by the morpher you should first align all the targets and the base mesh to 0,0,0 in the editor software. You should then select the base undeformed mesh and export just that to a .obj file calling it something like BaseMapping.obj. The morph system will use this to match the unity vertex information to the morph vertex data. This is required as unity will very often add new vertices to a mesh when it imports it. Next you will need to export your targets. The morph system allows for any number of morph channels and each channel can have any number of targets making up the morph, so you could export each individual target as a separate obj file, but a better way is to select all the meshes that make a morph and export to a single obj file. When you load the targets you will be able to change the order and add or delete targets if you need.

When you apply the Morph modifier to an object you will see a red button called ‘Load Mapping’ you need to click this and load the base mapping obj file. If the import went ok the button will go green to show you have valid mapping information. If the import fails it is probably because you didnt centre everything on 0,0,0 before exporting or the scales are wrong. Unity will apply a scaling to a mesh on import and you need to make sure the obj export is set to the same scale. If you think it is a scale issue there is a importScale value in the Morph interface you can adjust and try again.

Once you have the mapping in you will need to add a channel. You do this by clicking the Add Channel button. A new set of params will show up in the inspector. Click the Load Targets button and select the target obj file. The modifier will load all meshes in that file as targets and assign weights based on the order of meshes in the obj file. It may be that the order is not correct, if so use the Up/Dn buttons to reorganise the target list. You can also delete a target if you need to. If you select Load Targets again it will update any targets that are already imported and add any new ones it finds. You can also change the percentage at when each target is used to adust the animation. Also the morph uses a bezier interpolation system to give a much smoother animation result but sometimes this isnt desired, if so set the tension value to 0 to make it linear, a value of 0.5 usually gives a nice smooth result.

Each channel has a button to open and close the params just to tidy up the interface a little, the channel percentage slider will stay visible though so you can easily play with settings.

Quick guide to adding morph data to the modifier.

Step 1 – Create you meshes

Build your models and the morph targets as normal in your favorite 3d program.

Step 2 – Export

Export just the base mesh to an fbx as you would do for a normal game object.

Step 3 – Create Mapping

Export that same mesh to an OBJ file and call it something like BaseMapping.obj, this will be used to remap the unity imported vertices back to the original vertices from the 3d package, this has to happen as unity adds vertices during import based on smoothing groups, texture coords, tangents etc etc

Step 4 – Prepare Targets

Center all the morph targets at 0,0,0 and export them as either single obj files or if it is one morph anim as one obj file. The repositioning to 0,0,0 is needed as obj files do not save local vertex positions but world positions, I hope to remove this restriction in a future update.

Step 5 – Open Unity

OK that’s the ground work, so now we are in Unity.

Step 6 – Import Base Mesh

Import your base mesh and add it to the scene, add a modify object component from the component/megafiers menu and then add a Morpher component.

Step 7 – Create Morph

In the morpher inspector you will see a red button called ‘Load Mapping’ click that and select the BaseMapping.obj file you saved in step 3. The button should go green showing a successful load, if it doesn’t then my auto-detect has failed and in that case if you could send me the fbx (in ascii format) and the obj file I can figure out what I need to to do to make it work, but I think I have most packages covered now.

Step 8 – Add Channels

Next click the Add Channel button. The morpher can support any number of morph channels and each channel can have any number of targets to make up that channel. You may not need the multiple targets but the option is there. Anyway click the Load Targets button and select the obj file you exported in step 4. You should then see the targets loaded.

Step 9 – Working Morph

You should now have a morphing object, dragging the percent slider on the channel should change the object.

Exporters

We currently have exporters available for the following packages.

3DS Max 2009 to 2018 32 and 64 bit versions.
Blender v2.58 onwards.
Maya 2011 2012 2013 2014 2016 2017 32 and 64 bit versions (PC Only).
OSX Maya 2013 2014 64 bit.


If you require an exporter please contact us with your invoice number from the Asset Store and which exporter you need and we will get it to you. There is also a 3rd party plugin for Lightwave called Morph2Objs

When using the exporters you need to select the object that has the Morph modifier or Blendshapes attached and then choose the Export Selected option from your 3D package export menu. If you are using Maya you can not just click the object you need to click the ‘Select’ button in the BlendShape Attribute panel instead.

Reasons why Mapping Fails

Even with the exporters you could well be presented with an error box when you load a mor file saying the mapping has failed. This means that the system could not find corresponding vertices for the base morph vertices in the Unity mesh data. This can happen for various reasons.
Pivots
Try not apply any rotations, translation or scaling to the pivots of any base object or targets.
Working Pivots
Try and make sure you don’t alter the working pivots of any base mesh or targets as this will add a rotation and a translation to the morph data which will result in a mapping failure.
Exporting from FBX pose
You need to make sure that when you export the mor data that the mesh you are exporting from matches the FBX version of your mesh. For example if you are morphing an animated human character when you export to FBX the T pose mode is used but in your scene the character could be in an idle pose so the mesh data no longer matches the fbx data.
Mesh Smooth applied after Morphing
The system will not work if you have any system in place that alters the mesh after the morphing/blendshapes are calculated ie mesh smooth. As this will change the mesh that Unity sees compared to the internal morphing data that is exported.

Morphing example


Video guides to the Morphing system

Using 3DS Max to get morph data into Unity

Using Maya to get morph data into Unity

ZBrush Click to Play

Using Blender to get morph data into Unity

You must be logged in to post a comment.