question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Node.setMatrix() support

See original GitHub issue

How come this is not supported? There’s already a Node.getMatrix.

I’m seeing a bug that I think is related. Using gl-matrix, I have a mat4 and doing this (which I have to do without a setMatrix - bunch of useless calculation and precision loss):

const getTRS = (m: mat4) => {
    return { t: mat4.getTranslation(vec3.create(), m), r: mat4.getRotation(quat.create(), m), s: mat4.getScaling(vec3.create(), m) };
};

const setNodeMatrix = (node: GLTFNode, mat: mat4) => {
    let trs = getTRS(mat);
    node.setTranslation([trs.t[0], trs.t[1], trs.t[2]]);
    // quat.normalize(trs.r, trs.r); // i get asserts either way
    node.setRotation([trs.r[0], trs.r[1], trs.r[2], trs.r[3]]);
    node.setScale([trs.s[0], trs.s[1], trs.s[2]]);

    console.log(mat, node.getMatrix());
    assert(mat4.equals(mat, node.getMatrix()));
}

I get asserts in some cases. Example:

[-0.9307078123092651, 0.36324116587638855, -0.04288084805011749, 0, -0.3623482286930084,  -0.9316428303718567, -0.02730146236717701,  0, -0.044879984110593796, -0.00888469722121954,  0.8988363742828369, 0, 310.8424987792969, -5155.25244140625, -366.21356201171875, 1]
[-0.9307281322940169, 0.3632646333314609,  -0.04257750763746079, 0, -0.36232478384586403, -0.9316375673397288, -0.028303744841263856, 0, -0.04495307531851036,  -0.009824320406136731, 0.8988229395637517, 0, 310.8424987792969, -5155.25244140625, -366.21356201171875, 1]

I can also see that exported meshes are transformed incorrectly compared to my obj exporter that transforms each vertex at export time with the raw matrices, which may be related to this. I don’t know what else it could be. The rest of the code is pretty much the same, and my nodes are placed on the root.

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
VelocityRacommented, Jun 30, 2021

Ok, thanks anyway. Feel free to close this, or leave it open until/if you implement setMatrix.

1reaction
donmccurdycommented, Jun 1, 2021

I’ve had some trouble using gl-matrix to decompose matrices (https://github.com/toji/gl-matrix/issues/408) and so I don’t use those functions internally. There’s a (currently undocumented) MathUtils.decompose(...) function that follows the three.js implementation and this has worked much better for me with, e.g., conversions from other formats:

https://github.com/donmccurdy/glTF-Transform/blob/33a91aff3bfa281ee8fab60fd1d233b41bafd749/packages/core/src/utils/math-utils.ts#L60-L75

Does decomposition work correctly for your model with this approach?


As for whether a setMatrix(...) method should be added, I guess it comes down to what that method should do, if it were added? One option is to decompose the matrix within that method, but of course this will give the same result as using MathUtils.decompose by yourself. Another option is store the matrix on the Node, which currently only stores TRS properties. This would create some uncertainty about what happens when setTranslation is called after setMatrix, and what should be put in the exported file, though — TRS properties or a matrix? Other users may prefer that TRS properties be used.

Read more comments on GitHub >

github_iconTop Results From Across the Web

react.setMatrix JavaScript and Node.js code examples | Tabnine
Packs CommonJs/AMD modules for the browser. Allows to split your codebase into multiple bundles, which can be loaded on demand. Support loaders to...
Read more >
QSGTransformNode Class | Qt Quick 6.4.1
virtual, ~QSGTransformNode() override ... The transform nodes implement a 4x4 matrix which in theory supports full 3D ... See also setMatrix().
Read more >
Scripting API: Material.SetMatrix - Unity - Manual
Sets a named matrix for the shader. This is mostly used with custom shaders that need extra matrix parameters. Matrix parameters are not...
Read more >
Node - glTF-Transform
Documentation for Node in glTF-Transform, a glTF 2.0 SDK for JavaScript, TypeScript, ... Most engines only support 4-8 active morph targets at a...
Read more >
Matrix representation of graph node position? - API - LUME
Q) Are you considering representing the position of a node using a similar system as ... what would be convenient is a Node#setMatrix()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found