[英]Three JS: I am getting this error Unsupported OpenType signature <!DO
我有一个 three.js 项目,我使用parcel作为模块捆绑器。 我尝试使用三个 js TTFLoader加载字体,但出现此错误。 我在inte.net上查过类似的问题,有人说是跟字体的相对路径有关,我觉得不是。 我不知道问题到底出在哪里
这是项目树:
这是包含错误的代码的一部分:
应用程序.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);
这是codesandbox: https://codesandbox.io/s/three-project-0hpqjr
我怎么解决这个问题? 谢谢
最后我弄清楚了哪里做错了,这次我使用三个FontLoader而不是 TTFLoader,并且我使用根项目目录的绝对路径。 我将此添加到场景代码this.scene.add(this.textMesh)
中的回调 function 中。对于字体,我使用了库提供的字体示例: 'node_modules/three/examples/fonts/helvetiker_regular.typeface.json'
它使用绝对路径
这是代码:
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);
},
);
但是如果要加载.ttf字体,你可以只使用TTFLoader加载字体和FontLoader.parse()
来解析json,因为它将.ttf字体转换为json所以你需要解析它才能使用它
这是代码:
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.