乐趣区

threejs-3d中-获取鼠标经过地点的新建元素

获取鼠标经过地点的元素
// 获取鼠标经过地点的元素

intersectObjects(pointer,camera,obj) {
    var domElement = viewer.impl.canvas;
    var pointerVector = new THREE.Vector3();
    var pointerDir = new THREE.Vector3();
    var ray = new THREE.Raycaster();
    var rect = domElement.getBoundingClientRect();
    var x = (( pointer.clientX - rect.left) / rect.width ) * 2 - 1;
    var y = - (( pointer.clientY - rect.top) / rect.height ) * 2 + 1;
    if (camera.isPerspective) {pointerVector.set(x, y, 0.5);
        pointerVector.unproject(camera);
        ray.set(camera.position, pointerVector.sub(camera.position).normalize());
    } else {pointerVector.set(x, y, -1);
        pointerVector.unproject(camera);
        pointerDir.set(0, 0, -1);
        ray.set(pointerVector, pointerDir.transformDirection(camera.matrixWorld));
    }

    var intersections = ray.intersectObjects(obj, true);
    return intersections[0] ? intersections[0] : false;
}


// 鼠标滑过小球市时触发的事件
hoverIntersectObjects(event){
    var flag =true;
    var pointer = event.pointers ? event.pointers[0] : event;
    var camera = viewer.getCamera();
    var objects = viewer.impl.overlayScenes.lable.scene.children;
    var obj = viewer.vmethods.intersectObjects(pointer,camera,objects).object;
    if(obj){
        // 隐藏不可选
        if(!obj.visible)return;
        if(this.oldObj) this.oldObj.material.color.setHex(this.oldObj.currentHex);
        this.oldObj=obj;
        this.oldObj.currentHex = obj.material.color.getHex();
        this.oldObj.material.color.setHex(0x660000);
        viewer.impl.invalidate(false, false, true);
        var pos=viewer.worldToClient(this.oldObj.position);
        var posX=pos.x-$(".k-tooltip").width()/2;
        var posY=pos.y-$(".k-tooltip").height()-20;
        $(".k-tooltip").css({"left":posX,"top":posY});
        $(".k-tooltip").show();
        $(".k-tooltip .name").text(this.oldObj.name);
    }else{if(this.oldObj) {this.oldObj.material.color.setHex( this.oldObj.currentHex);
            viewer.impl.invalidate(false, false, true)
            $(".k-tooltip").hide();}

    }
}
退出移动版