Skip to content

Commit

Permalink
add uniform for time to shaders (and some commented out test code for…
Browse files Browse the repository at this point in the history
… using it to alter vertices and pixels)
  • Loading branch information
bcamper committed Jan 14, 2014
1 parent 9158907 commit bc5ae2a
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
5 changes: 3 additions & 2 deletions fragment.glsl
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
uniform vec2 resolution;
// uniform vec2 map_center;
uniform float time;

varying vec3 fcolor;

Expand All @@ -17,8 +17,9 @@ void main (void) {
// vec3 color = fcolor * max(1.0 - distance(position, vec2(0.0, 0.0)), 0.15);
// vec3 color = fcolor * (1.0 - dot(normalize(vec3(rand(gl_FragCoord.xy * 0.01) * 10.0, 0.0, -1.0)), vec3(0, 0, 1.0)));

// Mutate colors by screen position
// Mutate colors by screen position or time
// color += vec3(gl_FragCoord.x / resolution.x, 0.0, gl_FragCoord.y / resolution.y);
// color.r += sin(time);

gl_FragColor = vec4(color, 1.0);
// gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
Expand Down
2 changes: 2 additions & 0 deletions gl_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ GLRenderer.prototype.init = function GLRendererInit ()
this.zoom_step = 0.02; // for fractional zoom user adjustment
this.map_last_zoom = this.leaflet.map.getZoom();
this.map_zooming = false;
this.start_time = +new Date();

this.initMapHandlers();
this.initInputHandlers();
Expand Down Expand Up @@ -815,6 +816,7 @@ GLRenderer.prototype.render = function GLRendererRender ()

// Set values to this.program variables
gl.uniform2f(gl.getUniformLocation(this.program, 'resolution'), gl.canvas.width, gl.canvas.height);
gl.uniform1f(gl.getUniformLocation(this.program, 'time'), ((+new Date()) - this.start_time) / 1000);

var center = this.leaflet.map.getCenter(); // TODO: move map center tracking/projection to central class?
center = Geo.latLngToMeters(Point(center.lng, center.lat));
Expand Down
6 changes: 3 additions & 3 deletions vector_renderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ function VectorRenderer (leaflet, layers, styles)

this.tile_scale = 4096;
this.tiles = {};

this.leaflet = leaflet;

if (typeof(layers) == 'string') {
Expand Down Expand Up @@ -81,7 +81,7 @@ VectorRenderer.prototype.loadTile = function (coords, div)
// Re-scale from meters to local tile coords
renderer.scaleTile(tile, renderer.tile_scale);

tile.xhr = null;
tile.xhr = null;
tile.loading = false;
tile.loaded = true;

Expand Down Expand Up @@ -166,7 +166,7 @@ VectorRenderer.prototype.scaleTile = function (tile, scale)
VectorRenderer.prototype.printDebugForTile = function (tile)
{
console.log(
"debug for " + tile.key + ': [ ' +
"debug for " + tile.key + ': [ ' +
Object.keys(tile.debug).map(function (t) { return t + ': ' + tile.debug[t]; }).join(', ') + ' ]'
);
};
11 changes: 10 additions & 1 deletion vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ uniform vec2 meter_zoom;
uniform vec2 tile_min;
uniform vec2 tile_max;
uniform float tile_scale; // geometries are scaled to this range within each tile
// uniform float time;
uniform float time;

attribute vec3 position;
attribute vec3 normal;
Expand Down Expand Up @@ -41,6 +41,14 @@ void main() {
vposition.y *= -1.0; // adjust for flipped y-coords
// vposition.y += tile_scale; // alternate, to also adjust for force-positive y coords in tile
vposition.xy *= (tile_max - tile_min) / tile_scale; // adjust for vertex location within tile (scaled from local coords to meters)

// Vertex displacement tests
// if (vposition.z > 1.0) {
// // vposition.x += sin(vposition.z + time) * 10.0 * sin(position.x); // swaying buildings
// // vposition.y += cos(vposition.z + time) * 10.0;
// vposition.z *= (sin(vposition.z / 25.0 * time) + 1.0) / 2.0 + 0.1; // evelator buildings
// }

vposition.xy += tile_min.xy - map_center; // adjust for corner of tile relative to map center

// Isometric-style projections
Expand All @@ -53,6 +61,7 @@ void main() {

// Flat shading between surface normal and light
fcolor = color;
// fcolor += vec3(sin(position.z + time), 0.0, 0.0); // color change on height + time
light = vec3(-0.25, -0.25, 0.35); // vec3(0.1, 0.1, 0.35); // point light location
light = normalize(vec3(vposition.x, vposition.y, -vposition.z) - light); // light angle from light point to vertex
fcolor *= dot(vnormal, light * -1.0) + ambient + clamp(vposition.z * 2.0 / meter_zoom.x, 0.0, 0.25);
Expand Down

0 comments on commit bc5ae2a

Please sign in to comment.