rbasniak
rbasniak

Reputation: 4984

Transformation matrix not working with Three.js

I'm starting to learn Three.js now, and trying to do some really basic things at first, like creating a box and then applying a transformation matrix on it, rotating 15deg on Z axis:

<html>
<head>
<title>My first Three.js app</title>
<style>
    body {
        margin: 0;
    }

    canvas {
        width: 100%;
        height: 100%;
    }
</style>
</head>
<body>
<script src="scripts/three.js"></script>
<script>
    var scene = new THREE.Scene();
    var camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 100000);

    var renderer = new THREE.WebGLRenderer();
    renderer.setSize(window.innerWidth, window.innerHeight);
    document.body.appendChild(renderer.domElement);

    var geometry = new THREE.BoxGeometry(1, 0.5, 0.1);
    var material = new THREE.MeshBasicMaterial({ color: 0x00ff00 });
    material.wireframe = true;
    var cube = new THREE.Mesh(geometry, material);

    cube.matrixAutoUpdate = false;

    var matrix = new THREE.Matrix4(0.9659,  0.2588, 0.000,  0, 
                                  -0.2588,  0.9659, 0.000,  0,
                                   0.0000,  0.0000, 1.000,  0,
                                   0.0000,  0.0000, 0.000,  1);

    cube.applyMatrix(matrix);

    scene.add(cube);

    camera.position.z = 5;
    //camera.position.x = 5;
    //camera.position.y = 5;

    var n1 = cube.matrix.elements;

    camera.lookAt(new THREE.Vector3(0, 0, 0));

    var render = function () {
        requestAnimationFrame(render);

        //cube.rotation.x += 0.01;
        //cube.rotation.y += 0.01;

        renderer.render(scene, camera);
    };

    render();
</script>
</body>
</html>

The problem is that the box is not changing at all, it remains on the same position/orientation.

If I inspect the applied_matrix variable, it returns an identity matrix, like if I never applied my matrix.

What am I missing?

Upvotes: 2

Views: 2043

Answers (1)

meirm
meirm

Reputation: 1379

You got a warning in the console said: THREE.Matrix4: the constructor no longer reads arguments. use .set() instead.

So please instead of:

    var matrix = new THREE.Matrix4(0.9659,  0.2588, 0.000,  0, 
                              -0.2588,  0.9659, 0.000,  0,
                               0.0000,  0.0000, 1.000,  0,
                               0.0000,  0.0000, 0.000,  1);

Try

    var matrix = new THREE.Matrix4().set((0.9659,  0.2588, 0.000,  0, 
                              -0.2588,  0.9659, 0.000,  0,
                               0.0000,  0.0000, 1.000,  0,
                               0.0000,  0.0000, 0.000,  1);

Upvotes: 3

Related Questions