且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

导出场景从Three.js编辑器并导入

更新时间:2023-02-03 14:57:31

Develotecca的答案显示了如何从JSON文件加载基本的THREE.geometry。然而,根据我的经验,由three.js编辑器导出的几何形状是BufferGeometry类型(它比基本的几何体更有效),因此需要使用THREE.BufferGeometryLoader而不是THREE.JSONLoader加载它们。



另外,问题是关于保存和加载场景,而不是几何。 JSONLoader仅用于加载基本几何,几何只包含单一模型的每顶点和每面信息(其中包含用于索引到MeshfaceMatrial中的材料编号,但不包含其他材料信息,因此需要与使用前的材料)。如果您尝试使用JSONLoader加载整个场景,而不是场景中一个对象的一部分,加载程序应该发现并传递消息


THREE.JSONLoader:似乎是一个场景。使用THREE.SceneLoader代替。


到控制台日志。这给出了一个很好的线索,以正确的方式继续。



场景加载程序记录在 http://threejs.org/docs/#Reference/Loaders/SceneLoader (尽管文档目前不完整),其源代码位于 https://github.com/mrdoob/three.js/blob /master/src/loaders/SceneLoader.js ,其使用示例来自 http: //threejs.org/examples/webgl_loader_scene.html



所有这一切都是很多的。我自己还没有实际使用过SceneLoader,尽管我很快就打算,但是从目前看来,它看起来类似于BufferGeometryLoader或JSONLoader,除非你加载一个整个场景,而不是只有一部分你有

  scene = loaded.scene 

而不是

  scene.add()
/ pre>

,您可能需要为场景使用的任何特殊几何包含其他装载程序和处理程序,例如

 < script src =js / loaders / ColladaLoader.js>< / script> 
..
loader.addHierarchyHandler(dae,THREE.ColladaLoader); Collada的


I make a simple scene using the editor of three.js working in local. When i'm finished the scene i will go to "file" -> "export scene" and the editor generate JSON Object/Scene. Now i will copy and paste this code and save like a .js? How i can import this scene in my project preserving the textures?

Thanks !

Develotecca's answer shows how to load a basic THREE.geometry from a JSON file. However, in my experience, the geometries exported by the three.js editor are of type BufferGeometry (which is more efficient than a basic Geometry) so they need to be loaded using a THREE.BufferGeometryLoader rather than a THREE.JSONLoader.

Also, the question is about saving and loading scenes, not geometries. JSONLoader is designed only to load basic geometries, and a geometry contains only a single model's per-vertex and per-face information (which includes material numbers for indexing into a MeshfaceMatrial, but no other material information, and so it needs to be combined with a material before use). If you try to load an entire scene using JSONLoader, instead of just a part of one object in the scene, the loader should spot this and deliver the message

THREE.JSONLoader: seems to be a Scene. Use THREE.SceneLoader instead.'

to the console log. This gives a big clue to the proper way to proceed.

The scene loader is documented at http://threejs.org/docs/#Reference/Loaders/SceneLoader (though the documentation is currently incomplete), and its source code is at https://github.com/mrdoob/three.js/blob/master/src/loaders/SceneLoader.js and an example of its use is at http://threejs.org/examples/webgl_loader_scene.html

All of that is a lot to wade through. I haven't actually used SceneLoader myself yet, though I intend to soon, but from what I've read so far it looks similar to BufferGeometryLoader or JSONLoader except because you're loading a whole scene instead of just part of one you have

scene = loaded.scene

rather than

scene.add()

and you may need to include other loaders and handlers for any specialized geometries that your scene uses, e.g.

<script src="js/loaders/ColladaLoader.js"></script>
..
loader.addHierarchyHandler( "dae", THREE.ColladaLoader );

for Collada.