If you are not familiar with maptalks, refer to its documentation.This will help you read the document
This is the base class for all 3D objects,Its function and maptalks.geometry are as similar as possible.
Bar, ExtrudePolygon, ExtrudeLine, Line, Model base on it
If you're familiar with three.js. You can customize your own graphic elements based on it
such as examples we provide circle ,fatline,linetrip,linetrail
methods
getObject3d()
getId()
setId(id)
getType() Get the type of graphic element
const bar=threeLayer.toBar(...);
bar.getType()
getOptions() Get configuration information of graphic elements
getProperties() Get attribute information of graphic elements
setProperties(property) set attribute information of graphic elements
getLayer()
getMap()
getCenter()
getAltitude() Get the elevation of the graphic element
setAltitude(altitude) set the elevation of the graphic element
If you customize your own graphic elements, you may override this method.
such as Barshow()
hide()
isVisible()
getSymbol()
If you customize your own graphic elements, you may override this method
setSymbol(material)
If you customize your own graphic elements, you may override this method
setInfoWindow(options)
bar.setInfoWindow({
content: 'hello world,height:' + d.height * 400,
title: 'message',
animationDuration: 0,
autoOpenOn: false
});
getInfoWindow()
openInfoWindow(coordinate)
closeInfoWindow()
removeInfoWindow()
setToolTip(content, options)
bar.setToolTip(d.height * 400, {
showTimeout: 0,
eventsPropagation: true,
dx: 10
});
getToolTip()
openToolTip(coordinate)
closeToolTip()
removeToolTip()
on(eventsOn, handler, context)
bar.on('click',function(e){
});
addEventListener(eventTypes, handler, context)
once(eventTypes, handler, context)
off(eventsOff, handler, contextopt)
bar.off('click',function(e){
});
removeEventListener(eventTypes, handler, context)
fire(eventType, param)
animateShow(options = {}, cb)
If you customize your own graphic elements, you may need to override this method to delayed display of graphics
animationShow(options = {}, cb) {
if (this._showPlayer) {
this._showPlayer.cancel();
}
if (maptalks.Util.isFunction(options)) {
options = {};
cb = options;
}
const duration = options['duration'] || 1000,
easing = options['easing'] || 'out';
const player = this._showPlayer = maptalks.animation.Animation.animate({
'scale': 1
}, {
'duration': duration,
'easing': easing
}, frame => {
const scale = frame.styles.scale;
if (scale > 0) {
this.getObject3d().scale.set(scale, scale, scale);
}
if (cb) {
cb(frame, scale);
}
});
player.play();
return player;
}
identify(coordinate)
You need to add this method if you want to customize your own graphic elements and have your own hit detection implementation(By default, the detection of graphics is provided by Raycaster)
_initOptions(options) Initialize internal configuration information
class Circle extends maptalks.BaseObject {
constructor(coordinate, options, material, layer) {
options = maptalks.Util.extend({}, OPTIONS, options, { layer, coordinate });
super();
//Initialize internal configuration
// https://github.com/maptalks/maptalks.three/blob/1e45f5238f500225ada1deb09b8bab18c1b52cf2/src/BaseObject.js#L135
this._initOptions(options);
const { altitude, radius } = options;
//generate geometry
const r = layer.distanceToVector3(radius, radius).x
const geometry = new THREE.CircleBufferGeometry(r, 50);
//Initialize internal object3d
// https://github.com/maptalks/maptalks.three/blob/1e45f5238f500225ada1deb09b8bab18c1b52cf2/src/BaseObject.js#L140
this._createMesh(geometry, material);
//set object3d position
const z = layer.distanceToVector3(altitude, altitude).x;
const position = layer.coordinateToVector3(coordinate, z);
this.getObject3d().position.copy(position);
this.getObject3d().rotation.x = -Math.PI;
}
}
_createMesh(geometry, material) Creating THREE.Mesh objects
If you customize the graphics, you will use this method
_createGroup() Creating THREE.Group objects
If you customize the graphics, you will use this method
_createLine(geometry, material) Creating THREE.Line objects
If you customize the graphics, you will use this method
_animation()
If you customize graphics and want them to perform circular animation, you need to implement the function of this method
,such as linetrailevents support
A maptalks Layer to render with THREE.js
ThreeLayer
is a subclass of maptalks.CanvasLayer and inherits all the methods of its parent.
constructor
new maptalks.ThreeLayer(id, options)
methods
coordinateToVector3(coordinate, z = 0) Latitude and longitude transform three-dimensional vector
threeLayer.coordinateToVector3(map.getCenter(),1);
distanceToVector3(w, h, coord) Convert length to 3D vector
* toShape(polygon) `polygon|MultiPolygono to THREE.Shape`
* polygon
* return THREE.Shape
* toExtrudeMesh(polygon, altitude, material, height) `polygon to THREE.ExtrudeGeometry Mesh`
* polygon [Polygon|MultiPolygon]
* altitude
* material [THREE.Material]
* height [The value of the polygon being raised]
* return THREE.Mesh
* `In the new version, you can use toExtrudePolygon instead`
* toExtrudePolygon(polygon, options, material)
* polygon [Polygon|MultiPolygon]
* options.altitude=0
* options.height=1
* options.topColor=null
* options.bottomColor='#2d2f61'
* options.interactive=true [Can I interact]
* material [THREE.Material]
* return [ExtrudePolygon](./src/ExtrudePolygon.js)
```js
var mesh = threeLayer.toExtrudePolygon(maptalks.GeoJSON.toGeometry(g), {
height: levels * heightPerLevel,
topColor: '#fff'
}, material);
toBar(coordinate, options, material)
* toLine(lineString, options, material)
* lineString [maptalks.LineString]
* options.altitude=0
* options.colors=null
* options.interactive=true
* material [THREE.Material]
* return [Line](./src/Line.js)
```js
var line = threeLayer.toLine(lineString, { altitude: 0 }, material);
toExtrudeLine(lineString, options, material)
var line = threeLayer.toExtrudeLine(lineString, { altitude: 0, width: 3, height: 1 }, material);
toExtrudeLineTrail(lineString, options, material) Create trailing lines
lineTrails = lineStrings.slice(0, 300).map(function (d) {
var line = threeLayer.toExtrudeLineTrail(d.lineString,
{ altitude: 0, width: 3, height: 2, chunkLength: d.len / 40, speed: 1, trail: 6 },
highlightmaterial);
return line;
});
toModel(model, options)
var model=threeLayer.toModel(object,{
coordinate:map.getCenter(),
altitude:100
});
getCamera()
getScene()
getThreeRenderer()
renderScene() Render Scene
addMesh(meshes) add Graphical.Please add data in batch, so that you can have higher rendering performance
removeMesh(meshes) remove Graphical.Please add data in batch, so that you can have higher rendering performance
identify(coordinate, options) Graph hit detection
map.on('mousemove',function(e){
threeLayer.identify(e.coordinate);
});
BaseObject is the base class of graphics provided by us. If you need to customize your own graphics, please extend it based on it
class xxx extends maptalks.BaseObject{
constructor(...){
options = maptalks.Util.extend({}, OPTIONS, options, { layer, coordinate });
super();
this._initOptions(options);
...
this._createMesh(geometry, material);
//this._createGroup();
//this._createLine(geometry,material);
}
....
}
The value of BaseObject’s object3d attribute is three.js. You can perform relevant operations on it, such as scale, position, rotation, etc
In theory, you can customize any graphics component you need. Of course, it requires you to be familiar with three.js