forked from mikeseven/node-webgl
-
Notifications
You must be signed in to change notification settings - Fork 183
Expand file tree
/
Copy pathutils.js
More file actions
249 lines (221 loc) Β· 6.04 KB
/
utils.js
File metadata and controls
249 lines (221 loc) Β· 6.04 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
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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
const { gl } = require('./native-gl')
const { WebGLUniformLocation } = require('./webgl-uniform-location')
function bindPublics (props, wrapper, privateInstance, privateMethods) {
for (let i = 0; i < props.length; i++) {
const prop = props[i]
const value = privateInstance[prop]
if (typeof value === 'function') {
if (privateMethods.indexOf(prop) === -1) {
wrapper[prop] = value.bind(privateInstance)
}
} else {
if (prop[0] === '_' ||
prop[0] === '0' ||
prop[0] === '1') {
continue
}
wrapper[prop] = value
}
}
}
function checkObject (object) {
return typeof object === 'object' ||
(object === undefined)
}
// Works around the fact that "instanceof" doesn't work for polyfilled types
function isInstanceOfType (instance, type) {
return Object.prototype.toString.call(instance) === `[object ${type}]`
}
function checkUniform (program, location) {
return location instanceof WebGLUniformLocation &&
location._program === program &&
location._linkCount === program._linkCount
}
function isTypedArray (data) {
return isInstanceOfType(data, 'Uint8Array') ||
isInstanceOfType(data, 'Uint8ClampedArray') ||
isInstanceOfType(data, 'Int8Array') ||
isInstanceOfType(data, 'Uint16Array') ||
isInstanceOfType(data, 'Int16Array') ||
isInstanceOfType(data, 'Uint32Array') ||
isInstanceOfType(data, 'Int32Array') ||
isInstanceOfType(data, 'Float32Array') ||
isInstanceOfType(data, 'Float64Array')
}
// Don't allow: ", $, `, @, \, ', \0
function isValidString (str) {
// Remove comments first
const c = str.replace(/(?:\/\*(?:[\s\S]*?)\*\/)|(?:([\s;])+\/\/(?:.*)$)/gm, '')
return !(/["$`@\\'\0]/.test(c))
}
function vertexCount (primitive, count) {
switch (primitive) {
case gl.TRIANGLES:
return count - (count % 3)
case gl.LINES:
return count - (count % 2)
case gl.LINE_LOOP:
case gl.POINTS:
return count
case gl.TRIANGLE_FAN:
case gl.LINE_STRIP:
if (count < 2) {
return 0
}
return count
case gl.TRIANGLE_STRIP:
if (count < 3) {
return 0
}
return count
default:
return -1
}
}
function typeSize (type) {
switch (type) {
case gl.UNSIGNED_BYTE:
case gl.BYTE:
return 1
case gl.UNSIGNED_SHORT:
case gl.SHORT:
return 2
case gl.UNSIGNED_INT:
case gl.INT:
case gl.FLOAT:
return 4
}
return 0
}
function uniformTypeSize (type) {
switch (type) {
case gl.BOOL_VEC4:
case gl.INT_VEC4:
case gl.FLOAT_VEC4:
return 4
case gl.BOOL_VEC3:
case gl.INT_VEC3:
case gl.FLOAT_VEC3:
return 3
case gl.BOOL_VEC2:
case gl.INT_VEC2:
case gl.FLOAT_VEC2:
return 2
case gl.BOOL:
case gl.INT:
case gl.FLOAT:
case gl.SAMPLER_2D:
case gl.SAMPLER_CUBE:
return 1
default:
return 0
}
}
function unpackTypedArray (array) {
return (new Uint8Array(array.buffer)).subarray(
array.byteOffset,
array.byteLength + array.byteOffset)
}
function extractImageData (pixels) {
if (typeof pixels === 'object' && typeof pixels.width !== 'undefined' && typeof pixels.height !== 'undefined') {
if (typeof pixels.data !== 'undefined') {
return pixels
}
let context = null
if (typeof pixels.getContext === 'function') {
context = pixels.getContext('2d')
} else if (typeof pixels.src !== 'undefined' && typeof document === 'object' && typeof document.createElement === 'function') {
const canvas = document.createElement('canvas')
if (typeof canvas === 'object' && typeof canvas.getContext === 'function') {
canvas.width = pixels.width
canvas.height = pixels.height
context = canvas.getContext('2d')
if (context !== null) {
context.drawImage(pixels, 0, 0)
}
}
}
if (context !== null) {
return context.getImageData(0, 0, pixels.width, pixels.height)
}
}
return null
}
function convertPixelFormats (ctx, pixels, srcFormat, dstFormat) {
switch (srcFormat) {
case ctx.RGBA:
switch (dstFormat) {
case ctx.RGBA:
return pixels
case ctx.RED:
// extract the red channel from pixels, which is in typed array format, and convert to Uint8Array
return new Uint8Array(pixels.filter((_, i) => i % 4 === 0))
default:
throw new Error('unsupported destination format')
}
default:
throw new Error('unsupported source format')
}
}
function formatSize (internalFormat) {
switch (internalFormat) {
case gl.ALPHA:
case gl.LUMINANCE:
return 1
case gl.LUMINANCE_ALPHA:
return 2
case gl.RGB:
return 3
case gl.RGBA:
return 4
}
return 0
}
function convertPixels (pixels) {
if (typeof pixels === 'object' && pixels !== null) {
if (isInstanceOfType(pixels, 'ArrayBuffer')) {
return new Uint8Array(pixels)
} else if (isInstanceOfType(pixels, 'Uint8Array') ||
isInstanceOfType(pixels, 'Uint16Array') ||
isInstanceOfType(pixels, 'Uint8ClampedArray') ||
isInstanceOfType(pixels, 'Float32Array')) {
return unpackTypedArray(pixels)
} else if (isInstanceOfType(pixels, 'Buffer')) {
return new Uint8Array(pixels)
}
}
return null
}
function checkFormat (format) {
return (
format === gl.ALPHA ||
format === gl.LUMINANCE_ALPHA ||
format === gl.LUMINANCE ||
format === gl.RGB ||
format === gl.RGBA)
}
function validCubeTarget (target) {
return target === gl.TEXTURE_CUBE_MAP_POSITIVE_X ||
target === gl.TEXTURE_CUBE_MAP_NEGATIVE_X ||
target === gl.TEXTURE_CUBE_MAP_POSITIVE_Y ||
target === gl.TEXTURE_CUBE_MAP_NEGATIVE_Y ||
target === gl.TEXTURE_CUBE_MAP_POSITIVE_Z ||
target === gl.TEXTURE_CUBE_MAP_NEGATIVE_Z
}
module.exports = {
bindPublics,
checkObject,
isTypedArray,
isValidString,
vertexCount,
typeSize,
uniformTypeSize,
unpackTypedArray,
extractImageData,
convertPixelFormats,
formatSize,
checkFormat,
checkUniform,
convertPixels,
validCubeTarget
}