方块着色器 ·Shader Block· ▶ 在线运行案例案例合集三维可视化功能案例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.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, 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() }const uniforms { iResolution: { type: v2, value: new THREE.Vector2(box.clientWidth, box.clientHeight) }, iTime: { type: f, value: 1.0 } } animate() function animate() { uniforms.iTime.value 0.01 requestAnimationFrame(animate) renderer.render(scene, camera) }let geometry new THREE.PlaneGeometry(2, 2);let material new THREE.ShaderMaterial({ uniforms: uniforms, vertexShader:void main() { gl_Position vec4( position, 1.0 ); }, fragmentShader:// 屏幕尺寸 precision highp float; uniform vec2 iResolution; uniform float iTime;float gTime 0.; const float REPEAT 5.0;// 回転行列 mat2 rot(float a) { float c cos(a), s sin(a); return mat2(c,s,-s,c); }float sdBox( vec3 p, vec3 b ) { vec3 q abs(p) - b; return length(max(q,0.0)) min(max(q.x,max(q.y,q.z)),0.0); }float box(vec3 pos, float scale) { pos * scale; float base sdBox(pos, vec3(.4,.4,.1)) /1.5; pos.xy * 5.; pos.y - 3.5; pos.xy * rot(.75); float result -base; return result; }float box_set(vec3 pos, float iTime) { vec3 pos_origin pos; pos pos_origin; pos .y sin(gTime0.4)2.5; pos.xy * rot(.8); float box1 box(pos,2. - abs(sin(gTime0.4))1.5); pos pos_origin; pos .y -sin(gTime0.4)2.5; pos.xy * rot(.8); float box2 box(pos,2. - abs(sin(gTime0.4))1.5); pos pos_origin; pos .x sin(gTime0.4)2.5; pos.xy * rot(.8); float box3 box(pos,2. - abs(sin(gTime0.4))1.5); pos pos_origin; pos .x -sin(gTime0.4)2.5; pos.xy * rot(.8); float box4 box(pos,2. - abs(sin(gTime0.4))1.5); pos pos_origin; pos.xy * rot(.8); float box5 box(pos,.5) * 6.; pos pos_origin; float box6 box(pos,.5) * 6.; float result max(max(max(max(max(box1,box2),box3),box4),box5),box6); return result; }float map(vec3 pos, float iTime) { vec3 pos_origin pos; float box_set1 box_set(pos, iTime);return box_set1; } void main() { vec2 p (gl_FragCoord.xy * 1. - iResolution.xy) / min(iResolution.x, iResolution.y); vec3 ro vec3(0., -0.2 ,iTime * 4.); vec3 ray normalize(vec3(p, 1.5)); ray.xy ray.xyrot(sin(iTime.03) * 5.); ray.yz ray.yzrot(sin(iTime.05) * .2); float t 0.1; vec3 col vec3(0.); float ac 0.0;for (int i 0; i 99; i){ vec3 pos ro ray * t; pos mod(pos-2., 4.) -2.; gTime iTime -float(i) * 0.01; float d map(pos, iTime);d max(abs(d), 0.01); ac exp(-d*23.);t d* 0.55; }col vec3(ac * 0.02);col vec3(0.,0.2abs(sin(iTime)),0.5 sin(iTime)0.2);gl_FragColor vec4(col ,1.0 - t(0.02 0.02sin (iTime))); }});let mesh new THREE.Mesh(geometry, material); scene.add(mesh);完整源码GitHub小结本文提供方块着色器完整 Three.js 源码与在线 Demo建议先运行案例再改 uniform/参数做二次实验更多 Three.js 实战案例见 three-cesium-examples 合集 与 GitHub 开源仓库