-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFactory.js
147 lines (108 loc) · 2.79 KB
/
Factory.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
(function ($) {
/**
* Helps build graphs of blocks/nodes with chainable API.
*/
$.Factory = function () {
this.end();
};
$.Factory.prototype = {
snippet: function (code, op) {
op = op || 'append';
var block = new $.Block.Snippet(ShaderGraph.getShader(code));
this[op](block.node());
return this;
},
material: function (vertex, fragment, op) {
op = op || 'append';
var block = new $.Block.Material(ShaderGraph.getShader(vertex), ShaderGraph.getShader(fragment));
this[op](block.node());
return this;
},
snippetBefore: function (code) {
this.snippet(code, 'prepend');
return this;
},
materialBefore: function (vertex, fragment) {
this.material(code, 'prepend');
return this;
},
append: function (node) {
if (!node.graph) this.graph.add(node);
var context = this.stack[0];
_.each(context.end, function (end) {
end.connect(node);
});
if (!context.start.length) {
context.start = [node];
}
context.end = [node];
return this;
},
prepend: function (node) {
if (!node.graph) this.graph.add(node);
var context = this.stack[0];
_.each(context.start, function (start) {
node.connect(start);
});
if (!context.end.length) {
context.end = [node];
}
context.start = [node];
return this;
},
group: function () {
// Inner var holds working state, outer var holds accumulated state.
this.stack.unshift({ start: [], end: [] });
this.stack.unshift({ start: [], end: [] });
return this;
},
pass: function () {
this.next();
var sub = this.stack[0];
sub.start.push(null);
return this.combine();
},
next: function () {
var sub = this.stack.shift();
var main = this.stack[0];
main.start = main.start.concat(sub.start);
main.end = main.end.concat(sub.end);
this.stack.unshift({ start: [], end: [] });
return this;
},
combine: function () {
if (this.stack.length <= 2) throw "Popping factory stack too far.";
this.next();
this.stack.shift();
var sub = this.stack.shift(),
main = this.stack[0];
if (sub.start.length) {
_.each(sub.start, function (to) {
// Passthrough all outlets to other side
if (!to) {
sub.end = sub.end.concat(main.end);
}
// Normal destination
else _.each(main.end, function (from) {
from.connect(to, true);
});
});
main.end = sub.end;
}
return this;
},
end: function () {
var graph = this.graph;
this.graph = new $.Graph();
this.stack = [];
this.group();
// Add compile shortcut.
if (graph) {
graph.compile = function () {
return graph.tail().owner().compile();
};
}
return graph;
}//,
};
})(ShaderGraph);