|
| 1 | +## Package |
| 2 | + |
| 3 | +**new Package(config)** |
| 4 | + |
| 5 | +Accepts the following configuration: |
| 6 | + |
| 7 | +```js |
| 8 | +new Package({ |
| 9 | + arType: 'pattern', // see exported AR_* constants |
| 10 | + assetType: '3d', // see exported ASSET_* constants |
| 11 | + assetFile: yourAssetFile, // read details after this code block |
| 12 | + assetName: 'model.gltf', // the AR asset filename, including the file extension |
| 13 | + assetParam: { |
| 14 | + isValid: true, // whether the scale/size parameters are correct or not (if you don't know what this is, set to true) |
| 15 | + scale: 1.0, // scale of the asset |
| 16 | + size: { // sizes of the asset, unused |
| 17 | + width: 1.0, |
| 18 | + height: 1.0, |
| 19 | + depth: 1.0 |
| 20 | + }, |
| 21 | + locations: [ |
| 22 | + { |
| 23 | + latitude: 12.345678, // required for location based AR |
| 24 | + longitude: 12.345678 // required for location based AR |
| 25 | + } |
| 26 | + ] |
| 27 | + }, |
| 28 | + markerPatt: '...', // the content of the generated .patt file, as a string (required for pattern/location based AR) |
| 29 | + matrixType: '...', // see exported MATRIX_* constants (required for barcode based AR) |
| 30 | + markerValue: 7, // barcode value (required for barcode based AR) |
| 31 | +}); |
| 32 | +``` |
| 33 | + |
| 34 | +The `assetFile` parameter must be: |
| 35 | +- a base64 encoded string (use `FileReader.readAsDataURL()` and strip the initial `data:*/*;base64,` string) for 3d/image assets |
| 36 | +- a `Blob` for audio/video assets when using Zip provider |
| 37 | +- an `ArrayBuffer` for audio/video when using GitHub provider |
| 38 | + |
| 39 | +**serve(config)** |
| 40 | + |
| 41 | +Deploys the package, based on the chosen provider. Accepts the following configuration: |
| 42 | + |
| 43 | +```js |
| 44 | +package.serve({ |
| 45 | + packageType: 'zip', // see exported PACKAGE_* constants |
| 46 | + config: {...} // see either serveFiles() documentation for your chosen provider |
| 47 | +}); |
| 48 | +``` |
| 49 | + |
| 50 | +### Example |
| 51 | + |
| 52 | +```js |
| 53 | +const { |
| 54 | + AR_PATTERN, |
| 55 | + ASSET_3D, |
| 56 | + PACKAGE_GITHUB, |
| 57 | + PACKAGE_ZIP, |
| 58 | + MarkerModule |
| 59 | +} = ARjsStudioBackend; |
| 60 | + |
| 61 | +// helper function -- promisify file reading |
| 62 | +const reader = new FileReader(); |
| 63 | +const waitRead = () => { |
| 64 | + return new Promise(resolve => { |
| 65 | + reader.addEventListener('load', () => { |
| 66 | + resolve(reader.result); |
| 67 | + }); |
| 68 | + }) |
| 69 | +}; |
| 70 | + |
| 71 | +// read marker image |
| 72 | +const markerFile = document.querySelector('#marker-image').files[0]; |
| 73 | +reader.readAsDataURL(markerFile); |
| 74 | +const originalMarker = await waitRead(); |
| 75 | + |
| 76 | +// generate .patt file |
| 77 | +const textPatt = await MarkerModule.getMarkerPattern(originalMarker); |
| 78 | + |
| 79 | +// read asset file |
| 80 | +const assetFile = document.querySelector('#asset-file-selector').files[0]; |
| 81 | +reader.readAsDataURL(assetFile); |
| 82 | +const base64Asset = await waitRead(); |
| 83 | + |
| 84 | +// create the package |
| 85 | +const package = new Package({ |
| 86 | + arType: AR_PATTERN, |
| 87 | + assetType: ASSET_3D, |
| 88 | + assetFile: base64Asset, |
| 89 | + assetName: file.name, |
| 90 | + assetParam: { |
| 91 | + isValid: true, |
| 92 | + scale: 1.0 |
| 93 | + }, |
| 94 | + markerPatt: textPatt |
| 95 | +}); |
| 96 | + |
| 97 | +// deploy to github... |
| 98 | +const pagesURL = await package.serve({ |
| 99 | + packageType: PACKAGE_GITHUB, |
| 100 | + token: 'user github token', |
| 101 | + repo: 'arjs-awesome-repo-84265' |
| 102 | +}); |
| 103 | +console.log(`navigate to ${pagesURL} to see your project`); |
| 104 | + |
| 105 | +// ...or generate a zip file (or both!) |
| 106 | +const zipFile = await package.serve({ |
| 107 | + packageType: PACKAGE_ZIP, |
| 108 | + compress: 9 |
| 109 | +}); |
| 110 | +// trigger download |
| 111 | +window.location = `data:application/zip;base64,${zipFile}`; |
| 112 | +``` |
0 commit comments