我现在开始学习Three.js,并且首先尝试做一些非常基本的事情,比如创建一个框然后在其上应用变换矩阵,在Z轴上旋转15deg:
<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>
问题是盒子根本没有变化,它仍然保持在相同的位置/方向。
如果我检查applied_matrix变量,它会返回一个单位矩阵,就像我从未应用过我的矩阵一样。
我错过了什么?
答案 0 :(得分:2)
你在控制台得到一个警告说:THREE.Matrix4:构造函数不再读取参数。改为使用.set()。
所以请代替:
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);
尝试
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);