Skip to content

Commit

Permalink
Working example with wasm support
Browse files Browse the repository at this point in the history
  • Loading branch information
ThorstenBux committed Jul 4, 2018
1 parent 0310e0f commit 4229ff6
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 11 deletions.
2 changes: 1 addition & 1 deletion build/artoolkit.min.js

Large diffs are not rendered by default.

4 changes: 4 additions & 0 deletions build/artoolkit_wasm.js

Large diffs are not rendered by default.

Binary file added build/artoolkit_wasm.wasm
Binary file not shown.
122 changes: 122 additions & 0 deletions examples/simple_image_wasm.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<html>
<body>

<img id="v1" src="Data/img.jpg"></img>
<img id="v2" src="Data/chalk.jpg"></img>
<img id="v3" src="Data/chalk_multi.jpg"></img>
<img id="v4" src="Data/kuva.jpg"></img>
<img id="v5" src="Data/armchair.jpg"></img>

<script type='text/javascript'>
var Module = {};

function downloadWasm(url) {
return new Promise(function(resolve, reject) {
var wasmXHR = new XMLHttpRequest();
wasmXHR.open('GET', url, true);
wasmXHR.responseType = 'arraybuffer';
wasmXHR.onload = function() { resolve(wasmXHR.response); }
wasmXHR.onerror = function() { reject('error ' + wasmXHR.status); }
wasmXHR.send(null);
});
}

// var wasm = downloadWasm('../build/artoolkit.wasm.wasm');

// Module.instantiateWasm is a user-implemented callback which the Emscripten runtime calls to perform
// the WebAssembly instantiation action. The callback function will be called with two parameters, imports
// and successCallback. imports is a JS object which contains all the function imports that need to be passed
// to the Module when instantiating, and once instantiated, the function should call successCallback() with
// the WebAssembly Instance object.
// The instantiation can be performed either synchronously or asynchronously. The return value of this function
// should contain the exports object of the instantiated Module, or an empty dictionary object {} if the
// instantiation is performed asynchronously, or false if instantiation failed.
Module.instantiateWasm = async function(imports, successCallback) {
console.log('instantiateWasm: instantiating synchronously');
var wasmBinary = await downloadWasm('../build/artoolkit_wasm.wasm');
console.log('wasm download finished, begin instantiating');
try {
var wasmInstantiate = await WebAssembly.instantiate(new Uint8Array(wasmBinary), imports);
console.log('wasm instantiation succeeded');
Module.testWasmInstantiationSucceeded = 1;
successCallback(wasmInstantiate.instance);
} catch(e) {
console.log('wasm instantiation failed! ' + e);
return false;
}
return {}; // Module has no exports.
}
if(!window.Module) {
window.Module = Module;
}
// var script = document.createElement('script');
// script.src = "../build/artoolkit.wasm.js";
// document.body.appendChild(script);
</script>
<script src="../build/artoolkit_wasm.js"></script>

<script>

window.addEventListener('artoolkit-loaded', () => {
var cameraParam = new ARCameraParam();

var ar1, ar2, ar3, ar4, ar5;

cameraParam.onload = function() {
ar1 = new ARController(v1, cameraParam);
ar1.debugSetup();

ar2 = new ARController(v2, cameraParam);
ar2.debugSetup();

ar3 = new ARController(v3, cameraParam);
ar3.debugSetup();
ar3.process();

ar4 = new ARController(v4, cameraParam);
ar4.debugSetup();
ar4.process();

ar5 = new ARController(v5, cameraParam);

ar1.detectMarker();
ar1.debugDraw();
ar2.detectMarker();
ar2.debugDraw();

ar5.debugSetup();
ar5.detectMarker();
ar5.debugDraw();

var assertEq = function(message, value, shouldBe) {
if (value !== shouldBe) {
throw(message + ' ' + value + ' is not the expected ' + shouldBe);
}
};

ar5.setMarkerInfoDir(0, 1);
assertEq('dir changed to 1', ar5.getMarker(0).dir, 1);
ar5.setMarkerInfoDir(0, 2);
assertEq('dir changed to 2', ar5.getMarker(0).dir, 2);

// Test setter/getter pairs
var methods = "DebugMode LogLevel ProjectionNearPlane ProjectionFarPlane ThresholdMode Threshold PatternDetectionMode PattRatio MatrixCodeType LabelingMode ImageProcMode".split(" ");
for (var i=0; i<methods.length; i++) {
var m = methods[i];
var v = ar5['get'+m]();
var nv = v === 1 ? 0 : ((v < 1 && v > 0) ? 0.7848 : 1);
ar5['set'+m](nv);
assertEq(m + ' changed', ar5['get'+m](), nv);
ar5['set'+m](v);
assertEq(m + ' changed back', ar5['get'+m](), v);
}
console.log("Setter/Getter tests run successfully.");
};

cameraParam.load('Data/camera_para.dat');
});

</script>

</body>
</html>
15 changes: 11 additions & 4 deletions js/artoolkit.api.js
Original file line number Diff line number Diff line change
Expand Up @@ -1814,11 +1814,18 @@
window.ARController = ARController;
window.ARCameraParam = ARCameraParam;


window.Module = {
onRuntimeInitialized: function() {
if (window.Module) {
window.Module.onRuntimeInitialized = function() {
runWhenLoaded();
var event = new Event('artoolkit-loaded');
window.dispatchEvent(event);
}
};
} else {
window.Module = {
onRuntimeInitialized: function() {
runWhenLoaded();
}
};
}

})();
18 changes: 12 additions & 6 deletions tools/makem.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ var MEM = 256 * 1024 * 1024; // 64MB

var SOURCE_PATH = path.resolve(__dirname, '../emscripten/') + '/';
var OUTPUT_PATH = path.resolve(__dirname, '../build/') + '/';
var BUILD_FILE = 'artoolkit.debug.js';
var BUILD_DEBUG_FILE = 'artoolkit.debug.js';
var BUILD_WASM_FILE = 'artoolkit_wasm.js';
var BUILD_MIN_FILE = 'artoolkit.min.js';

var MAIN_SOURCES = HAVE_NFT ? [
Expand Down Expand Up @@ -124,7 +125,6 @@ var FLAGS = '' + OPTIMIZE_FLAGS;
FLAGS += ' -Wno-warn-absolute-paths ';
FLAGS += ' -s TOTAL_MEMORY=' + MEM + ' ';
FLAGS += ' -s USE_ZLIB=1';
FLAGS += ' -s WASM=0';
// FLAGS += ' -s FULL_ES2=1 '
// FLAGS += ' -s NO_BROWSER=1 '; // for 20k less
FLAGS += ' --memory-init-file 0 '; // for memless file
Expand Down Expand Up @@ -220,18 +220,23 @@ var compile_libjpeg = format(EMCC + ' ' + INCLUDES + ' '

var compile_combine = format(EMCC + ' ' + INCLUDES + ' '
+ ' {OUTPUT_PATH}*.bc ' + MAIN_SOURCES
+ FLAGS + ' ' + DEBUG_FLAGS + DEFINES + ' -o {OUTPUT_PATH}{BUILD_FILE} ',
OUTPUT_PATH, OUTPUT_PATH, BUILD_FILE);
+ FLAGS + ' -s WASM=0' + ' ' + DEBUG_FLAGS + DEFINES + ' -o {OUTPUT_PATH}{BUILD_FILE} ',
OUTPUT_PATH, OUTPUT_PATH, BUILD_DEBUG_FILE);

var compile_combine_min = format(EMCC + ' ' + INCLUDES + ' '
+ ' {OUTPUT_PATH}*.bc ' + MAIN_SOURCES
+ FLAGS + ' ' + DEFINES + PRE_FLAGS + ' -o {OUTPUT_PATH}{BUILD_FILE} ',
+ FLAGS + ' -s WASM=0' + ' ' + DEFINES + PRE_FLAGS + ' -o {OUTPUT_PATH}{BUILD_FILE} ',
OUTPUT_PATH, OUTPUT_PATH, BUILD_MIN_FILE);

var compile_wasm = format(EMCC + ' ' + INCLUDES + ' '
+ ' {OUTPUT_PATH}*.bc ' + MAIN_SOURCES
+ FLAGS + ' -s ASSERTIONS=1 ' + DEFINES + PRE_FLAGS + ' -o {OUTPUT_PATH}{BUILD_FILE} ',
OUTPUT_PATH, OUTPUT_PATH, BUILD_WASM_FILE);

var compile_all = format(EMCC + ' ' + INCLUDES + ' '
+ ar_sources.join(' ')
+ FLAGS + ' ' + DEFINES + ' -o {OUTPUT_PATH}{BUILD_FILE} ',
OUTPUT_PATH, BUILD_FILE);
OUTPUT_PATH, BUILD_DEBUG_FILE);

/*
* Run commands
Expand Down Expand Up @@ -276,6 +281,7 @@ addJob(compile_arlib);
// compile_kpm
// addJob(compile_libjpeg);
addJob(compile_combine);
addJob(compile_wasm);
addJob(compile_combine_min);
// addJob(compile_all);

Expand Down

0 comments on commit 4229ff6

Please sign in to comment.