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

[wip] layer-by-layer rendering #1197

Closed
wants to merge 13 commits into from
79 changes: 37 additions & 42 deletions js/render/draw_background.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
'use strict';

var mat3 = require('gl-matrix').mat3;
var TilePyramid = require('../source/tile_pyramid');
var Tile = require('../source/tile');

var pyramid = new TilePyramid({ tileSize: 512 });

module.exports = drawBackground;

function drawBackground(painter, layer, posMatrix) {
var gl = painter.gl;
var transform = painter.transform;
var color = layer.paint['background-color'];
var image = layer.paint['background-image'];
var opacity = layer.paint['background-opacity'];
Expand All @@ -14,7 +18,11 @@ function drawBackground(painter, layer, posMatrix) {
var imagePosA = image ? painter.spriteAtlas.getPosition(image.from, true) : null;
var imagePosB = image ? painter.spriteAtlas.getPosition(image.to, true) : null;

painter.setSublayer(0);
if (imagePosA && imagePosB) {

if (painter.opaquePass) return;

// Draw texture fill
shader = painter.patternShader;
gl.switchShader(shader, posMatrix);
Expand All @@ -25,60 +33,47 @@ function drawBackground(painter, layer, posMatrix) {
gl.uniform2fv(shader.u_pattern_br_b, imagePosB.br);
gl.uniform1f(shader.u_opacity, opacity);

var transform = painter.transform;
var sizeA = imagePosA.size;
var sizeB = imagePosB.size;
var center = transform.locationCoordinate(transform.center);
var scale = 1 / Math.pow(2, transform.zoomFraction);

gl.uniform1f(shader.u_mix, image.t);

var matrixA = mat3.create();
mat3.scale(matrixA, matrixA, [
1 / (sizeA[0] * image.fromScale),
1 / (sizeA[1] * image.fromScale)
]);
mat3.translate(matrixA, matrixA, [
(center.column * transform.tileSize) % (sizeA[0] * image.fromScale),
(center.row * transform.tileSize) % (sizeA[1] * image.fromScale)
]);
mat3.rotate(matrixA, matrixA, -transform.angle);
mat3.scale(matrixA, matrixA, [
scale * transform.width / 2,
-scale * transform.height / 2
]);

var matrixB = mat3.create();
mat3.scale(matrixB, matrixB, [
1 / (sizeB[0] * image.toScale),
1 / (sizeB[1] * image.toScale)
]);
mat3.translate(matrixB, matrixB, [
(center.column * transform.tileSize) % (sizeB[0] * image.toScale),
(center.row * transform.tileSize) % (sizeB[1] * image.toScale)
]);
mat3.rotate(matrixB, matrixB, -transform.angle);
mat3.scale(matrixB, matrixB, [
scale * transform.width / 2,
-scale * transform.height / 2
]);

gl.uniformMatrix3fv(shader.u_patternmatrix_a, false, matrixA);
gl.uniformMatrix3fv(shader.u_patternmatrix_b, false, matrixB);
var factor = (4096 / transform.tileSize) / Math.pow(2, 0);

gl.uniform2fv(shader.u_patternscale_a, [
1 / (imagePosA.size[0] * factor * image.fromScale),
1 / (imagePosA.size[1] * factor * image.fromScale)
]);

gl.uniform2fv(shader.u_patternscale_b, [
1 / (imagePosB.size[0] * factor * image.toScale),
1 / (imagePosB.size[1] * factor * image.toScale)
]);

painter.spriteAtlas.bind(gl, true);

} else {
// Draw filling rectangle.

if (painter.opaquePass !== (color[3] === 1)) return;

shader = painter.fillShader;
gl.switchShader(shader, posMatrix);
gl.uniform4fv(shader.u_color, color);
}

gl.disable(gl.STENCIL_TEST);
gl.bindBuffer(gl.ARRAY_BUFFER, painter.backgroundBuffer);
gl.vertexAttribPointer(shader.a_pos, painter.backgroundBuffer.itemSize, gl.SHORT, false, 0, 0);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, painter.backgroundBuffer.itemCount);
gl.bindBuffer(gl.ARRAY_BUFFER, painter.tileExtentBuffer);
gl.vertexAttribPointer(shader.a_pos, painter.tileExtentBuffer.itemSize, gl.SHORT, false, 0, 0);

var coveringTiles = pyramid.coveringTiles(transform);
for (var c = 0; c < coveringTiles.length; c++) {
var coord = coveringTiles[c];

var tile = new Tile(coord, transform.tileSize);
tile.calculateMatrices(coord.z, coord.x, coord.y, transform);

gl.uniformMatrix4fv(shader.u_matrix, false, tile.posMatrix);
gl.drawArrays(gl.TRIANGLE_STRIP, 0, painter.tileExtentBuffer.itemCount);
}

gl.enable(gl.STENCIL_TEST);

gl.stencilMask(0x00);
Expand Down
4 changes: 3 additions & 1 deletion js/render/draw_collision_debug.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ function drawPlacementDebug(painter, layer, posMatrix, tile) {
var shader = painter.collisionBoxShader;

gl.enable(gl.STENCIL_TEST);
painter.setClippingMask(tile);

gl.switchShader(shader, posMatrix);
gl.switchShader(shader);
gl.uniformMatrix4fv(shader.u_matrix, false, posMatrix);
buffer.bind(gl, shader);
gl.lineWidth(3);

Expand Down
Loading