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/three@0.170.0/build/three.module.js",
			"three/addons/": "https://cdn.jsdelivr.net/npm/three@0.170.0/examples/jsm/",
			"three/addons/geometries/TextGeometry.js": "https://cdn.jsdelivr.net/npm/three@0.170.0/examples/jsm/geometries/TextGeometry.js",
			"three/addons/loaders/FontLoader.js": "https://cdn.jsdelivr.net/npm/three@0.170.0/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>
相关推荐
Candy 91714 小时前
内盒拼箱计算与3D可视化
3d·内盒拼箱计算与3d可视化
jiayong2314 小时前
数字孪生 vs 虚拟仿真 vs 3D建模-技术选型指南
3d·数字孪生·虚拟仿真
calvinpaean15 小时前
Metric3D Towards Zero-shot Metric 3D Prediction from A Single Image 论文学习
学习·3d
CG_MAGIC17 小时前
Maya 关键帧动画基础:角色走路循环与姿态调整
3d·maya·效果图·建模教程·渲云渲染·渲云
云飞云共享云桌面18 小时前
SolidWorks如何实现多人共享
服务器·前端·数据库·人工智能·3d
Nan_Shu_6142 天前
学习:threejs案例—大屏3D地图可视化
前端·javascript·学习·three.js
HZjiangzi2 天前
天然气管道焊缝三维无损检测技术与思看科技TrackScan-Sharp解决方案
科技·3d
HZjiangzi2 天前
手机外壳平面度与翘曲度光学检测:思看科技SIMSCAN-E精细模式解决方案
人工智能·科技·3d·制造
HZjiangzi2 天前
盾构机刀盘磨损三维测量技术与思看科技SIMSCAN解决方案
人工智能·科技·3d
HyperAI超神经2 天前
在线教程丨微软开源3D生成模型TRELLIS.2,3秒生成高分辨率的全纹理资产
人工智能·深度学习·机器学习·3d