我用three.js做了3d文字,祝群里的大佬们中秋节快乐

言程序plus 荣耀黄金 2022-08-31 10:01:56


 篇幅较多,部分核心代码展示:
麻烦之处在于中文字体引入及优化

			let text ='中秋节快乐',

				bevelEnabled = true,

				font = undefined,

				fontName = 'optimer', // helvetiker, optimer, gentilis, droid sans, droid serif
				fontWeight = 'bold'; // normal bold

			const height = 20,
				size = 50,
				hover = 30,

				curveSegments = 4,

				bevelThickness = 2,
				bevelSize = 1.5;

			const mirror = true;

			const fontMap = {

				'helvetiker': 0,
				'optimer': 1,
				'gentilis': 2,
				'droid/droid_sans': 3,
				'droid/droid_serif': 4

			};

function loadFont() {

	const loader = new FontLoader();

	loader.load( './asset/fonts/ttf/STXihei_Regular.json', function ( response ) {
					font = response;
					refreshText();
				} );

			}
function refreshText() {

				updatePermalink();

				group.remove( textMesh1 );
				if ( mirror ) group.remove( textMesh2 );

				if ( ! text ) return;

				createText();

			}

function createText() {

				textGeo = new TextGeometry( text, {

					font: font,

					size: size,
					height: height,
					curveSegments: curveSegments,

					bevelThickness: bevelThickness,
					bevelSize: bevelSize,
					bevelEnabled: bevelEnabled

				} );

				textGeo.computeBoundingBox();

				const centerOffset = - 0.5 * ( textGeo.boundingBox.max.x - textGeo.boundingBox.min.x );

				textMesh1 = new THREE.Mesh( textGeo, materials );

				textMesh1.position.x = centerOffset;
				textMesh1.position.y = hover;
				textMesh1.position.z = 0;

				textMesh1.rotation.x = 0;
				textMesh1.rotation.y = Math.PI * 2;

				group.add( textMesh1 );

				if ( mirror ) {

					textMesh2 = new THREE.Mesh( textGeo, materials );

					textMesh2.position.x = centerOffset;
					textMesh2.position.y = - hover;
					textMesh2.position.z = height;

					textMesh2.rotation.x = Math.PI;
					textMesh2.rotation.y = Math.PI * 2;

					group.add( textMesh2 );

				}

			}

 

...全文
323 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
月谷丶 倔强青铜 2022-09-01
  • 打赏
  • 举报
回复

支持一下太棒了

17,518

社区成员

发帖
与我相关
我的任务
社区描述
前端技术交流和学习资源共享中心
vue.js前端微信小程序 个人社区
社区管理员
  • 言程序plus
  • 小   明
  • 叶落秋白
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

web全栈技术交流中心

试试用AI创作助手写篇文章吧