Three.js 着色器光效教程

发布时间:2026/10/15 1:09:31
Three.js 着色器光效教程 着色器光效 ·Shader Light· ▶ 在线运行案例案例合集三维可视化功能案例threehub.cn开源仓库github地址https://github.com/z2586300277/three-cesium-examples400个案例代码:网盘链接你将学到什么ShaderMaterial 自定义着色器实现核心视觉效果OrbitControls 相机轨道交互Canvas 动态纹理贴图requestAnimationFrame渲染循环与resize自适应效果说明本案例演示着色器光效效果用 Canvas 2D 绘制内容并实时映射为 Three.js 纹理核心用到 ShaderMaterial、OrbitControls、Canvas。建议先打开文首在线案例查看动态画面再对照下方源码逐步理解。核心概念Scene / Camera / WebGLRenderer构成最小渲染闭环大场景可开logarithmicDepthBuffer缓解 Z-fighting。ShaderMaterial通过uniforms 自定义 GLSL 控制逐像素/逐点效果透明粒子常配合depthTest: false。OrbitControls提供轨道旋转/缩放开启enableDamping后需在 animate 中controls.update()。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/addons/controls/OrbitControls.jslet scene, camera, renderer, controls let fakeLights [] const clock new THREE.Clock()init() animate()function init() { scene new THREE.Scene() scene.background new THREE.Color(#0a0a1a)camera new THREE.PerspectiveCamera( 60, window.innerWidth / window.innerHeight, 0.1, 1000 ) camera.position.set(0, 15, 30)renderer new THREE.WebGLRenderer({ antialias: true, preserveDrawingBuffer: true }) renderer.setSize(window.innerWidth, window.innerHeight) renderer.setPixelRatio(window.devicePixelRatio) document.body.appendChild(renderer.domElement)controls new OrbitControls(camera, renderer.domElement) controls.enableDamping true createFakeLights() }function createGlowTexture(color) { const canvas document.createElement(canvas) canvas.width 256 canvas.height 256 const ctx canvas.getContext(2d)const gradient ctx.createRadialGradient(128, 128, 0, 128, 128, 128) gradient.addColorStop(0, color) gradient.addColorStop(0.3, color.replace(1), 0.6))) gradient.addColorStop(0.6, color.replace(1), 0.2))) gradient.addColorStop(1, color.replace(1), 0)))ctx.fillStyle gradient ctx.fillRect(0, 0, 256, 256)const texture new THREE.CanvasTexture(canvas) texture.needsUpdate true return texture }function createFakeLight(position, color, size) { const group new THREE.Group()const glowTexture createGlowTexture(color)const coreMaterial new THREE.ShaderMaterial({ uniforms: { uTexture: { value: glowTexture }, uOpacity: { value: 1.0 }, uTime: { value: 0 } }, vertexShader:varying vec2 vUv; void main() { vUv uv; gl_Position projectionMatrixmodelViewMatrixvec4(position, 1.0); }, fragmentShader:uniform sampler2D uTexture; uniform float uOpacity; uniform float uTime; varying vec2 vUv;void main() { vec4 texColor texture2D(uTexture, vUv); float pulse 0.8 0.2sin(uTime2.0); gl_FragColor vec4(texColor.rgb, texColor.auOpacitypulse); }, transparent: true, blending: THREE.AdditiveBlending, depthWrite: false, side: THREE.DoubleSide })const coreGeometry new THREE.PlaneGeometry(size, size) const core new THREE.Mesh(coreGeometry, coreMaterial) core.position.copy(position) core.lookAt(camera.position) group.add(core)for (let i 1; i 3; i) { const layerMaterial coreMaterial.clone() layerMaterial.uniforms.uOpacity.value 0.3 / i const layerGeometry new THREE.PlaneGeometry(size(1 i0.5), size(1 i0.5)) const layer new THREE.Mesh(layerGeometry, layerMaterial) layer.position.copy(position) layer.position.y - i * 0.1 layer.lookAt(camera.position) group.add(layer) }return { group, core, material: coreMaterial } }function createFakeLights() { const lightConfigs [ { pos: new THREE.Vector3(-10, 5, -10), color: rgba(255, 100, 100, 1), size: 8 }, { pos: new THREE.Vector3(10, 5, -10), color: rgba(100, 255, 100, 1), size: 8 }, { pos: new THREE.Vector3(0, 8, 10), color: rgba(100, 100, 255, 1), size: 10 }, { pos: new THREE.Vector3(-15, 3, 5), color: rgba(255, 200, 100, 1), size: 6 }, { pos: new THREE.Vector3(15, 3, 5), color: rgba(255, 100, 200, 1), size: 6 } ]lightConfigs.forEach(config { const fakeLight createFakeLight(config.pos, config.color, config.size) scene.add(fakeLight.group) fakeLights.push(fakeLight) }) }function animate() { requestAnimationFrame(animate)const elapsed clock.getElapsedTime()fakeLights.forEach(fakeLight { fakeLight.material.uniforms.uTime.value elapsed fakeLight.core.lookAt(camera.position) })controls.update() renderer.render(scene, camera) }完整源码GitHub小结本文提供着色器光效完整 Three.js 源码与在线 Demo建议先运行案例再改 uniform/参数做二次实验更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库

关于本文作者

来自尧图内容编辑团队

尧图内容编辑团队 内容团队

尧图内容编辑团队

本文由尧图网络内容编辑团队执笔。团队由资深项目经理、前端工程师与设计师组成,所有内容均来自亲手交付的真实项目,先讲清问题、再给出可落地的解法。尧图深耕北京网站建设十年,服务过京华建材集团、智造科技等各行业客户,把一线经验沉淀为可复用的行业观察。

  • 十年建站经验,覆盖建材、制造、服务、文创等
  • 项目经理把关选题与事实准确性
  • 工程师与设计师联合撰写专业细节
  • 统一编辑规范,保证文风与排版一致
  • 每月复盘转化数据,迭代选题方向

延伸阅读

相关资讯与近期热门内容

深度阅读推荐

建站决策前值得细读的三篇

网站改版的5个关键决策
2024-08-12

网站改版的5个关键决策

什么时候该改版、改到什么程度、如何避免流量掉光,京华建材集团改版复盘给出答案。

获取专属建站方案

看完文章,把您的行业与预算告诉我们,免费获取一份量身定制的官网建设方案与报价。

立即免费咨询