From bc5ae2a41902bf0a672f415b7598d598b2059803 Mon Sep 17 00:00:00 2001 From: Brett Camper Date: Mon, 13 Jan 2014 22:48:04 -0500 Subject: [PATCH] add uniform for time to shaders (and some commented out test code for using it to alter vertices and pixels) --- fragment.glsl | 5 +++-- gl_renderer.js | 2 ++ vector_renderer.js | 6 +++--- vertex.glsl | 11 ++++++++++- 4 files changed, 18 insertions(+), 6 deletions(-) diff --git a/fragment.glsl b/fragment.glsl index f0e8acb33..bf62a7bba 100755 --- a/fragment.glsl +++ b/fragment.glsl @@ -1,5 +1,5 @@ uniform vec2 resolution; -// uniform vec2 map_center; +uniform float time; varying vec3 fcolor; @@ -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); diff --git a/gl_renderer.js b/gl_renderer.js index 90c523585..712c4a342 100644 --- a/gl_renderer.js +++ b/gl_renderer.js @@ -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(); @@ -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)); diff --git a/vector_renderer.js b/vector_renderer.js index daad160ef..5a1a12fda 100644 --- a/vector_renderer.js +++ b/vector_renderer.js @@ -8,7 +8,7 @@ function VectorRenderer (leaflet, layers, styles) this.tile_scale = 4096; this.tiles = {}; - + this.leaflet = leaflet; if (typeof(layers) == 'string') { @@ -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; @@ -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(', ') + ' ]' ); }; diff --git a/vertex.glsl b/vertex.glsl index 83157db8b..ce87e658c 100644 --- a/vertex.glsl +++ b/vertex.glsl @@ -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; @@ -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 @@ -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);