-
Notifications
You must be signed in to change notification settings - Fork 0
/
peer.html
657 lines (614 loc) · 32.2 KB
/
peer.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<div id="container">
<p id="location"></p>
<p id="peers"></p>
</div>
<script src="/bundle.js"></script>
<script src="/matrix4.js"></script>
<script src="/build/jquery-1.4.min.js"></script>
<script src="/build/three.js"></script>
<script src="/js/loaders/ColladaLoader.js"></script>
<script src="/js/libs/stats.min.js"></script>
<script src="/js/controls/thirdPersonControls.js"></script>
<script src="/scene-scale.js"></script>
<script type="text/javascript">
/*问题:
2.异步处理不恰当
3.文件没有流处理
7.没有山地地形且没有碰撞检测
*/
var WebTorrent=require("webtorrent");
/*******************************************************/
//尺寸定义
var SCREEN_WIDTH = window.innerWidth;
var SCREEN_HEIGHT = window.innerHeight;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
//webgl全局变量
var stats;
var controls;//角色移动
var camera, scene;
var canvasRenderer, webglRenderer;//渲染器
var clock = new THREE.Clock();
var mesh, zmesh, geometry;
var avatarModel;
var theta;
var skyBox;
var peerInfo;
var mouseX = 0, mouseY = 0;
var FLOOR = 0;
var render_canvas = 0, render_gl = 1;
var maxXCell=0,maxYCell=0,minXCell=1000,minYCell=1000;
var renderOKFlag=false;//是否可以开始渲染,在所有场景模型处理过一边后置1
var workerNum=3;
var sceneScale= new SceneScale({
maxX:1.1e3,
minX:-1.1e3,
maxY:1.1e3,
minY:-1.1e3,
stepX:22,
stepY:22
});
var aoiRange=3000;
var animateFlag=false;
var container = document.getElementById( 'container' );
var originalMeshData=[]; //存储原始模型信息
var cellMeshData={}; //存储每一个网格内各模型信息
var cellLoadFlag={};//存储每一个网格内模型加载flag
var colorLib=[];//存储颜色
var calWorker=[];//存储子线程
var peerAddress=[];//存储peers
/********************************************************/
var client=new WebTorrent({
rtcConfig:{
"iceServers":[
{"url":"stun:23.21.150.121","urls":"stun:23.21.150.121"},{"url":"turn:global.turn.twilio.com:3478?transport=udp","username":"b2f027d56b89bdaffe72ea7c3c74683dd8a7ed7671e00b140112e0c627fac70f","credential":"6i8fALIIfBrds2eBM9wz/l3Q3SgpVsrJhiu+MW7NO0o=","urls":"turn:global.turn.twilio.com:3478?transport=udp"},{"url":"turn:global.turn.twilio.com:3478?transport=tcp","username":"b2f027d56b89bdaffe72ea7c3c74683dd8a7ed7671e00b140112e0c627fac70f","credential":"6i8fALIIfBrds2eBM9wz/l3Q3SgpVsrJhiu+MW7NO0o=","urls":"turn:global.turn.twilio.com:3478?transport=tcp"},{"url":"turn:global.turn.twilio.com:443?transport=tcp","username":"b2f027d56b89bdaffe72ea7c3c74683dd8a7ed7671e00b140112e0c627fac70f","credential":"6i8fALIIfBrds2eBM9wz/l3Q3SgpVsrJhiu+MW7NO0o=","urls":"turn:global.turn.twilio.com:443?transport=tcp"}
]
}
});
init();
animate();
function init() {
scene = new THREE.Scene();
//小地图和坐标
locationPoint = document.getElementById("location");
locationPoint.style.location = 'absolute';
locationPoint.style.right = 100;
//其他节点地址信息
peerInfo=document.getElementById('peers');
//peerInfo.innerHTML=peerAddressLog;
//第三人称相机
camera = new THREE.PerspectiveCamera(75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000);
camera.position.z = 0;
camera.position.x = 0;
camera.position.y = 300;
var lookPosition = new THREE.Vector3(300, 300, 0);
camera.lookAt(lookPosition);
// 地面
var mapUrl = "/images/grass.jpg";
var map = THREE.ImageUtils.loadTexture(mapUrl);
var xm = new THREE.MeshBasicMaterial({map: map});
geometry = new THREE.PlaneGeometry(1, 1, 150, 100);
mesh = new THREE.Mesh(geometry, xm);
mesh.position.set(0, FLOOR, 0);
mesh.rotation.x = -Math.PI / 2;
mesh.scale.set(1000, 1000, 100);
scene.add(mesh);
// 灯光
var ambient = new THREE.AmbientLight(0x221100);
scene.add(ambient);
var directionalLight = new THREE.DirectionalLight(0xffeedd, 1.5);
directionalLight.position.set(0, -70, 100).normalize();
scene.add(directionalLight);
// 渲染器
if (render_gl) {
try {
webglRenderer = new THREE.WebGLRenderer();
webglRenderer.setClearColor(0xffffff);
webglRenderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
webglRenderer.domElement.style.position = "relative";
container.appendChild(webglRenderer.domElement);
}
catch (e) {
}
}
if (render_canvas) {
canvasRenderer = new THREE.CanvasRenderer();
canvasRenderer.setClearColor(0xffffff);
canvasRenderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
container.appendChild(canvasRenderer.domElement);
}
// STATS
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
stats.domElement.style.zIndex = 100;
container.appendChild(stats.domElement);
//人物化身及动画
var daeLoader = new THREE.ColladaLoader();
daeLoader.load("/models/collada/avatar.dae", function (collada) {
collada.scene.traverse(function (child) {
if (child instanceof THREE.SkinnedMesh) {
var animation = new THREE.Animation(child, child.geometry.animation);
window.addEventListener('keydown', function () {
if (!animateFlag) {
animation.play();
animateFlag = true;
}
}, false);
window.addEventListener('keyup', function () {
animation.stop();
animateFlag = false;
}, false);
}
});
collada.scene.scale.set(10, 10, 10);
collada.scene.position.set(camera.position.x + 200, 0, 0);
avatarModel = collada.scene;
scene.add(avatarModel);
controls = new THREE.ThirdPersonControls(avatarModel);
controls.movementSpeed = 10;
controls.lookSpeed = 50;
controls.lookVertical = false;
controls.constrainVertical = false;
controls.verticalMin = 1.1;
controls.verticalMax = 2.2;
});
//天空盒
var imgs = [
"/skybox/px.jpg", "/skybox/nx.jpg", "/skybox/py.jpg", "/skybox/ny.jpg", "/skybox/pz.jpg", "/skybox/nz.jpg"
];
var textureCube = THREE.ImageUtils.loadTextureCube(imgs);
var shader = THREE.ShaderLib["cube"];
shader.uniforms["tCube"].value = textureCube;
var material = new THREE.ShaderMaterial({
fragmentShader: shader.fragmentShader,
vertexShader: shader.vertexShader,
uniforms: shader.uniforms,
depthWrite: false,
side: THREE.BackSide
}),
skyBox = new THREE.Mesh(new THREE.BoxGeometry(100000, 100000, 100000), material);
scene.add(skyBox);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
if (xhr.status == 200 && xhr.readyState == 4) {
sceneHandle(xhr.responseText);
}
};
xhr.open('GET', "/data/sceneData/sceneData.json");
xhr.send();
}
function sceneHandle(result) {
var sceneData = JSON.parse(result);
for (var p = 0; p < sceneData.meshes.length; p++) {
var meshBox = sceneData.meshes[p].bbox;
var meshMin = {x: meshBox[0], y: meshBox[1], z: meshBox[2]};
var meshMax = {x: meshBox[3], y: meshBox[4], z: meshBox[5]};
var position = {};
position.x = (meshMin.x + meshMax.x) / 2;
position.y = (meshMin.y + meshMax.y) / 2;
position.z = (meshMin.z + meshMax.z) / 2;
position.m = new THREE.Matrix4(position.x, position.y, position.z, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
sceneData.meshes[p].position = position;
var a=sceneData.meshes[p].meshID.toString();
var b= a.length-3;
var c= a.substr(0,b);
sceneData.meshes[p].group=sceneData.group[parseInt(c)];
originalMeshData[sceneData.meshes[p].meshID] = sceneData.meshes[p];
}
//遍历树型json**************************************/
var tempTrans = []; //存储树根到叶子的全部转换矩阵
sceneData.node = [];//存储各个模型数据
function recurseJson(node) {
if ("nodes" in node) {
tempTrans.push(node.transform);
for (var i = 0; i < node.nodes.length; i++) {
recurseJson(node.nodes[i]);
}
tempTrans.pop();
return 1;
}
else {
tempTrans.push(node.transform);
var tempMatrix = new THREE.Matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1); //存储中间计算过程中的临时矩阵
for (var i = tempTrans.length - 1; i >= 0; i--) {
var t = tempTrans[i];
var m1 = new THREE.Matrix4(t[0], t[1], t[2], t[3], t[4], t[5], t[6], t[7], t[8], t[9], t[10], t[11], t[12], t[13], t[14], t[15]);
tempMatrix.multiply(m1);
}
var tempP = originalMeshData[node.meshID].position.m;
node.finalTransform = tempMatrix;
var tempPosition = new THREE.Matrix4(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1);
tempPosition.multiplyMatrices(tempP, tempMatrix);
var a = tempPosition.elements[0];
var b = tempPosition.elements[4];
var c = tempPosition.elements[8];
var ps = {x: a, y: b, z: c};
node.position = ps;
sceneScale.locateCell(ps);
node.xCell = sceneScale.xCell;
node.yCell = sceneScale.yCell;
if ((node.xCell) > maxXCell) {
maxXCell = node.xCell;
}
if ((node.yCell) > maxYCell) {
maxYCell = node.yCell;
}
if ((node.xCell) < minXCell) {
minXCell = node.xCell;
}
if ((node.yCell) < minYCell) {
minYCell = node.yCell;
}
sceneData.node.push(node);
tempTrans.pop();
return 1;
}
}
recurseJson(sceneData.rootnode);
console.log(maxXCell, minXCell, maxYCell, minYCell);
/*********************读取meshID对应的magnet***************/
sceneData.magnet.forEach(function (obj) {
originalMeshData[obj.meshID].magnetUrl = obj.magnetUrl;
});
/*originalMeshData.forEach(function(obj){
obj.magnetUrl=sceneData.magnet[obj.meshID].magnetUrl;
});*/
/*********************************************************/
for (var xr = 0; xr < 1000; xr++) {
cellMeshData[xr] = [];
cellLoadFlag[xr] = [];
for (var zr = 0; zr < 1000; zr++) {
cellMeshData[xr][zr] = [];
cellLoadFlag[xr][zr] = false;
}
}
for (var l = 0; l < sceneData.node.length; l++) {
var x = sceneData.node[l].xCell;
var z = sceneData.node[l].yCell;
cellMeshData[x][z].push(sceneData.node[l]);
}
colorLib = [0xCD0000, 0xFFA500, 0xFFFF00, 0x90EE90, 0x7EC0EE, 0xEE30A7, 0xB23AEE, 0x919191, 0x8B4513, 0x0A0A0A];
/*模型加载子线程的相关函数*/
/*for(var i=0;i<loadWorkerNum;i++){
loadWorker[i]=new Worker("/modelLoader.js");
loadWorker[i].onmessage=function(event){
var geometryData=event.data.geometryData;
var q=event.data.node;
var content=event.data.content;
originalMeshData[q.meshID].geometry=copyModel(geometryData);
for(var i=0;i<originalMeshData[q.meshID].waitForLoad.length;i++){
var m=originalMeshData[q.meshID].waitForLoad[i].jobID%workerNum;
calWorker[m].postMessage({type:"waitForLoad",geometry:originalMeshData[q.meshID].geometry,
node:originalMeshData[q.meshID].waitForLoad[i].node});
}
originalMeshData[q.meshID].flag = "isload";
glRtc.fileAddressor.writeFile(JSON.stringify(content),"modelData/"+ q.meshID+".json");
};
loadWorker[i].onerror=function(event){
console.log(event.data);
};
}*/
/*********************************************************/
/*模型矩阵计算的子线程*/
for(var i=0;i< workerNum;i++){
calWorker[i]= new Worker("/matrixCalculating.js");
calWorker [i].onmessage=function(event){
switch(event.data.type){
case"waitForLoad" :
var fGeometry=event.data.fGeometry;
var tempGeometry =copyModel(fGeometry);
var tempModel = new THREE.Mesh(tempGeometry,
new THREE.MeshBasicMaterial({color: colorLib[Math.round(Math.random()*10)]}));
scene.add(tempModel);
console.log("model add ok!");
break;
case "done":
var tempGeometry=copyModel(event.data.qGeometry);
var tempModel=new THREE.Mesh(tempGeometry,
new THREE.MeshBasicMaterial({color: colorLib[Math.round(Math.random()*10)]}));
scene.add(tempModel);
break;
default:
break;
}
};
calWorker[i].onerror= function(event){
console.log(event.data);
};
}
/***************************************************************/
var jobID=0;
for (var xr = 0; xr < 1000; xr++) {
for (var zr = 0; zr < 1000; zr++) {
for (var l = 0; l < cellMeshData[xr][zr].length; l++) {
var q = cellMeshData[xr][zr][l];
originalMeshData[q.meshID].waitForLoad = [];//把每一个waitforload初始化
}
}
}
var currentPos={};
currentPos.x= avatarModel.position.x;
currentPos.y=avatarModel.position.z;
locationPoint.innerHTML='('+avatarModel.position.x+','+avatarModel.position.y+','+avatarModel.position.z+')';
sceneScale.locateCell(currentPos);
var xr=sceneScale.xCell;
var zr=sceneScale.yCell;
console.log(xr,zr);
cellLoadFlag[xr][zr]=true;
for (var l = 0; l < cellMeshData[xr][zr].length; l++) {
var q = cellMeshData[xr][zr][l];
console.log(xr, zr, l, "there are",cellMeshData[xr][zr].length,"trees");
jobID++;
if ("flag" in originalMeshData[q.meshID].group) {
if (originalMeshData[q.meshID].group.flag == "isload") {
console.log(q.meshID,"doneload");
var n = jobID % workerNum;
calWorker[n].postMessage({
type: "done",
geometry: originalMeshData[q.meshID].geometry,
node: q
});
}
else if (originalMeshData[q.meshID].group.flag == "isloading") {
// originalMeshData[q.meshID].group.waitForLoad.push({node: q, jobID: jobID});
originalMeshData[q.meshID].waitForLoad.push({node: q, jobID: jobID});
console.log(q.meshID, "push", originalMeshData[q.meshID].waitForLoad.length);
}
}
else {
//1.去掉下载模型时的webworker,保留处理模型数据的webworker,并且全写入webtorrent里面 //2.webtorrent里面都是二进制数据,怎么改成json
//3.webworker的消息循环队列
//4。需要开多少webworker
//5.一格稍微画大一点
//6.下载需要magnet uri //7.方案一:由server来发种子,server本身也作为node参与下载 //方案二:初始文件全部保存在一个peer上,peer上传文件seed。中心服务器只做tracker,不提供文件下载 //8.由seed peer发布种子,正常情况下,应该是节点询问seed peer,seed peer告知。之后该信息缓存在每一个节点上 //但是为了方便,我们直接把magnet url 写进sceneData里,免去通信的麻烦了
originalMeshData[q.meshID].group.flag = "isloading";
console.log(q.meshID, "unload");
//loadJobID++; //var k = loadJobID % loadWorkerNum;
//originalMeshData[q.meshID].group.waitForLoad.push({node: q, jobID: jobID});
originalMeshData[q.meshID].waitForLoad.push({node: q, jobID: jobID});
//loadWorker[k].postMessage({node: q, type: "superNode"});
var magnetUrl= originalMeshData[q.meshID].group.infoHash;
var cDate=new Date();
originalMeshData[q.meshID].group.timeStart=cDate.getTime();
client.add(magnetUrl,{
announce:['ws://cg.tongji.edu.cn:80']
},function(torrent){
console.log('Client is downloading:', torrent.infoHash);
torrent.on('wire',function(wire){
console.log("new node"+wire.peerId.toString()+''+wire.remoteAddress);
});
torrent.on('peer',function(peer){
if(peerAddress.every(function(element,index,array){
return element.id!==peer.id;
})){
peerAddress.push(peer);
peerInfo.innerHTML=peerInfo.innerHTML+" "+peer.id;
console.log("new peerAddress",peerAddress);
}else{
console.log("old peer");
}
});
torrent.on('done',function(){
var cDate=new Date();
originalMeshData[q.meshID].group.timeEnd=cDate.getTime();
originalMeshData[q.meshID].group.loadTime=originalMeshData[q.meshID].group.timeEnd
-originalMeshData[q.meshID].group.timeStart;
console.log("group"+originalMeshData[q.meshID].group.groupID+"time:"+
originalMeshData[q.meshID].group.loadTime);
});
torrent.on('download', function (bytes) {
console.log('just downloaded: ' + bytes);
console.log('total downloaded: ' + torrent.downloaded);
console.log('download speed: ' + torrent.downloadSpeed);
console.log('progress: ' + torrent.progress);
});
torrent.files.forEach(function(file){
file.getBuffer(function (err,buf){
if(err){
console.log("get file buffer error");
} else{
var meshID=file.name.split('.')[0];
var fileString= buf.toString();
console.log("file doneload");
handleResult(JSON.parse(fileString),meshID);
}
});
});
}
);
}
}
renderOKFlag=true;
}
/* result {object}
* q {object}
* */
function handleResult(content,meshID){
var geometry={faceVertexUvs:[],vertices:[],faces:[]};
parseModel(content);
originalMeshData[meshID].geometry=copyModel(geometry);
originalMeshData[meshID].group.flag = "isload";
for(var i=0;i<originalMeshData[meshID].waitForLoad.length;i++){
var m=originalMeshData[meshID].waitForLoad[i].jobID%workerNum;
calWorker[m].postMessage({type:"waitForLoad", geometry:originalMeshData[meshID].geometry,
node:originalMeshData[meshID].waitForLoad[i].node});
}
function parseModel( json ) {
var i, offset, zLength,
vertex, face,
faces =json.faces,
vertices = json.verts,nUvLayers = 0;
if (json.uvs !== undefined ) {for ( i = 0; i < json.uvs.length; i ++ ) {
if (json.uvs[ i ].length ) nUvLayers ++;
}
for ( i = 0; i < nUvLayers; i ++ ) {
geometry.faceVertexUvs[ i ] = [];
}
}
offset = 0;
zLength = vertices.length;
while ( offset < zLength ) {
vertex ={};
vertex.x = vertices[ offset++ ];
vertex.y = vertices[offset++ ] ;
vertex.z = vertices[offset ++ ] ;
geometry.vertices.push( vertex );
}
offset = 0;
zLength = faces.length;
while ( offset < zLength ) {
face = {};
face.a = faces[ offset ++ ];
face.b = faces[offset ++ ];
face.c = faces[offset ++ ];
geometry.faces.push( face );
}
}
}
function renderingLoad(){
if(renderOKFlag==true) {
var jobID = 0;
var currentPos = {};
currentPos.x = avatarModel.position.x;
currentPos.y = avatarModel.position.z;
locationPoint.innerHTML='('+avatarModel.position.x+','+avatarModel.position.y+','+avatarModel.position.z+')';
sceneScale.locateCell(currentPos);
var xr = sceneScale.xCell;
var zr = sceneScale.yCell;
if (cellLoadFlag[xr][zr] == false) {
cellLoadFlag[xr][zr] = true;
console.log("in this rol",xr,zr,"there are",cellMeshData[xr][zr].length,"trees");
for (var l = 0; l < cellMeshData[xr][zr].length; l++) {
var q = cellMeshData[xr][zr][l];
jobID++;
if ("flag" in originalMeshData[q.meshID].group) {
if (originalMeshData[q.meshID].group.flag == "isload") {
console.log(q.meshID,"doneload");
var n = jobID % workerNum;
calWorker[n].postMessage({
type: "done",
geometry: originalMeshData[q.meshID].geometry,
node: q
});
}
else if (originalMeshData[q.meshID].group.flag == "isloading") {
//originalMeshData[q.meshID].group.waitForLoad.push({node: q, jobID: jobID});
originalMeshData[q.meshID].waitForLoad.push({node: q, jobID: jobID});
console.log(q.meshID, "push", originalMeshData[q.meshID].waitForLoad.length);
}
}
else {
originalMeshData[q.meshID].group.flag = "isloading";
console.log(q.meshID,"unload");
//loadJobID++;
//var k = loadJobID % loadWorkerNum;
//originalMeshData[q.meshID].group.waitForLoad.push({node: q, jobID: jobID});
originalMeshData[q.meshID].waitForLoad.push({node: q, jobID: jobID});
//loadWorker[k].postMessage({node: q, type: "superNode"});
var magnetUrl= originalMeshData[q.meshID].group.infoHash;
var result='';
var cDate=new Date();
originalMeshData[q.meshID].group.timeStart=cDate.getTime();
client.add(magnetUrl,{
announce:['ws://cg.tongji.edu.cn:80']
}, function(torrent){
console.log('Client is downloading:', torrent.infoHash);
torrent.on('wire',function(wire){
console.log("new node"+wire.peerId.toString()+''+wire.remoteAddress);
});
torrent.on('peer',function(peer){
if(peerAddress.every(function(element){
return element.id!==peer.id;
})){
peerAddress.push(peer);
peerInfo.innerHTML=peerInfo.innerHTML+" "+peer.id;
console.log("new peerAddress",peerAddress);
}else{
console.log("old peer");
}
});
torrent.on('done',function(){
var cDate=new Date();
originalMeshData[q.meshID].group.timeEnd=cDate.getTime();
originalMeshData[q.meshID].group.loadTime=originalMeshData[q.meshID].group.timeEnd
-originalMeshData[q.meshID].group.timeStart;
console.log("group"+originalMeshData[q.meshID].group.groupID+"time:"+
originalMeshData[q.meshID].group.loadTime);
});
torrent.on('download', function (bytes) {
console.log('just downloaded: ' + bytes);
console.log('total downloaded: ' + torrent.downloaded);
console.log('download speed: ' + torrent.downloadSpeed);
console.log('progress: ' + torrent.progress);
});
torrent.files.forEach(function(file){
file.getBuffer(function(err,buf){
if(err){
console.log("get file buffer error");
}else{
var meshID=file.name.split('.')[0];
var fileString= buf.toString();
console.log("file doneload");
handleResult(JSON.parse(fileString),meshID);
}
});
});
}
);
}
}
}
}
}
function copyModel(geometryData){
var geometry=new THREE.Geometry();
for(var i=0;i<geometryData.vertices.length;i++){
var vertex=new THREE.Vector3();
vertex.x=geometryData.vertices[i].x;
vertex.y=geometryData.vertices[i].y;
vertex.z=geometryData.vertices[i].z;
geometry.vertices[i] =vertex;
}
for(var i=0;i< geometryData.faces.length;i++){
var face=new THREE.Face3();
face.a=geometryData.faces[i].a;
face.b= geometryData.faces[i].b;
face.c=geometryData.faces[i].c;
geometry.faces[i]=face;
}
geometry.computeFaceNormals();
geometry.computeBoundingSphere();
return geometry;
}
function animate() {
var delta = clock.getDelta();
render();
if(controls) {
theta=controls.update(delta);
}
if(avatarModel){
camera.position.x=avatarModel.position.x-10*Math.cos(theta);
camera.position.y=avatarModel.position.y+15;
camera.position.z=avatarModel.position.z-10*Math.sin(theta);
var lookPosition = new THREE.Vector3(avatarModel.position.x, camera.position.y, avatarModel.position.z );
camera.lookAt(lookPosition);
}
renderingLoad();
requestAnimationFrame( animate );
THREE.AnimationHandler.update( delta );
stats.update();
}
function render() {
if (render_gl ) webglRenderer.render(scene, camera);
}
</script>
</body>
</html>