简体   繁体   English

3D 模型未使用 Three.js 加载到 Mapbox

[英]3D Models not loading on Mapbox using Three.js

My problem is I can't load any.gltf file, only a standard one.我的问题是我无法加载任何.gltf 文件,只能加载一个标准文件。 Please read further to understand in detail.请进一步阅读以了解详细信息。 I have the following map on my application, that has that 3D model pointed by the red arrow:我的应用程序上有以下 map,其中有红色箭头指向的 3D model:

地图

The model is a GLFT file from here . model 是来自 此处的 GLFT 文件。 The code I have is provided by Mapbox itself on their documents, here .我拥有的代码由 Mapbox 本身在他们的文档中提供,这里

Here it is how I have it:这就是我的方式:

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');
});

As you can see, I have the model link on the loader.load() function.如您所见,我在loader.load() function 上有 model 链接。 It works perfectly for THAT model.它非常适用于 model。 Yet, when I try other models, locally and instead of the link, I have something like ('./models/file.gltf) it doesn't work.然而,当我在本地尝试其他模型而不是链接时,我有类似('./models/file.gltf)东西,它不起作用。 I can't understand why, and can't seem to make it work.我不明白为什么,似乎无法让它工作。

Not sure what was the issue in your code, probably it was related to the relative path to the files.不确定您的代码中的问题是什么,可能与文件的相对路径有关。

I have created a PR for you in your repo , with a basic node project with 2 examples.在您的 repo 中为您创建了一个 PR ,其中包含一个带有 2 个示例的基本节点项目。 The first example is how to load your 3D model using the standard code from Mapbox.第一个示例是如何使用 Mapbox 的标准代码加载 3D model。 The second example is to do the same using threebox and adding some cool features like selecting, rotating, dragging, bounding box and adding tooltips.第二个示例是使用threebox执行相同的操作,并添加一些很酷的功能,例如选择、旋转、拖动、边界框和添加工具提示。

在此处输入图像描述

On my GLTF loader, when I use that link from the mapbox docs, it loads the object, perfectly:在我的 GLTF 加载器上,当我使用 mapbox 文档中的链接时,它会完美地加载 object:

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;

Yet, it doesn't work when I trade that link for the original repo link like this:但是,当我像这样将该链接换成原始回购链接时,它不起作用:

https://github.com/jscastro76/threebox/blob/master/examples/models/radar/34M_17.gltf

It doesn't crash the app, it simply doesn't load and I get this error:它不会使应用程序崩溃,它根本不会加载,并且我收到此错误: 错误

It's the same when I download all the files into a folder and I use the path like ./folder/34M_17.gltf , it also doesn't load and shows the same error.当我将所有文件下载到一个文件夹中并使用./folder/34M_17.gltf之类的路径时,它也是一样的,它也不会加载并显示相同的错误。 And that's what's weird, working one way, and not the other...这就是奇怪的地方,以一种方式工作,而不是另一种......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM