Morph two geometries in vertex shader

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

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 to get an idea.

@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

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 ).

I have an example of morph targets here: 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.

1 Like

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

And it compiles against 0.9.

–8