Jump to content

Scale child meshes with maintained aspect ratio


Hersir
 Share

Recommended Posts

Hi,

trying to achieve mesh scaling from parent but with maintained aspect ratio, so if parent is scaled, child mesh will scale together but will maintain aspect ratio.

would it be possible to do that by overriding `computeWorldMatrix` or there is better way to override default scaling behaviour ?

I tried to override `_afterComputeWorldMatrix` to update world and local matrix, but no luck , probably doing something totally wrong :) 

 protected _afterComputeWorldMatrix() {
        super._afterComputeWorldMatrix();
        const min = Math.min(this._worldMatrix.m[0], this._worldMatrix.m[10]);
        this._worldMatrix.m[0] = min;
        this._worldMatrix.m[10] = min;
        this._localWorld.m[0] = min;
        this._localWorld.m[10] = min;
    }

thanks for any hint

Link to comment
Share on other sites

Hey @Deltakosh, by that I mean if there is square plane with scaling (1,1,1) and it has child  square box scale (1,1,1), if plane is scaled box should still be square so it should react to changes on both axes (x,z) if just 1 axis is scaled it should not change.

Have small PG with example, if commented line is added, child box should still be square (scaled by smallest world scale ratio from x,z ?)

Link to comment
Share on other sites

I would extend TransformNode as a container and add the plane and cube to it. This container should never actually scale itself but assign scales and positions to its child meshes..

So if you add a custom scale property or overwrite its scaling property and calculate the new positions and scales of the submeshes you would not have to mess with the worldMatrix per mesh.

Would that work for you?

Link to comment
Share on other sites

In this PG I made some changes. I changed the plane to a ground, simply because rotating the plane swapped reference to y and z when scaling plane and hence box and this confused my simple brain.

So a scaling of the ground by (2 , 1,  3) must scale the box also by (2, 1,  3) giving a box of course of width 2, height 1 and depth 3. So if you want a face (say the font and back faces) to be square then the height has to be 2 also, so the above PG scales the child's y by 2, since the scaling is carried out relative to the ground and the child is at 0.5 relative to the ground the child needs re-aligning to the ground.

A simpler way, given that you know you want a square face is to adjust y from the start as in this second PG

Link to comment
Share on other sites

Ok made working using @JohnK proposide solution, just added listener on parent for `onAfterWorldMatrixUpdateObservable`, cached scaling and looped through children and applied scale fix. Just 1 thing is that children can be rotated, any good ways to apply proper scaling on rotated meshes, as axes are fliped, on way what I thought was to wrap them in transform node that is not rotated and scale it but maybe there are other means to calculate proper scaling for rotated meshes ?

 

Big thank for help

Link to comment
Share on other sites

4 hours ago, Hersir said:

Just 1 thing is that children can be rotated, any good ways to apply proper scaling on rotated meshes

Look at bone scale code.  

Edit: Eh,  I thought about it some more and the lack of local matrix might be a problem. 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...