Skip to content

Commit ccccbf6

Browse files
committed
use es6 string templates in core node generation
1 parent d665986 commit ccccbf6

File tree

4 files changed

+16
-47
lines changed

4 files changed

+16
-47
lines changed

shader-nodes/shader-nodes/flexOp.js

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,14 @@ var flexOp = {
1010
}
1111
return comp2Type[maxComp];
1212
},
13-
getHeadCode: function (retType, opName, argTypes, outputRetComponents ) {
14-
var code = retType + ' ' + opName + '( ';
13+
getHeadCode: function (retType, opName, argTypes, precision) {
14+
precision = precision ? `_${precision}` : '';
15+
var code = `${retType} ${opName}${precision}( `; // alter name to create variant
1516
for (var argIndex = 0; argIndex < argTypes.length; argIndex++) {
16-
code += 'in ' + argTypes[argIndex] + ' arg' + argIndex;
17+
code += `in ${argTypes[argIndex]} arg${argIndex}`;
1718

1819
code += (argIndex === argTypes.length - 1) ? ' )\n' : ', ';
1920
}
20-
if (outputRetComponents && type2Comp[retType] === 4) {
21-
code.replace(' )\n', ', out vec3 rgb, out float r, out float g, out float b, out float a )\n');
22-
}
23-
2421
return code;
2522
},
2623
genOp: function (opName, opCode, label ) {
@@ -40,31 +37,23 @@ var flexOp = {
4037
var retType = this.getRetType(argTypes);
4138

4239
// construct head code
43-
var code = this.getHeadCode(retType, opName, argTypes);
44-
45-
// precision - alter name to create variant
46-
if (options && options.precision) {
47-
code = code.replace(' ' + opName, ' ' + opName + '_' + options.precision);
48-
}
40+
var code = this.getHeadCode(retType, opName, argTypes, options ? options.precision : '');
4941

5042
// construct body code
5143
code += '{\n';
5244

53-
// precision - tmp: useful comment
54-
code += ((options && options.precision) ? '// precision ' + options.precision + ' float;\n' : '');
55-
5645
code += ' return ';
5746

5847
for (var argIndex = 0; argIndex < argTypes.length; argIndex++) {
5948
// convert all arguments to return type
60-
code += retType + '(arg' + argIndex;
49+
code += `${retType}(arg${argIndex}`;
6150

6251
// expand if needed - extra components are set to zero - NB scalar values are broadcast to all components
6352
var argComp = type2Comp[argTypes[argIndex]];
6453
var extend = type2Comp[retType] - argComp;
65-
code += (extend > 0 && argComp !== 1) ? ', ' + comp2Type[expand] + '(0))' : ')';
54+
code += (extend > 0 && argComp !== 1) ? `, ${comp2Type[expand]}(0))` : ')';
6655

67-
code += (argIndex === argTypes.length - 1) ? ';\n' : ' ' + opCode + ' ';
56+
code += (argIndex === argTypes.length - 1) ? ';\n' : ` ${opCode} `;
6857
}
6958

7059
code += '}\n';

shader-nodes/shader-nodes/select.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,27 +10,19 @@ var select = {
1010
var retType = flexOp.getRetType(argTypes);
1111

1212
// construct head code
13-
var code = flexOp.getHeadCode(retType, 'select', argTypes);
14-
15-
// precision - alter name to create variant
16-
if (options && options.precision) {
17-
code = code.replace(' select', ' select_' + options.precision);
18-
}
13+
var code = flexOp.getHeadCode(retType, 'select', argTypes, options ? options.precision : '');
1914

2015
// construct body code
2116
code += '{\n';
2217

23-
// precision - tmp: useful comment
24-
code += ((options && options.precision) ? '// precision ' + options.precision + ' float;\n' : '');
25-
2618
for (var argIndex = 1; argIndex < argTypes.length; argIndex++) {
27-
code += 'if (arg0>=float(' + (argIndex - 1) + ') && arg0<float(' + (argIndex + 0) + ') )\n';
19+
code += `if (arg0>=float(${argIndex - 1}) && arg0<float(${argIndex + 0}) )\n`;
2820
code += '{\n';
2921
code += ' return ';
30-
code += retType + '(arg' + argIndex;
22+
code += `${retType}(arg${argIndex}`;
3123
var argComp = type2Comp[argTypes[argIndex]];
3224
var extend = type2Comp[retType] - argComp;
33-
code += (extend > 0 && argComp !== 1) ? ', ' + comp2Type[extend] + '(0))' : ')';
25+
code += (extend > 0 && argComp !== 1) ? `, ${comp2Type[extend]}(0))` : ')';
3426
code += ';\n';
3527
code += '}\n';
3628
}

shader-nodes/shader-nodes/simpleOp.js

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,8 @@ var simpleOp = {
66
editor: { label: label },
77
// generator
88
gen: function ( argTypes, options ) {
9-
var code = retType + " " + opName + "(){\n return " + retName + ";\n}\n";
10-
if (options && options.precision) {
11-
// precision - alter name to create variant
12-
code = code.replace(' ' + opName, ' ' + opName + '_' + options.precision);
13-
// precision - tmp: useful comment
14-
code = code.replace( '{\n', '{\n// precision ' + options.precision + ' float;\n');
15-
}
9+
var precision = (options && options.precision) ? `_${options.precision}` : ''; // alter name to create variant
10+
var code = `${retType} ${opName}${precision}(){\n return ${retName};\n}\n`;
1611
return code;
1712
}
1813
};

shader-nodes/shader-nodes/texSample.js

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,12 @@ var texSample = {
66
// generator
77
gen: function ( argTypes, options ) {
88
// header
9-
var code = "void texSample(in sampler2D tex, in vec2 uv, out vec4 rgba, out vec4 srgba, out vec3 rgbm)\n";
10-
11-
// precision - alter name to create variant
12-
if (options && options.precision) {
13-
code = code.replace(' texSample', ' texSample_' + options.precision);
14-
}
9+
var precision = (options && options.precision) ? `_${options.precision}` : ''; // alter name to create variant
10+
var code = `void texSample${precision}(in sampler2D tex, in vec2 uv, out vec4 rgba, out vec4 srgba, out vec3 rgbm)\n`;
1511

1612
// body
1713
code += '{\n';
1814

19-
// precision - tmp: useful comment
20-
code += ((options && options.precision) ? '// precision ' + options.precision + ' float;\n' : '');
21-
2215
code += 'rgba = texture2D(tex, uv);\n';
2316

2417
code += 'srgba.rgb = gammaCorrectInput(rgba.rgb);\n';

0 commit comments

Comments
 (0)