-
Notifications
You must be signed in to change notification settings - Fork 2
/
default-scene-renderer.js
83 lines (64 loc) · 1.71 KB
/
default-scene-renderer.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
import assert from 'assert'
import drawTriangle from 'a-big-triangle'
class DefaultSceneRenderer {
constructor (gl) {
assert(gl, 'A gl context is required')
this.gl = gl
this.width = window.innerWidth
this.height = window.innerHeight
}
resize (width, height) {
this.width = width
this.height = height
}
render (shader, t) {
let { width, height, gl } = this
shader.uniforms.resolution = [width, height]
gl.viewport(0, 0, width, height)
var projectionMatrix = this.projectionMatrix(80, 1, 1)
shader.uniforms.projectionMatrix = projectionMatrix
drawTriangle(gl)
}
projectionMatrix (fov, aspect, zoom) {
const near = 0.01
const far = 10000
const DEG2RAD = Math.PI / 180
const skew = 0
const filmGauge = 35
var top = near * Math.tan(
DEG2RAD * 0.5 * fov) / zoom
var height = 2 * top
var width = aspect * height
var left = -0.5 * width
const filmWidth = filmGauge * Math.min(aspect, 1)
if (skew !== 0) left += near * skew / filmWidth
return this.makeFrustum(left, left + width, top - height, top, near, far)
}
makeFrustum (left, right, bottom, top, near, far) {
var te = []
var x = 2 * near / (right - left)
var y = 2 * near / (top - bottom)
var a = (right + left) / (right - left)
var b = (top + bottom) / (top - bottom)
var c = -(far + near) / (far - near)
var d = -2 * far * near / (far - near)
te[0] = x
te[4] = 0
te[8] = a
te[12] = 0
te[1] = 0
te[5] = y
te[9] = b
te[13] = 0
te[2] = 0
te[6] = 0
te[10] = c
te[14] = d
te[3] = 0
te[7] = 0
te[11] = -1
te[15] = 0
return te
}
}
module.exports = DefaultSceneRenderer