fromFlatbuffer static method
Implementation
static PhysicallyBasedMaterial fromFlatbuffer(
fb.Material fbMaterial,
List<gpu.Texture> textures,
) {
if (fbMaterial.type != fb.MaterialType.kPhysicallyBased) {
throw Exception('Cannot unpack PBR material from non-PBR material');
}
PhysicallyBasedMaterial material = PhysicallyBasedMaterial();
// Base color.
if (fbMaterial.baseColorFactor != null) {
material.baseColorFactor = Vector4(
fbMaterial.baseColorFactor!.r,
fbMaterial.baseColorFactor!.g,
fbMaterial.baseColorFactor!.b,
fbMaterial.baseColorFactor!.a,
);
}
if (fbMaterial.baseColorTexture >= 0 &&
fbMaterial.baseColorTexture < textures.length) {
material.baseColorTexture = textures[fbMaterial.baseColorTexture];
}
// Metallic-roughness.
material.metallicFactor = fbMaterial.metallicFactor;
material.roughnessFactor = fbMaterial.roughnessFactor;
debugPrint('Total texture count: ${textures.length}');
if (fbMaterial.metallicRoughnessTexture >= 0 &&
fbMaterial.metallicRoughnessTexture < textures.length) {
material.metallicRoughnessTexture =
textures[fbMaterial.metallicRoughnessTexture];
}
// Normal.
if (fbMaterial.normalTexture >= 0 &&
fbMaterial.normalTexture < textures.length) {
material.normalTexture = textures[fbMaterial.normalTexture];
}
material.normalScale = fbMaterial.normalScale;
// Emissive.
if (fbMaterial.emissiveFactor != null) {
material.emissiveFactor = Vector4(
fbMaterial.emissiveFactor!.x,
fbMaterial.emissiveFactor!.y,
fbMaterial.emissiveFactor!.z,
1,
);
}
if (fbMaterial.emissiveTexture >= 0 &&
fbMaterial.emissiveTexture < textures.length) {
material.emissiveTexture = textures[fbMaterial.emissiveTexture];
}
// Occlusion.
material.occlusionStrength = fbMaterial.occlusionStrength;
if (fbMaterial.occlusionTexture >= 0 &&
fbMaterial.occlusionTexture < textures.length) {
material.occlusionTexture = textures[fbMaterial.occlusionTexture];
}
return material;
}