Skip to content

Commit

Permalink
Refactor code so that based on passed in parameters, we'll create eit…
Browse files Browse the repository at this point in the history
…her a sphere to texture, or a cube to texture with the tiles
  • Loading branch information
zhanghaowx committed Aug 1, 2016
1 parent 77225f3 commit ca3a8ed
Show file tree
Hide file tree
Showing 7 changed files with 169 additions and 282 deletions.
2 changes: 1 addition & 1 deletion examples/BasicCube.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<script data-main="../src/config.js" src="../node_modules/requirejs/require.js"></script>
<script type="application/javascript">
requirejs(['app'], function() {
var cube = $("#container").cube({
$("#container").panorama({
tiles: [
'images/tiles/42762724_r8_f0_x0_y0.jpg',
'images/tiles/42762724_r8_f1_x0_y0.jpg',
Expand Down
2 changes: 1 addition & 1 deletion examples/BasicSphere.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
<script data-main="../src/config.js" src="../node_modules/requirejs/require.js"></script>
<script type="application/javascript">
requirejs(['app'], function() {
var cube = $("#container").cube({
$("#container").panorama({
texture: "images/panoramas/indoor.jpg"
});
});
Expand Down
10 changes: 2 additions & 8 deletions examples/HereCube.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
<script data-main="../src/config.js" src="../node_modules/requirejs/require.js"></script>
<script type="application/javascript">
// Start loading the main app file. Put all of your application logic in there.
requirejs(['app', 'here/api'], function() {
var cube3D = $("#container").cube({
requirejs(['app', 'here/Api'], function() {
var cube3D = $("#container").panorama({
tiles: HERE.Panorama.getImageUrls("1027084269", HERE.Panorama.RESOLUTION.HIGH)
});
cube3D.controller.autoRotate = true;
Expand Down Expand Up @@ -52,12 +52,6 @@

blurGUI.open();
}

var debugGUI = gui.addFolder('Debug');
debugGUI.add(cube3D.settings, 'showPanorama').onChange(function(value) {
cube3D.cube.visible = value;
});
debugGUI.open();
};
createDebugGUI(cube3D);
});
Expand Down
161 changes: 146 additions & 15 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,146 @@
define(["scene/Cube3D"], function (Cube3D) {
$.fn.cube = function (options) {
var name = "cube3D";
var cube = $(this).data(name);
if (cube) {
cube.clearPanorama();
cube.loadScene(options);
} else {
var cube = new Cube3D(this, options);
cube.loadScene();
$(this).data(name, cube);
}
return cube;
};
});
/**
* This library helps to draw a panorama image in web browser.
* It builds on three.js r67. There is no guarantee that it will work with
* other version of three.js.
*/

define([
"scene/Cube3D",
"scene/Sphere3D",
"scene/BlurArea",
"geometries/PanoramaCubeGeometry",
"controls/OrbitControls",
], function (Cube3D, Sphere3D) {
var App = function (mapContainer) {
// create a perspective camera
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

// create a proper renderer
this.renderer = this.supportsWebGL() ? new THREE.WebGLRenderer({
antialias: true,
preserveDrawingBuffer: true // for taking screenshots, it can cause significant performance loss on some platforms
}) : new THREE.CanvasRenderer();
this.renderer.setSize(window.innerWidth, window.innerHeight);

// create a camera controller
this.controller = new THREE.OrbitControls(this.camera, this.renderer.domElement);

// link renderer to a DOM element
this.mapContainer = mapContainer[0];
this.mapContainer.appendChild(this.renderer.domElement);

// create a scene and add needed meshes for displaying panoramas
this.scene = new THREE.Scene();

// bind resize event
window.addEventListener('resize', this.onWindowResize.bind(this), false);

this.draw();
};

/**
* Load panorama image to renderer
*/
App.prototype.loadScene = function (options) {
this.createPanorama(options);
this.createBlurArea();
}

/**
* Clear current panorama
*/
App.prototype.clearPanorama = function () {
this.scene.remove(this.panorama);
this.panorama = null;
};

App.prototype.createPanorama = function (options) {
if (Cube3D.validate(options)) {
this.panorama = (new Cube3D(options)).createCube();
} else if (Sphere3D.validate(options)) {
this.panorama = (new Sphere3D(options)).createSphere();
}

if (this.panorama) {
this.scene.add(this.panorama);
}
}

App.prototype.clearBlurArea = function () {
if (this.blurArea != null) {
this.scene.remove(this.blurArea);
this.blurArea = null;
}

this.controller.selectable = [];
}

App.prototype.createBlurArea = function () {
if (this.settings && this.settings.blurArea) {
this.blurArea = new THREE.BlurArea();
if (this.settings.blurArea.static) {
this.blurArea.addStaticMesh(this.settings.blurArea.azimuth, this.settings.blurArea.polar,
this.settings.blurArea.width, this.settings.blurArea.height);
} else {
this.blurArea.addDynamicMesh(this.settings.blurArea.azimuth, this.settings.blurArea.polar,
this.settings.blurArea.width, this.settings.blurArea.height, this.settings.blurArea.offset);
}

this.scene.add(this.blurArea);

// move camera to blur area
if (this.settings.blurArea.focus) {
this.controller.yaw = this.blurArea.getParameters().yaw;
this.controller.pitch = this.blurArea.getParameters().pitch;
}

// add blur area to selectable
if (!this.settings.blurArea.static) {
this.controller.selectable.push(this.blurArea);
}
}
}

/**
* Detects whether the web browser supports WebGL or not
*/
App.prototype.supportsWebGL = function () {
try {
return !!window.WebGLRenderingContext && !!document.createElement('canvas').getContext('experimental-webgl');
} catch (e) {
return false;
}
};

App.prototype.update = function () {
this.controller.update();
this.renderer.render(this.scene, this.camera);
};

App.prototype.draw = function () {
requestAnimationFrame(this.draw.bind(this));
this.update();
};

App.prototype.onWindowResize = function () {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
};

$.fn.panorama = function (options) {
var name = "zhanghaowx-panorama";
var panorama = $(this).data(name);
if (panorama) {
panorama.clearPanorama();
panorama.loadScene(options);
} else {
var panorama = new App(this);
panorama.loadScene(options);
$(this).data(name, panorama);
}
return panorama;
};

return Cube3D;
});
File renamed without changes.
137 changes: 9 additions & 128 deletions src/scene/Cube3D.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
/**
* This library helps to draw a panorama image in web browser.
* It builds on three.js r67. There is no guarantee that it will work with
* other version of three.js.
*/

define([
"geometries/PanoramaCubeGeometry",
"controls/OrbitControls",
"scene/BlurArea",
"core/Object3D",
"core/String",
"three"
Expand All @@ -23,54 +15,24 @@ define([
* middle of this cube. Panoramic images are loaded for all the 6 faces of
* the cube.
*/
var Cube3D = function (mapContainer, options) {
var Cube3D = function (options) {
// create user settings
this.settings = $.extend({
tiles: [], // in order of front,right,back,left,bottom,top
onTilesReady: function () {}, // get notification when all tiles are loaded
showPanorama: true, // visibility of panorama, used for debugging
}, options);

// create a perspective camera
this.camera = new THREE.PerspectiveCamera(75, window.innerWidth / window.innerHeight, 0.1, 1000);

// create a proper renderer
this.renderer = this.supportsWebGL() ? new THREE.WebGLRenderer({
antialias: true,
preserveDrawingBuffer: true // for taking screenshots, it can cause significant performance loss on some platforms
}) : new THREE.CanvasRenderer();
this.renderer.setSize(window.innerWidth, window.innerHeight);

// create a camera controller
this.controller = new THREE.OrbitControls(this.camera, this.renderer.domElement);

// link renderer to a DOM element
this.mapContainer = mapContainer[0];
this.mapContainer.appendChild(this.renderer.domElement);

// create a scene and add needed meshes for displaying panoramas
this.scene = new THREE.Scene();

// bind resize event
window.addEventListener('resize', this.onWindowResize.bind(this), false);

this.draw();
};

/**
* Load panorama image to renderer
*/
Cube3D.prototype.loadScene = function (options) {
$.extend(this.settings, options);
this.createPanorama();
this.createBlurArea();
}

/**
* Check if the configuration is valid and fill some optional (unset) configuration values
*/
Cube3D.prototype.validate = function () {
var tilesPerFace = Math.round(this.settings.tiles.length / 6);
Cube3D.validate = function (options) {
if (!options.tiles) {
console.debug("Skip creating cube: no tiles defined!");
return false;
}

var tilesPerFace = Math.round(options.tiles.length / 6);
switch (tilesPerFace) {
case 0:
break;
Expand All @@ -87,78 +49,13 @@ define([
cubeResolution = 3;
break;
default:
console.error("Fail to create cube: invlaid number of tiles {0}!".format(this.settings.tiles.length));
console.debug("Skip creating cube: invlaid number of tiles {0}!".format(options.tiles.length));
return false;
}

return true;
}

/**
* Clear current panorama
*/
Cube3D.prototype.clearPanorama = function () {
this.scene.remove(this.cube);
this.cube = null;
};

Cube3D.prototype.createPanorama = function () {
if (!this.validate())
return;

this.cube = this.createCube();
this.scene.add(this.cube);
}

Cube3D.prototype.clearBlurArea = function () {
if (this.blurArea != null) {
this.scene.remove(this.blurArea);
this.blurArea = null;
}

this.controller.selectable = [];
}

Cube3D.prototype.createBlurArea = function () {
if (!this.validate())
return;

if (this.settings.blurArea) {
this.blurArea = new THREE.BlurArea();
if (this.settings.blurArea.static) {
this.blurArea.addStaticMesh(this.settings.blurArea.azimuth, this.settings.blurArea.polar,
this.settings.blurArea.width, this.settings.blurArea.height);
} else {
this.blurArea.addDynamicMesh(this.settings.blurArea.azimuth, this.settings.blurArea.polar,
this.settings.blurArea.width, this.settings.blurArea.height, this.settings.blurArea.offset);
}

this.scene.add(this.blurArea);

// move camera to blur area
if (this.settings.blurArea.focus) {
this.controller.yaw = this.blurArea.getParameters().yaw;
this.controller.pitch = this.blurArea.getParameters().pitch;
}

// add blur area to selectable
if (!this.settings.blurArea.static) {
this.controller.selectable.push(this.blurArea);
}
}
}

/**
* Detects whether the web browser supports WebGL or not
*/
Cube3D.prototype.supportsWebGL = function () {
try {
return !!window.WebGLRenderingContext && !!document.createElement('canvas').getContext('experimental-webgl');
} catch (e) {
return false;
}
};

/**
* Load texture for a image tile
*/
Expand Down Expand Up @@ -236,21 +133,5 @@ define([
return new THREE.Mesh(geometry, this.createMaterials());
};

Cube3D.prototype.update = function () {
this.controller.update();
this.renderer.render(this.scene, this.camera);
};

Cube3D.prototype.draw = function () {
requestAnimationFrame(this.draw.bind(this));
this.update();
};

Cube3D.prototype.onWindowResize = function () {
this.camera.aspect = window.innerWidth / window.innerHeight;
this.camera.updateProjectionMatrix();
this.renderer.setSize(window.innerWidth, window.innerHeight);
};

return Cube3D;
});
Loading

0 comments on commit ca3a8ed

Please sign in to comment.