Skip to content

Commit 82d64a5

Browse files
authored
Merge pull request #8 from martinRenou/warp
Add Warp effect
2 parents f291f4d + 80a1261 commit 82d64a5

File tree

4 files changed

+111
-7
lines changed

4 files changed

+111
-7
lines changed

src/EffectBlock.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ class Effect extends Block {
106106
}
107107
}
108108

109+
/**
110+
* Add transform node to materials
111+
*/
112+
addTransformNode (operation: NodeOperation, transformNode: Nodes.Node) {
113+
for (const nodeMesh of this.meshes) {
114+
nodeMesh.addTransformNode(operation, transformNode);
115+
}
116+
117+
this.buildMaterial();
118+
}
119+
109120
/**
110121
* Add color node to materials
111122
*/

src/Effects/Warp.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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 * (offset + input) + initialPosition`
18+
**/
19+
export
20+
class Warp extends Effect {
21+
22+
constructor (parent: Block, input: Input, factor: THREE.Vector3, offset: THREE.Vector3) {
23+
super(parent, input);
24+
25+
this.offsetNode = new Nodes.Vector3Node(offset.x, offset.y, offset.z);
26+
this.factorNode = new Nodes.Vector3Node(factor.x, factor.y, factor.z);
27+
28+
this.intermediateTransformNode = new Nodes.OperatorNode(this.offsetNode, this.inputNode, Nodes.OperatorNode.ADD);
29+
30+
this.transformNode = new Nodes.OperatorNode(
31+
this.intermediateTransformNode,
32+
this.factorNode,
33+
Nodes.OperatorNode.MUL
34+
);
35+
36+
this.addTransformNode(NodeOperation.ADD, this.transformNode);
37+
38+
this.buildMaterial();
39+
40+
this.initialized = true;
41+
}
42+
43+
setInput(input?: Input) : void {
44+
super.setInput(input);
45+
46+
if (this.initialized) {
47+
this.intermediateTransformNode.b = this.inputNode;
48+
49+
this.buildMaterial();
50+
}
51+
}
52+
53+
set offset (value: THREE.Vector3) {
54+
this.offsetNode.value = value;
55+
}
56+
57+
get offset () {
58+
return this.offsetNode.value;
59+
}
60+
61+
set factor (value: THREE.Vector3) {
62+
this.factorNode.value = value;
63+
}
64+
65+
get factor () {
66+
return this.factorNode.value;
67+
}
68+
69+
get inputDimension () : InputDimension {
70+
return 3;
71+
}
72+
73+
private initialized: boolean = false;
74+
75+
private offsetNode: Nodes.Vector3Node;
76+
private factorNode: Nodes.Vector3Node;
77+
private intermediateTransformNode: Nodes.OperatorNode;
78+
private transformNode: Nodes.OperatorNode;
79+
80+
protected inputNode: Nodes.Node;
81+
82+
}

src/Effects/effects.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,5 @@ export * from './IsoColor';
44

55
export * from './IsoSurface';
66
export * from './Threshold';
7+
8+
export * from './Warp';

src/NodeMesh.ts

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -121,10 +121,14 @@ class NodeMesh {
121121
}
122122

123123
buildMaterial () {
124-
let position = new Nodes.PositionNode();
124+
let position: NodeOperationResult<Nodes.Node> = new Nodes.PositionNode();
125125
let alpha: NodeOperationResult<Nodes.Node> = new Nodes.FloatNode(1.);
126126
let color: NodeOperationResult<Nodes.Node> = this.defaultColorNode;
127127

128+
for (const transformOperator of this.transformOperators) {
129+
position = transformOperator.operate(position);
130+
}
131+
128132
for (const colorOperator of this.colorOperators) {
129133
color = colorOperator.operate(color);
130134
}
@@ -133,13 +137,11 @@ class NodeMesh {
133137
alpha = alphaOperator.operate(alpha);
134138
}
135139

136-
// TODO: Same for position Node, alpha Node and varyings
137-
138140
this.material.flatShading = true;
139141
this.material.side = THREE.DoubleSide;
140142

141143
// @ts-ignore
142-
this.material.transform = position;
144+
this.material.position = position;
143145
// @ts-ignore
144146
this.material.alpha = alpha;
145147
// @ts-ignore
@@ -164,7 +166,7 @@ class NodeMesh {
164166

165167
copy.hasIndex = this.hasIndex;
166168

167-
// TODO: Copy other operators
169+
copy.transformOperators = this.transformOperators.slice();
168170
copy.alphaOperators = this.alphaOperators.slice();
169171
copy.colorOperators = this.colorOperators.slice();
170172

@@ -174,7 +176,7 @@ class NodeMesh {
174176
}
175177

176178
copyMaterial (other: NodeMesh) {
177-
// TODO: Copy other operators
179+
this.transformOperators = other.transformOperators.slice();
178180
this.alphaOperators = other.alphaOperators.slice();
179181
this.colorOperators = other.colorOperators.slice();
180182

@@ -200,6 +202,13 @@ class NodeMesh {
200202
return this._defaultColor;
201203
}
202204

205+
/**
206+
* Add a Transform node to this mesh material
207+
*/
208+
addTransformNode (operation: NodeOperation, transformNode: Nodes.Node) {
209+
this.transformOperators.push(new NodeOperator<Nodes.Node>(operation, transformNode));
210+
}
211+
203212
/**
204213
* Add a Color node to this mesh material
205214
*/
@@ -297,7 +306,7 @@ class NodeMesh {
297306

298307
private meshCtor: MeshConstructor;
299308

300-
// private transformOperators: NodeOperator<Nodes.TransformNode>[];
309+
private transformOperators: NodeOperator<Nodes.Node>[] = [];
301310
private alphaOperators: NodeOperator<Nodes.Node>[] = [];
302311
private colorOperators: NodeOperator<Nodes.Node>[] = [];
303312

0 commit comments

Comments
 (0)