Skip to content

Commit

Permalink
fix(gltf, converter): make ext-mesh-features independent from ext-str…
Browse files Browse the repository at this point in the history
…uctural-metadata. Fix comments on prev pr
  • Loading branch information
mspivak-actionengine committed Sep 21, 2023
1 parent 0fd3321 commit 44d5484
Show file tree
Hide file tree
Showing 19 changed files with 1,087 additions and 827 deletions.
8 changes: 3 additions & 5 deletions modules/3d-tiles/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import type {GLTFPostprocessed} from '@loaders.gl/gltf';
import type {GLTFPostprocessed, FeatureTableJson} from '@loaders.gl/gltf';
export type {FeatureTableJson};

import {LoaderWithParser} from '@loaders.gl/loader-utils';
import {Matrix4, Vector3} from '@math.gl/core';
import {TILESET_TYPE, LOD_METRIC_TYPE, TILE_TYPE, TILE_REFINEMENT} from '@loaders.gl/tiles';

export type FeatureTableJson = {
[key: string]: any[];
};

export type B3DMContent = {
batchTableJson?: FeatureTableJson;
byteLength: number;
Expand Down
13 changes: 9 additions & 4 deletions modules/gltf/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ export type {
GLTF_KHR_draco_mesh_compression,
GLTF_KHR_texture_basisu,
GLTF_EXT_meshopt_compression,
GLTF_EXT_texture_webp,
// 3DTiles extensions
GLTF_EXT_texture_webp
} from './lib/types/gltf-json-schema';

// 3DTiles extensions
export type {
GLTF_EXT_feature_metadata_GLTF,
GLTF_EXT_feature_metadata_Schema,
GLTF_EXT_feature_metadata_Class,
Expand All @@ -42,7 +45,7 @@ export type {
GLTF_EXT_feature_metadata_FeatureIdAttributeFeatureIds,
GLTF_EXT_feature_metadata_FeatureIdTexture,
GLTF_EXT_feature_metadata_FeatureIdTextureAccessor
} from './lib/types/gltf-json-schema';
} from './lib/types/gltf-ext-feature-metadata-schema';

export type {GLTF_EXT_structural_metadata_GLTF} from './lib/types/gltf-ext-structural-metadata-schema';

Expand All @@ -54,6 +57,8 @@ export type {
export {name as EXT_MESH_FEATURES} from './lib/extensions/EXT_mesh_features';
export {name as EXT_STRUCTURAL_METADATA} from './lib/extensions/EXT_structural_metadata';
export {name as EXT_FEATURE_METADATA} from './lib/extensions/deprecated/EXT_feature_metadata';
export {getPropertyTableFromExtFeatureMetadata} from './lib/extensions/deprecated/EXT_feature_metadata';
export {getPropertyTableFromExtStructuralMetadata} from './lib/extensions/EXT_structural_metadata';

// Postprocessed types (modified GLTF types)
export type {
Expand All @@ -67,7 +72,7 @@ export type {
GLTFTexturePostprocessed
} from './lib/types/gltf-postprocessed-schema';

export type {GLTFWithBuffers} from './lib/types/gltf-types';
export type {GLTFWithBuffers, FeatureTableJson} from './lib/types/gltf-types';

// glTF loader/writer definition objects
export {GLTFLoader} from './gltf-loader';
Expand Down
2 changes: 1 addition & 1 deletion modules/gltf/src/lib/api/gltf-extensions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ export const EXTENSIONS: GLTFExtensionPlugin[] = [
// KHR_binary_gltf,

// 2.0
EXT_mesh_features,
EXT_structural_metadata,
EXT_mesh_features,
EXT_meshopt_compression,
EXT_texture_webp,
// Basisu should come after webp, we want basisu to be preferred if both are provided
Expand Down
60 changes: 16 additions & 44 deletions modules/gltf/src/lib/extensions/EXT_mesh_features.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import type {
GLTF_EXT_mesh_features,
GLTF_EXT_mesh_features_featureId
} from '../types/gltf-ext-mesh-features-schema';
import type {GLTF_EXT_structural_metadata_PropertyTable} from '../types/gltf-ext-structural-metadata-schema';

import {GLTFScenegraph} from '../api/gltf-scenegraph';
import {getPrimitiveTextureData, primitivePropertyDataToAttributes} from './utils/3d-tiles-utils';
import {getPropertyTablePopulated} from './EXT_structural_metadata';
import {getPrimitiveTextureData} from './utils/3d-tiles-utils';

const EXT_MESH_FEATURES_NAME = 'EXT_mesh_features';
export const name = EXT_MESH_FEATURES_NAME;
Expand All @@ -22,9 +20,9 @@ export async function decode(gltfData: {json: GLTF}, options: GLTFLoaderOptions)
}

/**
* Decodes feature metadata from extension
* Decodes feature metadata from extension.
* @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
* @param {GLTFLoaderOptions} options - loader options.
* @param {GLTFLoaderOptions} options - GLTFLoader options.
*/
function decodeExtMeshFeatures(scenegraph: GLTFScenegraph, options: GLTFLoaderOptions): void {
const json = scenegraph.gltf.json;
Expand All @@ -44,47 +42,38 @@ function decodeExtMeshFeatures(scenegraph: GLTFScenegraph, options: GLTFLoaderOp
* Takes data from EXT_mesh_features and store it in 'data' property of featureIds.
* If combined with EXT_structural_metadata, corresponding data are taken from the property tables of that extension.
* @param {GLTFScenegraph} scenegraph - Instance of the class for structured access to GLTF data.
* @param {GLTFMeshPrimitive} primitive - primitive that contains extensions.
* @param {GLTFLoaderOptions} options - loader options.
* @param {GLTFMeshPrimitive} primitive - Primitive that contains extensions.
* @param {GLTFLoaderOptions} options - GLTFLoader options.
*/
function processMeshPrimitiveFeatures(
scenegraph: GLTFScenegraph,
primitive: GLTFMeshPrimitive,
options: GLTFLoaderOptions
): void {
// Processing of mesh primitive features requires buffers to be loaded.
if (!options?.gltf?.loadBuffers) {
return;
}

const extension = primitive.extensions?.[EXT_MESH_FEATURES_NAME] as GLTF_EXT_mesh_features;
const featureIds: GLTF_EXT_mesh_features_featureId[] = extension?.featureIds;
if (!featureIds) return;

if (!extension.dataAttributeNames) {
extension.dataAttributeNames = [];
if (!featureIds) {
return;
}

let featureIdCount = 0; // It can be used to name the feature if neither label nor property table name is provided.
for (const featureId of featureIds) {
/*
When combined with the EXT_structural_metadata extension, feature ID sets can be associated with property tables.
A property table maps each feature ID to a set of values that are associated with the respective feature.
The feature ID in this case serves as an index for the row of the table.
The index of the property table that a certain set of feature IDs is associated with is stored in the propertyTable of the feature ID set definition.
*/
let propertyTable: GLTF_EXT_structural_metadata_PropertyTable | null = null;
if (typeof featureId.propertyTable === 'number') {
propertyTable = getPropertyTablePopulated(scenegraph, featureId.propertyTable);
}

let propertyData: number[] | null = null;
let featureIdData: number[] | null = null;
// Process "Feature ID by Vertex"
if (typeof featureId.attribute !== 'undefined') {
const accessorKey = `_FEATURE_ID_${featureId.attribute}`;
const accessorIndex = primitive.attributes[accessorKey];
const propertyDataTypedArray = scenegraph.getTypedArrayForAccessor(accessorIndex);
propertyData = Array.prototype.slice.call(propertyDataTypedArray);
featureIdData = scenegraph.getTypedArrayForAccessor(accessorIndex) as number[];
}

// Process "Feature ID by Texture Coordinates"
else if (typeof featureId.texture !== 'undefined' && options?.gltf?.loadImages) {
propertyData = getPrimitiveTextureData(scenegraph, featureId.texture, primitive);
featureIdData = getPrimitiveTextureData(scenegraph, featureId.texture, primitive);
}

// Process "Feature ID by Index"
Expand All @@ -97,23 +86,6 @@ function processMeshPrimitiveFeatures(
// TODO: At the moment of writing we don't have a tileset with the data of that kind. Implement it later.
}

const attributeName =
featureId.label || propertyTable?.name || `featureAttribute${featureIdCount}`;

// featureTable - an array where unique data from the property data are being stored
const featureTable: number[] = [];
if (propertyData) {
primitivePropertyDataToAttributes(
scenegraph,
attributeName,
propertyData,
featureTable,
primitive
);
}
extension.dataAttributeNames.push(attributeName);
featureId.data = featureTable;

featureIdCount++;
featureId.data = featureIdData;
}
}
Loading

0 comments on commit 44d5484

Please sign in to comment.