-
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
Changes from 3 commits
844eb06
78830ca
2fa7650
a118419
71479ff
22532b1
9bbf131
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,15 +4,12 @@ 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 +47,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 +74,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 +102,6 @@ class Box3DRenderer extends EventEmitter { | |
* @return {void} | ||
*/ | ||
destroy() { | ||
this.removeListener(EVENT_TRIGGER_RENDER, this.handleOnRender); | ||
|
||
if (!this.box3d) { | ||
return; | ||
} | ||
|
@@ -142,16 +136,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 +152,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 +161,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 +198,32 @@ 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 new Promise((resolve, reject) => { | ||
const xhr = new XMLHttpRequest(); | ||
|
||
xhr.open('GET', url); | ||
|
||
/** | ||
* Callback for xhr completion. | ||
* @return {void} | ||
*/ | ||
const complete = () => { | ||
resolve(JSON.parse(xhr.responseText)); | ||
}; | ||
|
||
xhr.addEventListener('load', complete); | ||
xhr.addEventListener('error', reject); | ||
|
||
xhr.send(); | ||
}); | ||
} | ||
|
||
/** | ||
* Initialize the Box3D engine. | ||
* | ||
|
@@ -216,6 +232,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 +246,49 @@ 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); | ||
} | ||
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 |
||
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 +324,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 +405,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 +457,9 @@ 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); | ||
|
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.
You should import
whatwg-fetch
and then use the fetch api :)