|
| 1 | +<html> |
| 2 | +<head> |
| 3 | + <link rel="stylesheet" href="./Data/css/style.css"> |
| 4 | +</head> |
| 5 | +<body> |
| 6 | + <div id="stats" class="ui stats"> |
| 7 | + <div class="stats-item"> |
| 8 | + <p class="stats-item-title"> |
| 9 | + Stats |
| 10 | + </p> |
| 11 | + </div> |
| 12 | + </div> |
| 13 | + <script src="./Data/js/third_party/three.js/three.min.js"></script> |
| 14 | + <script src="./Data/js/third_party/three.js/stats.min.js"></script> |
| 15 | + <script type='text/javascript'> |
| 16 | + window.artoolkitX_wasm_url = '../SDK/lib/artoolkitx.wasm'; |
| 17 | + </script> |
| 18 | + |
| 19 | + |
| 20 | + <script id="vert" type="glsl-vertex"> |
| 21 | + precision highp float; |
| 22 | + precision lowp int; |
| 23 | + |
| 24 | + uniform mat4 cameraMatrix; |
| 25 | + uniform mat4 transformationMatrix; |
| 26 | + |
| 27 | + varying vec2 vUv; |
| 28 | + |
| 29 | + void main(void) |
| 30 | + { |
| 31 | + vUv = uv; |
| 32 | + gl_Position = cameraMatrix * transformationMatrix * vec4(position, 1.0); |
| 33 | + } |
| 34 | + </script> |
| 35 | + |
| 36 | + <script id="frag" type="glsl-fragment"> |
| 37 | + precision highp float; |
| 38 | + precision lowp int; |
| 39 | + |
| 40 | + varying vec2 vUv; |
| 41 | + |
| 42 | + void main(void) |
| 43 | + { |
| 44 | + gl_FragColor = vec4(vUv, 1.0, 1.0); |
| 45 | + } |
| 46 | + </script> |
| 47 | + |
| 48 | +<script type="module" src="../SDK/lib/artoolkitX.api.js"></script> |
| 49 | +<script type="module"> |
| 50 | +import ARController from '../SDK/lib/artoolkitX.api.js'; |
| 51 | + |
| 52 | +var statsMain = new Stats(); |
| 53 | +statsMain.showPanel( 0 ); // 0: fps, 1: ms, 2: mb, 3+: custom |
| 54 | +document.getElementById( 'stats' ).appendChild( statsMain.dom ); |
| 55 | + |
| 56 | +let ar1, interval; |
| 57 | + |
| 58 | +const cameraParam = './Data/camera_para.dat'; |
| 59 | + |
| 60 | +const config = { |
| 61 | + cameraParam: cameraParam, |
| 62 | + width: 640, |
| 63 | + height: 480 |
| 64 | +}; |
| 65 | + |
| 66 | +var trackable = { |
| 67 | + trackableType: "2d", |
| 68 | + url: './Data/Alterra_Postcard_2.jpg', |
| 69 | + width: 1.0 |
| 70 | +} |
| 71 | + |
| 72 | +window.addEventListener('artoolkitX-loaded', () => { |
| 73 | + |
| 74 | +var cMat = new THREE.Matrix4(); |
| 75 | +var tMat = new THREE.Matrix4(); |
| 76 | + |
| 77 | +var USE_SHADER = false; |
| 78 | + |
| 79 | +var shaderMaterial = new THREE.ShaderMaterial({ |
| 80 | + uniforms: { |
| 81 | + cameraMatrix: {type: 'm4', value: cMat }, |
| 82 | + transformationMatrix: {type: 'm4', value: tMat } |
| 83 | + }, |
| 84 | + vertexShader: vert.text, |
| 85 | + fragmentShader: frag.text |
| 86 | +}); |
| 87 | + |
| 88 | +var renderer = new THREE.WebGLRenderer(); |
| 89 | +var scene = new THREE.Scene(); |
| 90 | + |
| 91 | +ARController.getUserMediaARController(config).then( arController => { |
| 92 | + |
| 93 | + renderer.setSize(config.width, config.height); |
| 94 | + document.body.appendChild(renderer.domElement); |
| 95 | + renderer.domElement.style = 'position: absolute; width: 100%; height: 100%' |
| 96 | + |
| 97 | + var videoTex = new THREE.Texture(arController.image); |
| 98 | + |
| 99 | + videoTex.minFilter = THREE.LinearFilter; |
| 100 | + videoTex.flipY = false; |
| 101 | + |
| 102 | + // Then create a plane textured with the video. |
| 103 | + var plane = new THREE.Mesh( |
| 104 | + new THREE.PlaneBufferGeometry(2, 2), |
| 105 | + new THREE.MeshBasicMaterial({map: videoTex, side: THREE.DoubleSide}) |
| 106 | + ); |
| 107 | + |
| 108 | + // The video plane shouldn't care about the z-buffer. |
| 109 | + plane.material.depthTest = false; |
| 110 | + plane.material.depthWrite = false; |
| 111 | + |
| 112 | + // Create a camera and a scene for the video plane and |
| 113 | + // add the camera and the video plane to the scene. |
| 114 | + var videoCamera = new THREE.OrthographicCamera(-1, 1, -1, 1, -1, 1); |
| 115 | + var videoScene = new THREE.Scene(); |
| 116 | + videoScene.add(plane); |
| 117 | + videoScene.add(videoCamera); |
| 118 | + |
| 119 | + // Create a camera and a marker root object for your Three.js scene. |
| 120 | + const camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.01, 1000 ); |
| 121 | + scene.add(camera); |
| 122 | + |
| 123 | + var light = new THREE.PointLight(0xffffff); |
| 124 | + light.position.set(400, 500, 100); |
| 125 | + scene.add(light); |
| 126 | + var light = new THREE.PointLight(0xffffff); |
| 127 | + light.position.set(-400, -500, -100); |
| 128 | + scene.add(light); |
| 129 | + |
| 130 | + var markerRoot = new THREE.Object3D(); |
| 131 | + |
| 132 | + markerRoot.wasVisible = false; |
| 133 | + markerRoot.markerMatrix = new Float64Array(16); |
| 134 | + markerRoot.matrixAutoUpdate = false; |
| 135 | + |
| 136 | + // Add the marker models and suchlike into your marker root object. |
| 137 | + |
| 138 | + var cube = new THREE.Mesh( |
| 139 | + new THREE.BoxGeometry(0.5,0.5,0.5), |
| 140 | + USE_SHADER ? |
| 141 | + shaderMaterial : |
| 142 | + new THREE.MeshLambertMaterial({ color: 0x44ffff, wireframe: false }) |
| 143 | + ); |
| 144 | + markerRoot.add(cube); |
| 145 | + markerRoot.visible = true; |
| 146 | + // adding a grid xz and an axesHelper |
| 147 | + var axesHelper = new THREE.AxesHelper( 5 ); |
| 148 | + markerRoot.add( axesHelper ); |
| 149 | + |
| 150 | + var gridXZ = new THREE.GridHelper(10, 10); |
| 151 | + markerRoot.add(gridXZ); |
| 152 | + |
| 153 | + // Add the marker root to your scene. |
| 154 | + scene.add(markerRoot); |
| 155 | + |
| 156 | + arController.addEventListener('getMarker', (trackableInfo) => { |
| 157 | + console.log("TrackableID: " + trackableInfo.data.trackableId); |
| 158 | + markerRoot.visible = true; |
| 159 | + markerRoot.matrix.elements = trackableInfo.data.transformation |
| 160 | + }); |
| 161 | + |
| 162 | + try { |
| 163 | + arController.start().then( () => { |
| 164 | + console.log("start done"); |
| 165 | + const camMatrix = arController.getCameraMatrix(0.01, 1000); |
| 166 | + camera.projectionMatrix.fromArray(camMatrix); |
| 167 | + camera.updateProjectionMatrix(); |
| 168 | + var trackableId = arController.addTrackable(trackable); |
| 169 | + interval = setInterval(function() { |
| 170 | + arController.process(); |
| 171 | + videoTex.needsUpdate = true; |
| 172 | + const ac = renderer.autoClear; |
| 173 | + renderer.autoClear = false; |
| 174 | + renderer.clear(); |
| 175 | + statsMain.update() |
| 176 | + renderer.render(videoScene, videoCamera); |
| 177 | + renderer.render(scene, camera); |
| 178 | + renderer.autoClear = ac; |
| 179 | + }, 13); |
| 180 | + ar1 = arController; |
| 181 | + }); |
| 182 | + } |
| 183 | + catch (e) { |
| 184 | + console.log(e); |
| 185 | + } |
| 186 | +} ); |
| 187 | + |
| 188 | +}); |
| 189 | + |
| 190 | +window.closeVideo = function() { |
| 191 | + if(ar1) { |
| 192 | + ar1.dispose(); |
| 193 | + clearInterval(interval); |
| 194 | + } |
| 195 | + else { |
| 196 | + console.error("Trying to close before opened"); |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | + |
| 201 | +</script> |
| 202 | + |
| 203 | +<button class="ui marker" onclick="window.closeVideo()">Close Video</button> |
| 204 | +</body> |
| 205 | +</html> |
0 commit comments