maptalks.three

API

如果您不熟悉maptalks,请参阅其文档。这将帮助您阅读该文档

BaseObject

这是所有三维对象的基类,其函数和maptalks.geometry尽可能类似。

Bar, ExtrudePolygon, ExtrudeLine, Line, Model 基于它的

如果你熟悉three.js。您可以基于它自定义自己的图形元素

比如我们提供的例子 circle ,fatline,linetrip,linetrail

methods

  const bar=threeLayer.toBar(...);
  bar.getType()

bar.setInfoWindow({ content: ‘hello world,height:’ + d.height * 400, title: ‘message’, animationDuration: 0, autoOpenOn: false });



* getInfoWindow()

  * return ui.InfoWindow



* openInfoWindow(coordinate)

  * coordinate [infowindow position]
  * return this



* closeInfoWindow()

  * return this



* removeInfoWindow()

  * return this



* setToolTip(content, options)

  * content [tooltip content]
  * options [tooltip cofig  [detail](https://maptalks.org/maptalks.js/api/0.x/ui.ToolTip.html)]
  * return this
  
```js
bar.setToolTip(d.height * 400, {
    showTimeout: 0,
    eventsPropagation: true,
    dx: 10
});




* addEventListener(eventTypes, handler, context)

  * eventTypes  [要注册的事件类型,如果有多个,则用空格分隔。]
  * handler  [要调用的处理程序函数]
  * context  [处理程序的上下文]
  * return this
  * [detail maptalks.Eventable](https://maptalks.org/maptalks.js/api/0.x/Eventable.html)



* once(eventTypes, handler, context)

  * eventTypes  [要注册的事件类型,如果有多个,则用空格分隔。]
  * handler  [要调用的处理程序函数]
  * context  [处理程序的上下文]
  * return this
  * [detail maptalks.Eventable](https://maptalks.org/maptalks.js/api/0.x/Eventable.html)



* off(eventsOff, handler, contextopt)

  * eventsOff  [要注销的事件类型,如果有多个,则用空格分隔。]
  * handler  [要调用的处理程序函数]
  * context  [处理程序的上下文]
  * return this
  * [detail maptalks.Eventable](https://maptalks.org/maptalks.js/api/0.x/Eventable.html)
```js
   bar.off('click',function(e){

   });





* identify(coordinate)
  
  * coordinate [Coordinate]
  * return Boolean
  * `如果要自定义自己的图形元素并拥有自己的命中检测实现,则需要添加此方法(默认情况下,图形的检测由Raycaster提供)`
  * such as [fatline](/maptalks.three/demo/custom-fatline.html)





* _initOptions(options)  `初始化内部配置信息`

  * options
  * return this
  * `如果自定义图形,将使用此方法`. 比如我们提供的例子[circle](/maptalks.three/demo/custom-circle.html) ,[fatline](/maptalks.three/demo/custom-fatline.html)

```js
 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;
            }
 }

events support

ThreeLayer

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

   threeLayer.coordinateToVector3(map.getCenter(),1);





* 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 [多边形拔高的值]]
  * return THREE.Mesh
  * `在新版本中,您可以使用toExtrudePolygon代替`





* toExtrudePolygon(polygon, options, material)

  * polygon [Polygon|MultiPolygon]
  * options.altitude=0
  * options.height=1
  * options.topColor=null 
  * options.bottomColor='#2d2f61'
  * options.interactive=true [可以交互与否]
  * material [THREE.Material]
  * return [ExtrudePolygon](./src/ExtrudePolygon.js)

```js
  var mesh = threeLayer.toExtrudePolygon(maptalks.GeoJSON.toGeometry(g), {
        height: levels * heightPerLevel,
        topColor: '#fff'
  }, 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);

 var line = threeLayer.toExtrudeLine(lineString, { altitude: 0, width: 3, height: 1 }, material);

   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;
   });




* getCamera()

  * return THREE.Camera




* getScene()

  * return THREE.Scene




* getThreeRenderer()

  * return THREE.WebGLRenderer




* renderScene() `Render Scene`

  * return this




* addMesh(meshes) `添加图形。请批量添加数据,以便获得更高的渲染性能`

  * meshes Array[BaseObjectr]
  * return this




* removeMesh(meshes) `移除图形。请批量添加数据,以便获得更高的渲染性能`

  * meshes Array[BaseObjectr]
  * return this




* identify(coordinate, options) `图命中检测`

  * coordinate [maptalks.Coordinate]
  * options.count [返回图形元素的数目]
  * return Array[BaseObject]

```js
 map.on('mousemove',function(e){
     threeLayer.identify(e.coordinate);
 });

如何自定义自己的图形

BaseObject是我们提供的图形基类。如果您需要自定义自己的图形,请在此基础上进行扩展

  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);
    }

    ....
   
  }

BaseObject的object3d属性值为three.js原生对象。可以对其执行相关操作,如缩放、位置、旋转等

理论上,您可以自定义所需的任何图形组件 当然,它要求您熟悉three.js