我正在尝试用THREE.js制作游戏。我有一条路径,有很多曲线和一些直线路径。
我在互联网上找到了一个例子并试图实现TrackballControls.js以使相机跟随一条线,但是相机看不到前方,它移动很奇怪。
以下是代码:
var controls = new THREE.TrackballControls(camera, render.domElement);
var numPoints = 50;
spline = new THREE.CatmullRomCurve3([
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(50, 0, 0),
new THREE.Vector3(0, 0, -100)
]);
var material = new THREE.LineBasicMaterial({
color: 0xff00f0,
});
var geometry = new THREE.Geometry();
var splinePoints = spline.getPoints(numPoints);
for (var i = 0; i < splinePoints.length; i++) {
geometry.vertices.push(splinePoints[i]);
}
line = new THREE.Line(geometry, material);
line.position.set(0, 0, 0);
scene.add(line);
var counter = 0;
if (counter <= 1) {
camera.position.copy( spline.getPointAt(counter) );
tangent = spline.getTangentAt(counter).normalize();
axis.crossVectors(up, tangent).normalize();
var radians = Math.acos(up.dot(tangent));
camera.quaternion.setFromAxisAngle(axis, radians);
counter += 0.005
} else {
counter = 0;
}
感谢。
答案 0 :(得分:2)
以下example显示了如何创建遵循样条曲线的摄像机。只需点击按钮&#34; Camera Spline Animation View&#34;看效果。也许您可以使用底层代码来解决您的问题。