news 2026/7/18 17:00:45

Three.js 灯罩教程

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Three.js 灯罩教程

灯罩 ·Lampshade· ▶ 在线运行案例

  • 案例合集:三维可视化功能案例(threehub.cn)
  • 开源仓库github地址:https://github.com/z2586300277/three-cesium-examples
  • 400个案例代码:网盘链接

你将学到什么

  • ShaderMaterial 自定义着色器实现核心视觉效果
  • OrbitControls 相机轨道交互
  • requestAnimationFrame渲染循环与resize自适应

效果说明

本案例演示灯罩效果:基于 WebGL 实现「灯罩」可视化效果,附完整可运行源码;核心用到 ShaderMaterial、OrbitControls。建议先打开文首在线案例查看动态画面,再对照下方源码逐步理解。

核心概念

  • Scene / Camera / WebGLRenderer构成最小渲染闭环;大场景可开logarithmicDepthBuffer缓解 Z-fighting。
  • ShaderMaterial通过uniforms+ 自定义 GLSL 控制逐像素/逐点效果;透明粒子常配合depthTest: false
  • OrbitControls提供轨道旋转/缩放;开启enableDamping后需在 animate 中controls.update()

实现步骤

  • 创建 OrbitControls 并处理 resize
  • 定义 uniforms,在 rAF 中更新并 render
  • 搭建灯光与环境(如有)
  • requestAnimationFrame 循环 update + render
  • 代码要点

    import {

    Color, CylinderGeometry, Group, Mesh, MeshBasicMaterial, MeshStandardMaterial, PerspectiveCamera, PlaneGeometry, PointLight, Scene, ShaderMaterial, SphereGeometry, WebGLRenderer } from 'three' import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'

    const size = { width: window.innerWidth, height: window.innerHeight } const scene = new Scene() scene.background = new Color('#070630')

    const camera = new PerspectiveCamera(45, size.width / size.height, 0.1, 1000) camera.position.set(30, 30, 30)

    const renderer = new WebGLRenderer({ antialias: true }) renderer.setSize(size.width, size.height) renderer.setPixelRatio(window.devicePixelRatio) document.body.appendChild(renderer.domElement)

    const controls = new OrbitControls(camera, renderer.domElement)

    class DeskLamp extends Group { constructor() { super() this.#createWick() this.#createLampshade() }

    /*灯芯/ #createWick() { const geometry = new SphereGeometry(1, 32, 16, 0, Math.PI * 2, 0, Math.PI / 2) const material = new MeshBasicMaterial({ color: 0xffffff }) const sphere = new Mesh(geometry, material) sphere.position.set(0, 3, 0) this.#createLight(sphere) this.add(sphere) }

    /*灯罩/ #createLampshade() { const cylinderGeometry = new CylinderGeometry(1, 5, 3, 32) const cylinderMaterial = new ShaderMaterial({ transparent: true, uniforms: { color: { value: new Color('#CB00E3') } }, vertexShader:varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrixmodelViewMatrixvec4(position, 1.0); }, fragmentShader:uniform vec3 color; varying vec2 vUv; void main() { gl_FragColor = vec4(color, vUv.y); }}) const cylinder = new Mesh(cylinderGeometry, cylinderMaterial) cylinder.position.set(0, 1.7, 0) this.add(cylinder) }

    /*点光源/ #createLight(mesh) { const pointLight = new PointLight(0xffffff, 1, 100) pointLight.power = 1000 mesh.add(pointLight) } }

    const light1 = new DeskLamp() light1.position.set(0, 10, 0) scene.add(light1)

    const light2 = new DeskLamp() light2.position.set(10, 10, 0) scene.add(light2)

    const light3 = new DeskLamp() light3.position.set(0, 10, 10) scene.add(light3)

    /**

    • 创建地面
    / const planeGeometry = new PlaneGeometry(100, 100) const planeMaterial = new MeshStandardMaterial({ color: 0x999999, side: 2 }) const plane = new Mesh(planeGeometry, planeMaterial) plane.rotation.x = -Math.PI / 2 scene.add(plane)

    animate() function animate() {

    renderer.render(scene, camera) requestAnimationFrame(animate) }

    完整源码:GitHub

    小结

    • 本文提供灯罩完整 Three.js 源码与在线 Demo,建议先运行案例再改 uniform/参数做二次实验
    • 更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/18 16:59:40

计算机仿真计算工作站选型全指南

2026年跑ANSYS/Fluent仿真要什么硬件配置?仿真计算工作站选型全指南 仿真计算的硬件配置没有"唯一解"。CPU主频决定隐式求解速度,核心数和内存通道决定CFD/显式动力学并行效率,GPU加速则需要软件支持且精度匹配。本文基于ANSYS、Ab…

作者头像 李华
网站建设 2026/7/18 16:59:17

Docker容器化部署实战:从原理到生产环境最佳实践

1. 项目概述:从“部署地狱”到“一键交付”如果你和我一样,在运维或者开发岗位上摸爬滚打了好些年,一定经历过那种“部署地狱”:新来的同事要花一整天配环境,测试环境和生产环境因为一个库的版本差异导致bug&#xff0…

作者头像 李华
网站建设 2026/7/18 16:58:56

代码签名证书OV和EV怎么选?2026年购买指南

准备为软件申请代码签名证书时,开发者最常遇到的问题就是: OV和EV代码签名证书到底怎么选? 价格相差数倍,两者的实际差距到底在哪里?本文从验证强度、信任等级、签名方式、适用场景四大维度展开对比,帮你…

作者头像 李华
网站建设 2026/7/18 16:57:54

Windows更新失败修复全攻略:从症状到解决方案

1. Windows更新失败的五大典型症状与危害预警 当Windows更新卡在某个进度百分比超过2小时,或是反复出现"无法完成更新"的红色警告时,你的系统已经处于危险边缘。我见过太多用户强行重启导致启动项损坏的案例——有位设计师因此丢失了即将交付的…

作者头像 李华
网站建设 2026/7/18 16:52:53

SAP ABAP调试技巧与实战指南

1. SAP ABAP调试方法全景解析 作为SAP系统的核心开发语言,ABAP调试是每位开发者必须掌握的生存技能。在15年的SAP项目实施中,我发现90%的开发问题都能通过有效调试快速定位。不同于普通编程语言的调试环境,ABAP调试器需要与SAP GUI深度集成&a…

作者头像 李华
网站建设 2026/7/18 16:45:11

上篇:九部门新政定调——零售业进入“精耕细作”时代

九部门新政定调——零售业进入“精耕细作”时代7月9日,商务部等9部门联合印发了《关于加快零售业创新发展的意见》。这是国家层面首次以多部门联合发文形式,针对零售业出台的系统性顶层设计文件。政策明确提出,力争到2030年,基本形…

作者头像 李华