forked from stackgl/headless-gl
-
Notifications
You must be signed in to change notification settings - Fork 5
/
node_index.js
155 lines (124 loc) Β· 4.16 KB
/
node_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
var bits = require('bit-twiddle')
var webgl = require('./webgl')
var wrap = require('./wrap')
var Canvas = require('canvas')
var CONTEXT_COUNTER = 0
function flag (options, name, dflt) {
if (!options || !(typeof options === 'object') || !(name in options)) {
return dflt
}
return !!options[name]
}
function createContext (width, height, options) {
var i
width = width | 0
height = height | 0
if (!(width > 0 && height > 0)) {
return null
}
var contextAttributes = new webgl.WebGLContextAttributes(
flag(options, 'alpha', true),
flag(options, 'depth', true),
flag(options, 'stencil', false),
false, // flag(options, 'antialias', true),
flag(options, 'premultipliedAlpha', true),
flag(options, 'preserveDrawingBuffer', false),
flag(options, 'preferLowPowerToHighPerformance', false),
flag(options, 'failIfMajorPerformanceCaveat', false))
// Can only use premultipliedAlpha if alpha is set
contextAttributes.premultipliedAlpha =
contextAttributes.premultipliedAlpha && contextAttributes.alpha
var gl
try {
gl = new webgl.WebGLRenderingContext(
1,
1,
contextAttributes.alpha,
contextAttributes.depth,
contextAttributes.stencil,
contextAttributes.antialias,
contextAttributes.premultipliedAlpha,
contextAttributes.preserveDrawingBuffer,
contextAttributes.preferLowPowerToHighPerformance,
contextAttributes.failIfMajorPerformanceCaveat)
} catch (e) {}
if (!gl) {
return null
}
gl.drawingBufferWidth = width
gl.drawingBufferHeight = height
gl._ = CONTEXT_COUNTER++
gl._contextattributes = contextAttributes
gl._extensions = {}
gl._programs = {}
gl._shaders = {}
gl._buffers = {}
gl._textures = {}
gl._framebuffers = {}
gl._renderbuffers = {}
gl._buffersByTarget = {}
gl._activeProgram = null
gl._activeFramebuffer = null
gl._activeArrayBuffer = null
gl._activeElementArrayBuffer = null
gl._activeRenderbuffer = null
// Initialize texture units
var numTextures = gl.getParameter(gl.MAX_COMBINED_TEXTURE_IMAGE_UNITS)
gl._textureUnits = new Array(numTextures)
for (i = 0; i < numTextures; ++i) {
gl._textureUnits[i] = new webgl.WebGLTextureUnit(i)
}
gl._activeTextureUnit = 0
gl.activeTexture(gl.TEXTURE0)
gl._errorStack = []
// Initialize vertex attributes
var numAttribs = gl.getParameter(gl.MAX_VERTEX_ATTRIBS)
gl._vertexAttribs = new Array(numAttribs)
for (i = 0; i < numAttribs; ++i) {
gl._vertexAttribs[i] = new webgl.WebGLVertexAttribute(gl, i)
}
// Store limits
gl._maxTextureSize = gl.getParameter(gl.MAX_TEXTURE_SIZE)
gl._maxTextureLevel = bits.log2(bits.nextPow2(gl._maxTextureSize))
gl._maxCubeMapSize = gl.getParameter(gl.MAX_CUBE_MAP_TEXTURE_SIZE)
gl._maxCubeMapLevel = bits.log2(bits.nextPow2(gl._maxCubeMapSize))
// Unpack alignment
gl._unpackAlignment = 4
gl._packAlignment = 4
// Allocate framebuffer
webgl.allocateDrawingBuffer(gl, width, height)
var attrib0Buffer = gl.createBuffer()
gl._attrib0Buffer = attrib0Buffer
// Initialize defaults
gl.bindBuffer(gl.ARRAY_BUFFER, null)
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null)
gl.bindFramebuffer(gl.FRAMEBUFFER, null)
gl.bindRenderbuffer(gl.RENDERBUFFER, null)
// Set viewport and scissor
gl.viewport(0, 0, width, height)
gl.scissor(0, 0, width, height)
// Clear buffers
gl.clearDepth(1)
gl.clearColor(0, 0, 0, 0)
gl.clearStencil(0)
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT)
return wrap(gl)
}
// Create an adapter layer to emulate a browser and Canvas (using node-canvas)
var oldGetContext = Canvas.prototype.getContext
Canvas.prototype.getContext = function (contextType, contextAttributes) {
if (contextType === '2d') {
return oldGetContext.call(this, contextType, contextAttributes)
} else if (contextType in { 'webgl': true, 'webgl2': true, 'experimental-webgl': true }) {
return createContext(this.width || 1, this.height || 1, contextAttributes)
}
}
global.document = {}
global.document.createElement = function (type) {
if (type !== 'canvas') {
return null
}
return new Canvas()
}
document = global.document
module.exports = createContext