-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate-ShaderVectorTypes.js
More file actions
204 lines (144 loc) · 5.8 KB
/
generate-ShaderVectorTypes.js
File metadata and controls
204 lines (144 loc) · 5.8 KB
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
const fs = require('fs');
// Returns an array of arrays with all the combinations of
// vector elements -- xyz, zyx, xxyy, etc
// els: an array of elements to generate the combinations for -- ['x', 'y', 'z']
// ct: the final number of elements in the arrays to be generated
function getElementCombinations(els, ct, currarr = []) {
if (currarr.length == ct) return [currarr];
const res = [];
for (const e in els) {
res.push(...getElementCombinations(els, ct, [...currarr, els[e]]));
}
return res;
}
// Creates a function based on the elements and size of vector being used
// ex:
// public static Vector2 xx(this Vector3 v) { return new Vector2(v.x, v.x); }
function elementsToFunction(els, size) {
const rawfields = ['x', 'y', 'z', 'w'];
const thisvec = `Vector${ size }`;
const tgvec = `Vector${ els.length }`;
const args = els.map(e => `${ e }`).join(', ');
let member = `public ${ tgvec } ${ els.join('') } { `;
member += `get { return new ${ tgvec }(${args}); } `;
member += `set { ${ els.map((e, i) => `${ e } = value.${ rawfields[i] };` ).join(' ') } }`;
member += ' }';
return member;
}
// returns an overloaded operator function for a vector with the provided members
// ex:
// public static Vector2 operator +(Vector2 a, Vector2 b) { a.x += b.x; a.y += b.y; return a; }
function toOperatorFunction(els, op) {
const thisvec = `Vector${ els.length }`;
let func = `public static ${ thisvec } operator ${ op }(${ thisvec } a, ${ thisvec } b) `;
func += `{ ${ els.map(e => `a.${ e } ${ op }= b.${ e };`).join(' ') } return a; }`
return func;
}
// returns a list of constructors for all combinations of vector lengths
function generateConstructors(els) {
function getArgLengthArray(ct, arr = []) {
if (ct == 0) return [arr];
const res = [];
if (arr.length !== 0) res.push(arr);
for (let i = 1; i <= ct; i ++) {
res.push(...getArgLengthArray(ct - i, [...arr, i]));
}
return res;
}
const rawfields = ['x', 'y', 'z', 'w'];
return getArgLengthArray(els.length)
.map(lens => {
const args = lens
.map((l, i) => `${ l == 1 ? 'float' : `Vector${ l }` } v${ i }`)
.join(', ');
let cons = `public Vector${ els.length }(${ args }) { `;
let curr = 0;
lens.forEach((l, i) => {
if (l == 1) {
cons += `${ rawfields[curr] } = v${ i }; `;
curr ++;
} else {
for(let j = 0; j < l; j ++) {
cons += `${ rawfields[curr] } = v${ i }.${ rawfields[j] }; `;
curr ++;
}
}
});
while (curr < els.length) {
cons += `${ rawfields[curr] } = 0; `;
curr ++;
}
cons += '}';
return cons;
});
}
function generateArrayAccessor(els) {
let getter = els
.reduce((v, e, i) => v + `if(i == ${ i }) return ${ els[ i ] }; else `, '') + 'throw new System.IndexOutOfRangeException();';
let setter = els
.reduce((v, e, i) => v + `if(i == ${ i }) ${ els[ i ] } = value; else `, '') + 'throw new System.IndexOutOfRangeException();';
return `public float this[int i] { get { ${ getter } } set { ${setter} } }`
}
const vecfields = ['x', 'y', 'z', 'w'];
const colfields = ['r', 'g', 'b', 'a'];
const operators = ['+', '-', '*', '/'];
let body = '';
// vector size
for (let i = 2; i <= 4; i ++) {
// extension size
const fields = vecfields.slice(0, i);
const cfields = colfields.slice(0, i);
let structdef =
`
public struct Vector${ i } {
public float ${ fields.join() };
${
// getters and setters for rgb fields
fields
.map((v, i) => {
const getter = `get { return this.${ fields[i] }; }`;
const setter = `set { this.${ fields[i] } = value; }`;
return ` public float ${ cfields[i] } { ${ getter } ${ setter } }`;
})
.join('\n')
}
// constructors
${ generateConstructors(fields).map(c => ' ' + c).join('\n') }
// operators
${ operators.map(op => ' ' + toOperatorFunction(fields, op)).join('\n') }
public static Vector${ i } operator *(Vector${ i } v, float d) { ${ fields.map(f => `v.${ f } *= d; `).join('') }return v; }
public static Vector${ i } operator /(Vector${ i } v, float d) { ${ fields.map(f => `v.${ f } /= d; `).join('') }return v; }
public static bool operator ==(Vector${ i } a, Vector${ i } b) { return a.Equals(b); }
public static bool operator !=(Vector${ i } a, Vector${ i } b) { return !a.Equals(b); }
public static implicit operator Vector${ i }(UnityEngine.Vector${ i } v) { return new Vector${ i }(${ fields.map(f => `v.${ f }`).join(', ') }); }
public static implicit operator UnityEngine.Vector${ i }(Vector${ i } v) { return new UnityEngine.Vector${ i }(${ fields.map(f => `v.${ f }`).join(', ') }); }
// getters & setters
${ generateArrayAccessor(fields) }
`;
for (let v = 2; v <= 4; v ++) {
structdef +=
getElementCombinations(fields, v)
.map(arr => ` ${ elementsToFunction(arr, i) }`)
.join('\n')
+ '\n';
structdef +=
getElementCombinations(cfields, v)
.map(arr => ` ${ elementsToFunction(arr, i) }`)
.join('\n')
+ '\n';
}
structdef += ' }\n';
body += structdef;
}
fs.writeFileSync('ShaderVectorTypes.cs',
`
/*
* @author Garrett Johnson <garrett.kjohnson@gmail.com>
* https://github.com/gkjohnson/unity-vector-extensions
*
* Generated file
*/
namespace ShaderTypes {
${ body }
}
`);