Scaling a geometry in one direction only

Was looking for quite a while for the right way to apply scaling to a geometry. On the stackoverflow was written, that instead of changing geometry of the mesh directly, it’s better to apply a matrix. What I’m doing (as i understood it correctly), i should translate Mesh to the origin first, scale in a certain amount, then move her back. I’ve take next approach

let transform_m = new Matrix4(this.mesh.matrixWorld);
let height = this.mesh.geometry.parameters.height;

//SCALES IN -y direction
transform_m.makeTranslation(0, -height / 2, 0);
this.mesh.applyMatrix4(transform_m);

transform_m.makeScale(1, 4, 1);
this.mesh.applyMatrix4(transform_m);

transform_m.makeTranslation(0, height / 2, 0);
this.mesh.applyMatrix4(transform_m);

I don’t know whether it’s efficient to apply matrix on every call. Also i don’t understand, why i can’t
chain calls one after another, and applyMatrix at the end once :

transform_m.makeTranslation(0, -height / 2, 0);
transform_m.makeScale(1, 4, 1);
transform_m.makeTranslation(0, height / 2, 0);

this.mesh.applyMatrix4(transform_m);

My knowledge is chunked, but isn’t it correct, that if I’m having a vector, i can apply one matrix after another to it, and get the same result, as if I’ve composed matrices, and then multiplied the vector by the resulting matrix?

That depends on how you use the geometry, you might permanently scale it, if you don’t need the original.

If you do, and you want to transform the mesh on every frame you can use compose method to set rotation, translation and scale for the matrix:
https://threejs.org/docs/?q=matrix4#api/en/math/Matrix4.compose

You can also decompose existing matrix into rotation, translation and scale and then just change the scale and put it back together.

One matrix does rotation, translation and scale at the same time.

I believe your code might not work because makeTranslation creates a matrix with only translation in it and will erase existing rotation and scale that transform_m has after you copied matrixWorld into it.

1 Like

Okay, it worked out. Appreciate a lot. I thought before, that makeTranslation, makeScale, are operating on the same matrix. You cleared things out.