Morph 3D Tutorial Part 3: Adding MCSMaleLite to scene and adding / changing clothes at runtime 9 comments


This tutrial’s got outdated with M3D 1.6 release. Might still be useful but not entirely accurate.

In this part of the tutorial we’ll instantiate the MCS model at runtime and add some clothes on him. But before that let’s see how we can show/hide clothes on an existing Morph 3D model at runtime.

We’ll use the scene we created for Tutorial Part 2. Open the scene and select Canvas from hierarchy view. Add two buttons to UI by selecting Create -> UI -> Button. Name them DressedButton and NakedButton. Edit their text as Dressed and Naked. Anchor them to top left adn position them to 120, -65, 0 and 120, -100, 0 respectively.

Open the UIController script and modify it so it looks something like this.

What we basically do is setting and keeping a list of visible clothing and make them visible/invisible in the loop. We keep it in the clothingList field because GetVisibleClothing only gets the visible clothings. When we make them invisible we can’t use this method to get the list. (I couldn’t figure out a way to get the invisible or full clothing list, if anyone knows a way please leave a comment.) Since eyes are considered clothing by Morph 3D we need to check if the clothing is G2FSimplifiedEyes before making it invisible, unless you want to see a freaky naked man.

Go back to unity and add new methods MakeModelDressed and MakeModelNaked to buttons’ On Click events. (Hit + at the bottom, drag Canvas object to slot and select UIController -> MakeModelDressed() and same for the naked one)

If you hit play you’ll see that you can dress and undress the model at runtime. You’ll notice if you change the weight blenshape when naked and hit Dressed button bone positions look awkward. This is probably because the clothing blendshapes doesn’t update when invisible. You can add a few lines of code at the end of dressed code that sets one of the blendshapes to 0 and back to current value to workaround that issue (If anyone knows a method to trigger that please leave a comment).

This approach requires all of the clothings attached to the model beforehand. This could cause some performance issues and it also is not convenient by any means since you would need to attach every possible clothing to every model with a probability of changing clothes (player, companions etc.)

There is one other issue. If you are planning to have a character creation feature and leave the choice of sex to the player you won’t know which model to use at runtime either. You can always have both male and female model and do the hide/show thing like the clothing. But it gets even more complicated when coupled with all the clothings on those two models.

I think you’ll agree with me whan I say we need to be able to instantiate models and clothes at runtime.

Save your scene if you haven’t done so yet and open a new scene then save it too. Like in the Tutorial Part 2 add a plane, a canvas, MultipurposeCaneraRig and ThirdPersonController prefab to the scene. Delete MainCamera and the Ethan stuff under ThirdPersonController. Remember to tag ThirdPersonController as player and MultipurposeCameraRig as Main Camera. You should have a hierarchy view looking like this.

0014

We need an object in the scene to hold the character creation script. We could use the player game object but we’ll create an empty game object in the hierarchy view and name it GameController. Now create a C# script under Assets/Scripts (create the scripts folder) named “GameController” and drag it on to GameController game object in hierarchy view.

Now we are ready to start coding. The rest of the stuff we’ll add at runtime.

Start by adding imports.

Then add these variables.

The ones we marked as [SerializeField] needs to be assigned before we run the game. Go back to unity editor and click on the GameController in the hierarchy view. You should see the GameController (Script) section in Inspector. Drag ThirdPersonController from hierarchy view to Player Controller slot. Drag MCSMaleLite prefab from project view (MORPH3D->Content_>StarterPacks->Male->MCSMaleLite folder) to Moel Prefab slot. Drag UrbanMetro prefab from project view (MORPH3D->Content_>StarterPacks->Male->hair->BritHair folder) to Clothing Content Pack slot. Drag BritHair prefab from project view (MORPH3D->Content_>StarterPacks->Male->outfits->UrbanMetro folder) to Hair Content Pack slot.

GameController (Script) should look something like this:

0015

Add methods for model creation and hair and call them from Start method.

I will not go into detail much but one of the important things here is setting the player controller’s animator’s avatar as MCSMaleLite model’s avatar. If we skipped this our model wouldn’t animate. You could set this in the scene by changing from the Unity UI but consider that you are creating the character from a saved character creation data. And that you must instantiate a male or female model accordingly. You wouldn’t know beforehand which avatar to set as player controller’s.

If you hit play now you’ll see that the Male model will be created and hair added. (You can run around naked for a while, you deserved it. Hit play button again to stop when you’re done.)

To demonstrate adding and removing clothes we’ll add two buttons to the scene much like the ones at the beginning of this tutorial part.

For dressing up add this function to GameController script. Remember to bind the Dressed button to this function.

As you see adding clothing is very similar to adding hair. The last line makes manager aware of newly added clothing. If you ommit this line you can’t access currently loaded clothing by manager later (naked function). I haven’t tried but I suspect blendshapes might not propogate to clothing either.

I think you could add a game object for clothings to the scene. Add a script to it with an array to hold all the content pack prefab refernces. Drag the prefabs to the arrays slots and make the game object itself a prefab. This could be used as a clothing library. The same goes for hair.

The last part is the function to remove clothings. (remember to bind this function to the second button)

As you can see we loop through the clothing items and destroy them. I used destroy because I couldn’t find any manager method to remove clothing (leave a comment if you are aware of a better way please). Since eyes are considered clothing by Morph3D and we want to keep them we check if it’s the eyes before destroying.

If you run the game now you can dress/undress the model by clicking the buttons.

And for convenience here is the complete GameController script.

Links to the other parts of the tutorial:

[Total: 0    Average: 0/5]
  • Dan

    I am having issues also working with hair, and clothes to a lesser degree. I really appreciate your tutorial though. It at least got me started. I’ve been building a character creation scene that I want to implement in atavism in Unity. Here is a video of my results. https://www.youtube.com/watch?v=Wir_lPkJit8 . As you can see in the vid, there are a lot of issues using Morph3D at runtime. Any suggestions no a faster way of loading hair and clothes? And what about keeping hair on a head when adjusting height?

    I wish the API left simple singular slots for all clothes and hair. I’ve got to figure out eye color too at some point.

    • Hi Dan,

      The method I used in this tutorial (adding clothing items) doesn’t add the required alpha injection masks. That causes the skin poke throughs. If you add whole ContentPack with manager.AddContentPack (or manager.AddContentPackToModel, I don’t remember which one) it should work better.

      Morph3D will release a new version and API will probably change, so I am waiting that release to rewrite this tutorial. I hope this helps.

      • Dan

        Thanks Umut. That fixed the tearing. The hair not staying with the head on a resize was due to me not calling manager.DetectAttachedHair() after I placed the new hair. I guess the usual game gear slots are not cleanly possible right now with having to load entire content packs and not individual gear items. It would be good to find a way to replace hair on an avatar, without deleting the avatar and re-instantiating. I am doing that for gear also, since I can’t seem to get gear deleted and put new gear on the avatar.

        • Yeah at it’s current state API have problems with removing clothes too. That’s why I am waiting on new version before I start working on MCS. I hope they fix current version’s issues with the new release.

  • Mike Norton

    Awesome tutorial.

    I have a little issue which seems to be simply due to them updating the asset since you wrote this. But I am having issues where the clothing is not shaping correctly to the body. The skin is peeking through.

    Is there a new call that we should use?

    Also, for your next tutorial or if you know how. . I am really wishing to figure out how to change colors of skin and hair.

    Thanks

    • Hi Mike sorry I didn’t have time to check comments and answer earlier. Skin poking occurs when you loop through the clothing and add them seperately. If I remember correctly using methods that loads all items as package results with better (less) skin bleeding.

      On the other hand they have MCS 2.0 on the way with a new model (DAZ genesis 3) I assume API itself will probably change too. I am planing to document my experiment with that new version when it’s released as a new tutorial series. I’ll keep your request about colors in mind.

      For current version there should be grayscale materials for hair you can use those and change albedo color to get the color you want. Changing albedo color doesn’t require a white/grayscale material actually you can change the tint of material the same way. I haven’t tried but changing color of material by code should be possible too.

      Hope it helps.

  • Pingback: Morph 3D Tutorial | o3n Games()

  • Pingback: Morph 3D Tutorial Part 1: Setting up the scene | o3n Games()

  • Pingback: Morph 3D Tutorial Part 2: Changing blendshapes at runtime | o3n Games()