%PDF- %PDF-
| Direktori : /home/tjamichg/cursos.tjamich.gob.mx/web/assets/vrview/src/embed/ |
| Current File : /home/tjamichg/cursos.tjamich.gob.mx/web/assets/vrview/src/embed/main.js |
/*
* Copyright 2016 Google Inc. All Rights Reserved.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
// Initialize the loading indicator as quickly as possible to give the user
// immediate feedback.
var LoadingIndicator = require('./loading-indicator');
var loadIndicator = new LoadingIndicator();
var ES6Promise = require('es6-promise');
// Polyfill ES6 promises for IE.
ES6Promise.polyfill();
var IFrameMessageReceiver = require('./iframe-message-receiver');
var Message = require('../message');
var SceneInfo = require('./scene-info');
var Stats = require('../../node_modules/stats-js/build/stats.min');
var Util = require('../util');
var WebVRPolyfill = require('webvr-polyfill');
var WorldRenderer = require('./world-renderer');
var receiver = new IFrameMessageReceiver();
receiver.on(Message.PLAY, onPlayRequest);
receiver.on(Message.PAUSE, onPauseRequest);
receiver.on(Message.ADD_HOTSPOT, onAddHotspot);
receiver.on(Message.SET_CONTENT, onSetContent);
receiver.on(Message.SET_VOLUME, onSetVolume);
receiver.on(Message.MUTED, onMuted);
receiver.on(Message.SET_CURRENT_TIME, onUpdateCurrentTime);
receiver.on(Message.GET_POSITION, onGetPosition);
receiver.on(Message.SET_FULLSCREEN, onSetFullscreen);
window.addEventListener('load', onLoad);
var stats = new Stats();
var scene = SceneInfo.loadFromGetParams();
var worldRenderer = new WorldRenderer(scene);
worldRenderer.on('error', onRenderError);
worldRenderer.on('load', onRenderLoad);
worldRenderer.on('modechange', onModeChange);
worldRenderer.on('ended', onEnded);
worldRenderer.on('play', onPlay);
worldRenderer.hotspotRenderer.on('click', onHotspotClick);
window.worldRenderer = worldRenderer;
var isReadySent = false;
var volume = 0;
function onLoad() {
if (!Util.isWebGLEnabled()) {
showError('WebGL not supported.');
return;
}
// Load the scene.
worldRenderer.setScene(scene);
if (scene.isDebug) {
// Show stats.
showStats();
}
if (scene.isYawOnly) {
WebVRConfig = window.WebVRConfig || {};
WebVRConfig.YAW_ONLY = true;
}
requestAnimationFrame(loop);
}
function onVideoTap() {
worldRenderer.videoProxy.play();
hidePlayButton();
// Prevent multiple play() calls on the video element.
document.body.removeEventListener('touchend', onVideoTap);
}
function onRenderLoad(event) {
if (event.videoElement) {
var scene = SceneInfo.loadFromGetParams();
// On mobile, tell the user they need to tap to start. Otherwise, autoplay.
if (Util.isMobile()) {
// Tell user to tap to start.
showPlayButton();
document.body.addEventListener('touchend', onVideoTap);
} else {
event.videoElement.play();
}
// Attach to pause and play events, to notify the API.
event.videoElement.addEventListener('pause', onPause);
event.videoElement.addEventListener('play', onPlay);
event.videoElement.addEventListener('timeupdate', onGetCurrentTime);
event.videoElement.addEventListener('ended', onEnded);
}
// Hide loading indicator.
loadIndicator.hide();
// Autopan only on desktop, for photos only, and only if autopan is enabled.
if (!Util.isMobile() && !worldRenderer.sceneInfo.video && !worldRenderer.sceneInfo.isAutopanOff) {
worldRenderer.autopan();
}
// Notify the API that we are ready, but only do this once.
if (!isReadySent) {
if (event.videoElement) {
Util.sendParentMessage({
type: 'ready',
data: {
duration: event.videoElement.duration
}
});
} else {
Util.sendParentMessage({
type: 'ready'
});
}
isReadySent = true;
}
}
function onPlayRequest() {
if (!worldRenderer.videoProxy) {
onApiError('Attempt to pause, but no video found.');
return;
}
worldRenderer.videoProxy.play();
}
function onPauseRequest() {
if (!worldRenderer.videoProxy) {
onApiError('Attempt to pause, but no video found.');
return;
}
worldRenderer.videoProxy.pause();
}
function onAddHotspot(e) {
if (Util.isDebug()) {
console.log('onAddHotspot', e);
}
// TODO: Implement some validation?
var pitch = parseFloat(e.pitch);
var yaw = parseFloat(e.yaw);
var radius = parseFloat(e.radius);
var distance = parseFloat(e.distance);
var id = e.id;
worldRenderer.hotspotRenderer.add(pitch, yaw, radius, distance, id);
}
function onSetContent(e) {
if (Util.isDebug()) {
console.log('onSetContent', e);
}
// Remove all of the hotspots.
worldRenderer.hotspotRenderer.clearAll();
// Fade to black.
worldRenderer.sphereRenderer.setOpacity(0, 500).then(function() {
// Then load the new scene.
var scene = SceneInfo.loadFromAPIParams(e.contentInfo);
worldRenderer.destroy();
// Update the URL to reflect the new scene. This is important particularily
// on iOS where we use a fake fullscreen mode.
var url = scene.getCurrentUrl();
//console.log('Updating url to be %s', url);
window.history.pushState(null, 'VR View', url);
// And set the new scene.
return worldRenderer.setScene(scene);
}).then(function() {
// Then fade the scene back in.
worldRenderer.sphereRenderer.setOpacity(1, 500);
});
}
function onSetVolume(e) {
// Only work for video. If there's no video, send back an error.
if (!worldRenderer.videoProxy) {
onApiError('Attempt to set volume, but no video found.');
return;
}
worldRenderer.videoProxy.setVolume(e.volumeLevel);
volume = e.volumeLevel;
Util.sendParentMessage({
type: 'volumechange',
data: e.volumeLevel
});
}
function onMuted(e) {
// Only work for video. If there's no video, send back an error.
if (!worldRenderer.videoProxy) {
onApiError('Attempt to mute, but no video found.');
return;
}
worldRenderer.videoProxy.mute(e.muteState);
Util.sendParentMessage({
type: 'muted',
data: e.muteState
});
}
function onUpdateCurrentTime(time) {
if (!worldRenderer.videoProxy) {
onApiError('Attempt to pause, but no video found.');
return;
}
worldRenderer.videoProxy.setCurrentTime(time);
onGetCurrentTime();
}
function onGetCurrentTime() {
var time = worldRenderer.videoProxy.getCurrentTime();
Util.sendParentMessage({
type: 'timeupdate',
data: time
});
}
function onSetFullscreen() {
if (!worldRenderer.videoProxy) {
onApiError('Attempt to set fullscreen, but no video found.');
return;
}
worldRenderer.manager.onFSClick_();
}
function onApiError(message) {
console.error(message);
Util.sendParentMessage({
type: 'error',
data: {message: message}
});
}
function onModeChange(mode) {
Util.sendParentMessage({
type: 'modechange',
data: {mode: mode}
});
}
function onHotspotClick(id) {
Util.sendParentMessage({
type: 'click',
data: {id: id}
});
}
function onPlay() {
Util.sendParentMessage({
type: 'paused',
data: false
});
}
function onPause() {
Util.sendParentMessage({
type: 'paused',
data: true
});
}
function onEnded() {
Util.sendParentMessage({
type: 'ended',
data: true
});
}
function onSceneError(message) {
showError('Loader: ' + message);
}
function onRenderError(message) {
showError('Render: ' + message);
}
function showError(message) {
// Hide loading indicator.
loadIndicator.hide();
// Sanitize `message` as it could contain user supplied
// values. Re-add the space character as to not modify the
// error messages used throughout the codebase.
message = encodeURI(message).replace(/%20/g, ' ');
var error = document.querySelector('#error');
error.classList.add('visible');
error.querySelector('.message').innerHTML = message;
error.querySelector('.title').innerHTML = 'Error';
}
function hideError() {
var error = document.querySelector('#error');
error.classList.remove('visible');
}
function showPlayButton() {
var playButton = document.querySelector('#play-overlay');
playButton.classList.add('visible');
}
function hidePlayButton() {
var playButton = document.querySelector('#play-overlay');
playButton.classList.remove('visible');
}
function showStats() {
stats.setMode(0); // 0: fps, 1: ms
// Align bottom-left.
stats.domElement.style.position = 'absolute';
stats.domElement.style.left = '0px';
stats.domElement.style.bottom = '0px';
document.body.appendChild(stats.domElement);
}
function loop(time) {
// Use the VRDisplay RAF if it is present.
if (worldRenderer.vrDisplay) {
worldRenderer.vrDisplay.requestAnimationFrame(loop);
} else {
requestAnimationFrame(loop);
}
stats.begin();
// Update the video if needed.
if (worldRenderer.videoProxy) {
worldRenderer.videoProxy.update(time);
}
worldRenderer.render(time);
worldRenderer.submitFrame();
stats.end();
}
function onGetPosition() {
Util.sendParentMessage({
type: 'getposition',
data: {
Yaw: worldRenderer.camera.rotation.y * 180 / Math.PI,
Pitch: worldRenderer.camera.rotation.x * 180 / Math.PI
}
});
}