在球体表面放置一个物体

问题描述 投票:0回答:1

这是我的第一篇文章,所以我会尽力解释。每次我将一个物体放在旋转的球体表面上时,球体旋转得越多,它就会偏移。

我尝试将其偏移回鼠标,但没有用。我不知所措。

这是我的代码:

function placeObj(event) {
    if (event.button == 0) {
        switch(placeObj) {
            case null:
                Mouse.x = (event.offsetX / window.innerWidth) * 2 - 1;
                Mouse.y = -(event.offsetY / window.innerHeight) * 2 + 1;
                var Intersects = raycast(Mouse, [Moon]) // Returns the intersects

                if (Intersects.length > 0) {
                    let intersectSelect = Intersects[0];
                    let intersectObj = intersectSelect.object;
                    if (intersectObj.name == "Sphere") // Check if its a sphere {
                        var fbxLoader = new FBXLoader();
                        var cube = new THREE.Mesh(
                            new THREE.BoxGeometry(0.2, 0.2, 0.02),
                            new THREE.MeshBasicMaterial(),
                        )

                        Moon.add(cube);
                        cube.position.copy(intersectSelect.point);
                        cube.lookAt(Intersects[0].face.normal);
                    }
                }
                break;
            
            default:

                break;
        }
    }
}

这是我的动画功能。

function animate() {
    Controls.update();
    requestAnimationFrame( animate );

    if (!stopRotation) {
        Moon.rotation.y += 0.0001;
    }

    renderer.render( Scene, camera );
}

这里有一个 GIF 来演示发生了什么。

javascript jquery three.js quaternions raycasting
1个回答
0
投票

感谢 Marquizzo,我找到了解决问题的办法。

我需要使用 WorldToLocal 来获取本地位置而不是世界位置。

cube.position.copy(Moon.worldToLocal(intersectSelect.point));
© www.soinside.com 2019 - 2024. All rights reserved.