Skip to content

Commit

Permalink
Merge pull request #189 from tangrams/scene-includes
Browse files Browse the repository at this point in the history
Preparation for scene `include` functionality
  • Loading branch information
bcamper committed Sep 18, 2015
2 parents fc7875d + 702da01 commit 6853a95
Show file tree
Hide file tree
Showing 12 changed files with 460 additions and 205 deletions.
2 changes: 2 additions & 0 deletions demos/scene.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# include: transit.yaml

cameras:
perspective:
type: perspective
Expand Down
31 changes: 31 additions & 0 deletions demos/transit.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
layers:
transit:
data: { source: osm }
draw:
lines:
order: 49
color: gray
width: 4px
outline:
color: black
width: 1px
interactive: true

colored:
filter: { colour: true }
draw:
lines:
color: |
function() {
function hexToRgb(hex) {
var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
return result ? {
r: parseInt(result[1], 16),
g: parseInt(result[2], 16),
b: parseInt(result[3], 16)
} : null;
}
var c = hexToRgb(feature.colour);
return [c.r/255, c.g/255, c.b/255];
}
31 changes: 23 additions & 8 deletions src/gl/glsl.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ GLSL.parseUniforms = function (uniforms, prefix = null) {
var parsed = [];

for (var name in uniforms) {
var key = name; // save the original name
var uniform = uniforms[name];
var u;

Expand All @@ -33,8 +34,10 @@ GLSL.parseUniforms = function (uniforms, prefix = null) {
parsed.push({
type: 'float',
method: '1f',
name, value:
uniform
name,
value: uniform,
key,
uniforms
});
}
// Array: vector, array of floats, array of textures, or array of structs
Expand All @@ -47,7 +50,9 @@ GLSL.parseUniforms = function (uniforms, prefix = null) {
type: 'vec' + uniform.length,
method: uniform.length + 'fv',
name,
value: uniform
value: uniform,
key,
uniforms
});
}
// float array
Expand All @@ -56,7 +61,9 @@ GLSL.parseUniforms = function (uniforms, prefix = null) {
type: 'float[]',
method: '1fv',
name: name + '[0]',
value: uniform
value: uniform,
key,
uniforms
});
}
// TODO: assume matrix for (typeof == Float32Array && length == 16)?
Expand All @@ -68,7 +75,9 @@ GLSL.parseUniforms = function (uniforms, prefix = null) {
type: 'sampler2D',
method: '1i',
name: name + '[' + u + ']',
value: uniform[u]
value: uniform[u],
key: u,
uniforms: uniform
});
}
}
Expand All @@ -82,7 +91,9 @@ GLSL.parseUniforms = function (uniforms, prefix = null) {
type: 'vec' + uniform[0].length,
method: uniform[u].length + 'fv',
name: name + '[' + u + ']',
value: uniform[u]
value: uniform[u],
key: u,
uniforms: uniform
});
}
}
Expand All @@ -102,7 +113,9 @@ GLSL.parseUniforms = function (uniforms, prefix = null) {
type: 'bool',
method: '1i',
name,
value: uniform
value: uniform,
key,
uniforms
});
}
// Texture
Expand All @@ -111,7 +124,9 @@ GLSL.parseUniforms = function (uniforms, prefix = null) {
type: 'sampler2D',
method: '1i',
name,
value: uniform
value: uniform,
key,
uniforms
});
}
// Structure
Expand Down
72 changes: 13 additions & 59 deletions src/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import Texture from './gl/texture';
import VertexArrayObject from './gl/vao';
import {StyleManager} from './styles/style_manager';
import {StyleParser} from './styles/style_parser';
import SceneLoader from './scene_loader';
import Camera from './camera';
import Light from './light';
import TileManager from './tile_manager';
Expand Down Expand Up @@ -118,7 +119,7 @@ export default class Scene {

// Load (or reload) scene config
// Optionally specify new scene file URL
load(config_source = null) {
load(config_source = null, config_path = null) {
if (this.initializing) {
return Promise.resolve();
}
Expand All @@ -128,7 +129,7 @@ export default class Scene {
this.initializing = true;

// Load scene definition (sources, styles, etc.), then create styles & workers
return this.loadScene(config_source)
return this.loadScene(config_source, config_path)
.then(() => this.createWorkers())
.then(() => {
this.createCanvas();
Expand All @@ -150,6 +151,7 @@ export default class Scene {
this.initializing = false;
this.initialized = true;
this.last_valid_config_source = this.config_source;
this.last_valid_config_path = this.config_path;

if (this.render_loop !== false) {
this.setupRenderLoop();
Expand All @@ -174,16 +176,16 @@ export default class Scene {
if (this.last_valid_config_source) {
log.warn(message, error);
log.info(`Scene.load() reverting to last valid configuration`);
return this.load(this.last_valid_config_source);
return this.load(this.last_valid_config_source, this.last_valid_config_path);
}
log.error(message, error);
throw error;
});
}

// For API compatibility
reload(config_source = null) {
return this.load(config_source);
reload(config_source = null, config_path = null) {
return this.load(config_source, config_path);
}

destroy() {
Expand Down Expand Up @@ -930,26 +932,26 @@ export default class Scene {
Load (or reload) the scene config
@return {Promise}
*/
loadScene(config_source = null) {
loadScene(config_source = null, config_path = null) {
this.config_source = config_source || this.config_source;

if (typeof this.config_source === 'string') {
this.config_path = Utils.pathForURL(this.config_source);
this.config_path = config_path || Utils.pathForURL(this.config_source);
}
else {
this.config_path = null;
}
Texture.base_url = this.config_path;

return Utils.loadResource(this.config_source).then((config) => {
return SceneLoader.loadScene(this.config_source, this.config_path).then(config => {
this.config = config;
return this.preProcessConfig().then(() => { this.trigger('load', { config: this.config }); });
this.trigger('load', { config: this.config });
return this.config;
});
}

loadDataSources() {
for (var name in this.config.sources) {
let source = this.config.sources[name];
source.url = Utils.addBaseURL(source.url);
this.sources[name] = DataSource.create(Object.assign({}, source, {name}));

if (!this.sources[name]) {
Expand All @@ -960,59 +962,11 @@ export default class Scene {
}
}

// Normalize some settings that may not have been explicitly specified in the scene definition
preProcessConfig() {
// Assign ids to data sources
let source_id = 0;
for (let source in this.config.sources) {
this.config.sources[source].id = source_id++;
}

// If only one camera specified, set it as default
this.config.cameras = this.config.cameras || {};
if (this.config.camera) {
this.config.cameras.default = this.config.camera;
}
let camera_names = Object.keys(this.config.cameras);
if (camera_names.length === 0) {
this.config.cameras.default = { active: true };

}
else if (!this._active_camera) {
// If no camera set as active, use first one
this.config.cameras[camera_names[0]].active = true;
}

this.config.lights = this.config.lights || {}; // ensure lights object
this.config.styles = this.config.styles || {}; // ensure styles object

return StyleManager.preload(this.config.styles, this.config_path);
}

// Load all textures in the scene definition
loadTextures() {
this.normalizeTextures();
return Texture.createFromObject(this.gl, this.config.textures);
}

// Handle single or multi-texture syntax, for stylesheet convenience
normalizeTextures() {
if (!this.config.styles) {
return;
}

for (let [style_name, style] of Utils.entries(this.config.styles)) {
// If style has a single 'texture' object, move it to the global scene texture set
// and give it a default name
if (style.texture && typeof style.texture === 'object') {
let texture_name = '__' + style_name;
this.config.textures = this.config.textures || {};
this.config.textures[texture_name] = style.texture;
style.texture = texture_name; // point stlye to location of texture
}
}
}

// Called (currently manually) after styles are updated in stylesheet
updateStyles() {
if (!this.initialized && !this.initializing) {
Expand Down
Loading

0 comments on commit 6853a95

Please sign in to comment.