-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
214 lines (178 loc) · 5.23 KB
/
index.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
var loadImage = require('image-loaded')
var GL_UNSIGNED_BYTE = 5121
var GL_RGBA = 6048
var GL_NEAREST = 9728
var GL_CLAMP_TO_EDGE = 33071
var bogusData = new Uint8Array(4)
module.exports = Texture2D
function Texture2D () {
if (!(this instanceof Texture2D)) {
return new Texture2D()
}
this.gl = null
this.texture = null
this.hasContent = false
this.type = GL_UNSIGNED_BYTE
this.shape = new Float32Array(3)
this.format = GL_RGBA
this.minFilter = GL_NEAREST
this.magFilter = GL_NEAREST
this.wrap = [GL_CLAMP_TO_EDGE, GL_CLAMP_TO_EDGE]
this._loadedWith = null
this.isLoaded = true
}
Texture2D.prototype.bind = function (gl, location) {
gl = gl || this.gl
location = location || 0
if (gl && this.gl && this.gl !== gl) throw new Error('Textures are currently only supported for a single WebGL context')
if (!this.texture) {
this.gl = gl
this.texture = gl.createTexture()
gl.activeTexture(gl.TEXTURE0 + location)
gl.bindTexture(gl.TEXTURE_2D, this.texture)
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, 1, 1, 0, gl.RGBA, gl.UNSIGNED_BYTE, bogusData)
} else {
gl.activeTexture(gl.TEXTURE0 + location)
gl.bindTexture(gl.TEXTURE_2D, this.texture)
}
if (this._loadedWith !== null) {
this.uploadElement(this._loadedWith)
this.setFilters(this.minFilter, this.magFilter)
this.setWrapping(this.wrap[0], this.wrap[1])
this._loadedWith = null
}
return location
}
Texture2D.prototype.init = function (shape, DataType) {
var size = 1
size *= shape[0]
size *= shape[1]
size *= shape.length > 2 ? shape[2] : 4
DataType = DataType || Uint8Array
this.uploadData(new DataType(size), shape)
this.setFilters(this.minFilter, this.magFilter)
this.setWrapping(this.wrap[0], this.wrap[1])
return this
}
Texture2D.prototype.setFilters = function (min, mag) {
if (!min && !mag) return this
if (arguments.length === 1) return this.setFilters(min, min)
var gl = this.gl
if (min) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, min)
if (mag) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, mag)
this.minFilter = min
this.magFilter = mag
return this
}
Texture2D.prototype.setWrapping = function (x, y) {
if (!x && !y) return this
if (arguments.length === 1) return this.setWrapping(x, x)
var gl = this.gl
if (x) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, x)
if (y) gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, y)
this.wrap[0] = x
this.wrap[1] = y
return this
}
var floatExt = null
Texture2D.prototype.uploadData = function (data, shape) {
var gl = this.gl
this.bind()
this.shape[0] = shape[0]
this.shape[1] = shape[1]
this.shape[2] = shape.length > 2 ? shape[2] : 4
this.format = getFormat(gl, this.shape[2])
if (Array.isArray(data)) {
data = new Uint8Array(data)
}
if (data instanceof Float32Array) {
floatExt = floatExt || gl.getExtension('OES_TEXTURE_FLOAT')
this.type = gl.FLOAT
if (!floatExt) {
throw new Error('Floating point textures are not available on this device')
}
} else {
this.type = gl.UNSIGNED_BYTE
}
gl.pixelStorei(gl.UNPACK_ALIGNMENT, this.shape[2])
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false)
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false)
gl.texImage2D(
gl.TEXTURE_2D,
0, // mip level
this.format,
this.shape[0],
this.shape[1],
0, // border
this.format,
this.type,
data
)
return this
}
Texture2D.prototype.uploadElement = function (el) {
var gl = this.gl
this.shape[0] = el.naturalWidth || el.width
this.shape[1] = el.naturalHeight || el.height
this.shape[2] = 4
this.format = getFormat(gl, this.shape[2])
gl.pixelStorei(gl.UNPACK_ALIGNMENT, this.shape[2])
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false)
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, true)
gl.texImage2D(gl.TEXTURE_2D, 0, this.format, this.format, this.type, el)
}
Texture2D.prototype.insertData = function (data, coord, shape) {
var gl = this.gl
if (Array.isArray(data)) {
data = this.type === gl.FLOAT
? new Float32Array(data)
: new Uint8Array(data)
}
var expectedLength = shape[0] * shape[1] * this.shape[2]
if (expectedLength !== data.length) {
throw new Error(
'Your data should be ' + expectedLength + ' elements long, but is currently ' + data.length
)
}
this.bind()
gl.pixelStorei(gl.UNPACK_ALIGNMENT, this.shape[2])
gl.pixelStorei(gl.UNPACK_PREMULTIPLY_ALPHA_WEBGL, false)
gl.pixelStorei(gl.UNPACK_FLIP_Y_WEBGL, false)
gl.texSubImage2D(
gl.TEXTURE_2D,
0, // mip level
coord[0],
coord[1],
shape[0],
shape[1],
this.format,
this.type,
data
)
return this
}
function getFormat (gl, channels) {
switch (channels) {
case 1: return gl.LUMINANCE
case 2: return gl.LUMINANCE_ALPHA
case 4: return gl.RGBA
}
throw new Error(
'Invalid pixel format: textures must have 1, 2 or 4 channels, not ' + channels
)
}
Texture2D.fromImage = function (img) {
if (typeof img === 'string') {
var src = img
img = document.createElement('img')
img.src = src
}
var texture = Texture2D()
texture.isLoaded = false
loadImage(img, function (err) {
if (err) throw err
texture.isLoaded = true
texture._loadedWith = img
})
return texture
}