-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoblikodAR.html
377 lines (325 loc) · 13.7 KB
/
oblikodAR.html
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
376
377
<!--
/*
* 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.
*/
-->
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.ar.js - Spawn At Camera</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no,
minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: monospace;
margin: 0;
overflow: hidden;
position: fixed;
width: 100%;
height: 100vh;
-webkit-user-select: none;
user-select: none;
}
#info {
position: absolute;
left: 50%;
bottom: 0;
transform: translate(-50%, 0);
margin: 1em;
z-index: 10;
display: block;
width: 100%;
line-height: 2em;
text-align: center;
}
#info * {
color: #fff;
}
.title {
background-color: rgba(40, 40, 40, 0.4);
padding: 0.4em 0.6em;
border-radius: 0.1em;
}
.links {
background-color: rgba(40, 40, 40, 0.6);
padding: 0.4em 0.6em;
border-radius: 0.1em;
}
canvas {
position: absolute;
top: 0;
left: 0;
}
</style>
</head>
<body>
<div id="info">
<span class="title">Tap to spawn objects at camera position.</span>
<br/>
<!-- <span class="links">
<a href="https://github.com/google-ar/three.ar.js">three.ar.js</a>
<a href="https://developers.google.com/ar/develop/web/getting-started#examples">examples</a>
</span> -->
</div>
<script src="js/three.js"></script>
<script src="js/VRControls.js"></script>
<script src="js/three.ar.js"></script>
<script>
//UTILITY
function getQueryVariable(variable) {
var query = window.location.search.substring(1);
var vars = query.split("&");
for (var i = 0; i < vars.length; i++) {
var pair = vars[i].split("=");
if (pair[0] == variable) { return pair[1]; }
}
return (false);
}
var N = parseInt(getQueryVariable("N")) || 5;
var H = parseFloat(getQueryVariable("H")) || 0.4;
var R = parseFloat(getQueryVariable("R")) || 0.6;
var D = 0.02;
var mat = parseInt(getQueryVariable("mat")) || 1;
var changed = true;
var vrDisplay;
var vrFrameData;
var vrControls;
var arView;
var canvas;
var camera;
var scene;
var renderer;
var model;
var colors = [
new THREE.Color(0xffffff),
new THREE.Color(0xffff00),
new THREE.Color(0xff00ff),
new THREE.Color(0xff0000),
new THREE.Color(0x00ffff),
new THREE.Color(0x00ff00),
new THREE.Color(0x0000ff),
new THREE.Color(0x000000)
];
/**
* Use the `getARDisplay()` utility to leverage the WebVR API
* to see if there are any AR-capable WebVR VRDisplays. Returns
* a valid display if found. Otherwise, display the unsupported
* browser message.
*/
THREE.ARUtils.getARDisplay().then(function (display) {
//if (display) {
if (true){
vrFrameData = new VRFrameData();
vrDisplay = display;
init();
} else {
THREE.ARUtils.displayUnsupportedMessage();
}
});
function init() {
// Turn on the debugging panel
var arDebug = new THREE.ARDebug(vrDisplay);
document.body.appendChild(arDebug.getElement());
// Setup the three.js rendering environment
renderer = new THREE.WebGLRenderer({ alpha: true });
renderer.setPixelRatio(window.devicePixelRatio);
console.log('setRenderer size', window.innerWidth, window.innerHeight);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.autoClear = false;
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFSoftShadowMap;
canvas = renderer.domElement;
document.body.appendChild(canvas);
scene = new THREE.Scene();
// Creating the ARView, which is the object that handles
// the rendering of the camera stream behind the three.js
// scene
arView = new THREE.ARView(vrDisplay, renderer);
// The ARPerspectiveCamera is very similar to THREE.PerspectiveCamera,
// except when using an AR-capable browser, the camera uses
// the projection matrix provided from the device, so that the
// perspective camera's depth planes and field of view matches
// the physical camera on the device.
camera = new THREE.ARPerspectiveCamera(
vrDisplay,
60,
window.innerWidth / window.innerHeight,
vrDisplay.depthNear,
vrDisplay.depthFar
);
// VRControls is a utility from three.js that applies the device's
// orientation/position to the perspective camera, keeping our
// real world and virtual world in sync.
vrControls = new THREE.VRControls(camera);
// Create the cube geometry that we'll copy and place in the
// scene when the user clicks the screen
scene.add( new THREE.AmbientLight(0xffffff,0.8));
rebuild();
// Bind our event handlers
window.addEventListener('resize', onWindowResize, false);
canvas.addEventListener('touchstart', onClick, false);
// Kick off the render loop!
update();
}
/**
* The render loop, called once per frame. Handles updating
* our scene and rendering.
*/
function update() {
// Clears color from the frame before rendering the camera (arView) or scene.
renderer.clearColor();
// Render the device's camera stream on screen first of all.
// It allows to get the right pose synchronized with the right frame.
arView.render();
// Update our camera projection matrix in the event that
// the near or far planes have updated
camera.updateProjectionMatrix();
// From the WebVR API, populate `vrFrameData` with
// updated information for the frame
vrDisplay.getFrameData(vrFrameData);
// Update our perspective camera's positioning
vrControls.update();
// Render our three.js virtual scene
renderer.clearDepth();
renderer.render(scene, camera);
// Kick off the requestAnimationFrame to call this function
// when a new VRDisplay frame is rendered
vrDisplay.requestAnimationFrame(update);
}
/**
* On window resize, update the perspective camera's aspect ratio,
* and call `updateProjectionMatrix` so that we can get the latest
* projection matrix provided from the device
*/
function onWindowResize() {
console.log('setRenderer size', window.innerWidth, window.innerHeight);
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
}
/**
* When clicking on the screen, create a cube at the user's
* current position.
*/
function onClick(e) {
// If we don't have a touches object, abort
// TODO: is this necessary?
if (!e.touches[0]) {
return;
}
// Inspect the event object and generate normalize screen coordinates
// (between 0 and 1) for the screen position.
var x = e.touches[0].pageX / window.innerWidth;
var y = e.touches[0].pageY / window.innerHeight;
// Send a ray from the point of click to the real world surface
// and attempt to find a hit. `hitTest` returns an array of potential
// hits.
var hits = vrDisplay.hitTest(x, y);
// If a hit is found, just use the first one
if (hits && hits.length) {
var hit = hits[0];
// Use the `placeObjectAtHit` utility to position
// the cube where the hit occurred
THREE.ARUtils.placeObjectAtHit(model, // The object to place
hit, // The VRHit object to move the cube to
1, // Easing value from 0 to 1; we want to move
// the cube directly to the hit position
true); // Whether or not we also apply orientation
model.rotateX(Math.PI / 2);
}
}
function rebuild() {
//material
var textureLoader = new THREE.TextureLoader();
var material = new THREE.MeshLambertMaterial({ wireframe: false });
var texture = textureLoader.load("./img/"+mat+".jpg");
texture.wrapS = THREE.RepeatWrapping;
texture.wrapT = THREE.RepeatWrapping;
texture.repeat.set(1, 1);
material.map = texture;
//extrude settings
var extrudeSettings = {
steps: 1,
amount: D,
bevelEnabled: false
};
var mGrupa = new THREE.Group();
mGrupa.name = 'mg';
if (changed) {
var obj = scene.getObjectByName('mg');
scene.remove(obj);
//obj.dispose();
changed = false;
}
// osnovne tocke
var kut = Math.PI * 2 / N;
var tocke = [];
for (var i = 0; i < N; i++) {
tocke.push(new THREE.Vector2(Math.sin(kut * i) * R, Math.cos(kut * i) * R));
}
//ploca
var ploca2d = new THREE.Shape();
ploca2d.fromPoints(tocke);
var plocaExtrude = new THREE.ExtrudeBufferGeometry(ploca2d, extrudeSettings);
plocaExtrude.groups[1].materialIndex = 0;
plocaExtrude.addGroup(plocaExtrude.groups[0].start, plocaExtrude.groups[1].count, 1);
var ploca = new THREE.Mesh(plocaExtrude, material);
ploca.castShadow = true;
mGrupa.add(ploca);
//noge
for (var j = 0; j < N; j++) {
var t1 = new THREE.Vector3(tocke[j].x, tocke[j].y, D);
if (j != N - 1)
var t2 = new THREE.Vector3(tocke[j + 1].x, tocke[j + 1].y, D);
else
var t2 = new THREE.Vector3(tocke[0].x, tocke[0].y, D);
var tempVec = new THREE.Vector3(
(t1.x + t2.x) / 2,
(t1.y + t2.y) / 2,
0);
var vec = tempVec.clone();
vec.normalize().multiplyScalar(H).add(tempVec);
var noga2d = new THREE.Shape([t1, vec, t2]);
var nogaExtrude = new THREE.ExtrudeGeometry(noga2d, extrudeSettings);
var noga = new THREE.Mesh(nogaExtrude, material);
//var pivot=new THREE.Object3D();
noga.translateOnAxis(new THREE.Vector3().copy(tempVec).normalize(), tempVec.length());
//pivot.add(noga);
var axis = new THREE.Vector3().subVectors(t2, t1);
axis.normalize();
noga.rotateOnAxis(axis, Math.PI / 2);
noga.position.z = - tempVec.length() + D;
// noga.updateMatrix();
noga.castShadow = true;
mGrupa.add(noga);
}
//mGrupa.rotateX(Math.PI / 2);
mGrupa.translateZ(-H - D);
mGrupa.castShadow = true;
var light= new THREE.PointLight ( 0xefefef, 0.8,4,2);
light.position.z= -2;
light.castShadow=true;
mGrupa.add( light );
var tlo= new THREE.Mesh( new THREE.PlaneGeometry(3,3,3,3), new THREE.ShadowMaterial({opacity:0.5}) );
tlo.rotateX(Math.PI);
tlo.position.z=H+D;
tlo.receiveShadow = true;
mGrupa.add(tlo);
model = mGrupa;
scene.add(model);
}
</script>
</body>
</html>