Skip to content

Commit a9f5267

Browse files
committed
Add Warp effect
1 parent f291f4d commit a9f5267

File tree

4 files changed

+95
-7
lines changed

4 files changed

+95
-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: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
export
17+
class Warp extends Effect {
18+
19+
constructor (parent: Block, input: Input, factor: number) {
20+
super(parent, input);
21+
22+
this.factorNode = new Nodes.FloatNode(factor);
23+
24+
this.transformNode = new Nodes.OperatorNode(
25+
this.inputNode,
26+
this.factorNode,
27+
Nodes.OperatorNode.MUL
28+
);
29+
30+
this.addTransformNode(NodeOperation.ADD, this.transformNode);
31+
32+
this.buildMaterial();
33+
34+
this.initialized = true;
35+
}
36+
37+
setInput(input?: Input) : void {
38+
super.setInput(input);
39+
40+
if (this.initialized) {
41+
this.transformNode.a = this.inputNode;
42+
43+
this.buildMaterial();
44+
}
45+
}
46+
47+
set factor (value: number) {
48+
this.factorNode.value = value;
49+
}
50+
51+
get factor () {
52+
return this.factorNode.value;
53+
}
54+
55+
get inputDimension () : InputDimension {
56+
return 3;
57+
}
58+
59+
private initialized: boolean = false;
60+
61+
private factorNode: Nodes.FloatNode;
62+
private transformNode: Nodes.OperatorNode;
63+
64+
protected inputNode: Nodes.Node;
65+
66+
}

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)