-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateBlocks.js
166 lines (157 loc) · 4.77 KB
/
generateBlocks.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
var hydraFnTypesToBlocklyCategories = {
"src": {
"category": "I/O & Texture",
"index": 0
},
"coord": {
"category": "Geometry Transforms",
"index": 1
},
"color": {
"category": "Adjust Color",
"index": 2
},
"combine": {
"category": "Blend",
"index": 3
},
// "combineCoord": {
// "category": "Modulators",
// "index": 4
// },
// "parameter": {
// "category": "Parameters",
// "index": 5
// },
};
var hueDivisor = Object.keys(hydraFnTypesToBlocklyCategories).length + 1;
var hueMultiplier = 360 / hueDivisor;
var parser = new DOMParser();
var toolbox = `<xml xmlns="https://developers.google.com/blockly/xml"></xml>`;
toolbox = parser.parseFromString(toolbox, "application/xml");
toolbox = toolbox.firstElementChild;
var workspaceBlocks = `<xml xmlns="https://developers.google.com/blockly/xml">
<block type="webcam">
<next>
<block type="out"></block>
</next>
</block>
</xml>`;
workspaceBlocks = parser.parseFromString(workspaceBlocks, "application/xml");
workspaceBlocks = workspaceBlocks.firstElementChild;
function code(blockType, args, dot) {
return `${dot ? '' : '.'}${blockType}(${args.join(',')})`
}
var gradients = ["linearGradient", "radialGradient", "conicGradient"];
var blendModes = ["darken","multiply","colorBurn","linearBurn","darkerColor","lighten","screen","colorDodge","linearDodge","lighterColor","overlay","softLight","hardLight","vividLight","linearLight","pinLight","hardMix","difference","exclusion","subtract","divide","hueBlend","colorBlend","saturationBlend","luminosityBlend"];
var unsupportedFns = ['r', 'g', 'b', 'a', 'prev', 'sum', 'src'];
var unsupportedTypes = ["combineCoord"];
var specialFns = [{
name: 'blendMode',
type: 'combine'
},{
name: 'webcam',
type: 'src'
},
{
name: 'linearGradient',
type: 'src'
},
{
name: 'radialGradient',
type: 'src'
},
{
name: 'conicGradient',
type: 'src'
},{
name: 'image',
type: 'src'
},{
name: 'video',
type: 'src'
},{
name: 'out',
type: 'src'
}
];
var hydraFns = Object.values(hydra.generator.glslTransforms);
hydraFns.forEach(fn => {
if(unsupportedFns.indexOf(fn.name) > -1) return;
if(blendModes.indexOf(fn.name) > -1) return;
if(gradients.indexOf(fn.name) > -1) return;
if(unsupportedTypes.indexOf(fn.type) > -1) return;
fn.inputs.forEach(ip => {
if(ip.type === 'vec4') {
ip.name = 'texture';
}
});
Blockly.defineBlocksWithJsonArray([{
type: fn.name,
message0: `${fn.type === 'src' ? '' : '.'}${fn.name}( ${fn.inputs.map((ip, i) => `${ip.name}: %${i+1}`).join(' , ')})`,
args0: fn.inputs.map(ip => {
if(ip.type === 'vec4') {
return {
type: 'input_statement',
name: ip.name
}
} else if (ip.type === 'sampler2D') {
return {
type: 'input_value',
name: ip.name
}
} else {
return {
type: 'field_number',
name: ip.name,
value: ip.default
}
}
}
),
previousStatement: '',
nextStatement: '',
colour: hydraFnTypesToBlocklyCategories[fn.type].index * hueMultiplier,
tooltip: '',
helpUrl: ''
}]);
Blockly.JavaScript[fn.name] = function (block) {
if(block.getNextBlock() === null && block.getPreviousBlock() === null) return '';
var args = fn.inputs.map(ip => {
if(ip.type === 'vec4') {
return Blockly.JavaScript.statementToCode(block, ip.name);
} else if (ip.type === 'sampler2D') {
var value_tex = Blockly.JavaScript.valueToCode(block, ip.name, Blockly.JavaScript.ORDER_ATOMIC);
value_tex = value_tex.substring(1, value_tex.length - 1);
return value_tex;
} else {
return block.getFieldValue(ip.name)
// Blockly.JavaScript.valueToCode(block, ip.name, Blockly.JavaScript.ORDER_ATOMIC);
}
});
return code(block.type, args, fn.type === 'src');
};
var blockXML = `<block type="${fn.name}">
${fn.inputs.map(ip => {
if(ip.type === 'vec4') return '';
else if (ip.type === 'sampler2D') {
return `<value name="${ip.name}">
<block type="text">
<field name="TEXT">o1</field>
</block>
</value>`
}
else return ``
}).join('\n')}
</block>`;
blockXML = parser.parseFromString(blockXML, "application/xml");
var categoryNode = toolbox.querySelector(`category[name="${hydraFnTypesToBlocklyCategories[fn.type].category}"]`);
if(!categoryNode) {
categoryNode = toolbox.parentNode.createElement('category');
categoryNode.setAttribute('name', hydraFnTypesToBlocklyCategories[fn.type].category);
categoryNode.setAttribute('colour', hydraFnTypesToBlocklyCategories[fn.type].index * hueMultiplier);
toolbox.appendChild(categoryNode);
}
categoryNode.appendChild(blockXML.firstElementChild);
});
specialFns.forEach(addSpecialBlock);