-
Notifications
You must be signed in to change notification settings - Fork 0
/
testing5.html
335 lines (275 loc) · 13.2 KB
/
testing5.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
<!DOCTYPE html>
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<title>A cube of pollution</title>
<!--redirect favicon-->
<link rel="shortcut icon" href="./img/favicon.ico">
<!-- three.js library -->
<script src='./js/ThreeJS/three.js'></script>
<!--Additional Libraries-->
<!-- States frame rate monitor -->
<!-- <script src="js/PreformanceMonitor/stats.min.js"></script> -->
<!-- ar.js -->
<script src="./js/ARJS/ar-threex.js"></script>
<!--Set Base URL for the THEEx Library to refernece normally '../' -->
<script>THREEx.ArToolkitContext.baseURL = './js/'</script>
<body style='margin : 0px; overflow: hidden;'>
<div style='position: absolute; top: 10px; width:100%; text-align: center; z-index: 1;'>
<div style="font-size: 20pt; text-align: center;"><label><a href="https://www.csustan.edu/warrior-fab-lab/one-metric-ton-carbon-emissions-project">Click For Info on Carbon Footprints</a></label></div>
</div>
<script>
//////////////////////////////////////////////////////////////////////////////////
// Init
//////////////////////////////////////////////////////////////////////////////////
// init renderer
var renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true
});
renderer.setClearColor(new THREE.Color('lightgrey'), 0)
//renderer.setSize(640, 480);
renderer.setSize(window.innerWidth, window.innerHeight);
renderer.domElement.style.position = 'absolute'
renderer.domElement.style.top = '0px'
renderer.domElement.style.left = '0px'
//renderer.domElement.style.zIndex = '1';
document.body.appendChild(renderer.domElement);
// array of functions for the rendering loop
var onRenderFcts = [];
var arToolkitContext, arMarkerControls, markerRoot;
// init scene and camera
var scene = new THREE.Scene();
//////////////////////////////////////////////////////////////////////////////////
// Initialize a basic camera
//////////////////////////////////////////////////////////////////////////////////
// Create a camera
var camera = new THREE.Camera();
scene.add(camera);
markerRoot = new THREE.Group
scene.add(markerRoot)
////////////////////////////////////////////////////////////////////////////////
// handle arToolkitSource
////////////////////////////////////////////////////////////////////////////////
var arToolkitSource = new THREEx.ArToolkitSource({
// to read from the webcam
sourceType: 'webcam',
sourceWidth: window.innerWidth > window.innerHeight ? 640 : 480,
sourceHeight: window.innerWidth > window.innerHeight ? 480 : 640,
// // to read from an image
// sourceType : 'image',
// sourceUrl : THREEx.ArToolkitContext.baseURL + '../data/images/img.jpg',
// to read from a video
// sourceType : 'video',
// sourceUrl : THREEx.ArToolkitContext.baseURL + '../data/videos/headtracking.mp4',
})
arToolkitSource.init(function onReady() {
//initARContext()
//onResize()
arToolkitSource.domElement.addEventListener('canplay', () => {
console.log(
'canplay',
'actual source dimensions',
arToolkitSource.domElement.videoWidth,
arToolkitSource.domElement.videoHeight
);
initARContext();
});
window.arToolkitSource = arToolkitSource;
setTimeout(() => {
onResize()
}, 2000);
})
// arToolkitSource.init(function onReady() {
// arToolkitSource.domElement.addEventListener('canplay', () => {
// console.log(
// 'canplay',
// 'actual source dimensions',
// arToolkitSource.domElement.videoWidth,
// arToolkitSource.domElement.videoHeight
// );
//
// initARContext();
// });
// window.arToolkitSource = arToolkitSource;
// setTimeout(() => {
// onResize()
// }, 2000);
// })
// handle resize
window.addEventListener('resize', function () {
onResize()
})
function onResize() {
arToolkitSource.onResizeElement()
arToolkitSource.copyElementSizeTo(renderer.domElement)
if (window.arToolkitContext) { //The arToolKitContext is generated in a differnt function, so check that the arToolkitContext is actually on the Window before searching it.
if (window.arToolkitContext.arController !== null) {
arToolkitSource.copyElementSizeTo(window.arToolkitContext.arController.canvas)
}
}
}
////////////////////////////////////////////////////////////////////////////////
// initialize arToolkitContext
////////////////////////////////////////////////////////////////////////////////
function initARContext() { // create atToolkitContext
arToolkitContext = new THREEx.ArToolkitContext({
cameraParametersUrl: THREEx.ArToolkitContext.baseURL + 'data/camera_para.dat',
detectionMode: 'mono'
})
// initialize it
arToolkitContext.init(function onCompleted() {
// copy projection matrix to camera
camera.projectionMatrix.copy(arToolkitContext.getProjectionMatrix());
arToolkitContext.arController.orientation = getSourceOrientation();
arToolkitContext.arController.options.orientation = getSourceOrientation();
console.log('arToolkitContext', arToolkitContext);
window.arToolkitContext = arToolkitContext;
})
artoolkitMarker = new THREEx.ArMarkerControls(arToolkitContext, markerRoot, {
type: 'pattern',
patternUrl: THREEx.ArToolkitContext.baseURL + 'data/lambda.patt',
minConfidence: 0.2, //(0-1) the "confidence" percentage before the app considered a Marker to be a marker. Low numbers means it will track about anything, high numbers means that it wants to be sure before it bothers.
smooth: true,
// number of matrices to smooth tracking over, more = smoother but slower follow
smoothCount: 5, //15, //(1-20) The number of frames that the tracking data is averaged to smooth out the marker's position, rotation, and scale.
// distance tolerance for smoothing, if smoothThreshold # of matrices are under tolerance, tracking will stay still
smoothTolerance: .75, //.05 //0.01, //skippy stutter controler -- a value between .01 and 1. High values priotize stability and smoothness over responsiveness
// threshold for smoothing, will keep still unless enough matrices are over tolerance
smoothThreshold: 3 //7 //2 //Tracking Update/Wobble control -- think of it as the number of frames the marker needs to appear before a tracking update occurs. About ten per second in this app. This should be higher than Smooth Tolerance
})
}
// arToolkitContext.init(() => { // copy projection matrix to camera
// camera.projectionMatrix.copy(arToolkitContext.getProjectionMatrix());
// arToolkitContext.arController.orientation = getSourceOrientation();
// arToolkitContext.arController.options.orientation = getSourceOrientation();
// console.log('arToolkitContext', arToolkitContext);
// window.arToolkitContext = arToolkitContext;
// })
// // MARKER
// arMarkerControls = new THREEx.ArMarkerControls(arToolkitContext, camera, {
// type: 'pattern',
// patternUrl : THREEx.ArToolkitContext.baseURL + 'data/lambda.patt',
// // as we controls the camera, set changeMatrixMode: 'cameraTransformMatrix'
// changeMatrixMode: 'cameraTransformMatrix'
// })
// scene.visible = false
// console.log('ArMarkerControls', arMarkerControls);
// window.arMarkerControls = arMarkerControls;
//}
function getSourceOrientation() {
if (!arToolkitSource) {
return null;
}
console.log(
'actual source dimensions',
arToolkitSource.domElement.videoWidth,
arToolkitSource.domElement.videoHeight
);
if (arToolkitSource.domElement.videoWidth > arToolkitSource.domElement.videoHeight) {
console.log('source orientation', 'landscape');
return 'landscape';
} else {
console.log('source orientation', 'portrait');
return 'portrait';
}
}
// update artoolkit on every frame
onRenderFcts.push(function () {
if (!arToolkitContext || !arToolkitSource || !arToolkitSource.ready) {
return;
}
arToolkitContext.update(arToolkitSource.domElement)
// // update scene.visible if the marker is seen
//scene.visible = camera.visible
})
////////////////////////////////////////////////////////////////////////////////
// Create a ArMarkerControls
////////////////////////////////////////////////////////////////////////////////
var markerRoot = new THREE.Group
scene.add(markerRoot)
// build a smoothedControls
var smoothedRoot = new THREE.Group()
scene.add(smoothedRoot)
var smoothedControls = new THREEx.ArSmoothedControls(smoothedRoot, {
lerpPosition: .1,//.5, //Stutter control (.1 - 1) -- the lower it is, the looser the tracking and the smoother the updates
lerpQuaternion: 0.5, //jiggle control on rotation -- the lower the value the looser the tracking nad the soother the update
lerpScale: .5, //Scale jitter control, the lower the number, the looser the scale tracking and the soother the scaling
//Defaults:
//lerpPosition: 0.4,
//lerpQuaternion: 0.3,
//lerpScale: 1,
})
onRenderFcts.push(function (delta) {
smoothedControls.update(markerRoot)
})
//////////////////////////////////////////////////////////////////////////////////
// add an object in the scene
//////////////////////////////////////////////////////////////////////////////////
//const loader = new THREE.CubeTextureLoader();
//loader.setPath( 'img/numbers/' );
//const textureCube = loader.load( [
// '1_350px-350px.png', '2_350px-350px.png',
// '3_350px-350px.png', '4_350px-350px.png',
// '5_350px-350px.png', '6_350px-350px.png'
// ]
//);
//const imageMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube, transparent: true, opacity: .5, side: THREE.DoubleSide } );
// //const imageMaterial = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube } );
var arWorldRoot = smoothedRoot;
const textureOpacity = 0.75;
var cubeMaterial = [
new THREE.MeshBasicMaterial({ map: new THREE.TextureLoader().load('./img/CarbonTexture_01.png'), transparent: true, opacity: textureOpacity, side: THREE.FrontSide }), //RIGHT SIDE
new THREE.MeshBasicMaterial({ map: new THREE.TextureLoader().load('./img/CarbonTexture_01.png'), transparent: true, opacity: textureOpacity, side: THREE.FrontSide }), //LEFT SIDE
new THREE.MeshBasicMaterial({ map: new THREE.TextureLoader().load('./img/CarbonTexture_01.png'), transparent: true, opacity: textureOpacity, side: THREE.FrontSide }), //TOP SIDE
new THREE.MeshBasicMaterial({ map: new THREE.TextureLoader().load('./img/CarbonTexture_01.png'), transparent: true, opacity: textureOpacity, side: THREE.FrontSide }), //BOTTOM SIDE
new THREE.MeshBasicMaterial({ map: new THREE.TextureLoader().load('./img/CarbonTexture_01.png'), transparent: true, opacity: textureOpacity, side: THREE.FrontSide }), //FRONT SIDE
new THREE.MeshBasicMaterial({ map: new THREE.TextureLoader().load('./img/CarbonTexture_01.png'), transparent: true, opacity: textureOpacity, side: THREE.FrontSide }) //BACK SIDE
];
var imageMaterial2 = new THREE.MeshFaceMaterial( cubeMaterial );
//var geometry = new THREE.BoxGeometry(16.875, 16.875, 16.875);
var geometry = new THREE.BoxGeometry(17.053, 17.053, 17.053); // the cube should be 27 feet (324 inches), and the unit size is 19", so scaling it by 17.053 should get us there.
var material = new THREE.MeshNormalMaterial({
transparent: true,
opacity: .95,
side: THREE.DoubleSide
});
//var mesh = new THREE.Mesh(geometry, material);
//mesh.position.y = geometry.parameters.height / 2
var cubeMesh = new THREE.Mesh(geometry, imageMaterial2);
//var cubeMesh = new THREE.Mesh(geometry, imageMaterial);
//cubeMesh.position.y = geometry.parameters.height / 2
//cubeMesh.position.set(x, y, z) //cubeMesh.position.set( 0, -8, -6.5); //with a 16 unit cube and a three foot A-frame
cubeMesh.position.set( 0, -10, -8.5); // The cube is about 17 units, and needs to have the A-frame about in the bottom middle
//the new marker with the printed marker is about four feet off the ground.
//scene.add(cubeMesh);
arWorldRoot.add(cubeMesh);
onRenderFcts.push(function () {
cubeMesh.rotation.z += 0.001
})
//////////////////////////////////////////////////////////////////////////////////
// render the whole thing on the page
//////////////////////////////////////////////////////////////////////////////////
//if issues with frame rate occur, add a stats box to monitor it:
//var stats = new Stats(); //test code
//document.body.appendChild(stats.dom); //Test Code
// render the scene
onRenderFcts.push(function () {
renderer.render(scene, camera);
//stats.update(); //Test Code
})
console.dir(onRenderFcts); //test code
// run the rendering loop
var lastTimeMsec = null
requestAnimationFrame(function animate(nowMsec) {
// keep looping
requestAnimationFrame(animate);
// measure time
lastTimeMsec = lastTimeMsec || nowMsec - 1000 / 60
var deltaMsec = Math.min(200, nowMsec - lastTimeMsec)
lastTimeMsec = nowMsec
// call each update function
onRenderFcts.forEach(function (onRenderFct) {
onRenderFct(deltaMsec / 1000, nowMsec / 1000)
})
})
</script>
</body>