Skip to content

Commit

Permalink
Improve cutting with path and its shaders.
Browse files Browse the repository at this point in the history
  • Loading branch information
alan-wu committed Mar 25, 2019
1 parent eca62df commit 5a78025
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 35 deletions.
11 changes: 7 additions & 4 deletions src/modules/ScaffoldViewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,19 +167,22 @@ var ScaffoldViewer = function(typeAtStartUp) {
if (xmlhttp.readyState == 4) {
if (xmlhttp.status == 200) {
var data = JSON.parse(xmlhttp.responseText);
if (csg)
csg.enablePathCutting(data);
if (csg) {
if (data != null)
csg.enablePathCutting(data);
else
csg.enableStandardCutting();
}
} else {
if (csg)
csg.enableStandardCutting();
}
if (csg) {
csg.allDownloadsCompletedCallback();
csg.updatePlane();
}
}
}
var finalURL = "./getCentralLine";
var finalURL = "./getCenterLine";
xmlhttp.open("GET", finalURL, true);
xmlhttp.send();
settingsChanged = false;
Expand Down
5 changes: 2 additions & 3 deletions src/shaders/uvCutting.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ varying vec3 vViewPosition;
varying vec3 vNormal;
varying vec2 vUv;
uniform float progress;
uniform vec3 ambient;
uniform vec3 emissive;
uniform vec3 specular;
uniform vec3 diffuse;
Expand All @@ -17,7 +16,7 @@ void main(void) {
#ifdef ALPHATEST
if ( gl_FragColor.a < ALPHATEST ) discard;
#endif
float pxielTimeStep = vUv.x;
float pxielTimeStep = vUv.y;
if (reverse == 1)
{
if (pxielTimeStep > progress)
Expand Down Expand Up @@ -67,6 +66,6 @@ vec3 totalSpecular = vec3( 0.0 );
totalDiffuse += dirDiffuse;
totalSpecular += dirSpecular;
#endif
gl_FragColor.xyz = totalDiffuse;
gl_FragColor.xyz = totalDiffuse + totalSpecular + ambientLightColor;
}

78 changes: 50 additions & 28 deletions src/utilities/csg.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ exports.csg = function(sceneIn, zincRendererIn) {
if (zincRendererIn)
zincRendererIn.getThreeJSRenderer().localClippingEnabled = true;
var originalGlyphs = undefined;
var zincRenderer = zincRendererIn;
var preRenderCallbackId = -1;

var getCentroid = function() {
if (currentGeometry) {
Expand Down Expand Up @@ -118,27 +120,27 @@ exports.csg = function(sceneIn, zincRendererIn) {

var updateUniforms = function() {
return function(zincObject) {
if (zincObject.isGeometry) {
if (zincObject.isGeometry && zincObject.groupName) {
if (zincObject.morph.material.uniforms) {
zincObject.morph.material.uniforms["progress"].value = guiControls.distance;
if (guiControls.reverse)
zincObject.morph.material.uniforms["reverse"] = 1;
if (guiControls.reverse == false)
zincObject.morph.material.uniforms["reverse"].value = 0;
else
zincObject.morph.material.uniforms["reverse"] = 0;
zincObject.morph.material.uniforms["reverse"].value = 1;
}
}
}
}

var boxTransformedWithPath = function() {
var transformeBoxWithPath = function() {
if (currentClippingMode == ClippingModes.PATH) {
csgSceneCamera.playPath();
csgSceneCamera.setTime(guiControls.distance*3000.0);
csgSceneCamera.calculatePathNow();
csgSceneCamera.stopPath();
csgScene.camera.updateProjectionMatrix();
var normal = csgScene.camera.target.clone().sub(csgScene.camera.position).normalize();
plane.normal.set(normal.x, normal.y, normal.z);
plane.setFromNormalAndCoplanarPoint(normal, csgScene.camera.position.clone());
planeHelper.updateMatrix();
boxGeometry.morph.setRotationFromEuler(planeHelper.rotation);
boxGeometry.morph.updateMatrix();
Expand Down Expand Up @@ -169,19 +171,11 @@ exports.csg = function(sceneIn, zincRendererIn) {
plane.normal.applyEuler(euler).normalize();
boxGeometry.morph.updateMatrix();
} else if (currentClippingMode == ClippingModes.PATH) {
boxTransformedWithPath();
scene.forEachGeometry(updateUniforms(scene));
transformeBoxWithPath();
scene.forEachGeometry(updateUniforms());
}
}

var updateUniforms = function() {
myUniforms["progress"].value = pathSceneCamera.getTime() / 3000.0;
var directionalLight = scene.directionalLight;
myUniforms["directionalLightDirection"].value.set(directionalLight.position.x,
directionalLight.position.y,
directionalLight.position.z);
}

var createCSG = function() {
if (!zincCSG && currentGeometry) {
var zincGeometry = csgScene.addZincGeometry(currentGeometry, 23499, 0xffffff, 1.0);
Expand Down Expand Up @@ -272,20 +266,17 @@ exports.csg = function(sceneIn, zincRendererIn) {

var setShaderMaterial = function(scene) {
return function(zincObject) {
if (zincObject.isGeometry) {
if (zincObject.isGeometry && zincObject.groupName) {
var originalMaterial = zincObject.morph.material;
var dirLight = scene.directionalLight;
var myUniforms= THREE.UniformsUtils.merge( [
{
"ambient" : { type: "c", value: new THREE.Color( 0xffffff ) },
"emissive" : { type: "c", value: new THREE.Color( originalMaterial.emissive ) },
"specular" : { type: "c", value: new THREE.Color( originalMaterial.specular ) },
"diffuse" : { type: "c", value: new THREE.Color( originalMaterial.color ) },
"emissive" : { type: "c", value: originalMaterial.emissive },
"specular" : { type: "c", value: originalMaterial.specular },
"diffuse" : { type: "c", value: originalMaterial.color },
"shininess": { type: "f", value: originalMaterial.shininess },
"ambientLightColor": { type: "c", value: new THREE.Color( 0x444444 ) },
"directionalLightColor": { type: "c", value: new THREE.Color( 0x888888 ) },
"directionalLightDirection": { type: "v3", value: new THREE.Vector3(
dirLight.position.x, dirLight.position.y, dirLight.position.z ) },
"ambientLightColor": { type: "c", value: scene.ambient.color },
"directionalLightColor": { type: "c", value: scene.directionalLight.color },
"directionalLightDirection": { type: "v3", value: scene.directionalLight.position },
"progress": { type: "f", value: 0.0 },
"reverse": { type: "i", value: 0 },
}
Expand All @@ -303,8 +294,32 @@ exports.csg = function(sceneIn, zincRendererIn) {
}
}


var updateLightDirectionUniforms = function() {
return function(zincObject) {
if (zincObject.isGeometry && zincObject.groupName) {
if (zincObject.morph.material.uniforms) {
var directionalLight = scene.directionalLight;
zincObject.morph.material.uniforms["directionalLightDirection"].value.set(directionalLight.position.x,
directionalLight.position.y,
directionalLight.position.z);
}
}
}
}

var updateLightDirection = function() {
scene.forEachGeometry(updateLightDirectionUniforms());
}

this.allDownloadsCompletedCallback = function() {
if (preRenderCallbackId > 0) {
zincRenderer.removePreRenderCallbackFunction(preRenderCallbackId);
preRenderCallbackId = -1;
}
if (currentClippingMode == ClippingModes.STANDARD) {
boxGeometry.morph.visible = false;
planeHelper.visible = true;
getCentroid();
if (meshRadius > 0) {
var dimension = meshRadius * 2.0 *1.1 + meshCenter.length();
Expand All @@ -328,7 +343,14 @@ exports.csg = function(sceneIn, zincRendererIn) {
}
}
} else if (currentClippingMode == ClippingModes.PATH) {
preRenderCallbackId = zincRenderer.addPreRenderCallbackFunction(updateLightDirection);
scene.forEachGeometry(setShaderMaterial(scene));
boxGeometry.morph.visible = true;
boxGeometry.morph.material.side = THREE.DoubleSide;
planeHelper.visible = false;
if (boxGeometry && boxGeometry.geometry) {
boxGeometry.geometry.scale(3.3, 3.3, 1.0);
}
if (distanceSlider) {
distanceSlider.__min = 0.0;
distanceSlider.__max = 1.0;
Expand All @@ -347,6 +369,7 @@ exports.csg = function(sceneIn, zincRendererIn) {
guiControls.reverse = false;
reverseController.updateDisplay();
}
updatePlane();
}

this.enableStandardCutting = function() {
Expand Down Expand Up @@ -388,10 +411,9 @@ exports.csg = function(sceneIn, zincRendererIn) {
scene.forEachGlyphset(removeCutFace(scene));
};
datGui.add(controls, "remove cuts");
distanceSlider = datGui.add(guiControls, 'distance', -1.0, 1.0).step(0.01).onChange(distanceSliderChanged());
distanceSlider = datGui.add(guiControls, 'distance', -1.0, 1.0).step(0.001).onChange(distanceSliderChanged());
xRotationSlider = datGui.add(guiControls, 'xRotation', -90, 90).step(1).onChange(xRotationSliderChanged());
yRotationSlider = datGui.add(guiControls, 'yRotation', -90, 90).step(1).onChange(yRotationSliderChanged());
//datGui.add(guiControls, 'continuous');
reverseController = datGui.add(guiControls, 'reverse');
reverseController.onChange(function(value) {
updatePlane();
Expand Down

0 comments on commit 5a78025

Please sign in to comment.