├── .babelrc ├── .browserlistrc ├── .eslintrc ├── .gitignore ├── .prettierrc ├── LICENSE ├── README.md ├── example ├── .babelrc ├── README.md ├── data │ ├── camera_para.dat │ └── patt.hiro ├── hiro.png ├── package-lock.json ├── package.json ├── src │ ├── index.html │ └── index.js └── webpack.config.js ├── package-lock.json ├── package.json └── src ├── ar ├── ar.js ├── arCanvas.js ├── arMarker.js └── index.js └── index.js /.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | [ 4 | "@babel/preset-env", 5 | { 6 | "useBuiltIns": "usage", 7 | "corejs": 3 8 | } 9 | ], 10 | "@babel/preset-react" 11 | ] 12 | } 13 | -------------------------------------------------------------------------------- /.browserlistrc: -------------------------------------------------------------------------------- 1 | defaults 2 | -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": [ 3 | "eslint:recommended", 4 | "plugin:compat/recommended", 5 | "plugin:import/recommended", 6 | "plugin:react/recommended", 7 | "plugin:react-hooks/recommended", 8 | "plugin:prettier/recommended" 9 | ], 10 | "env": { 11 | "browser": true 12 | }, 13 | "globals": { 14 | "THREEx": true, 15 | "THREE": true 16 | }, 17 | "rules": { 18 | "react/prop-types": 0, 19 | "react/no-unknown-property": 0 20 | } 21 | } -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode 2 | node_modules/ 3 | example/cert.pem 4 | example/key.pem 5 | example/dist 6 | lib/ 7 | -------------------------------------------------------------------------------- /.prettierrc: -------------------------------------------------------------------------------- 1 | { 2 | "arrowParens": "avoid", 3 | "printWidth": 100, 4 | "tabWidth": 2, 5 | "useTabs": false, 6 | "semi": false, 7 | "singleQuote": false, 8 | "trailingComma": "all", 9 | "bracketSpacing": true, 10 | "jsxBracketSameLine": true 11 | } 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 ART+COM AG 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # @artcom/react-three-arjs 2 | 3 | React components and hooks for creating [AR.js](https://github.com/AR-js-org/AR.js) applications with [react-three-fiber](https://github.com/pmndrs/react-three-fiber) 4 | 5 | 6 | ``` 7 | npm i @artcom/react-three-arjs 8 | ``` 9 | 10 | ## Usage 11 | 12 | Provide a [camera_para.dat](./example/data/camera_para.dat) file in your public folder, default path is `data/camera_para.dat`. See the [example](./example). 13 | 14 | ```jsx 15 | import ReactDOM from "react-dom" 16 | import React from "react" 17 | 18 | import { ARCanvas, ARMarker } from "@artcom/react-three-arjs" 19 | 20 | ReactDOM.render( 21 | { 24 | gl.setSize(window.innerWidth, window.innerHeight) 25 | } }> 26 | 27 | 28 | 31 | 32 | 33 | 34 | 35 | 36 | , 37 | document.getElementById("root") 38 | ) 39 | 40 | ``` 41 | ### Demo 42 | 43 | 44 | * [CodeSandbox Demo](https://codesandbox.io/s/jolly-hodgkin-ssu33) 45 | 46 | ## API 47 | 48 | ### ARCanvas 49 | 50 | ```jsx 51 | without AR context 54 | tracking // if false, it will stop tracking 55 | patternRatio = 0.5 // passed to arToolkit context ¹ 56 | detectionMode = "mono_and_matrix" // passed to arToolkit context ¹ 57 | cameraParametersUrl = "data/camera_para.dat" // passed to arToolkit context ¹ 58 | matrixCodeType = "3x3" // passed to arToolkit context ¹ 59 | onCameraStreamReady // callback which will be invoked when camera stream starts 60 | onCameraStreamError // callback which will be invoked when camera stream fails, e.g.: permissions 61 | sourceType = "webcam" // set camera source type, see ar.js code ² 62 | ...props // props are passed to the r3f canvas ³ 63 | /> 64 | ``` 65 | 66 | 1 https://ar-js-org.github.io/AR.js-Docs/marker-based/#threejs 67 | 68 | 2 https://github.com/AR-js-org/AR.js/blob/00fc2a92af1a756600eb53a57a84f101a2c0435f/three.js/src/threex/threex-artoolkitsource.js#L11-L26 69 | 70 | 3 https://docs.pmnd.rs/react-three-fiber/api/canvas 71 | 72 | ### ARMarker 73 | 74 | ```jsx 75 | 84 | ``` 85 | 86 | 1 https://ar-js-org.github.io/AR.js-Docs/marker-based/#api-reference-for-marker-based 87 | -------------------------------------------------------------------------------- /example/.babelrc: -------------------------------------------------------------------------------- 1 | { 2 | "presets": [ 3 | [ 4 | "@babel/preset-env", 5 | { 6 | "useBuiltIns": "usage", 7 | "corejs": 3 8 | } 9 | ], 10 | "@babel/preset-react" 11 | ] 12 | } -------------------------------------------------------------------------------- /example/README.md: -------------------------------------------------------------------------------- 1 | # @artcom/react-three-arjs example 2 | 3 | ## Run development 4 | 5 | ### Build library 6 | 7 | - `cd ..` 8 | - `npm install` 9 | - `npm run build` 10 | 11 | ### Run example 12 | 13 | - `cd example` 14 | - `npm install` 15 | - `npm run watch` 16 | -------------------------------------------------------------------------------- /example/data/camera_para.dat: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/artcom/react-three-arjs/a6112f27b285d583ee21df9b4662db38ac4913d3/example/data/camera_para.dat -------------------------------------------------------------------------------- /example/data/patt.hiro: -------------------------------------------------------------------------------- 1 | 234 235 240 233 240 234 240 235 240 237 240 238 240 240 240 232 2 | 229 240 240 240 240 240 240 240 240 240 240 240 240 240 240 228 3 | 227 240 240 240 240 240 240 240 240 240 240 240 240 240 240 239 4 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 5 | 236 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 6 | 234 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 7 | 236 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 8 | 231 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 9 | 229 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 10 | 225 149 240 240 186 216 225 174 240 240 240 237 238 240 240 240 11 | 150 107 238 231 75 208 115 147 238 228 223 226 237 180 226 240 12 | 150 62 181 213 62 187 113 169 197 72 29 237 120 50 53 207 13 | 149 63 47 78 53 184 113 101 142 5 150 150 45 217 186 83 14 | 121 84 220 222 58 180 121 92 128 109 237 124 155 232 161 64 15 | 149 71 240 240 76 210 98 109 122 108 240 129 51 119 161 155 16 | 149 186 240 240 98 219 135 152 207 191 236 227 152 77 175 209 17 | 235 235 240 233 240 234 240 235 240 236 240 238 240 240 240 240 18 | 229 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 19 | 227 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 20 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 21 | 236 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 22 | 234 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 23 | 236 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 24 | 232 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 25 | 229 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 26 | 225 156 240 240 186 216 225 186 240 240 240 240 240 240 240 240 27 | 150 117 240 231 72 206 115 162 240 232 223 237 240 180 226 240 28 | 150 74 187 213 51 184 103 168 197 78 29 237 120 50 53 216 29 | 144 77 51 74 61 184 106 101 142 5 150 152 52 217 186 85 30 | 117 89 219 219 65 184 121 92 128 100 236 125 156 240 170 73 31 | 148 71 240 240 76 210 109 109 121 99 240 137 51 120 166 164 32 | 140 186 240 240 98 220 150 156 207 192 236 230 152 77 176 212 33 | 234 235 240 233 240 234 240 235 240 236 240 238 240 240 240 233 34 | 229 240 240 240 240 240 240 240 240 240 240 240 240 240 240 239 35 | 227 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 36 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 37 | 234 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 38 | 232 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 39 | 235 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 40 | 232 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 41 | 228 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 42 | 225 156 240 240 182 212 225 180 240 240 240 240 240 240 240 240 43 | 150 116 238 228 66 205 115 151 238 236 225 240 240 180 226 240 44 | 156 84 186 211 47 184 109 170 200 92 30 240 120 50 53 216 45 | 147 83 51 73 50 184 106 110 148 17 151 150 45 217 186 85 46 | 127 98 219 219 58 179 109 101 128 107 237 125 155 240 163 72 47 | 155 86 240 240 76 201 85 108 121 95 232 137 51 118 153 155 48 | 149 189 240 240 98 220 141 154 206 178 235 230 152 77 175 209 49 | 50 | 232 228 239 240 240 240 240 240 240 240 240 207 83 64 155 209 51 | 240 240 240 240 240 240 240 240 240 240 226 53 186 161 161 175 52 | 240 240 240 240 240 240 240 240 240 240 180 50 217 232 119 77 53 | 240 240 240 240 240 240 240 240 240 238 237 120 45 155 51 152 54 | 238 240 240 240 240 240 240 240 240 237 226 237 150 124 129 227 55 | 240 240 240 240 240 240 240 240 240 240 223 29 150 237 240 236 56 | 237 240 240 240 240 240 240 240 240 240 228 72 5 109 108 191 57 | 240 240 240 240 240 240 240 240 240 240 238 197 142 128 122 207 58 | 235 240 240 240 240 240 240 240 240 174 147 169 101 92 109 152 59 | 240 240 240 240 240 240 240 240 240 225 115 113 113 121 98 135 60 | 234 240 240 240 240 240 240 240 240 216 208 187 184 180 210 219 61 | 240 240 240 240 240 240 240 240 240 186 75 62 53 58 76 98 62 | 233 240 240 240 240 240 240 240 240 240 231 213 78 222 240 240 63 | 240 240 240 240 240 240 240 240 240 240 238 181 47 220 240 240 64 | 235 240 240 240 240 240 240 240 240 149 107 62 63 84 71 186 65 | 234 229 227 240 236 234 236 231 229 225 150 150 149 121 149 149 66 | 240 240 240 240 240 240 240 240 240 240 240 216 85 73 164 212 67 | 240 240 240 240 240 240 240 240 240 240 226 53 186 170 166 176 68 | 240 240 240 240 240 240 240 240 240 240 180 50 217 240 120 77 69 | 240 240 240 240 240 240 240 240 240 240 240 120 52 156 51 152 70 | 238 240 240 240 240 240 240 240 240 240 237 237 152 125 137 230 71 | 240 240 240 240 240 240 240 240 240 240 223 29 150 236 240 236 72 | 236 240 240 240 240 240 240 240 240 240 232 78 5 100 99 192 73 | 240 240 240 240 240 240 240 240 240 240 240 197 142 128 121 207 74 | 235 240 240 240 240 240 240 240 240 186 162 168 101 92 109 156 75 | 240 240 240 240 240 240 240 240 240 225 115 103 106 121 109 150 76 | 234 240 240 240 240 240 240 240 240 216 206 184 184 184 210 220 77 | 240 240 240 240 240 240 240 240 240 186 72 51 61 65 76 98 78 | 233 240 240 240 240 240 240 240 240 240 231 213 74 219 240 240 79 | 240 240 240 240 240 240 240 240 240 240 240 187 51 219 240 240 80 | 235 240 240 240 240 240 240 240 240 156 117 74 77 89 71 186 81 | 235 229 227 240 236 234 236 232 229 225 150 150 144 117 148 140 82 | 233 239 240 240 240 240 240 240 240 240 240 216 85 72 155 209 83 | 240 240 240 240 240 240 240 240 240 240 226 53 186 163 153 175 84 | 240 240 240 240 240 240 240 240 240 240 180 50 217 240 118 77 85 | 240 240 240 240 240 240 240 240 240 240 240 120 45 155 51 152 86 | 238 240 240 240 240 240 240 240 240 240 240 240 150 125 137 230 87 | 240 240 240 240 240 240 240 240 240 240 225 30 151 237 232 235 88 | 236 240 240 240 240 240 240 240 240 240 236 92 17 107 95 178 89 | 240 240 240 240 240 240 240 240 240 240 238 200 148 128 121 206 90 | 235 240 240 240 240 240 240 240 240 180 151 170 110 101 108 154 91 | 240 240 240 240 240 240 240 240 240 225 115 109 106 109 85 141 92 | 234 240 240 240 240 240 240 240 240 212 205 184 184 179 201 220 93 | 240 240 240 240 240 240 240 240 240 182 66 47 50 58 76 98 94 | 233 240 240 240 240 240 240 240 240 240 228 211 73 219 240 240 95 | 240 240 240 240 240 240 240 240 240 240 238 186 51 219 240 240 96 | 235 240 240 240 240 240 240 240 240 156 116 84 83 98 86 189 97 | 234 229 227 240 234 232 235 232 228 225 150 156 147 127 155 149 98 | 99 | 209 175 77 152 227 236 191 207 152 135 219 98 240 240 186 149 100 | 155 161 119 51 129 240 108 122 109 98 210 76 240 240 71 149 101 | 64 161 232 155 124 237 109 128 92 121 180 58 222 220 84 121 102 | 83 186 217 45 150 150 5 142 101 113 184 53 78 47 63 149 103 | 207 53 50 120 237 29 72 197 169 113 187 62 213 181 62 150 104 | 240 226 180 237 226 223 228 238 147 115 208 75 231 238 107 150 105 | 240 240 240 238 237 240 240 240 174 225 216 186 240 240 149 225 106 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 229 107 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 231 108 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 236 109 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 234 110 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 236 111 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 112 | 239 240 240 240 240 240 240 240 240 240 240 240 240 240 240 227 113 | 228 240 240 240 240 240 240 240 240 240 240 240 240 240 240 229 114 | 232 240 240 240 238 240 237 240 235 240 234 240 233 240 235 234 115 | 212 176 77 152 230 236 192 207 156 150 220 98 240 240 186 140 116 | 164 166 120 51 137 240 99 121 109 109 210 76 240 240 71 148 117 | 73 170 240 156 125 236 100 128 92 121 184 65 219 219 89 117 118 | 85 186 217 52 152 150 5 142 101 106 184 61 74 51 77 144 119 | 216 53 50 120 237 29 78 197 168 103 184 51 213 187 74 150 120 | 240 226 180 240 237 223 232 240 162 115 206 72 231 240 117 150 121 | 240 240 240 240 240 240 240 240 186 225 216 186 240 240 156 225 122 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 229 123 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 232 124 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 236 125 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 234 126 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 236 127 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 128 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 227 129 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 229 130 | 240 240 240 240 238 240 236 240 235 240 234 240 233 240 235 235 131 | 209 175 77 152 230 235 178 206 154 141 220 98 240 240 189 149 132 | 155 153 118 51 137 232 95 121 108 85 201 76 240 240 86 155 133 | 72 163 240 155 125 237 107 128 101 109 179 58 219 219 98 127 134 | 85 186 217 45 150 151 17 148 110 106 184 50 73 51 83 147 135 | 216 53 50 120 240 30 92 200 170 109 184 47 211 186 84 156 136 | 240 226 180 240 240 225 236 238 151 115 205 66 228 238 116 150 137 | 240 240 240 240 240 240 240 240 180 225 212 182 240 240 156 225 138 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 228 139 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 232 140 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 235 141 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 232 142 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 234 143 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 144 | 240 240 240 240 240 240 240 240 240 240 240 240 240 240 240 227 145 | 239 240 240 240 240 240 240 240 240 240 240 240 240 240 240 229 146 | 233 240 240 240 238 240 236 240 235 240 234 240 233 240 235 234 147 | 148 | 149 149 121 149 150 150 225 229 231 236 234 236 240 227 229 234 149 | 186 71 84 63 62 107 149 240 240 240 240 240 240 240 240 235 150 | 240 240 220 47 181 238 240 240 240 240 240 240 240 240 240 240 151 | 240 240 222 78 213 231 240 240 240 240 240 240 240 240 240 233 152 | 98 76 58 53 62 75 186 240 240 240 240 240 240 240 240 240 153 | 219 210 180 184 187 208 216 240 240 240 240 240 240 240 240 234 154 | 135 98 121 113 113 115 225 240 240 240 240 240 240 240 240 240 155 | 152 109 92 101 169 147 174 240 240 240 240 240 240 240 240 235 156 | 207 122 128 142 197 238 240 240 240 240 240 240 240 240 240 240 157 | 191 108 109 5 72 228 240 240 240 240 240 240 240 240 240 237 158 | 236 240 237 150 29 223 240 240 240 240 240 240 240 240 240 240 159 | 227 129 124 150 237 226 237 240 240 240 240 240 240 240 240 238 160 | 152 51 155 45 120 237 238 240 240 240 240 240 240 240 240 240 161 | 77 119 232 217 50 180 240 240 240 240 240 240 240 240 240 240 162 | 175 161 161 186 53 226 240 240 240 240 240 240 240 240 240 240 163 | 209 155 64 83 207 240 240 240 240 240 240 240 240 239 228 232 164 | 140 148 117 144 150 150 225 229 232 236 234 236 240 227 229 235 165 | 186 71 89 77 74 117 156 240 240 240 240 240 240 240 240 235 166 | 240 240 219 51 187 240 240 240 240 240 240 240 240 240 240 240 167 | 240 240 219 74 213 231 240 240 240 240 240 240 240 240 240 233 168 | 98 76 65 61 51 72 186 240 240 240 240 240 240 240 240 240 169 | 220 210 184 184 184 206 216 240 240 240 240 240 240 240 240 234 170 | 150 109 121 106 103 115 225 240 240 240 240 240 240 240 240 240 171 | 156 109 92 101 168 162 186 240 240 240 240 240 240 240 240 235 172 | 207 121 128 142 197 240 240 240 240 240 240 240 240 240 240 240 173 | 192 99 100 5 78 232 240 240 240 240 240 240 240 240 240 236 174 | 236 240 236 150 29 223 240 240 240 240 240 240 240 240 240 240 175 | 230 137 125 152 237 237 240 240 240 240 240 240 240 240 240 238 176 | 152 51 156 52 120 240 240 240 240 240 240 240 240 240 240 240 177 | 77 120 240 217 50 180 240 240 240 240 240 240 240 240 240 240 178 | 176 166 170 186 53 226 240 240 240 240 240 240 240 240 240 240 179 | 212 164 73 85 216 240 240 240 240 240 240 240 240 240 240 240 180 | 149 155 127 147 156 150 225 228 232 235 232 234 240 227 229 234 181 | 189 86 98 83 84 116 156 240 240 240 240 240 240 240 240 235 182 | 240 240 219 51 186 238 240 240 240 240 240 240 240 240 240 240 183 | 240 240 219 73 211 228 240 240 240 240 240 240 240 240 240 233 184 | 98 76 58 50 47 66 182 240 240 240 240 240 240 240 240 240 185 | 220 201 179 184 184 205 212 240 240 240 240 240 240 240 240 234 186 | 141 85 109 106 109 115 225 240 240 240 240 240 240 240 240 240 187 | 154 108 101 110 170 151 180 240 240 240 240 240 240 240 240 235 188 | 206 121 128 148 200 238 240 240 240 240 240 240 240 240 240 240 189 | 178 95 107 17 92 236 240 240 240 240 240 240 240 240 240 236 190 | 235 232 237 151 30 225 240 240 240 240 240 240 240 240 240 240 191 | 230 137 125 150 240 240 240 240 240 240 240 240 240 240 240 238 192 | 152 51 155 45 120 240 240 240 240 240 240 240 240 240 240 240 193 | 77 118 240 217 50 180 240 240 240 240 240 240 240 240 240 240 194 | 175 153 163 186 53 226 240 240 240 240 240 240 240 240 240 240 195 | 209 155 72 85 216 240 240 240 240 240 240 240 240 240 239 233 196 | 197 | -------------------------------------------------------------------------------- /example/hiro.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/artcom/react-three-arjs/a6112f27b285d583ee21df9b4662db38ac4913d3/example/hiro.png -------------------------------------------------------------------------------- /example/package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@artcom/react-three-arjs-example", 3 | "version": "0.1.0", 4 | "license": "MIT", 5 | "scripts": { 6 | "watch": "webpack serve --hot --https --mode=development", 7 | "build": "webpack --mode=production", 8 | "start": "http-server -S -C cert.pem dist/" 9 | }, 10 | "engines": { 11 | "node": ">= 16.15" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/artcom/react-three-arjs.git" 16 | }, 17 | "devDependencies": { 18 | "@babel/cli": "^7.22.6", 19 | "@babel/core": "^7.22.8", 20 | "@babel/preset-env": "^7.22.7", 21 | "@babel/preset-react": "^7.22.5", 22 | "@pmmmwh/react-refresh-webpack-plugin": "^0.5.10", 23 | "babel-loader": "^9.1.3", 24 | "copy-webpack-plugin": "^11.0.0", 25 | "eslint": "^8.44.0", 26 | "eslint-config-prettier": "^8.8.0", 27 | "eslint-plugin-compat": "^4.1.4", 28 | "eslint-plugin-import": "^2.27.5", 29 | "eslint-plugin-prettier": "^4.2.1", 30 | "eslint-plugin-react": "^7.32.2", 31 | "eslint-plugin-react-hooks": "^4.6.0", 32 | "html-webpack-plugin": "^5.5.3", 33 | "http-server": "^14.1.1", 34 | "react-refresh": "^0.14.0", 35 | "webpack": "^5.88.1", 36 | "webpack-cli": "^5.1.4", 37 | "webpack-dev-server": "^4.15.1" 38 | }, 39 | "dependencies": { 40 | "@ar-js-org/ar.js": "^3.4.5", 41 | "@artcom/react-three-arjs": "file:..", 42 | "@react-three/drei": "^9.78.1", 43 | "@react-three/fiber": "^8.13.4", 44 | "buffer": "^6.0.3", 45 | "core-js": "^3.31.1", 46 | "react": "^18.2.0", 47 | "react-dom": "^18.2.0", 48 | "three": "^0.154.0" 49 | }, 50 | "browserslist": [ 51 | "defaults" 52 | ] 53 | } 54 | -------------------------------------------------------------------------------- /example/src/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | @artcom/react-three-arjs example 5 | 6 | 7 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | -------------------------------------------------------------------------------- /example/src/index.js: -------------------------------------------------------------------------------- 1 | import { ARCanvas, ARMarker } from "@artcom/react-three-arjs" 2 | import React from "react" 3 | import { createRoot } from "react-dom/client" 4 | 5 | function Box() { 6 | return ( 7 | { 9 | window.alert("click") 10 | console.log(e) 11 | }}> 12 | 13 | 14 | 15 | ) 16 | } 17 | 18 | createRoot(document.getElementById("root")).render( 19 | console.log("Camera stream ready")} 22 | onCameraStreamError={() => console.error("Camera stream error")} 23 | onCreated={({ gl }) => { 24 | gl.setSize(window.innerWidth, window.innerHeight) 25 | }}> 26 | 27 | 28 | { 33 | console.log("Marker Found") 34 | }}> 35 | 36 | 37 | , 38 | ) 39 | -------------------------------------------------------------------------------- /example/webpack.config.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable no-undef */ 2 | 3 | const HtmlWebpackPlugin = require("html-webpack-plugin") 4 | const ReactRefreshWebpackPlugin = require("@pmmmwh/react-refresh-webpack-plugin") 5 | const CopyWebpackPlugin = require("copy-webpack-plugin") 6 | const path = require("path") 7 | 8 | module.exports = (_, { mode }) => ({ 9 | entry: { 10 | main: "./src/index.js", 11 | }, 12 | devServer: { 13 | host: "0.0.0.0", 14 | hot: true, 15 | }, 16 | resolve: { 17 | alias: { 18 | react: path.resolve("./node_modules/react"), 19 | three: path.resolve("./node_modules/three"), 20 | "react-dom": path.resolve("./node_modules/react-dom"), 21 | "@react-three/fiber": path.resolve("./node_modules/@react-three/fiber"), 22 | }, 23 | }, 24 | plugins: [ 25 | mode === "development" && new ReactRefreshWebpackPlugin(), 26 | new HtmlWebpackPlugin({ 27 | template: "src/index.html", 28 | }), 29 | new CopyWebpackPlugin({ 30 | patterns: [ 31 | { from: "./data/", to: "data/" }, 32 | ], 33 | }), 34 | ].filter(Boolean), 35 | devtool: mode === "development" ? "eval-source-map" : "source-map", 36 | module: { 37 | rules: [ 38 | { 39 | test: /\.js$/, 40 | exclude: /node_modules/, 41 | use: [ 42 | { 43 | loader: "babel-loader", 44 | options: { 45 | plugins: [mode === "development" && "react-refresh/babel"].filter(Boolean), 46 | }, 47 | }, 48 | ], 49 | }, 50 | ], 51 | }, 52 | }) 53 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "@artcom/react-three-arjs", 3 | "version": "0.5.5", 4 | "description": "AR.js with react-three-fiber ", 5 | "main": "lib/index.js", 6 | "files": [ 7 | "lib/" 8 | ], 9 | "scripts": { 10 | "watch": "babel --watch src --out-dir lib", 11 | "build": "babel src --out-dir lib --source-maps" 12 | }, 13 | "engines": { 14 | "node": ">= 16.15" 15 | }, 16 | "repository": { 17 | "type": "git", 18 | "url": "git+https://github.com/artcom/react-three-arjs.git" 19 | }, 20 | "keywords": [ 21 | "react", 22 | "react-three-fiber", 23 | "ar.js", 24 | "ar", 25 | "three" 26 | ], 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/artcom/react-three-arjs/issues" 30 | }, 31 | "homepage": "https://github.com/artcom/react-three-arjs#readme", 32 | "peerDependencies": { 33 | "@ar-js-org/ar.js": "^3.4.5", 34 | "@react-three/fiber": "^8.13.4", 35 | "buffer": "^6.0.3", 36 | "react": "^18.2.0", 37 | "react-dom": "^18.2.0", 38 | "three": "^0.154.0" 39 | }, 40 | "devDependencies": { 41 | "@ar-js-org/ar.js": "^3.4.5", 42 | "@babel/cli": "^7.22.6", 43 | "@babel/core": "^7.22.8", 44 | "@babel/preset-env": "^7.22.7", 45 | "@babel/preset-react": "^7.22.5", 46 | "@react-three/fiber": "^8.13.4", 47 | "buffer": "^6.0.3", 48 | "core-js": "^3.31.1", 49 | "eslint": "^8.44.0", 50 | "eslint-config-prettier": "^8.8.0", 51 | "eslint-plugin-compat": "^4.1.4", 52 | "eslint-plugin-import": "^2.27.5", 53 | "eslint-plugin-prettier": "^4.2.1", 54 | "eslint-plugin-react": "^7.32.2", 55 | "eslint-plugin-react-hooks": "^4.6.0", 56 | "prettier": "^3.0.0", 57 | "react": "^18.2.0", 58 | "react-dom": "^18.2.0", 59 | "three": "^0.154.0" 60 | }, 61 | "dependencies": { 62 | "react-use-measure": "^2.1.1" 63 | } 64 | } 65 | -------------------------------------------------------------------------------- /src/ar/ar.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable import/named */ 2 | import { ArToolkitContext, ArToolkitSource } from "@ar-js-org/ar.js/three.js/build/ar-threex" 3 | import { useFrame, useThree } from "@react-three/fiber" 4 | import React, { createContext, useCallback, useEffect, useMemo } from "react" 5 | 6 | const ARContext = createContext({}) 7 | const videoDomElemSelector = "#arjs-video" 8 | 9 | const AR = React.memo(function AR({ 10 | tracking = true, 11 | children, 12 | sourceType, 13 | patternRatio, 14 | matrixCodeType, 15 | detectionMode, 16 | cameraParametersUrl, 17 | onCameraStreamReady, 18 | onCameraStreamError, 19 | }) { 20 | const { gl, camera } = useThree() 21 | 22 | const arContext = useMemo(() => { 23 | const arToolkitSource = new ArToolkitSource({ sourceType }) 24 | const arToolkitContext = new ArToolkitContext({ 25 | cameraParametersUrl, 26 | detectionMode, 27 | patternRatio, 28 | matrixCodeType, 29 | }) 30 | 31 | return { arToolkitContext, arToolkitSource } 32 | }, [patternRatio, matrixCodeType, cameraParametersUrl, detectionMode, sourceType]) 33 | 34 | const onResize = useCallback(() => { 35 | const { arToolkitContext, arToolkitSource } = arContext 36 | 37 | arToolkitSource.onResizeElement() 38 | arToolkitSource.copyElementSizeTo(gl.domElement) 39 | if (arToolkitContext.arController !== null) { 40 | arToolkitSource.copyElementSizeTo(arToolkitContext.arController.canvas) 41 | camera.projectionMatrix.copy(arToolkitContext.getProjectionMatrix()) 42 | } 43 | }, [gl, arContext, camera]) 44 | 45 | const onUnmount = useCallback(() => { 46 | window.removeEventListener("resize", onResize) 47 | 48 | arContext.arToolkitContext.arController.dispose() 49 | if (arContext.arToolkitContext.arController.cameraParam) { 50 | arContext.arToolkitContext.arController.cameraParam.dispose() 51 | } 52 | 53 | delete arContext.arToolkitContext 54 | delete arContext.arToolkitSource 55 | 56 | const video = document.querySelector(videoDomElemSelector) 57 | if (video) { 58 | video.srcObject.getTracks().map(track => track.stop()) 59 | video.remove() 60 | } 61 | }, [onResize, arContext]) 62 | 63 | useEffect(() => { 64 | arContext.arToolkitSource.init(() => { 65 | const video = document.querySelector(videoDomElemSelector) 66 | video.style.position = "fixed" 67 | 68 | video.onloadedmetadata = () => { 69 | console.log("actual source dimensions", video.videoWidth, video.videoHeight) 70 | 71 | if (video.videoWidth > video.videoHeight) { 72 | arContext.arToolkitContext.arController.orientation = "landscape" 73 | arContext.arToolkitContext.arController.options.orientation = "landscape" 74 | } else { 75 | arContext.arToolkitContext.arController.orientation = "portrait" 76 | arContext.arToolkitContext.arController.options.orientation = "portrait" 77 | } 78 | 79 | if (onCameraStreamReady) { 80 | onCameraStreamReady() 81 | } 82 | onResize() 83 | } 84 | }, onCameraStreamError) 85 | 86 | arContext.arToolkitContext.init(() => 87 | camera.projectionMatrix.copy(arContext.arToolkitContext.getProjectionMatrix()), 88 | ) 89 | 90 | window.addEventListener("resize", onResize) 91 | 92 | return onUnmount 93 | }, [arContext, camera, onCameraStreamReady, onCameraStreamError, onResize, onUnmount]) 94 | 95 | useFrame(() => { 96 | if (!tracking) { 97 | return 98 | } 99 | 100 | if (arContext.arToolkitSource && arContext.arToolkitSource.ready !== false) { 101 | arContext.arToolkitContext.update(arContext.arToolkitSource.domElement) 102 | } 103 | }) 104 | 105 | const value = useMemo(() => ({ arToolkitContext: arContext.arToolkitContext }), [arContext]) 106 | 107 | return {children} 108 | }) 109 | 110 | const useAR = () => { 111 | const arValue = React.useContext(ARContext) 112 | return React.useMemo(() => ({ ...arValue }), [arValue]) 113 | } 114 | 115 | export { AR, useAR } 116 | -------------------------------------------------------------------------------- /src/ar/arCanvas.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable indent */ 2 | /* eslint-disable react/jsx-indent */ 3 | /* eslint-disable react/jsx-pascal-case */ 4 | 5 | import React from "react" 6 | import { Canvas, events } from "@react-three/fiber" 7 | 8 | import { AR } from "./ar" 9 | 10 | const eventManagerFactory = state => ({ 11 | ...events(state), 12 | 13 | compute(event, state) { 14 | state.pointer.set( 15 | (event.clientX / state.size.width) * 2 - 1, 16 | -(event.clientY / state.size.height) * 2 + 1, 17 | ) 18 | state.raycaster.setFromCamera(state.pointer, state.camera) 19 | }, 20 | }) 21 | 22 | const ARCanvas = ({ 23 | arEnabled = true, 24 | tracking = true, 25 | children, 26 | patternRatio = 0.5, 27 | detectionMode = "mono_and_matrix", 28 | cameraParametersUrl = "data/camera_para.dat", 29 | matrixCodeType = "3x3", 30 | sourceType = "webcam", 31 | onCameraStreamReady, 32 | onCameraStreamError, 33 | ...props 34 | }) => ( 35 | 39 | {arEnabled ? ( 40 | 49 | {children} 50 | 51 | ) : ( 52 | children 53 | )} 54 | 55 | ) 56 | 57 | export default ARCanvas 58 | -------------------------------------------------------------------------------- /src/ar/arMarker.js: -------------------------------------------------------------------------------- 1 | /* eslint-disable import/named */ 2 | /* eslint-disable no-underscore-dangle */ 3 | import { ArMarkerControls } from "@ar-js-org/ar.js/three.js/build/ar-threex" 4 | import { useFrame } from "@react-three/fiber" 5 | import React, { useEffect, useRef, useState } from "react" 6 | import { useAR } from "./ar" 7 | 8 | const ARMarker = ({ 9 | children, 10 | type, 11 | barcodeValue, 12 | patternUrl, 13 | params, 14 | onMarkerFound, 15 | onMarkerLost, 16 | }) => { 17 | const markerRoot = useRef() 18 | const { arToolkitContext } = useAR() 19 | const [isFound, setIsFound] = useState(false) 20 | 21 | useEffect(() => { 22 | if (!arToolkitContext) { 23 | return 24 | } 25 | 26 | const markerControls = new ArMarkerControls(arToolkitContext, markerRoot.current, { 27 | type, 28 | barcodeValue: type === "barcode" ? barcodeValue : null, 29 | patternUrl: type === "pattern" ? patternUrl : null, 30 | ...params, 31 | }) 32 | 33 | return () => { 34 | const index = arToolkitContext._arMarkersControls.indexOf(markerControls) 35 | arToolkitContext._arMarkersControls.splice(index, 1) 36 | } 37 | }, []) 38 | 39 | useFrame(() => { 40 | if (markerRoot.current.visible && !isFound) { 41 | setIsFound(true) 42 | if (onMarkerFound) { 43 | onMarkerFound() 44 | } 45 | } else if (!markerRoot.current.visible && isFound) { 46 | setIsFound(false) 47 | if (onMarkerLost) { 48 | onMarkerLost() 49 | } 50 | } 51 | }) 52 | 53 | return {children} 54 | } 55 | 56 | export default ARMarker 57 | -------------------------------------------------------------------------------- /src/ar/index.js: -------------------------------------------------------------------------------- 1 | import ARMarker from "./arMarker" 2 | import ARCanvas from "./arCanvas" 3 | 4 | export { ARCanvas, ARMarker } 5 | -------------------------------------------------------------------------------- /src/index.js: -------------------------------------------------------------------------------- 1 | export * from "./ar" 2 | --------------------------------------------------------------------------------