# Initial Morph Target Weights

**URL:** <https://forum.babylonjs.com/t/initial-morph-target-weights/2565>\
**Category:** Questions\
**Created:** [April 14, 2019, 2:51am UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565 "2019-04-14T02:51:35Z")\
**Posts on this page:** 17\
**Page:** 1

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [April 14, 2019, 2:51am UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/1 "2019-04-14T02:51:35Z")

</div>

Yo @Deltakosh and @bghgary … How do i assign the initial morph target weights… In GLTF there is a and array for gltfMesh.weights… So if the guy is supposed to be smiling (WITHOUT ANIMATION) you can set blendShape - SmileLeft = 1 and SmileRight = 1…

When using Babylon Entities… I dont see anything to set the initial weights…

@bghgary … We had this problem in GLTF… You fixed it … How… So we can try implement in Babylon Entities as well ???

FYI… Here is how i export blend shaped now… Just dont see a **WEIGHTS** property

```auto
        private void ConvertUnityBlendShapesToBabylon(UnityEngine.Mesh mesh, SkinnedMeshRenderer skin, BabylonScene babylonScene, BabylonMesh babylonMesh)
        {
            // Blend Shapes / Morph Targets
			if (mesh.blendShapeCount > 0)
			{
                // Morph Target Manager
                var babylonMorphTargetManager = new BabylonMorphTargetManager();
                babylonMesh.morphTargetManagerId = babylonMorphTargetManager.id;

                // Convert Morph Targets
                var babylonMorphTargets = new List<BabylonMorphTarget>();
				for (int blendShapeIndex = 0; blendShapeIndex < mesh.blendShapeCount; blendShapeIndex++)
				{
					// As described above, a blend shape can have multiple frames. Given that glTF only supports a single frame
					// per blend shape, we'll always use the final frame (the one that would be for when 100% weight is applied).
					string morphName = mesh.GetBlendShapeName(blendShapeIndex);
                    float morphWeight = skin.GetBlendShapeWeight(blendShapeIndex) / 100;
					int morphFrame = mesh.GetBlendShapeFrameCount(blendShapeIndex) - 1;

					var deltaVertices = new Vector3[mesh.vertexCount];
					var deltaNormals = new Vector3[mesh.vertexCount];
					var deltaTangents = new Vector3[mesh.vertexCount];
					mesh.GetBlendShapeFrameVertices(blendShapeIndex, morphFrame, deltaVertices, deltaNormals, deltaTangents);
                    
                    // Blendshape
                    var babylonMorphTarget = new BabylonMorphTarget();
                    babylonMorphTarget.name = morphName;
                    // Influence
                    babylonMorphTarget.influence = 0f;
                    // Positions
                    babylonMorphTarget.positions = deltaVertices.ToFloat();
                    // Normals
                    babylonMorphTarget.normals = deltaNormals.ToFloat();
                    // Tangents
                    babylonMorphTarget.tangents = deltaTangents.ToFloat();
                    
                    // Animations
                    /* TODO: Morph Animations
                    var animations = new List<BabylonAnimation>();
                    ExportFloatGameController(morphWeight, "influence", animations);
                    if (animations.Count > 0)
                    {
                        babylonMorphTarget.animations = animations.ToArray();
                    }
                    */

                    babylonMorphTargets.Add(babylonMorphTarget);
				}
                babylonMorphTargetManager.targets = babylonMorphTargets.Count > 0 ? babylonMorphTargets.ToArray() : null;
                babylonScene.MorphTargetManagersList.Add(babylonMorphTargetManager);
			}
        }

```

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [April 14, 2019, 2:06pm UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/2 "2019-04-14T14:06:22Z")

</div>

The initial weights in babylon.js are supposed to be 0 (like no weight at all)

I’m ok if you want to add more info into the bjs loader

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [April 14, 2019, 5:30pm UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/3 "2019-04-14T17:30:06Z")

</div>

Yo @bghgary … Is there any conversions that is going on with the morph target positions.

In unity i serialize a Vector3[] containing morph target positions. I did a trace output of what those values are in Unity. The VERY FIRST position:

GLTF BlendShape: Blink\_Left - Posiition:  
-5.960464E-08  
2.384186E-07  
1.043081E-07

But that very first position in babylon is showing as something TOTALLY different:  
-0.1176459789276123  
1.5863003730773926  
-0.051375649869441986

I have NO CLUE What is going on there.

Here is Screen shot from Unity Clearly showing Blink Left Position

 ![25%20AM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/f/f0dbf42dc6614f897f0666ae3254a26fbd3d20c6.jpeg)

But in babylon if i inspect the morph target positions:

 ![57%20AM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/1/11643221c96e245a47f0dc7543809efa9fd56939.png)

Why are these number different… Is there some conversion or something that is going on GLTF for morph target positions or what ???

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [April 14, 2019, 5:51pm UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/4 "2019-04-14T17:51:49Z")

</div>

Yo @Deltakosh … What is the class **VertexData** used for… Am i supposed to be using that class for something in the entities export?

```auto
/// <summary>
   /// Store the data of the vertex used to extract the geometry.
   /// It used by the morph target in order to have the same vertex order between the mesh and the target.
   /// </summary>
   public class VertexData
   {
       public int polygonId { get; set; }
       public int vertexIndexGlobal { get; set; }
       public int vertexIndexLocal { get; set; }

       public VertexData(int _polygonId, int _vertexIndexGlobal, int _vertexIndexLocal)
       {
           polygonId = _polygonId;
           vertexIndexGlobal = _vertexIndexGlobal;
           vertexIndexLocal = _vertexIndexLocal;
       }
   }

```

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [April 15, 2019, 4:38pm UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/5 "2019-04-15T16:38:51Z")

</div>

They are used to store mapping info between morph targets. I used them while exporting morph info

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [April 15, 2019, 7:11pm UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/6 "2019-04-15T19:11:05Z")

</div>

Should I be using this class to export more targets from unity ???

If so, Have you got any examples using this class ???

---

<div class="post-metadata">

**Author:** ![Deltakosh](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/deltakosh/32/26635_2.png) [@Deltakosh](https://forum.babylonjs.com/u/Deltakosh)\
**Post date:** [April 15, 2019, 7:50pm UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/7 "2019-04-15T19:50:17Z")

</div>

Well this one is used by our 3dsmax exporter so this is probably the best place to look for an example

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [April 16, 2019, 7:33am UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/8 "2019-04-16T07:33:15Z")

</div>

Yo @bghgary … Hey man do have any clue what going on here:

> [@Initial Morph Target Weights](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/3):
>
> Yo @bghgary … Is there any conversions that is going on with the morph target positions. In unity i serialize a Vector3[] containing morph target positions. I did a trace output of what those values are in Unity. The VERY FIRST position: GLTF BlendShape: Blink\_Left - Posiition: -5.960464E-08 2.384186E-07 1.043081E-07 But that very first position in babylon is showing as something TOTALLY different: -0.1176459789276123 1.5863003730773926 -0.051375649869441986 I have NO CLUE What is goin…

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [April 16, 2019, 8:10pm UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/9 "2019-04-16T20:10:29Z")

</div>

Unity stores blend shapes (morph targets) using a delta of the original. glTF does the same. Babylon, however, stores the absolute values of the shapes/targets. Is that perhaps what you are seeing?

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [April 16, 2019, 8:55pm UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/10 "2019-04-16T20:55:36Z")

</div>

Yo @bghgary

I think that it… How can i apply this delta ???

because i am trying to add morph targets to rgular babylon entities… so i am serializing these raw deltas WHICH IS WRONG… I should be calculating something with this delta right ???

Can you please show me that calculation ???

Where do you apply this delta to the original in GLTF Loader ???

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [April 16, 2019, 9:00pm UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/11 "2019-04-16T21:00:05Z")

</div>

It’s a delta (or displacement), just add the values to the original mesh values.

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [April 16, 2019, 9:34pm UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/12 "2019-04-16T21:34:59Z")

</div>

That **seems** to work… But the values are still different in babylon than when i serialize with GLTF… Why would that be ???

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [April 16, 2019, 9:50pm UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/13 "2019-04-16T21:50:15Z")

</div>

I’m not sure. What differences are you seeing?

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [April 16, 2019, 10:15pm UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/14 "2019-04-16T22:15:43Z")

</div>

Visually Nothing Different. But when i was tracing the value in babylon morph target manager targets… the first blend shape vertices.x value for Blink Left in GLTF = -0.1176459789276123

but with adding the delta to the mesh.vertices … the x = -0.2 something…

But they both look perfect… i have blend shape Smile\_Left and Smile\_Right set to 100

and this is BABYLON SCENE FILE (updating the delta positions in C# code):

```auto
        public static void UpdateBlendShapeDeltas(UnityEngine.Mesh mesh, Vector3[] deltaVertices, Vector3[] deltaNormals, Vector3[] deltaTangents)
        {
            int index = 0;
            // Verticies
            if (mesh.vertices != null && mesh.vertices.Length == deltaVertices.Length)
            {
                foreach (Vector3 vv in mesh.vertices)
                {
                    deltaVertices[index] += vv;
                    index++;
                }
            }
            // Normals
            index = 0;
            if (mesh.normals != null && mesh.normals.Length == deltaNormals.Length)
            {
                foreach (Vector3 nn in mesh.normals)
                {
                    deltaNormals[index] += nn;
                    index++;
                }
            }
            // Tangents
            index = 0;
            if (mesh.tangents != null && mesh.tangents.Length == deltaTangents.Length)
            {
                foreach (Vector4 tt in mesh.tangents)
                {
                    deltaTangents[index] += new Vector3(tt.x, tt.y, tt.z);
                    index++;
                }
            }
        }

```

Look slike this:

 ![49%20PM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/5/5942e52d4bde72a60ff2ae208fb2ce34eb7180ad.jpeg)

and the GLTF Version:

 ![43%20PM](https://us1.discourse-cdn.com/flex024/uploads/babylonjs/original/2X/1/105183a7de0f4c6ff173deefd074f437414e3e39.jpeg)

Look the exact same though… Which is good… Just seem weird that they would have different values

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [April 16, 2019, 10:18pm UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/15 "2019-04-16T22:18:30Z")

</div>

Anyways @bghgary It looks pretty good though … Thanks Again… You are always a **BIG HELP** 🙂

---

<div class="post-metadata">

**Author:** ![bghgary](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/bghgary/32/48532_2.png) [@bghgary](https://forum.babylonjs.com/u/bghgary)\
**Post date:** [April 16, 2019, 11:23pm UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/16 "2019-04-16T23:23:17Z")

</div>

Hmm, can’t explain why there would be a difference. The code for adding the delta for the glTF loader is here: [Babylon.js/glTFLoader.ts at master · BabylonJS/Babylon.js · GitHub](https://github.com/BabylonJS/Babylon.js/blob/master/loaders/src/glTF/2.0/glTFLoader.ts#L898)

---

<div class="post-metadata">

**Author:** ![MackeyK24](https://sea2.discourse-cdn.com/flex024/user_avatar/forum.babylonjs.com/mackeyk24/32/3741_2.png) [@MackeyK24](https://forum.babylonjs.com/u/MackeyK24)\
**Post date:** [April 17, 2019, 12:31am UTC](https://forum.babylonjs.com/t/initial-morph-target-weights/2565/17 "2019-04-17T00:31:51Z")

</div>

Me neither… I will double check GLTF Blend Shape Positions vs BABYLON Scene File Blend Shape Positions… But what ever the case, for some reason… its working in both like the screen shots above 🙂
