-
Notifications
You must be signed in to change notification settings - Fork 1
/
ViewSaveAnimate.html
513 lines (449 loc) · 19.2 KB
/
ViewSaveAnimate.html
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Save and Animate view</title>
<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/themes/smoothness/jquery-ui.css" />
<style>
#myViewsList
{
list-style-type: none;
margin: 0;
padding: 0;
}
#myViewsList li
{
margin: 3px;
float: left;
}
#myViewsList button
{
width: 50px;
text-align: center;
}
.myHandle
{
cursor: move;
}
body, html
{
height: 100%;
}
#myViewer
{
position: absolute;
left: 0px;
top: 0px;
width: 100%;
height: 100%;
}
#myViewsToolbar
{
opacity: 0.1;
left: 30px;
position: absolute;
z-index: 1;
}
#myViewsToolbarHandle
{
min-width: 5px;
}
#myViewsToolbarList
{
float: right;
}
</style>
<link rel="stylesheet" href="https://developer.api.autodesk.com/viewingservice/v1/viewers/style.css"
type="text/css" />
</head>
<body>
<div id="myViewer">
</div>
<div id="myViewsToolbar" class="ui-widget-header ui-corner-all">
<table>
<tr>
<td id="myViewsToolbarHandle" class="ui-widget-content myHandle">
</td>
<td id="myViewsToolbarList">
<ul id="myViewsList">
<li class="myNoSelect">
<button id="myStoreView">
S</button></li>
<li class="myNoSelect">
<button id="myAnimateView">
A</button></li>
</ul>
</td>
</tr>
</table>
</div>
<script src="https://developer.api.autodesk.com/viewingservice/v1/viewers/viewer3D.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.4/jquery-ui.min.js"></script>
<script>
// Variable for storing all our data
var MyClass = {};
// Number of steps to take in between transitioning from onew stored view
// to the next when animating
MyClass.kInBetweenSteps = 20;
MyClass.kMilliSecondsBetweenSteps = 50;
MyClass.initializeOneView = function () {
// Get the parameters passed in the URL
// ViewSaveAnimate.html?accessToken=<accessToken>&urn=<the urn of the file to view>
var urn = Autodesk.Viewing.Private.getParameterByName("urn");
var accessToken = Autodesk.Viewing.Private.getParameterByName("accessToken");
var options = {};
// Environment controls service end points viewer uses
// Development, Staging, and Production are valid values
options.env = "AutodeskProduction";
// Access token required for authentication and authorization
// It is for now a 3 legged oauth 1 access token.
// Here we have a hardcoded token for testing purpose.
// In practice you need to obtain a valid access token and pass it to viewer, instead of using this token.
// Note it is valid for two hours so you will need to recreate from time to time
//
options.accessToken = accessToken;
// The document to load in the viewer ...
options.document = "urn:" + urn;
// Use 3D viewer
var viewerElement = document.getElementById('myViewer');
MyClass.viewer = new Autodesk.Viewing.Private.GuiViewer3D(viewerElement, {});
Autodesk.Viewing.Initializer(options, function () {
MyClass.viewer.initialize();
MyClass.loadDocument(MyClass.viewer, null, options.document);
});
}; // MyClass.initializeOneView
MyClass.loadDocument = function loadDocument(viewer, auth, documentId, initialItemId) {
// Load the document. Once loaded, find the item requested. If not found,
// just find the first 3d geometry and load that
//
Autodesk.Viewing.Document.load(
documentId,
auth,
function (document) { // onLoadCallback
var geometryItems = [];
if (initialItemId) {
geometryItems = Autodesk.Viewing.Document.getSubItemsWithProperties(document.getRootItem(), { 'guid': initialItemId }, true);
}
if (geometryItems.length == 0) {
geometryItems = Autodesk.Viewing.Document.getSubItemsWithProperties(document.getRootItem(), { 'type': 'geometry', 'role': '3d' }, true);
}
if (geometryItems.length > 0) {
viewer.load(document.getViewablePath(geometryItems[0]));
}
},
function (errorMsg, httpErrorCode) { // onErrorCallback
var container = document.getElementById('myViewer');
if (container) {
var errorCode = ErrorHander.getErrorCode(httpErrorCode);
ErrorHandler.reportError(container, errorCode);
}
}
);
}; // MyClass.loadDocument
MyClass.onErrorLoadModel = function (msg) {
var container = document.getElementById('myViewer');
if (container) {
Autodesk.Viewing.Private.AlertBox.displayError(container, "LOAD Error: " + msg);
}
}; // MyClass.onErrorLoadModel
// The array to store the view properties
MyClass.storedViews = [];
//
MyClass.inBetweenSteps = 0;
MyClass.currentInBetweenStep = 0;
// The list stores the idSeed that contains the id that was used last time
// We could have simply store it in a global variable, but for fun
// we store it in the list object using jQuery
MyClass.getNewViewId = function () {
// It always contains #myStoreView and #myAnimateView
//
var idSeed = $.data($("#myViewsList")[0], "idSeed");
if (idSeed) {
idSeed++;
} else {
idSeed = 1;
}
$.data($("#myViewsList")[0], "idSeed", idSeed);
return idSeed.toString();
}; // MyClass.getNewViewId
// Storing the current view in the array of stored views and adding a list item
// with a button in it to represent it on the toolbar
MyClass.actionStoreView = function () {
var id = MyClass.getNewViewId();
var vwr = MyClass.viewer;
var thisView = [];
thisView[0] = vwr.getFOV();
thisView[1] = vwr.navigation.getPosition();
thisView[2] = vwr.navigation.getTarget();
thisView[3] = vwr.navigation.getCameraUpVector();
MyClass.storedViews[id] = thisView;
// Add a new view button to the list
// The sortable("refresh") did not work well enough
// but recreating the whole thing seems to be good
var $newItem = $("<li />").addClass("myDroppable");
$("<button />").attr("id", id).html(id)
.appendTo($newItem).button();
$newItem.appendTo("#myViewsList");
MyClass.refreshMyViewsList();
}; // MyClass.actionStoreView
// Retrieves a saved view and generates in-between steps as well
// if necessary. If it is called directly by clicking on a saved view
// then MyClass.inBetweenSteps = 0, if the animation calls it
// then it will set it to some other value
MyClass.retrieveView = function () {
if (!MyClass.curView)
return;
// Get id from button under the list item
//
var id = MyClass.curView.firstChild.attributes["id"].value;
console.info("Current id = " + id);
console.info("inBetweenSteps = " + MyClass.inBetweenSteps.toString());
console.info("currentInBetweenStep = " + MyClass.currentInBetweenStep.toString());
var vwr = MyClass.viewer;
// Making a copy so that we can modify it below without overwriting
// the saved view properties
var view = MyClass.storedViews[id];
var newView = [];
newView[0] = view[0];
newView[1] = view[1].clone();
newView[2] = view[2].clone();
newView[3] = view[3].clone();
console.info("storedViews[id] = " + newView[0].toString() + "; " +
newView[1].x.toString() + "/" + newView[1].y.toString() + "/" + newView[1].z.toString() + "; " +
newView[2].x.toString() + "/" + newView[2].y.toString() + "/" + newView[2].z.toString() + "; " +
newView[3].x.toString() + "/" + newView[3].y.toString() + "/" + newView[3].z.toString());
// If the saved view list item has a sibling, i.e. another list item next to it
// that we should move to then we'll calculate inbetween steps. If this is the last view
// during animation then (i.e. this saved view list item has no sibling)
// then we can skip this
if (MyClass.curView.nextSibling && MyClass.inBetweenSteps > 0) {
var nextId = MyClass.curView.nextSibling.firstChild.attributes["id"].value;
var newViewNext = MyClass.storedViews[nextId];
// Get intermediate steps in between two stored views
var devider = MyClass.currentInBetweenStep / MyClass.inBetweenSteps;
newView[0] = newView[0] + (newViewNext[0] - newView[0]) * devider;
newView[1].x = newView[1].x + (newViewNext[1].x - newView[1].x) * devider;
newView[1].y = newView[1].y + (newViewNext[1].y - newView[1].y) * devider;
newView[1].z = newView[1].z + (newViewNext[1].z - newView[1].z) * devider;
newView[2].x = newView[2].x + (newViewNext[2].x - newView[2].x) * devider;
newView[2].y = newView[2].y + (newViewNext[2].y - newView[2].y) * devider;
newView[2].z = newView[2].z + (newViewNext[2].z - newView[2].z) * devider;
newView[3].x = newView[3].x + (newViewNext[3].x - newView[3].x) * devider;
newView[3].y = newView[3].y + (newViewNext[3].y - newView[3].y) * devider;
newView[3].z = newView[3].z + (newViewNext[3].z - newView[3].z) * devider;
} else {
// No more steps
MyClass.inBetweenSteps = 0;
}
console.info("modified storedViews[id] = " + newView[0].toString() + "; " + newView[1].toString() + "; " +
newView[2].x.toString() + "/" + newView[2].y.toString() + "/" + newView[2].z.toString() + "; " +
newView[3].x.toString() + "/" + newView[3].y.toString() + "/" + newView[3].z.toString());
vwr.navigation.setRequestTransitionWithUp(
true, newView[1], newView[2], newView[0], newView[3]
);
}; // MyClass.retrieveView
// Storing the current view in the array of stored views and adding a list item
// with a button in it to represent it on the toolbar
MyClass.actionStoreView_old = function () {
var id = MyClass.getNewViewId();
var vwr = MyClass.viewer;
var thisView = [];
var curCam = vwr.getCamera();
thisView[0] = curCam.aspect;
thisView[1] = curCam.fov;
thisView[2] = curCam.position.clone();
thisView[3] = vwr.impl.controls.getLookAtPoint().clone();
MyClass.storedViews[id] = thisView;
// Add a new view button to the list
// The sortable("refresh") did not work well enough
// but recreating the whole thing seems to be good
var $newItem = $("<li />").addClass("myDroppable");
$("<button />").attr("id", id).html(id)
.appendTo($newItem).button();
$newItem.appendTo("#myViewsList");
MyClass.refreshMyViewsList();
}; // MyClass.actionStoreView
// Retrieves a saved view and generates in-between steps as well
// if necessary. If it is called directly by clicking on a saved view
// then MyClass.inBetweenSteps = 0, if the animation calls it
// then it will set it to some other value
MyClass.retrieveView_old = function () {
if (!MyClass.curView)
return;
// Get id from button under the list item
//
var id = MyClass.curView.firstChild.attributes["id"].value;
console.info("Current id = " + id);
console.info("inBetweenSteps = " + MyClass.inBetweenSteps.toString());
console.info("currentInBetweenStep = " + MyClass.currentInBetweenStep.toString());
var vwr = MyClass.viewer;
// Making a copy so that we can modify it below without overwriting
// the saved view properties
var view = MyClass.storedViews[id];
var newView = [];
newView[0] = view[0];
newView[1] = view[1];
newView[2] = view[2].clone();
newView[3] = view[3].clone();
console.info("storedViews[id] = " + newView[0].toString() + "; " + newView[1].toString() + "; " +
newView[2].x.toString() + "/" + newView[2].y.toString() + "/" + newView[2].z.toString() + "; " +
newView[3].x.toString() + "/" + newView[3].y.toString() + "/" + newView[3].z.toString());
// If the saved view list item has a sibling, i.e. another list item next to it
// that we should move to then we'll calculate inbetween steps. If this is the last view
// during animation then (i.e. this saved view list item has no sibling)
// then we can skip this
if (MyClass.curView.nextSibling && MyClass.inBetweenSteps > 0) {
var nextId = MyClass.curView.nextSibling.firstChild.attributes["id"].value;
var newViewNext = MyClass.storedViews[nextId];
// Get intermediate steps in between two stored views
var devider = MyClass.currentInBetweenStep / MyClass.inBetweenSteps;
newView[0] = newView[0] + (newViewNext[0] - newView[0]) * devider;
newView[1] = newView[1] + (newViewNext[1] - newView[1]) * devider;
newView[2].x = newView[2].x + (newViewNext[2].x - newView[2].x) * devider;
newView[2].y = newView[2].y + (newViewNext[2].y - newView[2].y) * devider;
newView[2].z = newView[2].z + (newViewNext[2].z - newView[2].z) * devider;
newView[3].x = newView[3].x + (newViewNext[3].x - newView[3].x) * devider;
newView[3].y = newView[3].y + (newViewNext[3].y - newView[3].y) * devider;
newView[3].z = newView[3].z + (newViewNext[3].z - newView[3].z) * devider;
} else {
// No more steps
MyClass.inBetweenSteps = 0;
}
console.info("modified storedViews[id] = " + newView[0].toString() + "; " + newView[1].toString() + "; " +
newView[2].x.toString() + "/" + newView[2].y.toString() + "/" + newView[2].z.toString() + "; " +
newView[3].x.toString() + "/" + newView[3].y.toString() + "/" + newView[3].z.toString());
vwr.impl.camera.aspect = newView[0];
vwr.impl.camera.fov = newView[1];
vwr.impl.controls.setViewpoint(newView[2], newView[3]);
}; // MyClass.retrieveView
// We also use a timer so that you can easily modify how fast
// the animation should move from one step to the next
MyClass.timerListener = function () {
console.info("timerListener");
// Stop listening to the timer
//
clearInterval(MyClass.timer);
// Start listening to "progress" so that
// we know when the view is fully loaded
//
var vwr = MyClass.viewer;
vwr.addEventListener("progress", MyClass.progressListener);
MyClass.retrieveView();
}; // MyClass.timerListener
// When the view changes the viewer will re-render the scene
// and report the progress through the "progress" event
// When it reaches 100% we move on to the next saved view during animation
MyClass.progressListener = function (param) {
console.info("progressListener: " + param.percent);
if (param.percent === 100) {
// Stop listening to progress
//
var vwr = MyClass.viewer;
vwr.removeEventListener("progress", MyClass.progressListener);
if (MyClass.currentInBetweenStep >= MyClass.inBetweenSteps - 1) {
MyClass.currentInBetweenStep = 0;
MyClass.curView = MyClass.curView.nextSibling;
} else {
MyClass.currentInBetweenStep++;
}
if (MyClass.curView) {
// The current view finished loading so we can move to the next one
// after a small delay
//
MyClass.timer = setInterval("MyClass.timerListener()", MyClass.kMilliSecondsBetweenSteps);
} else {
// No more views so we are finished and can
// re-enable the buttons
//
$("#myViewsToolbar button").button("enable");
}
}
}; // MyClass.progressListener
MyClass.animateView = function () {
// Disable toolbar buttons while animating
var $buttons = $("#myViewsToolbar button");
$buttons.button("disable");
// Set how many steps should be taken in between saved views
MyClass.inBetweenSteps = MyClass.kInBetweenSteps;
MyClass.currentInBetweenStep = 0;
if (!MyClass.curView) {
// Get first view list item (3rd in the list)
//
MyClass.curView = $("#myViewsList").children()[2];
}
// Show the first view and listen to the "progress"
// to reach 100
//
var vwr = MyClass.viewer;
vwr.addEventListener("progress", MyClass.progressListener);
MyClass.retrieveView();
}; // MyClass.animateView
MyClass.refreshMyViewsList = function () {
// MrefreshMyViewsListake the buttons use the jQuery UI style
//
$("button").button();
// If there is only one view item, no point of sorting
// length includes #myStoreView and #myAnimateView,
// therefore use 4
//
if ($("#myViewsList")[0].children.length < 4)
return;
$("#myViewsList").sortable({
items: "li:not(.myNoSelect)", handle: "button",
cancel: "", scroll: false
});
// If the items are dropped on the toolbar
// it won't propagate to "body"
//
$("#myViewsToolbar").droppable({
greedy: true
});
}; // MyClass.refreshMyViewsList
// Handling OnDocumentReady through jQuery
$(function () {
// Use "body" as dustbin for toolbar items
// flagged with myDroppable class
//
$('body').droppable({
accept: ".myDroppable",
drop: function (event, ui) {
ui.draggable.remove();
}
});
MyClass.refreshMyViewsList();
$("#myViewsList").on("click", "button", function () {
// If it's the myStoreView button then we'll save
// otherwise retrieve view
//
var id = $(this).attr("id");
if (id === "myStoreView") {
MyClass.actionStoreView();
} else if (id === "myAnimateView") {
MyClass.animateView();
} else {
MyClass.curView = $(this).parent()[0];
MyClass.retrieveView();
MyClass.curView = null;
}
});
$("#myViewsToolbar").draggable({
handle: "#myViewsToolbarHandle", scroll: false
}).mouseenter(function () {
$(this).css({ opacity: 1.0 });
}).mouseleave(function () {
$(this).css({ opacity: 0.1 });
});
MyClass.initializeOneView();
$(document).keypress(function (e) {
console.info("e.which = " + e.which);
// If 's' was pressed
//
if (e.which === 115) {
MyClass.actionStoreView();
}
});
});
</script>
</body>
</html>