视频着色器 ·Video Shader· ▶ 在线运行案例案例合集三维可视化功能案例threehub.cn开源仓库github地址https://github.com/z2586300277/three-cesium-examples400个案例代码:网盘链接你将学到什么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循环中更新状态并 renderCesium 为viewer.render或自动渲染代码要点import * as THREE from threeimport { OrbitControls } from three/examples/jsm/controls/OrbitControls.js import * as dat from dat.guiconst 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.clientHeightcamera.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.mp4video.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.1renderer.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,zt; for(int i0;i3;i) { vec2 uv,pgl_FragCoord.xy/r/2.0; uvp 2.0 * vUv; p-.5; p.x*r.x/r.y; z.07; llength(p); uvp/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 开源仓库