news 2026/7/8 19:51:31

Three.js 视频着色器教程

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
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 开源仓库
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/7/8 19:49:01

本地商家线上投放效率提升:从自主摸索到专业化运营的路径思考

随着本地生活服务市场的发展&#xff0c;线上投放已经成为实体商家获客的重要渠道。巨量本地推等产品的出现&#xff0c;降低了商家线上投放的门槛&#xff0c;但投放效果的差异化依然很大。本文从运营效率的角度&#xff0c;探讨本地商家线上投放的两种模式&#xff1a;自主摸…

作者头像 李华
网站建设 2026/7/8 19:42:58

Pearcleaner:macOS终极清理工具,免费解决应用残留难题

Pearcleaner&#xff1a;macOS终极清理工具&#xff0c;免费解决应用残留难题 【免费下载链接】Pearcleaner A free, source-available and fair-code licensed mac app cleaner 项目地址: https://gitcode.com/gh_mirrors/pe/Pearcleaner 你的Mac存储空间是否在不知不觉…

作者头像 李华
网站建设 2026/7/8 19:42:45

Codex API 实战指南:从命令行到 VS Code 的 Vibe Coding 工作流

1. 项目概述&#xff1a;Codex 与 Vibe Coding 不是“新模型”&#xff0c;而是开发者工作流的重构 Codex 这个名字&#xff0c;2023年之前在程序员圈子里几乎无人不晓——它是 OpenAI 在 2021 年底发布的、专为代码生成而微调的 GPT-3 变体&#xff0c;底层架构仍是 transform…

作者头像 李华
网站建设 2026/7/8 19:34:27

Cursor快捷键速成手册:从新手到高手,7天掌握90%高频操作场景

更多请点击&#xff1a; https://kaifayun.com 第一章&#xff1a;Cursor快捷键速成导论 Cursor 作为面向 AI 编程工作流深度优化的智能代码编辑器&#xff0c;其快捷键体系并非 VS Code 的简单复刻&#xff0c;而是围绕“意图驱动编码”重新设计的操作范式。掌握核心快捷键&…

作者头像 李华
网站建设 2026/7/8 19:30:43

HyperWorks 2026 CAE仿真环境工程化部署指南

1. 这不是“下载链接搬运工”&#xff0c;而是CAE工程师的HyperWorks 2026落地实操手记我第一次在客户现场部署HyperWorks 2026时&#xff0c;被三个问题卡了整整两天&#xff1a;License Server Manager反复报错“no socket connection to license server manager”&#xff0…

作者头像 李华
网站建设 2026/7/8 19:30:43

最新AI量化流程,API数据策略逻辑执行要连起来

用 AI 做量化开发&#xff0c;并不意味着可以跳过原有的落地过程。对已有经验者来说&#xff0c;更有效的方式是把 AI 放进阶段路径里&#xff0c;让它在学习、表达、开发和验证中分别承担不同的辅助作用。代码要回到规则本身学习阶段的重点不是追求产出&#xff0c;而是弄明白…

作者头像 李华