-
-
Notifications
You must be signed in to change notification settings - Fork 4k
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
move scene logic to components (fixes #239, #759, #886) #776
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
f8e29e3
make <a-scene> default components visible on DOM
ngokevin 8b5d995
refactor scene code to components (canvas, fullscreen metatags, stats…
ngokevin 4699078
fix AEntity.is for when state is 0th index
ngokevin 285d4c6
leave entity defaults
ngokevin 7d1ab4c
move scene components back to modules
ngokevin 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,21 @@ | ||
require('../components/camera'); | ||
require('../components/cursor'); | ||
require('../components/fog'); | ||
require('../components/geometry'); | ||
require('../components/light'); | ||
require('../components/loader'); | ||
require('../components/look-at'); | ||
require('../components/look-controls'); | ||
require('../components/material'); | ||
require('../components/position'); | ||
require('../components/raycaster'); | ||
require('../components/rotation'); | ||
require('../components/scale'); | ||
require('../components/sound'); | ||
require('../components/visible'); | ||
require('../components/wasd-controls'); | ||
require('./camera'); | ||
require('./cursor'); | ||
require('./geometry'); | ||
require('./light'); | ||
require('./loader'); | ||
require('./look-at'); | ||
require('./look-controls'); | ||
require('./material'); | ||
require('./position'); | ||
require('./raycaster'); | ||
require('./rotation'); | ||
require('./scale'); | ||
require('./sound'); | ||
require('./visible'); | ||
require('./wasd-controls'); | ||
|
||
require('./scene/canvas'); | ||
require('./scene/fog'); | ||
require('./scene/keyboard-shortcuts'); | ||
require('./scene/stats'); | ||
require('./scene/vr-mode-ui'); |
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 |
---|---|---|
@@ -0,0 +1,45 @@ | ||
var register = require('../../core/component').registerComponent; | ||
|
||
module.exports.Component = register('canvas', { | ||
schema: { | ||
canvas: { | ||
type: 'selector', | ||
default: undefined | ||
}, | ||
height: { | ||
default: 100 | ||
}, | ||
width: { | ||
default: 100 | ||
} | ||
}, | ||
|
||
update: function () { | ||
var data = this.data; | ||
var canvas = data.canvas; | ||
var scene = this.el; | ||
|
||
// No updating canvas. | ||
if (scene.canvas) { return; } | ||
|
||
// Inject canvas if one not specified with height and width. | ||
if (!canvas) { | ||
canvas = document.createElement('canvas'); | ||
canvas.classList.add('a-canvas'); | ||
canvas.style.height = data.height + '%'; | ||
canvas.style.width = data.width + '%'; | ||
scene.appendChild(canvas); | ||
} | ||
|
||
// Prevent overscroll on mobile. | ||
canvas.addEventListener('touchmove', function (event) { | ||
event.preventDefault(); | ||
}); | ||
|
||
// Set canvas on scene. | ||
scene.canvas = canvas; | ||
scene.emit('render-target-loaded', { | ||
target: canvas | ||
}); | ||
} | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,73 @@ | ||
var register = require('../../core/component').registerComponent; | ||
var THREE = require('../../lib/three'); | ||
var debug = require('../../utils/debug'); | ||
|
||
var warn = debug('components:fog:warn'); | ||
|
||
/** | ||
* Fog component. | ||
* Applies only to the scene entity. | ||
*/ | ||
module.exports.Component = register('fog', { | ||
schema: { | ||
color: { default: '#000' }, | ||
density: { default: 0.00025 }, | ||
far: { default: 1000, min: 0 }, | ||
near: { default: 1, min: 0 }, | ||
type: { default: 'linear', oneOf: ['linear', 'exponential'] } | ||
}, | ||
|
||
update: function () { | ||
var data = this.data; | ||
var el = this.el; | ||
var fog = this.el.object3D.fog; | ||
|
||
if (!el.isScene) { | ||
warn('Fog component can only be applied to <a-scene>'); | ||
return; | ||
} | ||
|
||
// (Re)create fog if fog doesn't exist or fog type changed. | ||
if (!fog || data.type !== fog.name) { | ||
el.object3D.fog = getFog(data); | ||
el.updateMaterials(); | ||
return; | ||
} | ||
|
||
// Fog data changed. Update fog. | ||
Object.keys(this.schema).forEach(function (key) { | ||
var value = data[key]; | ||
if (key === 'color') { value = new THREE.Color(value); } | ||
fog[key] = value; | ||
}); | ||
}, | ||
|
||
/** | ||
* Remove fog on remove (callback). | ||
*/ | ||
remove: function () { | ||
var fog = this.el.object3D.fog; | ||
if (fog) { | ||
fog.density = 0; | ||
fog.far = 0; | ||
fog.near = 0; | ||
} | ||
} | ||
}); | ||
|
||
/** | ||
* Creates a fog object. Sets fog.name to be able to detect fog type changes. | ||
* | ||
* @param {object} data - Fog data. | ||
* @returns {object} fog | ||
*/ | ||
function getFog (data) { | ||
var fog; | ||
if (data.type === 'exponential') { | ||
fog = new THREE.FogExp2(data.color, data.density); | ||
} else { | ||
fog = new THREE.Fog(data.color, data.near, data.far); | ||
} | ||
fog.name = data.type; | ||
return fog; | ||
} |
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 |
---|---|---|
@@ -0,0 +1,35 @@ | ||
var registerComponent = require('../../core/component').registerComponent; | ||
var THREE = require('../../lib/three'); | ||
|
||
var controls = new THREE.VRControls(new THREE.Object3D()); | ||
|
||
module.exports.Component = registerComponent('keyboard-shortcuts', { | ||
schema: { | ||
enterVR: { default: true }, | ||
resetSensor: { default: true } | ||
}, | ||
|
||
init: function () { | ||
var self = this; | ||
var scene = this.el; | ||
|
||
this.listener = window.addEventListener('keyup', function (event) { | ||
if (self.enterVREnabled && event.keyCode === 70) { // f. | ||
scene.enterVR(); | ||
} | ||
if (self.resetSensorEnabled && event.keyCode === 90) { // z. | ||
controls.resetSensor(); | ||
} | ||
}, false); | ||
}, | ||
|
||
update: function (oldData) { | ||
var data = this.data; | ||
this.enterVREnabled = data.enterVR; | ||
this.resetSensorEnabled = data.resetSensor; | ||
}, | ||
|
||
remove: function () { | ||
window.removeEventListener('keyup', this.listener); | ||
} | ||
}); |
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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
var registerComponent = require('../../core/component').registerComponent; | ||
var RStats = require('../../../vendor/rStats'); | ||
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. Stats are a a dev tool so it's nice to make them optional/configurable 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. yeah it's optional. if you want it, attach it to the scene. else, detach it. |
||
|
||
var HIDDEN_CLASS = 'a-hidden'; | ||
|
||
/** | ||
* Stats appended to document.body by RStats. | ||
*/ | ||
module.exports.Component = registerComponent('stats', { | ||
init: function () { | ||
var scene = this.el; | ||
|
||
this.stats = createStats(); | ||
this.statsEl = document.querySelector('.rs-base'); | ||
|
||
this.hideBound = this.hide.bind(this); | ||
this.showBound = this.show.bind(this); | ||
|
||
scene.addEventListener('enter-vr', this.hideBound); | ||
scene.addEventListener('exit-vr', this.showBound); | ||
}, | ||
|
||
remove: function () { | ||
this.el.removeEventListener('enter-vr', this.hideBound); | ||
this.el.removeEventListener('exit-vr', this.showBound); | ||
this.statsEl.parentNode.removeChild(this.statsEl); | ||
}, | ||
|
||
tick: function () { | ||
var stats = this.stats; | ||
stats('rAF').tick(); | ||
stats('FPS').frame(); | ||
stats().update(); | ||
}, | ||
|
||
hide: function () { | ||
this.statsEl.classList.add(HIDDEN_CLASS); | ||
}, | ||
|
||
show: function () { | ||
this.statsEl.classList.remove(HIDDEN_CLASS); | ||
} | ||
}); | ||
|
||
function createStats () { | ||
return new RStats({ | ||
CSSPath: '../../style/', | ||
values: { | ||
fps: { caption: 'fps', below: 30 } | ||
}, | ||
groups: [ | ||
{ caption: 'Framerate', values: [ 'fps', 'raf' ] } | ||
] | ||
}); | ||
} |
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.
Do we want to make this non configurable as well? It would be good for to keep these basic keyboard shortcuts consistent across all aframe experiences.
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.
I was trying to make a text editor, and I couldn't input the letter "f" so I really wanted a way to turn that off.