qidao123.com ToB IT社区-企服评测·应用市场

标题: three.js用粒子利用canvas生成的中笔墨符位图材质 [打印本页]

作者: 王海鱼    时间: 2025-1-26 10:25
标题: three.js用粒子利用canvas生成的中笔墨符位图材质


three.js用粒子利用canvas生成中笔墨符材质

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4.     <meta charset="UTF-8">
  5.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6.     <title>Three.js Canvas 文字粒子</title>
  7.     <style>
  8.         body { margin: 0; }
  9.         canvas { display: block; }
  10.     </style>
  11. </head>
  12. <body>
  13.      <script type="importmap">
  14.                 {
  15.                   "imports": {
  16.                         "three": "https://cdn.jsdelivr.net/npm/three@0.170.0/build/three.module.js",
  17.                         "three/addons/": "https://cdn.jsdelivr.net/npm/three@0.170.0/examples/jsm/",
  18.                         "three/addons/geometries/TextGeometry.js": "https://cdn.jsdelivr.net/npm/three@0.170.0/examples/jsm/geometries/TextGeometry.js",
  19.                         "three/addons/loaders/FontLoader.js": "https://cdn.jsdelivr.net/npm/three@0.170.0/examples/jsm/loaders/FontLoader.js"
  20.                   }
  21.                 }
  22.         </script>
  23.     <script type="module">
  24.        
  25.                 import * as THREE from 'three';
  26.                
  27.                 //--------------------------------------
  28.                 //需要显示的文字
  29.         let textDisplayed = "新年好";
  30.                        
  31.         // 创建场景、相机和渲染器
  32.         const scene = new THREE.Scene();
  33.         const camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);
  34.         const renderer = new THREE.WebGLRenderer();
  35.         renderer.setSize(window.innerWidth, window.innerHeight);
  36.         document.body.appendChild(renderer.domElement);
  37.                
  38.         // 设置相机位置
  39.         camera.position.z = 50;
  40.   
  41.         // 创建 Canvas 纹理
  42.         function createCanvasTexture(text) {
  43.             const canvas = document.createElement('canvas');
  44.             const context = canvas.getContext('2d');
  45.             const size = 300; // Canvas 大小
  46.             canvas.width = size;
  47.             canvas.height = size;
  48.             // 绘制背景
  49.             context.fillStyle = 'rgba(0, 0, 0, 0)'; // 透明背景
  50.             context.fillRect(0, 0, size, size);
  51.             // 绘制文字
  52.             context.font = '100px Arial';
  53.             context.fillStyle = 'gold';
  54.             context.textAlign = 'center';
  55.             context.textBaseline = 'middle';
  56.             context.fillText(text, size / 2, size / 2);
  57.             // 将 Canvas 转换为纹理
  58.             return new THREE.CanvasTexture(canvas);
  59.         }
  60.         // 创建粒子系统
  61.         const particles = [];
  62.         const particleCount = 100;
  63.         const geometry = new THREE.BufferGeometry();
  64.         const positions = [];
  65.         for (let i = 0; i < particleCount; i++) {
  66.             // 随机位置
  67.             positions.push((Math.random() - 0.5) * 100, (Math.random() - 0.5) * 100, (Math.random() - 0.5) * 100);
  68.         }
  69.         geometry.setAttribute('position', new THREE.Float32BufferAttribute(positions, 3));
  70.         // 创建粒子材质
  71.         const texture = createCanvasTexture(textDisplayed);
  72.         const material = new THREE.PointsMaterial({
  73.             size: 10,
  74.             map: texture,
  75.             transparent: true,
  76.             alphaTest: 0.5
  77.         });
  78.         const particleSystem = new THREE.Points(geometry, material);
  79.         scene.add(particleSystem);
  80.         // 动画循环
  81.                
  82.                 let angle = 0;
  83.                 const radius = 80; // 相机绕场景中心旋转的半径
  84.                
  85.                 renderer.setClearColor(0xff0000, 0.5);
  86.         function animate() {
  87.             requestAnimationFrame(animate);
  88.                         angle += 0.01;
  89.                         camera.position.x = radius * Math.cos(angle);
  90.                         camera.position.z = radius * Math.sin(angle);
  91.                         camera.lookAt(scene.position);
  92.                        
  93.                        
  94.             renderer.render(scene, camera);
  95.         }
  96.         animate();
  97.     </script>
  98. </body>
  99. </html>
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。




欢迎光临 qidao123.com ToB IT社区-企服评测·应用市场 (https://dis.qidao123.com/) Powered by Discuz! X3.5