-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
arjs-anchor.js
136 lines (114 loc) · 5.17 KB
/
arjs-anchor.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// @namespace
var ARjs = ARjs || {}
// TODO this is a controls... should i give the object3d here ?
// not according to 'no three.js dependancy'
/**
* Create an anchor in the real world
*
* @param {ARjs.Session} arSession - the session on which we create the anchor
* @param {Object} markerParameters - parameter of this anchor
*/
ARjs.Anchor = function(arSession, markerParameters){
var _this = this
var arContext = arSession.arContext
var scene = arSession.parameters.scene
var camera = arSession.parameters.camera
this.arSession = arSession
this.parameters = markerParameters
// log to debug
console.log('ARjs.Anchor -', 'changeMatrixMode:', this.parameters.changeMatrixMode, '/ markersAreaEnabled:', markerParameters.markersAreaEnabled)
var markerRoot = new THREE.Group
scene.add(markerRoot)
// set controlledObject depending on changeMatrixMode
if( markerParameters.changeMatrixMode === 'modelViewMatrix' ){
var controlledObject = markerRoot
}else if( markerParameters.changeMatrixMode === 'cameraTransformMatrix' ){
var controlledObject = camera
}else console.assert(false)
if( markerParameters.markersAreaEnabled === false ){
var markerControls = new THREEx.ArMarkerControls(arContext, controlledObject, markerParameters)
this.controls = markerControls
}else{
// sanity check - MUST be a trackingBackend with markers
console.assert( arContext.parameters.trackingBackend === 'artoolkit' || arContext.parameters.trackingBackend === 'aruco' )
// honor markers-page-resolution for https://webxr.io/augmented-website
if( location.hash.substring(1).startsWith('markers-page-resolution=') === true ){
// get resolutionW/resolutionH from url
var markerPageResolution = location.hash.substring(1)
var matches = markerPageResolution.match(/markers-page-resolution=(\d+)x(\d+)/)
console.assert(matches.length === 3)
var resolutionW = parseInt(matches[1])
var resolutionH = parseInt(matches[2])
var arContext = arSession.arContext
// generate and store the ARjsMultiMarkerFile
ARjs.MarkersAreaUtils.storeMarkersAreaFileFromResolution(arContext.parameters.trackingBackend, resolutionW, resolutionH)
}
// if there is no ARjsMultiMarkerFile, build a default one
if( localStorage.getItem('ARjsMultiMarkerFile') === null ){
ARjs.MarkersAreaUtils.storeDefaultMultiMarkerFile(arContext.parameters.trackingBackend)
}
// get multiMarkerFile from localStorage
console.assert( localStorage.getItem('ARjsMultiMarkerFile') !== null )
var multiMarkerFile = localStorage.getItem('ARjsMultiMarkerFile')
// set controlledObject depending on changeMatrixMode
if( markerParameters.changeMatrixMode === 'modelViewMatrix' ){
var parent3D = scene
}else if( markerParameters.changeMatrixMode === 'cameraTransformMatrix' ){
var parent3D = camera
}else console.assert(false)
// build a multiMarkerControls
var multiMarkerControls = ARjs.MarkersAreaControls.fromJSON(arContext, parent3D, controlledObject, multiMarkerFile)
this.controls = multiMarkerControls
// honor markerParameters.changeMatrixMode
multiMarkerControls.parameters.changeMatrixMode = markerParameters.changeMatrixMode
// TODO put subMarkerControls visibility into an external file. with 2 handling for three.js and babylon.js
// create ArMarkerHelper - useful to debug - super three.js specific
var markerHelpers = []
multiMarkerControls.subMarkersControls.forEach(function(subMarkerControls){
// add an helper to visuable each sub-marker
var markerHelper = new THREEx.ArMarkerHelper(subMarkerControls)
markerHelper.object3d.visible = false
// subMarkerControls.object3d.add( markerHelper.object3d )
subMarkerControls.object3d.add( markerHelper.object3d )
// add it to markerHelpers
markerHelpers.push(markerHelper)
})
// define API specific to markersArea
this.markersArea = {}
this.markersArea.setSubMarkersVisibility = function(visible){
markerHelpers.forEach(function(markerHelper){
markerHelper.object3d.visible = visible
})
}
}
this.object3d = new THREE.Group()
//////////////////////////////////////////////////////////////////////////////
// THREEx.ArSmoothedControls
//////////////////////////////////////////////////////////////////////////////
var shouldBeSmoothed = true
if( shouldBeSmoothed === true ){
// build a smoothedControls
var smoothedRoot = new THREE.Group()
scene.add(smoothedRoot)
var smoothedControls = new THREEx.ArSmoothedControls(smoothedRoot)
smoothedRoot.add(this.object3d)
}else{
markerRoot.add(this.object3d)
}
//////////////////////////////////////////////////////////////////////////////
// Code Separator
//////////////////////////////////////////////////////////////////////////////
this.update = function(){
// update _this.object3d.visible
_this.object3d.visible = _this.object3d.parent.visible
// console.log('controlledObject.visible', _this.object3d.parent.visible)
if( smoothedControls !== undefined ){
// update smoothedControls parameters depending on how many markers are visible in multiMarkerControls
if( multiMarkerControls !== undefined ){
multiMarkerControls.updateSmoothedControls(smoothedControls)
}
// update smoothedControls
smoothedControls.update(markerRoot)
}
}
}