使用Three.js无法在Mapbox上加载3D模型

蒂亚戈·里贝罗

我的问题是我无法加载任何.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] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

为什么three.js投射阴影无法在3D模型上运行

来自分类Dev

无法使用three.js更改3D模型的颜色

来自分类Dev

使用mapbox更新3d模型位置

来自分类Dev

使用Three.js加载Maya模型

来自分类Dev

three.js 3d模型作为超链接

来自分类Dev

无法使用Three.js加载动画

来自分类Dev

无法使用ThreeJS和React导入3D模型

来自分类Dev

使用Webpack加载GLB模型-Three.js

来自分类Dev

如何使用three.js加载VRML模型(wrl)

来自分类Dev

使用 js 获取 3d 模型的尺寸(大小)

来自分类Dev

Three.js使用正交相机将2D映射到3D

来自分类Dev

使用three.js从2D旋转到3D

来自分类Dev

Three.js-我想在某个地方使我的爱尔兰3D模型可点击

来自分类Dev

拖动以更改Three.js场景中3D模型对象的位置

来自分类Dev

在运行时从 localfile 导入 Three.js 中的 3d 模型

来自分类Dev

Three.js .obj加载程序无法与3Ds Max .obj一起使用

来自分类Dev

如何使用three.js将3d框放置在另一个3d框内?

来自分类常见问题

使用THREE.js TransformControls

来自分类Dev

使用THREE.js TransformControls

来自分类Dev

Modernizr 3D转换无法在Chrome上使用

来自分类Dev

如何使用three.js绘制自定义3d曲面?

来自分类Dev

使用THREE.js通过坐标自定义3D对象

来自分类Dev

更改使用Three.js创建的3D“立方体”的角度

来自分类Dev

如何使用three.js绘制自定义3d曲面?

来自分类Dev

如何使用three.js从随机点制作3D对象?

来自分类Dev

使用three.js在3D场景中绘制不规则水

来自分类Dev

Three js 3D rotation

来自分类Dev

初始加载时,在Three.JS Canvas中适合3D对象(Collada文件)

来自分类Dev

Three.js-如何使用Three.js冒烟?