Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
648 views
in Technique[技术] by (71.8m points)

reactjs - Load bm-font in react-three-fiber and NextJS

I'm trying to get bitmap fonts working in react-three-fiber on a NextJS app but I'm having trouble figuring out how to get it working. This is the package I'm trying to use: https://github.com/Jam3/three-bmfont-text

As you can see, both of the required packages three-bmfont-text and load-bmfont use CommonJS require() syntax which to be honest has always given me nothing but trouble when using NextJS. I went down the path of trying to use this in getServerSideProps since that only runs on the server, but that only runs in the pages/ folder, which I need this in a global canvas component. Also, I don't think that was the right idea.

I also found a three-bmfont package for react-three-fiber but it threw encoding errors when trying to import the PNG file for the font texture. I had no idea how to troubleshoot this, so I decided maybe that package was not the answer.

I'm sure there is a way to do this and I apologize for maybe a slightly opinionated question, but I have went down a few paths that led me nowhere in how to implement this and I am stuck. Any help would be appreciated.

question from:https://stackoverflow.com/questions/65833291/load-bm-font-in-react-three-fiber-and-nextjs

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

three-bmfont-text expects a global THREE object. You can provide it like this in next.config.js:

config.plugins.push(
 new webpack.ProvidePlugin({
  THREE: 'three',
 })
)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...