three.js JSONLoader材料未显示

时间:2014-02-05 00:37:07

标签: json three.js

我正在尝试从Maya导入一个对象,这里是json文件

{

    "metadata" :
    {
        "formatVersion" : 3.1,
        "sourceFile"    : "myobj.obj",
        "generatedBy"   : "OBJConverter",
        "vertices"      : 22470,
        "faces"         : 42837,
        "normals"       : 128511,
        "colors"        : 0,
        "uvs"           : 52126,
        "materials"     : 1
    },

    "scale" : 1.000000,

    "materials": [  {
        "DbgColor" : 15658734,
        "DbgIndex" : 0,
        "DbgName" : "lambert2SG",
        "colorAmbient" : [0.0, 0.0, 0.0],
        "colorDiffuse" : [0.0, 0.0, 0.0],
        "illumination" : 4,
        "mapDiffuse" : "mytexture.tga",
        "opticalDensity" : 1.0
    }],
    "vertices": [...] //Omitted because there are too many
    "morphTargets": [],
    "morphColors": [],
    "normals": [...],
    "colors": [],
    "uvs": [...],
    "faces": [...]
}

以下是我加载JSON文件的方法:

这是我如何初始化场景并加载json文件。我有一个很大的远点因为物体很大:

        var scene = new THREE.Scene();
        var camera = new THREE.PerspectiveCamera(75, window.innerWidth/window.innerHeight, 0.1, 100000);
        var renderer = new THREE.WebGLRenderer();
        renderer.setSize(window.innerWidth, window.innerHeight);
        var controls = new THREE.OrbitControls(camera);
        document.body.appendChild(renderer.domElement);

        var jsonLoader = new THREE.JSONLoader();
        var mesh;
        var render = function () {
            stats.update();
            controls.update();
            requestAnimationFrame(render);
            renderer.render(scene, camera);
        }
        jsonLoader.load("Mesh/myobj.js", function(geometry, materials){
            console.log(materials); // This prints an array, whose first element is a MeshLambertMaterial.
            mesh = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial( materials )); 
            // If I remove the second parameter, 
            //the object renders correctly without texture. 
            //Otherwise the screen shows nothing.
            console.log(geometry);
            scene.add(mesh);
        });
        render();
        camera.position.y = 10000;

很抱歉,如果我做了些蠢事。我是three.js的新手。 如果我将纹理应用于对象,则屏幕不显示任何内容。我已经尝试了所有可以找到的东西。请帮忙!

1 个答案:

答案 0 :(得分:0)

您是否已将相机正确添加到场景中? scene.add(camera);