Skip to content

Commit

Permalink
frontClippingOnly flag and shader code
Browse files Browse the repository at this point in the history
  • Loading branch information
shrekshao committed Aug 2, 2016
1 parent b435e2a commit 5318286
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 11 deletions.
50 changes: 47 additions & 3 deletions api/latest/scenejs.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* A WebGL-based 3D scene graph from xeoLabs
* http://scenejs.org/
*
* Built on 2016-08-01
* Built on 2016-08-02
*
* MIT License
* Copyright 2016, Lindsay Kay
Expand Down Expand Up @@ -8859,6 +8859,7 @@ new (function () {

picking: true, // Picking enabled
clipping: true, // User-defined clipping enabled
frontClippingOnly: true, // Used to assist drawing clipping caps
enabled: true, // Node not culled from traversal
transparent: false, // Node transparent - works in conjunction with matarial alpha properties
backfaces: true, // Show backfaces
Expand Down Expand Up @@ -8892,6 +8893,7 @@ new (function () {

this._core.picking = true; // Picking enabled
this._core.clipping = true; // User-defined clipping enabled
this._core.frontClippingOnly = false;
this._core.enabled = true; // Node not culled from traversal
this._core.transparent = false; // Node transparent - works in conjunction with matarial alpha properties
this._core.backfaces = true; // Show backfaces
Expand Down Expand Up @@ -8920,6 +8922,11 @@ new (function () {
this._engine.display.imageDirty = true;
}

if (flags.frontClippingOnly != undefined) {
core.frontClippingOnly = !!flags.frontClippingOnly;
this._engine.display.imageDirty = true;
}

if (flags.enabled != undefined) {
core.enabled = !!flags.enabled;
this._engine.display.drawListDirty = true;
Expand Down Expand Up @@ -8979,6 +8986,7 @@ new (function () {
return {
picking: core.picking,
clipping: core.clipping,
frontClippingOnly: core.frontClippingOnly,
enabled: core.enabled,
transparent: core.transparent,
backfaces: core.backfaces,
Expand Down Expand Up @@ -9011,10 +9019,23 @@ new (function () {
return this;
};

SceneJS.Flags.prototype.setFrontClippingOnly = function (frontClippingOnly) {
frontClippingOnly = !!frontClippingOnly;
if (this._core.frontClippingOnly != frontClippingOnly) {
this._core.frontClippingOnly = frontClippingOnly;
this._engine.display.imageDirty = true;
}
return this;
};

SceneJS.Flags.prototype.getClipping = function () {
return this._core.clipping;
};

SceneJS.Flags.prototype.getFrontClippingOnly = function () {
return this._core.frontClippingOnly;
};

SceneJS.Flags.prototype.setEnabled = function (enabled) {
enabled = !!enabled;
if (this._core.enabled != enabled) {
Expand Down Expand Up @@ -18462,6 +18483,7 @@ var SceneJS_ProgramSourceFactory = new (function () {
var billboard;
var tangents;
var clipping;
var frontClippingOnly;
var morphing;
var regionMapping;
var regionInteraction;
Expand Down Expand Up @@ -18498,6 +18520,7 @@ var SceneJS_ProgramSourceFactory = new (function () {
billboard = !states.billboard.empty;
tangents = hasTangents(states);
clipping = states.clips.clips.length > 0;
frontClippingOnly = states.flags.frontClippingOnly;
morphing = !!states.morphGeometry.targets;
regionMapping = hasRegionMap();
regionInteraction = regionMapping && states.regionMap.mode !== "info";
Expand Down Expand Up @@ -19061,6 +19084,10 @@ var SceneJS_ProgramSourceFactory = new (function () {

add("uniform vec3 SCENEJS_uWorldEye;");

if (frontClippingOnly) {
add("uniform vec3 SCENEJS_uWorldLook;") // World-space look position
}


/*-----------------------------------------------------------------------------------
* Variables
Expand Down Expand Up @@ -19256,7 +19283,17 @@ var SceneJS_ProgramSourceFactory = new (function () {
add(" float dist = 0.0;");
for (var i = 0; i < states.clips.clips.length; i++) {
add(" if (SCENEJS_uClipMode" + i + " != 0.0) {");

if (frontClippingOnly) {
add(" if (dot(SCENEJS_uWorldLook - SCENEJS_uWorldEye, SCENEJS_uClipNormalAndDist" + i + ".xyz) < 0.0) {");
}

add(" dist += clamp(dot(SCENEJS_vWorldVertex.xyz, SCENEJS_uClipNormalAndDist" + i + ".xyz) - SCENEJS_uClipNormalAndDist" + i + ".w, 0.0, 1000.0);");

if (frontClippingOnly) {
add(" }");
}

add(" }");
}
add(" if (dist > 0.0) { discard; }");
Expand Down Expand Up @@ -20766,8 +20803,8 @@ SceneJS_ChunkFactory.createChunkType({

gl.viewport(0, 0, gl.drawingBufferWidth, gl.drawingBufferHeight);
gl.clearColor(frameCtx.ambientColor[0], frameCtx.ambientColor[1], frameCtx.ambientColor[2], 1.0);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
// gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
//gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT | gl.STENCIL_BUFFER_BIT);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

frameCtx.renderBuf = renderBuf;
}
Expand Down Expand Up @@ -21233,6 +21270,9 @@ SceneJS_ChunkFactory.createChunkType({
this._uVNMatrixDraw = this.program.draw.getUniform("SCENEJS_uVNMatrix");
this._uWorldEyeDraw = this.program.draw.getUniform("SCENEJS_uWorldEye");

// for clipping caps
this._uWorldLookDraw = this.program.draw.getUniform("SCENEJS_uWorldLook");

this._uvMatrixPick = this.program.pick.getUniform("SCENEJS_uVMatrix");
},

Expand All @@ -21256,6 +21296,10 @@ SceneJS_ChunkFactory.createChunkType({
this._uWorldEyeDraw.setValue(this.core.lookAt.eye);
}

if (this._uWorldLookDraw) {
this._uWorldLookDraw.setValue(this.core.lookAt.look);
}

frameCtx.viewMat = this.core.mat;
},

Expand Down
16 changes: 8 additions & 8 deletions api/latest/scenejs.min.js

Large diffs are not rendered by default.

7 changes: 7 additions & 0 deletions src/core/display/chunks/lookAtChunk.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ SceneJS_ChunkFactory.createChunkType({
this._uVNMatrixDraw = this.program.draw.getUniform("SCENEJS_uVNMatrix");
this._uWorldEyeDraw = this.program.draw.getUniform("SCENEJS_uWorldEye");

// for clipping caps
this._uWorldLookDraw = this.program.draw.getUniform("SCENEJS_uWorldLook");

this._uvMatrixPick = this.program.pick.getUniform("SCENEJS_uVMatrix");
},

Expand All @@ -34,6 +37,10 @@ SceneJS_ChunkFactory.createChunkType({
this._uWorldEyeDraw.setValue(this.core.lookAt.eye);
}

if (this._uWorldLookDraw) {
this._uWorldLookDraw.setValue(this.core.lookAt.look);
}

frameCtx.viewMat = this.core.mat;
},

Expand Down
16 changes: 16 additions & 0 deletions src/core/display/programSourceFactory.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ var SceneJS_ProgramSourceFactory = new (function () {
var billboard;
var tangents;
var clipping;
var frontClippingOnly;
var morphing;
var regionMapping;
var regionInteraction;
Expand Down Expand Up @@ -58,6 +59,7 @@ var SceneJS_ProgramSourceFactory = new (function () {
billboard = !states.billboard.empty;
tangents = hasTangents(states);
clipping = states.clips.clips.length > 0;
frontClippingOnly = states.flags.frontClippingOnly;
morphing = !!states.morphGeometry.targets;
regionMapping = hasRegionMap();
regionInteraction = regionMapping && states.regionMap.mode !== "info";
Expand Down Expand Up @@ -621,6 +623,10 @@ var SceneJS_ProgramSourceFactory = new (function () {

add("uniform vec3 SCENEJS_uWorldEye;");

if (frontClippingOnly) {
add("uniform vec3 SCENEJS_uWorldLook;") // World-space look position
}


/*-----------------------------------------------------------------------------------
* Variables
Expand Down Expand Up @@ -816,7 +822,17 @@ var SceneJS_ProgramSourceFactory = new (function () {
add(" float dist = 0.0;");
for (var i = 0; i < states.clips.clips.length; i++) {
add(" if (SCENEJS_uClipMode" + i + " != 0.0) {");

if (frontClippingOnly) {
add(" if (dot(SCENEJS_uWorldLook - SCENEJS_uWorldEye, SCENEJS_uClipNormalAndDist" + i + ".xyz) < 0.0) {");
}

add(" dist += clamp(dot(SCENEJS_vWorldVertex.xyz, SCENEJS_uClipNormalAndDist" + i + ".xyz) - SCENEJS_uClipNormalAndDist" + i + ".w, 0.0, 1000.0);");

if (frontClippingOnly) {
add(" }");
}

add(" }");
}
add(" if (dist > 0.0) { discard; }");
Expand Down
21 changes: 21 additions & 0 deletions src/core/scene/flags.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

picking: true, // Picking enabled
clipping: true, // User-defined clipping enabled
frontClippingOnly: true, // Used to assist drawing clipping caps
enabled: true, // Node not culled from traversal
transparent: false, // Node transparent - works in conjunction with matarial alpha properties
backfaces: true, // Show backfaces
Expand Down Expand Up @@ -43,6 +44,7 @@

this._core.picking = true; // Picking enabled
this._core.clipping = true; // User-defined clipping enabled
this._core.frontClippingOnly = false;
this._core.enabled = true; // Node not culled from traversal
this._core.transparent = false; // Node transparent - works in conjunction with matarial alpha properties
this._core.backfaces = true; // Show backfaces
Expand Down Expand Up @@ -71,6 +73,11 @@
this._engine.display.imageDirty = true;
}

if (flags.frontClippingOnly != undefined) {
core.frontClippingOnly = !!flags.frontClippingOnly;
this._engine.display.imageDirty = true;
}

if (flags.enabled != undefined) {
core.enabled = !!flags.enabled;
this._engine.display.drawListDirty = true;
Expand Down Expand Up @@ -130,6 +137,7 @@
return {
picking: core.picking,
clipping: core.clipping,
frontClippingOnly: core.frontClippingOnly,
enabled: core.enabled,
transparent: core.transparent,
backfaces: core.backfaces,
Expand Down Expand Up @@ -162,10 +170,23 @@
return this;
};

SceneJS.Flags.prototype.setFrontClippingOnly = function (frontClippingOnly) {
frontClippingOnly = !!frontClippingOnly;
if (this._core.frontClippingOnly != frontClippingOnly) {
this._core.frontClippingOnly = frontClippingOnly;
this._engine.display.imageDirty = true;
}
return this;
};

SceneJS.Flags.prototype.getClipping = function () {
return this._core.clipping;
};

SceneJS.Flags.prototype.getFrontClippingOnly = function () {
return this._core.frontClippingOnly;
};

SceneJS.Flags.prototype.setEnabled = function (enabled) {
enabled = !!enabled;
if (this._core.enabled != enabled) {
Expand Down

0 comments on commit 5318286

Please sign in to comment.