/
arjs.service.ts
186 lines (161 loc) · 4.92 KB
/
arjs.service.ts
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
import { Injectable } from '@angular/core';
// import { ArToolkitSource, ArToolkitContext }from 'threex';
let onRenderFcts = [];
let scene;
let renderer;
let arToolkitSource;
let camera;
let arToolkitContext;
@Injectable({
providedIn: 'root'
})
export class ArjsService {
constructor() { }
initAR(canvas) {
renderer = this.createRenderer(canvas);
scene = this.createScene();
arToolkitContext = this.createToolkitContext();
arToolkitSource = this.handleARToolkitSource();
this.initCameraControls();
this.createTorus();
// render the scene
onRenderFcts.push(function(){
renderer.render( scene, camera );
})
// run the rendering loop
var lastTimeMsec= null
requestAnimationFrame(function animate(nowMsec){
// keep looping
requestAnimationFrame( animate );
// measure time
lastTimeMsec = lastTimeMsec || nowMsec-1000/60
var deltaMsec = Math.min(200, nowMsec - lastTimeMsec)
lastTimeMsec = nowMsec
// call each update function
onRenderFcts.forEach(function(onRenderFct){
onRenderFct(deltaMsec/1000, nowMsec/1000)
})
})
}
createRenderer(canvas){
var renderer = new THREE.WebGLRenderer({
antialias: true,
alpha: true,
canvas: canvas
});
renderer.setClearColor(new THREE.Color('lightgrey'), 0)
renderer.setSize( 640, 480 );
renderer.domElement.style.position = 'absolute'
renderer.domElement.style.top = '0px'
renderer.domElement.style.left = '0px'
return renderer;
}
createScene(){
var scene = new THREE.Scene();
// Create a camera
camera = new THREE.Camera();
scene.add(camera);
return scene;
}
handleARToolkitSource(){
var arToolkitSource = new THREEx.ArToolkitSource({
// to read from the webcam
sourceType : 'webcam',
// // to read from an image
// sourceType : 'image',
// sourceUrl : THREEx.ArToolkitContext.baseURL + 'assets/Data/img.jpg',
// to read from a video
// sourceType : 'video',
// sourceUrl : THREEx.ArToolkitContext.baseURL + 'assets/Data/videos/headtracking.mp4',
})
arToolkitSource.init(function onReady(){
arToolkitSource.onResizeElement()
arToolkitSource.copyElementSizeTo(renderer.domElement)
if( arToolkitContext.arController !== null ){
arToolkitSource.copyElementSizeTo(arToolkitContext.arController.canvas)
}
})
return arToolkitSource;
}
createToolkitContext(){
var arToolkitContext = new THREEx.ArToolkitContext({
cameraParametersUrl: 'assets/Data/camera_para.dat',
detectionMode: 'mono',
})
// initialize it
arToolkitContext.init(function onCompleted(){
// copy projection matrix to camera
camera.projectionMatrix.copy( arToolkitContext.getProjectionMatrix() );
})
// update artoolkit on every frame
onRenderFcts.push(function(){
if( arToolkitSource.ready === false ) return
arToolkitContext.update( arToolkitSource.domElement )
// update scene.visible if the marker is seen
scene.visible = camera.visible
})
return arToolkitContext;
}
initCameraControls () {
var markerControls = new THREEx.ArMarkerControls(arToolkitContext, camera, {
type : 'pattern',
// Define the pattern to recognize
patternUrl : 'assets/Data/patt.hiro',
// as we controls the camera, set changeMatrixMode: 'cameraTransformMatrix'
changeMatrixMode: 'cameraTransformMatrix'
})
// as we do changeMatrixMode: 'cameraTransformMatrix', start with invisible scene
scene.visible = false
}
createTorus () {
// add a torus knot
var geometry = new THREE.CubeGeometry(1,1,1);
var material = new THREE.MeshNormalMaterial({
transparent : true,
opacity: 0.5,
side: THREE.DoubleSide
});
var mesh = new THREE.Mesh( geometry, material );
mesh.position.y = geometry.parameters.height/2
scene.add( mesh );
var geometry = new THREE.TorusKnotGeometry(0.3,0.1,64,16);
var material = new THREE.MeshNormalMaterial();
var mesh = new THREE.Mesh( geometry, material );
mesh.position.y = 0.5
scene.add( mesh );
onRenderFcts.push(function(delta){
mesh.rotation.x += Math.PI*delta
})
}
sayHi() {
}
createModel(type) {
let model: any;
if (type === 'sphere') {
model = this.createSphere();
} else if (type === 'torus') {
model = this.createTorus();
} else {
model = this.createBox();
}
return model;
}
createSphere() {
let sphere = new THREE.Mesh(
new THREE.SphereGeometry(0.5, 8, 8),
new THREE.MeshNormalMaterial()
);
sphere.material.shading = THREE.FlatShading;
sphere.position.z = 0.5;
return sphere;
}
createBox() {
let box = new THREE.Mesh(
new THREE.BoxGeometry(1, 1, 1),
new THREE.MeshNormalMaterial()
);
box.material.shading = THREE.FlatShading;
box.position.z = 0.5;
return box;
}
}