导入3D模型Threejs

时间:2019-06-02 11:38:21

标签: three.js

我尝试导入3D模型,但不确定我是否做对了。

var loader = new THREE.GLTFLoader();

    loader.load('./CesiumMilkTruck.gltf', 
        function (gltf) {

            scene.add(gltf.scene);
            gltf.animations; 
            gltf.scene;
            gltf.scenes; 
            gltf.cameras; 
            gltf.asset;


        },

        function (xhr) {
            console.log((xhr.loaded / xhr.total * 100 ) + '% loaded' );
        },

        function (error) {
            console.log( 'An error happened = ', error );
        }
    );

我的html是

  <script type="text/javascript" src="js/three.min.js"></script>
  <script type="text/javascript" src="js/GLTFLoader.js"></script>

我在此页面上下载了模型:

link

尤其是我下载了三个文件:

CesiumMilkTruck.gltf
CesiumMilkTruck.png

CesiumMilkTruck0.bin

我遇到了这个错误:

An error happened =  SyntaxError: "JSON.parse: unexpected character at 
line 7 column 1 of the JSON data" parse 

file:///C:/Users/an/Desktop/master/js/GLTFLoader.js:157
load file:///C:/Users/an/Desktop/master/js/GLTFLoader.js:75
load file:///C:/Users/an/Desktop/master/js/three.min.js:715

我错了吗?

对不起,这是我第一次与Threejs合作。

1 个答案:

答案 0 :(得分:0)

GLTF模型可能有问题,也许已损坏?该错误消息使我相信GLTF文件的格式不正确(全为数字)。

这是我使用Threejs和Konva.js构建的飞行模拟器的代码摘录:https://github.com/mpaccione/everest_flight_sim

在下面的代码中,我加载了直升机模型,将其旋转到线框,然后将其添加到包含第一人称相机的组中,以便您似乎在驾驶直升机。

我正在为该项目使用带有browserfy的commonJS模块。

const GLTFLoader = require('three-gltf-loader'),
      miniModelLoader = new GLTFLoader();
// Load Helicopter Model
miniModelLoader.load( './src/models/helicopter/scene.gltf', function(gltf){
    miniModel = gltf.scene;
    miniModel.name = "miniHeli"
    miniModel.rotation.y = -90 * Math.PI / 180; // Radians
    miniModel.position.set( 0, 0, 0 );

    let miniModelMesh = miniModel.children[0].children[0].children[0],
        miniModelMeshArr = [ miniModelMesh.children[0], miniModelMesh.children[1], miniModelMesh.children[2] ];

    for (var i = miniModelMeshArr.length - 1; i >= 0; i--) {
        miniModelMeshArr[i].material.wireframe = true;
    }

    miniHeliGroup.add( miniModel );
} )