我很抱歉,如果之前有人问过,但到目前为止我还没有找到任何东西。但我的问题是我想要创建两个函数,我在其中一个中创建一个路径,在另一个中创建一个对象,比如说一个球体。
function makeCurve() {
var Path = new THREE.CubicBezierCurve3(
new THREE.Vector3(0, 0, 0),
new THREE.Vector3(0, 70, 0),
new THREE.Vector3(0, 70, 20),
new THREE.Vector3(0, 0, 20)
);
Path.id = 'IwantThisPath';
var geometry = new THREE.Geometry();
geometry.vertices = Path.getPoints(50);
var material = new THREE.LineBasicMaterial({
color: 0xff0000
});
var PathObject = new THREE.Line(geometry, material);
PathObject.name = pathname;
scene.add(PathObject)
}
function sphere() {
var object = new THREE.Mesh(new THREE.SphereGeometry(20, 100, 100), new THREE.MeshNormalMaterial());
object.overdraw = true;
sphere.name = spherename;
scene.add(sphere)
}
然后将textarea链接到这些函数,然后用户可以通过
轻松创建球体和路径path1 = new makeCurve();
sphere1 = new sphere();
然后我想创建第三个函数,在那里我可以让球体沿着路径行进。
function along(path, object) {
name1 = path.name;
name2 = object.name;
var MyPath = document.getElementById('IwantThisPath') //givesNull
myObjPath = scene.getObjectByName(name1); //I don't want this one
myObjObject = scene.getObjectByName(name2);
counter = 0;
var tangent = new THREE.Vector3();
var axis = new THREE.Vector3();
var up = new THREE.Vector3(0, 1, 0);
setInterval(moveObject, 50);
function moveObject() {
if (counter <= 1) {
myObjObject.position.copy(myObjPath.getPointAt(counter));
tangent = myObjPath.getTangentAt(counter).normalize();
axis.crossVectors(up, tangent).normalize();
var radians = Math.acos(up.dot(tangent));
myObjObject.quaternion.setFromAxisAngle(axis, radians);
} else {
counter = 0
}
}
}
在最后的功能中,事情变得棘手。因为我无法使用添加到场景中的PathObject来使球体移动,所以我需要在开头制作Path。但无论我尝试什么,我都无法取得它。它没有被添加到场景中并在其上放置id或名称只是呈现null。 我希望有人能为我解决这个问题提供更好的解决方案,而且这个问题已经足够清楚了。
对于好奇:我正在编写一个程序,这样我就可以更容易地创建three.js动画了,我想用这个来创建物理教材。