jsartoolkit5 - ARToolkit.js
html
Talkingdata - 用數據的心智去超越ios
three.js 系列教程 - 良心之做git
<script src="build/artoolkit.min.js"></script>複製代碼
ARController.getUserMedia(options)
github
var video = ARController.getUserMedia({
maxARVideoSize: 320, // do AR processing on scaled down video of this size
facing: "environment",
onSuccess: function(video) {
console.log('got video', video);
}
});複製代碼
...
var arController = new ARController(video, 'Data/camera_para.dat');
arController.onload = function() {
console.log('ARController ready for use', arController);
};
...複製代碼
...
var camera = new ARCameraParam('Data/camera_para.dat');
camera.onload = function() {
var arController = new ARController(video.videoWidth, video.videoHeight, camera);
console.log('ARController ready for use', arController);
};
...複製代碼
// Set the ARController pattern detection mode to detect barcode markers.
arController.setPatternDetectionMode( artoolkit.AR_MATRIX_CODE_DETECTION );
// Add an event listener to listen to getMarker events on the ARController.
// Whenever ARController#process detects a marker, it fires a getMarker event
// with the marker details.
//
var detectedBarcodeMarkers = {};
arController.addEventListener('getMarker', function(ev) {
var barcodeId = ev.data.marker.idMatrix;
if (barcodeId !== -1) {
console.log("saw a barcode marker with id", barcodeId);
// Note that you need to copy the values of the transformation matrix,
// as the event transformation matrix is reused for each marker event
// sent by an ARController.
//
var transform = ev.data.matrix;
if (!detectedBarcodeMarkers[barcodeId]) {
detectedBarcodeMarkers[barcodeId] = {
visible: true,
matrix: new Float32Array(16)
}
}
detectedBarcodeMarkers[barcodeId].visible = true;
detectedBarcodeMarkers[barcodeId].matrix.set(transform);
}
});
var cameraMatrix = arController.getCameraMatrix();
for (var i in detectedBarcodeMarkers) {
detectedBarcodeMarkers.visible = false;
}
// Process a video frame to find markers in it.
// Each detected marker fires a getMarker event.
//
arController.process(video);複製代碼
js/artoolkit.three.js
canvas
<script src="build/artoolkit.min.js"></script>
<script src="js/artoolkit.three.js"></script>
<script>
ARController.getUserMediaThreeScene(
facing: 'environment',
onSuccess: function(arScene, arController, arCameraParam) {
arController.setPatternDetectionMode(artoolkit.AR_MATRIX_CODE_DETECTION);
// Track the barcode marker with id 20.
// markerRoot is a THREE.Object3D that tracks the marker position.
//
var markerRoot = arController.createThreeBarcodeMarker(20);
// Add the openable box to the marker root.
//
var box = createOpenableBox();
markerRoot.add(box);
// Add the marker root to the AR scene.
//
arScene.scene.add(markerRoot);
// Add event handlers to make the box open/close on tap.
//
window.addEventListener('touchend', function(ev) {
if (box.hit( ev.touches[0], arScene.camera )) {
box.toggleOpen();
}
}, false);
window.addEventListener('mouseup', function(ev) {
if (box.hit( ev, arScene.camera )) {
box.toggleOpen();
}
}, false);
// Create renderer and deal with mobile orientation.
//
var renderer = new THREE.WebGLRenderer({antialias: true});
var f = Math.min(
window.innerWidth / arScene.video.videoWidth,
window.innerHeight / arScene.video.videoHeight
);
var w = f * arScene.video.videoWidth;
var h = f * arScene.video.videoHeight;
if (arController.orientation === 'portrait') {
renderer.setSize(h,w);
renderer.domElement.style.transformOrigin = '0 0';
renderer.domElement.style.transform = 'rotate(-90deg) translateX(-100%)';
} else {
renderer.setSize(w,h);
}
document.body.appendChild(renderer.domElement);
// Call arScene.renderOn on each frame,
// it does marker detection, updates the Three.js scene and draws a new frame.
//
var tick = function() {
requestAnimationFrame(tick);
arScene.renderOn(renderer);
};
tick();
}
);
</script>複製代碼
(ios11 目前支持)segmentfault
// Create a marker root object to keep track of the marker.
//
var markerRoot = new THREE.Object3D();
// Make the marker root matrix manually managed.
//
markerRoot.matrixAutoUpdate = false;
// Add a getMarker event listener that keeps track of barcode marker with id 20.
//
arController.addEventListener('getMarker', function(ev) {
if (ev.data.marker.idMatrix === 20) {
// The marker was found in this video frame, make it visible.
markerRoot.visible = true;
// Copy the marker transformation matrix to the markerRoot matrix.
markerRoot.matrix.elements.set(ev.matrix);
}
});
// Add a cube to the marker root.
//
markerRoot.add( new THREE.Mesh(new THREE.BoxGeometry(1,1,1), new THREE.NormalGeometry()) );
// Create renderer with a size that matches the video.
//
var renderer = new THREE.WebGLRenderer({ antialias: true });
renderer.setSize(video.videoWidth, video.videoHeight);
document.body.appendChild(renderer.domElement);
// Set up the scene and camera.
//
var scene = new THREE.Scene();
var camera = new THREE.Camera();
scene.add(camera);
scene.add(markerRoot);
// Make the camera matrix manually managed.
//
camera.matrixAutoUpdate = false;
// Set the camera matrix to the AR camera matrix.
//
camera.matrix.elements.set(arController.getCameraMatrix());
// On each frame, detect markers, update their positions and
// render the frame on the renderer.
//
var tick = function() {
requestAnimationFrame(tick);
// Hide the marker, we don't know if it's visible in this frame.
markerRoot.visible = false;
// Process detects markers in the video frame and sends
// getMarker events to the event listeners.
arController.process(video);
// Render the updated scene.
renderer.render(scene, camera);
};
tick();複製代碼
// To display the video, first create a texture from it.
var videoTex = new THREE.Texture(video);
// Use linear downscaling for videoTex
// (otherwise it needs to be power-of-two sized and you
// need to generate mipmaps, which are kinda useless here)
videoTex.minFilter = THREE.LinearFilter;
// And unflip the video Y-axis.
videoTex.flipY = false;
// Then create a plane textured with the video.
var plane = new THREE.Mesh(
new THREE.PlaneBufferGeometry(2, 2),
new THREE.MeshBasicMaterial({map: videoTex, side: THREE.DoubleSide})
);
// The video plane shouldn't care about the z-buffer. plane.material.depthTest = false; plane.material.depthWrite = false; // Create a scene and a camera to draw the video. var videoScene = new THREE.Scene(); var videoCamera = new THREE.OrthographicCamera(-1, 1, -1, 1, -1, 1); videoScene.add(videoCamera); videoScene.add(plane); // Set the renderer autoClear to false, otherwise it // clears the canvas before each render call. renderer.autoClear = false; // Draw the videoScene before the AR scene. var tick = function() { requestAnimationFrame(tick); markerRoot.visible = false; arController.process(video); // Clear the renderer before drawing the videoScene, // followed by the AR scene. renderer.clear(); renderer.render(videoScene, videoCamera); renderer.render(scene, camera); }; tick();複製代碼
// Rotate the video plane and the renderer if the arController is in portrait mode.
if (arController.orientation === 'portrait') {
plane.rotation.z = Math.PI/2;
renderer.setSize(video.videoHeight, video.videoWidth);
renderer.domElement.style.transformOrigin = '0 0';
renderer.domElement.style.transform = 'rotate(-90deg) translateX(-100%)';
} else {
renderer.setSize(video.videoWidth, video.videoHeight);
}複製代碼
如下是ARtoolkit使用C風格處理Marker的循環方法:api
// Detect barcode markers
arController.setPatternDetectionMode(artoolkit.AR_MATRIX_CODE_DETECTION);
// Process the video frame.
arController.detectMarker(video);
var markerMatrix = new Float32Array(12);
var glMatrix = new Float32Array(16);
// Get the number of markers from the ARController and iterate over each marker.
var markers = arController.getMarkerNum();
for (var i=0; i < markers.length; i++) {
var marker = arController.getMarker(i);
// If we found the number 5 marker, let's get its transform matrix. if (marker.idMatrix === 5) { arController.getTransMatSquare(i, 1 /* marker width */, markerMatrix); // And convert it to a WebGL matrix. arController.transMatToGLMat(markerMatrix, glMatrix); // Do something with the glMatrix ... } }複製代碼
// ... skip to the marker detection loop
if (marker.idMatrix === 5) {
if (markerWasInPreviousFrame) {
// Get the marker matrix using continuous tracking
arController.getTransMatSquareCont(i, 1, markerMatrix, markerMatrix);
} else {
arController.getTransMatSquare(i, 1, markerMatrix);
}
// ... The rest is as usual
}
複製代碼
build/artoolkit.debug.js
並js/artoolkit.api.js進入您的頁面。該
artoolkit.api.js
文件包含與ARController和ARCameraParam的實現一塊兒的C ++ < - > JS API綁定。瀏覽器
<script src="build/artoolkit.debug.js"></script>
<script src="js/artoolkit.api.js"></script>複製代碼
複製代碼