Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. const Transform = require('./geo/transform'),
  2. Painter = require('./render/painter'),
  3. Style = require('./style/style'),
  4. Camera = require('./ui/camera'),
  5. TileCoord = require('./source/tile_coord'),
  6. EXTENT = require('./data/extent'),
  7. glmatrix = require('@mapbox/gl-matrix');
  8.  
  9. const mat4 = glmatrix.mat4;
  10.  
  11. const SIZE = 512;
  12.  
  13. class MapboxSingleTile extends Camera {
  14.  
  15. constructor(options) {
  16. var transform = new Transform(options.minZoom, options.maxZoom, options.renderWorldCopies);
  17. options = options || {};
  18. super(transform, options);
  19. transform.calculatePosMatrix = this._transform_calculatePosMatrix.bind(transform);
  20. this._transform = transform;
  21. this._initOptions = options;
  22. this._style = new Style(options.style, this);
  23. this._style.setEventedParent(this, {style: this._style});
  24. this._canvas = document.createElement('canvas');
  25. this._canvas.width = SIZE;
  26. this._canvas.height = SIZE;
  27.  
  28. this._transform.resize(SIZE, SIZE);
  29. this._canvas.addEventListener('webglcontextlost', () => console.log("webglcontextlost"), false);
  30. this._canvas.addEventListener('webglcontextrestored', () => this._createGlContext(), false);
  31. this._createGlContext();
  32. this._sourceCaches = this._style.sourceCaches
  33. this._renderingTiles = {};
  34. }
  35.  
  36. _transform_calculatePosMatrix(tileCoord, maxZoom) {
  37. // override for this._transform.calculatePosMatrix (patched in constructor, above)
  38. const S = 4092; // I think this is the size of the tile in its own coordiante scheme, not sure why we have to x2
  39. const posMatrix = mat4.identity(new Float64Array(16));
  40. mat4.scale(posMatrix, posMatrix, [1/S,-1/S,1]);
  41. mat4.translate(posMatrix, posMatrix, [-S,-S,0]);
  42. return new Float32Array(posMatrix);
  43. }
  44.  
  45. showCanvasForDebug(){
  46. document.body.appendChild(this._canvas);
  47. this._canvas.style.position = "fixed";
  48. this._canvas.style.top = "20px";
  49. this._canvas.style.right = "20px";
  50. this._canvas.style.border = "1px solid red";
  51. this._canvas.style.background = "#666";
  52. }
  53.  
  54. _createGlContext(){
  55. const attributes = Object.assign({
  56. failIfMajorPerformanceCaveat: this._initOptions.failIfMajorPerformanceCaveat,
  57. preserveDrawingBuffer: this._initOptions.preserveDrawingBuffer
  58. }, require('mapbox-gl-supported').webGLContextAttributes);
  59.  
  60. this._gl = this._canvas.getContext('webgl', attributes) ||
  61. this._canvas.getContext('experimental-webgl', attributes);
  62. if (!this._gl) {
  63. throw new Error('Failed to initialize WebGL');
  64. }
  65. this.painter = new Painter(this._gl, this._transform);
  66. this.painter.resize(SIZE, SIZE);
  67. this.painter.style = this._style;
  68. }
  69.  
  70. applyStyles(z) {
  71. // alternative to style.js@_recalculate
  72. for (const sourceId in this._sourceCaches){
  73. this._sourceCaches[sourceId].used = false;
  74. }
  75. for(var layerId of this._style._order){
  76. const layer = this._style._layers[layerId];
  77. layer.recalculate(z);
  78. if (!layer.isHidden(z) && layer.source) {
  79. this._sourceCaches[layer.source].used = true;
  80. }
  81. }
  82. this._style._applyClasses([], {transition: false});
  83. }
  84.  
  85. _renderTileNowDataIsAvailable(e){
  86. var state = this._renderingTiles[e.coord.id];
  87. if(--state.awaitingSources > 0){
  88. return;
  89. }
  90.  
  91. var z = e.coord.z;
  92.  
  93. this.applyStyles(z); // TODO: only do this if zoom has changed
  94. this.painter.render(this._style, {
  95. showTileBoundaries: this._initOptions.showTileBoundaries,
  96. showOverdrawInspector: this._initOptions.showOverdrawInspector
  97. });
  98.  
  99. delete this._renderingTiles[e.coord.id];
  100. state.next && state.next();
  101.  
  102. }
  103.  
  104. _initSourcesCaches(){
  105. if(this._initSourcesCachesDone){
  106. return;
  107. }
  108. for(var k in this._sourceCaches){
  109. this._sourceCaches[k]._coveredTiles = {};
  110. this._sourceCaches[k].transform = this._transform;
  111. this._sourceCaches[k].on('data', this._renderTileNowDataIsAvailable.bind(this));
  112. }
  113. this._initSourcesCachesDone = true;
  114. }
  115.  
  116. renderTile(z, x, y, options, next){
  117. // TODO: use x and y properly...
  118. z = z || 15; x = x || 16370; y = y || 10900;
  119.  
  120. this._initSourcesCaches();
  121.  
  122. this.jumpTo({ // TODO: work out why this is still needed
  123. center: m.map.getCenter().toJSON(), //{Lat:, Lng: }
  124. zoom: z || 15
  125. });
  126.  
  127. var coords = new TileCoord(z, x, y, 0);
  128. var state = this._renderingTiles[coords.id] = {
  129. next: next,
  130. awaitingSources: 0
  131. };
  132. for(var k in this._sourceCaches){
  133. var tile = this._sourceCaches[k].addTile(coords);
  134. !tile.hasData() && state.awaitingSources++;
  135. }
  136. if(state.awaitingSources == 0){
  137. this._renderTileNowDataIsAvailable({coord: coords});
  138. }
  139.  
  140. }
  141.  
  142. }
  143.  
  144. export default MapboxSingleTile;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement