Skip to content

Commit ee79e7f

Browse files
authored
Merge pull request #33 from martinRenou/threshold_inclusive
Add inclusive option to the Threshold effect
2 parents 59ace32 + ba06d8c commit ee79e7f

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

src/Effects/Threshold.ts

+26-13
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ interface ThresholdOptions extends BlockOptions {
2828
max?: number;
2929

3030
dynamic?: boolean;
31+
inclusive?: boolean;
3132

3233
}
3334

@@ -42,24 +43,28 @@ class Threshold extends Effect {
4243
this._min = options.min !== undefined ? options.min : this._min;
4344
this._max = options.max !== undefined ? options.max : this._max;
4445
this.dynamic = options.dynamic !== undefined ? options.dynamic : this.dynamic;
46+
this._inclusive = options.inclusive !== undefined ? options.inclusive : this._inclusive;
4547
}
4648

4749
// Create min and max float nodes
4850
this.minNode = new Nodes.FloatNode(this._min);
4951
this.maxNode = new Nodes.FloatNode(this._max);
5052

51-
// GLSL's STEP function is more optimized than an if statement
52-
// Returns 1 if input < max, 0 otherwise
53-
this.isUnderMax = new Nodes.MathNode(this.inputNode, this.maxNode, Nodes.MathNode.STEP);
54-
55-
// Returns 1 if input > min, 0 otherwise
56-
this.isOverMin = new Nodes.MathNode(this.minNode, this.inputNode, Nodes.MathNode.STEP);
57-
58-
this.addMaskNode(new Nodes.CondNode(
59-
new Nodes.OperatorNode(this.isUnderMax, this.isOverMin, Nodes.OperatorNode.MUL), new Nodes.FloatNode(0),
60-
Nodes.CondNode.EQUAL,
61-
new Nodes.BoolNode(false), new Nodes.BoolNode(true)
62-
));
53+
// Show only if min < or<= input
54+
this.condNode1 = new Nodes.CondNode(
55+
this.minNode, this.inputNode,
56+
this._inclusive ? Nodes.CondNode.LESS_EQUAL : Nodes.CondNode.LESS,
57+
new Nodes.BoolNode(true), new Nodes.BoolNode(false)
58+
);
59+
this.addMaskNode(this.condNode1);
60+
61+
// Show only if input < or<= max
62+
this.condNode2 = new Nodes.CondNode(
63+
this.inputNode, this.maxNode,
64+
this._inclusive ? Nodes.CondNode.LESS_EQUAL : Nodes.CondNode.LESS,
65+
new Nodes.BoolNode(true), new Nodes.BoolNode(false)
66+
);
67+
this.addMaskNode(this.condNode2);
6368

6469
// If there are tetrahedrons, compute new iso-surfaces
6570
if (this.parent.tetrahedronIndices != null) {
@@ -92,7 +97,6 @@ class Threshold extends Effect {
9297

9398
this.buildMaterial();
9499

95-
96100
this.initialized = true;
97101
}
98102

@@ -168,6 +172,12 @@ class Threshold extends Effect {
168172
return this._max;
169173
}
170174

175+
set inclusive (value: boolean) {
176+
const op = this._inclusive ? Nodes.CondNode.LESS_EQUAL : Nodes.CondNode.LESS;
177+
this.condNode1.op = op;
178+
this.condNode2.op = op;
179+
}
180+
171181
get inputDimension () : InputDimension {
172182
return 1;
173183
}
@@ -178,9 +188,12 @@ class Threshold extends Effect {
178188
private _max: number = 1;
179189

180190
private dynamic: boolean = false;
191+
private _inclusive: boolean = true;
181192

182193
private minNode: Nodes.FloatNode;
183194
private maxNode: Nodes.FloatNode;
195+
private condNode1: Nodes.CondNode;
196+
private condNode2: Nodes.CondNode;
184197

185198
private isUnderMax: Nodes.MathNode;
186199
private isOverMin: Nodes.MathNode;

0 commit comments

Comments
 (0)