0%

09 透明贴图和UV动画 - Threejs入门

在材质中设置 transparent 属性,开启透明选项,然后动态修改贴图的偏移属性,贴图就动起来啦~ [ 练一练 ] [ 看一看 ]

示例代码

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
47
48
49
50
import * as THREE from 'three'
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls.js'

const $app = document.getElementById('app')

const scene = new THREE.Scene()

const camera = new THREE.PerspectiveCamera(
50,
window.innerWidth / window.innerHeight,
0.1,
1000
)
camera.position.set(0, 0, 60)

const renderer = new THREE.WebGLRenderer({
antialias: true,
})
renderer.setSize(window.innerWidth, window.innerHeight)
$app.appendChild(renderer.domElement)

const txLoader = new THREE.TextureLoader()
const txImg = txLoader.load('/htmls/three.js/imgs/箭头.png')

txImg.wrapS = THREE.RepeatWrapping
txImg.repeat.set(10, 1)

const geo = new THREE.PlaneGeometry(30, 3)

// 开启透明选项
const mtr = new THREE.MeshBasicMaterial({
map: txImg,
transparent: true,
side: THREE.DoubleSide
})

const mesh = new THREE.Mesh(geo, mtr)
const meshBack = new THREE.Mesh(geo, new THREE.MeshBasicMaterial({color: 0xcccccc}))
meshBack.position.z = -10
scene.add(mesh, meshBack)

new OrbitControls(camera, renderer.domElement)

function animate() {
// UV偏移动画
txImg.offset.x -= 0.01
requestAnimationFrame(animate)
renderer.render(scene, camera)
}
animate()

演示效果