API V3 - Help to Clone node

Hello,

For a new project, I need to clone several times the same object. Instead of adding several sessions in my viewer, I prefer to clone it for better repsonse time.

I found this codepen : cloning and transforming - session update - CodeSandbox

In the codepen, you use mat4 and vec3 from gl-matrix
How to reproduce this in JS code ?

Do I have to load a specific script to use GL Matrix ?

I tried to load this script :
https://unpkg.com/gl-matrix@3.4.3/cjs/index.js

but then mat4 is not working.
How to declare mat4 and vec3 variables?

Many Thanks
Olivia

mat4 is simply an array of 16 numbers (column-major matrix representation), vec3 is an array of 3 numbers.

If you do not want to use the gl-matrix package, you can also use Matrix4 of three.js.
The Matrix4.toArray function gives a column-major matrix representation like mat4.

Thanks !
I’ll try and let you know

Olivia

1 Like

Here is hte solution to use the GL Matrix script if someone want to

  1. Register the script
    https://cdnjs.cloudflare.com/ajax/libs/gl-matrix/3.4.2/gl-matrix-min.js

  2. On top of your JS, declare the variables

const { mat4, quat, vec3 } = glMatrix
  1. Use them in your code
clonedNode.transformations.push({
    id: "transformation",
    matrix: mat4.fromRotationTranslation(
      mat4.create(),
      quat.fromValues(0, 0, 0.7071068, 0.7071068),
      vec3.fromValues(0, 50, 0)
    )
  })
2 Likes