gpt4 book ai didi

javascript - 3D 文本未在 three.js 场景中呈现

转载 作者:行者123 更新时间:2023-11-30 15:13:31 25 4
gpt4 key购买 nike

文本“Hello three.js!”不在我的场景中渲染。当我使用 BoxGeometry 时它工作正常,但我似乎缺少 TextBufferGeometry 的一些东西。

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); // fov, aspect, near and far
camera.position.set(-15, 0, 25);
camera.lookAt( scene.position );

var loader = new THREE.FontLoader();

loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {

var material = new THREE.MeshPhongMaterial( { color: 0x0033ff, specular: 0x555555, shininess: 30 } );

var textGeo = new THREE.TextGeometry( 'Hello three.js!', {
font: font,
size: 80,
height: 5,
curveSegments: 12,
bevelEnabled: true,
material: 0,
extrudeMaterial: 1
} );

textGeo.computeBoundingBox();
textGeo.computeVertexNormals();

var mesh = new THREE.Mesh( textGeo, material );

scene.add(mesh);

} );

var light = new THREE.DirectionalLight( 0xffffff );
light.position.set( 0, 1, 1 ).normalize();
scene.add(light);


var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

renderer.render( scene, camera );

我无法理解 TextGeometry 的 three.js 文档 https://threejs.org/docs/#api/geometries/TextGeometry

enter image description here

最佳答案

我需要将渲染代码移到字体加载器回调中。

var scene = new THREE.Scene();
var camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000 ); // fov, aspect, near and far
camera.position.set(-15, 0, 25);
camera.lookAt( scene.position );

var loader = new THREE.FontLoader();

loader.load( 'fonts/helvetiker_regular.typeface.json', function ( font ) {

var material = new THREE.MeshPhongMaterial( { color: 0x0033ff, specular: 0x555555, shininess: 30 } );

var geometry = new THREE.TextGeometry( 'Hello three.js!', {
font: font,
size: 80,
height: 5,
curveSegments: 12,
bevelEnabled: true,
bevelThickness: 10,
bevelSize: 8,
bevelSegments: 5
} );

var mesh = new THREE.Mesh( geometry, material );

scene.add(mesh);

var light = new THREE.DirectionalLight( 0xffffff );
light.position.set( 0, 1, 1 ).normalize();
scene.add(light);


var renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );

renderer.render( scene, camera );

} );

关于javascript - 3D 文本未在 three.js 场景中呈现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44795118/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com