Threebox example

by jscastro76

HTML

<script src="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.js"></script>
<link rel="stylesheet" href="https://api.mapbox.com/mapbox-gl-js/v1.11.1/mapbox-gl.css">
<script src="https://unpkg.com/threebox-plugin/dist/threebox.min.js"></script>
<link rel="stylesheet" href="https://unpkg.com/threebox-plugin/examples/css/threebox.css">
<div id='map' class='map'></div>

CSS

body, html {
			width: 100%;
			height: 100%;
			margin: 0;
		}

		#map {
			width: 100%;
			height: 100%;
		}

JavaScript

mapboxgl.accessToken = "pk.eyJ1IjoianNjYXN0cm8iLCJhIjoiY2s2YzB6Z25kMDVhejNrbXNpcmtjNGtpbiJ9.28ynPf1Y5Q8EyB_moOHylw";

		//starting location for both map and eventual sphere
		var origin = [-122.4340, 37.7353, 1];

		var map = new mapboxgl.Map({
		  container: 'map',
		  style: 'mapbox://styles/mapbox/light-v9',
		  center: origin,
		  zoom: 17,
		  pitch: 60,
		  antialias: true
		});

		let stats;
		import Stats from 'https://threejs.org/examples/jsm/libs/stats.module.js';

		function animate() {
		  requestAnimationFrame(animate);
		  stats.update();
		}

		map.on('style.load', function() {
		  // stats
		  stats = new Stats();
		  map.getContainer().appendChild(stats.dom);
		  animate();

		  map.addLayer({
		    id: 'custom_layer',
		    type: 'custom',
		    renderingMode: '3d',
		    onAdd: function(map, mbxContext) {

		      // instantiate threebox
		      window.tb = new Threebox(
		        map,
		        mbxContext, {
		          defaultLights: true,
		          enableSelectingObjects: true
		        }
		      );

		      //instantiate a red sphere and position it at the origin lnglat
		      var sphere = tb.sphere({
		          color: 'red',
		          material: 'MeshToonMaterial'
		        })
		        .setCoords(origin);
		      // add sphere to the scene
		      sphere.addEventListener('ObjectMouseOver', onObjectMouseOver, false);
		      sphere.addEventListener('ObjectMouseOut', onObjectMouseOut, false);
		      tb.add(sphere);

		    },

		    render: function(gl, matrix) {
		      tb.update();
		    }
		  })
		});


		//actions to execute onObjectMouseOver
		function onObjectMouseOver(e) {
		  console.log("ObjectMouseOver: " + e.detail.name);
		}

		//actions to execute onObjectMouseOut
		function onObjectMouseOut(e) {
		  console.log("ObjectMouseOut: " + e.detail.name);
		}