Materials — LambertMaterial, PhongMaterial, PbrMaterial

Materials — LambertMaterial, PhongMaterial, PbrMaterial

包:: @aspose/3d (v24.12.0)

@aspose/3d 提供三个具体的材质类,继承自抽象的 Material 基类。它们覆盖了 3D 交换格式中最常见的三种着色模型::

  • LambertMaterial — 漫反射 + 环境光 + 自发光(OBJ/MTL 和较旧的 FBX 资源使用)
  • PhongMaterial — 在 Lambert 基础上扩展了高光属性
  • PbrMaterial — 与 glTF 2.0 材质定义相匹配的基于物理的模型

所有材质类都从 main 导出 @aspose/3d 入口点。.

import { LambertMaterial, PhongMaterial, PbrMaterial } from '@aspose/3d';

类 Material(抽象基类)

Material 是所有材质的抽象基类。它扩展自 A3DObject,,提供 name 属性以及纹理槽 API。.

export class Material extends A3DObject

Properties

PropertiesPropertiesProperties
namestring材质名称,在场景图和导出输出中显示的名称。.

Properties

getTexture(slotName)

返回 TextureBase 分配给指定槽的,或者 null 如果未分配任何内容,则返回.

getTexture(slotName: string): TextureBase | null

setTexture(slotName, texture)

分配一个 TextureBase 实例化到命名槽。.

setTexture(slotName: string, texture: TextureBase): void

类 LambertMaterial

LambertMaterial 对漫反射进行建模,并可选地加入环境光和自发光贡献。它是使用加载 OBJ 文件时常见的材质类型,with .mtl 库。.

export class LambertMaterial extends Material

Properties

A3DObject ← Material ← LambertMaterial

Properties

为节点分配一个红色漫反射材质。.

import { Scene, LambertMaterial } from '@aspose/3d';
import { Vector3 } from '@aspose/3d/utilities';

const scene = new Scene();
scene.open('model.obj');

const mat = new LambertMaterial('RedSurface');
mat.diffuseColor = new Vector3(1, 0, 0);   // R=1, G=0, B=0
mat.ambientColor = new Vector3(0.1, 0, 0);
mat.transparency = 0;

// Assign material to the first child node's entity
const firstNode = scene.rootNode.childNodes[0];
if (firstNode && firstNode.entity) {
  (firstNode.entity as any).material = mat;
}
scene.save('output.fbx');

Properties

PropertiesPropertiesPropertiesProperties
diffuseColor`Vector3null`null
ambientColor`Vector3null`null
emissiveColor`Vector3null`null
transparentColor`Vector3null`null
transparencynumber0.0不透明度因子,范围为 0.0(完全不透明)到 1.0(完全透明)。会自动限制。.

类 PhongMaterial

PhongMaterial 扩展 LambertMaterial 支持镜面高光,实现经典的 Phong 着色模型。它在 FBX 和 COLLADA 资源中很常见。.

export class PhongMaterial extends LambertMaterial

Properties

A3DObject ← Material ← LambertMaterial ← PhongMaterial

Properties

创建一个光亮的蓝色 Phong 材质。.

import { PhongMaterial } from '@aspose/3d';
import { Vector3 } from '@aspose/3d/utilities';

const mat = new PhongMaterial('ShinyBlue');
mat.diffuseColor = new Vector3(0, 0.2, 0.8);
mat.specularColor = new Vector3(1, 1, 1);
mat.shininess = 64;
mat.specularFactor = 0.8;
console.log(`Shininess: ${mat.shininess}`); // Shininess: 64

Properties

PropertiesPropertiesPropertiesProperties
specularColor`Vector3null`null
specularFactornumber0.0镜面贡献的标量乘数。.
shininessnumber0.0Phong 高光指数 — 值越高产生越紧凑的高光。.
reflectionColor`Vector3null`null
reflectionFactornumber0.0反射贡献的标量乘数。.

类 PbrMaterial

PbrMaterial 实现 glTF 2.0 金属-粗糙度 PBR 模型。将此类用于将保存为 glTF/GLB 的场景,或当源 FBX 资源包含 PBR 材质时使用。.

export class PbrMaterial extends Material

Properties

A3DObject ← Material ← PbrMaterial

Properties

构建一个金色的 PBR 材质并将其保存为 glTF 文件。.

import { Scene, PbrMaterial } from '@aspose/3d';
import { Vector3 } from '@aspose/3d/utilities';
import { GltfSaveOptions } from '@aspose/3d/formats/gltf';

const scene = new Scene();
scene.open('base-mesh.obj');

const gold = new PbrMaterial('Gold');
gold.albedo = new Vector3(1.0, 0.766, 0.336);  // gold base color
gold.metallicFactor = 1.0;
gold.roughnessFactor = 0.3;

// Attach material to the first mesh node
const meshNode = scene.rootNode.childNodes[0];
if (meshNode && meshNode.entity) {
  (meshNode.entity as any).material = gold;
}

const opts = new GltfSaveOptions();
opts.binaryMode = true;
scene.save('gold-model.glb', opts);

Properties

PropertiesPropertiesPropertiesProperties
albedo`Vector3null`null
albedoTexture`TextureBasenull`null
normalTexture`TextureBasenull`null
metallicFactornumber0.0金属度因子在 0–1 范围内。1.0 = 完全金属。.
roughnessFactornumber0.0粗糙度因子在 0–1 范围内。0.0 = 镜面光滑。.
metallicRoughness`TextureBasenull`null
occlusionTexture`TextureBasenull`null
occlusionFactornumber0.0环境遮蔽效果的强度。.
emissiveColor`Vector3null`null
emissiveTexture`TextureBasenull`null
transparencynumber0.0不透明度因子在 0–1 范围内。自动钳位。.

静态方法

PbrMaterial.fromMaterial(material)

创建一个新的 PbrMaterial 从通用的 Material 实例,复制 name.

static fromMaterial(material: Material): PbrMaterial
 中文