-
Notifications
You must be signed in to change notification settings - Fork 46
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
First draft for merging structural metadata
- Loading branch information
Showing
5 changed files
with
835 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
src/tools/gltfExtensionsUtils/StructuralMetadataMergeUtilities.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
// NOTE: The functions in this class are "ported" from | ||
// https://github.com/donmccurdy/glTF-Transform/pull/1375/files | ||
// | ||
// The only exported function is "copyToDocument", which will be replaced | ||
// by the `copyToDocument` functionality of glTF-Transform 4.0 | ||
|
||
import { Document } from "@gltf-transform/core"; | ||
import { Graph } from "@gltf-transform/core"; | ||
import { Property } from "@gltf-transform/core"; | ||
import { PropertyType } from "@gltf-transform/core"; | ||
import { PropertyResolver } from "@gltf-transform/core/dist/properties"; | ||
|
||
const { TEXTURE_INFO, ROOT } = PropertyType; | ||
type PropertyConstructor = new (g: Graph<Property>) => Property; | ||
const NO_TRANSFER_TYPES = new Set<string>([TEXTURE_INFO, ROOT]); | ||
|
||
export function copyToDocument( | ||
target: Document, | ||
source: Document, | ||
sourceProperties: Property[], | ||
resolve?: PropertyResolver<Property> | ||
): Map<Property, Property> { | ||
const sourcePropertyDependencies = new Set<Property>(); | ||
for (const property of sourceProperties) { | ||
if (NO_TRANSFER_TYPES.has(property.propertyType)) { | ||
throw new Error(`Type "${property.propertyType}" cannot be transferred.`); | ||
} | ||
listPropertyDependencies(property, sourcePropertyDependencies); | ||
} | ||
return _copyToDocument( | ||
target, | ||
source, | ||
Array.from(sourcePropertyDependencies), | ||
resolve | ||
); | ||
} | ||
|
||
function _copyToDocument( | ||
target: Document, | ||
source: Document, | ||
sourceProperties: Property[], | ||
resolve?: PropertyResolver<Property> | ||
): Map<Property, Property> { | ||
resolve ||= createDefaultPropertyResolver(target, source); | ||
|
||
// Create stub classes for every Property in other Document. | ||
const propertyMap = new Map<Property, Property>(); | ||
for (const sourceProp of sourceProperties) { | ||
// TextureInfo copy handled by Material or ExtensionProperty. | ||
if ( | ||
!propertyMap.has(sourceProp) && | ||
sourceProp.propertyType !== TEXTURE_INFO | ||
) { | ||
propertyMap.set(sourceProp, resolve(sourceProp)); | ||
} | ||
} | ||
|
||
// Assemble relationships between Properties. | ||
for (const [sourceProp, targetProp] of propertyMap.entries()) { | ||
targetProp.copy(sourceProp, resolve); | ||
} | ||
|
||
return propertyMap; | ||
} | ||
|
||
function createDefaultPropertyResolver( | ||
target: Document, | ||
source: Document | ||
): PropertyResolver<Property> { | ||
const propertyMap = new Map<Property, Property>([ | ||
[source.getRoot(), target.getRoot()], | ||
]); | ||
|
||
return (sourceProp: Property): Property => { | ||
// TextureInfo lifecycle is bound to a Material or ExtensionProperty. | ||
if (sourceProp.propertyType === TEXTURE_INFO) return sourceProp; | ||
|
||
let targetProp = propertyMap.get(sourceProp); | ||
if (!targetProp) { | ||
// Create stub class, defer copying properties. | ||
const PropertyClass = sourceProp.constructor as PropertyConstructor; | ||
targetProp = new PropertyClass(target.getGraph()); | ||
propertyMap.set(sourceProp, targetProp); | ||
} | ||
|
||
return targetProp; | ||
}; | ||
} | ||
|
||
function listPropertyDependencies( | ||
parent: Property, | ||
visited: Set<Property> | ||
): Set<Property> { | ||
const graph = parent.getGraph(); | ||
const queue: Property[] = [parent]; | ||
|
||
let next: Property | undefined = undefined; | ||
while ((next = queue.pop())) { | ||
visited.add(next); | ||
for (const child of graph.listChildren(next)) { | ||
if (!visited.has(child)) { | ||
queue.push(child); | ||
} | ||
} | ||
} | ||
|
||
return visited; | ||
} |
Oops, something went wrong.