如何从我的json结构中旋转特定对象?

时间:2016-05-26 12:00:52

标签: javascript json three.js

我在three.js / editor中构建了一个场景,并将场景导出到我现在使用的JSON文件中。我可以对相机和轨道控件进行编程,但我不知道如何从我的JSON结构中选择特定对象。有没有办法做到这一点?如果是这样,我怎么能"目标"一个特定的对象?我可以使用" uuid"? 这是我的JSON代码:

{
"metadata": {
    "version": 4.4,
    "type": "Object",
    "generator": "Object3D.toJSON"
},
"geometries": [
    {
        "uuid": "DEB90436-B316-4E49-83A6-323712AA3A78",
        "type": "TorusGeometry",
        "radius": 1,
        "tube": 0.34,
        "radialSegments": 42,
        "tubularSegments": 44,
        "arc": 6.32,
    },
    {
        "uuid": "0F8E3492-4B1B-436A-973C-7F8433AA7582",
        "type": "PlaneGeometry",
        "width": 2,
        "height": 2
    }                    /...and so on...
]

这是我的JavaScript:

var scene, camera, renderer;

init();
animate();

function init() {

    // Creating the scene and set the scene size.
    scene = new THREE.Scene();
    var WIDTH = window.innerWidth,
        HEIGHT = window.innerHeight;

    // Creating a renderer and add it to the DOM.
    renderer = new THREE.WebGLRenderer({antialias:true});
    renderer.setSize(WIDTH, HEIGHT);
    document.body.appendChild(renderer.domElement);

    // Create a camera, zoom it out from the model a bit, and add it to the scene.
        camera = new THREE.OrthographicCamera( window.innerWidth / - 2, window.innerWidth / 2, window.innerHeight / 2, window.innerHeight / - 2, - 2000, 1000 );
        camera.position.x = 200;
        camera.position.y = 100;
        camera.position.z = 200;
     scene.add(camera);



 // Loading in the mesh and add it to the scene. THIS is were I get the JSON structure in...
 var loader = new THREE.ObjectLoader();
 loader.load("scene.json",function ( obj ) {
 scene.add( obj );
 });

 // OrbitControls
controls = new THREE.OrbitControls(camera, renderer.domElement);
controls.enablePan = false;
            controls.minDistance = 6    ; // how far can you zoom in
controls.maxDistance = 71; // how far can you zoom out

  }

function animate() {


     // Render the scene.
     renderer.render(scene, camera);
     controls.update();

   }

有没有办法

1 个答案:

答案 0 :(得分:0)

Object3D class有一个名为java.lang.NoClassDefFoundError的方法。用法示例:

getObjectByName

您可以在loader.load("scene.json",function ( obj ) { scene.add( obj ); var targetObject = obj.getObjectByName('name of object from scene.json'); // Use targetObject here... }); 文件中找到更进一步的对象名称。查找scene.json部分。