Skip to content

Commit

Permalink
feat(scene): 按需进行渲染刷新
Browse files Browse the repository at this point in the history
  • Loading branch information
lzxue committed Mar 26, 2019
1 parent 8158a1d commit 691f223
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 8 deletions.
1 change: 0 additions & 1 deletion src/core/engine/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ export default class Engine extends EventEmitter {

}
run() {

this.update();
this.engineID = requestAnimationFrame(this.run.bind(this));
}
Expand Down
2 changes: 2 additions & 0 deletions src/core/layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,8 @@ export default class Layer extends Base {
if (type === 'fill') {
this._addPickMesh(object);// 不对边界线进行拾取
}
this.scene._engine.update();
setTimeout(() => this.scene._engine.update(), 500);
}
remove(object) {
if (object.type === 'composer') {
Expand Down
31 changes: 29 additions & 2 deletions src/core/scene.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ export default class Scene extends Base {
this.addImage();
this.fontAtlasManager = new FontAtlasManager();
this._layers = [];
this.animateCount = 0;
}

_initEngine(mapContainer) {
this._engine = new Engine(mapContainer, this);
this._engine.run();
this.registerMapEvent();
// this.workerPool = new WorkerPool();
compileBuiltinModules();
}
Expand All @@ -40,8 +41,8 @@ export default class Scene extends Base {
this._container = document.getElementById(Map.container);
// const Map = new MapProvider(this.mapContainer, this._attrs);
Map.on('mapLoad', () => {
this._initEngine(Map.renderDom);
this.map = Map.map;
this._initEngine(Map.renderDom);
Map.asyncCamera(this._engine);
this.initLayer();
this._registEvents();
Expand Down Expand Up @@ -117,5 +118,31 @@ export default class Scene extends Base {
layer.destroy();
layer = null;
}
startAnimate() {
if (this.animateCount === 0) {
this.unRegsterMapEvent();
this._engine.run();
}
this.animateCount++;
}
stopAnimate() {
if (this.animateCount === 1) {
this._engine.stop();
this.registerMapEvent();
}
this.animateCount++;
}
// 地图状态变化时更新可视化渲染
registerMapEvent() {
this._updateRender = () => this._engine.update();
this.map.on('mousemove', this._updateRender);
this.map.on('mapmove', this._updateRender);
this.map.on('camerachange', this._updateRender);
}
unRegsterMapEvent() {
this.map.off('mousemove', this._updateRender);
this.map.off('mapmove', this._updateRender);
this.map.off('camerachange', this._updateRender);
}

}
9 changes: 5 additions & 4 deletions src/geom/shader/text_vert2.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ void main(){
mat4 matModelViewProjection=projectionMatrix*modelViewMatrix;
vec4 cur_position=matModelViewProjection*vec4(a_position.xy,0,1);
gl_Position=cur_position / cur_position.w+ vec4(a_textSize*position.xy/u_glSize, 0., 0.)+vec4(a_textOffset/u_glSize * 2.0,0,0);
v_color=vec4(a_color.rgb,a_color.a*u_opacity);
if(pickingId == u_activeId) {
v_color = u_activeColor;
}
// v_color=vec4(a_color.rgb,a_color.a*u_opacity);
// if(pickingId == u_activeId) {
// v_color = u_activeColor;
// }
v_color = a_color;
v_texcoord=(textUv.xy + vec2(uv.x,1.-uv.y) * textUv.zw)/u_textTextureSize;
worldId = id_toPickColor(pickingId);

Expand Down
5 changes: 4 additions & 1 deletion src/layer/lineLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ export default class LineLayer extends Layer {
if (animateOptions.enable) {

material.setDefinesvalue('ANIMATE', true);
this.scene.startAnimate();
const { duration, interval, trailLength, repeat = Infinity } = animateOptions;
this.animateDuration = this.scene._engine.clock.getElapsedTime() + duration * repeat;
material.upDateUninform({
Expand Down Expand Up @@ -92,17 +93,19 @@ export default class LineLayer extends Layer {
});
if (animateOptions.enable) {
material.setDefinesvalue('ANIMATE', true);
this.scene.startAnimate();
}

const mesh = new THREE.LineSegments(geometry, material);
this.add(mesh);
}
return this;
}
_preRender() {
preRender() {
if (this.animateDuration > 0 && this.animateDuration < this.scene._engine.clock.getElapsedTime()) {
this.layerMesh.material.setDefinesvalue('ANIMATE', false);
this.emit('animateEnd');
this.scene.stopAnimate();
this.animateDuration = Infinity;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/layer/polygonLayer.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export default class PolygonLayer extends Layer {
return drawPolygon.DrawLine(attributes, style);
} else if (animateOptions.enable) {
const { near, far } = this.map.getCameraState();
this.scene.startAnimate();
return drawPolygon.DrawAnimate(attributes, { ...style, near, far });
}
return drawPolygon.DrawFill(attributes, config);
Expand Down

0 comments on commit 691f223

Please sign in to comment.