具有React-Three-Renderer的Sprite标签(包括MVCE)

Sco*_*y H 10 javascript label sprite three.js reactjs

我正在使用react-three-renderer(npm,github)来构建一个包含three.js的场景.

我正在尝试使用<sprite><spriteMaterial>制作一个始终面向相机的标签,就像在stemkoski的例子中一样.

但是,我无法显示标签并正确设置其坐标.我在Sprite-Label-Test上有一个最低限度可验证的完整示例.下载,运行npm install并打开_dev/public/home.html.

我的目标是看到文字显示在我期望的地方,但正如你所看到的,它只是黑色.为了证明精灵在相机的视野中,我把一个盒子放在同一个位置.要查看它从render方法中取消注释并重新gulp.

这是我的档案.它有两个主要组件,componentDidMount即为sprite创建文本的render方法,以及方法.

var React = require('react');
var React3 = require('react-three-renderer');
var THREE = require('three');
var ReactDOM = require('react-dom');

class Simple extends React.Component {
  constructor(props, context) {
    super(props, context);

    // construct the position vector here, because if we use 'new' within render,
    // React will think that things have changed when they have not.
    this.cameraPosition = new THREE.Vector3(0, 0, 100);
  }

  componentDidMount() {
    var text = "Hello world";
    var canvas = document.createElement('canvas');
    var context = canvas.getContext('2d');
    var metrics = context.measureText( text );
    var textWidth = metrics.width;

    context.font = "180px arial Bold";
    context.fillStyle = "rgba(255,0,0,1)";
    context.strokeStyle = "rgba(255,0,0,1)";
    context.lineWidth = 4;

    context.fillText( text, 0, 0);

    var texture = new THREE.Texture(canvas)
    texture.needsUpdate = true;

    this.spriteMaterial.map = texture;
    this.spriteMaterial.useScreenCoordinates = false;
  }

  render() {
    const width = window.innerWidth; // canvas width
    const height = window.innerHeight; // canvas height

    var position = new THREE.Vector3(0, 0, 10);
    var scale = new THREE.Vector3(1,1,1);

    return (<React3
      mainCamera="camera" // this points to the perspectiveCamera which has the name set to "camera" below
      width={width}
      height={height}
    >
      <scene>
        <perspectiveCamera
          name="camera"
          fov={75}
          aspect={width / height}
          near={0.1}
          far={1000}

          position={this.cameraPosition}
        />
        <sprite position={position} scale={scale} ref={(sprite) => this.sprite = sprite}>
          <spriteMaterial ref={(spriteMaterial) => this.spriteMaterial = spriteMaterial}></spriteMaterial>
        </sprite>
        {/*<mesh position={position}>
          <boxGeometry
            width={10}
            height={10}
            depth={10}
          />
          <meshBasicMaterial
            color={0x00ff00}
          />
        </mesh>*/}
      </scene>
    </React3>);
  }
}

ReactDOM.render(<Simple/>, document.querySelector('.root-anchor'));
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?如何在与该行建立的位置显示精灵文本标签var position = new THREE.Vector3(0, 0, 10);?提前致谢.

Rob*_*xyz 6

你所拥有的只是一个小错误:

绘制的文字<canvas>固定在左下角.因此,在(0,0)处绘制文本甚至不可见.它完全在画布外面(如下面的白色所示):

画布外的文字

-    context.fillText( text, 0, 0);
+    context.fillText( text, 0, 18);
Run Code Online (Sandbox Code Playgroud)

这就是@df fontsize第147行设置绘图位置时使用的原因.


此外,你的相机没有看任何东西.React-Three允许将此作为​​您的属性<perspectiveCamera/>.

+          lookAt={this.cameraLook}
Run Code Online (Sandbox Code Playgroud)

我打开了针对你的MVCE存储库的pull请求.