Three.js 图片粒子教程

📅 2026/7/5 8:37:21
Three.js 图片粒子教程
图片粒子 ·Image Particle· ▶ 在线运行案例案例合集三维可视化功能案例threehub.cn开源仓库github地址https://github.com/z2586300277/three-cesium-examples400个案例代码:网盘链接你将学到什么ShaderMaterial 自定义着色器实现核心视觉效果OrbitControls 相机轨道交互THREE.Points 粒子点渲染Canvas 动态纹理贴图BufferGeometry 自定义顶点/索引数据requestAnimationFrame渲染循环与resize自适应效果说明本案例演示图片粒子效果用 Canvas 2D 绘制内容并实时映射为 Three.js 纹理核心用到 ShaderMaterial、OrbitControls、THREE.Points。建议先打开文首在线案例查看动态画面再对照下方源码逐步理解。核心概念Scene / Camera / WebGLRenderer构成最小渲染闭环大场景可开logarithmicDepthBuffer缓解 Z-fighting。ShaderMaterial通过uniforms 自定义 GLSL 控制逐像素/逐点效果透明粒子常配合depthTest: false。OrbitControls提供轨道旋转/缩放开启enableDamping后需在 animate 中controls.update()。THREE.Points将每个顶点渲染为可控大小的粒子可用自定义 attribute如u_index驱动片元/顶点动画。CanvasTexture每帧或按需把 2D Canvas 内容上传 GPU适合动态文字、图表、视频帧贴图。实现步骤搭建 Scene、PerspectiveCamera、WebGLRenderer挂载 canvas 并处理resize定义 uniforms / onBeforeCompile 或 ShaderMaterial编写 GLSL 与材质参数创建 OrbitControls及 Raycaster 等交互控件若源码包含在requestAnimationFrame循环中更新状态并 renderCesium 为viewer.render或自动渲染代码要点import * as THREE from threeimport { OrbitControls } from three/examples/jsm/controls/OrbitControls.jsconst box document.getElementById(box)const scene new THREE.Scene()const camera new THREE.PerspectiveCamera(50, box.clientWidth / box.clientHeight, 0.1, 1000)camera.position.set(0, 0, 10)const renderer new THREE.WebGLRenderer({ antialias: true, alpha: true, logarithmicDepthBuffer: true })renderer.setSize(box.clientWidth, box.clientHeight)box.appendChild(renderer.domElement)const controls new OrbitControls(camera, renderer.domElement)controls.enableDamping trueanimate()function animate() {requestAnimationFrame(animate)controls.update()renderer.render(scene, camera)}window.onresize () {renderer.setSize(box.clientWidth, box.clientHeight)camera.aspect box.clientWidth / box.clientHeightcamera.updateProjectionMatrix()}// 粒子系统配置 const config { imageUrl: HOST files/author/z2586300277.png, targetSize: 2, // 缩放目标大小 depth: 0.3, // 深度范围 pointSize: 0.001, // 粒子基础大小 sizeScale: 0.5, // 粒子大小缩放系数 color: 0xff0000, // 自定义颜色 useCustomColor: false, intensity: 1.1, particleGap: 6, // 粒子间隔(1-10, 值越大粒子越少) particleOpacity: 0.8 // 粒子透明度 };createParticles(config, particles { particles.position.set(-1.5, 1.5, 0); scene.add(particles); });createParticles({ ...config, imageUrl: HOST files/author/FFMMCC.jpg, }, particles { particles.position.set(1.5, 1.5, 0); scene.add(particles); });createParticles({ ...config, imageUrl: HOST files/author/flowers-10.jpg, }, particles { particles.position.set(-1.5, -1.5, 0); scene.add(particles); });createParticles({ ...config, imageUrl: HOST files/author/KallkaGo.jpg, }, particles { particles.position.set(1.5, -1.5, 0); scene.add(particles); });function createParticles(config, callback) { new THREE.TextureLoader().load(config.imageUrl, texture { const { width: w, height: h } texture.image; const scale w h ? config.targetSize/w : config.targetSize/h; // 获取像素数据 const canvas document.createElement(canvas); const ctx canvas.getContext(2d); if (!ctx) return; [canvas.width, canvas.height] [w, h]; ctx.drawImage(texture.image, 0, 0); const data ctx.getImageData(0, 0, w, h).data;// 收集顶点和颜色数据按间隔采样以控制粒子数量 const [positions, colors] [[], []]; for(let i 0; i data.length; i 4 * config.particleGap) { if(data[i 3] 0) { const x (i/4 % w - w/2) * scale; const y -(Math.floor(i/4/w) - h/2) * scale; positions.push(x, y, Math.random() * config.depth); colors.push(data[i]/255, data[i1]/255, data[i2]/255); } }// 创建几何体和材质 const geometry new THREE.BufferGeometry() .setAttribute(position, new THREE.Float32BufferAttribute(positions, 3)) .setAttribute(color_list, new THREE.Float32BufferAttribute(colors, 3));callback(new THREE.Points(geometry, new THREE.ShaderMaterial({ uniforms: { zPos: { value: 1 }, useCustomColor: { value: config.useCustomColor }, customColor: { value: new THREE.Color() }, opacity: { value: config.particleOpacity }, intensity: { value: config.intensity } }, vertexShader:attribute vec3 color_list; varying vec3 vColor; uniform float zPos; void main() { vColor color_list; vec4 mvPosition modelViewMatrixvec4(position.xy, position.zzPos, 1.0); gl_PointSize ${config.pointSizeconfig.sizeScale}(1.0 - mvPosition.z); gl_Position projectionMatrix * mvPosition; }, fragmentShader:varying vec3 vColor; uniform bool useCustomColor; uniform vec3 customColor; uniform float opacity; void main() { vec3 color useCustomColor ? customColor : vColor; gl_FragColor vec4(color * vec3(${config.intensity}), opacity); }, transparent: true }))) }) }完整源码GitHub小结本文提供图片粒子完整 Three.js 源码与在线 Demo建议先运行案例再改 uniform/参数做二次实验更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库