Skip to content

Commit ca727e5

Browse files
authored
Merge pull request #36 from martinRenou/warpscalar
Add WarpByScalar effect
2 parents 6c0da1d + cb59148 commit ca727e5

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/Effects/WarpByScalar.ts

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
import * as Nodes from 'three/examples/jsm/nodes/Nodes';
2+
3+
import {
4+
Effect, Input, InputDimension
5+
} from '../EffectBlock';
6+
7+
import {
8+
Block
9+
} from '../Block';
10+
11+
import {
12+
NodeOperation
13+
} from '../NodeMesh';
14+
15+
16+
/**
17+
* Transform the vertices positions. The new vertice position is equal to `factor * input * normal + initialPosition`
18+
**/
19+
export
20+
class WarpByScalar extends Effect {
21+
22+
constructor (parent: Block, input: Input, factor: number) {
23+
super(parent, input);
24+
25+
this.factorNode = new Nodes.FloatNode(factor);
26+
27+
this.intermediateTransformNode = new Nodes.OperatorNode(
28+
this.factorNode,
29+
this.inputNode,
30+
Nodes.OperatorNode.MUL
31+
);
32+
33+
this.transformNode = new Nodes.OperatorNode(
34+
this.intermediateTransformNode,
35+
new Nodes.NormalNode(Nodes.NormalNode.WORLD),
36+
Nodes.OperatorNode.MUL
37+
);
38+
39+
this.addTransformNode(NodeOperation.ADD, this.transformNode);
40+
41+
this.buildMaterial();
42+
43+
this.initialized = true;
44+
45+
// There is no new geometry specific to this effect, we forward the parent event
46+
this.parent.on('change:geometry', () => { this.trigger('change:geometry'); });
47+
48+
this.updateMatrix();
49+
}
50+
51+
setInput(input?: Input) : void {
52+
super.setInput(input);
53+
54+
if (this.initialized) {
55+
this.intermediateTransformNode.b = this.inputNode;
56+
57+
this.buildMaterial();
58+
}
59+
}
60+
61+
set factor (value: number) {
62+
this.factorNode.value = value;
63+
}
64+
65+
get factor () {
66+
return this.factorNode.value;
67+
}
68+
69+
get inputDimension () : InputDimension {
70+
return 1;
71+
}
72+
73+
private initialized: boolean = false;
74+
75+
private factorNode: Nodes.FloatNode;
76+
private intermediateTransformNode: Nodes.OperatorNode;
77+
private transformNode: Nodes.OperatorNode;
78+
79+
protected inputNode: Nodes.Node;
80+
81+
}

src/Effects/effects.ts

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export * from './IsoSurface';
77
export * from './Threshold';
88

99
export * from './Warp';
10+
export * from './WarpByScalar';
1011

1112
export * from './Water/Water';
1213
export * from './Water/UnderWater';

0 commit comments

Comments
 (0)