-
Notifications
You must be signed in to change notification settings - Fork 0
/
boxeditor.js
1095 lines (803 loc) · 42 KB
/
boxeditor.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
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
var BoxEditor = (function () {
//min and max are both number[3]
function AABB (min, max) {
this.min = [min[0], min[1], min[2]];
this.max = [max[0], max[1], max[2]];
}
AABB.prototype.computeVolume = function () {
var volume = 1;
for (var i = 0; i < 3; ++i) {
volume *= (this.max[i] - this.min[i]);
}
return volume;
}
AABB.prototype.computeSurfaceArea = function () {
var width = this.max[0] - this.min[0];
var height = this.max[1] - this.min[1];
var depth = this.max[2] - this.min[2];
return 2 * (width * height + width * depth + height * depth);
}
//returns new AABB with the same min and max (but not the same array references)
AABB.prototype.clone = function () {
return new AABB(
[this.min[0], this.min[1], this.min[2]],
[this.max[0], this.max[1], this.max[2]]
);
}
AABB.prototype.randomPoint = function () { //random point in this AABB
var point = [];
for (var i = 0; i < 3; ++i) {
point[i] = this.min[i] + Math.random() * (this.max[i] - this.min[i]);
}
return point;
}
var InteractionMode = {
RESIZING: 0,
TRANSLATING: 1,
DRAWING: 2, //whilst we're drawing a rectangle on a plane
EXTRUDING: 3 //whilst we're extruding that rectangle into a box
};
var STEP = 1.0;
function exclusiveAABBOverlap (a, b) {
return a.min[0] < b.max[0] && a.max[0] > b.min[0] &&
a.min[1] < b.max[1] && a.max[1] > b.min[1] &&
a.min[2] < b.max[2] && a.max[2] > b.min[2];
}
function inclusiveAABBOverlap (a, b) {
return a.min[0] <= b.max[0] && a.max[0] >= b.min[0] &&
a.min[1] <= b.max[1] && a.max[1] >= b.min[1] &&
a.min[2] <= b.max[2] && a.max[2] >= b.min[2];
}
/*
if there is an intersection then this returns:
{
aabb: aabb,
t: distance to intersection,
point: point of intersection,
//axis and side together define the plane of intersection (+x, -x, etc)
axis: 0, 1 or 2 depending on x, y or z,
side: -1 or 1 depending on which side the intersection happened on
}
otherwise it returns null
*/
function rayAABBIntersection (rayOrigin, rayDirection, aabb) {
//we see it as a series of clippings in t of the line in the AABB planes along each axis
//the part we are left with after clipping if successful is the region of the line within the AABB and thus we can extract the intersection
//the part of the line we have clipped so far
var lowT = -Infinity;
var highT = Infinity;
var intersectionAxis = 0;
for (var i = 0; i < 3; ++i) {
var t1 = (aabb.min[i] - rayOrigin[i]) / rayDirection[i];
var t2 = (aabb.max[i] - rayOrigin[i]) / rayDirection[i];
//so between t1 and t2 we are within the aabb planes in this dimension
//ensure t1 < t2 (swap if necessary)
if (t1 > t2) {
var temp = t1;
t1 = t2;
t2 = temp;
}
//t1 and t2 now hold the lower and upper intersection t's respectively
//the part of the line we just clipped for does not overlap the part previously clipped and thus there is no intersection
if (t2 < lowT || t1 > highT) return null;
//further clip the line between the planes in this axis
if (t1 > lowT) {
lowT = t1;
intersectionAxis = i; //if we needed to futher clip in this axis then this is the closest intersection axis
}
if (t2 < highT) highT = t2;
}
if (lowT > highT) return null;
//if we've reached this far then there is an intersection
var intersection = [];
for (var i = 0; i < 3; ++i) {
intersection[i] = rayOrigin[i] + rayDirection[i] * lowT;
}
return {
aabb: aabb,
t: lowT,
axis: intersectionAxis,
side: rayDirection[intersectionAxis] > 0 ? -1 : 1,
point: intersection
};
}
//finds the closest points between the line1 and line2
//returns [closest point on line1, closest point on line2]
function closestPointsOnLines (line1Origin, line1Direction, line2Origin, line2Direction) {
var w0 = Utilities.subtractVectors([], line1Origin, line2Origin);
var a = Utilities.dotVectors(line1Direction, line1Direction);
var b = Utilities.dotVectors(line1Direction, line2Direction);
var c = Utilities.dotVectors(line2Direction, line2Direction);
var d = Utilities.dotVectors(line1Direction, w0);
var e = Utilities.dotVectors(line2Direction, w0);
var t1 = (b * e - c * d) / (a * c - b * b);
var t2 = (a * e - b * d) / (a * c - b * b);
return [
Utilities.addVectors([], line1Origin, Utilities.multiplyVectorByScalar([], line1Direction, t1)),
Utilities.addVectors([], line2Origin, Utilities.multiplyVectorByScalar([], line2Direction, t2))
];
}
//this defines the bounds of our editing space
//the grid starts at (0, 0, 0)
//gridSize is [width, height, depth]
//onChange is a callback that gets called anytime a box gets edited
function BoxEditor (canvas, wgl, projectionMatrix, camera, gridSize, onLoaded, onChange) {
this.canvas = canvas;
this.wgl = wgl;
this.gridWidth = gridSize[0];
this.gridHeight = gridSize[1];
this.gridDepth = gridSize[2];
this.gridDimensions = [this.gridWidth, this.gridHeight, this.gridDepth];
this.projectionMatrix = projectionMatrix;
this.camera = camera;
this.onChange = onChange;
//the cube geometry is a 1x1 cube with the origin at the bottom left corner
this.cubeVertexBuffer = wgl.createBuffer();
wgl.bufferData(this.cubeVertexBuffer, wgl.ARRAY_BUFFER, new Float32Array([
// Front face
0.0, 0.0, 1.0,
1.0, 0.0, 1.0,
1.0, 1.0, 1.0,
0.0, 1.0, 1.0,
// Back face
0.0, 0.0, 0.0,
0.0, 1.0, 0.0,
1.0, 1.0, 0.0,
1.0, 0.0, 0.0,
// Top face
0.0, 1.0, 0.0,
0.0, 1.0, 1.0,
1.0, 1.0, 1.0,
1.0, 1.0, 0.0,
// Bottom face
0.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 0.0, 1.0,
0.0, 0.0, 1.0,
// Right face
1.0, 0.0, 0.0,
1.0, 1.0, 0.0,
1.0, 1.0, 1.0,
1.0, 0.0, 1.0,
// Left face
0.0, 0.0, 0.0,
0.0, 0.0, 1.0,
0.0, 1.0, 1.0,
0.0, 1.0, 0.0
]), wgl.STATIC_DRAW);
this.cubeIndexBuffer = wgl.createBuffer();
wgl.bufferData(this.cubeIndexBuffer, wgl.ELEMENT_ARRAY_BUFFER, new Uint16Array([
0, 1, 2, 0, 2, 3, // front
4, 5, 6, 4, 6, 7, // back
8, 9, 10, 8, 10, 11, // top
12, 13, 14, 12, 14, 15, // bottom
16, 17, 18, 16, 18, 19, // right
20, 21, 22, 20, 22, 23 // left
]), wgl.STATIC_DRAW);
this.cubeWireframeVertexBuffer = wgl.createBuffer();
wgl.bufferData(this.cubeWireframeVertexBuffer, wgl.ARRAY_BUFFER, new Float32Array([
0.0, 0.0, 0.0,
1.0, 0.0, 0.0,
1.0, 1.0, 0.0,
0.0, 1.0, 0.0,
0.0, 0.0, 1.0,
1.0, 0.0, 1.0,
1.0, 1.0, 1.0,
0.0, 1.0, 1.0]), wgl.STATIC_DRAW);
this.cubeWireframeIndexBuffer = wgl.createBuffer();
wgl.bufferData(this.cubeWireframeIndexBuffer, wgl.ELEMENT_ARRAY_BUFFER, new Uint16Array([
0, 1, 1, 2, 2, 3, 3, 0,
4, 5, 5, 6, 6, 7, 7, 4,
0, 4, 1, 5, 2, 6, 3, 7
]), wgl.STATIC_DRAW);
//there's one grid vertex buffer for the planes normal to each axis
this.gridVertexBuffers = [];
for (var axis = 0; axis < 3; ++axis) {
this.gridVertexBuffers[axis] = wgl.createBuffer();
var vertexData = [];
var points; //the points that make up this grid plane
if (axis === 0) {
points = [
[0, 0, 0],
[0, this.gridHeight, 0],
[0, this.gridHeight, this.gridDepth],
[0, 0, this.gridDepth]
];
} else if (axis === 1) {
points = [
[0, 0, 0],
[this.gridWidth, 0, 0],
[this.gridWidth, 0, this.gridDepth],
[0, 0, this.gridDepth]
];
} else if (axis === 2) {
points = [
[0, 0, 0],
[this.gridWidth, 0, 0],
[this.gridWidth, this.gridHeight, 0],
[0, this.gridHeight, 0]
];
}
for (var i = 0; i < 4; ++i) {
vertexData.push(points[i][0]);
vertexData.push(points[i][1]);
vertexData.push(points[i][2]);
vertexData.push(points[(i + 1) % 4][0]);
vertexData.push(points[(i + 1) % 4][1]);
vertexData.push(points[(i + 1) % 4][2]);
}
wgl.bufferData(this.gridVertexBuffers[axis], wgl.ARRAY_BUFFER, new Float32Array(vertexData), wgl.STATIC_DRAW);
}
this.pointVertexBuffer = wgl.createBuffer();
wgl.bufferData(this.pointVertexBuffer, wgl.ARRAY_BUFFER, new Float32Array([-1.0, -1.0, 0.0, -1.0, 1.0, 0.0, 1.0, -1.0, 0.0, 1.0, 1.0, 0.0]), wgl.STATIC_DRAW);
this.quadVertexBuffer = wgl.createBuffer();
wgl.bufferData(this.quadVertexBuffer, wgl.ARRAY_BUFFER, new Float32Array([-1.0, -1.0, -1.0, 1.0, 1.0, -1.0, 1.0, 1.0]), wgl.STATIC_DRAW);
/////////////////////////////////////////////////
// box state
this.boxes = [];
////////////////////////////////////////////////
// interaction stuff
//mouse x and y are in [-1, 1] (clip space)
this.mouseX = 999;
this.mouseY = 999;
this.keyPressed = []; //an array of booleans that maps a key code to whether or not it's pressed
for (var i = 0; i < 256; ++i) {
this.keyPressed[i] = false;
}
/*
interactions:
click on a plane and hold down to begin drawing
when mouse is released we enter extrusion mode for new box
click again to create box
click and drag on side of boxes to resize
click and drag on side of boxes whilst holding shift to move
//while we're not interacting, this is null
//while we are interacting this contains an object
/*
{
mode: the interaction mode,
during resizing or translating or extrusion:
box: box we're currently manipulating,
axis: axis of plane we're manipulating: 0, 1 or 2
side: side of plane we're manipulating: -1 or 1
point: the point at which the interaction started
during translation we also have:
startMax: the starting max along the interaction axis
startMin: the starting min along the interaction axis
during drawing
box: box we're currently drawing
point: the point at which we started drawing
axis: the axis of the plane which we're drawing on
side: the side of the plane which we're drawin on
}
*/
this.interactionState = null;
///////////////////////////////////
// load programs
wgl.createProgramsFromFiles({
backgroundProgram: {
vertexShader: 'shaders/background.vert',
fragmentShader: 'shaders/background.frag'
},
boxProgram: {
vertexShader: 'shaders/box.vert',
fragmentShader: 'shaders/box.frag'
},
boxWireframeProgram: {
vertexShader: 'shaders/boxwireframe.vert',
fragmentShader: 'shaders/boxwireframe.frag'
},
gridProgram: {
vertexShader: 'shaders/grid.vert',
fragmentShader: 'shaders/grid.frag'
},
pointProgram: {
vertexShader: 'shaders/point.vert',
fragmentShader: 'shaders/point.frag'
}
}, (function (programs) {
for (var programName in programs) {
this[programName] = programs[programName];
}
onLoaded();
}).bind(this));
}
function quantize (x, step) {
return Math.round(x / step) * step;
}
function quantizeVector (v, step) {
for (var i = 0; i < v.length; ++i) {
v[i] = quantize(v[i], step);
}
return v;
}
BoxEditor.prototype.onKeyDown = function (event) {
this.keyPressed[event.keyCode] = true;
}
BoxEditor.prototype.onKeyUp = function (event) {
this.keyPressed[event.keyCode] = false;
}
BoxEditor.prototype.onMouseMove = function (event) {
event.preventDefault();
var position = Utilities.getMousePosition(event, this.canvas);
var normalizedX = position.x / this.canvas.width;
var normalizedY = position.y / this.canvas.height;
this.mouseX = normalizedX * 2.0 - 1.0;
this.mouseY = (1.0 - normalizedY) * 2.0 - 1.0;
if (this.interactionState !== null) {
this.onChange();
if (this.interactionState.mode === InteractionMode.RESIZING || this.interactionState.mode === InteractionMode.EXTRUDING) {
var mouseRay = this.getMouseRay();
//so when we are dragging to make a box bigger or smaller, what we do is we extend a line out from the intersection point normal to the plane
var dragLineOrigin = this.interactionState.point;
var dragLineDirection = [0, 0, 0];
dragLineDirection[this.interactionState.axis] = 1.0;
//then we find the closest point between the mouse ray and this line and use that to determine how far we've 'dragged'
var closestPoints = closestPointsOnLines(dragLineOrigin, dragLineDirection, mouseRay.origin, mouseRay.direction);
var newCoordinate = closestPoints[0][this.interactionState.axis]; //the new coordinate for this box plane
newCoordinate = quantize(newCoordinate, STEP);
var box = this.interactionState.box,
side = this.interactionState.side,
axis = this.interactionState.axis;
//resize the box, clamping it to itself and the overall grid
if (side === -1) {
box.min[axis] = Math.max(Math.min(newCoordinate, box.max[axis]), 0);
} else if (side === 1) {
box.max[axis] = Math.min(Math.max(newCoordinate, box.min[axis]), this.gridDimensions[axis]);
}
//collision detection
for (var i = 0; i < this.boxes.length; ++i) {
var otherBox = this.boxes[i];
if (box !== otherBox) { //don't collide with self
if (exclusiveAABBOverlap(box, otherBox)) {
//resolve collision
if (side === -1) {
box.min[axis] = otherBox.max[axis];
} else if (side === 1) {
box.max[axis] = otherBox.min[axis];
}
}
}
}
} else if (this.interactionState.mode === InteractionMode.TRANSLATING) {
var mouseRay = this.getMouseRay();
//so when we are translating a box, what we do is we extend a line out from the intersection point normal to the plane
var dragLineOrigin = this.interactionState.point;
var dragLineDirection = [0, 0, 0];
dragLineDirection[this.interactionState.axis] = 1.0;
//then we find the closest point between the mouse ray and this line and use that to determine how far we've 'dragged'
var closestPoints = closestPointsOnLines(dragLineOrigin, dragLineDirection, mouseRay.origin, mouseRay.direction);
var newCoordinate = closestPoints[0][this.interactionState.axis]; //the new coordinate for this box plane
newCoordinate = quantize(newCoordinate, STEP);
var box = this.interactionState.box,
side = this.interactionState.side,
axis = this.interactionState.axis;
var length = this.interactionState.startMax - this.interactionState.startMin; //the length of the box along the translation axis
if (side === -1) {
box.min[axis] = newCoordinate;
box.max[axis] = newCoordinate + length;
} else if (side === 1) {
box.max[axis] = newCoordinate;
box.min[axis] = newCoordinate - length;
}
//clamp to boundaries
if (box.min[axis] < 0) {
box.min[axis] = 0;
box.max[axis] = length;
}
if (box.max[axis] > this.gridDimensions[axis]) {
box.max[axis] = this.gridDimensions[axis];
box.min[axis] = this.gridDimensions[axis] - length;
}
var translationDirection = 0; //is either -1 or 1 depending on which way we're pushing our box
//how we resolve collisions depends on our translation direction
if (side === -1) {
translationDirection = newCoordinate < this.interactionState.startMin ? -1 : 1;
} else if (side === 1) {
translationDirection = newCoordinate < this.interactionState.startMax ? -1 : 1;
}
var sweptBox = box.clone(); //we sweep out translating AABB for collision detection to prevent ghosting through boxes
//reset swept box to original box location before translation
sweptBox.min[axis] = this.interactionState.startMin;
sweptBox.max[axis] = this.interactionState.startMax;
//sweep out the correct plane to where it has been translated to
if (translationDirection === 1) {
sweptBox.max[axis] = box.max[axis];
} else if (translationDirection === -1) {
sweptBox.min[axis] = box.min[axis];
}
//collision detection
for (var i = 0; i < this.boxes.length; ++i) {
var otherBox = this.boxes[i];
if (box !== otherBox) { //don't collide with self
if (exclusiveAABBOverlap(sweptBox, otherBox)) {
//resolve collision
if (translationDirection === -1) {
box.min[axis] = otherBox.max[axis];
box.max[axis] = otherBox.max[axis] + length;
} else if (translationDirection === 1) {
box.max[axis] = otherBox.min[axis];
box.min[axis] = otherBox.min[axis] - length;
}
}
}
}
} else if (this.interactionState.mode === InteractionMode.DRAWING) {
var mouseRay = this.getMouseRay();
//get the mouse ray intersection with the drawing plane
var axis = this.interactionState.axis,
side = this.interactionState.side,
startPoint = this.interactionState.point;
var planeCoordinate = side === -1 ? 0 : this.gridDimensions[axis];
var t = (planeCoordinate - mouseRay.origin[axis]) / mouseRay.direction[axis];
if (t > 0) { //if the mouse ray misses the drawing plane then the box just stays the same size as it was before
var intersection = Utilities.addVectors([], mouseRay.origin, Utilities.multiplyVectorByScalar([], mouseRay.direction, t));
quantizeVector(intersection, STEP);
for (var i = 0; i < 3; ++i) {
intersection[i] = Utilities.clamp(intersection[i], 0, this.gridDimensions[i]);
intersection[i] = Utilities.clamp(intersection[i], 0, this.gridDimensions[i]);
}
var min = [Math.min(startPoint[0], intersection[0]), Math.min(startPoint[1], intersection[1]), Math.min(startPoint[2], intersection[2])];
var max = [Math.max(startPoint[0], intersection[0]), Math.max(startPoint[1], intersection[1]), Math.max(startPoint[2], intersection[2])];
var box = this.interactionState.box;
var sweptBox = new AABB(min, max); //we sweep the box a bit into the grid to make sure it collides along the plane axis
if (this.interactionState.side === -1) {
sweptBox.max[this.interactionState.axis] = STEP * 0.1;
} else {
sweptBox.min[this.interactionState.axis] = this.gridDimensions[this.interactionState.axis] - STEP * 0.1;
}
//collision detection
for (var i = 0; i < this.boxes.length; ++i) {
var otherBox = this.boxes[i];
if (box !== otherBox) { //don't collide with self
if (exclusiveAABBOverlap(sweptBox, otherBox)) {
//we resolve along the axis with the smaller overlap and where the start point doesn't already overlap the other box in that axis
var smallestOverlap = 99999999;
var smallestOverlapAxis = -1;
for (var axis = 0; axis < 3; ++axis) {
if (axis !== this.interactionState.axis) { //only resolve collisions in the drawing plane
var overlap = Math.min(max[axis], otherBox.max[axis]) - Math.max(min[axis], otherBox.min[axis]);
if (overlap > 0 && overlap < smallestOverlap && (startPoint[axis] < otherBox.min[axis] || startPoint[axis] > otherBox.max[axis])) {
smallestOverlap = overlap;
smallestOverlapAxis = axis;
}
}
}
if (intersection[smallestOverlapAxis] > startPoint[smallestOverlapAxis]) { //if we're resizing in the positive direction
max[smallestOverlapAxis] = otherBox.min[smallestOverlapAxis];
} else { //if we're resizing in the negative direction
min[smallestOverlapAxis] = otherBox.max[smallestOverlapAxis];
}
}
}
}
this.interactionState.box.min = min;
this.interactionState.box.max = max;
}
}
}
this.camera.onMouseMove(event);
}
//returns the closest box intersection data (same as rayAABBIntersection) for the given ray
//if there is no intersection it returns null
BoxEditor.prototype.getBoxIntersection = function (rayOrigin, rayDirection) {
//find the closest box that this collides with
var bestIntersectionSoFar = {
aabb: null,
t: Infinity
}
for (var i = 0; i < this.boxes.length; ++i) {
var box = this.boxes[i];
var intersection = rayAABBIntersection(rayOrigin, rayDirection, box);
if (intersection !== null) { //if there is an intersection
if (intersection.t < bestIntersectionSoFar.t) { //if this is closer than the best we've seen so far
bestIntersectionSoFar = intersection;
}
}
}
if (bestIntersectionSoFar.aabb === null) { //if we didn't intersect any boxes
return null;
} else {
return bestIntersectionSoFar;
}
}
//tests for intersection with one of the bounding planes
/*
if there is an intersection returns
{axis, side, point}
otherwise, returns null
*/
BoxEditor.prototype.getBoundingPlaneIntersection = function (rayOrigin, rayDirection) {
//we try to intersect with the two planes on each axis in turn (as long as they are facing towards the camera)
//we assume we could only ever intersect with one of the planes so we break out as soon as we've found something
for (var axis = 0; axis < 3; ++axis) {
//now let's try intersecting with each side in turn
for (var side = -1; side <= 1; side += 2) { //goes between -1 and 1 (hackish!
//first let's make sure the plane is front facing to the ray
var frontFacing = side === -1 ? rayDirection[axis] < 0 : rayDirection[axis] > 0;
if (frontFacing) {
var planeCoordinate = side === -1 ? 0 : this.gridDimensions[axis]; //the coordinate of the plane along this axis
var t = (planeCoordinate - rayOrigin[axis]) / rayDirection[axis];
if (t > 0) {
var intersection = Utilities.addVectors([], rayOrigin, Utilities.multiplyVectorByScalar([], rayDirection, t));
//if we're still within the bounds of the grid
if (intersection[0] >= 0.0 && intersection[0] <= this.gridDimensions[0] &&
intersection[1] >= 0.0 && intersection[1] <= this.gridDimensions[1] &&
intersection[2] >= 0.0 && intersection[2] <= this.gridDimensions[2]) {
return {
axis: axis,
side: side,
point: intersection
}
}
}
}
}
}
return null; //no intersection found
}
BoxEditor.prototype.onMouseDown = function (event) {
event.preventDefault();
this.onMouseMove(event);
if (!this.keyPressed[32]) { //if space isn't held down
//we've finished extruding a box
if (this.interactionState !== null && this.interactionState.mode === InteractionMode.EXTRUDING) {
//delete zero volume boxes
if (this.interactionState.box.computeVolume() === 0) {
this.boxes.splice(this.boxes.indexOf(this.interactionState.box), 1);
}
this.interactionState = null;
this.onChange();
return;
} else {
var mouseRay = this.getMouseRay();
//find the closest box that this collides with
var boxIntersection = this.getBoxIntersection(mouseRay.origin, mouseRay.direction);
//if we've intersected at least one box then let's start manipulating that box
if (boxIntersection !== null) {
var intersection = boxIntersection;
if (this.keyPressed[16]) { //if we're holding shift we start to translate
this.interactionState = {
mode: InteractionMode.TRANSLATING,
box: intersection.aabb,
axis: intersection.axis,
side: intersection.side,
point: intersection.point,
startMax: intersection.aabb.max[intersection.axis],
startMin: intersection.aabb.min[intersection.axis]
};
} else { //otherwise we start resizing
this.interactionState = {
mode: InteractionMode.RESIZING,
box: intersection.aabb,
axis: intersection.axis,
side: intersection.side,
point: intersection.point
};
}
}
//if we've not intersected any box then let's see if we should start the box creation process
if (boxIntersection === null) {
var mouseRay = this.getMouseRay();
var planeIntersection = this.getBoundingPlaneIntersection(mouseRay.origin, mouseRay.direction);
if (planeIntersection !== null) { //if we've hit one of the planes
//go into drawing mode
var point = planeIntersection.point;
point[0] = quantize(point[0], STEP);
point[1] = quantize(point[1], STEP);
point[2] = quantize(point[2], STEP);
var newBox = new AABB(point, point);
this.boxes.push(newBox);
this.interactionState = {
mode: InteractionMode.DRAWING,
box: newBox,
axis: planeIntersection.axis,
side: planeIntersection.side,
point: planeIntersection.point
};
}
this.onChange();
}
}
}
if (this.interactionState === null) {
this.camera.onMouseDown(event);
}
}
BoxEditor.prototype.onMouseUp = function (event) {
event.preventDefault();
if (this.interactionState !== null) {
if (this.interactionState.mode === InteractionMode.RESIZING) { //the end of a resize
//if we've resized to zero volume then we delete the box
if (this.interactionState.box.computeVolume() === 0) {
this.boxes.splice(this.boxes.indexOf(this.interactionState.box), 1);
}
this.interactionState = null;
} else if (this.interactionState.mode === InteractionMode.TRANSLATING) { //the end of a translate
this.interactionState = null;
} else if (this.interactionState.mode === InteractionMode.DRAWING) { //the end of a draw
//TODO: DRY this
if (this.interactionState.box.computeSurfaceArea() > 0) { //make sure we have something to extrude
var mouseRay = this.getMouseRay();
var axis = this.interactionState.axis,
side = this.interactionState.side,
startPoint = this.interactionState.point;
var planeCoordinate = side === -1 ? 0 : this.gridDimensions[axis];
var t = (planeCoordinate - mouseRay.origin[axis]) / mouseRay.direction[axis];
var intersection = Utilities.addVectors([], mouseRay.origin, Utilities.multiplyVectorByScalar([], mouseRay.direction, t));
quantizeVector(intersection, STEP);
//clamp extrusion point to grid and to box
for (var i = 0; i < 3; ++i) {
intersection[i] = Utilities.clamp(intersection[i], 0, this.gridDimensions[i]);
intersection[i] = Utilities.clamp(intersection[i], this.interactionState.box.min[i], this.interactionState.box.max[i]);
}
//go into extrusion mode
this.interactionState = {
mode: InteractionMode.EXTRUDING,
box: this.interactionState.box,
axis: this.interactionState.axis,
side: this.interactionState.side * -1,
point: intersection
};
} else { //otherwise delete the box we were editing and go straight back into regular mode
this.boxes.splice(this.boxes.indexOf(this.interactionState.box), 1);
this.interactionState = null;
}
}
this.onChange();
}
if (this.interactionState === null) {
this.camera.onMouseUp(event);
}
}
//returns an object
/*
{
origin: [x, y, z],
direction: [x, y, z] //normalized
}
*/
BoxEditor.prototype.getMouseRay = function () {
var fov = 2.0 * Math.atan(1.0 / this.projectionMatrix[5]);
var viewSpaceMouseRay = [
this.mouseX * Math.tan(fov / 2.0) * (this.canvas.width / this.canvas.height),
this.mouseY * Math.tan(fov / 2.0),
-1.0];
var inverseViewMatrix = Utilities.invertMatrix([], this.camera.getViewMatrix());
var mouseRay = Utilities.transformDirectionByMatrix([], viewSpaceMouseRay, inverseViewMatrix);
Utilities.normalizeVector(mouseRay, mouseRay);
var rayOrigin = this.camera.getPosition();
return {
origin: rayOrigin,
direction: mouseRay
};
}
BoxEditor.prototype.draw = function () {
var wgl = this.wgl;
wgl.clear(
wgl.createClearState().bindFramebuffer(null).clearColor(0.9, 0.9, 0.9, 1.0),
wgl.COLOR_BUFFER_BIT | wgl.DEPTH_BUFFER_BIT);
/////////////////////////////////////////////
//draw background
var backgroundDrawState = wgl.createDrawState()
.bindFramebuffer(null)
.viewport(0, 0, this.canvas.width, this.canvas.height)
.useProgram(this.backgroundProgram)
.vertexAttribPointer(this.quadVertexBuffer, this.backgroundProgram.getAttribLocation('a_position'), 2, wgl.FLOAT, wgl.FALSE, 0, 0);
wgl.drawArrays(backgroundDrawState, wgl.TRIANGLE_STRIP, 0, 4);
/////////////////////////////////////////////
//draw grid
for (var axis = 0; axis < 3; ++axis) {
for (var side = 0; side <= 1; ++side) {
var cameraPosition = this.camera.getPosition();
var planePosition = [this.gridWidth / 2, this.gridHeight / 2, this.gridDepth / 2];
planePosition[axis] = side === 0 ? 0 : this.gridDimensions[axis];
var cameraDirection = Utilities.subtractVectors([], planePosition, cameraPosition);
var gridDrawState = wgl.createDrawState()
.bindFramebuffer(null)
.viewport(0, 0, this.canvas.width, this.canvas.height)
.useProgram(this.gridProgram)
.vertexAttribPointer(this.gridVertexBuffers[axis], this.gridProgram.getAttribLocation('a_vertexPosition'), 3, wgl.FLOAT, wgl.FALSE, 0, 0)
.uniformMatrix4fv('u_projectionMatrix', false, this.projectionMatrix)
.uniformMatrix4fv('u_viewMatrix', false, this.camera.getViewMatrix());
var translation = [0, 0, 0];
translation[axis] = side * this.gridDimensions[axis];
gridDrawState.uniform3f('u_translation', translation[0], translation[1], translation[2]);
if (side === 0 && cameraDirection[axis] <= 0 || side === 1 && cameraDirection[axis] >= 0) {
wgl.drawArrays(gridDrawState, wgl.LINES, 0, 8);
}
}
}
///////////////////////////////////////////////
//draw boxes and point
var boxDrawState = wgl.createDrawState()
.bindFramebuffer(null)
.viewport(0, 0, this.canvas.width, this.canvas.height)
.enable(wgl.DEPTH_TEST)
.enable(wgl.CULL_FACE)
.useProgram(this.boxProgram)
.vertexAttribPointer(this.cubeVertexBuffer, this.boxProgram.getAttribLocation('a_cubeVertexPosition'), 3, wgl.FLOAT, wgl.FALSE, 0, 0)
.bindIndexBuffer(this.cubeIndexBuffer)
.uniformMatrix4fv('u_projectionMatrix', false, this.projectionMatrix)
.uniformMatrix4fv('u_viewMatrix', false, this.camera.getViewMatrix())
.enable(wgl.POLYGON_OFFSET_FILL)
.polygonOffset(1, 1);
var boxToHighlight = null,
sideToHighlight = null,
highlightColor = null;
if (this.interactionState !== null) {
if (this.interactionState.mode === InteractionMode.RESIZING || this.interactionState.mode === InteractionMode.EXTRUDING) {
boxToHighlight = this.interactionState.box;
sideToHighlight = [1.5, 1.5, 1.5];
sideToHighlight[this.interactionState.axis] = this.interactionState.side;
highlightColor = [0.75, 0.75, 0.75];
}
} else if (!this.keyPressed[32] && !this.camera.isMouseDown()) { //if we're not interacting with anything and we're not in camera mode
var mouseRay = this.getMouseRay();
var boxIntersection = this.getBoxIntersection(mouseRay.origin, mouseRay.direction);
//if we're over a box, let's highlight the side we're hovering over
if (boxIntersection !== null) {
boxToHighlight = boxIntersection.aabb;