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>
相关推荐
三年三月12 小时前
three中相机
three.js
大佛拈花13 小时前
Godot学习-3D基本环境设置以及3D角色移动
学习·3d·godot
好脾气先生13 小时前
[论文解析]Mip-Splatting: Alias-free 3D Gaussian Splatting
人工智能·计算机视觉·3d·三维重建
sunbyte16 小时前
Three.js + React 实战系列-3D 个人主页:构建 Hero 场景组件(项目核心)✨
3d
小桥风满袖17 小时前
Three.js-硬要自学系列16 (贝塞尔曲线应用、组合曲线、路径管道、旋转成型、轮廓填充)
前端·css·three.js
爱看书的小沐18 小时前
【小沐学Web3D】three.js 加载三维模型(Svelte.js)
javascript·vue.js·webgl·three.js·opengl·web3d·svelte.js
广州华锐视点18 小时前
广州 3D 展厅开启企业展示新时代
3d·3d展厅
在下胡三汉1 天前
来自 3D 世界的 JPEG。什么是 glTF?什么是 glb?
3d
Mapmost2 天前
【数据可视化艺术·应用篇】三维管线分析如何重构城市“生命线“管理?
3d·信息可视化·性能优化·demo