围栏着色器 ·Fence Shader· ▶ 在线运行案例案例合集三维可视化功能案例threehub.cn开源仓库github地址https://github.com/z2586300277/three-cesium-examples400个案例代码:网盘链接你将学到什么ShaderMaterial 自定义着色器实现核心视觉效果OrbitControls 相机轨道交互BufferGeometry 自定义顶点/索引数据requestAnimationFrame渲染循环与resize自适应效果说明本案例演示围栏着色器效果基于 WebGL 实现「围栏着色器」可视化效果附完整可运行源码核心用到 ShaderMaterial、OrbitControls、BufferGeometry。建议先打开文首在线案例查看动态画面再对照下方源码逐步理解。核心概念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.jsconst 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, 40, 40)const renderer new THREE.WebGLRenderer()renderer.setSize(box.clientWidth, box.clientHeight)new OrbitControls(camera, renderer.domElement)window.onresize () {renderer.setSize(box.clientWidth, box.clientHeight)camera.aspect box.clientWidth / box.clientHeightcamera.updateProjectionMatrix() }box.appendChild(renderer.domElement)/顶点着色器/ const vertexs varying vec2 vUv; void main() { vec4 mvPosition modelViewMatrix * vec4( position, 1.0 ); vUv uv; gl_Position projectionMatrixmodelViewMatrixvec4(position, 1.0); }/片元着色器/ const fragments uniform float time; uniform float opacity; uniform vec3 color; uniform float num; uniform float hiz; varying vec2 vUv; void main() { vec4 fragColor vec4(0.); float sin sin((vUv.y - timehiz)10. * num); float high 0.92; float medium 0.4; if (sin high) { fragColor vec4(mix(vec3(.8, 1., 1.), color, (1. - sin) / (1. - high)), 1.); } else if(sin medium) { fragColor vec4(color, mix(1., 0., 1.-(sin - medium) / (high - medium))); } else { fragColor vec4(color, 0.); } vec3 fade mix(color, vec3(0., 0., 0.), vUv.y); fragColor mix(fragColor, vec4(fade, 1.), 0.85); gl_FragColor vec4(fragColor.rgb, fragColor.aopacity(1. - vUv.y)); }// 自定义材质 const material new THREE.ShaderMaterial({ uniforms: { time: { type: pv2, value: 0, }, color: { type: uvs, value: new THREE.Color(#FF4127), }, opacity: { type: pv2, value: 1.0, }, num: { type: pv2, value: 10, }, hiz: { type: pv2, value: 0.03, }, }, vertexShader: vertexs, fragmentShader: fragments, blending: THREE.AdditiveBlending, transparent: !0, depthWrite: !1, depthTest: !0, side: THREE.DoubleSide, });let c [0,0, 10, 0, 10, 10, 0, 10, 0, 0]let posArr [];let uvrr [];let h 10; //围墙拉伸高度for (let i 0; i c.length - 2; i 2) {// 矩形的三角形1 posArr.push(c[i], c[i 1], 0, c[i 2], c[i 3], 0, c[i 2], c[i 3], h);// 矩形的三角形2 posArr.push(c[i], c[i 1], 0, c[i 2], c[i 3], h, c[i], c[i 1], h);// 注意顺序问题和顶点位置坐标对应 uvrr.push(0, 0, 1, 0, 1, 1);uvrr.push(0, 0, 1, 1, 0, 1);}const geometry new THREE.BufferGeometry(); //声明一个空几何体对象// 设置几何体attributes属性的位置position属性 geometry.attributes.position new THREE.BufferAttribute(new Float32Array(posArr), 3);// 设置几何体attributes属性的位置uv属性 geometry.attributes.uv new THREE.BufferAttribute(new Float32Array(uvrr), 2);geometry.computeVertexNormals()const mesh new THREE.Mesh(geometry, material) //网格模型对象Meshscene.add(mesh)function animate() {requestAnimationFrame(animate)mesh.rotation.x 0.01mesh.rotation.y 0.01material.uniforms.time.value 0.03renderer.render(scene, camera)}animate()完整源码GitHub小结本文提供围栏着色器完整 Three.js 源码与在线 Demo建议先运行案例再改 uniform/参数做二次实验更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库