-
Notifications
You must be signed in to change notification settings - Fork 1
/
texture.js
43 lines (38 loc) · 1.31 KB
/
texture.js
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
class Texture extends Object {
// 表示三维物体的类
constructor(source) {
super()
const lines = source.split('\n')
const width = Number(lines[2])
const height = Number(lines[3])
this.width = width
this.height = height
this.pixels = []
const colorList = lines.slice(4, 4 + width)
const delimiter = ' '
for (let i = 0; i < height; i++) {
const line = colorList[i]
const row = line.split(delimiter)
for(let j = 0; j < width; j++) {
const hex = Number(row[j]) // 具体的单个数据,不是hex
let r = (hex & 0xff000000) >>> 24
let g = (hex & 0x00ff0000) >>> 16
let b = (hex & 0x0000ff00) >>> 8
let a = (hex & 0x000000ff)
const pixel = [r, g, b, a]
this.pixels[i * width + j] = pixel;
}
}
}
sample(u, v) {
if (this.pixels) {
let tu = Math.abs(Math.floor(u * (this.width - 1)));
let tv = Math.abs(Math.floor(v * (this.height - 1)));
let index = tu + tv * this.width;
const c = Color.new(...this.pixels[index]);
return c;
} else {
return Color.randomColor()
}
}
}