-
Notifications
You must be signed in to change notification settings - Fork 7
/
commonar.html
executable file
·313 lines (206 loc) · 10.2 KB
/
commonar.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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
<!DOCTYPE html>
<meta name="viewport" content="width=device-width,minimum-scale=1.0,maximum-scale=1.0,user-scalable=no"/>
<meta charset="UTF-8">
<!-- include three.js -->
<script src='webarjs/three.js'></script>
<!-- <script src='webarjs/stats.min.js'></script>FPS的js-->
<!-- include js-aruco -->
<script src='webarjs/svd.js'></script><!--SVD PCA的实现一般有两种,一种是用特征值分解去实现的,一种是用奇异值分解去实现-->
<script src='webarjs/posit1-patched.js'></script><!--基于共面特征点的姿态估计-->
<script src='webarjs/cv.js'></script><!--OPENcv -->
<script src='webarjs/aruco.js'></script><!-- 一个微型的基于OPENCV的为了增强现实的库-->
<!-- include some extensions -->
<script src='webarjs/threex.webcamgrabbing.js'></script> <!--摄像头这个界面的JS-->
<script src="js/jquery-3.1.1.min.js"></script>
<script src='webarjs/threex.jsarucomarker.js'></script><!--跟踪识别JS-->
<script src='vendor/three.js/examples/js/loaders/OBJLoader.js'></script>
<script src='vendor/three.js/examples/js/loaders/MTLLoader.js'></script>
<script src='vendor/three.js/examples/js/loaders/OBJMTLLoader.js'></script>
<script src='webarjs/threex.universalloader.js'></script>
<style>
#elemdiv div{
height: 35px;width: 200px;background-color: red;color: white;text-align: center;
}
</style>
<body style='margin: 0px; overflow: hidden;'>
<div id="elemdiv">
</div>
<script>
</script>
<script>
//////////////////////////////////////////////////////////////////////////////////
// Test if the browser support WebGL and getUserMedia
//////////////////////////////////////////////////////////////////////////////////
;(function(){
// TODO backport those 2 in Detector.js
var hasGetUserMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia) ? true : false
var hasMediaStreamTrackSources = MediaStreamTrack.getSources ? true : false
var hasWebGL = ( function () { try { var canvas = document.createElement( 'canvas' ); return !! ( window.WebGLRenderingContext && ( canvas.getContext( 'webgl' ) || canvas.getContext( 'experimental-webgl' ) ) ); } catch ( e ) { return false; } } )()
if( hasWebGL === false ){
alert('your browser doesn\'t support navigator.getUserMedia()')
}
if( hasMediaStreamTrackSources === false ){
alert('your browser doesn\'t support MediaStreamTrack.getSources()')
}
if( hasGetUserMedia === false ){
alert('your browser doesn\'t support navigator.getUserMedia()')
}
})()
//////////////////////////////////////////////////////////////////////////////////
// Init
//////////////////////////////////////////////////////////////////////////////////
// init renderer
var renderer = new THREE.WebGLRenderer({
antialias : true,
alpha : true
});
renderer.setSize( window.innerWidth, window.innerHeight );
document.body.appendChild( renderer.domElement );
// array of functions for the rendering loop 渲染数组 每一次渲染时执行里面的函数
var onRenderFcts = [];
// init scene and camera
var scene = new THREE.Scene()
var camera = new THREE.PerspectiveCamera(30, window.innerWidth / window.innerHeight, 0.01, 1000);
camera.position.z = 2;
// camera.position.set(4, -3, 5);
//////////////////////////////////////////////////////////////////////////////////
// create a markerObject3D
//////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////
// 创建路标
//////////////////////////////////////////////////////////////////////////////////
/// camera.lookAt(new THREE.Vector3(0, 0, 0));
var light = new THREE.DirectionalLight(0xffffff);
light.position.set(4,0, 2);
scene.add(light);
var objarray=[];
var idarray=[];
var datalist;
$.ajax(
{ url: "http://www.herozhou.com:8080/ar/getmarker",
type:'GET',
dataType:'json',
success: function(data){
datalist=data.markerlist;
for(var i=0;i<datalist.length;i++)
{
var filename='upload/'+datalist[i].modelpath;
var markerObject3D = new THREE.Object3D()
scene.add(markerObject3D)
var material = new THREE.SpriteMaterial({
map: THREE.ImageUtils.loadTexture( filename )
});
var geometry = new THREE.BoxGeometry(0.1,0.1,0.1)
var object3d = new THREE.Sprite(material );
object3d.scale.set( 1, 1, 1 );
markerObject3D.add(object3d)
objarray[i]=markerObject3D;
idarray[i]=datalist[i].markerid;
}
}
});
//////////////////////////////////////////////////////////////////////////////////
// render the whole thing on the page
//////////////////////////////////////////////////////////////////////////////////
// handle window resize
window.addEventListener('resize', function(){
renderer.setSize( window.innerWidth, window.innerHeight )
camera.aspect = window.innerWidth / window.innerHeight
camera.updateProjectionMatrix()
}, false)
// render the scene
onRenderFcts.push(function(){
renderer.render( scene, camera );
})
// run the rendering loop 定时显示画面看来像动画
var previousTime = performance.now()
requestAnimationFrame(function animate(now){
requestAnimationFrame( animate );
// clickobject1();
//每次渲染执行数组里面的数
onRenderFcts.forEach(function(onRenderFct){
onRenderFct(now, now - previousTime)
})
previousTime = now
})
//////////////////////////////////////////////////////////////////////////////////
// Do the Augmented Reality part
//////////////////////////////////////////////////////////////////////////////////
// init the marker recognition 跟踪识别标记
var jsArucoMarker = new THREEx.JsArucoMarker()
// if no specific image source is specified, take the webcam by default
if( location.hash === '' ) location.hash = '#webcam'
// init the image source grabbing
if( location.hash === '#video' ){
var videoGrabbing = new THREEx.VideoGrabbing()
jsArucoMarker.videoScaleDown = 2
}else if( location.hash === '#webcam' ){
var videoGrabbing = new THREEx.WebcamGrabbing() //调用js里的函数
jsArucoMarker.videoScaleDown = 2
}else if( location.hash === '#image' ){
var videoGrabbing = new THREEx.ImageGrabbing()
jsArucoMarker.videoScaleDown = 10
}else console.assert(false)
// attach the videoGrabbing.domElement to the body
document.body.appendChild(videoGrabbing.domElement)
//////////////////////////////////////////////////////////////////////////////////
// Process video source to find markers
//////////////////////////////////////////////////////////////////////////////////
// set the markerObject3D as visible
// markerObject3D.visible = false //先让物体不显示 识别到之后让物体显示出来
// process the image source with the marker recognition
onRenderFcts.push(function(){
var domElement = videoGrabbing.domElement //返回摄像机
var markers = jsArucoMarker.detectMarkers(domElement) //如果把参数去掉 就不会卡 如果不去掉 就会很卡
for(var i=0;i<objarray.length;i++)
{
objarray[i].visible = false
}
// see if this.markerId has been found
markers.forEach(function(marker){
console.log(marker.id)
// if( marker.id !== 265 ) return
for(var i=0;i<objarray.length;i++)
{
if( marker.id === parseInt(idarray[i]) )
{
if(window.innerWidth>1000)
jsArucoMarker.markerToObject3D(marker, objarray[i])
else{
jsArucoMarker.markerToObject3D(marker, objarray[i])
objarray[i].position.z-=75;
// alert(objarray[i].position.z)
}
//alert("123")
/*
}
if(window.innerWidth>1000)
jsArucoMarker.markerToObject3D(marker, objarray[i]) //去掉的话就会整个东西显示在屏幕上 而不是显示在坐标上
else
{
jsArucoMarker.markerToObject3D(marker, objarray[i])
objarray[i].position.x+=150;
if( objarray[i].position.z>-450)
objarray[i].position.x-=20;
else if( objarray[i].position.z>-370)
objarray[i].position.x-=50;
else if( objarray[i].position.z>-300)
{
objarray[i].position.x-=50;
}
console.log( objarray[i].position.x+","+ objarray[i].position.z )
alert(objarray[i].position.x+","+ objarray[i].position.z)
}
*/
//alert(window.innerWidth+","+ window.innerHeight)
objarray[i].visible = true
// alert("123");
}
}
})
})
</script>
<script src="js/jquery-3.1.1.min.js">
</script>
</body>
</html>