【问题标题】:how to decide rotation of a marker or pointer on a 3d .obj Object so that marker will look like it is pointing to some area of 3D object如何决定标记或指针在 3d .obj 对象上的旋转,以便标记看起来像是指向 3D 对象的某个区域
【发布时间】:2020-02-18 12:39:40
【问题描述】:

我看起来像这样 (https://jsfiddle.net/xg1o4ekr/378/) 而不是地球,想象各种其他形状的 3D 对象和标记向外出现。由于我的 3d 对象不是圆形,我不能使用上面给出的代码 单击加载的 .obj 3D 模型时,将在单击位置上呈现一个指针(使用 raycaster)。使用三个 js 几何形状,我创建了一个指针。该指针将呈现在单击的位置。如何确定指针的旋转。 (它应该动态决定指针的旋转)

onMouseClick = (event) => {
    event.preventDefault();

    // calculate objects intersecting the picking ray
    var intersects = this.getIntersects(event.layerX, event.layerY);
    if (intersects.length > 0) {
        var res = intersects.filter(function (res) {
            return res && res.object;
        })[0];
        if (res && res.object) {
            this.selectedObject = res.object;
            console.log(res);
            const Pointer = this.createPointer();
            var g2 = new THREE.Group();
            g2.add(Pointer.cone);
            g2.add(Pointer.sphere);
            g2.position.set(res.point.x, res.point.y, res.point.z);
            //g2.rotation.set(res.point.x, res.point.y, res.point.z); //not working fine

            g2.rotateX(0.7);
            g2.rotateZ(0.9);
            g2.rotateY(1);// not working fine
            this.scene.add(g2);
        }
    }
}

getIntersects = (x, y) => {
    x = (x / this.width) * 2 - 1;
    y = - (y / this.height) * 2 + 1;
    this.mouseVector.set(x, y);
    this.raycaster.setFromCamera(this.mouseVector, this.camera);
    return this.raycaster.intersectObject(this.group, true);
}

【问题讨论】:

    标签: reactjs three.js rotation marker raycasting


    【解决方案1】:

    有趣的问题。这里已经很晚了,所以我无法尝试代码,但这可能有帮助吗?

    你点击的对象有法线吗? 您可以在 intersets[0].face.normal(或 .vertexNormals)中找到它。 您可以将指针与它对齐..

    var normal = intersects[0].face.normal:
    var axis = new THREE.Vector3(0, 1, 0); //(or maybe -1? i'm not sure, play around)
    g2.quaternion.setFromUnitVectors(axis, normal);
    

    如果您想将指针设置在距表面一定距离处;

    var distance = 4;
    g2.position.add( normal.multiplyScalar(distance) );
    

    另外,与其每次都创建一个指针,不如只创建一次然后做pointer.clone()?

    【讨论】:

    • 以上代码正在运行,谢谢:) 我有一个疑问,假设我有一块布的 .obj 格式。它的正面和背面共享相同的坐标。如何确定它的正面或背面?如果我要将坐标详细信息保存在数据库中并通过该坐标再次呈现指针/标记。
    • 太棒了!是的,我帮助了。我理解你的问题,我试过了,没有办法确定你在光线投射的材料的哪一边。我认为这是一个棘手的问题,而且本身就是一个全新的问题。我能想到的唯一解决方法是使用相机的位置/方向向量来确定你在布料的哪一侧(相对于法线方向)。
    • 嗨,我试过 var normal = intersects[0].face.normal; var axis = new THREE.Vector3(0, 1, 0); pointer.position.set(res.point.x.toFixed(4), res.point.y.toFixed(4),res.point.z.toFixed(4)); pointer.quaternion.setFromUnitVectors(axis, normal);
    • 以上代码适用于 obj 格式模型,但不适用于 GLTF 和 FBX 格式。怎么办?
    • 我不确定。它会在控制台中给出错误吗?会不会是 gltf/fbx 模型上没有法线?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-17
    • 1970-01-01
    • 2012-02-20
    • 1970-01-01
    • 1970-01-01
    • 2017-08-18
    • 1970-01-01
    相关资源
    最近更新 更多