Model custom animation¶
Custom animation for gltf model
model_custom_animation.html
<!DOCTYPE html>
<!--
Model Information:
* title: Beach Ball
* source: https://sketchfab.com/3d-models/beach-ball-25e1816c0e22444bb62816d3999d1b0b
* author: MaggaModels (https://sketchfab.com/MaggaModels)
Model License:
* license type: CC-BY-4.0 (http://creativecommons.org/licenses/by/4.0/)
* requirements: Author must be credited. Commercial use is allowed.
If you use this 3D model in your project be sure to copy paste this credit wherever you share it:
This work is based on "Beach Ball" (https://sketchfab.com/3d-models/beach-ball-25e1816c0e22444bb62816d3999d1b0b) by MaggaModels (https://sketchfab.com/MaggaModels) licensed under CC-BY-4.0 (http://creativecommons.org/licenses/by/4.0/)
-->
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Model custom animation</title>
<meta property="og:description" content="Custom animation for gltf model" />
<meta name="mapray-version" content="experimental-feature">
<meta name="mapray-api-status" content="experimental">
<meta name="mapray-api-package" content="model-pbr">
<script src="./model-pbr/mapray.min.js"></script>
<script src="./model-pbr/maprayui.min.js"></script>
<link rel="stylesheet" href="./model-pbr/mapray.css">
<style>
body {margin: 0; padding: 0;}
html, body, div#mapray-container { height: 100%; }
</style>
</head>
<body>
<div id="mapray-container"></div>
</body>
</html>
<script>
// Animation end time
const orbit_end = 20;
const rotate_end = 3;
const orbit_updater = new mapray.animation.Updater();
const rotate_updater = new mapray.animation.Updater();
class AnimationView extends maprayui.StandardUIViewer {
constructor(container, options) {
super(container, options);
this.orbit_time = 0;
this.rotate_time = 0;
}
onUpdateFrame(delta_time) {
super.onUpdateFrame(delta_time);
if ( this.getEntityNum() > 0 ) {
const entity = this.getEntity(0);
orbit_updater.update( mapray.animation.Time.fromNumber( this.orbit_time ));
rotate_updater.update( mapray.animation.Time.fromNumber( this.rotate_time ));
// Loop animation
this.orbit_time += delta_time;
if (this.orbit_time > orbit_end) {
this.orbit_time = 0.0;
}
this.rotate_time += delta_time;
if( this.rotate_time > rotate_end ) {
this.rotate_time = 0.0;
}
}
}
}
const cloudApi = new mapray.cloud.CloudApiV2({
tokenType: mapray.cloud.CloudApi.TokenType.ACCESS_TOKEN,
token: "<YOUR_MAPRAY_ACCESS_TOKEN>"
});
// The StandardUIView in the ui package includes mouse-based camera manipulation.
const uiviewer = new AnimationView( "mapray-container", {
dem_provider: new mapray.StandardDemProvider( cloudApi.getDefaultDemAsResource() )
} );
uiviewer.setCameraPosition({
longitude: 139.76489,
latitude: 35.67209,
height: 455
});
uiviewer.setLookAtPosition({
longitude: 139.76489,
latitude: 35.67209,
height: 455
});
const resource = cloudApi.get3DDatasetAsResource( [ "5178883078881280" ] );
const loader = new mapray.SceneLoader( uiviewer.viewer.scene, resource, {
onEntity: ( loader, entity, prop ) => {
// Offset the center of entity
const offest_transform = entity.getMatrixOffset();
offest_transform[12] = -10;
entity.setMatrixOffset(offest_transform);
const block = new mapray.animation.EasyBindingBlock();
const vector3 = mapray.animation.Type.find( "vector3" );
block.addEntry( "rotate", [vector3], null, value => {
entity.setNodeOrientation("RootNode", new mapray.Orientation(value[0], value[1], value[2]));
} );
block.addEntry( "orbit", [vector3], null, value => {
entity.setOrientation(new mapray.Orientation(value[0], value[1], value[2]));
} );
// Orbit center animation
const orbit_curve = new mapray.animation.KFLinearCurve( vector3 );
const orbit_frames = [];
orbit_frames.push( mapray.animation.Time.fromNumber( 0 ));
orbit_frames.push( mapray.GeoMath.createVector3([0, 0, 0]));
orbit_frames.push( mapray.animation.Time.fromNumber( orbit_end ));
orbit_frames.push( mapray.GeoMath.createVector3([360, 0, 0]));
orbit_curve.setKeyFrames( orbit_frames );
// Self rotate animation
const rotate_curve = new mapray.animation.KFLinearCurve( vector3 );
const rotate_frames = [];
rotate_frames.push( mapray.animation.Time.fromNumber( 0 ));
rotate_frames.push( mapray.GeoMath.createVector3([0, 0, 0]));
rotate_frames.push( mapray.animation.Time.fromNumber( rotate_end ));
rotate_frames.push( mapray.GeoMath.createVector3([0,-360,0]));
rotate_curve.setKeyFrames( rotate_frames );
block.bind( "orbit", orbit_updater, orbit_curve );
block.bind( "rotate", rotate_updater, rotate_curve );
uiviewer.addEntity(entity);
}
} );
loader.load();
</script>
Info
このサンプルコードは、<YOUR_MAPRAY_ACCESS_TOKEN>を、あなたのMapray CloudアカウントのOrganization Tokenに置き換えるまで、期待通りに動作しません。