1

我正在尝试为我的项目确定最好的 Javascript 3D 库。

我喜欢Three.js的不止一点,Scene.js,但似乎更有效的编码风格Scene.js,因为我需要动态创建大约 100 个对象,这意味着使用Three.js.

Three.js似乎对于每个不同的对象,您需要定义变量并将它们添加到场景中,如下所示:

var renderer = new THREE.WebGLRenderer();
var camera =
  new THREE.PerspectiveCamera(
    VIEW_ANGLE,
    ASPECT,
    NEAR,
    FAR);

var scene = new THREE.Scene();

var sphere = new THREE.Mesh(

  new THREE.SphereGeometry(
    50,
    16,
    16),

  sphereMaterial);

scene.add(sphere);

在 中Scene.js,对象以类似 JSON 的结构创建,如下所示:

var scene = SceneJS.createScene({
    nodes:[
        {
            type:"material",
            color: { r: 0.3, g: 0.3, b: 1.0 },

            nodes:[
                {
                    type: "rotate",
                    id: "myRotate",
                    y: 1.0,
                    angle: 0,

                    nodes: [
                        {
                            type:"geometry",
                            source:{
                                type:"teapot"
                            }
                        }
                    ]
                }
            ]
        }
    ]
});

现在我的问题是,是否也可以使用更类似于 JSON 的编码风格Three.js

我已经发现了THREE.JSONLoader,但是由于 Three.js 没有很好的文档,我发现很难发现这是否适合我。它似乎更针对从 3D 软件转换的模型,但我真的需要自己构建对象。

4

2 回答 2

3

Yes you can use JSON to define geometries in Three.js. You can parse JSON directly like this (untested, but at least the idea is there):

    var json = { .... }; // your JSON geometry, not as string but already parsed object 
    var loader = new THREE.JSONLoader();
var result = loader.parse(json, texturePath);
    var mesh = new THREE.Mesh(result.geometry, result.materials);

This can be used to define only geometries (and materials).

I agree that the documentation is sparse. Fortunately all Three.js JSON formats are pretty much human readable. You can check existing example models or take a look at https://github.com/mrdoob/three.js/wiki/JSON-Model-format-3.1 for geometry JSON format to use with JSONLoader.

For complex scene hierarchies (multiple geometries, lights etc) you can use the apparently undocumented ObjectLoader JSON format pretty much in a similar fashion as the JSONLoader format. For checking out the ObjectLoader JSON format, I suggest reading the source at src/loaders/ObjectLoader.js and/or generating sample files using http://threejs.org/editor/ (File->Export Scene / Object)

于 2013-08-06T14:31:19.810 回答
0

您可以查看 GLGE 的“快速表示法”:http ://www.glge.org/ (我还没有深入研究过那个 BTW,所以我不确定它是否符合您的需要)。

如果您使用 SceneJS,请首先确保它的功能满足您的要求,因为它的功能集比 THREE.js 更窄,更多地用于高细节可视化而不是广泛的渲染效果。

如果我是你,我会加入并为 THREE.js 编写你自己的开源 JSON 层,从你需要的开始。我相信您很快就会让贡献者填写其余部分。

于 2013-08-07T12:15:49.627 回答