Skip to content

Commit

Permalink
Merge pull request xeolabs#498 from xeolabs/revert-497-shrekshao-clip…
Browse files Browse the repository at this point in the history
…ping-caps-stencil-plugin

Revert 497 shrekshao clipping caps stencil plugin
  • Loading branch information
xeolabs authored Aug 18, 2016
2 parents 70110a1 + 63df3c5 commit 44e676e
Show file tree
Hide file tree
Showing 17 changed files with 1,134 additions and 27 deletions.
6 changes: 4 additions & 2 deletions api/latest/plugins/node/geometry/boundary.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
* type: "geometry/boundary",
* min: [-9, -3, -2],
* max: [2,3,12],
* wire: false // Default
* wire: true // Default
* });
* </pre>
*/
Expand All @@ -25,6 +25,8 @@

function build(params) {

var wire = params.wire !== undefined ? params.wire : true;

var min = params.min || [0, 0, 0];
var max = params.max || [0, 0, 0];

Expand All @@ -50,7 +52,7 @@
// Otherwise, create a new geometry
return {
type:"geometry",
primitive:params.wire ? "lines" : "triangles",
primitive:wire ? "lines" : "triangles",
coreId:coreId,
positions:[
xmax, ymax, zmax,
Expand Down
93 changes: 93 additions & 0 deletions api/latest/plugins/node/geometry/boxBoundary.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
/**
* Box geometry node type
* init in a boundary style
*
* @author xeolabs / http://xeolabs.com
*
* <p>Usage example:</p>
*
* <pre>
* someNode.addNode({
* type: "geometry/box",
* min: [-9, -3, -2],
* max: [2,3,12],
* wire: false // Default
* });
* </pre>
*/
(function () {

SceneJS.Types.addType("geometry/boxBoundary", {

construct:function (params) {
this.addNode(build.call(this, params));
}
});

function build(params) {

var min = params.min || [0, 0, 0];
var max = params.max || [0, 0, 0];

var xmin = min[0];
var ymin = min[1];
var zmin = min[2];
var xmax = max[0];
var ymax = max[1];
var zmax = max[2];

var x = xmax - xmin;
var y = ymax - ymin;

var coreId = "geometry/boxBoundary_" + xmin + "_" + ymin + "_" + zmin + "_"
+ xmax + "_" + ymax + "_" + zmax + (params.wire ? "wire" : "_solid");

// If a node core already exists for a prim with the given properties,
// then for efficiency we'll share that core rather than create another geometry
if (this.getScene().hasCore("geometry", coreId)) {
return {
type:"geometry",
coreId:coreId
};
}

// Otherwise, create a new geometry
return {
type:"geometry",
primitive:params.wire ? "lines" : "triangles",
coreId:coreId,
positions:new Float32Array([
xmax, ymax, zmax, xmin, ymax, zmax, xmin, ymin, zmax, xmax, ymin, zmax, // v0-v1-v2-v3 front
xmax, ymax, zmax, xmax, ymin, zmax, xmax, ymin, zmin, xmax, ymax, zmin, // v0-v3-v4-v5 right
xmax, ymax, zmax, xmax, ymax, zmin, xmin, ymax, zmin, xmin, ymax, zmax, // v0-v5-v6-v1 top
xmin, ymax, zmax, xmin, ymax, zmin, xmin, ymin, zmin, xmin, ymin, zmax, // v1-v6-v7-v2 left
xmin, ymin, zmin, xmax, ymin, zmin, xmax, ymin, zmax, xmin, ymin, zmax, // v7-v4-v3-v2 bottom
xmax, ymin, zmin, xmin, ymin, zmin, xmin, ymax, zmin, xmax, ymax, zmin // v4-v7-v6-v5 back
]),
normals:new Float32Array([
0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, // v0-v1-v2-v3 front
1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, // v0-v3-v4-v5 right
0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, // v0-v5-v6-v1 top
-1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, // v1-v6-v7-v2 left
0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1, 0, // v7-v4-v3-v2 bottom
0, 0, -1, 0, 0, -1, 0, 0, -1, 0, 0, -1 // v4-v7-v6-v5 back
]),
uv:new Float32Array([
x, y, 0, y, 0, 0, x, 0, // v0-v1-v2-v3 front
0, y, 0, 0, x, 0, x, y, // v0-v3-v4-v5 right
x, 0, x, y, 0, y, 0, 0, // v0-v5-v6-v1 top
x, y, 0, y, 0, 0, x, 0, // v1-v6-v7-v2 left
0, 0, x, 0, x, y, 0, y, // v7-v4-v3-v2 bottom
0, 0, x, 0, x, y, 0, y // v4-v7-v6-v5 back
]),
indices:[
0, 1, 2, 0, 2, 3, // front
4, 5, 6, 4, 6, 7, // right
8, 9, 10, 8, 10, 11, // top
12, 13, 14, 12, 14, 15, // left
16, 17, 18, 16, 18, 19, // bottom
20, 21, 22, 20, 22, 23 // back
]
};
}
})();
223 changes: 223 additions & 0 deletions api/latest/plugins/node/postprocess/clippingCap.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/**
* Credit: algorithm learnt from: https://github.com/daign/clipping-with-caps
*/
SceneJS.Types.addType("postprocess/clippingCap", {

construct: function (params) {

var clipsNodeId = params.clipsNodeId || "clippingCapClipsNode";

var solidColorCaps = params.solidColorCaps !== undefined ? params.solidColorCaps : true;

this._clips = params.clips != undefined ? params.clips : [{x: 0, y: 0, z: 1, dist: 0, mode: "inside"}];

var lenClips = this._clips.length;
var i;

// Build custom clipping shader

var frontClippingFS = [
"precision highp float;\n",
"uniform vec3 SCENEJS_uWorldEye;\n",
"uniform vec3 SCENEJS_uWorldLook;\n",
"uniform vec3 SCENEJS_uMaterialColor;\n"//,
];


for (i = 0; i < lenClips; i++) {
frontClippingFS.push("uniform float SCENEJS_uClipMode" + i + ";\n");
frontClippingFS.push("uniform vec4 SCENEJS_uClipNormalAndDist" + i + ";\n");
}

frontClippingFS.push("varying vec4 SCENEJS_vWorldVertex;\n");
frontClippingFS.push("varying vec2 vUv;\n");

frontClippingFS.push("void main () {\n");

frontClippingFS.push(" float dist = 0.0;\n");

for (i = 0; i < lenClips; i++) {
frontClippingFS.push(" if (SCENEJS_uClipMode" + i + " != 0.0) {");
frontClippingFS.push(" if (dot(SCENEJS_uWorldLook - SCENEJS_uWorldEye, SCENEJS_uClipNormalAndDist" + i + ".xyz) < -SCENEJS_uClipNormalAndDist" + i + ".w) {");
frontClippingFS.push(" dist += clamp(dot(SCENEJS_vWorldVertex.xyz, SCENEJS_uClipNormalAndDist" + i + ".xyz) - SCENEJS_uClipNormalAndDist" + i + ".w, 0.0, 1000.0);");
frontClippingFS.push(" }");
frontClippingFS.push(" }");
}

frontClippingFS.push(" if (dist > 0.0) { discard; }\n");

frontClippingFS.push(" gl_FragColor = vec4(0.0, 0.0, 0.0, 1.0);\n");

frontClippingFS.push("}");


// Whether use solid color cheap frag shader or not
var capDrawingNodes;
if (solidColorCaps) {
capDrawingNodes = [{
type: "shader",

shaders: [
{
stage: "fragment",
code: [
"precision mediump float;",
"uniform vec3 SCENEJS_uMaterialColor;",
"void main () {",
" gl_FragColor = vec4(SCENEJS_uMaterialColor, 1.0);",
"}"
]
}
],

nodes: params.capNodes
}];
} else {
capDrawingNodes = params.capNodes;
}



this.addNodes([
{
type: "clips",
id: clipsNodeId,
clips: this._clips,

nodes: [

// Stage 1
// Back face frag -> stencil buffer +1
// Front face frag -> stencil buffer -1
// Use Front face clipping plane only
{
type: "stage",
priority: 1,

nodes: [
{
type: "depthBuffer",

clearDepth: 1.0,
enabled: true,
depthFunc: "lequal",
clear: true,

nodes: [
{
type: "stencilBuffer",

enabled: true,
clear: true,
clearStencil: 128,
stencilFunc: {
func: "always",
ref: 128,
mask: 0xff
},
stencilOp: {
front: {
sfail: "keep",
dpfail: "decr",
dppass: "decr"
},
back: {
sfail: "keep",
dpfail: "incr",
dppass: "incr"
}
},


nodes: [
{
type: "shader",
shaders: [
{
stage: "fragment",
code: frontClippingFS
}
],

nodes: params.nodes
}
]
}
]
}
]
}


,
// Stage 2
// Draw the actual clipped geometry
{
type: "stage",
priority: 2,

nodes: [
{
type: "depthBuffer",
clear: true,
nodes: [
{
type: "flags",
flags: {
backfaces: false,
clearColorBuffer: true
},

nodes: params.nodes
}
]
}
]
}
]
}

,
// Stage 3
// Draw Caps with stencil test
// Draw the capNodes as the caps
// With certain knowledge of the clipping planes, we can use appropriate
// capNodes geometry to get rid of the overlapping clipping planes artifacts
{
type: "stage",
priority: 3,

nodes: [

{
type: "depthBuffer",
enabled: true,
clear: true,

nodes: [
{
type: "stencilBuffer",

enabled: true,
clear: false,
stencilFunc: {
func: "less",
ref: 128,
mask: 0xff
},
stencilOp: {
sfail: "keep",
dpfail: "keep",
dppass: "keep"
},

nodes: capDrawingNodes
}
]
}
]
}
]);
}

});
Loading

0 comments on commit 44e676e

Please sign in to comment.