当前位置: 首页 > news >正文

Three.js 视频着色器教程

视频着色器 ·Video Shader· ▶ 在线运行案例

  • 案例合集:三维可视化功能案例(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()

实现步骤

  • 搭建 Scene、PerspectiveCamera、WebGLRenderer,挂载 canvas 并处理resize
  • 定义 uniforms / onBeforeCompile 或 ShaderMaterial,编写 GLSL 与材质参数
  • 创建 OrbitControls(及 Raycaster 等交互控件,若源码包含)
  • requestAnimationFrame循环中更新状态并 render(Cesium 为viewer.render或自动渲染)
  • 代码要点

    import * as THREE from 'three'

    import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js' import * as dat from 'dat.gui'

    const box = document.getElementById('box')

    const scene = new THREE.Scene()

    const camera = new THREE.PerspectiveCamera(75, box.clientWidth / box.clientHeight, 0.1, 1000)

    camera.position.set(0, 10, 10)

    const renderer = new THREE.WebGLRenderer()

    renderer.setSize(box.clientWidth, box.clientHeight)

    box.appendChild(renderer.domElement)

    new OrbitControls(camera, renderer.domElement)

    window.onresize = () => {

    renderer.setSize(box.clientWidth, box.clientHeight)

    camera.aspect = box.clientWidth / box.clientHeight

    camera.updateProjectionMatrix() }

    scene.add(new THREE.AxesHelper(50000)) // 坐标轴

    const amibientLight = new THREE.AmbientLight(0xffffff, 4) // 环境光

    scene.add(amibientLight) // 添加环境光

    const geometry = new THREE.BoxGeometry(5, 5, 5) // 立方体

    const video = document.createElement('video')

    video.crossOrigin = 'anonymous' // 跨域

    video.src = 'https://z2586300277.github.io/3d-file-server/video/test.mp4'

    video.loop = true // 循环播放

    video.muted = true // 静音

    video.play()

    const texture = await new Promise(r => video.onloadeddata = () => r(new THREE.VideoTexture(video))) // 创建视频纹理

    // 使用 shader 库中的phong材质 进行修改 const shader = { uniforms: THREE.UniformsUtils.merge([

    THREE.ShaderLib['phong'].uniforms,

    { r: { type: 'v2', value: new THREE.Vector2(box.clientWidth, box.clientHeight) }, t: { type: 'f', value: 0.0 }, colorTexture: { value: texture }, calcType: { value: 2 } }

    ]),

    vertexShader: THREE.ShaderLib['phong'].vertexShader,

    fragmentShader: THREE.ShaderLib['phong'].fragmentShader,

    }

    // GUI 切换混合运算类型 const GUI = new dat.GUI()

    GUI.add(shader.uniforms.calcType, 'value', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]).name('混合运算类型');

    // 动画 animate()

    function animate() {

    shader.uniforms.t.value += 0.1

    renderer.render(scene, camera)

    requestAnimationFrame(animate)

    }

    shader.vertexShader = shader.vertexShader.replace(/#include /,varying vec2 vUv; #include)

    shader.vertexShader = shader.vertexShader.replace('void main() {',void main() { vUv = uv;)

    shader.fragmentShader = shader.fragmentShader.replace(/#include /,precision highp float; varying vec2 vUv; uniform vec2 r; uniform float t; uniform float calcType; uniform sampler2D colorTexture; #include)

    shader.fragmentShader = shader.fragmentShader.replace('vec4 diffuseColor = vec4( diffuse, opacity );',vec3 c; float l,z=t; for(int i=0;i<3;i++) { vec2 uv,p=gl_FragCoord.xy/r/2.0; uv=p + 2.0 * vUv; p-=.5; p.x*=r.x/r.y; z+=.07; l=length(p); uv+=p/l(sin(z)+1.)abs(sin(l*9.-z-z)); c[i]=.01/length(mod(uv,1.)-.5); } vec3 color = texture2D( colorTexture, vUv ).rgb; vec3 mixedColor; if (calcType == 0.0) mixedColor = max(color, c); else if(calcType == 1.0) mixedColor = min(color, c); else if(calcType == 2.0) mixedColor = mix(color, c, 0.5); else if(calcType == 3.0) mixedColor = mod(color, c); else if(calcType == 4.0) mixedColor = pow(color, c); else if(calcType == 5.0) mixedColor = step(color, c); else if(calcType == 6.0) mixedColor = color + c; else if(calcType == 7.0) mixedColor = color - c; else if(calcType == 8.0) mixedColor = c - color; else if(calcType == 9.0) mixedColor = color + c - vec3(1.0)ccolor; else mixedColor = color; vec4 diffuseColor = vec4( diffuse * mixedColor, opacity );)

    const material = new THREE.ShaderMaterial(shader)

    material.lights = true // 光照影响

    const mesh = new THREE.Mesh(geometry, material)

    scene.add(mesh)

    完整源码:GitHub

    小结

    • 本文提供视频着色器完整 Three.js 源码与在线 Demo,建议先运行案例再改 uniform/参数做二次实验
    • 更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库
http://www.cnnetsun.cn/news/3228200.html

相关文章:

  • 本地商家线上投放效率提升:从自主摸索到专业化运营的路径思考
  • Pearcleaner:macOS终极清理工具,免费解决应用残留难题
  • Codex API 实战指南:从命令行到 VS Code 的 Vibe Coding 工作流
  • Cursor快捷键速成手册:从新手到高手,7天掌握90%高频操作场景
  • HyperWorks 2026 CAE仿真环境工程化部署指南
  • 最新AI量化流程,API数据策略逻辑执行要连起来
  • 腾讯云混元API KEY申请与安全配置全指南
  • 网易云音乐永久直链解析:如何5分钟搭建专属音乐API服务器
  • VS Code远程连接Linux服务器:绕过Codex误区,实战Remote-SSH
  • 终极Nerd Fonts指南:为开发者打造的6000+图标字体解决方案
  • 国内开发者Gemini高可用接入实战:Nginx三层架构方案
  • Hermes Agent国内多环境部署实战:从阿里云到Mac M2保姆级指南
  • VMware Workstation Pro 17 安装 Ubuntu 24.04.4 LTS 实战指南
  • OpenViking:为多Agent系统构建语义化共享记忆基础设施
  • :Conda 常用命令总结
  • Claude Code不是官方产品:API调用与跨平台客户端搭建指南
  • LiteLLM 实现 Claude Code 与 Azure OpenAI 无缝对接
  • SPT-AKI存档编辑器终极指南:5分钟掌握塔科夫离线版存档修改技巧
  • DLSS Swapper终极指南:如何一键智能切换DLSS版本,彻底释放显卡性能
  • Codex CLI工具链:本地化AI编程工作流实战指南
  • REPENTOGON终极指南:3分钟快速上手《以撒的结合》最强脚本扩展器
  • OpenClaw工作流智能体框架:生产级部署与安全加固指南
  • LangGraph StateGraph实战:从零构建可调试、可重试的智能体工作流
  • 终极指南:如何用FigmaToCode在5分钟内将设计稿变成可运行代码
  • TRAE SOLO:VS Code 深度集成的 AI 编程协作者
  • Trae本地模型代理:Go实现OpenAI协议兼容的反向代理
  • umy-ui:Vue大数据表格的虚拟滚动与高性能渲染机制解析
  • Windows本地部署OpenClaw+飞书Agent实战指南
  • 如何在Windows 10/11上直接安装Android应用:APK Installer终极指南
  • OpenCV 4.8 车牌识别实战:结合 PaddleOCR v3 实现 90%+ 准确率