Three.js 围墙着色器教程

围墙着色器 · Fence Wall · ▶ 在线运行案例

围墙着色器

你将学到什么

  • ShaderMaterial 自定义着色器实现核心视觉效果
  • OrbitControls 相机轨道交互

效果说明

本案例演示 围墙着色器 效果:基于 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/addons/controls/OrbitControls.js";

    const { innerWidth, innerHeight } = window; const aspect = innerWidth / innerHeight;

    class Base { constructor() { this.init(); this.main(); } main() { const geometry = new THREE.CylinderGeometry(30, 30, 20, 4, 64, true); const material = new THREE.ShaderMaterial({ side: THREE.DoubleSide, transparent: true, depthWrite: false, uniforms: { uTime: this.deltaTime, }, vertexShader: varying vec2 vUv; void main(){ vUv = uv; gl_Position = projectionMatrix modelViewMatrix vec4(position,1.0); } , fragmentShader: uniform float uTime; varying vec2 vUv; #define PI 3.14159265

    void main(){

    vec4 baseColor = vec4(0.0,1.,0.5,1.); vec4 finalColor; float amplitude = 1.; float frequency = 10.; float x = vUv.x;

    float y = sin(x * frequency) ; float t = 0.01(-uTime130.0); y += sin(xfrequency2.1 + t)*4.5; y += sin(xfrequency1.72 + t1.121)4.0; y += sin(xfrequency2.221 + t0.437)5.0; y += sin(xfrequency3.1122+ t4.269)2.5; y = amplitude0.06; y /= 3.; y += 0.55;

    vec4 color = gl_FragColor.rgba;

    float r = step(0.5, fract(vUv.y - uTime));

    baseColor.a = step(vUv.y,y) * (y-vUv.y)/y; gl_FragColor = baseColor;

    } , }); const cylinder = new THREE.Mesh(geometry, material); cylinder.position.y += 10; this.scene.add(cylinder); } init() { this.clock = new THREE.Clock();

    this.renderer = new THREE.WebGLRenderer({ antialias: true, logarithmicDepthBuffer: true, }); this.renderer.setPixelRatio(window.devicePixelRatio); this.renderer.setSize(innerWidth, innerHeight); this.renderer.setAnimationLoop(this.animate.bind(this)); document.body.appendChild(this.renderer.domElement);

    this.camera = new THREE.PerspectiveCamera(60, aspect, 0.01, 10000); this.camera.position.set(0, 40, 40);

    this.scene = new THREE.Scene();

    this.controls = new OrbitControls(this.camera, this.renderer.domElement);

    this.deltaTime = { value: 0, };

    const grid = new THREE.GridHelper(100); this.scene.add(grid);

    const light = new THREE.AmbientLight(0xffffff, 0.5); this.scene.add(light); } animate() { this.controls.update(); this.renderer.render(this.scene, this.camera); this.deltaTime.value = this.clock.getElapsedTime(); } } new Base();

    完整源码:GitHub

    小结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值