[英]Three JS: I am getting this error Unsupported OpenType signature <!DO
I have a three.js project, and I use parcel as module bundler.我有一个 three.js 项目,我使用parcel作为模块捆绑器。 I try to load a font with THREE js TTFLoader but I get this error.
我尝试使用三个 js TTFLoader加载字体,但出现此错误。 I've looked up similar problem on the inte.net, some says that it has something to do with the relative path of the font, but I think it doesn't.
我在inte.net上查过类似的问题,有人说是跟字体的相对路径有关,我觉得不是。 I don't know where the problem actually is
我不知道问题到底出在哪里
Here is the snapshot of the error这是错误的快照
Here is the project tree:这是项目树:
Here is part of the code that contains error:这是包含错误的代码的一部分:
app.js应用程序.js
// load font -- three font
const ttfLoader = new TTFLoader();
ttfLoader.load('fonts/Mont-Regular.ttf', (json) => {
const font = new FontLoader(json);
this.textGeo = new TextGeometry('Hello Text', {
font: font,
size: 200,
height: 50,
curveSegments: 12,
});
this.textMaterial = new THREE.MeshPhongMaterial({
color: 0xff0000,
side: THREE.DoubleSide,
});
this.textMesh = new THREE.Mesh(this.textGeo, this.textMaterial);
this.textMesh.position.set(0, 0, 0);
});
this.scene.add(this.textMesh);
Here is the codesandbox: https://codesandbox.io/s/three-project-0hpqjr这是codesandbox: https://codesandbox.io/s/three-project-0hpqjr
How can I solve this problem?我怎么解决这个问题? Thankyou
谢谢
Finally I've figured out Where I did wrong, this time I use THREE FontLoader instead of TTFLoader, and I use absolute path from the root project directory.最后我弄清楚了哪里做错了,这次我使用三个FontLoader而不是 TTFLoader,并且我使用根项目目录的绝对路径。 I put this add to scene code
this.scene.add(this.textMesh)
inside the callback function. For the font, I used font example provided by the library: 'node_modules/three/examples/fonts/helvetiker_regular.typeface.json'
and it uses absolute path我将此添加到场景代码
this.scene.add(this.textMesh)
中的回调 function 中。对于字体,我使用了库提供的字体示例: 'node_modules/three/examples/fonts/helvetiker_regular.typeface.json'
它使用绝对路径
Here is the code:这是代码:
const fontLoader = new FontLoader();
fontLoader.load(
'node_modules/three/examples/fonts/helvetiker_regular.typeface.json',
(Helvfont) => {
// const font = fontLoader.parse(json);
this.textGeo = new TextGeometry('Yeah', {
font: Helvfont,
size: 0.2,
height: 0,
// curveSegments: 12,
});
this.textMaterial = new THREE.MeshNormalMaterial();
this.textMesh = new THREE.Mesh(this.textGeo, this.textMaterial);
this.textMesh.position.set(-0.5, 0.5, 0);
this.scene.add(this.textMesh);
},
);
But if want to load the.ttf font, you can just use TTFLoader to load the font and FontLoader.parse()
to parse the json, because it converts.ttf font to json so you need to parse it before you can use it但是如果要加载.ttf字体,你可以只使用TTFLoader加载字体和
FontLoader.parse()
来解析json,因为它将.ttf字体转换为json所以你需要解析它才能使用它
Here is the code:这是代码:
const ttfLoader = new TTFLoader();
const fontLoader = new FontLoader();
ttfLoader.load('src/fonts/Mont-Regular.ttf', (json) => {
const MontFont = fontLoader.parse(json);
this.textGeo = new TextGeometry('Yeah', {
font: MontFont,
// font: Helvfont,
size: 0.2,
height: 0,
// curveSegments: 12,
});
// this.textMaterial = new THREE.MeshPhongMaterial({
// color: 0xff0000,
// side: THREE.DoubleSide,
// });
this.textMaterial = new THREE.MeshNormalMaterial();
this.textMesh = new THREE.Mesh(this.textGeo, this.textMaterial);
this.textMesh.position.set(-0.5, 0.5, 0);
this.scene.add(this.textMesh);
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.