mrdoob / three.js

JavaScript 3D Library.
https://threejs.org/
MIT License
102.34k stars 35.35k forks source link

JSONLoader won't load ouput from toJSON #8300

Closed fsparv closed 8 years ago

fsparv commented 8 years ago

If this is not a bug, consider it a feature request :)

17:47:43.450 TypeError: vertices is undefined
parseModel() three.js:14990
THREE.JSONLoader.prototype.parse() three.js:14930
THREE.JSONLoader.prototype.load/<() three.js:14906
THREE.XHRLoader.prototype.load/<() three.js:14683
1 three.js:14990:4

when I try to use JSONLoader to load the output from this code:

    <script src="js/ThreeCSG.js"></script>

    var mesh = intersect(
            new THREE.SphereGeometry(50, 20, 20),
            new THREE.SphereGeometry(50, 20, 20),
            new THREE.Vector3(0, -66, 0),
            new THREE.MeshBasicMaterial({color: 'white'}));
    console.log(JSON.stringify(mesh.toJSON()));

    function intersect(geo1, geo2, offsetVector3, material) {
        var mesh1, mesh2, bsp1, bsp2;

        mesh1 = new THREE.Mesh(geo1);
        mesh1.position.add(offsetVector3);
        bsp1 = new ThreeBSP(mesh1);
        mesh2 = new THREE.Mesh(geo2);
        bsp2 = new ThreeBSP(mesh2);
        var resultBsp = bsp1.intersect(bsp2);
        var resultMesh = resultBsp.toMesh(material);
        resultMesh.geometry.computeVertexNormals();

        return resultMesh;
    }

Mesh thus created renders just fine in the browser and in AltspaceVR, but when I tried to store the pre-computed mesh to speed things up I got the above error.

Loading is like this (It dies before I get to "do stuff"):

var GOBAN = {
        objLoader: new THREE.JSONLoader(),
}

then later

GOBAN.objLoader.load("js/stone.json", function (geom, mat) {/*do stuff*/});

I notice that the JSON produced has verticies in a deeper level, and that the THREE code is looking for them on the top level...

{
  "metadata": {
    "version": 4.4,
    "type": "Object",
    "generator": "Object3D.toJSON"
  },
  "geometries": [
    {
      "uuid": "472B9580-2C78-4B39-96B9-33F6A4AA8167",
      "type": "Geometry",
      "data": {
        "vertices": [
          0,
          50,
          0,
          -7.821723461151123,
          49.38441848754883,

produced in r73 from here:

<script src="http://sdk.altvr.com/libs/three.js/r73/build/three.js"></script>
mrdoob commented 8 years ago

You should use ObjectLoader instead.