How to morph a model when loaded by obj

I am using a OBJ loader and load a OBJ. I want to morph it by height. To morph it by height, i need to update only Y position of all coordinates. I don’t know how to make mesh “update = true”. This is what i tried. OBJ file loaded fine. How do i morph it by height?. I have the morphed OBJ. But i don’t know how to use it.

var createScene = function () {
// create a basic BJS Scene object
var scene = new BABYLON.Scene(engine);
// light
var light = new BABYLON.HemisphericLight(“Hemi0”, new BABYLON.Vector3(0, 1, 0), scene);
light.specular = new BABYLON.Color3(0, 0, 0);
// cam
var cam = new BABYLON.ArcRotateCamera(“ArcRotateCamera”, Math.PI/2, 1.2, 80, new BABYLON.Vector3(0, 35, 0), scene);
cam.attachControl(canvas);
// loader
var loader = new BABYLON.AssetsManager(scene);
var model = loader.addMeshTask(“model”, “”, “assets/”, “BaseMesh-html.obj”);
loader.onFinish = function(tasks) {
console.log(scene.meshes.length);

            sphere = scene.meshes[0];
        };
        loader.load();
        return scene;
    };
	
	var engine = new BABYLON.Engine(canvas, true, { preserveDrawingBuffer: true, stencil: true });
    var scene = createScene();

scene.debugLayer.show();
// ground.isPickable = false; //stop moving for ground
engine.runRenderLoop(function () {
scene.render();
});
window.addEventListener(“resize”, function () {
engine.resize();
});

Refrence Links: Load Files with Assets Manager - Babylon.js Documentation
Use Morph targets - Babylon.js Documentation

What do you mean by morph?
What about scaling the model on the Y-axis?
Or maybe create a simple bone-rig, which you can use to scale leg/spine bones, like this example:
https://playground.babylonjs.com/#94EHM5#2

If you actually want to make the vertices updateable on the imported model, you’ll have to do:

var positions = sphere.getVerticesData(BABYLON.VertexBuffer.PositionKind);
sphere.setVerticesData(BABYLON.VertexBuffer.PositionKind, positions, true);

If you can put this on a playground, it will be easier to help.

What I am doing is to change Y-access position for increasing/decreasing height.

var coordinates = coordinates_list;
function changeShape(slider, morphTarget )
{
if(morphTarget == “height”) {
for(var i = 0 ; i < coordinates_list.length; i++) {
if((i+2)%3 == 0) {

      if(lastValHeight < slider.value) {
        coordinates[i] += (heightCoordinate[i]-coordinates_list[i])/5; 
      } 
      else {
        coordinates[i] -= (heightCoordinate[i]-coordinates_list[i])/5;
      }
    }
  }
  lastValHeight = slider.value;
}
sphere.setVerticesData(BABYLON.VertexBuffer.PositionKind, coordinates);

}

Calculation is done and updated coordinates i got but issue is this line “sphere.setVerticesData(BABYLON.VertexBuffer.PositionKind, coordinates);” not update the view.

heightCoordinate[] are the are max height coordinates.

Raggar, Can you please tell me how to update the X, Y, Z values in positions variable. I think if it is possible, it will be the best solution for me.
Thanks for your valuable reply.

@bghgary, I am new here, I tried but not able to create playground, I will try again, and will update.

What i am trying to figure out is, something like this.
mesh.scaling = new BABYLON.Vector3(scale_x, scale_y, scale_z);
OR
mesh.scaling.y = 5;
Refrence: Position and Rotation - Babylon.js Documentation

Finally it’s working. The issue was i was not able to set vertices updatable. @Raggar, you help me to do it. second issue was I get sphere = scene.meashes[0] But real mesh was in scene.meashes[2].
I did it for inseam. We can up or down inseam. For height I will do latter.

How it look now

3 Likes