three#FontLoader TypeScript Examples

The following examples show how to use three#FontLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: TextMeshObject.ts    From movy with MIT License 5 votes vote down vote up
fontLoader = new FontLoader()
Example #2
Source File: Text.tsx    From spacesvr with MIT License 5 votes vote down vote up
export function Text(props: TextProps) {
  const {
    text,
    vAlign = "center",
    hAlign = "center",
    size = 1,
    bevel = false,
    color = "#000000",
    font: fontFile,
    material,
    ...rest
  } = props;

  const font = useLoader(FontLoader, fontFile || FONT_FILE);
  const mesh = useRef<Mesh>();

  const config = useMemo(
    () => ({
      font,
      size,
      height: 0.75 * size,
      curveSegments: 32,
      bevelEnabled: bevel,
      bevelThickness: 0.15 * size,
      bevelSize: 0.0625 * size,
      bevelOffset: 0,
      bevelSegments: 8,
    }),
    [font]
  );

  useEffect(() => {
    if (!mesh.current) return;

    const size = new Vector3();
    mesh.current.geometry.computeBoundingBox();
    mesh.current.geometry?.boundingBox?.getSize(size);
    mesh.current.position.x =
      hAlign === "center" ? -size.x / 2 : hAlign === "right" ? 0 : -size.x;
    mesh.current.position.y =
      vAlign === "center" ? -size.y / 2 : vAlign === "top" ? 0 : -size.y;
  }, [text]);

  return (
    <group name="spacesvr-text" {...rest} scale={[0.1 * size, 0.1 * size, 0.1]}>
      <mesh ref={mesh} material={material}>
        <textGeometry attach="geometry" args={[text, config]} />
        {!material && (
          <meshPhongMaterial
            attach="material"
            color={color}
            reflectivity={30}
          />
        )}
      </mesh>
    </group>
  );
}