|
| 1 | +var ar; |
| 2 | +function isMobile() { |
| 3 | + return /Android|mobile|iPad|iPhone/i.test(navigator.userAgent); |
| 4 | +} |
| 5 | + |
| 6 | +var setMatrix = function (matrix, value) { |
| 7 | + var array = []; |
| 8 | + for (var key in value) { |
| 9 | + array[key] = value[key]; |
| 10 | + } |
| 11 | + if (typeof matrix.elements.set === "function") { |
| 12 | + matrix.elements.set(array); |
| 13 | + } else { |
| 14 | + matrix.elements = [].slice.call(array); |
| 15 | + } |
| 16 | +}; |
| 17 | + |
| 18 | +function start( markerUrl, video, input_width, input_height, canvas_draw, render_update) { |
| 19 | + var vw, vh; |
| 20 | + var sw, sh; |
| 21 | + var pscale, sscale; |
| 22 | + var w, h; |
| 23 | + var pw, ph; |
| 24 | + var ox, oy; |
| 25 | + var camera_para = './../examples/Data/camera_para.dat'; |
| 26 | + var camera_matrix; |
| 27 | + |
| 28 | + var canvas_process = document.createElement('canvas'); |
| 29 | + var context_process = canvas_process.getContext('2d', { willReadFrequently: true }); |
| 30 | + |
| 31 | + var renderer = new THREE.WebGLRenderer({ canvas: canvas_draw, alpha: true, antialias: true }); |
| 32 | + renderer.setPixelRatio(window.devicePixelRatio); |
| 33 | + |
| 34 | + var scene = new THREE.Scene(); |
| 35 | + |
| 36 | + var camera = new THREE.Camera(); |
| 37 | + camera.matrixAutoUpdate = false; |
| 38 | + |
| 39 | + scene.add(camera); |
| 40 | + |
| 41 | + var sphere = new THREE.Mesh( |
| 42 | + new THREE.SphereGeometry(0.5, 8, 8), |
| 43 | + new THREE.MeshNormalMaterial() |
| 44 | + ); |
| 45 | + |
| 46 | + var root = new THREE.Object3D(); |
| 47 | + scene.add(root); |
| 48 | + |
| 49 | + var marker; |
| 50 | + |
| 51 | + var markerInfos = function () { |
| 52 | + window.addEventListener("markerInfos", function (ev) { |
| 53 | + marker = ev.detail.marker; |
| 54 | + }) |
| 55 | + } |
| 56 | + |
| 57 | + var getCameraMatrix = function () { |
| 58 | + window.addEventListener("loaded", function (ev) { |
| 59 | + camera_matrix = ev.detail.proj; |
| 60 | + }) |
| 61 | + } |
| 62 | + |
| 63 | + sphere.material.flatShading; |
| 64 | + sphere.scale.set(200, 200, 200); |
| 65 | + |
| 66 | + root.matrixAutoUpdate = false; |
| 67 | + root.add(sphere); |
| 68 | + |
| 69 | + var load = function () { |
| 70 | + vw = input_width; |
| 71 | + vh = input_height; |
| 72 | + |
| 73 | + pscale = 320 / Math.max(vw, vh / 3 * 4); |
| 74 | + sscale = isMobile() ? window.outerWidth / input_width : 1; |
| 75 | + |
| 76 | + sw = vw * sscale; |
| 77 | + sh = vh * sscale; |
| 78 | + |
| 79 | + w = vw * pscale; |
| 80 | + h = vh * pscale; |
| 81 | + pw = Math.max(w, h / 3 * 4); |
| 82 | + ph = Math.max(h, w / 4 * 3); |
| 83 | + ox = (pw - w) / 2; |
| 84 | + oy = (ph - h) / 2; |
| 85 | + canvas_process.style.clientWidth = pw + "px"; |
| 86 | + canvas_process.style.clientHeight = ph + "px"; |
| 87 | + canvas_process.width = pw; |
| 88 | + canvas_process.height = ph; |
| 89 | + |
| 90 | + renderer.setSize(sw, sh); |
| 91 | + |
| 92 | + var msg = { |
| 93 | + pw: pw, |
| 94 | + ph: ph, |
| 95 | + camera_para: camera_para, |
| 96 | + marker: markerUrl |
| 97 | + } |
| 98 | + |
| 99 | + load_thread(msg); |
| 100 | + markerInfos(); |
| 101 | + getCameraMatrix(); |
| 102 | + }; |
| 103 | + |
| 104 | + var world; |
| 105 | + |
| 106 | + var found = function () { |
| 107 | + window.addEventListener("markerFound", function (ev) { |
| 108 | + world = ev.detail.matrixGL_RH |
| 109 | + }) |
| 110 | + }; |
| 111 | + |
| 112 | + var lasttime = Date.now(); |
| 113 | + var time = 0; |
| 114 | + |
| 115 | + var setCameraMatrix = function () { |
| 116 | + var proj = camera_matrix; |
| 117 | + var ratioW = pw / w; |
| 118 | + var ratioH = ph / h; |
| 119 | + proj[0] *= ratioW; |
| 120 | + proj[4] *= ratioW; |
| 121 | + proj[8] *= ratioW; |
| 122 | + proj[12] *= ratioW; |
| 123 | + proj[1] *= ratioH; |
| 124 | + proj[5] *= ratioH; |
| 125 | + proj[9] *= ratioH; |
| 126 | + proj[13] *= ratioH; |
| 127 | + setMatrix(camera.projectionMatrix, proj); |
| 128 | + } |
| 129 | + |
| 130 | + var draw = function () { |
| 131 | + render_update(); |
| 132 | + var now = Date.now(); |
| 133 | + var dt = now - lasttime; |
| 134 | + time += dt; |
| 135 | + lasttime = now; |
| 136 | + found(); |
| 137 | + if (camera_matrix) { |
| 138 | + setCameraMatrix(); |
| 139 | + } |
| 140 | + |
| 141 | + if (!world) { |
| 142 | + sphere.visible = false; |
| 143 | + } else { |
| 144 | + sphere.visible = true; |
| 145 | + console.log(world); |
| 146 | + sphere.position.y = ((marker.height / marker.dpi) * 2.54 * 10) / 2.0; |
| 147 | + sphere.position.x = ((marker.width / marker.dpi) * 2.54 * 10) / 2.0; |
| 148 | + // set matrix of 'root' by detected 'world' matrix |
| 149 | + setMatrix(root.matrix, world); |
| 150 | + } |
| 151 | + window.addEventListener("endLoading", function (ev) { |
| 152 | + if (ev.detail.end == true) { |
| 153 | + // removing loader page if present |
| 154 | + var loader = document.getElementById('loading'); |
| 155 | + if (loader) { |
| 156 | + loader.querySelector('.loading-text').innerText = 'Start the tracking!'; |
| 157 | + setTimeout(function () { |
| 158 | + if(loader) { |
| 159 | + loader.querySelector('.loading-text').innerText = ''; |
| 160 | + loader.querySelector('img').src = ''; |
| 161 | + } |
| 162 | + }, 2000); |
| 163 | + } else { |
| 164 | + console.log("No loader found");} |
| 165 | + } |
| 166 | + }) |
| 167 | + renderer.render(scene, camera); |
| 168 | + }; |
| 169 | + |
| 170 | + var process = function () { |
| 171 | + context_process.fillStyle = 'black'; |
| 172 | + context_process.fillRect(0, 0, pw, ph); |
| 173 | + context_process.drawImage(video, 0, 0, vw, vh, ox, oy, w, h); |
| 174 | + |
| 175 | + var imageData = context_process.getImageData(0, 0, pw, ph); |
| 176 | + |
| 177 | + if (ar && ar.process) { |
| 178 | + ar.process(imageData); |
| 179 | + } |
| 180 | + |
| 181 | + requestAnimationFrame(process); |
| 182 | + } |
| 183 | + var tick = function () { |
| 184 | + draw(); |
| 185 | + requestAnimationFrame(tick); |
| 186 | + }; |
| 187 | + |
| 188 | + load(); |
| 189 | + tick(); |
| 190 | + process(); |
| 191 | +} |
0 commit comments