使用Three.js

时间:2015-07-28 19:01:15

标签: javascript json three.js blender

我正在尝试使用three.js r71在多面体的每个面上显示图像。我通过加载一个JSON文件来制作几何体,该文件包含定义形状的数据。我也在多面体下方设置了一个平面。我收到了一个错误,我想知道这意味着什么或我做错了什么。以下是我在JavaScript控制台中收到的错误消息:

  

[.WebGLRenderingContext-0888D200] GL错误:GL_INVALID_OPERATION:   glDrawElements:尝试访问属性1

中超出范围的顶点      

WebGL:错误太多,不会再向控制台报告此上下文的错误。

以下是JavaScript代码:

var four;

var meshFour;

var scene = new THREE.Scene();

function init() {
    var camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 1000);
    var renderer = new THREE.WebGLRenderer();
    renderer.setClearColor(new THREE.Color(0xEEEEEE, 1.0));
    renderer.setSize(window.innerWidth, window.innerHeight);
    renderer.shadowMapEnabled = true;
    var planeGeometry = new THREE.PlaneGeometry(60, 40, 1, 1);
    var planeMaterial = new THREE.MeshLambertMaterial({color: 0xffffff});
    var plane = new THREE.Mesh(planeGeometry, planeMaterial);
    plane.receiveShadow = true;
    plane.rotation.x = -0.5 * Math.PI;
    plane.position.x = 0;
    plane.position.y = 0;
    plane.position.z = 0;

    //LOADING GEOMETRY
    var loaderFour = new THREE.JSONLoader();
    var materialsArray = [];
    materialsArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture("./resources/images/IPT.PNG")}));
    materialsArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture("./resources/images/Alerts.png")}));
    materialsArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture("./resources/images/action-item-tracking.png")}));
    materialsArray.push(new THREE.MeshBasicMaterial( { map: THREE.ImageUtils.loadTexture("./resources/images/admin.png")}));

    for(var i = 0; i <= 3; i++) {
        materialsArray[i].map.minFilter = THREE.LinearFilter;
    }
    loaderFour.load("./resources/json/tetrahedron-try.json", function (model) {

        var materialFour = new THREE.MeshFaceMaterial(materialsArray);

        four = new THREE.Mesh(model, materialFour);//issue according to three.js
        four.translateY(1);
        four.scale = new THREE.Vector3(3, 3, 3);
        meshFour = THREE.SceneUtils.createMultiMaterialObject(four, materialFour);
        scene.add(four);
    });

    camera.position.x = 20;
    camera.position.y = 20;
    camera.position.z = 20;
    camera.lookAt(new THREE.Vector3(0, 0, 0));

    var spotLight = new THREE.SpotLight(0xffffff);
    spotLight.position.set(-40, 60, 10);
    spotLight.castShadow = true;
    scene.add(spotLight);
    scene.add(plane);


    document.getElementById("WebGL-output").appendChild(renderer.domElement);

    render();

    function render() {
        requestAnimationFrame(render);
        renderer.render(scene, camera);
    }

}

window.onload = init();

JSON文件代码在这里:

{
    "metadata": {
        "type": "Geometry",
        "vertices": 4,
        "uvs": 1,
        "faces": 4,
        "generator": "io_three",
        "version": 3,
        "normals": 4
    },
    "uvs": [[0.250046,0.433025,0.749954,0.433025,0.5,0.865958,0.999907,0.865957,9.3e-05,0.865957,0.5,9.3e-05]],
    "faces": [40,0,1,2,0,1,2,0,1,2,40,3,2,1,3,2,1,3,2,1,40,3,0,2,4,0,2,3,0,2,40,3,1,0,5,1,0,3,1,0],
    "normals": [-0.471389,-0.333323,0.816492,-0.471389,-0.333323,-0.816492,0.942808,-0.333323,0,0,1,0],
    "vertices": [-2.42416,0,4.19877,-2.42416,-0,-4.19877,4.84832,0,-0,-0,6.85655,-0],
    "name": "Tetrahedron.001Geometry.3"
}

2 个答案:

答案 0 :(得分:1)

我有同样的问题,但它与第71版有某种关联。这个问题在更为体面的第72版本中得到了解决,尽管它还没有发布。点击此处:https://github.com/mrdoob/three.js/issues/6774

您可以找到第72个版本here

答案 1 :(得分:1)

在你的json文件中,你有 RewriteRule ^checkout$ ./cart.php?a=add [QSA,L] ,这与我得到的图像一致:

enter image description here enter image description here

所以你只是根据紫外线将一个纹理应用于两个面。 所以问题再次出现在您的模型创建方法中。

这是我的小提琴版本(http://jsfiddle.net/dfnkhbjm/),由于"uvs": 1问题,这是不行的。如果你在本地复制它,它应该可以工作。