Skip to content

Commit e8a96bb

Browse files
author
Alexander Huth
committed
working clipping
1 parent e00e879 commit e8a96bb

File tree

4 files changed

+145
-1
lines changed

4 files changed

+145
-1
lines changed

cortex/webgl/resources/js/dataset.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ var dataset = (function(module) {
8383
this.uniforms = {
8484
framemix: { type:'f', value:0},
8585
dataAlpha: { type:'f', value:1.0},
86+
87+
slicexn: { type:'v3', value:new THREE.Vector3( 0,0,0 )},
88+
sliceyn: { type:'v3', value:new THREE.Vector3( 0,0,0 )},
89+
slicezn: { type:'v3', value:new THREE.Vector3( 0,0,0 )},
90+
91+
slicexc: { type:'v3', value:new THREE.Vector3( 0,0,0 )},
92+
sliceyc: { type:'v3', value:new THREE.Vector3( 0,0,0 )},
93+
slicezc: { type:'v3', value:new THREE.Vector3( 0,0,0 )},
94+
95+
doslicex: { type:'i', value:false},
96+
doslicey: { type:'i', value:false},
97+
doslicez: { type:'i', value:false},
8698
}
8799

88100
if (!this.vertex) {

cortex/webgl/resources/js/mriview.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,9 @@ var mriview = (function(module) {
4949
y: new sliceplane.Plane(this, 1),
5050
z: new sliceplane.Plane(this, 2),
5151
};
52+
this._clipx = false;
53+
this._clipy = false;
54+
this._clipz = false;
5255

5356
this.ui = new jsplot.Menu();
5457
this.ui.addEventListener("update", this.schedule.bind(this));
@@ -1164,6 +1167,18 @@ var mriview = (function(module) {
11641167
rotate_z: {action:[this.sliceplanes.z, 'setAngle', -89, 89]}
11651168
});
11661169

1170+
var sliceplane_clip = sliceplane_ui.addFolder("clip", true);
1171+
sliceplane_clip.add({
1172+
clip_x: {action:[this, "setClippingX"]},
1173+
flip_x: {action:[this.sliceplanes.x, "setFlip"]},
1174+
clip_y: {action:[this, "setClippingY"]},
1175+
flip_y: {action:[this.sliceplanes.y, "setFlip"]},
1176+
clip_z: {action:[this, "setClippingZ"]},
1177+
flip_z: {action:[this.sliceplanes.z, "setFlip"]},
1178+
})
1179+
1180+
//
1181+
11671182
if ($(this.object).find("#colormap_category").length > 0) {
11681183
$(this.object).find("#colormap").ddslick({ width:296, height:350,
11691184
onSelected: function() {
@@ -1294,6 +1309,39 @@ var mriview = (function(module) {
12941309
this.sliceplanes.z.setVisible(!this.sliceplanes.z._visible);
12951310
viewer.schedule();
12961311
};
1312+
module.Viewer.prototype.setClippingX = function(val) {
1313+
if (val === undefined)
1314+
return this._clipx;
1315+
1316+
this._clipx = val;
1317+
1318+
if (this.active !== undefined) {
1319+
this.active.uniforms.doslicex.value = this._clipx;
1320+
}
1321+
this.schedule();
1322+
}
1323+
module.Viewer.prototype.setClippingY = function(val) {
1324+
if (val === undefined)
1325+
return this._clipy;
1326+
1327+
this._clipy = val;
1328+
1329+
if (this.active !== undefined) {
1330+
this.active.uniforms.doslicey.value = this._clipy;
1331+
}
1332+
this.schedule();
1333+
}
1334+
module.Viewer.prototype.setClippingZ = function(val) {
1335+
if (val === undefined)
1336+
return this._clipz;
1337+
1338+
this._clipz = val;
1339+
1340+
if (this.active !== undefined) {
1341+
this.active.uniforms.doslicez.value = this._clipz;
1342+
}
1343+
this.schedule();
1344+
}
12971345

12981346
return module;
12991347
}(mriview || {}));

cortex/webgl/resources/js/shaderlib.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,18 @@ var Shaderlib = (function() {
363363
"uniform int bumpyflat;",
364364
"float f_bumpyflat = float(bumpyflat);",
365365

366+
"uniform vec3 slicexn;",
367+
"uniform vec3 sliceyn;",
368+
"uniform vec3 slicezn;",
369+
370+
"uniform vec3 slicexc;",
371+
"uniform vec3 sliceyc;",
372+
"uniform vec3 slicezc;",
373+
374+
"uniform bool doslicex;",
375+
"uniform bool doslicey;",
376+
"uniform bool doslicez;",
377+
366378
"attribute vec4 wm;",
367379
"attribute vec3 wmnorm;",
368380
"attribute vec4 auxdat;",
@@ -379,6 +391,8 @@ var Shaderlib = (function() {
379391
"varying float vCurv;",
380392
"varying float vMedial;",
381393
"varying float vThickmix;",
394+
"varying float vSliced;",
395+
"varying vec3 vWorldPosition;",
382396
// "varying float vDrop;",
383397

384398
"varying vec3 vPos_x[2];",
@@ -421,6 +435,8 @@ var Shaderlib = (function() {
421435
"vMedial = auxdat.x;",
422436
"vCurv = auxdat.y;",
423437

438+
//"vSliced = float(mpos.y > slicey) * float(doslicey);",
439+
424440
"vec3 pos, norm;",
425441
"mixfunc(mpos, mnorm, pos, norm);",
426442

@@ -444,6 +460,10 @@ var Shaderlib = (function() {
444460

445461
"gl_Position = projectionMatrix * modelViewMatrix * vec4( pos, 1.0 );",
446462

463+
"vWorldPosition = pos;",
464+
465+
//"vSliced = float( ((dot(pos - sliceyc, sliceyn) > 0.0) && bool(doslicey)) || ((dot(pos - slicexc, slicexn) > 0.0) && bool(doslicex)) );",
466+
447467
"}"
448468
].join("\n");
449469

@@ -477,6 +497,18 @@ var Shaderlib = (function() {
477497
"uniform vec2 dshape[2];",
478498
"uniform sampler2D data[4];",
479499

500+
"uniform vec3 slicexn;",
501+
"uniform vec3 sliceyn;",
502+
"uniform vec3 slicezn;",
503+
504+
"uniform vec3 slicexc;",
505+
"uniform vec3 sliceyc;",
506+
"uniform vec3 slicezc;",
507+
508+
"uniform bool doslicex;",
509+
"uniform bool doslicey;",
510+
"uniform bool doslicez;",
511+
480512
// "uniform float hatchAlpha;",
481513
// "uniform vec3 hatchColor;",
482514
// "uniform sampler2D hatch;",
@@ -489,6 +521,8 @@ var Shaderlib = (function() {
489521
"varying float vCurv;",
490522
"varying float vMedial;",
491523
"varying float vThickmix;",
524+
"varying float vSliced;",
525+
"varying vec3 vWorldPosition;",
492526

493527
utils.standard_frag_vars,
494528
utils.rand,
@@ -501,6 +535,21 @@ var Shaderlib = (function() {
501535
//Curvature Underlay
502536
"float ctmp = clamp(vCurv / smoothness, -0.5, 0.5);", // use limits here too
503537
"float curv = clamp(ctmp * contrast + brightness, 0.0, 1.0);",
538+
//"if (vSliced > 0.5) discard;",
539+
540+
"bool clipx = dot(vWorldPosition - slicexc, slicexn) > 0.0;",
541+
"bool clipy = dot(vWorldPosition - sliceyc, sliceyn) > 0.0;",
542+
"bool clipz = dot(vWorldPosition - slicezc, slicezn) > 0.0;",
543+
544+
"if (clipx && doslicex && !doslicey && !doslicez) discard;",
545+
"if (clipy && !doslicex && doslicey && !doslicez) discard;",
546+
"if (clipz && !doslicex && !doslicey && doslicez) discard;",
547+
"if (clipx && clipy && doslicex && doslicey && !doslicez) discard;",
548+
"if (clipx && clipz && doslicex && !doslicey && doslicez) discard;",
549+
"if (clipy && clipz && !doslicex && doslicey && doslicez) discard;",
550+
"if (clipx && clipy && clipz && doslicex && doslicey && doslicez) discard;",
551+
//"if( ((dot(vWorldPosition - sliceyc, sliceyn) > 0.0) && bool(doslicey)) && ((dot(vWorldPosition - slicexc, slicexn) > 0.0) && bool(doslicex)) && ((dot(vWorldPosition - slicezc, slicezn) > 0.0) && bool(doslicez)) ) discard;",
552+
504553
"vec4 cColor = vec4(vec3(curv), 1.0);",
505554

506555
"vec3 coord_x, coord_y;",

cortex/webgl/resources/js/sliceplane.js

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ var sliceplane = (function(module) {
5656
this.mesh = new THREE.Mesh(this.geometry, this.shader);
5757
this.mesh.doubleSided = true;
5858

59+
this.flip_clip = 1; // 1 or -1
60+
5961
this.object.add(this.mesh);
6062
//this.scene.add(this.mesh);
6163
this.scene.add(this.object);
@@ -127,6 +129,9 @@ var sliceplane = (function(module) {
127129
imat.multiplyVector3(this.geometry.vertices[2].set(shape[0]-0.5,-0.5,slice));
128130
imat.multiplyVector3(this.geometry.vertices[3].set(shape[0]-0.5,shape[1]-0.5,slice));
129131
}
132+
this.center = new THREE.Vector3().add(this.geometry.vertices[0]).add(this.geometry.vertices[1]).add(this.geometry.vertices[2]).add(this.geometry.vertices[3]).divideScalar(4);
133+
134+
this.updateClipping();
130135

131136
this.geometry.computeBoundingSphere();
132137
var center = this.geometry.boundingSphere.center;
@@ -184,15 +189,45 @@ var sliceplane = (function(module) {
184189

185190
this.mesh.rotation.set(0,0,0);
186191
this.mesh.rotateOnAxis(axis, angle / 180 * Math.PI);
192+
193+
this.updateClipping();
187194
}
188195
module.Plane.prototype.setVisible = function(val) {
189196
if (val === undefined)
190197
return this._visible;
191198

192199
this._visible = val;
193200

194-
if (this.mesh !== undefined)
201+
if (this.mesh !== undefined) {
195202
this.mesh.visible = this._visible;
203+
this.updateClipping();
204+
}
205+
206+
}
207+
module.Plane.prototype.updateClipping = function() {
208+
this.normal = this.geometry.faces[0].normal.clone().applyEuler(this.mesh.rotation).multiplyScalar(this.flip_clip);
209+
210+
if (this.dir == 0){
211+
this.viewer.active.uniforms.slicexn.value.copy(this.normal);
212+
this.viewer.active.uniforms.slicexc.value.copy(this.center);
213+
} else if (this.dir == 1){
214+
this.viewer.active.uniforms.sliceyn.value.copy(this.normal);
215+
this.viewer.active.uniforms.sliceyc.value.copy(this.center);
216+
} else if (this.dir == 2){
217+
this.viewer.active.uniforms.slicezn.value.copy(this.normal);
218+
this.viewer.active.uniforms.slicezc.value.copy(this.center);
219+
}
220+
}
221+
module.Plane.prototype.setFlip = function(val) {
222+
if (val === undefined) {
223+
return this.flip_clip == -1;
224+
}
225+
if (val) {
226+
this.flip_clip = -1;
227+
} else {
228+
this.flip_clip = 1;
229+
}
230+
this.updateClipping();
196231
}
197232

198233
module.MIP = function(viewer) {

0 commit comments

Comments
 (0)