> For the complete documentation index, see [llms.txt](https://docs.tenten.co/awesome/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.tenten.co/awesome/webgl/awesome-threejs-playground/docs/hello-loader.md).

# Hellow Three.js 之 加载外部3D模型

> [Demo查看](http://codeffe.com/demo/hello-threejs/hello-loader.html)

![teapot](https://3720450447-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fe5Q35edJYrEDsLYgJC3J%2Fuploads%2Fgit-blob-1e1000a9635ca206f98226415e1a0d41098db58c%2Fteapot.png?alt=media)

## 外部模型

在Three.js中，创建常见几何体比较方便，但是对于复杂模型，比如人、各种动物、植物甚至现实中复杂物体。就比较麻烦，这些复杂建模通常由3D Max等工具制作好三维模型。

## 支持的格式类型

`*.obj` 是最常用的模型格式， 导入带 `*.mtl` 材质的`*.obj` 文件需要 `MTLLoader.js` 以及 `OBJMTLLoader.js`。 另有 `PLYLoader.js`、`STLLoader.js` 等分别对应不同格式的加载器，可以根据模型格式自行选择。

目前，支持的模型格式有：

* `*.obj` -- 需要 `OBJLoader.js`；
* `*.obj`, `*.mtl` -- 需要 `MTLLoader.js` 以及 `OBJMTLLoader.js`
* `*.dae`
* `*.ctm`
* `*.ply` -- 需要 `PLYLoader.js`
* `*.stl` -- 需要 `STLLoader.js`
* `*.wrl`
* `*.vtk` - 需要 `VTKLoader.js`

## 模型导入方法

创建 `loader` 变量，用于导入模型

```
var objloader = new THREE.ObjectLoader(); // 导入json格式
var OBJloader = new THREE.OBJLoader(); // 可导入 .obj格式

…… 其他格式类似
```

`loader` 导入模型的时候，接受两个参数:

* 第一个表示模型路径
* 第二个表示完成导入后的回调函数 - 一般我们需要在这个回调函数中将导入的模型添加到场景中。

### 无材质的模型

**json 格式文件加载**

![teapot](https://3720450447-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fe5Q35edJYrEDsLYgJC3J%2Fuploads%2Fgit-blob-8162d5a897035c59763d13607c68cda2491c623c%2Floader-json.png?alt=media)

```
var objloader = new THREE.ObjectLoader();
objloader.load( "./demo/models/teapot.json", function( group ) {
    mesh = group.children[0];
    mesh.material = material;
    mesh.scale.set(4,4,4);
    mesh.position.y = -30;
    scene.add( mesh );
});
```

**.obj 格式文件加载**

![teapot](https://3720450447-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fe5Q35edJYrEDsLYgJC3J%2Fuploads%2Fgit-blob-a33d18dd3c28a006c4d013c23ea2d89890c23e94%2Floader-obj.png?alt=media)

```
var loader = new THREE.OBJLoader();
loader.load('./demo/models/teapot.obj', function(obj) {
    obj.traverse(function(child) {
        if (child instanceof THREE.Mesh) {
            child.material = new THREE.MeshLambertMaterial({
                color: 0xffff00,
                side: THREE.DoubleSide
            });
        }
    });
    mesh = obj;
    mesh.position.y = -30;
    mesh.scale.set(30,30,30);
    scene.add(obj);
});
```

### 有材质的模型

`.mtl` 为材质文件

![teapot](https://3720450447-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fe5Q35edJYrEDsLYgJC3J%2Fuploads%2Fgit-blob-b29fa3ea8c4a67acbc0474969a5a93b19ec65b03%2Floader-mtl.png?alt=media)

```
var mtlLoader = new THREE.MTLLoader();
mtlLoader.load('./demo/models/teapot.mtl', function(materials) {
    materials.preload();
    // model loader
    var objLoader = new THREE.OBJLoader();
    objLoader.setMaterials(materials);
    objLoader.load('./demo/models/teapot.obj', function (obj) {
        if (obj.children.length > 0) {
          mesh = obj;
          mesh.position.y = -30;
          mesh.scale.set(30,30,30);
          scene.add(obj);
        }
    });
});
```

***

本文部分内容参照及引用：

> [Three.js入门指南](http://www.ituring.com.cn/book/1272)


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.tenten.co/awesome/webgl/awesome-threejs-playground/docs/hello-loader.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
