我正在为我们的THREE.js应用程序开发正交相机。本质上,这个相机将以2D形式向用户呈现场景(用户可以在2D和3D相机之间切换)。这个相机将允许平移和缩放到鼠标点。我已经完成了平移和缩放,但是还没有实现缩放到鼠标点。以下是我的代码:
一些缩放朝向鼠标点发生,但似乎不稳定。我想保持一个2D视图,所以当我缩放时,我也移动相机(而不是有一个非垂直的目标,这会破坏2D效果)。
我从this question中得到了线索。据我所知,我成功地将鼠标转换为THREE.js坐标
那么,考虑到这个设置,如何使用正交相机平滑地缩放到鼠标点(
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中得到了线索。据我所知,我成功地将鼠标转换为THREE.js坐标
mouse3D
(请参阅this question的答案)。那么,考虑到这个设置,如何使用正交相机平滑地缩放到鼠标点(
mouse3D
),并保持二维视图?提前感谢。