0%

08 贴图阵列 - 实现一个瓷砖地板 - Threejs入门

通过开启阵列模式,设置x,y的重复次数,重复图案就出现啦 [ 练一练 ] [ 看一看 ]

示例代码

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
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/瓷砖.jpg')

// 开启阵列模式
txImg.wrapS = THREE.RepeatWrapping
txImg.wrapT = THREE.RepeatWrapping

// 设置重复多少次
txImg.repeat.set(5, 5)

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

const mtr = new THREE.MeshBasicMaterial({
map: txImg
})

const mesh = new THREE.Mesh(geo, mtr)
scene.add(mesh)

new OrbitControls(camera, renderer.domElement)

function animate() {
requestAnimationFrame(animate)
renderer.render(scene, camera)
}
animate()

演示效果