【发布时间】:2016-08-21 06:50:11
【问题描述】:
我正在尝试在 three.js 中加载一种字体,到目前为止这非常困难。希望某人能在这件事上有所帮助。
我尝试使用 Mastering three.js 一书中建议的 javascript 字体。使用有效的书中的版本。我不得不升级到最新版本的three.js,但是出于另一个原因,书中的字体不适用于最新的three.js 版本。所以我尝试通过https://gero3.github.io/facetype.js/将three.js的json字体转换为js-fonts,但没有成功,因为当我按下转换按钮时网站没有做任何事情。
然后,我尝试通过使用 FontLoader 的最新 three.js 版本中的一些示例来定位自己。由于 FontLoader 不返回任何内容,因此我很难获得字体。所以试图将字体分配给我传递给FontLoader的函数中的全局变量myfont。但是,当我执行 loader.load(...) 时,该函数不会被调用,但在我的代码中,仅当我放在这里的 sn-p 的最后一行被执行时。 IE。我事先得到了代码中的错误。 是否有充分的理由说明代码仅在那时执行而不是在我加载字体时执行?
干杯 汤姆
var loader = new THREE.FontLoader();
loader.load( 'three.js-master/examples/fonts/helvetiker_regular.typeface.json', function ( font ) {
init( font );
} );
var myfont;
function init( font ) {
myfont = font;
console.log("inner value "+myfont);
}
console.log("2nd time" +myfont);
var params = {
material: 0,
extrudeMaterial: 1,
bevelEnabled: false,
bevelThickness: 8,
bevelSize: 4,
font: myfont,
weight: "normal",
style: "normal",
height: 0,
size: 11,
curveSegments: 4
};
var textGeo = new THREE.TextGeometry("3D text", params);
console.log("3rd time "+myfont);
textGeo.computeBoundingBox();
console.log("4th time "+myfont);
textGeo.computeVertexNormals();
console.log("5th time "+myfont);
var material = new THREE.MeshFaceMaterial([
new THREE.MeshPhongMaterial({color: 0xff22cc, shading: THREE.FlatShading}), // front
new THREE.MeshPhongMaterial({color: 0xff22cc, shading: THREE.SmoothShading}) // side
]);
console.log("6th time "+myfont);
var textMesh = new THREE.Mesh(textGeo, material);
console.log("7th time "+myfont);
textMesh.position.x = -textGeo.boundingBox.max.x / 2;
textMesh.position.y = -5;
textMesh.position.z = 30;
textMesh.name = 'text';
scene.add(textMesh);
console.log("8th time "+myfont);
camControl = new THREE.OrbitControls(camera, renderer.domElement);
var geometry = new THREE.BoxGeometry( 70, 70, 70 );
var texture = THREE.ImageUtils.loadTexture('wallpaper.jpg');
var mainMaterial = new THREE.MeshBasicMaterial({
map:texture,
side:THREE.DoubleSide
});
console.log("9th time "+myfont);
var nonMainMaterial = new THREE.MeshBasicMaterial( { color: 0xcccccc } );
var materials = [mainMaterial, nonMainMaterial,nonMainMaterial,nonMainMaterial,nonMainMaterial,nonMainMaterial];
var meshFaceMaterial = new THREE.MeshFaceMaterial( materials );
console.log("10th time "+myfont);
var cube = new THREE.Mesh( geometry, meshFaceMaterial );
console.log("11th time "+myfont);
scene.add( cube );
console.log("12th time "+myfont);
var cubeBSP = new ThreeBSP(cube);
console.log("13th time "+myfont);
var textBSP = new ThreeBSP(textMesh);
console.log("14th time "+myfont);
var resultBSP = cubeBSP.subtract(textMesh);
【问题讨论】:
标签: three.js