p5.simpleAR is a simple JavaScript supplement(mini-library) file to easily convert existing sketches into AR for PCs and smart devices.
Concept movie.
Now, the latest version is 0.7.0
(beta release).
First, Please print the marker(#6) below, or just view it on the phone.
https://user-images.githubusercontent.com/14086390/230766242-8c15c1ae-b734-4790-9f74-b3de3d875d8f.mp4
<script src="https://tetunori.github.io/p5.simpleAR/dist/v0.7.0/p5SimpleAR.js"></script>
Just replace createCanvas
with createARCanvas
.
createCanvas(100, 100);
->
createARCanvas(100, 100);
OK, done.
Then, your sketch will be shown on the AR Marker.
This function deeply depends on AR.js. Please see the requirement of the library.
## Markers
We can choose markers from the 64 images below.
[AR Markers](https://github.com/tetunori/p5.simpleAR/tree/main/markers/)
| 0 | 1 | ... | 63 |
| :---: | :---: | :---: | :---: |
| |
| ... |
|
## createARCanvas
```javascript
createARCanvas(w, h, [renderer], [params])
```
Replace `createCanvas` with this function.
So, basically, this API has same parameters as `createCanvas`.
> **Warning**
> AR function does not work well in `WEBGL` mode...
`params` is an original `Object` parameter for `p5.simpleAR`.
### Properties:
| name | note |
| ---- | ---- |
| `scale` | `Number`: Scale of the sketch. Marker(3x3 dots) size is defined as `1`. Default value is `3`. |
| `opacity` | `Number`: Opacity of the sketch. Input a value between `0.0` and `1.0`. Default value is `1.0`. |
| `markerId` | `Number`: Id of the marker data. Input a integer value between `0` and `63`. Default value is `6`. |
```javascript
// Call like this
// createCanvas(100, 200);
createARCanvas(100, 200, P2D, { scale: 5, opacity: 0.7, markerId: 1 });
```
### Sample
- createARCanvas Demo
- [On GitHub](https://tetunori.github.io/p5.simpleAR/sample/parameters/index.html), [Source code On GitHub](https://github.com/tetunori/p5.simpleAR/tree/main/sample/parameters/)
- [On OpenProcessing](https://openprocessing.org/sketch/1898838)
- つぶやきProcessing Demo
- [Demo On GitHub](https://tetunori.github.io/p5.simpleAR/sample/tsubuyaki/index.html), [Source code On GitHub](https://github.com/tetunori/p5.simpleAR/tree/main/sample/tsubuyaki/)
- [On OpenProcessing](https://openprocessing.org/sketch/1899101)
## createARGraphics
```javascript
createARGraphics(w, h, [renderer], [params])
```
Replace `createGraphics` with this function.
So, basically, this API has same parameters as `createGraphics`.
By using this API, You can handle multiple markers.
> **Warning**
> AR function does not work well in `WEBGL` mode...
> **Warning**
> `createARGraphics` and `createARCanvas` cannot coexist.
`params` is an original `Object` parameter for `p5.simpleAR`.
### Properties:
| name | note |
| ---- | ---- |
| `scale` | `Number`: Scale of the sketch. Marker(3x3 dots) size is defined as `1`. Default value is `3`. |
| `opacity` | `Number`: Opacity of the sketch. Input a value between `0.0` and `1.0`. Default value is `1.0`. |
| `markerId` | `Number`: Id of the marker data. Input a integer value between `0` and `63`. Default value is `6`. Be sure to set unique id for each graphics. |
```javascript
// Call like this
// createGraphics(100, 200);
createARGraphics(100, 200, P2D, { scale: 5, opacity: 0.7, markerId: 1 });
```
### Sample
- createARGraphics Multi Marker Demo
- [On GitHub](https://tetunori.github.io/p5.simpleAR/sample/createARGraphics/index.html), [Source code On GitHub](https://github.com/tetunori/p5.simpleAR/tree/main/sample/createARGraphics/)
- [On OpenProcessing](https://openprocessing.org/sketch/1898840)
## p5SimpleARGetMarkerProperty
```javascript
p5SimpleARGetMarkerProperty([markerId])
```
Return a `Object` that has some information on the specified marker.
### Parameter:
| name | note |
| ---- | ---- |
| `markerId` | `Number`: Id of the marker data. If you do not specify this, default value `6` will be set. |
### Return Object Property:
| name | note |
| ---- | ---- |
| `markerId` | `Number`: Id of the specified marker data. |
| `markerVisible` | `Boolean`: Whether the specified marker is visible or not. |
| `rotation` | `Object`: Rotation information of the marker. Value format(radians/degrees) depends on the p5.js angle-mode setting(see [angleMode()](https://p5js.org/reference/#/p5/angleMode)).
**Property:**
x: Pitch, rotation about the X-axis.
y: Yaw, rotation about the Y-axis.
z: Roll, rotation about the Z-axis. |
| `position` | `Object`: Position information of the marker. This uses a right-handed coordinate system where the negative Z axis extends into the screen.
**Property:**
x: Negative X axis extends left. Positive X Axis extends right.
y: Negative Y axis extends down. Positive Y Axis extends up.
z: Negative Z axis extends in. Positive Z Axis extends out. |
```javascript
const markerProps = p5SimpleARGetMarkerProperty(6);
```
Received `Object` consists of objects as below.
```javascript
// Return value of p5SimpleARGetMarkerProperty() with angleMode 'DEGREES'
{
markerId: 6,
markerVisible: true,
rotation: {
x: 105.35504555645193,
y: -11.201540264006956,
z: 14.797999140808324,
},
position: {
x: -0.2322715400514963,
y: 0.956252183544887,
z: -12.228084209054696,
}
}
```
### Sample
- Simple GetMarkerProperty Demo
- [On GitHub](https://tetunori.github.io/p5.simpleAR/sample/getMarkerProperty/index.html), [Source code On GitHub](https://github.com/tetunori/p5.simpleAR/tree/main/sample/getMarkerProperty/)
- [On OpenProcessing](https://openprocessing.org/sketch/1899122)
- Position/Rotation Demo
- [On GitHub](https://tetunori.github.io/p5.simpleAR/sample/propPosRot/index.html), [Source code On GitHub](https://github.com/tetunori/p5.simpleAR/tree/main/sample/propPosRot/)
- [On OpenProcessing](https://openprocessing.org/sketch/1899120)
## p5SimpleARSetARProperty
```javascript
p5SimpleARSetARProperty(prop, [markerId])
```
Set AR poperties(rotation and position).
### Parameter:
| name | note |
| ---- | ---- |
| `prop` | `Object`: AR Properties to be set. See the table below in detail. |
| `markerId` | `Number`: Id of the marker data. If you do not specify this, default value `6` will be set. |
#### `prop` Properties:
| name | note |
| ---- | ---- |
| `rotation` | `Object`: Rotation information of the AR canvas. Value format(radians/degrees) depends on the p5.js angle-mode setting(see [angleMode()](https://p5js.org/reference/#/p5/angleMode)).
**Property:**
x: rotation about the X-axis.
y: rotation about the Y-axis.
z: rotation about the Z-axis.
order: rotation order default value is `'XYZ'`. |
| `position` | `Object`: Position information of the AR canvas.
**Property:**
x: translation in the X-axis direction.
y: translation in the Y-axis direction.
z: translation in the Z-axis direction. |
```javascript
const arProps = {
rotation: {
x: PI/4,
y: 0,
z: -PI/2,
order: 'ZYX',
},
position: {
x: 0,
y: 1,
z: 2,
},
};
p5SimpleARSetARProperty(arProps);
```
Combined with `p5SimpleARGetMarkerProperty()`, we can make use of existing 3D `p5.js` sketches as it is.
```javascript
// Get current marker poperty
const markerProps = p5SimpleARGetMarkerProperty();
// AR properties
const arProps = {
// Rotate canvas so that it always faces the camera.
// Default rotation order is 'XYZ' so we should use 'ZYX' to get back.
rotation: {
x: -markerProps.rotation.x,
y: -markerProps.rotation.y,
z: -markerProps.rotation.z,
order: 'ZYX',
},
position: {
x: 0,
y: 1,
z: 0,
},
};
p5SimpleARSetARProperty(arProps);
// Rotate objects in canvas
rotateX(PI - markerProps.rotation.x);
rotateY(PI - markerProps.rotation.y);
rotateZ(PI - markerProps.rotation.z);
// draw!
box(70);
```
### Sample
- Simple SetARProperty Demo
- [On GitHub](https://tetunori.github.io/p5.simpleAR/sample/setARProperty/index.html), [Source code On GitHub](https://github.com/tetunori/p5.simpleAR/tree/main/sample/setARProperty/)
- [On OpenProcessing](https://openprocessing.org/sketch/1920131)
- Floating box Demo
- [On GitHub](https://tetunori.github.io/p5.simpleAR/sample/floatingBox/index.html), [Source code On GitHub](https://github.com/tetunori/p5.simpleAR/tree/main/sample/floatingBox/)
- [On OpenProcessing](https://openprocessing.org/sketch/1920132)
- BMWalker Demo
- [On GitHub](https://tetunori.github.io/p5.simpleAR/sample/bmwalker/index.html), [Source code On GitHub](https://github.com/tetunori/p5.simpleAR/tree/main/sample/bmwalker/)
- [On OpenProcessing](https://openprocessing.org/sketch/1920133)
## p5SimpleAREnableGesture
```javascript
p5SimpleAREnableGesture([bEnable])
```
Enable/disalbe flick and pinch-in/out gesture to rotate or zoom-in/out.
### Parameter:
| name | note |
| ---- | ---- |
| `bEnable` | `Boolean`: Specify enable(`true`) or disable(`false`). |
```javascript
p5SimpleAREnableGesture(true);
```
### Sample
- Gesture Demo
- [On GitHub](https://tetunori.github.io/p5.simpleAR/sample/enableGesture/index.html), [Source code On GitHub](https://github.com/tetunori/p5.simpleAR/tree/main/sample/enableGesture/)
- [On OpenProcessing](https://openprocessing.org/sketch/1905887)
## p5SimpleARMarkerFound
```javascript
p5SimpleARMarkerFound([markerId])
```
The `p5SimpleARMarkerFound` function is called once when a specified marker has been found.
### Parameter:
| name | note |
| ---- | ---- |
| `markerId` | `Number`: Id of the found marker. |
```javascript
// Overwrite like below.
function p5SimpleARMarkerFound(markerId) {
console.log('p5SimpleARMarkerFound: ' + markerId);
}
```
### Sample
- Found/Lost callback Demo
- [On GitHub](https://tetunori.github.io/p5.simpleAR/sample/foundLost/index.html), [Source code On GitHub](https://github.com/tetunori/p5.simpleAR/tree/main/sample/foundLost/)
- [On OpenProcessing](https://openprocessing.org/sketch/1899233)
## p5SimpleARMarkerLost
```javascript
p5SimpleARMarkerLost([markerId])
```
The `p5SimpleARMarkerLost` function is called once when a specified marker has been lost.
### Parameter:
| name | note |
| ---- | ---- |
| `markerId` | `Number`: Id of the lost marker. |
```javascript
// Overwrite like below.
function p5SimpleARMarkerLost(markerId) {
console.log('p5SimpleARMarkerLost: ' + markerId);
}
```
### Sample
- Found/Lost callback Demo
- [On GitHub](https://tetunori.github.io/p5.simpleAR/sample/foundLost/index.html), [Source code On GitHub](https://github.com/tetunori/p5.simpleAR/tree/main/sample/foundLost/)
- [On OpenProcessing](https://openprocessing.org/sketch/1899233)
dat.GUI
dat.GUI
, please new dat.GUI
after completion of loading this tool. Otherwise you will not be able to use it correctly. So, it is recommended to do it in the setup()
function. See this sample and this source for example.MIT license
Copyright (c) 2023 Tetsunori Nakayama.
Tetsunori Nakayama.