three.js 加载显示文字

three.js 加载显示文字

代码放到 ./three.js/examples/ 下

<!DOCTYPE html>
<html lang="en">

<head>
	<title>three.js webgl - materials - video - webcam</title>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
	<link type="text/css" rel="stylesheet" href="main.css">
</head>

<body>

	<div id="info">
		<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - video webcam input
	</div>

	<video id="video" style="display:none" autoplay playsinline></video>

	<script type="module">
		import * as THREE from '../build/three.module.js';
		import { OrbitControls } from './jsm/controls/OrbitControls.js';
		import Stats from './jsm/libs/stats.module.js';
		let camera, scene, renderer, font, textGeo, textMesh1, materials;

		init();
		animate();
		// 加载文字
		function loadFont() {
			const loader = new THREE.FontLoader();
			loader.load('./fonts/helvetiker_bold.typeface.json', function (response) {
				font = response;
				textGeo = new THREE.TextGeometry(
					"three.js",  // 文字内容
					{
						font: font,
						size: 70, // 文字大小
						height: 20, // 文字厚度
						curveSegments: 3,  // 文字平滑度
						bevelThickness: 2, // 文字斜面厚度
						bevelSize: 1.5,  // 文字斜面的大小
						bevelEnabled: true // 文字阴影
					});
				textGeo.computeBoundingBox();
				materials = [
					new THREE.MeshPhongMaterial({ color: 0xffcc33, flatShading: true }), // front
					new THREE.MeshPhongMaterial({ color: 0xffcc33 }) // side
				];
				textMesh1 = new THREE.Mesh(textGeo, materials);
				const centerOffset = - 0.5 * (textGeo.boundingBox.max.x - textGeo.boundingBox.min.x);
				textMesh1.position.x = centerOffset;
				textMesh1.position.y = 0;
				textMesh1.position.z = 0;
				textMesh1.rotation.x = 0;
				textMesh1.rotation.y = Math.PI * 2;
				scene.add(textMesh1);
			});
		}
		function init() {
			camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 1500);
			// camera.position.z = 0.01;
			camera.position.set( // 眼睛的位置
				0,   // +:右,-:左
				0,   // +:上,-:下
				500  // +:近,-:远
			);
			camera.lookAt(new THREE.Vector3(  // 眼睛看的点的位置
				0, // +:右,-:左
				0, // +:上,-:下
				0  // +:近,-:远
			));
			scene = new THREE.Scene();
			// 光源
			const dirLight = new THREE.DirectionalLight(0xffffff, 0.125);
			dirLight.position.set(0, 0, 1).normalize();
			scene.add(dirLight);
			const pointLight = new THREE.PointLight(0xffffff, 1.5);
			pointLight.position.set(0, 100, 90);
			scene.add(pointLight);
			loadFont()
			renderer = new THREE.WebGLRenderer({ antialias: true });
			renderer.setPixelRatio(window.devicePixelRatio);
			renderer.setSize(window.innerWidth, window.innerHeight);
			document.body.appendChild(renderer.domElement);
			const controls = new OrbitControls(camera, renderer.domElement);
			controls.enableZoom = false;
			controls.enablePan = false;
			window.addEventListener('resize', onWindowResize);
		}
		function onWindowResize() {
			camera.aspect = window.innerWidth / window.innerHeight;
			camera.updateProjectionMatrix();
			renderer.setSize(window.innerWidth, window.innerHeight);
		}
		function animate() {
			requestAnimationFrame(animate);
			renderer.clear();
			renderer.render(scene, camera);
		}

	</script>

</body>

</html>
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

夏华东的博客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值