-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathARCoreCameraRenderer.js
375 lines (332 loc) · 9.55 KB
/
ARCoreCameraRenderer.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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
* Copyright 2017 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the 'License')
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an 'AS IS' BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const fragmentSource = `#extension GL_OES_EGL_image_external : require
precision mediump float;
varying vec2 vTextureCoord;
uniform samplerExternalOES uSampler;
void main(void) {
gl_FragColor = texture2D(uSampler, vTextureCoord);
}`
const vertexSource = `attribute vec3 aVertexPosition;
attribute vec2 aTextureCoord;
varying vec2 vTextureCoord;
void main(void) {
gl_Position = vec4(aVertexPosition, 1.0);
vTextureCoord = aTextureCoord;
}`
/**
* Creates and load a shader from a string, type specifies either 'vertex' or 'fragment'
*
* @param {WebGLRenderingContext} gl
* @param {string} str
* @param {string} type
* @return {!WebGLShader}
*/
function getShader(gl, str, type) {
if (type == 'fragment') {
var shader = gl.createShader(gl.FRAGMENT_SHADER)
} else if (type == 'vertex') {
var shader = gl.createShader(gl.VERTEX_SHADER)
} else {
return null
}
gl.shaderSource(shader, str)
gl.compileShader(shader)
const result = gl.getShaderParameter(shader, gl.COMPILE_STATUS)
if (!result) {
console.error(gl.getShaderInfoLog(shader))
return null
}
return shader
}
/**
* Creates a shader program from vertex and fragment shader sources
*
* @param {WebGLRenderingContext} gl
* @param {string} vs
* @param {string} fs
* @return {!WebGLProgram}
*/
function getProgram(gl, vs, fs) {
const vertexShader = getShader(gl, vs, 'vertex')
const fragmentShader = getShader(gl, fs, 'fragment')
if (!fragmentShader) {
return null
}
const shaderProgram = gl.createProgram()
gl.attachShader(shaderProgram, vertexShader)
gl.attachShader(shaderProgram, fragmentShader)
gl.linkProgram(shaderProgram)
const result = gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)
if (!result) {
console.error('Could not initialise arview shaders')
}
return shaderProgram
}
/**
* Calculate the correct orientation depending on the device and the camera
* orientations.
*
* @param {number} screenOrientation
* @param {number} seeThroughCameraOrientation
* @return {number}
*/
function combineOrientations(screenOrientation, seeThroughCameraOrientation) {
let seeThroughCameraOrientationIndex = 0
switch (seeThroughCameraOrientation) {
case 90:
seeThroughCameraOrientationIndex = 1
break
case 180:
seeThroughCameraOrientationIndex = 2
break
case 270:
seeThroughCameraOrientationIndex = 3
break
default:
seeThroughCameraOrientationIndex = 0
break
}
let screenOrientationIndex = 0
switch (screenOrientation) {
case 90:
screenOrientationIndex = 1
break
case 180:
screenOrientationIndex = 2
break
case 270:
screenOrientationIndex = 3
break
default:
screenOrientationIndex = 0
break
}
let ret = screenOrientationIndex - seeThroughCameraOrientationIndex
if (ret < 0) {
ret += 4
}
return ret % 4
}
/**
* Renders the ar camera's video texture
*/
class ARVideoRenderer {
/**
* @param {VRDisplay} vrDisplay
* @param {WebGLRenderingContext} gl
*/
constructor(vrDisplay, gl) {
this.vrDisplay = vrDisplay
this.gl = gl
this.passThroughCamera = vrDisplay.getPassThroughCamera()
this.program = getProgram(gl, vertexSource, fragmentSource)
gl.useProgram(this.program)
// Setup a quad
this.vertexPositionAttribute = gl.getAttribLocation(
this.program,
'aVertexPosition'
)
this.textureCoordAttribute = gl.getAttribLocation(
this.program,
'aTextureCoord'
)
this.samplerUniform = gl.getUniformLocation(this.program, 'uSampler')
this.vertexPositionBuffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexPositionBuffer)
let vertices = [
-1.0,
1.0,
0.0,
-1.0,
-1.0,
0.0,
1.0,
1.0,
0.0,
1.0,
-1.0,
0.0,
]
let f32Vertices = new Float32Array(vertices)
gl.bufferData(gl.ARRAY_BUFFER, f32Vertices, gl.STATIC_DRAW)
this.vertexPositionBuffer.itemSize = 3
this.vertexPositionBuffer.numItems = 12
this.textureCoordBuffer = gl.createBuffer()
gl.bindBuffer(gl.ARRAY_BUFFER, this.textureCoordBuffer)
// Precalculate different texture UV coordinates depending on the possible
// orientations of the device depending if there is a VRDisplay or not
let textureCoords = null
if (this.vrDisplay) {
let u =
this.passThroughCamera.width / this.passThroughCamera.textureWidth
let v =
this.passThroughCamera.height / this.passThroughCamera.textureHeight
textureCoords = [
[0.0, 0.0, 0.0, v, u, 0.0, u, v],
[u, 0.0, 0.0, 0.0, u, v, 0.0, v],
[u, v, u, 0.0, 0.0, v, 0.0, 0.0],
[0.0, v, u, v, 0.0, 0.0, u, 0.0],
]
} else {
textureCoords = [
[0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0, 1.0],
[1.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0, 1.0],
[1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0],
[0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0],
]
}
this.f32TextureCoords = []
for (let i = 0; i < textureCoords.length; i++) {
this.f32TextureCoords.push(new Float32Array(textureCoords[i]))
}
// Store the current combined orientation to check if it has changed
// during the update calls and use the correct texture coordinates.
this.combinedOrientation = combineOrientations(
screen.orientation.angle,
this.passThroughCamera.orientation
)
gl.bufferData(
gl.ARRAY_BUFFER,
this.f32TextureCoords[this.combinedOrientation],
gl.STATIC_DRAW
)
this.textureCoordBuffer.itemSize = 2
this.textureCoordBuffer.numItems = 8
gl.bindBuffer(gl.ARRAY_BUFFER, null)
this.indexBuffer = gl.createBuffer()
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer)
let indices = [0, 1, 2, 2, 1, 3]
let ui16Indices = new Uint16Array(indices)
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, ui16Indices, gl.STATIC_DRAW)
this.indexBuffer.itemSize = 1
this.indexBuffer.numItems = 6
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null)
this.texture = gl.createTexture()
gl.useProgram(null)
// The projection matrix will be based on an identify orthographic camera
this.projectionMatrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
this.mvMatrix = [1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1]
return this
}
/**
* Renders the quad
*/
render() {
let gl = this.gl
gl.useProgram(this.program)
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexPositionBuffer)
gl.enableVertexAttribArray(this.vertexPositionAttribute)
gl.vertexAttribPointer(
this.vertexPositionAttribute,
this.vertexPositionBuffer.itemSize,
gl.FLOAT,
false,
0,
0
)
gl.bindBuffer(gl.ARRAY_BUFFER, this.textureCoordBuffer)
// Check the current orientation of the device combined with the
// orientation of the VRSeeThroughCamera to determine the correct UV
// coordinates to be used.
let combinedOrientation = combineOrientations(
screen.orientation.angle,
this.passThroughCamera.orientation
)
if (combinedOrientation !== this.combinedOrientation) {
this.combinedOrientation = combinedOrientation
gl.bufferData(
gl.ARRAY_BUFFER,
this.f32TextureCoords[this.combinedOrientation],
gl.STATIC_DRAW
)
}
gl.enableVertexAttribArray(this.textureCoordAttribute)
gl.vertexAttribPointer(
this.textureCoordAttribute,
this.textureCoordBuffer.itemSize,
gl.FLOAT,
false,
0,
0
)
gl.activeTexture(gl.TEXTURE0)
gl.bindTexture(gl.TEXTURE_EXTERNAL_OES, this.texture)
// Update the content of the texture in every frame.
gl.texImage2D(
gl.TEXTURE_EXTERNAL_OES,
0,
gl.RGB,
gl.RGB,
gl.UNSIGNED_BYTE,
this.passThroughCamera
)
gl.uniform1i(this.samplerUniform, 0)
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, this.indexBuffer)
gl.drawElements(
gl.TRIANGLES,
this.indexBuffer.numItems,
gl.UNSIGNED_SHORT,
0
)
// Disable enabled states to allow other render calls to correctly work
gl.bindTexture(gl.TEXTURE_EXTERNAL_OES, null)
gl.disableVertexAttribArray(this.vertexPositionAttribute)
gl.disableVertexAttribArray(this.textureCoordAttribute)
gl.bindBuffer(gl.ARRAY_BUFFER, null)
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, null)
gl.useProgram(null)
}
}
/**
* A helper class that takes a VRDisplay with AR capabilities
* and renders the see through camera to the passed in WebGL context.
*/
export default class ARCoreCameraRenderer {
constructor(vrDisplay, gl) {
this.vrDisplay = vrDisplay
this.gl = gl
this.videoRenderer = new ARVideoRenderer(vrDisplay, this.gl)
// Cache the width/height so we're not potentially forcing
// a reflow if there's been a style invalidation
this.width = window.innerWidth
this.height = window.innerHeight
window.addEventListener('resize', this.onWindowResize.bind(this), false)
}
/**
* Updates the stored width/height of window on resize.
*/
onWindowResize() {
this.width = window.innerWidth
this.height = window.innerHeight
}
/**
* Renders the see through camera to the passed in gl context
*/
render() {
let gl = this.gl
let dpr = 1
let width = this.width * dpr
let height = this.height * dpr
if (gl.viewportWidth !== width) {
gl.viewportWidth = width
}
if (gl.viewportHeight !== height) {
gl.viewportHeight = height
}
this.gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight)
this.videoRenderer.render()
}
}