forked from playcanvas/engine
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaugmented-reality-basic.html
More file actions
170 lines (147 loc) · 5.63 KB
/
augmented-reality-basic.html
File metadata and controls
170 lines (147 loc) · 5.63 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
<!DOCTYPE html>
<html>
<head>
<title>PlayCanvas Virtual Reality</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link rel="icon" type="image/png" href="../playcanvas-favicon.png" />
<script src="../../build/output/playcanvas.js"></script>
<style>
body {
margin: 0;
padding: 0;
overflow: hidden;
}
canvas {
width:100%;
height:100%;
}
.message {
position: absolute;
padding: 8px 16px;
left: 20px;
bottom: 0px;
color: #ccc;
background-color: rgba(0, 0, 0, .5);
font-family: "Proxima Nova", Arial, sans-serif;
}
</style>
</head>
<body>
<canvas id="application-canvas"></canvas>
<div>
<p class="message"></p>
</div>
<script>
// draw some axes
var drawAxes = function (pos, scale) {
var color = new pc.Color(1,0,0);
var axis = new pc.Vec3();
var end = new pc.Vec3().copy(pos).add(axis.set(scale,0,0));
app.renderLine(pos, end, color);
color.set(0, 1, 0);
end.sub(axis.set(scale,0,0)).add(axis.set(0,scale,0));
app.renderLine(pos, end, color);
color.set(0, 0, 1);
end.sub(axis.set(0,scale,0)).add(axis.set(0,0,scale));
app.renderLine(pos, end, color);
}
</script>
<script>
var message = function (msg) {
var el = document.querySelector('.message');
el.textContent = msg;
}
var canvas = document.getElementById('application-canvas');
var app = new pc.Application(canvas, {
mouse: new pc.Mouse(canvas),
touch: new pc.TouchDevice(canvas),
keyboard: new pc.Keyboard(window)
});
app.setCanvasFillMode(pc.FILLMODE_FILL_WINDOW);
app.setCanvasResolution(pc.RESOLUTION_AUTO);
window.addEventListener("resize", function () {
app.resizeCanvas(canvas.width, canvas.height);
});
// use device pixel ratio
app.graphicsDevice.maxPixelRatio = window.devicePixelRatio;
app.start();
// create camera
var c = new pc.Entity();
c.addComponent('camera', {
clearColor: new pc.Color(0, 0, 0, 0),
farClip: 10000
});
app.root.addChild(c);
var l = new pc.Entity();
l.addComponent("light", {
type: "spot",
range: 30
});
l.translate(0,10,0);
app.root.addChild(l);
var createCube = function(x,y,z) {
var cube = new pc.Entity();
cube.addComponent("model", {
type: "box",
});
cube.setLocalScale(.5, .5, .5);
cube.translate(x * .5, y, z * .5);
app.root.addChild(cube);
};
// create a grid of cubes
var SIZE = 4;
for (var x = 0; x < SIZE; x++) {
for (var y = 0; y < SIZE; y++) {
createCube(2*x - SIZE, -1, 2*y - SIZE);
}
}
if (app.xr.supported) {
var activate = function () {
if (app.xr.isAvailable(pc.XR_TYPE_IMMERSIVE_AR)) {
c.camera.startXr(pc.XR_TYPE_IMMERSIVE_AR, function (err) {
if (err) message("WebXR Immersive AR failed to start: " + err.message);
});
} else {
message("WebXR Immersive AR is not available");
}
};
app.mouse.on("mousedown", function () {
if (! app.xr.active)
activate();
});
if (app.touch) {
app.touch.on("touchend", function () {
if (! app.xr.active) {
// if not in VR, activate
activate();
} else {
// otherwise reset camera
c.camera.endXr();
}
});
}
// end session by keyboard ESC
app.keyboard.on('keydown', function (evt) {
if (evt.key === pc.KEY_ESCAPE && app.xr.active) {
app.xr.end();
}
});
app.xr.on('start', function () {
message("WebXR Immersive AR session has started");
});
app.xr.on('end', function () {
message("WebXR Immersive AR session has ended");
});
app.xr.on('available:' + pc.XR_TYPE_IMMERSIVE_AR, function (available) {
message("WebXR Immersive AR is now " + (available ? 'available' : 'unavailable'));
});
if (! app.xr.isAvailable(pc.XR_TYPE_IMMERSIVE_AR)) {
message("WebXR Immersive AR is not available");
}
} else {
message("WebXR is not supported");
}
</script>
</body>
</html>