-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmodule_appearance.js
More file actions
292 lines (235 loc) · 7.38 KB
/
module_appearance.js
File metadata and controls
292 lines (235 loc) · 7.38 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
// the appearance module defines the general shape of the entity object
// it relies on a 'source code' that defines what shapes get drawn
// note: all geometry is drawn in local space!
module.exports = function(entity_module_builder) {
entity_module_builder.registerModule(
"appearance",
"this module handles representing the entity with customizable 3D shapes",
function(module) {
// overriding respond function
module.respondToMessage = function(message, data) {
var entity = this.API.getEntityById(this.entity_id);
switch(message) {
case "update":
break;
// inspector panel request
// we send back to the socket the contents of our block
case "inspector_panel":
data.socket.emit("inspector_panel_block", {
elements: [
this.API.outputPlainText("This is the code that defines what gets drawn."),
this.API.outputEditableCode("appearance_code", this.input_code)
],
rank: this.rank
});
break;
// we received a new value from the inspector
case "inspector_panel_change":
if(data.appearance_code) {
this.input_code = data.appearance_code;
this.parseInstructionList();
this.API.setChanged(this.entity_id);
}
break;
}
};
// override render function
module.appendDrawingInstructions = function(instructions_list) {
for(var i=0; i<this.instructions.length; i++) {
instructions_list.push(this.instructions[i]);
}
};
// this is the input code written by the client
// it is then parsed into drawing instructions
// the default code is randomized
var size = Math.random()*1.2 + 0.55;
var hue = Math.random();
module.input_code =
"color_hsl "+hue.toFixed(2)+" 0.8 0.7\n"+
"disc 1.1\n"+
"moveto 0 "+(size/2).toFixed(2)+" 0\n"+
"box 1 "+size.toFixed(2)+" 1\n"+
"move 0 "+(size/2+0.5).toFixed(2)+" 0\n"+
"box 1 0.7 1";
// generated drawing instructions list (refreshed on each edit)
module.instructions = [];
// this functions parses the input code
// if no error, returns null
// if there are errors, returns an object like so { line_number: error_string }
module.parseInstructionList = function() {
// a temp array of instructions
// if there is an error in the parsing, this will not be used
var instructions = [];
var lines = this.input_code.split("\n");
var result = {};
var error = false;
// instructions are composed of a verb and parameters
var verb, line, params, param_count, param, instr, number, a, b, i, j;
for(i=0; i<lines.length; i++) {
// init
params = [];
param_count = 0;
line = lines[i];
// first clean up the line (remove extra spaces, lower case)
line = line.replace(/\s+/g,' ').trim().toLowerCase();
// find the verb
a = line.indexOf(" ");
verb = line.substring(0, a);
switch(verb) {
case "moveto": param_count = 3; break;
case "move": param_count = 3; break;
case "box": param_count = 3; break;
case "disc": param_count = 1; break;
case "sphere": param_count = 1; break;
case "color": param_count = 3; break;
case "color_hsl": param_count = 3; break;
case "shiftcolor": param_count = 3; break;
case "shiftcolor_hsl": param_count = 3; break;
default: result[i] = "instruction invalid"; error = true;
}
params.push(verb);
// parse the parameters
for(j=0; j<param_count; j++) {
// check for quotes...
if(line.substring(a, 1) == "'") {
b = line.indexOf("'", a+1);
number = false;
} else {
b = line.indexOf(" ", a+1);
if(b == -1) {
// force end of line
b = line.length;
j = param_count;
}
number = true;
}
param = line.substring(a+1, b);
if(number) { params.push(parseFloat(param)); }
else { params.push(param); }
a = b;
}
instructions.push(params);
}
if(error) { return result; }
// generate drawing instructions
this.instructions = [];
var translation = {x:0, y:0, z:0};
var rotation = {x:0, y:0, z:0};
var color = {r:0, g:0, b:0}; // alpha not supported
for(i=0; i<instructions.length; i++) {
params = instructions[i];
switch(params[0]) {
case "moveto":
translation.x = params[1];
translation.y = params[2];
translation.z = params[3];
break;
case "move":
translation.x += params[1];
translation.y += params[2];
translation.z += params[3];
break;
case "box":
this.API.drawBox(
this.instructions,
translation.x, translation.y, translation.z,
0, 0, 0,
params[1], params[2], params[3],
color.r, color.g, color.b
);
break;
case "disc":
this.API.drawDisc(
this.instructions,
translation.x, translation.y, translation.z,
0, 0, 0,
params[1],
color.r, color.g, color.b
);
break;
case "sphere":
this.API.drawSphere(
this.instructions,
translation.x, translation.y, translation.z,
params[1],
color.r, color.g, color.b
);
break;
case "color":
color.r = Math.max(Math.min(params[1], 1), 0);
color.g = Math.max(Math.min(params[2], 1), 0);
color.b = Math.max(Math.min(params[3], 1), 0);
break;
case "color_hsl":
a = this.convertHSLtoRGB(
Math.max(Math.min(params[1], 1), 0),
Math.max(Math.min(params[2], 1), 0),
Math.max(Math.min(params[3], 1), 0)
);
color.r = a.r;
color.g = a.g;
color.b = a.b;
break;
case "shiftcolor":
color.r = Math.max(Math.min(color.r+params[1], 1), 0);
color.g = Math.max(Math.min(color.g+params[2], 1), 0);
color.b = Math.max(Math.min(color.b+params[3], 1), 0);
break;
case "shiftcolor_hsl":
a = this.convertRGBtoHSL(color.r, color.g, color.b);
a.h = Math.max(Math.min(a.h+params[1], 1), 0);
a.s = Math.max(Math.min(a.s+params[2], 1), 0);
a.l = Math.max(Math.min(a.l+params[3], 1), 0);
a = this.convertHSLtoRGB(a.h, a.s, a.l);
color.r = a.r;
color.g = a.g;
color.b = a.b;
break;
}
}
return null;
};
// utils
module.convertHSLtoRGB = function(h, s, l) {
result = {r: 0, g: 0, b: 0};
var r, g, b, i, f, p, q, t;
i = Math.floor(h * 6);
f = h * 6 - i;
p = l * (1 - s);
q = l * (1 - f * s);
t = l * (1 - (1 - f) * s);
switch (i % 6) {
case 0: result.r = l; result.g = t; result.b = p; break;
case 1: result.r = q; result.g = l; result.b = p; break;
case 2: result.r = p; result.g = l; result.b = t; break;
case 3: result.r = p; result.g = q; result.b = l; break;
case 4: result.r = t; result.g = p; result.b = l; break;
case 5: result.r = l; result.g = p; result.b = q; break;
}
return result;
};
module.convertRGBtoHSL = function(r, g, b) {
var result = {h: 0, s: 0, l: 0};
var max = Math.max(r, g, b);
var min = Math.min(r, g, b);
result.l = (max + min) / 2;
if(max == min) {
result.h = result.s = 0; // achromatic
} else {
var d = max - min;
result.s = result.l > 0.5 ? d / (2 - max - min) : d / (max + min);
switch(max) {
case r: result.h = (g - b) / d + (g < b ? 6 : 0); break;
case g: result.h = (b - r) / d + 2; break;
case b: result.h = (r - g) / d + 4; break;
}
result.h /= 6;
}
return result;
};
// first parse
module.parseInstructionList();
},
[] // registered channels
);
}; // end of exports