123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- /// <reference path="../../../../../dist/preview release/babylon.d.ts"/>
- module BABYLON.GLTF2.Extensions {
- interface IKHRMaterialsPbrSpecularGlossiness {
- diffuseFactor: number[];
- diffuseTexture: IGLTFTextureInfo;
- specularFactor: number[];
- glossinessFactor: number;
- specularGlossinessTexture: IGLTFTextureInfo;
- }
- export class KHRMaterialsPbrSpecularGlossiness extends GLTFLoaderExtension {
- public get name(): string {
- return "KHR_materials_pbrSpecularGlossiness";
- }
- protected _loadMaterial(loader: GLTFLoader, context: string, material: IGLTFMaterial, assign: (babylonMaterial: Material, isNew: boolean) => void): boolean {
- return this._loadExtension<IKHRMaterialsPbrSpecularGlossiness>(material, (extension, onComplete) => {
- loader._createPbrMaterial(material);
- loader._loadMaterialBaseProperties(context, material);
- this._loadSpecularGlossinessProperties(loader, context, material, extension);
- if (material.babylonMaterial) {
- assign(material.babylonMaterial, true);
- }
- });
- }
- private _loadSpecularGlossinessProperties(loader: GLTFLoader, context: string, material: IGLTFMaterial, properties: IKHRMaterialsPbrSpecularGlossiness): void {
- const babylonMaterial = material.babylonMaterial as PBRMaterial;
- babylonMaterial.albedoColor = properties.diffuseFactor ? Color3.FromArray(properties.diffuseFactor) : new Color3(1, 1, 1);
- babylonMaterial.reflectivityColor = properties.specularFactor ? Color3.FromArray(properties.specularFactor) : new Color3(1, 1, 1);
- babylonMaterial.microSurface = properties.glossinessFactor == null ? 1 : properties.glossinessFactor;
- if (loader._gltf.textures) {
- if (properties.diffuseTexture) {
- const texture = GLTFUtils.GetArrayItem(loader._gltf.textures, properties.diffuseTexture.index);
- if (!texture) {
- throw new Error(context + ": Failed to find diffuse texture " + properties.diffuseTexture.index);
- }
- if (properties.diffuseTexture.texCoord) {
- babylonMaterial.albedoTexture = loader._loadTexture("textures[" + texture.index + "]", texture, properties.diffuseTexture.texCoord);
- }
- }
- if (properties.specularGlossinessTexture) {
- const texture = GLTFUtils.GetArrayItem(loader._gltf.textures, properties.specularGlossinessTexture.index);
- if (!texture) {
- throw new Error(context + ": Failed to find diffuse texture " + properties.specularGlossinessTexture.index);
- }
- if (properties.specularGlossinessTexture.texCoord) {
- babylonMaterial.reflectivityTexture = loader._loadTexture("textures[" + texture.index + "]", texture, properties.specularGlossinessTexture.texCoord);
- }
- babylonMaterial.reflectivityTexture.hasAlpha = true;
- babylonMaterial.useMicroSurfaceFromReflectivityMapAlpha = true;
- }
- }
- loader._loadMaterialAlphaProperties(context, material, properties.diffuseFactor);
- }
- }
- GLTFLoader.RegisterExtension(new KHRMaterialsPbrSpecularGlossiness());
- }
|