Skip to content

Commit

Permalink
Type pattern.js
Browse files Browse the repository at this point in the history
  • Loading branch information
jfirebaugh committed Aug 2, 2017
1 parent 0d69fcf commit 5a263b1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 16 deletions.
34 changes: 23 additions & 11 deletions src/render/pattern.js
Original file line number Diff line number Diff line change
@@ -1,44 +1,56 @@
// @flow

const assert = require('assert');

const pixelsToTileUnits = require('../source/pixels_to_tile_units');

import type Painter from './painter';
import type {Program} from '../data/program_configuration';
import type TileCoord from '../source/tile_coord';

type CrossFaded<T> = {
from: T,
to: T,
fromScale: number,
toScale: number,
t: number
};

/**
* Checks whether a pattern image is needed, and if it is, whether it is not loaded.
*
* @returns {boolean} true if a needed image is missing and rendering needs to be skipped.
* @returns true if a needed image is missing and rendering needs to be skipped.
*/
exports.isPatternMissing = function(image, painter) {
exports.isPatternMissing = function(image: CrossFaded<string>, painter: Painter): boolean {
if (!image) return false;
const imagePosA = painter.spriteAtlas.getPattern(image.from);
const imagePosB = painter.spriteAtlas.getPattern(image.to);
return !imagePosA || !imagePosB;
};

exports.prepare = function (image, painter, program) {
exports.prepare = function (image: CrossFaded<string>, painter: Painter, program: Program) {
const gl = painter.gl;

const imagePosA = painter.spriteAtlas.getPattern(image.from);
const imagePosB = painter.spriteAtlas.getPattern(image.to);
assert(imagePosA && imagePosB);

gl.uniform1i(program.u_image, 0);
gl.uniform2fv(program.u_pattern_tl_a, imagePosA.tl);
gl.uniform2fv(program.u_pattern_br_a, imagePosA.br);
gl.uniform2fv(program.u_pattern_tl_b, imagePosB.tl);
gl.uniform2fv(program.u_pattern_br_b, imagePosB.br);
gl.uniform2fv(program.u_pattern_tl_a, (imagePosA : any).tl);
gl.uniform2fv(program.u_pattern_br_a, (imagePosA: any).br);
gl.uniform2fv(program.u_pattern_tl_b, (imagePosB: any).tl);
gl.uniform2fv(program.u_pattern_br_b, (imagePosB: any).br);
gl.uniform2fv(program.u_texsize, painter.spriteAtlas.getPixelSize());
gl.uniform1f(program.u_mix, image.t);
gl.uniform2fv(program.u_pattern_size_a, imagePosA.displaySize);
gl.uniform2fv(program.u_pattern_size_b, imagePosB.displaySize);
gl.uniform2fv(program.u_pattern_size_a, (imagePosA: any).displaySize);
gl.uniform2fv(program.u_pattern_size_b, (imagePosB: any).displaySize);
gl.uniform1f(program.u_scale_a, image.fromScale);
gl.uniform1f(program.u_scale_b, image.toScale);

gl.activeTexture(gl.TEXTURE0);
painter.spriteAtlas.bind(gl, true);
};

exports.setTile = function (tile, painter, program) {
exports.setTile = function (tile: {coord: TileCoord, tileSize: number}, painter: Painter, program: Program) {
const gl = painter.gl;

gl.uniform1f(program.u_tile_units_to_pixels, 1 / pixelsToTileUnits(tile, 1, painter.transform.tileZoom));
Expand Down
7 changes: 2 additions & 5 deletions src/source/pixels_to_tile_units.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const EXTENT = require('../data/extent');

import type Tile from './tile';
import type TileCoord from './tile_coord';

/**
* Converts a pixel value at a the given zoom level to tile units.
Expand All @@ -13,12 +13,9 @@ import type Tile from './tile';
* For example, a translation by 30 pixels at zoom 6.5 will be a
* translation by pixelsToTileUnits(30, 6.5) tile units.
*
* @param tile a {Tile object} will work well, but any object that follows the format {coord: {TileCord object}, tileSize: {number}} will work
* @param pixelValue
* @param z
* @returns value in tile units
* @private
*/
module.exports = function(tile: Tile, pixelValue: number, z: number): number {
module.exports = function(tile: {coord: TileCoord, tileSize: number}, pixelValue: number, z: number): number {
return pixelValue * (EXTENT / (tile.tileSize * Math.pow(2, z - tile.coord.z)));
};

0 comments on commit 5a263b1

Please sign in to comment.