Skip to content

Commit

Permalink
feat(source): reuse tileSource
Browse files Browse the repository at this point in the history
  • Loading branch information
lzxue committed Jun 14, 2019
1 parent 6a4af99 commit 4ce9d60
Show file tree
Hide file tree
Showing 21 changed files with 286 additions and 123 deletions.
2 changes: 1 addition & 1 deletion demos/vectorTile.html
Expand Up @@ -43,7 +43,7 @@

// http://alipay-rmsdeploy-image.cn-hangzhou.alipay.aliyun-inc.com/thinkgis/tile/point/{z}/{x}/{y}.pbf
// https://mvt.amap.com/district/CHN2/8/203/105/4096?key=
.source('http://alipay-rmsdeploy-image.cn-hangzhou.alipay.aliyun-inc.com/thinkgis/tile/china/all_point/{z}/{x}/{y}.pbf',{
.source('http://alipay-rmsdeploy-image.cn-hangzhou.alipay.aliyun-inc.com/thinkgis/tile/point2/{z}/{x}/{y}.pbf',{
parser:{
type: 'mvt',
sourceLayer:'layer',
Expand Down
36 changes: 15 additions & 21 deletions demos/vectorTilepolygon.html
Expand Up @@ -35,47 +35,41 @@
});
window.scene = scene;
var colorHash = new ColorHash();

scene.on('loaded', () => {
const layer = scene.VectorTileLayer({
zIndex:0,
layerType:'polygon'
})
//.source('https://pre-lbs-show.taobao.com/gettile?x={x}&y={y}&z={z}&pipeId=pipe_vt_test')

// http://alipay-rmsdeploy-image.cn-hangzhou.alipay.aliyun-inc.com/thinkgis/tile/point/{z}/{x}/{y}.pbf
// https://mvt.amap.com/district/CHN2/8/203/105/4096?key=
.source('http://alipay-rmsdeploy-image.cn-hangzhou.alipay.aliyun-inc.com/thinkgis/tile/china/village/{z}/{x}/{y}.pbf',{

const provinceSource = new L7.TileSource('http://alipay-rmsdeploy-image.cn-hangzhou.alipay.aliyun-inc.com/thinkgis/tile/china/province/{z}/{x}/{y}.pbf',{
parser:{
type: 'mvt',
sourceLayer:'layer',
idField:'code',
maxZoom: 17,
maxZoom: 5,
}
})
console.log(provinceSource);
const layer = scene.VectorTileLayer({
zIndex:0,
layerType:'line'
})
.source(provinceSource)
.scale({
total:{
type:'linear',
min:0,
max:5000
max:5000000
}
})
.shape('fill')
.shape('line')
.size(2)
.active(false)
.color('total', ['#ffffe5','#fff7bc','#fee391','#fec44f','#fe9929','#ec7014','#cc4c02','#993404','#662506'])
.style({
opacity:1.0
})
.render();
layer.on('mousemove',(feature)=>{
console.log(feature);
})
console.log(layer);
.render();

});
//OBJECTID',(id)=>{
// const index = id % 8;
//return ['#9e0142','#d53e4f','#f46d43','#fdae61','#fee08b','#ffffbf','#e6f598','#abdda4','#66c2a5','#3288bd','#5e4fa2'][index];
//}

</script>
</body>
</html>
Expand Down
16 changes: 12 additions & 4 deletions src/core/controller/buffer.js
Expand Up @@ -18,10 +18,18 @@ export default class BufferController {
const colorAttr = this.mesh.mesh.geometry.attributes.a_color;
const pickAttr = this.mesh.mesh.geometry.attributes.pickingId;
pickAttr.array.forEach((id, index) => {
id = Math.abs(id);
const color = colorKey[id];
id = Math.abs(id);
const item = filterData[id - 1];
let newId = Math.abs(id);
let item = null;
let color = null;
if (this.mesh.layerSource.data.featureKeys) { // hash数据映射
newId = this.mesh.layerSource.data.featureKeys[newId].index;
item = filterData[newId];
color = colorKey[item.id];
} else {
item = filterData[newId - 1];
color = colorKey[newId];
}

if (item.hasOwnProperty('filter') && item.filter === false) {
colorAttr.array[index * 4 + 0] = 0;
colorAttr.array[index * 4 + 1] = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/core/controller/mapping.js
Expand Up @@ -34,8 +34,8 @@ export default class Mapping {
this.mesh.set('scaleController', scaleController);
}
_createScale(field) {
// TODO scale更新
const scales = this.mesh.get('scales');
this._initControllers(); // scale更新
let scale = scales[field];
if (!scale) {
scale = this.createScale(field);
Expand Down
18 changes: 16 additions & 2 deletions src/core/engine/picking/picking.js
Expand Up @@ -50,8 +50,7 @@ class Picking {
}
_update(point) {
const texture = this._pickingTexture;
// this._pickingTexture
this._renderer.render(this._pickingScene, this._camera, this._pickingTexture);
this._renderer.render(this._pickingScene, this._camera, texture);
this.pixelBuffer = new Uint8Array(4);
this._renderer.readRenderTargetPixels(texture, point.x, this._height - point.y, 1, 1, this.pixelBuffer);

Expand All @@ -62,8 +61,23 @@ class Picking {
index === id ? object.visible = true : object.visible = false;
});
}
_layerIsVisable(object) {
const layers = this._world.getLayers();
let isVisable = false;
for (let i = 0; i < layers.length; i++) {
const layer = layers[i];
if (object.name === layer.layerId) {
isVisable = layer.get('visible');
break;
}
}
return isVisable;
}
_pickAllObject(point, normalisedPoint) {
this.world.children.forEach((object, index) => {
if (!this._layerIsVisable(object)) {
return;
}
this._filterObject(index);
const item = this._pick(point, normalisedPoint, object.name);
item.type = point.type;
Expand Down
21 changes: 14 additions & 7 deletions src/core/layer.js
Expand Up @@ -460,13 +460,15 @@ export default class Layer extends Base {
_initEvents() {
this.scene.on('pick-' + this.layerId, e => {
let { featureId, point2d, type } = e;
if (featureId < 0 && this._activeIds !== null) {
type = 'mouseleave';
}
this._activeIds = featureId;
// TODO 瓦片图层获取选中数据信息
const lnglat = this.scene.containerToLngLat(point2d);
const { feature, style } = this.getSelectFeature(featureId, lnglat);
let feature = null;
let style = null;
if (featureId !== -999) {
const res = this.getSelectFeature(featureId, lnglat);
feature = res.feature;
style = res.style;
}
const target = {
featureId,
feature,
Expand All @@ -475,9 +477,14 @@ export default class Layer extends Base {
type,
lnglat: { lng: lnglat.lng, lat: lnglat.lat }
};
if (featureId >= 0 || this._activeIds >= 0) { // 拾取到元素,或者离开元素
if (featureId >= 0) { // 拾取到元素,或者离开元素
this.emit(type, target);
}
if (featureId < 0 && this._activeIds >= 0) {
type = 'mouseleave';
this.emit(type, target);
}
this._activeIds = featureId;

});
}
Expand Down Expand Up @@ -538,7 +545,7 @@ export default class Layer extends Base {
offset = 1;
}
this._object3D.position && (this._object3D.position.z = offset * Math.pow(2, 20 - zoom));
if (zoom < minZoom || zoom > maxZoom) {
if (zoom < minZoom || zoom >= maxZoom) {
this._object3D.visible = false;
} else if (this.get('visible')) {
this._object3D.visible = true;
Expand Down
4 changes: 3 additions & 1 deletion src/core/scene.js
Expand Up @@ -105,7 +105,9 @@ export default class Scene extends Base {
this._container.addEventListener(event, e => {
// 要素拾取
e.pixel || (e.pixel = e.point);
this._engine._picking.pickdata(e);
requestAnimationFrame(() => {
this._engine._picking.pickdata(e);
});
}, false);
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/source.js
Expand Up @@ -58,7 +58,7 @@ export default class Source extends Base {
this.originData = getParser(type)(data, parser);
// this.data = {
// dataArray: clone(this.originData.dataArray)
// };
// }; // TODO 关闭数据备份
this.data = this.originData;
if (this.data !== null) {
this.data.extent = extent(this.data.dataArray);
Expand Down
8 changes: 4 additions & 4 deletions src/geom/material/lineMaterial.js
Expand Up @@ -17,8 +17,8 @@ export function LineMaterial(options) {
},
vertexShader: vs,
fragmentShader: fs,
transparent: true,
blending: THREE.AdditiveBlending
transparent: true
// blending: THREE.AdditiveBlending
});
return material;
}
Expand Down Expand Up @@ -50,8 +50,8 @@ export function MeshLineMaterial(options, defines) {
defines,
vertexShader: vs,
fragmentShader: fs,
transparent: true,
blending: THREE.AdditiveBlending
transparent: true
// blending: THREE.AdditiveBlending
});
return material;
}
3 changes: 1 addition & 2 deletions src/geom/shader/line_vert.glsl
Expand Up @@ -18,7 +18,6 @@ void main() {
vTime = 1.0- (mod(u_time*50.,3600.)- position.z) / 100.;
// vTime = 1.0- (28800. + mod(u_time* 10.,28800.)- position.z / 1000.) / 100.;
#endif
gl_Position = matModelViewProjection * vec4(position.xy, 0., 1.0);
gl_Position.z += 2. * gl_Position.w;
gl_Position = matModelViewProjection * vec4(position.xy, 10., 1.0);
worldId = id_toPickColor(pickingId);
}
2 changes: 2 additions & 0 deletions src/index.js
Expand Up @@ -3,6 +3,7 @@
import Scene from './core/scene';
import Global from './global';
import Source from './core/source';
import TileSource from './source/tileSource';
import { registerParser, registerTransform } from './source';
import { registerInteraction, getInteraction } from './interaction';
import { registerLayer } from './layer';
Expand All @@ -11,6 +12,7 @@ export {
version,
Scene,
Source,
TileSource,
registerParser,
registerTransform,
registerLayer,
Expand Down
4 changes: 4 additions & 0 deletions src/layer/render/polygon/drawFill.js
Expand Up @@ -26,6 +26,10 @@ export default function DrawPolygonFill(layerData, layer) {
SHAPE: false
});
const fillPolygonMesh = new THREE.Mesh(geometry, material);
delete attributes.vertices;
delete attributes.colors;
delete attributes.pickingIds;
delete attributes.normals;
return fillPolygonMesh;
}

16 changes: 15 additions & 1 deletion src/layer/tile/tile.js
Expand Up @@ -30,17 +30,31 @@ export default class Tile extends Base {
this.requestTileAsync(data => this._init(data));
}
_init(data) {
this._creatSource(data);
// this._creatSource(data); // 获取Source
this.layerSource = data;

if (this.layerSource.data === null) {
this.isValid = false;
return;
}
this.isValid = true;
this._initControllers();
this._createMesh();
}
repaint() {
this._initControllers();
this._createMesh();
}
requestTileAsync(done) {
const data = this.layer.tileSource.getTileData(this._tile[0], this._tile[1], this._tile[2]);
if (data.loaded) {
done(data.data);
} else {
data.data.then(data => {
done(data);
});
}
}
_initControllers() {
const mappingCtr = new Controller.Mapping({
layer: this.layer,
Expand Down
6 changes: 6 additions & 0 deletions src/layer/tile/tileCache.js
Expand Up @@ -14,4 +14,10 @@ export default class TileCache {
destory() {
this._cache.clear();
}
setNeedUpdate() {
this._cache._order.forEach(key => {
const tile = this._cache.get(key);
tile.needUpdate = true;
});
}
}

0 comments on commit 4ce9d60

Please sign in to comment.