# Morph two geometries in vertex shader

**URL:** <https://discourse.libcinder.org/t/morph-two-geometries-in-vertex-shader/257>\
**Category:** Using Cinder\
**Created:** [September 9, 2016, 9:31pm UTC](https://discourse.libcinder.org/t/morph-two-geometries-in-vertex-shader/257 "2016-09-09T21:31:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![eight\_io](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/eight_io/32/57_2.png) [@eight\_io](https://discourse.libcinder.org/u/eight_io)\
**Post date:** [September 9, 2016, 9:31pm UTC](https://discourse.libcinder.org/t/morph-two-geometries-in-vertex-shader/257/1 "2016-09-09T21:31:45Z")

</div>

Hi,

I would like to morph between two geometries in vertex shader. I imagine, I somehow would bind two geometries, between which I would like to have a morph, to the same vertex shader, and then calculate a weighted average between each vertex position. Something like this

in vec4 ciPosition1;  
in vec4 ciPosition2;  
uniform float weight;

…

vVertexOut = mix(ciPosition1, ciPosition2, weight)

But how would I create a batch with two geometries?

Thanks.

–8

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [September 9, 2016, 11:25pm UTC](https://discourse.libcinder.org/t/morph-two-geometries-in-vertex-shader/257/2 "2016-09-09T23:25:28Z")

</div>

Just add another custom attribute, which is a `vec4`. Very similar to how you’d add another color or texture coordinate. See [this piece of code](https://github.com/cinder/Cinder/blob/master/samples/_opengl/PickingFBO/src/PickingFBOApp.cpp#L254-L266) to get an idea.

---

<div class="post-metadata">

**Author:** ![eight\_io](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/eight_io/32/57_2.png) [@eight\_io](https://discourse.libcinder.org/u/eight_io)\
**Post date:** [September 12, 2016, 5:26pm UTC](https://discourse.libcinder.org/t/morph-two-geometries-in-vertex-shader/257/3 "2016-09-12T17:26:48Z")

</div>

@paul.houx Thanks Paul, I am going to try your suggestion. Also, once you mentioned the attributes it occurred to me that I could follow GlslProg.cpp etc to set ciPosition1.

–8

---

<div class="post-metadata">

**Author:** ![paul.houx](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/paul.houx/32/31_2.png) [@paul.houx](https://discourse.libcinder.org/u/paul.houx)\
**Post date:** [September 12, 2016, 6:19pm UTC](https://discourse.libcinder.org/t/morph-two-geometries-in-vertex-shader/257/4 "2016-09-12T18:19:35Z")

</div>

Cinder will automatically set the attribute for you after you have given it a name (e.g. “morphPosition”), see line 264 of the sample I linked to. You can then access it in your vertex shader. To morph, make sure both geometries have exactly the same number of vertices - this will probably require a bit of pre-processing. Then simply add a `uniform float uMorph` to the shader and use `vec4 position = mix( ciPosition, morphPosition, uMorph )`.

---

<div class="post-metadata">

**Author:** ![navadria](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/navadria/32/90_2.png) [@navadria](https://discourse.libcinder.org/u/navadria)\
**Post date:** [September 13, 2016, 11:50pm UTC](https://discourse.libcinder.org/t/morph-two-geometries-in-vertex-shader/257/5 "2016-09-13T23:50:09Z")

</div>

I have an example of morph targets here: [https://github.com/araid/Cinder-Utils/tree/master/MorphTargets](https://github.com/araid/Cinder-Utils/tree/master/MorphTargets)  
Not sure if it’s up to date with the latest version of Cinder, but it should work with minor changes if it doesn’t.

---

<div class="post-metadata">

**Author:** ![eight\_io](https://yyz2.discourse-cdn.com/flex030/user_avatar/discourse.libcinder.org/eight_io/32/57_2.png) [@eight\_io](https://discourse.libcinder.org/u/eight_io)\
**Post date:** [September 14, 2016, 3:45pm UTC](https://discourse.libcinder.org/t/morph-two-geometries-in-vertex-shader/257/6 "2016-09-14T15:45:14Z")

</div>

@navadria OMG: this is exactly what I was building. Thanks a lot for sharing.

And it compiles against 0.9.

–8
