使用threebox整合mapbox gl和three js DEMO

准备

three.js

mapbox-gl.js

mapbox-gl.css

threebox.js

DEMO

<!doctype html>
<head>
    <title>Threebox example</title>
    <script src="../dist/threebox.js" type="text/javascript"></script>

    <script src='mapbox-gl.js'></script>
    <link href='mapbox-gl.css' rel='stylesheet' />
    <style>
        body, html { 
            width: 100%;
            height: 100%;
            margin: 0;
        }
        #map { 
            width: 100%;
            height: 100%;
        }
    </style>
</head>
<body>
    <div id='map' class='map'></div>

    <script>
    mapboxgl.accessToken = 'pk.eyJ1Ijoia3JvbmljayIsImEiOiJjaWxyZGZwcHQwOHRidWxrbnd0OTB0cDBzIn0.u2R3NY5PnevWH3cHRk6TWQ';
    var map = new mapboxgl.Map({
      container: 'map',
      style: 'https://raw.githubusercontent.com/osm2vectortiles/mapbox-gl-styles/master/styles/bright-v9-cdn.json',
      center: [-122.4340, 37.7353],
      zoom: 8.55,
      pitch: 60,
      heading: 41,
      hash: true
    });

    var highlighted = [];

    map.on("load", function() {
        // Initialize threebox
        window.threebox = new Threebox(map);
        threebox.setupDefaultLights();

        // initialize geometry and material of our cube object
        
        var lng = -122.4340;
        var lat = 37.7353;
        
        for(x=1;x<10;x++){
            for(y=1;y<=10;y++){
                //new THREE.BoxGeometry(500, 500, 0);
                 var geometry = new THREE.CylinderGeometry(5,5,60,64,1,false);
                 
                    var greenMaterial = new THREE.MeshPhongMaterial( {color: 0xaaffaa, side: THREE.DoubleSide});
                    var redMaterial = new THREE.MeshPhongMaterial( {color: 0xff0000, side: THREE.DoubleSide});

                    var cube = new THREE.Mesh(geometry, redMaterial);
                    cube.rotation.x += 1.5;
                    cube.userData.name = "Red cube";
                    threebox.addAtCoordinate(cube, [-122.4340 + (x/1000), 37.7353 + (y/1000), 0], {preScale: 1});
            }
        }
        
        
       
        

        //add mousing interactions
        map.on('mousemove', function(e){

            raycaster = new THREE.Raycaster();
            var mouse = new THREE.Vector2();

            // Clear old objects
            highlighted.forEach(function(h) {
                h.material = redMaterial;
            });
            highlighted.length = 0;

            // scale mouse pixel position to a percentage of the screen's width and height
            mouse.x = ( e.point.x / threebox.map.transform.width ) * 2 - 1;
            mouse.y = -( ( e.point.y) / threebox.map.transform.height ) * 2 + 1;

            raycaster.setFromCamera(mouse, threebox.camera);

            // calculate objects intersecting the picking ray
            var intersects = raycaster.intersectObjects(threebox.world.children, true);
            if (!intersects[0]) return
            var nearestObject = intersects[0].object;
            //console.log(nearestObject);
            nearestObject.material = greenMaterial;
            highlighted.push(nearestObject)
            
        });
        

    });

    </script>
</body>

 

效果

 

原理

mapbox gl基本保持不变,通过threebox来进行对thee js的转换,使其能够把场景scene、相机camera转换同步到mapbox gl引擎中,各自渲染render不变,互补影响

转载于:https://www.cnblogs.com/lilei2blog/p/8622473.html

可以使用`v-for`指令结合`:class`绑定来动态渲染多个元素,并且通过Vue的数据驱动特性来控制它们的显示与隐藏。具体实现可以参考以下代码: ```html <template> <div> <div v-for="(item, index) in elements" :key="index" :class="[item.visible ? 'animate__animated animate__fadeIn' : 'animate__animated animate__fadeOut', 'threebox_image_box']" > 这是第 {{ index + 1 }} 个元素 </div> <button @click="hideElements">隐藏元素</button> </div> </template> <script> export default { data() { return { elements: [ { visible: true }, { visible: true }, { visible: true }, { visible: true }, { visible: true }, { visible: true }, { visible: true }, { visible: true } ] }; }, methods: { hideElements() { for (let i = 0; i < this.elements.length; i++) { this.elements[i].visible = false; } } } }; </script> <style> .threebox_image_box { display: block; } .animate__animated { animation-duration: 1s; } .animate__fadeIn { animation-name: fadeIn; } .animate__fadeOut { animation-name: fadeOut; } @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } @keyframes fadeOut { from { opacity: 1; } to { opacity: 0; } } </style> ``` 在上面的代码中,我们首先定义了一个`elements`数组,它包含了需要动态渲染的多个元素,并且每个元素都有一个`visible`属性,用于控制它们的显示与隐藏。然后在模板中使用`v-for`指令循环渲染多个元素,并且通过`:class`绑定来动态设置元素的`class`属性,以便实现渐变动画效果。最后在方法中,我们通过修改`elements`数组中每个元素的`visible`属性,来控制它们的显示与隐藏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值