Jump to content

update matrix with a clickevent inside shader


Recommended Posts

Hello i'm exploring the matrix func. inside Babylonjs, a matrix lib can be easy
see here : http://glmatrix.net/docs/module-mat4.html
or very hard see here : https://doc.babylonjs.com/api/classes/babylon.matrix

I kind of understand where the problem occurs, after unsucessfull 3h, i think i might be better to ask the community.


Link to comment
Share on other sites

Can you tell us more about what you want to do? Seems unclear to me :)

Just assuming the problem is in the matrix, let's see what we have here:

let nR = BABYLON.Matrix.Identity();
nR=	BABYLON.Matrix.RotationY(newPosY);
rM=	BABYLON.Matrix.RotationX(newPosX);

This cannot work :) all xxxToRef functions are used to avoid creating new objects and instead reuse existing one. So for instance the matrix.multiply function will return a NEW matrix where the multiplyToRef will use the last parameter to store the result and thus avoid creating a new matrix

So the correct code should be in your case:

let final = BABYLON.Matrix.Identity();
nR =	BABYLON.Matrix.RotationY(newPosY);
rM =	BABYLON.Matrix.RotationX(newPosX);

// Multiply rM by nR and store the result in final

If you don't want xxtoRef functions, you can use this code (probably easier to read but less efficient as it will put pressure on the GC):

nR =	BABYLON.Matrix.RotationY(newPosY);
rM =	BABYLON.Matrix.RotationX(newPosX);

// Multiply rM by nR and store the result in final
let final = rM.multiply(nR)


Link to comment
Share on other sites

thank you

i try simple to multiply to matrieces ( how hard can it be ) https://en.wikipedia.org/wiki/Rotation_matrix#Basic_rotations


http://glmatrix.net/docs/mat4.js.html#line584 multiply by http://glmatrix.net/docs/mat4.js.html#line541

let rMat = mat4.create(); 


const nRot = mat4.create();
mat4.rotateY(nRot, nRot, (deltaX *0.5)*0.01 );
mat4.rotateX(rMat, rMat, (deltaY *0.5)*0.01 );
mat4.multiply(rMat, nRot,rMat);

canvas.addEventListener end


gl.uniformMatrix4fv(uLocRot, false, rMat);

requestAnimationFrame end
 // and it's working


Yes, but the Matrix resets its self, see here: 


@DeltakoshRaw WebGL

Link to comment
Share on other sites

i guess becourse of 
scene.onBeforeRenderObservable.add or any other renderloop inside the createScene the Matrix isent stored properly. 
i leave this here, for later for debuging reasons
# just an other debug playground
# minimal copy paste from the docs
Dont worry i will investigate :)
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.

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.


  • Recently Browsing   0 members

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