How to avoid labels overlapping in JavaScript?

2019-06-13 01:50发布

I have textured an image to sphere which is in 3D (shown below) and I have also added planegeometry (labels) which are overlapping. How can I separate the labels using three.js library? enter image description here2

labelBox.prototype.update = function()
{

camera.updateMatrixWorld();
this.position.project(camera);

var screenvector = new THREE.Vector3();
screenvector.copy(this.position);
oldlabelpos.push(screenvector);
//this.position.normalize();

this.posx = Math.round((this.position.x + 1)* this.domElement.offsetWidth/2 + webglviewport.left);
this.posy = Math.round((1 - this.position.y)* this.domElement.offsetHeight/2);

var boundingRect = this.box.getBoundingClientRect();  //getBoundingClientRect() method returns the 
                                              //size of element (here it is box) and its position relative to viewport
//update the box overlays position
this.box.style.left = this.posx + 'px';
this.box.style.top = this.posy  + 'px';

this.occludeLabel(this.box, this.marker);
};

labelBox.prototype.LabelUpdateFn = function()
{       
    //var labels =  

document.getElementById('MovingLabel1');//.getElementsByClassName('spritelabel'); //labels = $(root).find(".spritelabel");

    for(var k=0; k<oldlabelpos.length; k++)
        {
            this.reposition = new THREE.Vector3();
            this.reposition = convertlatlonToVec3(oldlabelpos[k].x, 
            oldlabelpos[k].y).multiplyScalar(radius);
            this.reposition.normalize();

            camera.updateMatrixWorld();
            this.reposition.unproject(camera);

            this.newposx = Math.round((this.reposition.x + 1)* 
            this.domElement.offsetWidth/2 + webglviewport.left) - this.posx;
            this.newposy = Math.round((1 - this.reposition.y)* 
            this.domElement.offsetHeight/2) - this.posy;

            var boundingRect = this.box.getBoundingClientRect();

            this.box.style.left = (this.newposx + boundingRect.width) + 'px';
            this.box.style.top = this.newposy + 'px';

            //$(this.box).css(left) = 

            this.occludeLabel(this.box, this.marker);
            //var updatedpos = {updateposx, updateposy};

            //newlabelpos.push(updatedpos);             
        }                       
}

1条回答
够拽才男人
2楼-- · 2019-06-13 02:01

If you say "Avoid label overlapping" in 3D you want to checkout "Clipping" for a viewport. As an example: See this article

查看更多
登录 后发表回答