【发布时间】:2017-02-15 18:47:37
【问题描述】:
我正在为我们的 THREE.js 应用开发正交相机。本质上,这款相机将以 2D 的形式向用户呈现场景(用户可以选择在 2D 和 3D 相机之间切换)。该相机将允许平移和缩放到鼠标点。我有平移工作,我有缩放工作,但没有缩放到鼠标点。这是我的代码:
import React from 'react';
import T from 'three';
let panDamper = 0.15;
let OrthoCamera = React.createClass({
getInitialState: function () {
return {
distance: 150,
position: { x: 8 * 12, y: 2 * 12, z: 20 * 12 },
};
},
getThreeCameraObject: function () {
return this.camera;
},
applyPan: function (x, y) { // Apply pan by changing the position of the camera
let newPosition = {
x: this.state.position.x + x * -1 * panDamper,
y: this.state.position.y + y * panDamper,
z: this.state.position.z
};
this.setState({position: newPosition});
},
applyDirectedZoom: function(x, y, z) {
let zoomChange = 10;
if(z < 0) zoomChange *= -1;
let newDistance = this.state.distance + zoomChange;
let mouse3D = {
x: ( x / window.innerWidth ) * 2 - 1,
y: -( y / window.innerHeight ) * 2 + 1
};
let newPositionVector = new T.Vector3(mouse3D.x, mouse3D.y, 0.5);
newPositionVector.unproject(this.camera);
newPositionVector.sub(this.camera.position);
let newPosition = {
x: newPositionVector.x,
y: newPositionVector.y,
z: this.state.position.z
};
this.setState({
distance: newDistance,
position: newPosition
});
},
render: function () {
let position = new T.Vector3(this.state.position.x, this.state.position.y, this.state.position.z);
let left = (this.state.distance / -2) * this.props.aspect + this.state.position.x;
let right = (this.state.distance / 2) * this.props.aspect + this.state.position.x;
let top = (this.state.distance / 2) + this.state.position.y;
let bottom = (this.state.distance / -2) + this.state.position.y;
// Using react-three-renderer
// https://github.com/toxicFork/react-three-renderer
return <orthographicCamera
{...(_.pick(this.props, ['near', 'far', 'name']))}
position={position}
left={left}
right={right}
top={top}
bottom={bottom}
ref={(camera) => this.camera = camera}/>
}
});
module.exports = OrthoCamera;
发生了一些向鼠标点缩放的情况,但似乎不稳定。我想保持 2D 视图,所以当我缩放时,我也会移动相机(而不是有一个非垂直目标,这会破坏 2D 效果)。
我从this question 那里得到了提示。据我所知,我已成功转换为 mouse3D 中的 THREE.js 坐标(请参阅this question 的答案)。
那么,在这种设置下,如何使用正交相机平滑地缩放到鼠标点 (mouse3D) 并保持二维视图?提前致谢。
【问题讨论】:
-
你有没有办法尝试代码?
-
所以问题是在缩放过程中相机旋转?
-
你找到解决方案了吗 Scott H
标签: javascript camera three.js zooming