three.js用粒子使用canvas生成的中文字符位图材质

three.js用粒子使用canvas生成中文字符材质

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Three.js Canvas 文字粒子</title>
    <style>
        body { margin: 0; }
        canvas { display: block; }
    </style>
</head>
<body>
     <script type="importmap">
		{
		  "imports": {
			"three": "https://cdn.jsdelivr.net/npm/[email protected]/build/three.module.js",
			"three/addons/": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/",
			"three/addons/geometries/TextGeometry.js": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/geometries/TextGeometry.js",
			"three/addons/loaders/FontLoader.js": "https://cdn.jsdelivr.net/npm/[email protected]/examples/jsm/loaders/FontLoader.js"
		  }
		}
	</script>
    <script type="module">
	
		import * as THREE from 'three';
		
		//--------------------------------------
		//需要显示的文字
        let textDisplayed = "新年好";
			
        // 创建场景、相机和渲染器
        const scene = new THREE.Scene();
        const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
        const renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        document.body.appendChild(renderer.domElement);
		
        // 设置相机位置
        camera.position.z = 50;

  

        // 创建 Canvas 纹理
        function createCanvasTexture(text) {
            const canvas = document.createElement('canvas');
            const context = canvas.getContext('2d');
            const size = 300; // Canvas 大小

            canvas.width = size;
            canvas.height = size;

            // 绘制背景
            context.fillStyle = 'rgba(0, 0, 0, 0)'; // 透明背景
            context.fillRect(0, 0, size, size);

            // 绘制文字
            context.font = '100px Arial';
            context.fillStyle = 'gold';
            context.textAlign = 'center';
            context.textBaseline = 'middle';
            context.fillText(text, size / 2, size / 2);

            // 将 Canvas 转换为纹理
            return new THREE.CanvasTexture(canvas);
        }

        // 创建粒子系统
        const particles = [];
        const particleCount = 100;
        const geometry = new THREE.BufferGeometry();
        const positions = [];

        for (let i = 0; i < particleCount; i++) {
            // 随机位置
            positions.push((Math.random() - 0.5) * 100, (Math.random() - 0.5) * 100, (Math.random() - 0.5) * 100);
        }

        geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));

        // 创建粒子材质
        const texture = createCanvasTexture(textDisplayed);

        const material = new THREE.PointsMaterial({
            size: 10,
            map: texture, 
            transparent: true,
            alphaTest: 0.5
        });

        const particleSystem = new THREE.Points(geometry, material);
        scene.add(particleSystem);

        // 动画循环
		
		let angle = 0;
		const radius = 80; // 相机绕场景中心旋转的半径
		
		renderer.setClearColor(0xff0000, 0.5); 

        function animate() {
            requestAnimationFrame(animate);

			angle += 0.01;
			camera.position.x = radius * Math.cos(angle);
			camera.position.z = radius * Math.sin(angle);
			camera.lookAt(scene.position);
			
			
            renderer.render(scene, camera);
        }
        animate();
    </script>
</body>
</html>
相关推荐
广州智造13 小时前
OptiStruct实例:3D实体转子分析
数据库·人工智能·算法·机器学习·数学建模·3d·性能优化
码农黛兮_4613 小时前
4. 文字效果/2D-3D转换 - 3D翻转卡片
3d·html·css3
爱学习的大牛1232 天前
u3d 定义列表详细过程
3d
QUST-Learn3D2 天前
3D曲面上的TSP问题(一):曲面上点集距离求解
3d
龙湾开发2 天前
计算机图形学编程(使用OpenGL和C++)(第2版)学习笔记 10.增强表面细节(一)过程式凹凸贴图
c++·笔记·学习·3d·图形渲染
未来之窗软件服务2 天前
人体肢体工作识别-一步几个脚印从头设计数字生命——仙盟创梦IDE
3d·pygame·政务·仙盟创梦ide·人体识别
zhongqu_3dnest2 天前
3DVR制作的工具或平台
3d·vr·数字孪生技术·vr制作·沉浸式体验
三天不学习2 天前
一文讲透 Vue3 + Three.js 材质属性之皮革篇【扫盲篇】
javascript·webgl·three.js·材质
WenGyyyL3 天前
研读论文——《用于3D工业异常检测的自监督特征自适应》
人工智能·python·深度学习·机器学习·计算机视觉·3d
belldeep3 天前
python:trimesh 用于 STL 文件解析和 3D 操作
python·3d·stl