0
我剛剛瞭解到THREE.js,並且對它的能力着迷。我沒有先進到足夠寫我自己的代碼,所以我一直在瀏覽試圖找到一個例子來做到我需要或接近,但到目前爲止沒有用::(three.js網格傾斜而不是旋轉
我正在使用的是一些。代碼,用鼠標移動的物體的X軸工作prefectly但Y軸周圍的物體旋轉,傾斜,而不是它的左右
我當前的代碼:
var container, stats;
var camera, scene, renderer;
var group, text, plane;
var targetRotationX = 0;
var targetRotationOnMouseDownX = 0;
var targetRotationY = 0;
var targetRotationOnMouseDownY = 0;
var mouseX = 0;
var mouseXOnMouseDown = 0;
var mouseY = 0;
var mouseYOnMouseDown = 0;
var windowHalfX = window.innerWidth/2;
var windowHalfY = window.innerHeight/2;
var finalRotationY
var center
init();
animate();
function init() {
camera = new THREE.PerspectiveCamera(100, window.innerWidth/window.innerHeight, 0.1, 10000);
camera.position.z = 1000;
camera.eulerOrder = "YXZ"
scene = new THREE.Scene();
// lights
light = new THREE.DirectionalLight(0xffffff);
light.position.set(1, 1, 1);
scene.add(light);
light = new THREE.DirectionalLight(0x002288);
light.position.set(-1, -1, -1);
scene.add(light);
light = new THREE.AmbientLight(0x222222);
scene.add(light);
\t \t var geometry = new THREE.BoxGeometry(1400, 100, 700, 10, 10, 10);
\t \t var material = new THREE.MeshBasicMaterial({color: 0xfffff, wireframe: true});
\t \t var cube = new THREE.Mesh(geometry, material);
\t \t //cube.position.set(center.x, center.y, center.z);
\t \t //cube.geometry.applyMatrix(new THREE.Matrix4().makeTranslation(-center.x, -center.y, -center.z));
\t \t renderer = new THREE.WebGLRenderer({ antialias: true, alpha: true });
\t \t renderer.setSize(window.innerWidth, window.innerHeight);
\t \t container = document.getElementById('container');
container.appendChild(renderer.domElement);
\t \t group = new THREE.Object3D();
\t \t group.add(cube)
\t \t scene.add(group);
// renderer
//stats = new Stats();
//stats.domElement.style.position = 'absolute';
//stats.domElement.style.top = '0px';
//container.appendChild(stats.domElement);
document.addEventListener('mousedown', onDocumentMouseDown, false);
document.addEventListener('touchstart', onDocumentTouchStart, false);
document.addEventListener('touchmove', onDocumentTouchMove, false);
//
window.addEventListener('resize', onWindowResize, false);
//for debuging stats
interval = setInterval(debugInfo, 50);
}
function modelLoadedCallback(geometry) {
mesh = new THREE.Mesh(geometry, material);
group.add(mesh);
scene.add(group);
}
function onWindowResize() {
windowHalfX = window.innerWidth/2;
windowHalfY = window.innerHeight/2;
camera.aspect = window.innerWidth/window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
//
function onDocumentMouseDown(event) {
event.preventDefault();
document.addEventListener('mousemove', onDocumentMouseMove, false);
document.addEventListener('mouseup', onDocumentMouseUp, false);
document.addEventListener('mouseout', onDocumentMouseOut, false);
mouseXOnMouseDown = event.clientX - windowHalfX;
targetRotationOnMouseDownX = targetRotationX;
mouseYOnMouseDown = event.clientY - windowHalfY;
targetRotationOnMouseDownY = targetRotationY;
}
function onDocumentMouseMove(event) {
mouseX = event.clientX - windowHalfX;
mouseY = event.clientY - windowHalfY;
targetRotationY = targetRotationOnMouseDownY + (mouseY - mouseYOnMouseDown) * 0.02;
targetRotationX = targetRotationOnMouseDownX + (mouseX - mouseXOnMouseDown) * 0.02;
}
function onDocumentMouseUp(event) {
document.removeEventListener('mousemove', onDocumentMouseMove, false);
document.removeEventListener('mouseup', onDocumentMouseUp, false);
document.removeEventListener('mouseout', onDocumentMouseOut, false);
}
function onDocumentMouseOut(event) {
document.removeEventListener('mousemove', onDocumentMouseMove, false);
document.removeEventListener('mouseup', onDocumentMouseUp, false);
document.removeEventListener('mouseout', onDocumentMouseOut, false);
}
function onDocumentTouchStart(event) {
if (event.touches.length == 1) {
event.preventDefault();
mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
targetRotationOnMouseDownX = targetRotationX;
mouseYOnMouseDown = event.touches[ 0 ].pageY - windowHalfY;
targetRotationOnMouseDownY = targetRotationY;
}
}
function onDocumentTouchMove(event) {
if (event.touches.length == 1) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
targetRotationX = targetRotationOnMouseDownX + (mouseX - mouseXOnMouseDown) * 0.05;
mouseY = event.touches[ 0 ].pageY - windowHalfY;
targetRotationY = targetRotationOnMouseDownY + (mouseY - mouseYOnMouseDown) * 0.05;
}
}
//
function animate() {
requestAnimationFrame(animate);
render();
//stats.update();
}
function render() {
//horizontal rotation
group.rotation.y += (targetRotationX - group.rotation.y) * 0.1;
finalRotationY = (targetRotationY - group.rotation.x);
if (group.rotation.x <= 1 && group.rotation.x >= -1) {
group.rotation.x += finalRotationY * 0.1;
}
if (group.rotation.x > 1) {
group.rotation.x = 1
}
else if (group.rotation.x < -1) {
group.rotation.x = -1
}
renderer.render(scene, camera);
}
function debugInfo()
{
//$('#debug').html("mouseX : " + mouseX + " mouseY : " + mouseY + "</br>")
}
<style>canvas { width: 100%; height: 100% }</style>
<script src="http://threejs.org/build/three.min.js"></script>
<div id="container"></div>
我希望做的事情是,當鼠標被點擊並向左或向右移動時,除非鼠標向上或向下移動,否則盒子將向着鼠標傾斜並保持相同的「X」旋轉。現在,相機似乎圍繞對象旋轉,而不是沿相應的方向傾斜。
任何想法?
歡迎來到SO!您需要嘗試此操作,張貼您嘗試的代碼以及您遇到的任何問題,以便我們可以幫助您解決所述問題。請參閱[如何詢問頁面](http://stackoverflow.com/help/how-to-ask)尋求幫助改善您的問題。 – Madness
我不是100%確定要問什麼,所以我只是留下評論而不是答案。當鼠標向左或向右移動時,我認爲你想要做的是圍繞Z軸而不是Y軸旋轉......? – micnil
請確保您瞭解[three.js中的旋轉工作方式](http://stackoverflow.com/questions/17517937/three-js-camera-tilt-up-or-down-and-keep-horizon-level/17518092 #17518092)。 – WestLangley