我的问题是我无法加载任何.gltf文件,只能加载标准文件。请进一步阅读以了解详细信息。我的应用程序上有以下地图,该地图具有红色箭头所指的3D模型:
该模型是这里的GLFT文件。我的代码是由Mapbox本身提供了自己的文件,在这里。
这就是我的方法:
var modelOrigin = [-8.629134, 41.157902];
var modelAltitude = 0;
var modelRotate = [Math.PI / 2, 0, 0];
var modelAsMercatorCoordinate = mapboxgl.MercatorCoordinate.fromLngLat(modelOrigin,modelAltitude);
var modelTransform = {translateX: modelAsMercatorCoordinate.x,translateY: modelAsMercatorCoordinate.y,translateZ: modelAsMercatorCoordinate.z,rotateX: modelRotate[0],rotateY: modelRotate[1],rotateZ: modelRotate[2],scale: modelAsMercatorCoordinate.meterInMercatorCoordinateUnits()};
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();
// create two three.js lights to illuminate the model
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);
// use the three.js GLTF loader to add the 3D model to the three.js scene
var loader = new THREE.GLTFLoader();
loader.load('https://docs.mapbox.com/mapbox-gl-js/assets/34M_17/34M_17.gltf',function (gltf) {
this.scene.add(gltf.scene);
}.bind(this));
this.map = map;
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('load', async () => {
map.addLayer(customLayer, 'waterway-label');
});
如您所见,我在loader.load()
函数上有模型链接。它非常适合THAT模型。但是,当我在本地而不是链接上尝试其他模型时,我发现('./models/file.gltf)
它不起作用。我不明白为什么,而且似乎也无法使它起作用。
不确定代码中的问题是什么,可能与文件的相对路径有关。
我在您的repo中为您创建了一个PR,其中包含一个带有2个示例的基本节点项目。第一个示例是如何使用Mapbox中的标准代码加载3D模型。第二个示例是使用threebox并添加一些很酷的功能(例如选择,旋转,拖动,边界框和添加工具提示)来执行相同的操作。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句