Skip to content
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 5 commits into from
Feb 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 21 additions & 16 deletions src/components/index.js
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');
5 changes: 3 additions & 2 deletions src/components/look-controls.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module.exports.Component = registerComponent('look-controls', {
},

addEventListeners: function () {
var canvasEl = document.querySelector('a-scene').canvas;
var canvasEl = this.el.sceneEl.canvas;

// Mouse Events
canvasEl.addEventListener('mousedown', this.onMouseDown, false);
Expand Down Expand Up @@ -223,7 +223,8 @@ module.exports.Component = registerComponent('look-controls', {
var deltaY;
var yawObject = this.yawObject;
if (!this.touchStarted) { return; }
deltaY = 2 * Math.PI * (e.touches[0].pageX - this.touchStart.x) / this.canvasEl.clientWidth;
deltaY = 2 * Math.PI * (e.touches[0].pageX - this.touchStart.x) /
this.el.sceneEl.canvas.clientWidth;
// Limits touch orientaion to to yaw (y axis)
yawObject.rotation.y -= deltaY * 0.5;
this.touchStart = {
Expand Down
45 changes: 45 additions & 0 deletions src/components/scene/canvas.js
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
});
}
});
73 changes: 73 additions & 0 deletions src/components/scene/fog.js
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;
}
35 changes: 35 additions & 0 deletions src/components/scene/keyboard-shortcuts.js
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');
Copy link
Member

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.

Copy link
Member Author

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.


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);
}
});
55 changes: 55 additions & 0 deletions src/components/scene/stats.js
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');
Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member Author

Choose a reason for hiding this comment

The 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' ] }
]
});
}
Loading