当前位置: 首页 > article >正文

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

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

<!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>


http://www.kler.cn/a/524555.html

相关文章:

  • 2007-2020年各省国内专利申请授权量数据
  • Mybatis是如何进行分页的?
  • 小白爬虫冒险之反“反爬”:无限debugger、禁用开发者工具、干扰控制台...(持续更新)
  • FreeRTOS学习 --- 动态任务创建和删除的详细过程
  • Greenplum临时表未清除导致库龄过高处理
  • 从替代到覆盖:暴雨信创服务器打开市场新局面
  • 【Proteus仿真】【51单片机】简易计算器系统设计
  • python-leetcode-从中序与后序遍历序列构造二叉树
  • 【Spark速通】
  • MV结构下设置Qt表格的代理
  • EXCEL教程:如何打开Excel隐藏部分?
  • JavaScript - Web APIs(上)
  • 基于Arcsoft的人脸识别
  • doris:异常数据处理
  • DeepSeek部署教程(基于Ollama)
  • 具身智能研究报告
  • 展示统计信息收集情况
  • redis缓存和springboot缓存包冲突怎么办
  • 再见了流氓软件~~
  • 什么是AGI
  • PyTorch中的movedim、transpose与permute
  • [特殊字符] x-cmd pkg | fzf (1) - 强大的模糊搜索工具,一条命令颠覆你的命令行交互体验
  • Autogen_core 测试代码:test_cache_store.py
  • 003 mapper代理开发方式-注解方式
  • 64位的谷歌浏览器Chrome/Google Chrome
  • Maven项目JUnit测试的远程调试技巧