forked from younyzhu/younyzhu.github.com
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckboard.html
437 lines (366 loc) · 15.8 KB
/
checkboard.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js DMRI brain tractography</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<style>
body {
font-family: Monospace;
background-color: #000;
color: #fff;
margin: 0px;
overflow: hidden;
}
#info {
color: #fff;
position: absolute;
top: 15px;
width: 100%;
text-align: center;
z-index: 100;
display:block;
}
#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
</style>
</head>
<body>
<script src="./js/three.js"></script>
<script src="./js/SameBundleTubePointGeometry.js"></script>
<script src="./js/TrainingGeo.js"></script>
<script src="./js/TrackballControls.js"></script>
<script src="./js/Detector.js"></script>
<script src="./js/stats.min.js"></script>
<script>
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
var container, stats;
var camera, controls, scene, renderer;
var group;
var cross;
// custom global variables
var projector, mouse = { x: 0, y: 0 }, INTERSECTED;
var sprite1;
var canvas1, context1, texture1;
init();
animate();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
var info = document.createElement( 'div' );
info.style.position = 'absolute';
info.style.top = '15px';
info.style.width = '100%';
info.style.textAlign = 'center';
info.innerHTML = 'DMRI brain tractography-----Fiber bundle information Structure(Training)';
container.appendChild( info );
camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.z = 220;
controls = new THREE.TrackballControls( camera );
controls.rotateSpeed = 1.0;
controls.zoomSpeed = 1.2;
controls.panSpeed = 0.8;
controls.noZoom = false;
controls.noPan = false;
controls.staticMoving = true;
controls.dynamicDampingFactor = 0.3;
controls.keys = [ 65, 83, 68 ];
controls.addEventListener( 'change', render );
// scene
scene = new THREE.Scene();
var light;
light = new THREE.PointLight();
light.position.set( 0, 0, 0 );
scene.add( light );
light = new THREE.DirectionalLight( 0xffeedd );
light.position.set( 0, 0, 120 );
scene.add( light );
light = new THREE.DirectionalLight( 0xffeedd );
light.position.set( 0, 0, -120 );
scene.add( light );
light = new THREE.DirectionalLight( 0xffeedd );
light.position.set( 120, 180, 0 );
scene.add( light );
/*light = new THREE.DirectionalLight( 0xffeedd );
light.position.set( -120, 0, 0 );
scene.add( light );*/
//var helper = new THREE.GridHelper( 200, 10 );
//helper.setColors( 0x0000ff, 0x808080 );
//helper.position.y = -100;
//scene.add( helper );
// FLOOR
var floorTexture = new THREE.ImageUtils.loadTexture( './images/checkerboard.jpg' );
floorTexture.wrapS = floorTexture.wrapT = THREE.RepeatWrapping;
floorTexture.repeat.set( 10, 10 );
var floorMaterial = new THREE.MeshBasicMaterial( { map: floorTexture, side: THREE.DoubleSide } );
var floorGeometry = new THREE.PlaneGeometry(1000, 1000, 10, 10);
var floor = new THREE.Mesh(floorGeometry, floorMaterial);
floor.position.y = -100;
floor.rotation.x = Math.PI / 2;
scene.add(floor);
group = new THREE.Object3D();
//objectvar
var manager = new THREE.LoadingManager();
manager.onProgress = function ( item, loaded, total ) {
console.log( item, loaded, total );
};
var cc_loader = new GeometryLoader(manager,'red' );
cc_loader.load( './data/single/s3_cc.data', function ( object ) {
if(cc_loader.center!==null&&cc_loader.bundleposition !== null)
{
object.position.x = -cc_loader.center.x;
object.position.y = -cc_loader.center.y;
object.position.z = -cc_loader.center.z;
object.name = "CC";
group.add( object );
var spritey = makeTextSprite( " CC ",
{ fontsize:12, borderColor: {r:255, g:255, b:255, a:1.0}, backgroundColor: {r:255, g:0, b:0, a:0.8} } );
spritey.position.set(cc_loader.bundleposition.x-cc_loader.center.x,cc_loader.bundleposition.y-cc_loader.center.y,
cc_loader.bundleposition.z-cc_loader.center.z);
scene.add( spritey );
}
} );
var cg_loader = new GeometryLoader(manager,'green' );
cg_loader.load( './data/single/s3_cg.data', function ( object ) {
if(cg_loader.center!==null&&cg_loader.bundleposition !== null)
{
object.position.x = -cg_loader.center.x;
object.position.y = -cg_loader.center.y;
object.position.z = -cg_loader.center.z;
object.name = "CG";
group.add(object);
var spritey = makeTextSprite(" CG ",
{ fontsize: 12, borderColor: {r: 255, g: 255, b: 255, a: 1.0}, backgroundColor: {r:0 , g: 255, b: 0, a: 0.8} });
//spritey.position.set(cg_loader.bundleposition.x-cg_loader.center.x, cg_loader.bundleposition.y-cg_loader.center.y,
// cg_loader.bundleposition.z-cg_loader.center.z);
spritey.position.set((cg_loader.bundleposition.x-cg_loader.center.x)/4, (cg_loader.bundleposition.y-cg_loader.center.y)/2,
cg_loader.bundleposition.z-cg_loader.center.z);
scene.add(spritey);
}
} );
var cst_loader = new GeometryLoader(manager,'blue' );
cst_loader.load( './data/single/s3_cst.data', function ( object ) {
if(cst_loader.center!==null&&cst_loader.bundleposition !== null) {
object.position.x = -cst_loader.center.x;
object.position.y = -cst_loader.center.y;
object.position.z = -cst_loader.center.z;
object.name = "CST";
group.add(object);
var spritey = makeTextSprite(" CST ",
{ fontsize: 12, borderColor: {r: 255, g: 255, b: 255, a: 1.0}, backgroundColor: {r: 0, g: 0, b: 255, a: 0.8} });
spritey.position.set(cst_loader.bundleposition.x-cst_loader.center.x, cst_loader.bundleposition.y-cst_loader.bundleposition.y,
cst_loader.bundleposition.z-cst_loader.center.z);
scene.add(spritey);
}
} );
var ifo_loader = new GeometryLoader(manager,'yellow' );
ifo_loader.load( './data/single/s3_ifo.data', function ( object ) {
if(ifo_loader.center!==null&&ifo_loader.bundleposition !== null) {
object.position.x = -ifo_loader.center.x;
object.position.y = -ifo_loader.center.y;
object.position.z = -ifo_loader.center.z;
object.name = "IFO";
group.add(object);
var spritey = makeTextSprite(" IFO ",
{ fontsize: 12, borderColor: {r: 255, g: 255, b: 255, a: 1.0}, backgroundColor: {r: 255, g: 255, b: 0, a: 0.8} });
spritey.position.set(ifo_loader.bundleposition.x-ifo_loader.center.x, ifo_loader.bundleposition.y-ifo_loader.center.y,
ifo_loader.bundleposition.z-ifo_loader.center.z);
scene.add(spritey);
}
} );
var ilf_loader = new GeometryLoader(manager,'Cyan' );
ilf_loader.load( './data/single/s3_ilf.data', function ( object ) {
if(ilf_loader.center!==null&&ilf_loader.bundleposition !== null) {
object.position.x = -ilf_loader.center.x;
object.position.y = -ilf_loader.center.y;
object.position.z = -ilf_loader.center.z;
object.name = "ILF";
group.add(object);
var spritey = makeTextSprite(" ILF ",
{ fontsize: 12, borderColor: {r: 255, g: 255, b:255, a: 1.0}, backgroundColor: {r: 0, g:255, b: 255, a: 0.8} });
spritey.position.set(ilf_loader.center.x-ilf_loader.center.x, ilf_loader.center.y-ilf_loader.center.y,
ilf_loader.center.z-ilf_loader.center.z);
scene.add(spritey);
}
} );
scene.add(group);
//render
renderer = new THREE.WebGLRenderer({antialias:true});
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
stats = new Stats();
stats.domElement.style.position = 'absolute';
stats.domElement.style.top = '0px';
stats.domElement.style.zIndex = 100;
container.appendChild( stats.domElement );
// initialize object to perform world/screen calculations
projector = new THREE.Projector();
// when the mouse moves, call the given function
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
/////// draw text on canvas /////////
// create a canvas element
canvas1 = document.createElement('canvas');
context1 = canvas1.getContext('2d');
context1.font = "Bold 20px Arial";
context1.fillStyle = "rgba(0,0,0,0.95)";
context1.fillText('Hello, world!', 0, 20);
// canvas contents will be used for a texture
texture1 = new THREE.Texture(canvas1);
texture1.needsUpdate = true;
////////////////////////////////////////
var spriteMaterial = new THREE.SpriteMaterial( { map: texture1, useScreenCoordinates: true, alignment: THREE.Vector2(-1,1) } );
sprite1 = new THREE.Sprite( spriteMaterial );
sprite1.scale.set(200,100,1.0);
sprite1.position.set( 50, 50, 0 );
scene.add( sprite1 );
//////////////////////////////////////////
window.addEventListener( 'resize', onWindowResize, false );
}
function onDocumentMouseMove( event )
{
// the following line would stop any other event handler from firing
// (such as the mouse's TrackballControls)
// event.preventDefault();
// update sprite position
sprite1.position.set( event.clientX, event.clientY - 20, 0 );
// update the mouse variable
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
controls.handleResize();
render();
}
function makeTextSprite( message, parameters )
{
if ( parameters === undefined ) parameters = {};
var fontface = parameters.hasOwnProperty("fontface") ?
parameters["fontface"] : "Arial";
var fontsize = parameters.hasOwnProperty("fontsize") ?
parameters["fontsize"] : 12;
var borderThickness = parameters.hasOwnProperty("borderThickness") ?
parameters["borderThickness"] : 1;
var borderColor = parameters.hasOwnProperty("borderColor") ?
parameters["borderColor"] : { r:0, g:0, b:0, a:1.0 };
var backgroundColor = parameters.hasOwnProperty("backgroundColor") ?
parameters["backgroundColor"] : { r:255, g:255, b:255, a:1.0 };
var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
context.font = "Bold " + fontsize + "px " + fontface;
// get size data (height depends only on font size)
var metrics = context.measureText( message );
var textWidth = metrics.width;
// background color
context.fillStyle = "rgba(" + backgroundColor.r + "," + backgroundColor.g + ","
+ backgroundColor.b + "," + backgroundColor.a + ")";
// border color
context.strokeStyle = "rgba(" + borderColor.r + "," + borderColor.g + ","
+ borderColor.b + "," + borderColor.a + ")";
context.lineWidth = borderThickness;
roundRect(context, borderThickness/2, borderThickness/2, textWidth + borderThickness, fontsize * 1.2 + borderThickness, 2);
// 1.4 is extra height factor for text below baseline: g,j,p,q.
// text color
context.fillStyle = "rgba(255, 255, 255, 1.0)";
context.fillText( message, borderThickness, fontsize + borderThickness);
// canvas contents will be used for a texture
var texture = new THREE.Texture(canvas);
texture.needsUpdate = true;
var spriteMaterial = new THREE.SpriteMaterial(
{ map: texture, useScreenCoordinates: false} );
var sprite = new THREE.Sprite( spriteMaterial );
sprite.scale.set(60,60,60);
return sprite;
}
// function for drawing rounded rectangles
function roundRect(ctx, x, y, w, h, r)
{
ctx.beginPath();
ctx.moveTo(x+r, y);
ctx.lineTo(x+w-r, y);
ctx.quadraticCurveTo(x+w, y, x+w, y+r);
ctx.lineTo(x+w, y+h-r);
ctx.quadraticCurveTo(x+w, y+h, x+w-r, y+h);
ctx.lineTo(x+r, y+h);
ctx.quadraticCurveTo(x, y+h, x, y+h-r);
ctx.lineTo(x, y+r);
ctx.quadraticCurveTo(x, y, x+r, y);
ctx.closePath();
ctx.fill();
ctx.stroke();
}
//
function animate() {
requestAnimationFrame( animate );
render();
update();
}
function update()
{
// create a Ray with origin at the mouse position
// and direction into the scene (camera direction)
var vector = new THREE.Vector3( mouse.x, mouse.y, 1 );
projector.unprojectVector( vector, camera );
var ray = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );
// create an array containing all objects in the scene with which the ray intersects
var intersects = ray.intersectObjects( scene.children );
// INTERSECTED = the object in the scene currently closest to the camera
// and intersected by the Ray projected from the mouse position
// if there is one (or more) intersections
if ( intersects.length > 0 )
{
// if the closest object intersected is not the currently stored intersection object
if ( intersects[ 0 ].object != INTERSECTED )
{
// restore previous intersection object (if it exists) to its original color
if ( INTERSECTED )
INTERSECTED.material.color.setHex( INTERSECTED.currentHex );
// store reference to closest object as current intersection object
INTERSECTED = intersects[ 0 ].object;
// store color of closest object (for later restoration)
INTERSECTED.currentHex = INTERSECTED.material.color.getHex();
// set a new color for closest object
INTERSECTED.material.color.setHex( 0xffff00 );
// update text, if it has a "name" field.
if ( intersects[ 0 ].object.name )
{
context1.clearRect(0,0,640,480);
var message = intersects[ 0 ].object.name;
var metrics = context1.measureText(message);
var width = metrics.width;
context1.fillStyle = "rgba(0,0,0,0.95)"; // black border
context1.fillRect( 0,0, width+8,20+8);
context1.fillStyle = "rgba(255,255,255,0.95)"; // white filler
context1.fillRect( 2,2, width+4,20+4 );
context1.fillStyle = "rgba(0,0,0,1)"; // text color
context1.fillText( message, 4,20 );
texture1.needsUpdate = true;
}
else
{
context1.clearRect(0,0,300,300);
texture1.needsUpdate = true;
}
}
}
else // there are no intersections
{
// restore previous intersection object (if it exists) to its original color
if ( INTERSECTED )
INTERSECTED.material.color.setHex( INTERSECTED.currentHex );
// remove previous intersection object reference
// by setting current intersection object to "nothing"
INTERSECTED = null;
context1.clearRect(0,0,300,300);
texture1.needsUpdate = true;
}
controls.update();
stats.update();
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>