maptalks.mapboxgl加載 .dae 3D模型,攝像頭懸浮

拿到的是.skp 3d模型文件,下載SketchUp軟件.安裝后打開模型文件项阴,導(dǎo)出三維模型


image.png
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8"/>
    <title>Add a 3D model</title>
    <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no"/>
    <link type="text/css" rel="stylesheet" href="dist/maptalks.css">
    <script type="text/javascript" src="dist/maptalks.min.js"></script>
    <script src='dist/mapbox-gl.js'></script>
    <link href='dist/mapbox-gl.css' rel='stylesheet' />
    <script type="text/javascript" src="dist/maptalks.mapboxgl.js"></script>
    <script src='https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-language/v0.10.1/mapbox-gl-language.js'></script>
    <style>
        body {
            margin: 0;
            padding: 0;
        }

        #map {
            position: absolute;
            top: 0;
            bottom: 0;
            width: 100%;
        }
    </style>
</head>
<body>
<script src="../build/three.js"></script>
<script src="js/loaders/ColladaLoader.js"></script>
<div id="map"></div>
<script>
    mapboxgl.accessToken = 'pk.eyJ1Ijoiemh1bGlwaWFvIiwiYSI6ImNqemtsbXZwdzBtNjAzZG9hOWt4Z21ocHUifQ.dsdPomNsO7w4Q-rRduoBcg';
    
    var baseLayer = new maptalks.MapboxglLayer('tile',{
        glOptions : {
            'style' : 'mapbox://styles/mapbox/dark-v9'
        }
    });
    
    var mapT = new maptalks.Map("map",{
        //limit max pitch to 60 as mapbox-gl-js required
        maxPitch : 60,
        center: [121.509758, 31.300800],
        zoom   :  16,
        pitch: 60,
        bearing: 10,
        baseLayer : baseLayer
    });
    var markers=[];//所有攝像頭
    let cameraWidth=15,cameraHeight=15,textSize=4;
     //根據(jù)層級來控制攝像頭大小
     mapT.on('zoomend',function(zoom){
        let temp=zoom.from-zoom.to;
        if(temp>0){
            temp=1.2*temp;
            cameraWidth=cameraWidth/temp;
            cameraHeight=cameraHeight/temp;
            textSize=textSize/temp;
        }else if(temp<0){
            temp=Math.abs(temp)*1.2;
            cameraWidth=cameraWidth*temp;
            cameraHeight=cameraHeight*temp;
            textSize=textSize*temp;
        }
        markers.forEach(function(g){
            g.updateSymbol({
            'markerWidth' : cameraWidth,
            'markerHeight' : cameraHeight,
            'textSize' : textSize
          });
        });
     });
     
     var layer=new maptalks.VectorLayer('vector',{
        enableAltitude : true,        // enable altitude
        altitudeProperty : 'altitude' // altitude property in properties, default by 'altitude'
      }).addTo(mapT);
      
      
      var marker1 = new maptalks.Marker(
        [121.510758, 31.301800],
        {
          symbol: 
          {
            'markerFile'   : 'dist/2.png',//攝像頭圖片
            'markerWidth'  : cameraWidth,
            'markerHeight' : cameraHeight,
            'markerDx'     : 0,
            'markerDy'     : 0,
            'markerOpacity': 1,
            'textFaceName' : 'sans-serif',
            'textName' : '{name}',
            'textSize' : textSize,
            'textDy'   : 3
          },
          properties : {
            altitude : 20,
            name:'攝像頭1'
          }
        }
      );
    markers.push(marker1);
    layer.addGeometry(markers);

    var map=baseLayer.getGlMap();
    mapboxgl.setRTLTextPlugin('https://api.mapbox.com/mapbox-gl-js/plugins/mapbox-gl-rtl-text/v0.1.0/mapbox-gl-rtl-text.js');
    map.addControl(new MapboxLanguage({
      defaultLanguage: 'zh'
    }));

    // parameters to ensure the model is georeferenced correctly on the map
    var modelOrigin = [121.509758, 31.300800];
    var modelAltitude = 0;
    var modelRotate = [Math.PI / 2, 0, 0];

    var modelAsMercatorCoordinate = mapboxgl.MercatorCoordinate.fromLngLat(
        modelOrigin,
        modelAltitude
    );

    
    // transformation parameters to position, rotate and scale the 3D model onto the map
    var modelTransform = {
        translateX: modelAsMercatorCoordinate.x,
        translateY: modelAsMercatorCoordinate.y,
        translateZ: modelAsMercatorCoordinate.z,
        rotateX: modelRotate[0],
        rotateY: modelRotate[1],
        rotateZ: modelRotate[2],
        /* Since our 3D model is in real world meters, a scale transform needs to be
        * applied since the CustomLayerInterface expects units in MercatorCoordinates.
        */
        scale: modelAsMercatorCoordinate.meterInMercatorCoordinateUnits()
    };

    var THREE = window.THREE;

    // configuration of the custom layer for a 3D model per the CustomLayerInterface
    var customLayer = {
        id: '3d-model',
        type: 'custom',
        renderingMode: '3d',
        onAdd: function (map, gl) {
            this.camera = new THREE.Camera();
            this.scene = new THREE.Scene();

            // create two three.js lights to illuminate the model
            var directionalLight = new THREE.DirectionalLight(0xcccccc);
            directionalLight.position.set(0, -70, 100).normalize();
            this.scene.add(directionalLight);

            var directionalLight2 = new THREE.DirectionalLight(0xcccccc);
            directionalLight2.position.set(0, 70, 100).normalize();
            this.scene.add(directionalLight2);
            
            // use the three.js GLTF loader to add the 3D model to the three.js scene
            var loader = new THREE.ColladaLoader();
            loader.load('./models/collada/五角場.dae',
                function (gltf) {
                    this.scene.add(gltf.scene);
                }.bind(this)
            );
            this.map = map;

            // use the Mapbox GL JS map canvas for three.js
            this.renderer = new THREE.WebGLRenderer({
                canvas: map.getCanvas(),
                context: gl,
                antialias: true
            });
            this.renderer.autoClear = false;
        },
        render: function (gl, matrix) {
            var rotationX = new THREE.Matrix4().makeRotationAxis(
                new THREE.Vector3(1, 0, 0),
                modelTransform.rotateX
            );
            var rotationY = new THREE.Matrix4().makeRotationAxis(
                new THREE.Vector3(0, 1, 0),
                modelTransform.rotateY
            );
            var rotationZ = new THREE.Matrix4().makeRotationAxis(
                new THREE.Vector3(0, 0, 1),
                modelTransform.rotateZ
            );

            var m = new THREE.Matrix4().fromArray(matrix);
            var l = new THREE.Matrix4()
                .makeTranslation(
                    modelTransform.translateX,
                    modelTransform.translateY,
                    modelTransform.translateZ
                )
                .scale(
                    new THREE.Vector3(
                        modelTransform.scale,
                        -modelTransform.scale,
                        modelTransform.scale
                    )
                )
                .multiply(rotationX)
                .multiply(rotationY)
                .multiply(rotationZ);

            this.camera.projectionMatrix = m.multiply(l);
            this.renderer.state.reset();
            this.renderer.render(this.scene, this.camera);
            this.map.triggerRepaint();
        }
    };

    map.on('style.load', function () {
        map.addLayer(customLayer, 'waterway-label');
    });
</script>

</body>
</html>

效果圖:


image.png
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末邮府,一起剝皮案震驚了整個(gè)濱河市势就,隨后出現(xiàn)的幾起案子颅眶,更是在濱河造成了極大的恐慌塔拳,老刑警劉巖欠橘,帶你破解...
    沈念sama閱讀 218,525評論 6 507
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異赞季,居然都是意外死亡庆杜,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,203評論 3 395
  • 文/潘曉璐 我一進(jìn)店門碟摆,熙熙樓的掌柜王于貴愁眉苦臉地迎上來晃财,“玉大人,你說我怎么就攤上這事典蜕《鲜ⅲ” “怎么了?”我有些...
    開封第一講書人閱讀 164,862評論 0 354
  • 文/不壞的土叔 我叫張陵愉舔,是天一觀的道長钢猛。 經(jīng)常有香客問我,道長轩缤,這世上最難降的妖魔是什么命迈? 我笑而不...
    開封第一講書人閱讀 58,728評論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮火的,結(jié)果婚禮上壶愤,老公的妹妹穿的比我還像新娘。我一直安慰自己馏鹤,他們只是感情好征椒,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,743評論 6 392
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著湃累,像睡著了一般勃救。 火紅的嫁衣襯著肌膚如雪碍讨。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,590評論 1 305
  • 那天蒙秒,我揣著相機(jī)與錄音勃黍,去河邊找鬼。 笑死晕讲,一個(gè)胖子當(dāng)著我的面吹牛覆获,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播益兄,決...
    沈念sama閱讀 40,330評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼锻梳,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了净捅?” 一聲冷哼從身側(cè)響起疑枯,我...
    開封第一講書人閱讀 39,244評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蛔六,沒想到半個(gè)月后荆永,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,693評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡国章,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,885評論 3 336
  • 正文 我和宋清朗相戀三年具钥,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片液兽。...
    茶點(diǎn)故事閱讀 40,001評論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡骂删,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出四啰,到底是詐尸還是另有隱情宁玫,我是刑警寧澤,帶...
    沈念sama閱讀 35,723評論 5 346
  • 正文 年R本政府宣布柑晒,位于F島的核電站欧瘪,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏匙赞。R本人自食惡果不足惜佛掖,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,343評論 3 330
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望涌庭。 院中可真熱鬧芥被,春花似錦、人聲如沸脾猛。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,919評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽猛拴。三九已至羹铅,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間愉昆,已是汗流浹背职员。 一陣腳步聲響...
    開封第一講書人閱讀 33,042評論 1 270
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留跛溉,地道東北人焊切。 一個(gè)月前我還...
    沈念sama閱讀 48,191評論 3 370
  • 正文 我出身青樓,卻偏偏與公主長得像芳室,于是被迫代替她去往敵國和親专肪。 傳聞我的和親對象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,955評論 2 355