-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsquare.js
More file actions
146 lines (124 loc) · 4.22 KB
/
square.js
File metadata and controls
146 lines (124 loc) · 4.22 KB
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
const glmatrix = require('gl-matrix')
const vec3 = glmatrix.vec3
const mat4 = glmatrix.mat4
const glutils = require('./glutils')
const noise = require('./noise')
function Square(camera, environment, size) {
let attributes = null
let positionBuffer = null
let textureBuffer = null
let program = null
let texture = null
const createTexture = (context, width, height, data) => {
const texture = context.createTexture()
context.bindTexture(context.TEXTURE_2D, texture)
context.texImage2D(context.TEXTURE_2D,
0,
context.RGBA,
width,
height,
0,
context.RGBA,
context.UNSIGNED_BYTE,
data)
context.texParameteri(context.TEXTURE_2D,
context.TEXTURE_MAG_FILTER,
context.NEAREST)
context.texParameteri(context.TEXTURE_2D,
context.TEXTURE_MIN_FILTER,
context.NEAREST)
return texture
}
const getTextureCoords = () => {
return [0.0, 0.0,
1.0, 1.0,
0.0, 1.0,
1.0, 1.0,
0.0, 0.0,
1.0, 0.0]
}
const getVertices = () => {
return [-1.0*size, -1.0*size+2.0, 0.0,
+1.0*size, +1.0*size+2.0, 0.0,
-1.0*size, +1.0*size+2.0, 0.0,
+1.0*size, +1.0*size+2.0, 0.0,
-1.0*size, -1.0*size+2.0, 0.0,
+1.0*size, -1.0*size+2.0, 0.0]
}
this.initialize = (context, content) => {
program = content.programs['square']
positionBuffer = glutils.createArrayBuffer(context,
program,
new Float32Array(getVertices(environment)),
(context, program) => context.getAttribLocation(program, 'a_position'),
3)
textureBuffer = glutils.createArrayBuffer(context,
program,
new Float32Array(getTextureCoords()),
(context, program) => context.getAttribLocation(program, 'a_texcoord'),
2)
attributes = {
'u_world': context.getUniformLocation(program, 'u_world'),
'u_view': context.getUniformLocation(program, 'u_view'),
'u_projection': context.getUniformLocation(program, 'u_projection'),
'u_texture': context.getUniformLocation(program, 'u_texture')
}
const randomNoise = s => noise.randomNoise(s,
() => Math.random(),
32.0)
const clouds = s => noise.clouds(s,
() => Math.random(),
32.0)
const marble = s => noise.marble(s,
() => Math.random(),
16.0,
5.0,
10.0,
5.0,
v => {
const k = 0.58
v[0] = v[0]*k+70
v[1] = v[1]*k+10
v[2] = v[2]*k
return v
})
const wood = s => noise.wood(s,
() => Math.random(),
32.0,
12.0,
0.2,
v => {
v[0] = v[0]+80
v[1] = v[1]+30
v[2] = 30
return v
})
texture = createNoiseTexture(context, 128, wood)
}
const createNoiseTexture = (context, size, noise) => {
const data = noise(size).reduce((a, b) => {
return a.concat(b)
}, [])
return createTexture(context,
size,
size,
new Uint8Array(data))
}
this.update = (context, time) => {
context.useProgram(program)
const world = mat4.create()
context.uniformMatrix4fv(attributes['u_world'], false, world)
context.uniformMatrix4fv(attributes['u_view'], false, camera.getView())
context.uniformMatrix4fv(attributes['u_projection'], false, camera.getProjection(context))
}
this.draw = (context, time) => {
context.useProgram(program)
positionBuffer.bind(context)
textureBuffer.bind(context)
context.uniform1i(attributes['u_texture'], 0)
context.activeTexture(context.TEXTURE0)
context.bindTexture(context.TEXTURE_2D, texture)
context.drawArrays(context.TRIANGLE_STRIP, 0, 6)
}
}
module.exports = Square