-
Notifications
You must be signed in to change notification settings - Fork 0
/
Shelf.js
executable file
·279 lines (239 loc) · 9.18 KB
/
Shelf.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
/*
Shelf constructor
Purpose: creates a new Shelf object with a frame containing a graphical model - a single box-shaped shelf
Parameters:
width: width of the shelf (x-direction)
height: height of the shelf (y-direction)
depth: depth of the shelf (z-direction)
name: name of the shelf (to distinguish separate Shelf objects)
*/
function Shelf (width, height, depth, name) {
//from parameters
this.width = width;
this.height = height;
this.depth = depth;
this.name = name;
//predefined color
this.color = 0x8b3626;
//arrays to keep track of books on top of the shelf and on the floor, respectively
this.books = [];
this.floor = [];
//whether or not the shelf is empty (has no books on it)
this.shelfIsEmpty = false;
//frame (graphical model)
this.frame = new THREE.Object3D();
this.addInnerFrame();
}
/*
addInnerFrame()
Purpose: adds the inner frame to the graphics frame
origin: center of shelf
extends along x-axis by 0.5 * this.width in each direction,
y-axis by 0.5 * this.height in each direction,
z-axis by 0.5 * this.depth in each direction
*/
Shelf.prototype.addInnerFrame = function () {
this.innerFrame = new THREE.Object3D();
var flat = this.makeFlatShelf();
this.innerFrame.add(flat);
this.frame.add(this.innerFrame);
}
/*
makeFlatShelf()
Purpose: returns a single flat box-shaped shelf
origin: center of shelf
extends along x-axis by 0.5 * this.width in each direction,
y-axis by 0.5 * this.height in each direction,
z-axis by 0.5 * this.depth in each direction
*/
Shelf.prototype.makeFlatShelf = function () {
this.flatWidth = 1.1 * this.width;
this.flatThickness = 0.2 * this.height;
var flatFrame = new THREE.Object3D();
flatFrame.name = this.name + 'FlatFrame';
var flatGeom = new THREE.CubeGeometry(this.flatWidth, this.flatThickness, this.depth);
var flatMat = new THREE.MeshPhongMaterial({color: this.color, ambient: this.color});
//textured wood material - only if running on a server, cross-origin prevents running locally
//flatMat = textureMaterial('wood');
var flat = new THREE.Mesh(flatGeom, flatMat);
flat.name = this.name + 'FlatMesh';
flatFrame.add(flat);
return flatFrame;
}
/*
range()
Purpose: returns an object containing the min and max x, y, and z coordinates of the shelf's frame
*/
Shelf.prototype.range = function () {
var framePos = this.frame.position;
var minX = framePos.x - 0.5 * this.width;
var maxX = framePos.x + 0.5 * this.width;
var minY = framePos.y - 0.5 * this.flatThickness;
var maxY = framePos.y + 0.5 * this.flatThickness;
var minZ = framePos.z - 0.5 * this.depth;
var maxZ = framePos.z + 0.5 * this.depth;
return {'minX': minX, 'maxX': maxX, 'minY': minY, 'maxY': maxY, 'minZ': minZ, 'maxZ': maxZ};
}
/*
addIntersectedBook()
Purpose: if the book at the given index intersects the shelf, as determined by their ranges,
sets the book's position so it lines up nicely with the shelf,
removes the book from the floor array, and adds the book to the books array
Parameters:
bookIndex: index of the book to check
*/
Shelf.prototype.addIntersectedBook = function (bookIndex) {
if (!this.intersectsBook(bookIndex)) {
return;
}
var book = this.floor[bookIndex];
var bookPos = book.frame.position;
var shelfRange = this.range();
//set the x coordinate
var x = bookPos.x;
if (x < shelfRange.minX) {
x = shelfRange.minX;
}
if (x > shelfRange.minX) {
x = shelfRange.maxX;
}
//set the y coordinate
var y = shelfRange.minY + 0.5 * this.flatThickness + 0.5 * book.height;
//set the z coordinate
var z = shelfRange.minZ + 0.5 * this.depth + 0.5 * book.depth;
//position, rotate and unhighlight the book
book.frame.position.set(x, y, z);
book.frame.rotation.y = Math.PI/2;
book.unhighlight();
//move the book from the floor to the books array
this.floor[bookIndex] = undefined;
this.books[bookIndex] = book;
}
/*
intersectsBook()
Purpose: returns true iff the book at the given index (in the floor array) intersects with the shelf's frame
as determined by the ranges of the book and the shelf
Parameters:
bookIndex: index of the book to check
*/
Shelf.prototype.intersectsBook = function (bookIndex) {
var book = this.floor[bookIndex];
if (!book) {
return false;
}
//ranges of the shelf and book
var shelfRange = this.range();
var bookRange = book.range();
//check the x
var xTolerance = 1.0 * book.width;
var xMinOkay = bookRange.minX >= shelfRange.minX - xTolerance && bookRange.minX <= shelfRange.maxX + xTolerance;
var xMaxOkay = bookRange.maxX >= shelfRange.minX - xTolerance && bookRange.maxX <= shelfRange.maxX + xTolerance;
var xOkay = xMinOkay || xMaxOkay;
//check the y
var yTolerance = 0.5 * book.height;
var yMinOkay = bookRange.minY >= shelfRange.minY - yTolerance && bookRange.minY <= shelfRange.maxY + yTolerance;
var yMaxOkay = bookRange.maxY >= shelfRange.minY - yTolerance && bookRange.maxY <= shelfRange.maxY + yTolerance;
var yOkay = yMinOkay || yMaxOkay;
//check the z
var zTolerance = 0.5 * book.depth;
var zMinOkay = bookRange.minZ >= shelfRange.minZ - zTolerance && bookRange.minZ <= shelfRange.maxZ + zTolerance;
var zMaxOkay = bookRange.maxZ >= shelfRange.minZ - zTolerance && bookRange.maxZ <= shelfRange.maxZ + zTolerance;
var zOkay = zMinOkay || zMaxOkay;
return xOkay && yOkay && zOkay;
}
/*
clearBooks()
Purpose: make all the books on the shelf fall to the floor
*/
Shelf.prototype.clearBooks = function (objectsControls) {
if(this.floor.length == 0){ //check if it's already registered a swipe gesture before
this.floor = this.books; //floor gets all books
for (var i in this.books) {
var edgePos = new THREE.Vector3().copy(this.books[i].frame.position);
var bookXSign = edgePos.x == 0 ? 0 : edgePos.x/Math.abs(edgePos.x);
edgePos.z += this.depth;
var sign = i % 2 == 0 ? 1 : -1;
this.books[i].fall(edgePos, sign);
//this.books[i].unhighlight(); //unhighlight when clearing the books from shelf
}
this.books = []; //empties books array
this.shelfIsEmpty = true;
var len = objectsControls.length;
for(var i=0; i<len; i++){
var oc = objectsControls.shift(); //pop the head of array, not tail
oc.panEnabled = true;
objectsControls.push(oc);
}
//update objectControls: enable pan when books fall from shelf.
/*
for(objectControls in objectsControls){
console.log("pan before? " + objectControls.panEnabled);
objectControls.panEnabled = true;
console.log("pan after? " + objectControls.panEnabled);
}
*/
/*
for(var i=0; i<objectsControls.length; i++){
var objectControls = objectsControls.pop();
console.log("before:" + objectControls.panEnabled);
objectControls.panEnabled = true;
objectsControls.push(objectControls);
console.log("after:" + objectControls.panEnabled);
}
*/
//console.log("numbooks_floor:"+this.floor.length+",numbooks_shelf:"+this.books.length);
}
}
/*
addBook()
Purpose: adds one book to the shelf, updating the objects and the objectsControls arrays in the process
*/
Shelf.prototype.addBook = function (scene, objects, objectsControls) {
var shelfPos = this.frame.position;
var bookWidth = this.depth;
var bookHeight = randomInRange(1.25 * bookWidth, 2.0 * bookWidth);
var bookDepth = this.width/this.numBooks;
var bookSpacing = 0.3 * bookDepth;
this.bookSpacing = bookSpacing;
var book = new Book(bookWidth, bookHeight, randomInRange(0.5, 1) * (bookDepth - bookSpacing), Math.random() * 0xffffff, this.books.length);
var object = book.frame;
object.position.copy(shelfPos);
object.position.x -= 0.5 * this.width;
object.position.x += (this.books.length + 0.5) * bookDepth;
object.position.y += 0.5 * this.flatThickness + 0.5 * bookHeight;
object.position.z += 0.5 * this.depth;
object.rotateY(Math.PI/2);
this.books.push(book);
// leap object controls
var objectControls = new THREE.LeapObjectControls(camera, object);
objectControls.rotateEnabled = true;
objectControls.rotateSpeed = 3;
objectControls.rotateHands = 1;
objectControls.rotateFingers = [2, 3];
// *******Get rid of the scale function on objects********
// *******Set objectControls.scaleEnabled = false
objectControls.scaleEnabled = false;
objectControls.scaleSpeed = 3;
objectControls.scaleHands = 1;
objectControls.scaleFingers = [4, 5];
objectControls.panEnabled = false; //books on the shelf should not be panned!!
//console.log("disabled panning when adding book");
objectControls.panSpeed = 3;
objectControls.panHands = 2;
objectControls.panFingers = [6, 12];
objectControls.panRightHanded = false; // for left-handed person
scene.add(object);
objects.push(object);
objectsControls.push(objectControls);
}
/*
addBooks()
Purpose: adds numBooks books to the shelf
*/
Shelf.prototype.addBooks = function (numBooks, scene, objects, objectsControls) {
this.numBooks = numBooks;
var shelfPos = this.frame.position;
for (var i = 0; i < numBooks; i++) {
this.addBook(scene, objects, objectsControls);
}
}