-
Notifications
You must be signed in to change notification settings - Fork 116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make 3D preview logic load from generated JSON file. #274
Merged
MiiBond
merged 7 commits into
box:master
from
MiiBond:1029-build-3d-preview-as-studio-app
Aug 6, 2017
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
844eb06
New: 3D preview logic now loaded from generated json file
78830ca
Docs: Add missing jsdoc comments
2fa7650
Update: Add Box3D Studio project files
a118419
Fix: Set pivot position of orbit camera after reset
71479ff
Merge branch 'master' of github.com:box/box-content-preview into 1029…
22532b1
Fix: Add fetch and fix spacing
9bbf131
Merge branch 'master' into 1029-build-3d-preview-as-studio-app
MiiBond File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,16 @@ | ||
/* global Box3D */ | ||
/* eslint no-param-reassign:0 */ | ||
import 'whatwg-fetch'; | ||
import EventEmitter from 'events'; | ||
import { | ||
EVENT_SHOW_VR_BUTTON, | ||
EVENT_SCENE_LOADED, | ||
EVENT_TRIGGER_RENDER, | ||
EVENT_WEBGL_CONTEXT_RESTORED, | ||
EVENT_WEBGL_CONTEXT_LOST | ||
} from './box3DConstants'; | ||
import { MODEL3D_STATIC_ASSETS_VERSION } from '../../constants'; | ||
|
||
const PREVIEW_CAMERA_CONTROLLER_ID = 'orbit_camera'; | ||
const PREVIEW_CAMERA_POSITION = { x: 0, y: 0, z: 0 }; | ||
const PREVIEW_CAMERA_QUATERNION = { x: 0, y: 0, z: 0, w: 1 }; | ||
const OCULUS_TOUCH_LEFT = 'oculusTouchLeft'; | ||
const OCULUS_TOUCH_RIGHT = 'oculusTouchRight'; | ||
const HTC_VIVE = 'viveController'; | ||
|
@@ -50,10 +48,10 @@ class Box3DRenderer extends EventEmitter { | |
boxSdk; | ||
|
||
/** @property {Object} - Default X, Y, Z position for the scene camera in 3D space */ | ||
defaultCameraPosition = PREVIEW_CAMERA_POSITION; | ||
savedCameraPosition; | ||
|
||
/** @property {Object} - Default X, Y, Z, W quaternion for the scene camera in 3D space */ | ||
defaultCameraQuaternion = PREVIEW_CAMERA_QUATERNION; | ||
savedCameraQuaternion; | ||
|
||
/** @property {Object} - Mapping of promises that each resolve when it's controller model file is loaded */ | ||
vrGamepadLoadPromises = {}; | ||
|
@@ -77,7 +75,6 @@ class Box3DRenderer extends EventEmitter { | |
|
||
this.containerEl = containerEl; | ||
this.boxSdk = boxSdk; | ||
this.on(EVENT_TRIGGER_RENDER, this.handleOnRender); | ||
|
||
this.handleContextLost = this.handleContextLost.bind(this); | ||
this.handleContextRestored = this.handleContextRestored.bind(this); | ||
|
@@ -106,8 +103,6 @@ class Box3DRenderer extends EventEmitter { | |
* @return {void} | ||
*/ | ||
destroy() { | ||
this.removeListener(EVENT_TRIGGER_RENDER, this.handleOnRender); | ||
|
||
if (!this.box3d) { | ||
return; | ||
} | ||
|
@@ -142,16 +137,12 @@ class Box3DRenderer extends EventEmitter { | |
|
||
// Reset camera settings to default. | ||
if (camera) { | ||
camera.setPosition( | ||
this.defaultCameraPosition.x, | ||
this.defaultCameraPosition.y, | ||
this.defaultCameraPosition.z | ||
); | ||
camera.setPosition(this.savedCameraPosition.x, this.savedCameraPosition.y, this.savedCameraPosition.z); | ||
camera.setQuaternion( | ||
this.defaultCameraQuaternion.x, | ||
this.defaultCameraQuaternion.y, | ||
this.defaultCameraQuaternion.z, | ||
this.defaultCameraQuaternion.w | ||
this.savedCameraQuaternion.x, | ||
this.savedCameraQuaternion.y, | ||
this.savedCameraQuaternion.z, | ||
this.savedCameraQuaternion.w | ||
); | ||
} | ||
} | ||
|
@@ -162,7 +153,7 @@ class Box3DRenderer extends EventEmitter { | |
* @return {Box3DEntity} The camera instance | ||
*/ | ||
getCamera() { | ||
return this.box3d ? this.box3d.getObjectById('CAMERA_ID') : null; | ||
return this.box3d ? this.box3d.getObjectByClass(Box3D.CameraObject) : null; | ||
} | ||
|
||
/** | ||
|
@@ -171,7 +162,7 @@ class Box3DRenderer extends EventEmitter { | |
* @return {SceneObject} The scene object | ||
*/ | ||
getScene() { | ||
return this.box3d ? this.box3d.getEntityById('SCENE_ID') : null; | ||
return this.box3d ? this.box3d.getObjectByClass(Box3D.SceneObject) : null; | ||
} | ||
|
||
/** | ||
|
@@ -208,6 +199,15 @@ class Box3DRenderer extends EventEmitter { | |
return Promise.resolve(xhr); | ||
} | ||
|
||
/** | ||
* Load Box3D entities from a JSON file specified with the provided path. | ||
* @param {string} url - A path to a JSON file containing Box3D entity descriptions. | ||
* @return {Promise} - A promise that resolves on completion of the load. | ||
*/ | ||
getEntitiesFromUrl(url) { | ||
return fetch(url).then((response) => response.json()); | ||
} | ||
|
||
/** | ||
* Initialize the Box3D engine. | ||
* | ||
|
@@ -216,6 +216,7 @@ class Box3DRenderer extends EventEmitter { | |
* @param {string} [options.apiHost] - API URL base to make requests to | ||
* @param {Object|null} [options.file] - Information about the current box file we're using. | ||
* Used to get the parent.id of the box file. | ||
* @param {Object|string} [options.box3dApplication] - Path to a json file published from Box3D Studio (or the json itself). | ||
* @return {Promise} A promise that resolves with the created box3d | ||
*/ | ||
initBox3d(options = {}) { | ||
|
@@ -229,32 +230,51 @@ class Box3DRenderer extends EventEmitter { | |
} | ||
|
||
const resourceLoader = new Box3D.XhrResourceLoader(this.configureXHR.bind(this, options)); | ||
const json = options && options.box3dApplication ? options.box3dApplication : {}; | ||
let getApplication = Promise.resolve([]); | ||
if (typeof json === 'object') { | ||
getApplication = Promise.resolve(json); | ||
} else if (typeof json === 'string') { | ||
getApplication = this.getEntitiesFromUrl(json); | ||
} | ||
|
||
return this.createBox3d(resourceLoader, options.sceneEntities); | ||
return getApplication.then((applicationEntities) => | ||
this.createBox3d(resourceLoader, options.sceneEntities, applicationEntities.entities, `${options.apiHost}`) | ||
); | ||
} | ||
|
||
/** | ||
* Create a new Box3D engine. | ||
* | ||
* @param {Object} resourceLoader - The resource loader used to load assets used by the box3d engine | ||
* @param {Array} [sceneEntities] - The descriptor of the default scene. See ./scene-entities.js | ||
* @param {Object} [inputSettings] - Config for the input controller of the Box3D Engine | ||
* @param {Array} [applicationEntities] - Array of entities published from Box3D Studio project. | ||
* @param {string} [apiBase] - Optional base path for Box API calls. | ||
* @return {Promise} A promise that resolves with the Box3D Engine. | ||
*/ | ||
createBox3d(resourceLoader, sceneEntities) { | ||
createBox3d(resourceLoader, sceneEntities, applicationEntities, apiBase) { | ||
const box3d = new Box3D.Engine({ | ||
container: this.containerEl, | ||
engineName: 'Default', | ||
resourceLoader | ||
resourceLoader, | ||
apiBase | ||
}); | ||
if (box3d.canvas) { | ||
box3d.canvas.addEventListener('webglcontextlost', this.handleContextLost); | ||
box3d.canvas.addEventListener('webglcontextrestored', this.handleContextRestored); | ||
} | ||
|
||
return new Promise((resolve) => { | ||
if (applicationEntities) { | ||
box3d.addEntities(applicationEntities); | ||
} | ||
|
||
let app = box3d.getAssetByClass(Box3D.ApplicationAsset); | ||
box3d.addEntities(sceneEntities); | ||
const app = box3d.getAssetById('APP_ASSET_ID'); | ||
if (!app) { | ||
app = box3d.getAssetByClass(Box3D.ApplicationAsset); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. newline after the |
||
|
||
app.load(); | ||
this.box3d = box3d; | ||
resolve(this.box3d); | ||
|
@@ -290,6 +310,11 @@ class Box3DRenderer extends EventEmitter { | |
* @return {void} | ||
*/ | ||
onSceneLoad() { | ||
const camera = this.getCamera(); | ||
if (camera) { | ||
this.savedCameraPosition = camera.getPosition(); | ||
this.savedCameraQuaternion = camera.getQuaternion(); | ||
} | ||
// Reset the scene. | ||
this.reset(); | ||
this.emit(EVENT_SCENE_LOADED); | ||
|
@@ -366,18 +391,6 @@ class Box3DRenderer extends EventEmitter { | |
this.box3d.trigger('disableVrRendering'); | ||
} | ||
|
||
/** | ||
* Trigger an update and render event on the runtime. | ||
* | ||
* @return {void} | ||
*/ | ||
handleOnRender() { | ||
if (!this.box3d) { | ||
return; | ||
} | ||
this.box3d.trigger('render'); | ||
} | ||
|
||
/** | ||
* Call the onResize of the engine. | ||
* | ||
|
@@ -430,6 +443,10 @@ class Box3DRenderer extends EventEmitter { | |
|
||
const app = this.box3d.getApplication(); | ||
const vrPresenter = app.getComponentByScriptId('vr_presenter'); | ||
if (!vrPresenter) { | ||
return; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. newline |
||
|
||
vrPresenter.whenDisplaysAvailable((displays) => { | ||
if (displays.length) { | ||
this.emit(EVENT_SHOW_VR_BUTTON); | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
newline after the
if
:D