-
Notifications
You must be signed in to change notification settings - Fork 204
Expand file tree
/
Copy pathbrush.js
More file actions
526 lines (457 loc) · 16.2 KB
/
brush.js
File metadata and controls
526 lines (457 loc) · 16.2 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
/* globals AFRAME THREE BinaryManager */
var VERSION = 1;
AFRAME.BRUSHES = {};
APAINTER_STATS = {
brushes: {}
};
AFRAME.registerBrush = function (name, definition, options) {
var proto = {};
// Format definition object to prototype object.
Object.keys(definition).forEach(function (key) {
proto[key] = {
value: definition[key],
writable: true
};
});
if (AFRAME.BRUSHES[name]) {
throw new Error('The brush `' + name + '` has been already registered. ' +
'Check that you are not loading two versions of the same brush ' +
'or two different brushes of the same name.');
}
var BrushInterface = function () {};
var defaultOptions = {
spacing: 0,
maxPoints: 0
};
BrushInterface.prototype = {
options: Object.assign(defaultOptions, options),
reset: function () {},
tick: function (timeoffset, delta) {},
undo: function () {},
remove: function () {},
addPoint: function (position, orientation, pointerPosition, pressure, timestamp) {},
getJSON: function (system) {
var points = [];
for (var i = 0; i < this.data.points.length; i++) {
var point = this.data.points[i];
points.push({
'orientation': Utils.arrayNumbersToFixed(point.orientation.toArray()),
'position': Utils.arrayNumbersToFixed(point.position.toArray()),
'pressure': Utils.numberToFixed(point.pressure),
'timestamp': point.timestamp
});
}
return {
brush: {
index: system.getUsedBrushes().indexOf(this.brushName),
color: Utils.arrayNumbersToFixed(this.data.color.toArray()),
size: Utils.numberToFixed(this.data.size),
},
points: points
};
},
getBinary: function (system) {
// Color = 3*4 = 12
// NumPoints = 4
// Brush index = 1
// ----------- = 21
// [Point] = vector3 + quat + pressure + timestamp = (3+4+1+1)*4 = 36
var bufferSize = 21 + (36 * this.data.points.length);
var binaryManager = new BinaryManager(new ArrayBuffer(bufferSize));
binaryManager.writeUint8(system.getUsedBrushes().indexOf(this.brushName)); // brush index
binaryManager.writeColor(this.data.color); // color
binaryManager.writeFloat32(this.data.size); // brush size
// Number of points
binaryManager.writeUint32(this.data.points.length);
// Points
for (var i = 0; i < this.data.points.length; i++) {
var point = this.data.points[i];
binaryManager.writeFloat32Array(point.position.toArray());
binaryManager.writeFloat32Array(point.orientation.toArray());
binaryManager.writeFloat32(point.pressure);
binaryManager.writeUint32(point.timestamp);
}
return binaryManager.getDataView();
}
};
function wrapInit (initMethod) {
return function init (color, brushSize, owner, timestamp) {
this.object3D = new THREE.Object3D();
this.data = {
points: [],
size: brushSize,
prevPosition: null,
prevPointerPosition: null,
numPoints: 0,
color: color.clone(),
timestamp: timestamp,
owner: owner
};
initMethod.call(this, color, brushSize);
};
}
function wrapAddPoint (addPointMethod) {
return function addPoint (position, orientation, pointerPosition, pressure, timestamp) {
if ((this.data.prevPosition && this.data.prevPosition.distanceTo(position) <= this.options.spacing) ||
this.options.maxPoints !== 0 && this.data.numPoints >= this.options.maxPoints) {
return;
}
if (addPointMethod.call(this, position, orientation, pointerPosition, pressure, timestamp)) {
this.data.numPoints++;
this.data.points.push({
'position': position.clone(),
'orientation': orientation.clone(),
'pressure': pressure,
'timestamp': timestamp
});
this.data.prevPosition = position.clone();
this.data.prevPointerPosition = pointerPosition.clone();
}
};
}
var NewBrush = function () {};
NewBrush.prototype = Object.create(BrushInterface.prototype, proto);
NewBrush.prototype.brushName = name;
NewBrush.prototype.constructor = NewBrush;
NewBrush.prototype.init = wrapInit(NewBrush.prototype.init);
NewBrush.prototype.addPoint = wrapAddPoint(NewBrush.prototype.addPoint);
AFRAME.BRUSHES[name] = NewBrush;
// console.log('New brush registered `' + name + '`');
NewBrush.used = false; // Used to know which brushes have been used on the drawing
return NewBrush;
};
AFRAME.registerSystem('brush', {
schema: {},
brushes: {},
strokes: [],
getUsedBrushes: function () {
return Object.keys(AFRAME.BRUSHES)
.filter(function (name) { return AFRAME.BRUSHES[name].used; });
},
getBrushByName: function (name) {
return AFRAME.BRUSHES[name];
},
undo: function () {
var stroke;
for (var i = this.strokes.length - 1; i >= 0; i--) {
if (this.strokes[i].data.owner !== 'local') continue;
stroke = this.strokes.splice(i, 1)[0];
break;
}
if (stroke) {
stroke.undo();
var drawing = document.querySelector('.a-drawing');
drawing.emit('stroke-removed', {stroke: stroke});
}
},
removeById: function (order) {
order = 1;
var targetStroke = this.strokes[order];
console.log(targetStroke, this.strokes);
if (targetStroke) {
for (var i = this.strokes.length - 1; i > order; i--) {
stroke = this.strokes[i];
if (targetStroke.sharedBuffer === stroke.sharedBuffer) {
// Update idx and prevIdx
console.log('>>>', stroke.prevIdx, '->', stroke.idx, 'target', targetStroke.prevIdx, '->', targetStroke.idx);
for (key in targetStroke.idx) {
var diff = (targetStroke.idx[key] - targetStroke.prevIdx[key]);
stroke.idx[key] -= diff;
stroke.prevIdx[key] -= diff;
}
console.log('<<<', stroke.idx, stroke.prevIdx);
}
}
this.strokes.splice(order, 1)[0].remove();
}
},
clear: function () {
// Remove all the stroke entities
for (var i = this.strokes.length - 1; i >= 0; i--) {
if(this.strokes[i].data.owner !== 'local') continue;
var stroke = this.strokes[i];
stroke.undo();
var drawing = document.querySelector('.a-drawing');
drawing.emit('stroke-removed', { stroke: stroke });
}
// Reset the used brushes
Object.keys(AFRAME.BRUSHES).forEach(function (name) {
AFRAME.BRUSHES[name].used = false;
});
this.strokes = [];
},
init: function () {
this.version = VERSION;
this.clear();
this.controllerName = null;
var self = this;
this.sceneEl.addEventListener('controllerconnected', function (evt) {
self.controllerName = evt.detail.name;
});
},
tick: function (time, delta) {
if (!this.strokes.length) { return; }
for (var i = 0; i < this.strokes.length; i++) {
this.strokes[i].tick(time, delta);
}
},
generateTestLines: function () {
function randNeg() { return 2 * Math.random() - 1; }
var z = -2;
var size = 0.5;
var width = 3;
var pressure = 1;
var numPoints = 4;
var steps = width / numPoints;
var numStrokes = 1;
var brushesNames = Object.keys(AFRAME.BRUSHES);
brushesNames2 = [
'leaf1',
'fur2',
'star',
'squared-textured',
'flat',
'squared-textured',
'lines5'
];
var x = -(size + 0.1) * brushesNames.length / 2;
x= 0;
var y = 0;
brushesNames.forEach(function (brushName) {
var color = new THREE.Color(Math.random(), Math.random(), Math.random());
var stroke = this.addNewStroke(brushName, color, size);
var entity = document.querySelector('#left-hand');
entity.emit('stroke-started', { entity: entity, stroke: stroke });
var position = new THREE.Vector3(x, y, z);
var aux = new THREE.Vector3();
for (var i = 0; i < numPoints; i++) {
var orientation = new THREE.Quaternion();
aux.set(0, steps, 0.1);
var euler = new THREE.Euler(0, Math.PI, 0);
orientation.setFromEuler(euler);
position = position.add(aux);
var timestamp = 0;
var pointerPosition = this.getPointerPosition(position, orientation);
stroke.addPoint(position, orientation, pointerPosition, pressure, timestamp);
}
x+= size + 0.1;
});
},
generateRandomStrokes: function (numStrokes) {
function randNeg () { return 2 * Math.random() - 1; }
var entity = document.querySelector('#left-hand');
var brushesNames = Object.keys(AFRAME.BRUSHES);
for (var l = 0; l < numStrokes; l++) {
//var brushName = brushesNames[parseInt(Math.random() * 30)];
var brushName = brushesNames[parseInt(Math.random() * 13)];
var color = new THREE.Color(Math.random(), Math.random(), Math.random());
var size = Math.random() * 0.3;
var numPoints = parseInt(Math.random() * 500);
var stroke = this.addNewStroke(brushName, color, size);
entity.emit('stroke-started', {entity: entity, stroke: stroke});
var position = new THREE.Vector3(randNeg(), randNeg(), randNeg());
var aux = new THREE.Vector3();
var orientation = new THREE.Quaternion();
var pressure = 0.2;
for (var i = 0; i < numPoints; i++) {
aux.set(randNeg(), randNeg(), randNeg());
aux.multiplyScalar(randNeg() / 20);
orientation.setFromUnitVectors(position.clone().normalize(), aux.clone().normalize());
position = position.add(aux);
if (position.y < 0) {
position.y = -position.y;
}
var timestamp = 0;
pressure += 1 - 2 * Math.random();
if (pressure < 0) pressure = 0.2;
if (pressure > 1) pressure = 1;
var pointerPosition = this.getPointerPosition(position, orientation);
stroke.addPoint(position, orientation, pointerPosition, pressure, timestamp);
}
}
},
addNewStroke: function (brushName, color, size, owner, timestamp) {
if (!APAINTER_STATS.brushes[brushName]) {
APAINTER_STATS.brushes[brushName] = 0;
}
APAINTER_STATS.brushes[brushName]++;
owner = owner || 'local';
timestamp = timestamp || Date.now();
var Brush = this.getBrushByName(brushName);
if (!Brush) {
var newBrushName = Object.keys(AFRAME.BRUSHES)[0];
Brush = AFRAME.BRUSHES[newBrushName];
console.warn('Invalid brush name: `' + brushName + '` using `' + newBrushName + '`');
}
Brush.used = true;
var stroke = new Brush();
stroke.brush = Brush;
stroke.init(color, size, owner, timestamp);
this.strokes.push(stroke);
var drawing = document.querySelector('.a-drawing');
if (!drawing) {
drawing = document.createElement('a-entity');
drawing.className = "a-drawing";
document.querySelector('a-scene').appendChild(drawing);
}
//var entity = document.createElement('a-entity');
//entity.className = "a-stroke";
//drawing.appendChild(entity);
// drawing.object3D.add(stroke.object3D);
//entity.setObject3D('mesh', stroke.object3D);
//stroke.entity = entity;
return stroke;
},
getJSON: function () {
// Strokes
var json = {
version: VERSION,
strokes: [],
author: '',
brushes: this.getUsedBrushes()
};
for (i = 0; i < this.strokes.length; i++) {
json.strokes.push(this.strokes[i].getJSON(this));
}
return json;
},
getBinary: function () {
var dataViews = [];
var MAGIC = 'apainter';
// Used brushes
var usedBrushes = this.getUsedBrushes();
// MAGIC(8) + version (2) + usedBrushesNum(2) + usedBrushesStrings(*)
var bufferSize = MAGIC.length + usedBrushes.join(' ').length + 9;
var binaryManager = new BinaryManager(new ArrayBuffer(bufferSize));
// Header magic and version
binaryManager.writeString(MAGIC);
binaryManager.writeUint16(VERSION);
binaryManager.writeUint8(usedBrushes.length);
for (var i = 0; i < usedBrushes.length; i++) {
binaryManager.writeString(usedBrushes[i]);
}
// Number of strokes
binaryManager.writeUint32(this.strokes.length);
dataViews.push(binaryManager.getDataView());
// Strokes
for (i = 0; i < this.strokes.length; i++) {
dataViews.push(this.strokes[i].getBinary(this));
}
return dataViews;
},
getPointerPosition: (function () {
var pointerPosition = new THREE.Vector3();
var controllerOffset = {
'vive-controls': {
vec: {
left: new THREE.Vector3(0, 0.7, 1),
right: new THREE.Vector3(0, 0.7, 1),
},
mult: -0.03
},
'oculus-touch-controls': {
vec: {
left: new THREE.Vector3(-2, 0, 2.8),
right: new THREE.Vector3(2, 0, 2.8)
},
mult: -0.025
},
'windows-motion-controls': {
vec: {
left: new THREE.Vector3(0, 0, 1),
right: new THREE.Vector3(0, 0, 1),
},
mult: -.12
}
};
return function getPointerPosition (position, orientation, hand) {
if (!this.controllerName) {
return position;
}
var offsets = controllerOffset[this.controllerName];
var pointer = offsets.vec[hand]
.clone()
.applyQuaternion(orientation)
.normalize()
.multiplyScalar(offsets.mult);
pointerPosition.copy(position).add(pointer);
return pointerPosition;
};
})(),
loadJSON: function (data) {
if (data.version !== VERSION) {
console.error('Invalid version: ', data.version, '(Expected: ' + VERSION + ')');
}
console.time('JSON Loading');
var usedBrushes = [];
for (var i = 0; i < data.strokes.length; i++) {
var strokeData = data.strokes[i];
var brush = strokeData.brush;
var stroke = this.addNewStroke(
data.brushes[brush.index],
new THREE.Color().fromArray(brush.color),
brush.size
);
for (var j = 0; j < strokeData.points.length; j++) {
var point = strokeData.points[j];
var position = new THREE.Vector3().fromArray(point.position);
var orientation = new THREE.Quaternion().fromArray(point.orientation);
var pressure = point.pressure;
var timestamp = point.timestamp;
var pointerPosition = this.getPointerPosition(position, orientation);
stroke.addPoint(position, orientation, pointerPosition, pressure, timestamp);
}
}
console.timeEnd('JSON Loading');
},
loadBinary: function (buffer) {
var binaryManager = new BinaryManager(buffer);
var magic = binaryManager.readString();
if (magic !== 'apainter') {
console.error('Invalid `magic` header');
return;
}
var version = binaryManager.readUint16();
if (version !== VERSION) {
console.error('Invalid version: ', version, '(Expected: ' + VERSION + ')');
}
console.time('Binary Loading');
var numUsedBrushes = binaryManager.readUint8();
var usedBrushes = [];
for (var b = 0; b < numUsedBrushes; b++) {
usedBrushes.push(binaryManager.readString());
}
var numStrokes = binaryManager.readUint32();
for (var l = 0; l < numStrokes; l++) {
var brushIndex = binaryManager.readUint8();
var color = binaryManager.readColor();
var size = binaryManager.readFloat();
var numPoints = binaryManager.readUint32();
var stroke = this.addNewStroke(usedBrushes[brushIndex], color, size);
for (var i = 0; i < numPoints; i++) {
var position = binaryManager.readVector3();
var orientation = binaryManager.readQuaternion();
var pressure = binaryManager.readFloat();
var timestamp = binaryManager.readUint32();
var pointerPosition = this.getPointerPosition(position, orientation);
stroke.addPoint(position, orientation, pointerPosition, pressure, timestamp);
}
}
console.timeEnd('Binary Loading');
},
loadFromUrl: function (url, binary) {
var loader = new THREE.FileLoader(this.manager);
loader.crossOrigin = 'anonymous';
if (binary === true) {
loader.setResponseType('arraybuffer');
}
var self = this;
loader.load(url, function (buffer) {
if (binary === true) {
self.loadBinary(buffer);
} else {
self.loadJSON(JSON.parse(buffer));
}
});
}
});