Appearance
Class: OBJLoader
.obj 模型加载器,无需实例化该类,请使用静态方法
Table of contents
Constructors
Methods
Constructors
constructor
• new OBJLoader(): OBJLoader
Returns
Methods
batchLoadOBJModels
▸ batchLoadOBJModels(uri, options): void
使用批绘制渲染 .obj 模型,减少 draw call,可提高渲染性能。 仅渲染三角面(f),仅使用材质文件中和漫反射有关的关键字(Kd 或 map_Kd)进行渲染,若无关键字,则使用红色。 纹理图片格式额外支持 .tga 格式
Parameters
| Name | Type | Description |
|---|---|---|
uri | string | string[] | .obj 模型 URI |
options | BatchLoadOBJModelsOptions | 选项 * |
Returns
void
Example
js
const position = Cesium.Cartesian3.fromDegrees(115.8198195, 38.4373388, 0);
const m = Cesium.Transforms.eastNorthUpToFixedFrame(position);
const paths = ["obj/model-1.obj", "obj/model-2.obj", "obj/model-3.obj"];
joDVF.OBJLoader.batchLoadOBJModels(paths, {
viewer: viewer,
backFaceCulling: false,
generateMipmap: true,
positionCallback: function (x, y, z) {
const [longitude, latitude] = proj4("EPSG:32650", "WGS84", [x, y]);
const c3 = Cesium.Cartesian3.fromDegrees(longitude, latitude, z);
return [c3.x, c3.y, c3.z];
}
}).then((primitive) => {
// 如果不修改顶点坐标,则可能需要设置模型矩阵
// primitive.modelMatrix = m;
viewer.scene.primitives.add(primitive);
});Defined in
src/extensions/OBJLoader/index.ts:119
fetchOBJAndMTL
▸ fetchOBJAndMTL(uri): Promise<{ positions: any ; colors: any ; texcoords: any ; normals: any ; objects: any ; materials: {} }>
获取 .obj 文件 和引用的 .mtl 文件并解析它们。如果你对 .obj 格式和 .mtl 格式有所了解, 也可以自定义渲染方式。 单独解析 .obj 文件见 OBJLoader.parseOBJ,单独解析 .mtl 文件见 OBJLoader.parseMTL, 解析并渲染见 OBJLoader.loadOBJModel
Parameters
| Name | Type | Description |
|---|---|---|
uri | string | .obj 模型 URI |
Returns
Promise<{ positions: any ; colors: any ; texcoords: any ; normals: any ; objects: any ; materials: {} }>
Example
js
joDVF.OBJLoader.fetchOBJAndMTL("path/to/model.obj").then((parsedResult) => {
// parsedResult 对象有 positions、colors、texcoords、normals、objects、materials 属性(如果有的话)
const { positions, colors, texcoords, normals, objects, materials } =
parsedResult;
});Defined in
src/extensions/OBJLoader/index.ts:161
loadOBJModel
▸ loadOBJModel(uri, viewer): Promise<PrimitiveCollection>
加载 .obj 模型。仅使用材质文件中和漫反射有关的关键字(Kd 或 map_Kd)进行渲染, 如果顶点包含颜色信息,则使用顶点颜色。 纹理图片格式支持 .tga、.jpg、.jpeg、.png 格式
Parameters
| Name | Type | Description |
|---|---|---|
uri | string | .obj 模型 URI |
viewer | Viewer | Cesium.Viewer 类实例 |
Returns
Promise<PrimitiveCollection>
Example
js
const position = Cesium.Cartesian3.fromDegrees(113, 23, 10);
const m = Cesium.Transforms.eastNorthUpToFixedFrame(position);
joDVF.OBJLoader.loadOBJModel("path/to/model.obj", viewer).then((result) => {
for (let i = 0, len = result.length; i < len; ++i) {
result.get(i).modelMatrix = m;
}
viewer.scene.primitives.add(result);
});Defined in
src/extensions/OBJLoader/index.ts:275
parseMTL
▸ parseMTL(text): any
解析 .mtl 文件文本。仅支持以下关键字:
- newmtl
- Ka
- Kd
- Ks
- Ns
- Ke
- map_Ka
- map_Kd
- map_Ks
- map_Ke
- d
- Tr
- map_d
- Tf
- Ni
- illum
Parameters
| Name | Type | Description |
|---|---|---|
text | string | .mtl 文件文本 |
Returns
any
一个属性名为材质名的对象,属性值为对应材质的上述参数
Example
js
fetch("path/to/materials.mtl")
.then((res) => res.text())
.then((MTLText) => {
// 解析后的结果,属性名为材质名。
const materials = joDVF.OBJLoader.parseMTL(MTLText);
});Defined in
src/extensions/OBJLoader/index.ts:449
parseOBJ
▸ parseOBJ(text): Object
解析 .obj 文件,可用于自定义渲染。仅支持以下关键字:
- v:顶点坐标格式支持 v x y z 格式和 v x y z r g b 格式。
- vt
- vn
- f
- l
- mtllib
- usemtl
- o
- g
Parameters
| Name | Type | Description |
|---|---|---|
text | string | .obj 文件文本 |
Returns
Object
一个包含顶点坐标、顶点颜色、纹理坐标、法线、物体和材质路径的对象
| Name | Type |
|---|---|
positions | any[] |
colors | any[] |
texcoords | any[] |
normals | any[] |
objects | any |
mtllib | any[] |
Example
js
fetch("path/to/model.obj")
.then((res) => res.text())
.then((OBJText) => {
// 解析后的结果
const { positions, colors, texcoords, normals, objects, mtllib } =
joDVF.OBJLoader.parseOBJ(OBJText);
});Defined in
src/extensions/OBJLoader/index.ts:554
PositionCallback
▸ PositionCallback(x, y, z): void
一个用于修改顶点坐标的函数
Parameters
| Name | Type | Description |
|---|---|---|
x | number | 顶点坐标的 X 分量 |
y | number | 顶点坐标的 Y 分量 |
z | number | 顶点坐标的 Z 分量 |
Returns
void
修改后的顶点坐标,数组元素分别对应 X、Y、Z 分量
Defined in
src/extensions/OBJLoader/index.ts:85

