javascript-使用Three.js加载Maya模型

我正在关注关于如何使用Three.js加载Maya模型的this教程.

一切都很好,但是本教程仅说明了如何使用一种纹理加载模型.

这是教程中的源代码:

function createScene(geometry, x, y, z, scale, tmap) {
            zmesh = new THREE.Mesh(geometry, new THREE.MeshLambertMaterial({map: THREE.ImageUtils.loadTexture(tmap)}));
            zmesh.position.set(x, y, z);
            zmesh.scale.set(scale, scale, scale);
            meshes.push(zmesh);
            scene.add(zmesh);
        }

全JS Live Link

            var SCREEN_WIDTH = window.innerWidth;
        var SCREEN_HEIGHT = window.innerHeight;

        var container;

        var camera, scene;
        var canvasRenderer, webglRenderer;

        var mesh, zmesh, geometry, materials;

        var windowHalfX = window.innerWidth / 2;
        var windowHalfY = window.innerHeight / 2;

        var meshes = [];

        init();
        animate();

        function init() {

            container = document.createElement('div');
            document.body.appendChild(container);

            camera = new THREE.PerspectiveCamera(75, SCREEN_WIDTH / SCREEN_HEIGHT, 1, 100000);
            camera.position.x = 400;
            camera.position.y = 200;
            camera.position.z = 400;

            scene = new THREE.Scene();

            // LIGHTS
            var ambient = new THREE.AmbientLight(0x666666);
            scene.add(ambient);

            var directionalLight = new THREE.DirectionalLight(0xffeedd);
            directionalLight.position.set(0, 70, 100).normalize();
            scene.add(directionalLight);

            // RENDERER
            webglRenderer = new THREE.WebGLRenderer();
            webglRenderer.setSize(SCREEN_WIDTH, SCREEN_HEIGHT);
            webglRenderer.domElement.style.position = "relative";

            container.appendChild(webglRenderer.domElement);
            var loader = new THREE.JSONLoader(),
                callbackKey = function (geometry, materials) {
                    createScene(geometry, materials, 0, 0, 0, 6);
                };
            loader.load("chameleon.js", callbackKey);

            window.addEventListener('resize', onWindowResize, false);

        }

        function createScene(geometry, materials, x, y, z, scale) {


            zmesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial(materials));
            zmesh.position.set(x, y, z);
            zmesh.scale.set(scale, scale, scale);
            meshes.push(zmesh);
            scene.add(zmesh);

        }

        function onWindowResize() {

            windowHalfX = window.innerWidth / 2;
            windowHalfY = window.innerHeight / 2;

            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();

            webglRenderer.setSize(window.innerWidth, window.innerHeight);
        }

        function animate() {
            for (var i = 0; i < meshes.length; i++) {
                meshes[i].rotation.y += 0.01;
            }
            requestAnimationFrame(animate);
            render();
        }

        function render() {
            camera.lookAt(scene.position);
            webglRenderer.render(scene, camera);
        }

但是我的模型有四个纹理.我应该改变什么来加载所有这些?Live Link

解决方法:

看来您所遵循的教程忽略了JSON模型格式的材料,而只是将几何图形和纯文本引用传递给单个纹理文件,如下所示:

var loader = new THREE.JSONLoader(),
    callbackKey = function(geometry) {createScene(geometry,  0, 0, 0, 15, "chameleon.jpg")};
loader.load("chameleon.js", callbackKey);

JSONLoader不仅提取几何图形,而且提取数组中的所有材料. (请参阅:https://github.com/mrdoob/three.js/blob/master/src/loaders/JSONLoader.js行45)然后可以像下面这样将数组传递给MeshFaceMaterial(arrayOfMaterials):

var loader = new THREE.JSONLoader();,
    callbackKey = function(geometry, materials) {createScene(geometry, materials, 0, 0, 0, 15, )};
loader.load("chameleon.js", callbackKey);

然后在createScene函数中,将第一行更改为:

zmesh = new THREE.Mesh(geometry, new THREE.MeshFaceMaterial(materials));

编辑:添加有关修复Maya导出的详细信息

因此,您的模型正在加载但黑色.在这种情况下,问题出在模型文件chameleon.js中.看一下每种材质的colorAmbient和colorDiffuse属性.注意它们都是[0.0,0.0,0.0].这是Maya中已知的obj导出错误.因此,您有3个解决方案.

1)打开chameleon.js文件,并将所有colorAmbient和colorDiffuse行更改为如下所示(您需要使用这些值以使其看起来正确)

"colorAmbient" : [0.8, 0.8, 0.8],
"colorDiffuse" : [0.8, 0.8, 0.8],

要么

2)在Maya中,应用漫反射贴图之前,请始终确保先应用默认颜色值.由于某种原因,一旦地图打开,您将无法再访问color属性,并且导出器将使用默认值0.

要么

3)从Maya导出后,您可以通过更改以下几行来更改OBJ文件:

Kd 0.00 0.00 0.00

Kd 0.80 0.80 0.80

我已经在家里对此进行了测试,您的模型看起来不错,请告诉我情况如何?

上一篇:WebGL简易教程(十):光照


下一篇:javascript – 如何在threejs中克隆collada模型?