Three.js: change the pivot point of a sprite

后端 未结 3 1169
南旧
南旧 2021-01-06 07:25

I\'ve created a 3D map and I\'m labelling points on this map through Sprites. This in itself works fine, except for the positioning of the sprite labels.

Because I\'

3条回答
  •  猫巷女王i
    2021-01-06 07:49

    As suggested by WestLangley, I've created a workable solution by changing the sprite position based on the viewing angle though it took me hours to work out the few lines of code needed to get the math working. I've updated my application too, so see that for a live demo.

    With the tilt angle phi and the heading angle theta as computed from the camera in OrbitControls.js the following code computes a sprite offset that does exactly what I want it to:

    // Given:
    // phi = tilt; 0 = top down view, 1.48 = 85 degrees (almost head on)
    // theta = heading; 0 = north, < 0 looking east, > 0 looking west
    
    // Compute an "opposite" angle; note the 'YXZ' axis order is important
    var euler = new THREE.Euler( phi + Math.PI / 2, theta, 0, 'YXZ' );
    // Labels are positioned 5.5 units up the Y axis relative to its parent
    var spriteOffset = new THREE.Vector3( 0, -5.5, 0 );
    // Rotate the offset vector to be opposite to the camera
    spriteOffset.applyMatrix4( new THREE.Matrix4().makeRotationFromEuler( euler ) );
    
    scene.traverse( function ( object ) {
       if ( ( object instanceof THREE.Sprite ) && object.userData.isLabel ) {
          object.position.copy( spriteOffset );
       }
    } );
    

    Note for anyone using this code: that the sprite labels are children of the object group they're referring to, and this only sets a local offset from that parent object.

提交回复
热议问题