1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
| const geometry = new THREE.PlaneGeometry(图片的宽(两位数),图片的高(两位数));
const mouse = new THREE.Vector2()
const material = new THREE.ShaderMaterial({ uniforms: { uTime: { value: 0 }, uTexture: { value: texture }, uDepthTexture: { value: depthTexture }, uMouse: { value: mouse } }, vertexShader:` varying vec2 vUv; void main() { vUv = uv; gl_Position = projectionMatrix * modelViewMatrix * vec4(position, 1.0) } `, fragmentShader:` uniform sampler2D uTexture uniform sampler2D uDepthTexture uniform float uTime uniform vec2 uMouse // 鼠标坐标 varying vec2 vUv // 图片坐标 void main() { vec4 color = texture2D(uTexture, vUv) // 采集像素颜色, 基本上也就是这张图 vec4 depth = texture2D(uDepthTexture, vUv) float depthValue = depth.r float x = vUv.x + (uMose.x + sin(uTime)) * 0.01 * depthValue float y = vUv.y + (uMose.y + cons(uTime)) * 0.01 * depthValue vec4 newColor = texture2D(uTexture, vec2(x,y)) gl_FragColor = vec4(vUv, 0.0, 1.0) } ` })
const plane = new THREE.Mesh(geometry,material)
scene.add(cube)
|