Giter VIP home page Giter VIP logo

maptalks.three's Introduction

maptalks.three

CircleCI NPM Version

A maptalks Layer to render with three.js

20220116_213033.mp4

Examples

Install

  • Install with npm: npm install maptalks.three.
  • Download from dist directory.
  • Use unpkg CDN: https://unpkg.com/maptalks.three/dist/maptalks.three.min.js

Incompatible changes

  • three.js >=128 the default umd package is ES6
  • Starting from version 0.16.0, the default umd package is ES6,To fit the new version of three.js about three umd package change
  • If your running environment does not support ES6, we also provide Es5 version maptalks.three.es5.js,This requires the version of three.js < = 127,

Migration from <=v0.5.x to v0.6.0

  • Re-implementated locateCamera, sync with map's projMatrix and viewMatrix.
  • Model's z position is reversed from v0.5.0. So if you have models rendered with v0.5.x, rotation needs to be updated.
  • For THREE <= 0.94, material's side need to set to THREE.BackSide or THREE.DoubleSide to render correctly
    • THREE >= 0.95 doesn't need, maybe due to #14379
  • Add support for THREE >= 0.93
  • Add support for GroupGLLayer

Usage

As a plugin, maptalks.three must be loaded after maptalks.js and three.js in browsers.

<script type="text/javascript" src="https://unpkg.com/[email protected]/build/three.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/maptalks/dist/maptalks.min.js"></script>
<script type="text/javascript" src="https://unpkg.com/maptalks.three/dist/maptalks.three.js"></script>
<script>
var threeLayer = new maptalks.ThreeLayer('t');
threeLayer.prepareToDraw = function (gl, scene, camera) {
    var light = new THREE.DirectionalLight(0xffffff);
    light.position.set(0, -10, -10).normalize();
    scene.add(light);

    var material = new THREE.MeshPhongMaterial();
    countries.features.forEach(function (g) {
        //g is geojson Feature
        var num = g.properties.population;

        var extrudePolygon=threeLayer.toExtrudePolygon(g, { height: num }, material);
        threeLayer.addMesh(extrudePolygon)
    });
};

map.addLayer(threeLayer);
</script>

With ES Modules:

import * as THREE from 'three';
import * as maptalks from 'maptalks';
import { ThreeLayer } from 'maptalks.three';

const map = new maptalks.Map('map', { /* options */ });

const threeLayer = new ThreeLayer('t');
threeLayer.prepareToDraw = function (gl, scene, camera) {
    const light = new THREE.DirectionalLight(0xffffff);
    light.position.set(0, -10, -10).normalize();
    scene.add(light);
    //...
};

threeLayer.addTo(map);

Supported Browsers

IE 11, Chrome, Firefox, other modern and mobile browsers that support WebGL;

API Reference

Contributing

We welcome any kind of contributions including issue reportings, pull requests, documentation corrections, feature requests and any other helps.

Develop

The only source file is index.js.

It is written in ES6, transpiled by babel and tested with mocha and expect.js.

Scripts

  • Install dependencies
$ npm install
  • Watch source changes and generate runnable bundle repeatedly
$ npm run dev
  • Package and generate minified bundles to dist directory
$ npm run build
  • Lint
$ npm run lint

Publication

npm version ${version}
npm publish
npm push master ${version}

maptalks.three's People

Contributors

charlesleocc avatar cq641876623 avatar dependabot[bot] avatar deyihu avatar fuzhenn avatar gnaaobd avatar kekeqy avatar leecheck avatar windynature avatar xiangliliang avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

maptalks.three's Issues

toExtrudeMesh BufferGeometry Not used ?

        const geom = new THREE.ExtrudeGeometry(shape, config);
        const buffGeom = new THREE.BufferGeometry();
        buffGeom.fromGeometry(geom);
        const mesh = new THREE.Mesh(geom, material);

buffGeom Not used?

threejs画的一个飞线 但是没有先显示出来

drawFlyLine(scene) {
					const group = new THREE.Group();
					const flyDatas = [
							{ source: [116.41936,39.911282], target: [116.4194583893, 39.9087815920] },
							{ source: [116.3995456696, 39.9441932102], target: [116.4194583893, 39.9087815920] },
							{ source: [116.4195013046, 39.9290895650], target: [116.4194583893, 39.9087815920] },
							{ source: [116.4326334000,39.8811241043], target: [116.4194583893, 39.9087815920] },
							{ source: [116.4008331299, 39.8651171757], target: [116.4194583893, 39.9087815920] },
							{ source: [116.4139223099, 39.8953822745], target: [116.4194583893, 39.9087815920] }
						];
					flyDatas.forEach(d => {

						const slnglat = d.source;
						const tlnglat = d.target;
						const z = 10;
						const [x1, y1, z1] = this.lnglatToVector3(slnglat);
						const [x2, y2, z2] = this.lnglatToVector3(tlnglat);
						console.log([x1, y1, z1])
						console.log([x2, y2, z2])
						const curve = new THREE.QuadraticBezierCurve3(
							new THREE.Vector3(x1, y1, z1),
							new THREE.Vector3((x1 + x2) / 2, (y1 + y2) / 2, z),
							new THREE.Vector3(x2, y2, z2)
						);
						const points = curve.getPoints(this.pointsLength);
						const geometry = new THREE.Geometry();
						geometry.vertices = points;
						geometry.colors = new Array(points.length).fill(new THREE.Color('#003670'));
						const material = new THREE.LineBasicMaterial({
							vertexColors: THREE.VertexColors,
							transparent: true,
							side: THREE.DoubleSide
						});
						const mesh = new THREE.Line(geometry, material);
						group.add(mesh);
					});
					this.flyGroup = group;
					console.log(group)
					scene.add(group);
				}

这个飞线显示不出来,坐标是正确的。 可能是什么原因呢?

Raycaster 在特定情况下拾取不正常

maptalks v0.45.1
maptalks.three.js: v0.6.2
three.min.js:179 THREE.WebGLRenderer 97

可能和地图canvas的长宽相关

canvas height == canvas style height * 2 && canvas width == canvas style width* 2
上面这种情况无法拾取
调整电脑分辨率,到特定值时

canvas height == canvas style height 1 && canvas width == canvas style width 1
上面这种情况正常拾取

ExtrudeMesh extracted as a component as ExtrudePolygon

for:

class ExtrudePolygon extends BaseObject {
    constructor(polygon, options, material, layer) {
        options = maptalks.Util.extend({}, OPTIONS, options, { layer, polygon });
        super();
        this._initOptions(options);
        const { height, topColor, bottomColor, altitude } = options;
        const geometry = getExtrudeGeometry(polygon, height, layer);
        if (topColor && !material.map) {
            initVertexColors(geometry, bottomColor, topColor);
            material.vertexColors = THREE.VertexColors;
        }
        this._createMesh(geometry, material);
        const z = layer.distanceToVector3(altitude, altitude).x;
        this.getObject3d().position.z = z;
    }
}

a parent class for extrudemesh,bar, line, points, terrain etc

a parent class for extrudemesh,bar, line, points, terrain etc

for example:

class Bar extends BaseObject {
    constructor(coordinate, options, material, layer) {
        options = maptalks.Util.extend({}, OPTIONS, options, { layer, coordinate });
        super();
        this._initOptions(options);
        const { height, radius, topColor, bottomColor, altitude } = options;
        options.height = layer.distanceToVector3(height, height).x;
        options.radius = layer.distanceToVector3(radius, radius).x;
        const geometry = getGeometry(options);
        if (topColor && !material.map) {
            initVertexColors(geometry, bottomColor, topColor);
            material.vertexColors = THREE.VertexColors;
        }
        this._createMesh(geometry, material);
        const z = layer.distanceToVector3(altitude, altitude).x;
        const position = layer.coordinateToVector3(coordinate, z);
        this.getObject3d().position.copy(position);
        this.getObject3d().rotation.x = Math.PI / 2;
        this.getObject3d().translateY(options.height / 2);
    }
}


class Line  extends BaseObject


class Points  extends BaseObject



class Terrain extends BaseObject

....

Its function and maptalks. geometry are as similar as possible.

BaseObject Provide relevant functions. More features may be provided in the future

    getObject3d() {
        return this.object3d;
    }

    getId() {
        return this.id;
    }

    setId() {
        this.id = id;
        return this;
    }

    getType() {

    }

    getOptions() {
        return this.options;
    }

    getProperties() {
        return (this.options || {}).properties;
    }

    setProperties(property) {

        return this;
    }


    getLayer() {
        return this.options.layer;
    }

    show() {

        return this;
    }


    hide() {

        return this;
    }

    isVisible() {

    }


    getSymbol() {


    }

    setSymbol() {

        return this;
    }

    setInfoWindow(options) {

        return this;
    }

    getInfoWindow() {

    }

    openInfoWindow(coordinate) {

    }

    closeInfoWindow() {

    }


    removeInfoWindow() {

    }

    setTooltip(options) {

    }

    removeTooltip() {

    }

    config() {

    }

    on() {

    }

    off() {

    }

添加光晕效果无效

drawPlane(x, y, z, value, i) {
						const hei = value / 10;
						const geometry = new THREE.PlaneGeometry(1, hei);
						const material = new THREE.MeshBasicMaterial({
							map: texturesGZ[i % 2],
							depthTest: false,
							transparent: true,
							color: colorsGZ[i % 2],
							side: THREE.DoubleSide,
							blending: THREE.AdditiveBlending
						});
						const plane = new THREE.Mesh(geometry, material);
						plane.position.set(x, y, z + hei / 2);
						plane.rotation.x = Math.PI / 2;
						plane.rotation.z = Math.PI;
						const plane2 = plane.clone();
						plane2.rotation.y = Math.PI / 2;
						return [plane, plane2];
					}

不用maptalk渲染是没问题的 添加之后就没有了

O

.

加载大模型循环溢出

obj模型400M左右,加载慢致浏览器崩溃,有解决方案吗。。。拆分模型加载还是解决不了速度问题,就放弃了,ui也表示无解,拆分是个大工程

Transformation 转换点报错

new maptalks.Transformation().transform([116.41772800168769,39.89375265237956], 15)

控制台报错 跟踪发现
this.matrix 没有值
大概是什么原因导致的呢

me.toExtrudeGeometry is not a function error

Uncaught TypeError: me.toExtrudeGeometry is not a function
at demo.html:158
at Array.forEach ()
at ThreeLayer.threeLayer.prepareToDraw (demo.html:150)
at ThreeRenderer.t.prepareDrawContext (maptalks.min.js:6)
at ThreeRenderer.t.draw (maptalks.min.js:6)
at ThreeRenderer.r._drawAndRecord (maptalks.min.js:6)
at ThreeRenderer.r._tryToDraw (maptalks.min.js:6)
at ThreeRenderer.r.render (maptalks.min.js:6)
at r.drawLayers (maptalks.min.js:6)
at r.renderFrame (maptalks.min.js:6)

More Functional Support and Improvement of BaseObject

20191010154921

More Functional Support and Improvement of BaseObject

for

   //tooltip test
                mesh.setToolTip(levels * heightPerLevel, {
                    showTimeout: 0,
                    eventsPropagation: true,
                    dx: 10
                });

                //infowindow test
                mesh.setInfoWindow({
                    content: 'hello world,height:' + levels * heightPerLevel,
                    title: 'message',
                    animationDuration: 0,
                    autoOpenOn: false
                });

                // mesh.getInfoWindow().addTo(map);

                //event test
                ['click', 'mousemove', 'mouseout', 'mouseover', 'mousedown', 'mouseup', 'dblclick', 'contextmenu'].forEach(function (eventType) {
                    mesh.on(eventType, function (e) {
                        console.log(e.type, e);
                        // console.log(this);
                        if (e.type === 'mouseout') {
                            this.setSymbol(material);
                        }
                        if (e.type === 'mouseover') {
                            this.setSymbol(highlightmaterial);
                        }
                    });
                });

threeLayer support fire baseObject events, open ToolTip and InfoWinfow

for

/**
     * fire baseObject events
     * @param {*} e 
     */
    _identifyBaseObjectEvents(e) {
        const map = this.map || this.getMap();
        map.resetCursor('default');
        const { type, coordinate } = e;
        const baseObjects = this.identify(coordinate);
        if (type === 'mousemove') {
            if (baseObjects.length) {
                map.setCursor(`pointer`);
            }
            // mouseout objects
            const outBaseObjects = [];
            if (this._baseObjects) {
                this._baseObjects.forEach(baseObject => {
                    let isOut = true;
                    baseObjects.forEach(baseO => {
                        if (baseObject == baseO) {
                            isOut = false;
                        }
                    });
                    (isOut && outBaseObjects.push(baseObject));
                });
            }
            outBaseObjects.forEach(baseObject => {
                if (baseObject instanceof BaseObject) {
                    // reset _mouseover status
                    baseObject._mouseover = false;
                    baseObject._fire('mouseout', Object.assign({}, e, { target: baseObject, type: 'mouseout' }));
                    baseObject.closeToolTip();
                }
            });
            baseObjects.forEach(baseObject => {
                if (baseObject instanceof BaseObject) {
                    if (!baseObject._mouseover) {
                        baseObject._fire('mouseover', Object.assign({}, e, { target: baseObject, type: 'mouseover' }));
                        baseObject._mouseover = true;
                    }
                    baseObject._fire(type, Object.assign({}, e, { target: baseObject }));
                    // tooltip 
                    const tooltip = baseObject.getToolTip();
                    if (tooltip && (!tooltip._owner)) {
                        tooltip.addTo(this._innerMarker);
                    }
                    baseObject.openToolTip(coordinate);
                }
                else {
                    console.warn(baseObject, 'is not BaseObject');
                }
            });
        } else {
            baseObjects.forEach(baseObject => {
                if (baseObject instanceof BaseObject) {
                    baseObject._fire(type, Object.assign({}, e, { target: baseObject }));
                    if (type === 'click') {
                        const infoWindow = baseObject.getInfoWindow();
                        if (infoWindow && (!infoWindow._owner)) {
                            infoWindow.addTo(map);
                        }
                        baseObject.openInfoWindow(coordinate);
                    }
                } else {
                    console.warn(baseObject, 'is not BaseObject');
                }
            });
        }
        this._baseObjects = baseObjects;
        return this;
    }

完善BaseObject的功能和实现,使其的功能和maptalk.Geometry尽可以能的相同,从而提供统一的使用体验,比如on,off,setSymbol,setInfoWindow等一系列方法

camera rewrite for v0.39.0

As maptalks v0.39.0 corrected viewMatrix and projectMatrix, maptalks.three's z value doesn't need to be reversed anymore.

Change to sync three scene's camera with map's by viewMatrix and projectMatrix directly.

BaseObject support custom identify

Layer identification can be implemented by baseobject

Baseobject can have its own identity method, and identity for special mesh can have its own implementation, such as fatline

图层的identify也可以有Baseobject 来提供实现

Baseobject 可以有自己的identity 的方法,针对特殊的mesh的identity 可以有自己去实现,例如fatline

因为有些mesh不好用raycaster来处理的

RayCaster doesn't work

Version: 0.6.0

Will throw:

three.min.js:862 THREE.Raycaster: Unsupported camera type.

三维建筑纹理不显示

我想在绘制的建筑上添加图片纹理,但是不显示图片,代码如下:
var loader = new THREE.TextureLoader();
loader.load("2.jpeg",function(texture){

m = new THREE.MeshPhongMaterial();

m.map = texture;

features.slice(0,1).forEach(function (g) {
    var heightPerLevel = 10;
    var levels = g.properties.levels || 1;
    

          mesh = me.toExtrudeMesh(maptalks.GeoJSON.toGeometry(g), levels * heightPerLevel*10, m);
          if (Array.isArray(mesh)) {
            scene.add.apply(scene, mesh);
          } else {
            scene.add(mesh);
          }

          
      })    


});

};
threeLayer.addTo(map);

export BaseObject for custom component

export BaseObject

open BaseObject so that we can customize our own objects

for example

/**
         * custom component
         * */

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

            hello() {
                return 'hello ' + this.getId();
            }
        }

20191010160012

将BaseObject 暴露出来,允许用户自己去自定义自己的组件

Model container

Model container component

Used to accommodate a model and allow consent to the operation of the model

20191010155351

提供一个模型容器的组件,可以用来容纳各种模型,为所有模型的操作提供统一的方法等

SpotLight 聚光灯效果maptalks下无效

`// scene

			scene = new THREE.Scene();

			//PointLight-点光源
            var pointLight = new THREE.PointLight( 0xdcdbdb );
            pointLight.position.copy(camera.position);
           /* pointLight.distance = 100;//距离
            pointLight.intensity = 1;//强度 默认为1*/
            pointLight.visible =true;//开关,默认开
            scene.add(pointLight);
            //AmbientLight-环境光源
            scene.add(new THREE.AmbientLight(0xffffff,0.2));// soft white light

            // SpotLight-聚光灯
            var spotLight = new THREE.SpotLight(0xf44336,1);
            spotLight.position.copy(camera.position);//位置

            spotLight.castShadow = true;//能否产生阴影 默认不能
            spotLight.shadowCameraNear = 1;//投影近点
            spotLight.shadowCameraFar = 10;//投影远点
            spotLight.shadowCameraFov = 10;//视场有多大
            spotLight.distance = 0;//距离
            spotLight.angle = 0.1;//角度
            
           //spotLight.shadowCameraVisible = true;//开启调试模式

            `scene.add(spotLight);``

灯光配置如上,在threeJs中直接使用有红色聚光灯效果,同等配置在maptalks+threeJs中无效

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.