【问题标题】:Using raycaster to highlight a row of meshes three.js使用raycaster高亮一行网格three.js
【发布时间】:2020-09-01 19:32:28
【问题描述】:

我正在尝试使用 raycaster 来识别要在鼠标悬停时突出显示/着色的一行 3d 立方体。我关注了这篇文章Change color of mesh using mouseover in three js。我面临的问题是它只突出显示一个立方体,即鼠标所在的立方体而不是整行。请在下面找到我的伪代码:

var cubesList = new THREE.Group();

function createScene () {


    var cubeSize = 2;



    for ( var i = 0; i < noOfEntries; i++ ) {
        var entry = entries[ i ];

        var entryObjects = entry.objects;

        var entryCubesGroup = new THREE.Group();

        var noOfObjects = entry.objects.length;
        for ( var j = 0; j < noOfObjects; j++ ) {
            var object = entryObjects[ j ];


            var cube = createCube( cubeSize ); //THREE.Object3d group of 9 cubes

            entryCubesGroup.add( cube );
            if ( j === Math.round( noOfObjects / 4 ) - 1 && i === Math.round( noOfEntries / 4 ) - 1 ) {
                cameraTarget = cube;

            }

        }


        cubesList.add( entryCubesGroup );
    }

    scene.add( cubesList );

    camera.position.x = 15;
    camera.position.y = 15;
    camera.position.z = 15;
    camera.lookAt( new THREE.Vector3( cameraTarget.position.x, cameraTarget.position.y, cameraTarget.position.z ) );



    var light = new THREE.PointLight( 0xffffff, 1, 0 );
    light.position.set( 15, 15, 5 );
    light.castShadow = true;
    scene.add( light );


}

function animate () {


    renderer.render( scene, camera );
    update();

}

function onDocumentMouseMove ( event ) {

    event.preventDefault();

    mouse.x = ( event.clientX / renderer.domElement.width ) * 2 - 1;
    mouse.y = -( event.clientY / renderer.domElement.height ) * 2 + 1;



    animate();


}

    function update() {

                var vector = new THREE.Vector3(mouse.x, mouse.y, 1);
                vector.unproject(camera);
                var ray = new THREE.Raycaster(camera.position, vector.sub(camera.position).normalize());

                var intersects = ray.intersectObjects(eventCubesList.children, true);

                if (intersects.length > 0) {

                    if (intersects[0].object != INTERSECTED) {                       

                        if (highlightedRow)
                            unhighlightRow(highlightedRow);

                        INTERSECTED = intersects[0].object;
                        var timestamp = INTERSECTED.userData;

                        var selectedRow = getSelectedRow(timestamp);
                        highlightedRow = selectedRow;
                        highlightRow(selectedRow);

                    }
                    else {
                        if (INTERSECTED) {
                            if (highlightedRow) {
                                var timestamp = INTERSECTED.userData;
                                var row = getSelectedRow(timestamp);
                                unhighlightRow(row);
                            }
                            highlightedRow = null;

                        }


                        INTERSECTED = null;
                    }



                }

                 function unhighlightRow(cubes) {
                for (var i= 0; i < cubes.length; i++) {
                    var cube = cubes[i];
                    for (var j = 0; j < cube.children.length; j++) {
                        var child = cube.children[j];
                        child.material.color.setHex(cube.originalColor);

                    }


                }
            }

            function  highlightRow(cubes) {
                for (var i = 0; i < cubes.length; i++) {
                    var cube = cubes[i];
                    for (var j = 0; j < cube.children.length; j++) {
                        var child = cube.children[j];                       
                        child.material.color.setHex(0xffff00);
                        break;

                    }

                }
            }

有没有一种方法可以将一行中的所有立方体突出显示为黄色,而不仅仅是一个立方体?

【问题讨论】:

    标签: javascript arrays three.js mouseover raycasting


    【解决方案1】:

    您需要保留自己的数据,了解哪些多维数据集位于哪些行中。当一个多维数据集被突出显示时,您需要查找其所在的行并突出显示该行中的其他多维数据集

    ---伪代码---

    INTERSECTED = intersects[ index ].object;
    row = getRowObjectIsIn(INTERSECTED)
    for each object in row
       highlight object
    

    【讨论】:

    • 感谢@gman 的回复。不过我有一个问题。如果它识别出一个立方体(它是 9 个小立方体的 3d 对象)并将 9 个立方体一起着色为黄色,为什么不给整行着色。我已将 cubeList.children 作为 intersectObjects 中的数组传递。每个孩子都是一行。
    • 我按照您的建议进行了尝试,我可以让它突出显示该行并取消突出显示它(上面编辑的代码)但是当它这样做时会产生闪烁的灯泡效果。黄色在所有立方体上闪烁。是因为它是一组 9 个立方体吗?有没有办法让它稳定?
    猜你喜欢
    • 1970-01-01
    • 2014-03-05
    • 2014-08-18
    • 2018-07-13
    • 2013-04-14
    • 2015-12-25
    • 1970-01-01
    • 1970-01-01
    • 2013-07-05
    相关资源
    最近更新 更多