ReactVRで日本語フォントを扱う
ご無沙汰しております。ちらです。
以前からやるやる詐欺をしていたReact VRにとうとう手を出し始めました。
とは言っても、まだまだ、サンプルコード動いたー!やったー!って喜んでる段階ですが。
で、本題です。試しにサンプルコードのhello部分をはろーわーるどにしてみたのですが、こんな感じに文字化けしてしまいました。
日本語フォントをインストールしたら直ったので、備忘録として手順を残しておきます。
手順はこのドキュメントを参考にしました。
react-vr/Fonts.md at master · facebook/react-vr · GitHub
日本語フォントのダウンロード
ドキュメントを見ると、ovruiパッケージ内のfontsディレクトリに日本語フォント用のファイルが含まれているので、これを使えばいいよ的なことが書いてある気がします。
そこでまずプロジェクト直下のstatic_assetsにfontsディレクトリを作って、そこに必要なファイルをダウンロードしてきます。
必要なファイル: https://github.com/facebook/react-vr/raw/master/OVRUI/fonts/japanese.png https://github.com/facebook/react-vr/raw/master/OVRUI/fonts/japanese.fnt
client.jsを書き換える
次にドキュメントの真似をしながらvrディレクトリ内のclient.jsを書き換えます。こんな感じであってるかな?
// Auto-generated content. // This file contains the boilerplate to set up your React app. // If you want to modify your application, start in "index.vr.js" // Auto-generated content. import {VRInstance} from 'react-vr-web'; import * as OVRUI from 'ovrui'; var fallbackFonts = { '../static_assets/fonts/japanese.fnt': '../static_assets/fonts/japanese.png' }; // use the embedded defaultFont and and fallbacks var font = OVRUI.loadFont(); var count = 0; function init(bundle, parent, options) { function addFallback(fallbackFont) { OVRUI.addFontFallback(font, fallbackFont); count--; if (count === 0) { // all fallbacks are loaded start the VRInstance // 'font' contains everything React VR needs to render <Text> elements with // your custom font. // Pass it to the boilerplate init code const vr = new VRInstance(bundle, 'ReactVrSample', parent, { // Pass in the custom font as an initialization property font: font, ...options, }); vr.render = function() { // Any custom behavior you want to perform on each frame goes here }; // Begin the animation loop vr.start(); return vr; } } for (var key in fallbackFonts) { // allow each font to asynchronously load in parallel and start VR instance when all complete count++; OVRUI.loadFont(key, fallbackFonts[key]).then((fallbackFont) => { addFallback(fallbackFont); }); } } window.ReactVR = {init};
確認
画面をリロードするとこんな感じで日本語が表示されます。やったーヾ( ・ω・)ノ
おわりに
React VRが1.1.0からバージョンが上がった際には、コードをコピペしても動かなくなる可能性があるので、その際は公式ドキュメントをご覧ください。
以上です。