iTranslated by AI
The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
📚
How to load Three.js text fonts in Next.js 13
Conclusion
- Create a
fontsfolder in thepublicfolder and place the JSON file inside it.- A 404 error will occur if it is not in the
publicfolder.
- A 404 error will occur if it is not in the
Environment
"dependencies": {
"@types/node": "20.4.5",
"@types/react": "18.2.17",
"@types/react-dom": "18.2.7",
"next": "13.4.12",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-hook-form": "^7.45.2",
"three": "^0.155.0",
"typescript": "5.1.6",
},
"devDependencies": {
"@types/three": "^0.155.0",
}
}
Situation
const fontLoader = new FontLoader();
fontLoader.load(
'/fonts/helvetiker_regular.typeface.json',
(font) => {
const textGeometry = new TextGeometry("Hello Three.js", {
font: font,
size: 0.5,
height: 0.2,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: 0.03,
bevelSize: 0.02,
bevelOffset: 0,
bevelSegments: 5,
});
textGeometry.center();
const textMaterial = new THREE.MeshStandardMaterial();
const text = new THREE.Mesh(textGeometry, textMaterial);
text.castShadow = true;
text.position.z = 1;
scene.scene.add(text)
}
);
When executed,
Failed to load resource: the server responded with a status of 404 (Not Found) :3000/fonts/helvetiker_regular.typeface.json:1
an error like the one above occurs.
Solution
- Create a
fontsfolder in thepublicfolder of Next.js and place the font JSON inside it.
The following articles were helpful for learning how to convert files such as TTF into JSON.
Reference Articles
Reference Code
By looking at this repository, I found that placing the files in the public folder solves the issue. Thank you.
Discussion