I created a cube mesh using THREE.BoxBufferGeometry
, and used matrix transformation to implement mesh rotation and scaling in the vertex shader. These matrices are all used as uniform variables. After I made a displacement change to the mesh, the mesh successfully implemented the displacement in the scene, but then I used mesh.position
to get the position of the mesh in javascript and found that its value has not changed. mesh.position
is not updated.
material = new THREE.ShaderMaterial( {
vertexShader: document.getElementById( 'volumeVertex' ).textContent,
fragmentShader: document.getElementById( 'volumeFragment' ).textContent,
side: THREE.DoubleSide,
uniforms: {
rotateMatrix:{value:rotateMatrix.elements},
scaleMatrix:{value:scaleMatrix.elements},
translateMatrix:{value:translateMatrix.elements}
}
});
boxGeometry = new THREE.BoxBufferGeometry(2, 2, 2);
boxGeometry.doubleSided = true;
mesh = new THREE.Mesh( boxGeometry, material );
scene.add( mesh );
vertex shader code:
<script id="volumeVertex" type="x-shader/x-vertex">
uniform mat4 rotateMatrix;
uniform mat4 translateMatrix;
uniform mat4 scaleMatrix;
void main()
{
vec4 newPos = translateMatrix * rotateMatrix *scaleMatrix * vec4(position,1.0);
gl_Position = projectionMatrix * modelViewMatrix * newPos;
}
Change the position of the mesh through a matrix:
var translateMatrix = new THREE.Matrix4();
translateMatrix.makeTranslation(5,5,5);
var preTransMatrix = new THREE.Matrix4();
preTransMatrix.elements = mesh.material.uniforms.translateMatrix.value;
translateMatrix.multiply(preTransMatrix);
mesh.material.uniforms.translateMatrix.value = translateMatrix.elements;
the mesh can move to (5,5,5)
,but when I console.log(mesh.position.x,mesh.position.y,mesh.position.z)
,the result is the initial position (0,0,0)
.How to update the mesh.postion
after matrix movement ?