three.js gets model object from scene

In three.s, model objects can be obtained through scene objects. The scene object is a container that contains all the objects, lights, cameras and other elements to be dyed, while the model object is one of the objects, usually a three-dimensional model. The following is a sample code to get the model object from the scene:

// 获取场景对象
var scene = new THREE.Scene();
// 创建一个几河体
var geometry = new THREE.BoxGeometry(1,1,1);
// 创建一个材质
var material = new THREE.MeshBasicMaterial({ color: 0xff0000 });
// 创建一个网格对象,将几何体和材质作为参数传入
var cube = new THREE.Mesh(geometry, material) ;
// 将网格对象添加到场景中
scene.add(cube) :
// 从场景中获歌模型对象
var model = scene.getobjectByName(cube.name);
// 从场景中获取模型对象
console.log(model);

In this example, a scene object and a 3D cube model are first created. Then add the cube model to the scene and obtain the model object from the scene through the getobjectByName() method. Finally, the model object is output to the console. It should be noted that when obtaining the model object, you need to know the name of the model. If no name is specified, you can use the getobjectById() method to obtain the model object by its ID.

All three.js get model objects from scene 


To obtain all model objects from the scene, you can use the scene object's traverse() method to traverse all sub-elements in the scene and determine whether the sub-elements are model objects. Here is
a sample code to get all model objects from the scene:

//创建一个场景对象
var scene = new THREE.Scene();

//创建一个几何体1
var geomety1 = new THREE.BoxGeometry(1,1,1);

//创建一个材质1
var material1 = new THREE.MeshBasicMaterial({ color: 0xff0000 })

//创建一个网格对象1,将几何体和材质作为参数传入
var cube1 = new THREE.Mesh(geomety1,material1);

//将网格对象1添加到场景中
scene.add(cubel);

//创建一个几何体2
var geomety2 = new THREE.BoxGeometry(2,2,2);

//创建一个材质2
var material2 = new THREE.MeshBasicMaterial({ color: 0x00ff00 })

//创建一个网格对象2,将几何体和材质作为参数传入
var cube1 = new THREE.Mesh(geomety2,material2);

//将网格对象2添加到场景中
scene.add(cube2);

//遍历场景中的所有子元素,判断是否是模型对象
scene.traverse(function(child){
  if(child instanceof THREE.Mesh){
    console.log(child.name)
  }
})

In this example, a scene pair and two 3D cube models are first created. Then add these two models to the scene, and use the traverse() method to traverse all sub-elements in the scene to determine whether the sub-elements are Mesh objects. If it is a Meh object, output it to the console. In this example, the output model object names are BoxGeometry and BoxGeometry1 respectively. This is because in three.js, the name of the model object defaults to the name of its geometry plus A number suffix
three.js to save scenes and models
To save three.js scenes and models, you can use the following methods:
1. Save the scene: a. First, make sure that all objects in the scene have been created and added to the scene. This may include geometry, maps, lights, etc. b. Create a JSON object to store scene data. You can use THREE.objectExporter to export scene object data. c. To convert the scene object into a JSON string, you can use the JsoN.stringify() method. d. You can save the JSON string to a local file or send it to the server, etc.
2. Save the model: a. First, make sure the model has been loaded and added to the scene. You can use THREE, objectLoader to load the model. b. Use THREE, objectExporter to export the data of the model object and convert it into a JSON string. c. The JSON string can be saved to a local file or sent to the server, etc.
It should be noted that when saving scenes and models, you need to ensure that related resource files (such as texture images) are also saved or uploaded to the server, so that these resources can be loaded correctly when loading scenes or models. In addition, it should be noted that the
saved Model and scene data can be large, so it may take some time to save or load.

Guess you like

Origin blog.csdn.net/w418856/article/details/132845730
Recommended