-
Notifications
You must be signed in to change notification settings - Fork 1
/
canvas.js
227 lines (222 loc) · 7.76 KB
/
canvas.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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
class Camera extends Object {
constructor() {
super()
// 镜头在世界坐标系中的坐标
this.position = Vector.new(0, 0, -10)
// 镜头看的地方
this.target = Vector.new(0, 0, 0)
// 镜头向上的方向
this.up = Vector.new(0, 1, 0)
}
}
class Canvas extends Object {
constructor(selector) {
super()
let canvas = _e(selector)
this.canvas = canvas
this.context = canvas.getContext('2d')
this.w = canvas.width
this.h = canvas.height
this.pixels = this.context.getImageData(0, 0, this.w, this.h)
this.zLevel = Array(this.w * this.h).fill(null)
this.bytesPerPixel = 4
// this.pixelBuffer = this.pixels.data
this.camera = Camera.new()
}
render() {
// 执行这个函数后, 才会实际地把图像画出来
// ES6 新语法, 取出想要的属性并赋值给变量, 不懂自己搜「ES6 新语法」
let {pixels, context} = this
context.putImageData(pixels, 0, 0)
}
clear(color=Color.transparent()) {
// 清空zLevel
this.zLevel.fill(null, 0, this.w * this.h)
// color Color
// 用 color 填充整个 canvas
// 遍历每个像素点, 设置像素点的颜色
let {w, h} = this
for (let x = 0; x < w; x++) {
for (let y = 0; y < h; y++) {
this._setPixel(x, y, color, 0)
}
}
this.render()
}
_getPixel(x, y) {
let int = Math.floor
x = int(x)
y = int(y)
// 用座标算像素下标
let i = (y * this.w + x) * this.bytesPerPixel
// 设置像素
let p = this.pixels.data
return Color.new(p[i], p[i+1], p[i+2], p[i+3])
}
_setPixel(x, y, color, z) {
// color: Color
// 这个函数用来设置像素点, _ 开头表示这是一个内部函数, 这是我们的约定
// 浮点转 int
let int = Math.round
x = int(x)
y = int(y)
// 设置z
let j = y * this.w + x
const depth = this.zLevel[j]
if (depth != null && depth > z) return
this.zLevel[j] = z
// 用座标算像素下标
let i = j * this.bytesPerPixel
// 设置像素
let p = this.pixels.data
let {r, g, b, a} = color
// 一个像素 4 字节, 分别表示 r g b a
p[i] = int(r)
p[i+1] = int(g)
p[i+2] = int(b)
p[i+3] = int(a)
}
drawPoint(point, color=Color.black()) {
// point: Point
let {w, h} = this
let p = point
if (p.x >= 0 && p.x <= w) {
if (p.y >= 0 && p.y <= h) {
this._setPixel(p.x, p.y, color, p.z)
}
}
}
drawLine(p1, p2, color=Color.black()) {
let [x1, y1, x2, y2] = [p1.x, p1.y, p2.x, p2.y]
let dx = x2 - x1
let dy = y2 - y1
let R = (dx ** 2 + dy ** 2) ** 0.5
let ratio = dx === 0 ? undefined : dy / dx
let angle = 0
if (ratio === undefined) {
const p = Math.PI / 2
angle = dy >= 0 ? p : -p
} else {
const t = Math.abs(dy / R)
const sin = ratio >= 0 ? t : -t
const asin = Math.asin(sin)
angle = dx > 0 ? asin : asin + Math.PI
}
for (let r = 0; r <= R; r++) {
const x = x1 + Math.cos(angle) * r
const y = y1 + Math.sin(angle) * r
this.drawPoint(Vector.new(x, y, p1.z), color)
}
}
drawScanline(v1, v2) {
let [a, b] = [v1, v2].sort((va, vb) => va.position.x - vb.position.x)
let y = a.position.y
let x1 = a.position.x
let x2 = b.position.x
for (let x = x1; x <= x2; x++) {
let factor = 0
if (x2 != x1) {
factor = (x - x1) / (x2 - x1);
}
let color = a.color.interpolate(b.color, factor)
// console.log(v1.position.z)
this.drawPoint(Vector.new(x, y, v1.position.z), color)
}
}
drawTriangle(v1, v2, v3) {
let [a, b, c] = [v1, v2, v3].sort((va, vb) => va.position.y - vb.position.y)
// log(a, b, c)
let middle_factor = 0
if (c.position.y - a.position.y != 0) {
middle_factor = (b.position.y - a.position.y) / (c.position.y - a.position.y)
}
let middle = a.interpolate(c, middle_factor)
let start_y = a.position.y
let end_y = b.position.y
for (let y = start_y; y <= end_y; y++) {
let factor = 0
if (end_y != start_y) {
factor = (y - start_y) / (end_y - start_y)
}
let va = a.interpolate(middle, factor)
let vb = a.interpolate(b, factor)
// log(va.position, vb.position)
this.drawScanline(va, vb)
}
start_y = b.position.y
end_y = c.position.y
for (let y = start_y; y <= end_y; y++) {
let factor = 0
if (end_y != start_y) {
factor = (y - start_y) / (end_y - start_y)
}
let va = middle.interpolate(c, factor)
let vb = b.interpolate(c, factor)
// log(va.position, vb.position)
this.drawScanline(va, vb)
}
}
// drawTriangleLine
project(coordVector, transformMatrix) {
let {w, h} = this
let [w2, h2] = [w/2, h/2]
let point = transformMatrix.transform(coordVector.position)
let x = point.x * w2 + w2
let y = - point.y * h2 + h2
let z = point.z // coordVector.position.z
let v = Vector.new(x, y, z)
return Vertex.new(v, coordVector.color)
}
drawMesh(mesh) {
let self = this
// camera
let {w, h} = this
let {position, target, up} = self.camera
const view = Matrix.lookAtLH(position, target, up)
// field of view
const projection = Matrix.perspectiveFovLH(0.8, w / h, 0.1, 1)
// 得到 mesh 中点在世界中的坐标
const rotation = Matrix.rotation(mesh.rotation)
const translation = Matrix.translation(mesh.position)
const scale = Matrix.scale(mesh.scale)
const world = scale.multiply(rotation.multiply(translation))
const transform = world.multiply(view).multiply(projection)
for (let t of mesh.indices) {
// 拿到三角形的三个顶点
let [a, b, c] = t.map(i => mesh.vertices[i])
// 拿到屏幕上的 3 个像素点
let [v1, v2, v3] = [a, b, c].map(v => self.project(v, transform))
// 把这个三角形画出来
// self.drawLine(v1.position, v2.position)
// self.drawLine(v1.position, v3.position)
// self.drawLine(v2.position, v3.position)
self.drawTriangle(v1, v2, v3)
}
}
drawTexture(texture) {
const t = texture
t.pixels.forEach((p, index) => {
const [r, g, b, a] = p
const c = Color.new(r, g, b, a)
const y = Math.floor(index / t.width)
const x = index % t.width
this.drawPoint(Vector.new(x, y), c)
})
}
__debug_draw_demo() {
// 这是一个 demo 函数, 用来给你看看如何设置像素
// ES6 新语法, 取出想要的属性并赋值给变量, 不懂自己搜「ES6 新语法」
let {context, pixels} = this
// 获取像素数据, data 是一个数组
let data = pixels.data
// 一个像素 4 字节, 分别表示 r g b a
for (let i = 0; i < data.length; i += 4) {
let [r, g, b, a] = data.slice(i, i + 4)
r = 255
a = 255
data[i] = r
data[i+3] = a
}
context.putImageData(pixels, 0, 0)
}
}