Game Development Community

dev|Pro Game Development Curriculum

DTS with Morphs

by David Mathews · 08/12/2005 (5:42 pm) · 34 comments

Download Code File

For a game I'm building, I wanted to use morphs for some of the character animations. But instead of the vertex animation (which makes huge file) I wanted to predefine a morph by just those points I needed to move and then control them with a float value on each frame, 0.0 being no morph applied and 1.0 being fully applied, with values greater than one and negative allowed.

My implementation defines morphs for detail level 0 only, assuming farther away they will not be visible and to keep the runtime impact down. Morphs are much faster then bone animation, but every little bit is important.

As you can see in the DTS shape.h and mesh.h files, I added morph controls to the shape and sequence definition and morph data to the mesh definition. I added it to the base mesh in the engine, so you can use morphs on both skinned and standard meshes (boned and unboned).

In the engine, morphs are only applied if the current frame value is not 0.0. If 0.0, the morph processing is skipped.

All the places I made changes in the files (there are a lot) are marked with my initials - DSM, so you can find them easily. Code changes are only in the ts engine folder and DTSsdk folder.

Since the coding is done for both the engine and the exporter library, hopefully this will make it into TGE 1.4 and TSE (I'd be happy to put it in either of those guys, if desired - I have a TSE copy also).

I have tested some simple human models with morphs and I'm now building my game models that will use several morphs each.

Code Example for loading a morphs in an exporter:

In the Mesh loader -
// set dummy entries for morphs - setting local morph to shape morphs to be the same (same as for bones)

for(i=0;i M->morphIndex.push_back(i);
}

// push morph data
for(i=0;i for(j=0;jused;j++){ // for each vertex referened by this morph (only verts affected by this morph - you do NOT need to use them all!!
int curPt = mlist[i]->offsets[j].ptindex; // getting vert index

M->mindex.push_back(i); // which morph
M->mvindex.push_back(curPt); // which mesh vert index
M->mverts.push_back(DTS::Point(mlist[i]->offsets[j].offset.x * Scale,-mlist[i]->offsets[j].offset.z * Scale,
mlist[i]->offsets[j].offset.y * Scale)); // morph offset at setting 1.0

}
}

IN the shape loader - I do after materials and before nodes

// Load list of morphs

DTS::Morph mp;

mlist = curmesh->GetMorphList();
mcount = curmesh->GetMorphCount();
for(i=0;i mp.name = addName(mlist[i]->name) ;
morphs.push_back (mp) ;
morphDefSettings.push_back (mlist[i]->value) ; // can be just 0.0
}

Added to sequence set up- I do during bone export, so I only set them up when processing bone zero

s.baseTranslation = nodeTranslations.size() ;
s.baseRotation = nodeRotations.size();
s.baseMorph = morphSettings.size();


s.matters.morphs.assign (mcount, false);

for (i = 0 ; i < mcount ; i++)
{
s.matters.morphs[i] = true;
}


for (i = 0 ; i < bcount ; i++){
for (j = startSeq ; j <= endSeq ; j++){
if(i == 0){ //set morph settings only for bone zero (will be the same for all bones)
for (k = 0 ; k < mcount ; k++) // for each morph
{
morphSettings.push_back(mlist[k]->value); // morph setting for this frame
}
}

That's all the code I added to my ToolBox DTS exporter to support morphs (based on the milkshape exporter example).

Why morphs? They are faster than bones, they can simulate hard to do dynamics (I use them to animate hair and cloth for one) as well as the usual morph tricks - face animation, talking, changing from one critter to another, pulsing objects.

Have fun with morphs!

This is my first contribution to Torque. I'm building a game using just my ToolBox app (my pet project) and Photoshop in addition to TGE. This is making me add all the parts I find missing - like I just added a texture resize function, as my textures are always an invalid size.
The new map2dif got past a lot of dif export problems I was having. I can finally export all the special node and light types and just added the path nodes so I can pre set them up in ToolBox.

About the author

Recent Blogs

Page «Previous 1 2
#1
08/12/2005 (5:43 pm)
Very cool resource. Might make its way into 1.4. :)
#2
08/12/2005 (7:24 pm)
Neat I like morphing
#3
08/12/2005 (7:47 pm)
This is very cool. I have a question: Would it be possible with this to control the morph values on-the-fly with script? That would open up a huge amount of possibilities for dynamic animation.
#4
08/12/2005 (8:29 pm)
I would think it would be easier to switch between different morph animations, since that is already supported. Since even a complex morph uses only one value per frame, morph animations are very small, you can define a lot of them using multiple morphs.

But since morphs are value based (not the more complex rotations), it would probably be pretty easy to just add a script controlled factor that would be applied against the animating morphs, greatly reducing or increasing their effects. Making a moving or cycle go from nonexistent to extreme under script control. Just a predefined variable that has script access added.

Does that make sense?
#5
08/13/2005 (1:16 pm)
can these be blend animations?
#6
08/13/2005 (4:29 pm)
Yes, this needs to make it to 1.4, Ben.

Very awesome resource, David!
#7
08/14/2005 (11:39 am)
Cool stuff David, was tinkering with a hair anim a couple of weeks ago that could use this.

Any chance you'll be releasing the TSE version aswell ??
#8
08/14/2005 (3:16 pm)
*cough*Blender*cough*

Any chance of someone updating the blender exporter to support this? Unless I'm completely mistaken, the blender exporter is completely written in python and would require a modification to support this new type.
I personally do not know python - or even have the knowhow on DTS specifics... anyone else up for the task ;).

- Eric
#9
08/15/2005 (6:34 am)
Great resource but i agree with eric... please, blender support would be very nice expecially if it's going to be in 1.4
#10
08/16/2005 (6:25 am)
Eric & Hadoken,

Do not worry, i will be updating the blender exporter to support this format update.
#11
08/16/2005 (8:01 am)
David,

I'm a bit puzzled about the relationship between the Morph (morphs) and its "Default Value"
(morphDefSettings).

From your above example, it looks as if these two arrays go together (aka default value morphDefSettings[i] is used by morphs[i]). However, in your format, these arrays seem totally seperate, aka:

if(gVersion > 24){				//DSM
		 stream >> numMorphs ;
		 stream >> numMorphDefSettings ;
...
		 morphs.resize (numMorphs) ;
		 stream >> morphs ;

		 morphDefSettings.resize (numMorphDefSettings) ;
		 stream >> morphDefSettings ;

Couldn't this value be merged into the rather sparse Morph structure?


-- EDIT --

Ok, now there's something else for me to moan about ;)

In DtsMesh, you seem to have 3 size variables written out for the mindex, mvindex, and mverts arrays. From your example, i'd say that all of these arrays should be of equal size :

stream << (int) morphIndex.size();
	      stream << morphIndex;
	      stream << (int) mindex.size();
	      stream << mindex;
	      stream << (int) mvindex.size();
	      stream << mvindex;
		  stream << (int) mverts.size() ;
		  stream << mverts ;
		  stream.storeCheck() ;
(Ignoring the morphIndex of course)

This seems similar in concept to the SkinMesh bone weights, as in :

stream << (int) vindex.size();
         stream << vindex;
         stream << vbone;
         stream << vweight;

         // Vertex bone to node table
         stream << (int) nodeIndex.size();
         stream << nodeIndex;
(morphIndex = nodeIndex, mindex = vbone , mvindex = vindex, mverts = vweight)?

With all of these seperate array counts, it kindof wastes space (ok, a little, but it all adds up) :)
#12
08/20/2005 (10:13 am)
Try again! (number 3) I wrote a long post and then it doesn't post.

Morph default value - What the morph will be if not specified in an animation. IE the default setting. These are optional but can be used to allow morphs to help define the figure (several variations of the same model using morphs to change certain features). It is one float per morph of the model, so it should be pretty small.

The shape has a master list of morphs (all morphs in all subobjects)
Each mesh has the list of morphs that apply to this shape - morphindex
For each altered point in each morph for this mesh you must defined the morph, point and offset-
mindex, mvindex, mvert ( there is no equivalent to the bone weight - that is done by the setting in the sequence)
I am makiong additions this week to allow morphs on any detail level (your option). I'm doing them on only detail level 0 for my models, so I need to keeo the morphindex.

Morphindex could be dropped by requiring you to use the correct index number from the shape list in each mesh. did it this way to follow the bone setup standard.

These are easy blend animations, expecially mixed with node animations, since they affect different things. (Actually morphs are ALWAYS blend animations, morph animation is additive. The required offset of each morph is added to the base mesh before the bone nodes are applied, so they are always blended with each other and then the node animations. So no special blend mode is needed.)

Yes, the number of morphs (names actually) and the number of morph default settings could be merged, but then you would always have to have a default setting for each morph. Right now they are optional.

Yes - I need to remove the two dupe sizes - they are all three the same.

TSE- Being a TSE owner, I have already volunteered to make the same changes for TSE once we get everything complete for TGE.

I will post another version with these changes (plus fixing the DSQ 25 load which is missing a section)
#13
11/23/2005 (2:25 pm)
Did this make it into 1.4?
#14
11/23/2005 (6:31 pm)
Unfortunately, it ended up slipping off the list a bit. (One reason this get deprioritized was that there weren't any exporters that were going to support it, so we used our limited resources to focus on actual broken stuff rather than features that would probaby remain hidden for a while.)

I'd still definitely like to work with David to enhance the DTS capabilities of Torque, though!
#15
12/27/2005 (1:53 am)
I would definitely love to see a new morph feature put in Torque, I used morphs like a mad man during my days with Granny. Granny uses the point-to-point system which makes it easy to work with. If you guys get this working, then I'll never touch Granny again, that program is buggy and expensive. :O

Any chance of seeing a Max exporter version soon this new year? ;)
#16
12/27/2005 (3:54 am)
From the GG perspective:

I'd love to get this going under Max and all the other mainline exporters. Having a working exporter for one of those major applications would also make it a higher priority to put this into our core art pipeline. Right now it's hard to make the time to add a feature that we don't support anywhere in our art pipeline - it would actually make a lot of work for us to deploy morph onto all the exporters!

But if anyone in the community (Dave ;) wants to work on us, they have our full support. Just ping me via e-mail if you're working on this and I'll be happy to co-ordinate with you.
#17
02/13/2006 (2:27 am)
Are there any updates on the progress of this work? Morph targets would be an ideal solution for my needs of changing a character's physique over a period of time, rather than having a number of models that I need to alternate through.
#18
05/30/2006 (2:06 pm)
@Ben
I would really like to see this integrated into 1.4 as well (is it in a maya dts exporter yet?) I would also really like to see it make the jump to TSE. This really needs to be a standard option in Torque.

@David
Great Work David!
#19
05/30/2006 (3:57 pm)
Does anyone want to do max/maya integration? We have zero bandwidth to do this improvement in-house right now (we're moving offices atm!), so if it's going to get done the community needs to do it. We might consider bountying the work, at the least we're definitely open to gifting licenses to people who do it.
#20
05/30/2006 (9:28 pm)
Strta 5.0 with my wrk just got released, so I will have a little more time now.
I'd like to get this in 1.4 and my exporter.
Plus I'm a Maya developer and am willing to try to do the same for Maya.
I may finally have access to Max devel system now that they have merged. But I'm not experienced in Max plugins, but I wouldn't be writing from scratch, just adding the morphs.

Any interest Ben?

David
Page «Previous 1 2