1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
var modelOrigin = [120.230, 30.235]; var modelAltitude = 0; var modelRotate = [Math.PI / 2, 0, 0]; var modelScale = 5.41843220338983e-8; var modelTransform = { translateX: mapboxgl.MercatorCoordinate.fromLngLat(modelOrigin, modelAltitude).x, translateY: mapboxgl.MercatorCoordinate.fromLngLat(modelOrigin, modelAltitude).y, translateZ: mapboxgl.MercatorCoordinate.fromLngLat(modelOrigin, modelAltitude).z, rotateX: modelRotate[0], rotateY: modelRotate[1], rotateZ: modelRotate[2], scale: modelScale }; var THREE = window.THREE; var customLayer = { id: '3d-model', type: 'custom', renderingMode: '3d', onAdd: function(map, gl) { this.camera = new THREE.Camera(); this.scene = new THREE.Scene(); var directionalLight = new THREE.DirectionalLight(0xffffff); directionalLight.position.set(0, -70, 100).normalize(); this.scene.add(directionalLight); var directionalLight2 = new THREE.DirectionalLight(0xffffff); directionalLight2.position.set(0, 70, 100).normalize(); this.scene.add(directionalLight2); var loader = new THREE.GLTFLoader(); loader.load('./test.gltf', (function (gltf) { this.scene.add(gltf.scene); }).bind(this)); this.map = map; this.renderer = new THREE.WebGLRenderer({ canvas: map.getCanvas(), context: gl }); 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.elements = matrix; 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); })
|