View each floor of a building using an custom vertical 'explode' animation
Live Demo: DEMO
This effect is made up of three parts...
- Explode function (controlled by a slider)
- Slow down the ForgeViewer Camera
- Ease animation for level explode
- Restoring 4 different camera states
I first customize the explode code from Philippe's blog post. I then restrict the transforms to vertical only. Finally, I cluster objects by their vertical position within a level. You can test the functionality using the slider.
var cz = Math.floor(boxes[box_offset + 2] / 10) * 10;
pt.z = cz * scale * 4;
fragList.updateAnimTransform(fragId, null, null, pt);
You can slow down the Forge Viewer's camera transitions using these two lines...
viewer.autocam.shotParams.destinationPercent=3;
viewer.autocam.shotParams.duration = 3;
Next, I need to add smooth explode animation, when I click open or close. I borrowed from this simple example source-code, and simply applied it to the onSlider() method
// animate.js
function circ(timeFraction) { return 1 - Math.sin(Math.acos(timeFraction)) }
function makeEaseOut(timing) { return function(timeFraction) {return 1 - timing(1 - timeFraction)}}
function animate({timing, draw, duration}) {
let start = performance.now();
requestAnimationFrame(function animate(time) {
let timeFraction = (time - start) / duration;
if (timeFraction > 1) timeFraction = 1;
let progress = timing(timeFraction);
draw(progress); // draw it
if (timeFraction < 1) {
requestAnimationFrame(animate);
}
});
}
I've done this trick a few times before.
Simply set up your camera view and hide objects in the scene manually, then capture the camera-state using...
view_state_level1 = viewer.getState()
Then build a menu with buttons, with each button restoring your pre-canned view, like this...
viewer.restoreState(view_state_level1)