-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic.html
More file actions
294 lines (253 loc) · 10.6 KB
/
basic.html
File metadata and controls
294 lines (253 loc) · 10.6 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
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
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title>BASIC WebVR request and exit present</title>
<script src="iframeHelper.js"></script>
<script id="vertex" type="x-shader">
attribute vec2 aVertexPosition;
void main() {
gl_Position = vec4(aVertexPosition, 0.0, 1.0);
}
</script>
<script id="fragment" type="x-shader">
#ifdef GL_ES
precision highp float;
#endif
uniform vec4 uColor;
void main() {
gl_FragColor = uColor;
}
</script>
</head>
<body onload="init()">
<div id="testdiv">
<canvas id="webglcanvas"></canvas>
</div>
<button onclick="requestPresent()">Request Present</button>
<button onclick="exitPresent()">Exit Present</button>
<button onclick="getVRDisplays()">Get VR Displays</button>
<br/>
<button onclick="exitPendingRequestPresent()">Exit pending + request present</button>
<button onclick="requestPendingExitPresent()">Request pending + exit present</button>
<br/>
<button onclick="doubleRequestPresent()">Double request present</button>
<button onclick="doubleExitPresent()">Double exit present</button>
<br/>
<button onclick="printGamepads()">GetGamepads</button>
<br/>
<button onclick="newPage()">New page</button>
<div>
<textarea id="output" rows="20" cols="70"></textarea>
</div>
<script>
var logTextArea = null;
var vrDisplay;
var canvas = null;
var gamepad0;
function newPage() {
}
function printGamepads(){
var gps = navigator.getGamepads();
for ( var i=0; i<gps.length; ++i) {
output("GAMEPAD INDEX " + i);
if (!!gps[i]) {
output("-- Gamepad connected at index " + gps[i].index + " id: " + gps[i].id + " is connected: " + gps[i].connected);
}
else {
output("-- No gamepad at " + i);
}
}
}
// New requestPresent while one is already pending
function doubleRequestPresent(){
window.navigator.getVRDisplays().then(function (displays) {
output("getVRDisplays fulfilled with " + displays.length + " displays");
vrDisplay = displays[0];
var pendingPromise = vrDisplay.requestPresent();
vrDisplay.requestPresent().then(function(){
output("New request present while requestPresent is pending succeeded");
}).catch(function(){
output("New request present while requestPresent is pending failed");
})
pendingPromise.then(function(){
output("Pending request present succeeded.");
}).catch(function(){
output("Pending request present failed.");
})
})
}
// New exitPresent while one is already pending
function doubleExitPresent(){
var pendingPromise = vrDisplay.exitPresent();
vrDisplay.exitPresent().then(function(){
output("New exit present while exitPresent is pending succeeded");
}).catch(function(){
output("New exit present while exitPresent is pending failed");
})
pendingPromise.then(function(){
output("Pending exitPresent succeeded.");
}).catch(function(){
output("Pending exitPresent failed.");
})
}
// New exit present while request is pending
function requestPendingExitPresent(){
var pendingPromise = vrDisplay.requestPresent();
vrDisplay.exitPresent().then(function(){
output("New exit present while requestPresent is pending succeeded");
}).catch(function(){
output("New exit present while requestPresent is pending failed");
})
pendingPromise.then(function(){
output("Pending request present succeeded.");
}).catch(function(){
output("Pending request present failed.");
})
}
// New request present while exit is pending
function exitPendingRequestPresent(){
var pendingPromise = vrDisplay.exitPresent();
vrDisplay.requestPresent().then(function(){
output("New request present while exitPresent is pending succeeded");
}).catch(function(){
output("New request present while exitPresent is pending failed");
})
pendingPromise.then(function(){
output("Pending exit present succeeded.");
}).catch(function(){
output("Pending exit present failed.");
})
}
// getVRDisplays
function getVRDisplays()
{
output("GetVRDisplays");
window.navigator.getVRDisplays().then(function(displays){
output(displays[0]);
})
}
function onAnimationFrame() {
if (!!vrDisplay && vrDisplay.isPresenting) {
animationId = vrDisplay.requestAnimationFrame(onAnimationFrame);
vrDisplay.submitFrame();
}
}
// requestPresent
var animationId;
function requestPresent() {
output("Begin request present...");
window.navigator.getVRDisplays().then(function (displays) {
output("getVRDisplays fulfilled with " + displays.length + " displays");
vrDisplay = displays[0];
vrDisplay.requestPresent([{ source: canvas }]).then(() => {
output("Request present succeeded.");
animationId = vrDisplay.requestAnimationFrame(onAnimationFrame);
},
() => {
output("Request present failed.");
})
});
}
// Exit present
function exitPresent(){
if (!!animationId) {
vrDisplay.cancelAnimationFrame(animationId);
}
vrDisplay.exitPresent().then(function(){
output("Exit present succeeded")
}).catch(function(){
output("Exit present failed")
})
}
// init
function init(){
setupOutputLog();
setupCanvas();
window.addEventListener("gamepadconnected", function (e) {
output("gamepad connected " + e.gamepad.id);
gamepad0 = e.gamepad;
});
window.addEventListener("gamepaddisconnected", function () {
output("gamepad disconnected");
});
window.addEventListener("vrdisplaypresentchange", function () {
output("vrdisplaypresentchange triggered. Is presenting? " + vrDisplay.isPresenting);
});
window.addEventListener("vrdisplaydisconnect", function () {
if (!vrDisplay){
output("vrDisplay is not defined or null, getVRDIsplays to initialize it.");
}
output("vrdisplayDisconnect triggered. isConnected? " + vrDisplay.isConnected);
});
window.addEventListener("vrdisplayconnect", function () {
if (!vrDisplay){
output("vrDisplay is not defined or null, getVRDIsplays to initialize it.");
}
window.navigator.getVRDisplays().then(function (displays) {
output("getVRDisplays fulfilled with " + displays.length + " displays");
vrDisplay = displays[0];
vrDisplay.requestAnimationFrame(onAnimationFrame);
output("vrdisplayConnect triggered. isConnected? " + vrDisplay.isConnected);
});
});
window.addEventListener("vrdisplaypointerrestricted", function () {
output("vrdisplaypointerrestricted triggered.");
});
window.addEventListener("vrdisplaypointerunrestricted", function () {
output("vrdisplaypointerunrestricted triggered.");
});
}
function onAnimationFrame() {
vrDisplay.requestAnimationFrame(onAnimationFrame);
}
// Setup the textarea to output to
function setupOutputLog(){
logTextArea = document.getElementById("output");
logTextArea.value = "";
}
// Output to the text area
function output(s){
logTextArea.value += s + "\n";
logTextArea.scrollTop = logTextArea.scrollHeight;
}
// Setup the canvas
function setupCanvas(){
canvas = document.getElementById("webglcanvas");
var gl = canvas.getContext("experimental-webgl");
gl.viewport(0, 0, canvas.width, canvas.height);
gl.clearColor(0, 0.5, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT);
var v = document.getElementById("vertex").firstChild.nodeValue;
var f = document.getElementById("fragment").firstChild.nodeValue;
var vs = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(vs, v);
gl.compileShader(vs);
var fs = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(fs, f);
gl.compileShader(fs);
program = gl.createProgram();
gl.attachShader(program, vs);
gl.attachShader(program, fs);
gl.linkProgram(program);
var aspect = canvas.width / canvas.height;
var vertices = new Float32Array([
-0.5, 0.5*aspect, 0.5, 0.5*aspect, 0.5,-0.5*aspect, // Triangle 1
-0.5, 0.5*aspect, 0.5,-0.5*aspect, -0.5,-0.5*aspect // Triangle 2
]);
vbuffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, vbuffer);
gl.bufferData(gl.ARRAY_BUFFER, vertices, gl.STATIC_DRAW);
itemSize = 2;
numItems = vertices.length / itemSize;
gl.useProgram(program);
program.uColor = gl.getUniformLocation(program, "uColor");
gl.uniform4fv(program.uColor, [0.0, 0.3, 0.0, 1.0]);
program.aVertexPosition = gl.getAttribLocation(program, "aVertexPosition");
gl.enableVertexAttribArray(program.aVertexPosition);
gl.vertexAttribPointer(program.aVertexPosition, itemSize, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLES, 0, numItems);
}
</script>
</body>
</html>