【问题标题】:How would I be able to get my GLTF object follow someone using touch on a phone?我怎样才能让我的 GLTF 对象跟随在手机上使用触摸的人?
【发布时间】:2019-06-02 23:15:35
【问题描述】:

使用有关 three.js 和其他问题的资源,我创建了一个 GLTF 模型,当鼠标在画布上移动时它会跟随鼠标(请参阅下面的代码笔)。

但是,在我的 Android 手机上查看页面时,模型不会像在 OrbitControls 中使用时那样旋转以跟随触摸。我试图了解如何使用事件监听器; touchstart、touchend 和 touchmove,因为我认为这可能是我需要让它响应触摸事件,但不幸的是我不太擅长 JavaScript,而且对 three.js 很陌生。

一个工作示例(尽管我不知道他们是否使用three.js)将在此页面上:https://garden-eight.com/。模型跟随鼠标,在移动设备上查看时跟随手指。

请问有人可以借用他们的专业知识吗? :)

我非常乐意提供更多信息。我没有在 StackOverflow 上问过很多问题。

https://codepen.io/Beddalla/pen/qGLeyR

        var loader = new THREE.GLTFLoader(); /* Creates a GLTF Loader */


        loader.load( 'https://api.codetabs.com/v1/proxy?quest=https://www.adambeddall.com/monkey.glb', function ( gltf ) { /* Path to model */
            scene.add( gltf.scene ); /* Adds the model to the scene */
            model = gltf.scene
        });

        var plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), -4);
        var raycaster = new THREE.Raycaster();
        var mouse = new THREE.Vector2();
        var pointOfIntersection = new THREE.Vector3();
        canvas.addEventListener("mousemove", onMouseMove, false);

        function onMouseMove(event){
          mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
            mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
          raycaster.setFromCamera(mouse, camera);
          raycaster.ray.intersectPlane(plane, pointOfIntersection);
          model.lookAt(pointOfIntersection);
        }

【问题讨论】:

    标签: html three.js


    【解决方案1】:

    解决此问题的一种方法是向touchmove 事件添加一个事件侦听器,然后使用来自event.touches[ 0 ].clientXevent.touches[ 0 ].clientY 的事件数据来计算各自的mouse 向量。

    function onTouchMove(event){
    
        mouse.x = ( event.touches[ 0 ].clientX / window.innerWidth ) * 2 - 1;
        mouse.y = - ( event.touches[ 0 ].clientY / window.innerHeight ) * 2 + 1;
    
        // rest of your code
    
    }
    

    更新的 Codepen:https://codepen.io/anon/pen/VOgwvO

    three.js R105

    【讨论】:

    • 这太棒了!非常容易理解并且完全符合我的要求。自从您的回答以来,我一直在寻找一种方法,以便当有人将鼠标移出页面然后在不同的位置重新打开时,模型不会跳转到新的旋转,而是平滑过渡。我想你以前没有遇到过这种情况?
    • 嗯,也许你可以用Quaternion.rotateTowards()而不是lookAt()来解决这个问题。还有一个官方示例演示了这种方法:threejs.org/examples/webgl_math_orientation_transform
    • 有趣,谢谢你的链接 - 我得看看,看看我能不能理解哈哈!
    • 不幸的是,在按照 three.js 上的示例进行操作时,我无法让它工作。对于那些对这里感兴趣的人,我可以找到一个代码笔:codepen.io/Beddalla/pen/xNvLqY(模型已加载,但无法让它查看目标。即使可以,我也不知道我是否能够弄清楚了解如何让它看鼠标/触摸)尝试以下示例:threejs.org/examples/#webgl_materials_bumpmapcodepen.io/anon/pen/opGErz我很失望我无法让它工作,但也许这会在未来帮助某人。任何帮助表示赞赏。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2020-07-22
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多