-
Notifications
You must be signed in to change notification settings - Fork 48
/
minimal__modules__threejs_overlay.html
288 lines (186 loc) · 8.22 KB
/
minimal__modules__threejs_overlay.html
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Beyond Reality Face SDK - BRFv5 - Face Tracking for Browser/JavaScript - Minimal ThreeJS Example</title>
<style>
html, body { width: 100%; height: 100%; background-color: #ffffff; margin: 0; padding: 0; overflow: hidden; }
</style>
</head>
<body>
<!--
This is a minimal modules example. It's much shorter than minimal_no_modules.html, because we already
provide lots of functionality within the modules.
-->
<video id="_webcam" style="display: none;" playsinline></video>
<canvas id="_imageData" style="position: absolute"></canvas>
<!-- TODO: ThreeJS canvas, see configureTracking() for scaling -->
<canvas id="__brfv5__threejs_canvas" style="position: absolute; transform: scale(1.0); transform-origin: 0 0"></canvas>
<script type="module">
import { brfv5 } from './js/brfv5/brfv5__init.js'
import { loadBRFv5Model } from './js/brfv5/brfv5__init.js'
import { configureCameraInput } from './js/brfv5/brfv5__configure.js'
import { configureFaceTracking } from './js/brfv5/brfv5__configure.js'
import { configureNumFacesToTrack } from './js/brfv5/brfv5__configure.js'
import { startCamera } from './js/utils/utils__camera.js'
import { drawInputMirrored } from './js/utils/utils__canvas.js'
import { drawCircles } from './js/utils/utils__canvas.js'
import { drawRect, drawRects } from './js/utils/utils__canvas.js'
const _appId = 'brfv5.browser.minimal.modules.threejs' // (mandatory): 8 to 64 characters, a-z . 0-9 allowed
const _webcam = document.getElementById('_webcam')
const _imageData = document.getElementById('_imageData')
// Those variables will be retrieved from the stream and the library.
let _brfv5Manager = null
let _brfv5Config = null
let _width = 0
let _height = 0
// loadBRFv5Model and openCamera are being done simultaneously thanks to Promises. Both call
// configureTracking which only gets executed once both Promises were successful. Once configured
// trackFaces will do the tracking work and draw the results.
startCamera(_webcam, { width: 640, height: 480, frameRate: 30, facingMode: 'user' }).then(({ video }) => {
console.log('openCamera: done: ' + video.videoWidth + 'x' + video.videoHeight)
_width = video.videoWidth
_height = video.videoHeight
_imageData.width = _width
_imageData.height = _height
configureTracking()
}).catch((e) => { if(e) { console.error('Camera failed: ', e) } })
loadBRFv5Model('42l', 6, './js/brfv5/models/', _appId,
(progress) => { console.log(progress) }).then(({ brfv5Manager, brfv5Config }) => {
console.log('loadBRFv5Model: done')
_brfv5Manager = brfv5Manager
_brfv5Config = brfv5Config
configureTracking()
}).catch((e) => { console.error('BRFv5 failed: ', e) })
const configureTracking = () => {
if(_brfv5Config !== null && _width > 0 && _modelsLoaded) {
configureCameraInput(_brfv5Config, _width, _height)
configureNumFacesToTrack(_brfv5Config, 1)
configureFaceTracking(_brfv5Config, 3, true)
// Free rotation results in better tracking, but lower performance.
// _brfv5Config.faceTrackingConfig.enableFreeRotation = true
// _brfv5Config.faceTrackingConfig.maxRotationZReset = 34.0
_brfv5Manager.configure(_brfv5Config)
update3DLayout(t3d, _width, _height)
_threejs.style.transform = 'scale(' + (1.0 / t3d.sceneScale) + ')'
trackFaces()
}
}
const trackFaces = () => {
if(!_brfv5Manager || !_brfv5Config || !_imageData) { return }
const ctx = _imageData.getContext('2d')
drawInputMirrored(ctx, _width, _height, _webcam)
_brfv5Manager.update(ctx.getImageData(0, 0, _width, _height))
let doDrawFaceDetection = !_brfv5Config.enableFaceTracking
if(_brfv5Config.enableFaceTracking) {
const sizeFactor = Math.min(_width, _height) / 480.0
const faces = _brfv5Manager.getFaces()
for(let i = 0; i < faces.length; i++) {
const face = faces[i]
if(face.state === brfv5.BRFv5State.FACE_TRACKING) {
// Update the 3d model placement.
updateByFace(t3d, face, i, true)
drawRect(ctx, _brfv5Config.faceTrackingConfig.regionOfInterest, '#00a0ff', 2.0)
drawCircles(ctx, face.landmarks, '#00a0ff', 2.0 * sizeFactor)
drawRect(ctx, face.bounds, '#ffffff', 1.0)
} else {
doDrawFaceDetection = true
updateByFace(t3d, face, i, false)
}
}
render3DScene(t3d)
}
if(doDrawFaceDetection) {
// Only draw face detection results, if face detection was performed.
drawRect( ctx, _brfv5Config.faceDetectionConfig.regionOfInterest, '#ffffff', 2.0)
drawRects(ctx, _brfv5Manager.getDetectedRects(), '#00a0ff', 1.0)
drawRects(ctx, _brfv5Manager.getMergedRects(), '#ffffff', 3.0)
}
requestAnimationFrame(trackFaces)
}
//
//
//
// Find below most of the additional ThreeJS code for setup, loading models, setting models by face.
//
//
//
import { create3DScene } from './js/threejs/threejs__setup.js'
import { update3DLayout } from './js/threejs/threejs__setup.js'
import { updateByFace } from './js/threejs/threejs__brfv5_mapping.js'
import { hideAllBaseNodes } from './js/threejs/threejs__brfv5_mapping.js'
import { render3DScene, setNumFaces, prepareModelNodes,
hideAllModels, turnIntoOcclusionObject, add3DModel,
set3DModelByName } from './js/threejs/threejs__setup.js'
import { load3DModelList,
getModelInstance } from './js/threejs/threejs__loading.js'
let _models = [
// Load the occlusion model (an invisible head). It hides anything behind it.
{
pathToModel: './assets/3d/occlusion_head.artov5',
pathToTextures: './assets/3d/textures/',
nameModel: null,
isOcclusionModel: true,
isMaterialCollection: false
},
// The actual 3d model as exported from ThreeJS editor.
// either rayban.json or earrings.json
// Textures might be embedded or set as file name in a certain path.
{
pathToModel: './assets/3d/rayban.artov5',
pathToTextures: './assets/3d/textures/',
nameModel: null,
isOcclusionModel: false,
isMaterialCollection: false
},
{
pathToModel: './assets/3d/pearlearring_left.artov5',
pathToTextures: './assets/3d/textures/',
nameModel: null,
isOcclusionModel: false,
isMaterialCollection: false
},
{
pathToModel: './assets/3d/pearlearring_right.artov5',
pathToTextures: './assets/3d/textures/',
nameModel: null,
isOcclusionModel: false,
isMaterialCollection: false
}
]
// TODO: The ThreeJS namespace object
const t3d = { sceneScale: 2.0 } // sceneScale: 2.0 increases render quality, 1.0 for lower render quality
const _threejs = document.getElementById('__brfv5__threejs_canvas')
let _modelsLoaded = false
// TODO: Create scene and load the models:
if(create3DScene(t3d, _threejs)) {
setNumFaces(t3d, 1)
hideAllBaseNodes(t3d)
load3DModelList({ fileList: _models, onProgress: null }).then(() => {
prepareModelNodes(t3d)
hideAllModels(t3d)
for(let i = 0; i < _models.length; i++) {
const model = _models[i]
const obj3d = getModelInstance(t3d, model.pathToModel)
if(model.isOcclusionModel) {
turnIntoOcclusionObject(obj3d)
}
if(!model.isMaterialCollection && obj3d) {
add3DModel(t3d, obj3d)
}
}
for(let i = 0; i < _models.length; i++) {
const model = _models[i]
if(!model.isMaterialCollection) {
set3DModelByName(t3d, model.pathToModel, model.nameModel, (url, name) => {
console.error('SETTING_3D_MODEL_NAME_FAILED:', name, url)
})
}
}
_modelsLoaded = true
configureTracking()
}).catch((e) => { console.error(e) })
}
</script>
</body>
</html>