three
Version:
JavaScript 3D library
2,011 lines (1,300 loc) • 110 kB
JavaScript
import {
AnimationClip,
BoxGeometry,
BufferAttribute,
BufferGeometry,
CapsuleGeometry,
ClampToEdgeWrapping,
Color,
ConeGeometry,
CylinderGeometry,
DirectionalLight,
Euler,
Group,
Matrix4,
Mesh,
MeshPhysicalMaterial,
MirroredRepeatWrapping,
NoColorSpace,
Object3D,
OrthographicCamera,
PerspectiveCamera,
PointLight,
Quaternion,
QuaternionKeyframeTrack,
RectAreaLight,
RepeatWrapping,
ShapeUtils,
SkinnedMesh,
Skeleton,
Bone,
SphereGeometry,
SpotLight,
SRGBColorSpace,
Texture,
Vector2,
Vector3,
VectorKeyframeTrack
} from 'three';
// Pre-compiled regex patterns for performance
const VARIANT_PATH_REGEX = /^(.+?)\/\{(\w+)=(\w+)\}\/(.+)$/;
// Spec types (must match USDCParser)
const SpecType = {
Unknown: 0,
Attribute: 1,
Connection: 2,
Expression: 3,
Mapper: 4,
MapperArg: 5,
Prim: 6,
PseudoRoot: 7,
Relationship: 8,
RelationshipTarget: 9,
Variant: 10,
VariantSet: 11
};
// UsdGeomCamera fallback values (OpenUSD schema)
const USD_CAMERA_DEFAULTS = {
projection: 'perspective',
clippingRange: [ 1, 1000000 ],
horizontalAperture: 20.955,
verticalAperture: 15.2908,
horizontalApertureOffset: 0,
verticalApertureOffset: 0,
focalLength: 50,
focusDistance: 0,
fStop: 0
};
/**
* USDComposer handles scene composition from parsed USD data.
* This includes reference resolution, variant selection, transform handling,
* and building the Three.js scene graph.
*
* Works with specsByPath format from USDCParser.
*/
class USDComposer {
constructor( manager = null ) {
this.textureCache = {};
this.skinnedMeshes = [];
this.manager = manager;
}
/**
* Compose a Three.js scene from parsed USD data.
* @param {Object} parsedData - Data from USDCParser or USDAParser
* @param {Object} assets - Dictionary of referenced assets (specsByPath or blob URLs)
* @param {Object} variantSelections - External variant selections
* @param {string} basePath - Base path for resolving relative references
* @returns {Group} Three.js scene graph
*/
compose( parsedData, assets = {}, variantSelections = {}, basePath = '' ) {
this.specsByPath = parsedData.specsByPath;
this.assets = assets;
this.externalVariantSelections = variantSelections;
this.basePath = basePath;
this.skinnedMeshes = [];
this.skeletons = {};
// Build indexes for O(1) lookups
this._buildIndexes();
// Get FPS from root spec
const rootSpec = this.specsByPath[ '/' ];
const rootFields = rootSpec ? rootSpec.fields : {};
this.fps = rootFields.framesPerSecond || rootFields.timeCodesPerSecond || 30;
const group = new Group();
this._buildHierarchy( group, '/' );
// Bind skeletons to skinned meshes
this._bindSkeletons();
// Expose skeleton on the root group so that AnimationMixer's
// PropertyBinding.findNode resolves bone names before scene objects.
// Without this, Xform prims that share a name with a skeleton joint
// would be animated instead of the bone.
const skeletonPaths = Object.keys( this.skeletons );
if ( skeletonPaths.length === 1 ) {
group.skeleton = this.skeletons[ skeletonPaths[ 0 ] ].skeleton;
}
// Build animations
group.animations = this._buildAnimations();
// Handle metersPerUnit scaling
const metersPerUnit = rootFields.metersPerUnit;
if ( metersPerUnit !== undefined && metersPerUnit !== 1 ) {
group.scale.setScalar( metersPerUnit );
}
// Handle Z-up to Y-up conversion
if ( rootSpec && rootSpec.fields && rootSpec.fields.upAxis === 'Z' ) {
group.rotation.x = - Math.PI / 2;
}
return group;
}
/**
* Apply USD transforms to a Three.js object.
* Handles xformOpOrder with proper matrix composition.
* USD uses row-vector convention, Three.js uses column-vector.
*/
applyTransform( obj, fields, attrs = {} ) {
const data = { ...fields, ...attrs };
const xformOpOrder = data[ 'xformOpOrder' ];
// If we have xformOpOrder, apply transforms using matrices
if ( xformOpOrder && xformOpOrder.length > 0 ) {
const matrix = new Matrix4();
const tempMatrix = new Matrix4();
// Track scale for handling negative scale with rotation
let scaleValues = null;
// Iterate FORWARD for Three.js column-vector convention
for ( let i = 0; i < xformOpOrder.length; i ++ ) {
const op = xformOpOrder[ i ];
const isInverse = op.startsWith( '!invert!' );
const opName = isInverse ? op.slice( 8 ) : op;
if ( opName === 'xformOp:transform' ) {
const m = data[ 'xformOp:transform' ];
if ( m && m.length === 16 ) {
tempMatrix.set(
m[ 0 ], m[ 4 ], m[ 8 ], m[ 12 ],
m[ 1 ], m[ 5 ], m[ 9 ], m[ 13 ],
m[ 2 ], m[ 6 ], m[ 10 ], m[ 14 ],
m[ 3 ], m[ 7 ], m[ 11 ], m[ 15 ]
);
if ( isInverse ) tempMatrix.invert();
matrix.multiply( tempMatrix );
}
} else if ( opName === 'xformOp:translate' ) {
const t = data[ 'xformOp:translate' ];
if ( t ) {
tempMatrix.makeTranslation( t[ 0 ], t[ 1 ], t[ 2 ] );
if ( isInverse ) tempMatrix.invert();
matrix.multiply( tempMatrix );
}
} else if ( opName === 'xformOp:translate:pivot' ) {
const t = data[ 'xformOp:translate:pivot' ];
if ( t ) {
tempMatrix.makeTranslation( t[ 0 ], t[ 1 ], t[ 2 ] );
if ( isInverse ) tempMatrix.invert();
matrix.multiply( tempMatrix );
}
} else if ( opName === 'xformOp:scale' ) {
const s = data[ 'xformOp:scale' ];
if ( s ) {
if ( Array.isArray( s ) ) {
tempMatrix.makeScale( s[ 0 ], s[ 1 ], s[ 2 ] );
scaleValues = [ s[ 0 ], s[ 1 ], s[ 2 ] ];
} else {
tempMatrix.makeScale( s, s, s );
scaleValues = [ s, s, s ];
}
if ( isInverse ) tempMatrix.invert();
matrix.multiply( tempMatrix );
}
} else if ( opName === 'xformOp:rotateXYZ' ) {
const r = data[ 'xformOp:rotateXYZ' ];
if ( r ) {
// USD rotateXYZ: matrix = Rx * Ry * Rz
// Three.js Euler 'ZYX' order produces same result
const euler = new Euler(
r[ 0 ] * Math.PI / 180,
r[ 1 ] * Math.PI / 180,
r[ 2 ] * Math.PI / 180,
'ZYX'
);
tempMatrix.makeRotationFromEuler( euler );
if ( isInverse ) tempMatrix.invert();
matrix.multiply( tempMatrix );
}
} else if ( opName === 'xformOp:rotateX' ) {
const r = data[ 'xformOp:rotateX' ];
if ( r !== undefined ) {
tempMatrix.makeRotationX( r * Math.PI / 180 );
if ( isInverse ) tempMatrix.invert();
matrix.multiply( tempMatrix );
}
} else if ( opName === 'xformOp:rotateY' ) {
const r = data[ 'xformOp:rotateY' ];
if ( r !== undefined ) {
tempMatrix.makeRotationY( r * Math.PI / 180 );
if ( isInverse ) tempMatrix.invert();
matrix.multiply( tempMatrix );
}
} else if ( opName === 'xformOp:rotateZ' ) {
const r = data[ 'xformOp:rotateZ' ];
if ( r !== undefined ) {
tempMatrix.makeRotationZ( r * Math.PI / 180 );
if ( isInverse ) tempMatrix.invert();
matrix.multiply( tempMatrix );
}
} else if ( opName === 'xformOp:orient' ) {
const q = data[ 'xformOp:orient' ];
if ( q && q.length === 4 ) {
const quat = new Quaternion( q[ 0 ], q[ 1 ], q[ 2 ], q[ 3 ] );
tempMatrix.makeRotationFromQuaternion( quat );
if ( isInverse ) tempMatrix.invert();
matrix.multiply( tempMatrix );
}
}
}
obj.matrix.copy( matrix );
obj.matrix.decompose( obj.position, obj.quaternion, obj.scale );
// Fix for negative scale: decompose() may absorb negative scale into quaternion
// Restore original scale signs to keep animation consistent
if ( scaleValues ) {
const negX = scaleValues[ 0 ] < 0;
const negY = scaleValues[ 1 ] < 0;
const negZ = scaleValues[ 2 ] < 0;
const negCount = ( negX ? 1 : 0 ) + ( negY ? 1 : 0 ) + ( negZ ? 1 : 0 );
// decompose() absorbs pairs of negative scales into rotation
// For [-1,-1,-1] → [-1,1,1], Y and Z were absorbed, flip quat.y and quat.w
if ( negCount === 3 ) {
obj.scale.set( scaleValues[ 0 ], scaleValues[ 1 ], scaleValues[ 2 ] );
obj.quaternion.set(
obj.quaternion.x,
- obj.quaternion.y,
obj.quaternion.z,
- obj.quaternion.w
);
}
}
return;
}
// Fallback: handle individual transform ops without order
if ( data[ 'xformOp:translate' ] ) {
const t = data[ 'xformOp:translate' ];
obj.position.set( t[ 0 ], t[ 1 ], t[ 2 ] );
}
if ( data[ 'xformOp:translate:pivot' ] ) {
const p = data[ 'xformOp:translate:pivot' ];
obj.pivot = new Vector3( p[ 0 ], p[ 1 ], p[ 2 ] );
}
if ( data[ 'xformOp:scale' ] ) {
const s = data[ 'xformOp:scale' ];
if ( Array.isArray( s ) ) {
obj.scale.set( s[ 0 ], s[ 1 ], s[ 2 ] );
} else {
obj.scale.set( s, s, s );
}
}
if ( data[ 'xformOp:rotateXYZ' ] ) {
const r = data[ 'xformOp:rotateXYZ' ];
obj.rotation.set(
r[ 0 ] * Math.PI / 180,
r[ 1 ] * Math.PI / 180,
r[ 2 ] * Math.PI / 180
);
}
if ( data[ 'xformOp:orient' ] ) {
const q = data[ 'xformOp:orient' ];
if ( q.length === 4 ) {
obj.quaternion.set( q[ 0 ], q[ 1 ], q[ 2 ], q[ 3 ] );
}
}
}
/**
* Build indexes for efficient lookups.
* Called once during compose() to avoid O(n) scans per lookup.
*/
_buildIndexes() {
// childrenByPath: parentPath -> [childName1, childName2, ...]
this.childrenByPath = new Map();
// attributesByPrimPath: primPath -> Map(attrName -> attrSpec)
this.attributesByPrimPath = new Map();
// materialsByRoot: rootPath -> [materialPath1, materialPath2, ...]
this.materialsByRoot = new Map();
// shadersByMaterialPath: materialPath -> [shaderPath1, shaderPath2, ...]
this.shadersByMaterialPath = new Map();
// geomSubsetsByMeshPath: meshPath -> [subsetPath1, subsetPath2, ...]
this.geomSubsetsByMeshPath = new Map();
for ( const path in this.specsByPath ) {
const spec = this.specsByPath[ path ];
if ( spec.specType === SpecType.Prim ) {
// Build parent-child index
const lastSlash = path.lastIndexOf( '/' );
if ( lastSlash > 0 ) {
const parentPath = path.slice( 0, lastSlash );
const childName = path.slice( lastSlash + 1 );
if ( ! this.childrenByPath.has( parentPath ) ) {
this.childrenByPath.set( parentPath, [] );
}
this.childrenByPath.get( parentPath ).push( { name: childName, path: path } );
} else if ( lastSlash === 0 && path.length > 1 ) {
// Direct child of root
const childName = path.slice( 1 );
if ( ! this.childrenByPath.has( '/' ) ) {
this.childrenByPath.set( '/', [] );
}
this.childrenByPath.get( '/' ).push( { name: childName, path: path } );
}
const typeName = spec.fields.typeName;
// Build material index
if ( typeName === 'Material' ) {
const parts = path.split( '/' );
const rootPath = parts.length > 1 ? '/' + parts[ 1 ] : '/';
if ( ! this.materialsByRoot.has( rootPath ) ) {
this.materialsByRoot.set( rootPath, [] );
}
this.materialsByRoot.get( rootPath ).push( path );
}
// Build shader index (shaders are children or descendants of materials)
if ( typeName === 'Shader' && lastSlash > 0 ) {
// Walk up ancestors to find the nearest Material prim.
// Shaders may be direct children of a Material, or nested
// inside a NodeGraph (common with MaterialX materials).
let ancestorPath = path.slice( 0, lastSlash );
while ( ancestorPath.length > 0 ) {
const ancestorSpec = this.specsByPath[ ancestorPath ];
if ( ancestorSpec && ancestorSpec.specType === SpecType.Prim && ancestorSpec.fields.typeName === 'Material' ) {
if ( ! this.shadersByMaterialPath.has( ancestorPath ) ) {
this.shadersByMaterialPath.set( ancestorPath, [] );
}
this.shadersByMaterialPath.get( ancestorPath ).push( path );
break;
}
const slash = ancestorPath.lastIndexOf( '/' );
if ( slash <= 0 ) break;
ancestorPath = ancestorPath.slice( 0, slash );
}
}
// Build GeomSubset index (subsets are children of meshes)
if ( typeName === 'GeomSubset' && lastSlash > 0 ) {
const meshPath = path.slice( 0, lastSlash );
if ( ! this.geomSubsetsByMeshPath.has( meshPath ) ) {
this.geomSubsetsByMeshPath.set( meshPath, [] );
}
this.geomSubsetsByMeshPath.get( meshPath ).push( path );
}
} else if ( spec.specType === SpecType.Attribute || spec.specType === SpecType.Relationship ) {
// Build attribute index
const dotIndex = path.lastIndexOf( '.' );
if ( dotIndex > 0 ) {
const primPath = path.slice( 0, dotIndex );
const attrName = path.slice( dotIndex + 1 );
if ( ! this.attributesByPrimPath.has( primPath ) ) {
this.attributesByPrimPath.set( primPath, new Map() );
}
this.attributesByPrimPath.get( primPath ).set( attrName, spec );
}
}
}
}
/**
* Check if a path is a direct child of parentPath.
*/
_isDirectChild( parentPath, path, prefix ) {
if ( ! path.startsWith( prefix ) ) return false;
const remainder = path.slice( prefix.length );
if ( remainder.length === 0 ) return false;
// Check for variant paths or simple names
if ( remainder.startsWith( '{' ) ) {
return false; // Variant paths are not direct children
}
return ! remainder.includes( '/' );
}
/**
* Build the scene hierarchy recursively.
* Uses childrenByPath index for O(1) child lookup instead of O(n) iteration.
*/
_buildHierarchy( parent, parentPath ) {
// Collect children from parentPath and any active variant paths
const childEntries = [];
const seenPaths = new Set();
// Get direct children using the index
const directChildren = this.childrenByPath.get( parentPath );
if ( directChildren ) {
for ( const child of directChildren ) {
if ( ! seenPaths.has( child.path ) ) {
seenPaths.add( child.path );
childEntries.push( child );
}
}
}
// Also get children from active variant paths
const variantPaths = this._getVariantPaths( parentPath );
for ( const vp of variantPaths ) {
const variantChildren = this.childrenByPath.get( vp );
if ( variantChildren ) {
for ( const child of variantChildren ) {
if ( ! seenPaths.has( child.path ) ) {
seenPaths.add( child.path );
childEntries.push( child );
}
}
}
}
// Process each child
for ( const { name, path } of childEntries ) {
const spec = this.specsByPath[ path ];
if ( ! spec || spec.specType !== SpecType.Prim ) continue;
const typeName = spec.fields.typeName;
// Check for references/payloads
const refValues = this._getReferences( spec );
if ( refValues.length > 0 ) {
// Get local variant selections from this prim
const localVariants = this._getLocalVariantSelections( spec.fields );
// Resolve all references
const resolvedGroups = [];
for ( const refValue of refValues ) {
const referencedGroup = this._resolveReference( refValue, localVariants );
if ( referencedGroup ) resolvedGroups.push( referencedGroup );
}
if ( resolvedGroups.length > 0 ) {
const attrs = this._getAttributes( path );
// Single reference with single mesh: use optimized path
// This handles the USDZExporter pattern: Xform references geometry file
if ( resolvedGroups.length === 1 ) {
const singleMesh = this._findSingleMesh( resolvedGroups[ 0 ] );
if ( singleMesh && ( typeName === 'Xform' || ! typeName ) ) {
// Merge the mesh into this prim
singleMesh.name = name;
this.applyTransform( singleMesh, spec.fields, attrs );
// Apply material binding from the referencing prim if present
this._applyMaterialBinding( singleMesh, path );
parent.add( singleMesh );
// Still build local children (overrides)
this._buildHierarchy( singleMesh, path );
continue;
}
}
// Create a container for the referenced content
const obj = new Object3D();
obj.name = name;
this.applyTransform( obj, spec.fields, attrs );
// Add all children from all resolved references
for ( const referencedGroup of resolvedGroups ) {
while ( referencedGroup.children.length > 0 ) {
obj.add( referencedGroup.children[ 0 ] );
}
}
parent.add( obj );
// Still build local children (overrides)
this._buildHierarchy( obj, path );
continue;
}
}
// Build appropriate object based on type
if ( typeName === 'SkelRoot' ) {
// Skeletal root - treat as transform but track for skeleton binding
const obj = new Object3D();
obj.name = name;
obj.userData.isSkelRoot = true;
const attrs = this._getAttributes( path );
this.applyTransform( obj, spec.fields, attrs );
parent.add( obj );
this._buildHierarchy( obj, path );
} else if ( typeName === 'Skeleton' ) {
// Build skeleton and store it
const skeleton = this._buildSkeleton( path );
if ( skeleton ) {
this.skeletons[ path ] = skeleton;
}
// Recursively build children (may contain SkelAnimation)
this._buildHierarchy( parent, path );
} else if ( typeName === 'SkelAnimation' ) {
// Skip - animations are processed separately in _buildAnimations
} else if ( typeName === 'Mesh' ) {
const obj = this._buildMesh( path, spec );
if ( obj ) {
parent.add( obj );
this._buildHierarchy( obj, path );
}
} else if ( typeName === 'Camera' ) {
const obj = this._buildCamera( path );
obj.name = name;
const attrs = this._getAttributes( path );
this.applyTransform( obj, spec.fields, attrs );
parent.add( obj );
this._buildHierarchy( obj, path );
} else if ( typeName === 'DistantLight' || typeName === 'SphereLight' || typeName === 'RectLight' || typeName === 'DiskLight' ) {
const obj = this._buildLight( path, typeName );
obj.name = name;
const attrs = this._getAttributes( path );
this.applyTransform( obj, spec.fields, attrs );
parent.add( obj );
this._buildHierarchy( obj, path );
} else if ( typeName === 'Cube' || typeName === 'Sphere' || typeName === 'Cylinder' || typeName === 'Cone' || typeName === 'Capsule' ) {
const obj = this._buildGeomPrimitive( path, spec, typeName );
if ( obj ) {
parent.add( obj );
this._buildHierarchy( obj, path );
}
} else if ( typeName === 'Material' || typeName === 'Shader' || typeName === 'GeomSubset' ) {
// Skip materials/shaders/subsets, they're referenced by meshes
} else {
// Transform node, group, or unknown type
const obj = new Object3D();
obj.name = name;
const attrs = this._getAttributes( path );
this.applyTransform( obj, spec.fields, attrs );
parent.add( obj );
this._buildHierarchy( obj, path );
}
}
}
/**
* Get variant paths for a parent path based on variant selections.
*/
_getVariantPaths( parentPath ) {
const parentSpec = this.specsByPath[ parentPath ];
const variantSetChildren = parentSpec?.fields?.variantSetChildren;
const variantPaths = [];
if ( ! variantSetChildren || variantSetChildren.length === 0 ) {
return variantPaths;
}
for ( const variantSetName of variantSetChildren ) {
// External selections take priority
let selectedVariant = this.externalVariantSelections[ variantSetName ] || null;
// Fall back to file's internal selection
if ( ! selectedVariant ) {
const variantSelection = parentSpec.fields.variantSelection;
selectedVariant = variantSelection ? variantSelection[ variantSetName ] : null;
}
// Fall back to first variant child
if ( ! selectedVariant ) {
const variantSetPath = parentPath + '/{' + variantSetName + '=}';
const variantSetSpec = this.specsByPath[ variantSetPath ];
if ( variantSetSpec?.fields?.variantChildren ) {
selectedVariant = variantSetSpec.fields.variantChildren[ 0 ];
}
}
if ( selectedVariant ) {
const variantPath = parentPath + '/{' + variantSetName + '=' + selectedVariant + '}';
variantPaths.push( variantPath );
}
}
return variantPaths;
}
/**
* Resolve a file path relative to basePath.
*/
_resolveFilePath( refPath ) {
let cleanPath = refPath;
// Remove ./ prefix
if ( cleanPath.startsWith( './' ) ) {
cleanPath = cleanPath.slice( 2 );
}
// Combine with base path
if ( this.basePath ) {
return this.basePath + '/' + cleanPath;
}
return cleanPath;
}
/**
* Resolve a USD reference and return the composed content.
* @param {string} refValue - Reference value like "@./path/to/file.usdc@"
* @param {Object} localVariants - Variant selections to apply
* @returns {Group|null} Composed content or null
*/
_resolveReference( refValue, localVariants = {} ) {
if ( ! refValue ) return null;
const match = refValue.match( /@([^@]+)@(?:<([^>]+)>)?/ );
if ( ! match ) return null;
const filePath = match[ 1 ];
const primPath = match[ 2 ]; // e.g., "/Geometry"
const resolvedPath = this._resolveFilePath( filePath );
// Merge variant selections - external takes priority, then local
const mergedVariants = { ...localVariants, ...this.externalVariantSelections };
// Look up pre-parsed data in assets
const referencedData = this.assets[ resolvedPath ];
if ( ! referencedData ) return null;
// If it's specsByPath data, compose it
if ( referencedData.specsByPath ) {
const composer = new USDComposer( this.manager );
const newBasePath = this._getBasePath( resolvedPath );
const composedGroup = composer.compose( referencedData, this.assets, mergedVariants, newBasePath );
// If a primPath is specified, find and return just that subtree
if ( primPath ) {
const primName = primPath.split( '/' ).pop();
// Find the direct child with this name (not a deep search)
// This is important because there may be multiple objects with the same name
let targetObject = null;
for ( const child of composedGroup.children ) {
if ( child.name === primName ) {
targetObject = child;
break;
}
}
if ( targetObject ) {
// Detach from parent for re-parenting
composedGroup.remove( targetObject );
// Wrap in a group to maintain consistent return type
const wrapper = new Group();
wrapper.add( targetObject );
return wrapper;
}
}
return composedGroup;
}
// If it's already a Three.js Group (legacy support), clone it
if ( referencedData.isGroup || referencedData.isObject3D ) {
return referencedData.clone();
}
return null;
}
/**
* Find a single mesh in the group's shallow hierarchy.
* Only returns a mesh if it's at depth 0 or 1, not deeply nested.
* This preserves transforms in complex hierarchies like Kitchen Set
* while supporting USDZExporter round-trip (Xform > Xform > Mesh pattern).
*/
_findSingleMesh( group ) {
// Check direct children first
for ( const child of group.children ) {
if ( child.isMesh ) {
group.remove( child );
return child;
}
}
// Check grandchildren (USDZExporter pattern: Xform > Geometry > Mesh)
// Only if there's exactly one child with exactly one grandchild
if ( group.children.length === 1 ) {
const child = group.children[ 0 ];
if ( child.children && child.children.length === 1 ) {
const grandchild = child.children[ 0 ];
if ( grandchild.isMesh && ! this._hasNonIdentityTransform( child ) ) {
// Safe to merge - intermediate has identity transform
child.remove( grandchild );
return grandchild;
}
}
}
return null;
}
/**
* Check if an object has a non-identity local transform.
*/
_hasNonIdentityTransform( obj ) {
const pos = obj.position;
const rot = obj.rotation;
const scale = obj.scale;
const hasPosition = pos.x !== 0 || pos.y !== 0 || pos.z !== 0;
const hasRotation = rot.x !== 0 || rot.y !== 0 || rot.z !== 0;
const hasScale = scale.x !== 1 || scale.y !== 1 || scale.z !== 1;
return hasPosition || hasRotation || hasScale;
}
/**
* Get the base path (directory) from a file path.
*/
_getBasePath( filePath ) {
const lastSlash = filePath.lastIndexOf( '/' );
return lastSlash >= 0 ? filePath.slice( 0, lastSlash ) : '';
}
/**
* Extract variant selections from a spec's fields.
*/
_getLocalVariantSelections( fields ) {
const variants = {};
if ( fields.variantSelection ) {
for ( const key in fields.variantSelection ) {
variants[ key ] = fields.variantSelection[ key ];
}
}
return variants;
}
/**
* Get all reference values from a prim spec.
* @returns {string[]} Array of reference strings like "@path@" or "@path@<prim>"
*/
_getReferences( spec ) {
const results = [];
if ( spec.fields.references && spec.fields.references.length > 0 ) {
const ref = spec.fields.references[ 0 ];
if ( typeof ref === 'string' ) {
// Extract all @...@ references (handles both single and array values)
const matches = ref.matchAll( /@([^@]+)@(?:<([^>]+)>)?/g );
for ( const match of matches ) {
results.push( match[ 0 ] );
}
} else if ( ref.assetPath ) {
results.push( '@' + ref.assetPath + '@' );
}
}
if ( results.length === 0 && spec.fields.payload ) {
const payload = spec.fields.payload;
if ( typeof payload === 'string' ) results.push( payload );
else if ( payload.assetPath ) results.push( '@' + payload.assetPath + '@' );
}
return results;
}
/**
* Get attributes for a path from attribute specs.
*/
_getAttributes( path ) {
const attrs = {};
this._collectAttributesFromPath( path, attrs );
// Collect overrides from sibling variants (when path is inside a variant)
const variantMatch = path.match( VARIANT_PATH_REGEX );
if ( variantMatch ) {
const basePath = variantMatch[ 1 ];
const relativePath = variantMatch[ 4 ];
const variantPaths = this._getVariantPaths( basePath );
for ( const vp of variantPaths ) {
if ( path.startsWith( vp ) ) continue;
const overridePath = vp + '/' + relativePath;
this._collectAttributesFromPath( overridePath, attrs );
}
} else {
// Check for variant overrides at ancestor levels
const parts = path.split( '/' );
for ( let i = 1; i < parts.length - 1; i ++ ) {
const ancestorPath = parts.slice( 0, i + 1 ).join( '/' );
const relativePath = parts.slice( i + 1 ).join( '/' );
const variantPaths = this._getVariantPaths( ancestorPath );
for ( const vp of variantPaths ) {
const overridePath = vp + '/' + relativePath;
this._collectAttributesFromPath( overridePath, attrs );
}
}
}
return attrs;
}
_collectAttributesFromPath( path, attrs ) {
// Use the attribute index for O(1) lookup instead of O(n) iteration
const attrMap = this.attributesByPrimPath.get( path );
if ( ! attrMap ) return;
for ( const [ attrName, attrSpec ] of attrMap ) {
if ( attrSpec.fields?.default !== undefined ) {
attrs[ attrName ] = attrSpec.fields.default;
} else if ( attrSpec.fields?.timeSamples ) {
// For animated attributes without default, use the first time sample (rest pose)
const { times, values } = attrSpec.fields.timeSamples;
if ( times && values && times.length > 0 ) {
// Find time 0, or use the first available time
const idx = times.indexOf( 0 );
attrs[ attrName ] = idx >= 0 ? values[ idx ] : values[ 0 ];
}
}
if ( attrSpec.fields?.elementSize !== undefined ) {
attrs[ attrName + ':elementSize' ] = attrSpec.fields.elementSize;
}
if ( attrName.startsWith( 'primvars:' ) && attrSpec.fields?.typeName !== undefined ) {
attrs[ attrName + ':typeName' ] = attrSpec.fields.typeName;
}
}
}
/**
* Build a mesh from a USD geometric primitive (Cube, Sphere, Cylinder, Cone, Capsule).
*/
_buildGeomPrimitive( path, spec, typeName ) {
const attrs = this._getAttributes( path );
const name = path.split( '/' ).pop();
let geometry;
switch ( typeName ) {
case 'Cube': {
const size = attrs[ 'size' ] || 2;
geometry = new BoxGeometry( size, size, size );
break;
}
case 'Sphere': {
const radius = attrs[ 'radius' ] || 1;
geometry = new SphereGeometry( radius, 32, 16 );
break;
}
case 'Cylinder': {
const height = attrs[ 'height' ] || 2;
const radius = attrs[ 'radius' ] || 1;
geometry = new CylinderGeometry( radius, radius, height, 32 );
break;
}
case 'Cone': {
const height = attrs[ 'height' ] || 2;
const radius = attrs[ 'radius' ] || 1;
geometry = new ConeGeometry( radius, height, 32 );
break;
}
case 'Capsule': {
const height = attrs[ 'height' ] || 1;
const radius = attrs[ 'radius' ] || 0.5;
geometry = new CapsuleGeometry( radius, height, 16, 32 );
break;
}
}
// USD defaults axis to "Z", Three.js uses Y
const axis = attrs[ 'axis' ] || 'Z';
if ( axis === 'X' ) {
geometry.rotateZ( - Math.PI / 2 );
} else if ( axis === 'Z' ) {
geometry.rotateX( Math.PI / 2 );
}
const material = this._buildMaterial( path, spec.fields );
const mesh = new Mesh( geometry, material );
mesh.name = name;
this.applyTransform( mesh, spec.fields, attrs );
return mesh;
}
/**
* Build a mesh from a Mesh spec.
*/
_buildMesh( path, spec ) {
const attrs = this._getAttributes( path );
// Check for skinning data
const jointIndices = attrs[ 'primvars:skel:jointIndices' ];
const jointWeights = attrs[ 'primvars:skel:jointWeights' ];
const hasSkinning = jointIndices && jointWeights &&
jointIndices.length > 0 && jointWeights.length > 0;
// Collect GeomSubsets for multi-material support
const geomSubsets = this._getGeomSubsets( path );
let geometry, material;
if ( geomSubsets.length > 0 ) {
geometry = this._buildGeometryWithSubsets( attrs, geomSubsets, hasSkinning );
const meshMaterialPath = this._getMaterialPath( path, spec.fields );
material = geomSubsets.map( subset => {
const matPath = subset.materialPath || meshMaterialPath;
return this._buildMaterialForPath( matPath );
} );
} else {
geometry = this._buildGeometry( path, attrs, hasSkinning );
material = this._buildMaterial( path, spec.fields );
}
const displayColor = attrs[ 'primvars:displayColor' ];
if ( displayColor && displayColor.length >= 3 ) {
const applyDisplayColor = ( mat ) => {
if ( mat.color && mat.color.r === 1 && mat.color.g === 1 && mat.color.b === 1 && ! mat.map ) {
mat.color.setRGB( displayColor[ 0 ], displayColor[ 1 ], displayColor[ 2 ], SRGBColorSpace );
}
};
if ( Array.isArray( material ) ) {
material.forEach( applyDisplayColor );
} else {
applyDisplayColor( material );
}
}
const displayOpacity = attrs[ 'primvars:displayOpacity' ];
if ( displayOpacity && displayOpacity.length === 1 && geomSubsets.length === 0 ) {
const opacity = displayOpacity[ 0 ];
const applyDisplayOpacity = ( mat ) => {
if ( opacity < 1 && mat.opacity === 1 && mat.transparent === false ) {
mat.opacity = opacity;
mat.transparent = true;
}
};
if ( Array.isArray( material ) ) {
material.forEach( applyDisplayOpacity );
} else {
applyDisplayOpacity( material );
}
}
let mesh;
if ( hasSkinning ) {
mesh = new SkinnedMesh( geometry, material );
// Find skeleton path from skel:skeleton relationship
let skelBindingSpec = this.specsByPath[ path + '.skel:skeleton' ];
if ( ! skelBindingSpec ) {
skelBindingSpec = this.specsByPath[ path + '.rel skel:skeleton' ];
}
let skeletonPath = null;
if ( skelBindingSpec ) {
if ( skelBindingSpec.fields.targetPaths && skelBindingSpec.fields.targetPaths.length > 0 ) {
skeletonPath = skelBindingSpec.fields.targetPaths[ 0 ];
} else if ( skelBindingSpec.fields.default ) {
skeletonPath = skelBindingSpec.fields.default.replace( /<|>/g, '' );
}
}
// Get per-mesh joint mapping
const localJoints = attrs[ 'skel:joints' ];
// Get geomBindTransform if present
const geomBindTransform = attrs[ 'primvars:skel:geomBindTransform' ];
this.skinnedMeshes.push( { mesh, skeletonPath, path, localJoints, geomBindTransform } );
} else {
mesh = new Mesh( geometry, material );
}
mesh.name = path.split( '/' ).pop();
this.applyTransform( mesh, spec.fields, attrs );
return mesh;
}
/**
* Build a camera from a Camera spec.
*/
_buildCamera( path ) {
const attrs = this._getAttributes( path );
const projectionToken = attrs[ 'projection' ];
const projection = typeof projectionToken === 'string'
? projectionToken.toLowerCase()
: USD_CAMERA_DEFAULTS.projection;
const clippingRange = attrs[ 'clippingRange' ] || USD_CAMERA_DEFAULTS.clippingRange;
const near = Math.max(
Number.EPSILON,
this._parseNumber( clippingRange[ 0 ], USD_CAMERA_DEFAULTS.clippingRange[ 0 ] )
);
const far = Math.max(
near + Number.EPSILON,
this._parseNumber( clippingRange[ 1 ], USD_CAMERA_DEFAULTS.clippingRange[ 1 ] )
);
const horizontalAperture = this._parseNumber(
attrs[ 'horizontalAperture' ],
USD_CAMERA_DEFAULTS.horizontalAperture
);
const verticalAperture = this._parseNumber(
attrs[ 'verticalAperture' ],
USD_CAMERA_DEFAULTS.verticalAperture
);
const horizontalApertureOffset = this._parseNumber(
attrs[ 'horizontalApertureOffset' ],
USD_CAMERA_DEFAULTS.horizontalApertureOffset
);
const verticalApertureOffset = this._parseNumber(
attrs[ 'verticalApertureOffset' ],
USD_CAMERA_DEFAULTS.verticalApertureOffset
);
const focalLength = this._parseNumber( attrs[ 'focalLength' ], USD_CAMERA_DEFAULTS.focalLength );
const focusDistance = this._parseNumber( attrs[ 'focusDistance' ], USD_CAMERA_DEFAULTS.focusDistance );
const fStop = this._parseNumber( attrs[ 'fStop' ], USD_CAMERA_DEFAULTS.fStop );
let camera;
if ( projection === 'orthographic' ) {
// USD orthographic apertures are in tenths of a world unit.
const width = horizontalAperture / 10;
const height = verticalAperture / 10;
const offsetX = horizontalApertureOffset / 10;
const offsetY = verticalApertureOffset / 10;
camera = new OrthographicCamera(
offsetX - width * 0.5,
offsetX + width * 0.5,
offsetY + height * 0.5,
offsetY - height * 0.5,
near,
far
);
} else {
const safeVerticalAperture = Math.max( Number.EPSILON, verticalAperture );
const safeFocalLength = Math.max( Number.EPSILON, focalLength );
const aspect = horizontalAperture / safeVerticalAperture;
const fov = 2 * Math.atan( safeVerticalAperture / ( 2 * safeFocalLength ) ) * 180 / Math.PI;
camera = new PerspectiveCamera( fov, aspect, near, far );
camera.filmGauge = Math.max( horizontalAperture, verticalAperture );
camera.filmOffset = horizontalApertureOffset;
camera.focus = focusDistance;
camera.setFocalLength( safeFocalLength );
if ( verticalApertureOffset !== 0 ) {
// Three.js supports only horizontal film offset directly.
camera.userData.verticalApertureOffset = verticalApertureOffset;
}
}
camera.userData.fStop = fStop;
camera.userData.usdProjection = projection;
return camera;
}
/**
* Build a light from a UsdLux light spec.
*/
_buildLight( path, typeName ) {
const attrs = this._getAttributes( path );
const intensity = this._parseNumber( attrs[ 'inputs:intensity' ], 1 );
const baseColor = attrs[ 'inputs:color' ] || [ 1, 1, 1 ];
const enableColorTemperature = attrs[ 'inputs:enableColorTemperature' ] === true;
const colorTemperature = this._parseNumber( attrs[ 'inputs:colorTemperature' ], 6500 );
const color = new Color( baseColor[ 0 ], baseColor[ 1 ], baseColor[ 2 ] );
if ( enableColorTemperature ) {
const temp = this._colorTemperature( colorTemperature );
color.multiply( temp );
}
let light;
switch ( typeName ) {
case 'DistantLight':
light = new DirectionalLight( color, intensity );
break;
case 'SphereLight': {
const coneAngle = this._parseNumber( attrs[ 'shaping:cone:angle' ], 0 );
if ( coneAngle > 0 ) {
const angle = coneAngle * Math.PI / 180;
const softness = this._parseNumber( attrs[ 'shaping:cone:softness' ], 0 );
light = new SpotLight( color, intensity, 0, angle, softness );
} else {
light = new PointLight( color, intensity );
}
break;
}
case 'RectLight': {
const width = this._parseNumber( attrs[ 'inputs:width' ], 1 );
const height = this._parseNumber( attrs[ 'inputs:height' ], 1 );
light = new RectAreaLight( color, intensity, width, height );
break;
}
case 'DiskLight': {
const radius = this._parseNumber( attrs[ 'inputs:radius' ], 0.5 );
const side = radius * 2;
light = new RectAreaLight( color, intensity, side, side );
break;
}
}
return light;
}
/**
* Convert a color temperature in Kelvin to an RGB Color.
* Based on Tanner Helland's algorithm.
*/
_colorTemperature( kelvin ) {
const temp = kelvin / 100;
let r, g, b;
if ( temp <= 66 ) {
r = 1;
g = 0.39008157876901960784 * Math.log( temp ) - 0.63184144378862745098;
} else {
r = 1.29293618606274509804 * Math.pow( temp - 60, - 0.1332047592 );
g = 1.12989086089529411765 * Math.pow( temp - 60, - 0.0755148492 );
}
if ( temp >= 66 ) {
b = 1;
} else if ( temp <= 19 ) {
b = 0;
} else {
b = 0.54320678911019607843 * Math.log( temp - 10 ) - 1.19625408914;
}
return new Color(
Math.min( Math.max( r, 0 ), 1 ),
Math.min( Math.max( g, 0 ), 1 ),
Math.min( Math.max( b, 0 ), 1 )
);
}
_parseNumber( value, fallback ) {
const n = Number( value );
return Number.isFinite( n ) ? n : fallback;
}
_getGeomSubsets( meshPath ) {
const subsets = [];
const subsetPaths = this.geomSubsetsByMeshPath.get( meshPath );
if ( ! subsetPaths ) return subsets;
for ( const p of subsetPaths ) {
const attrs = this._getAttributes( p );
const indices = attrs[ 'indices' ];
if ( ! indices || indices.length === 0 ) continue;
// Get material binding - check direct path and variant paths
const materialPath = this._getMaterialBindingTarget( p );
subsets.push( {
name: p.split( '/' ).pop(),
indices: indices,
materialPath: materialPath
} );
}
return subsets;
}
/**
* Get material binding target path, checking variant paths if needed.
*/
_getMaterialBindingTarget( primPath ) {
const attrName = 'material:binding';
// First check direct path
const directPath = primPath + '.' + attrName;
const directSpec = this.specsByPath[ directPath ];
if ( directSpec?.fields?.targetPaths?.length > 0 ) {
return directSpec.fields.targetPaths[ 0 ];
}
// Check variant paths at ancestor levels
const parts = primPath.split( '/' );
for ( let i = 1; i < parts.length; i ++ ) {
const ancestorPath = parts.slice( 0, i + 1 ).join( '/' );
const relativePath = parts.slice( i + 1 ).join( '/' );
const variantPaths = this._getVariantPaths( ancestorPath );
for ( const vp of variantPaths ) {
const overridePath = relativePath ? vp + '/' + relativePath + '.' + attrName : vp + '.' + attrName;
const overrideSpec = this.specsByPath[ overridePath ];
if ( overrideSpec?.fields?.targetPaths?.length > 0 ) {
return overrideSpec.fields.targetPaths[ 0 ];
}
}
}
return null;
}
_buildGeometry( path, fields, hasSkinning = false ) {
const geometry = new BufferGeometry();
const points = fields[ 'points' ];
if ( ! points || points.length === 0 ) return geometry;
const faceVertexIndices = fields[ 'faceVertexIndices' ];
const faceVertexCounts = fields[ 'faceVertexCounts' ];
// Parse polygon holes (Arnold format: [holeFaceIdx, parentFaceIdx, ...])
const polygonHoles = fields[ 'primvars:arnold:polygon_holes' ];
const holeMap = this._buildHoleMap( polygonHoles );
// Compute triangulation pattern once using actual vertex positions
// This pattern will be reused for normals, UVs, etc.
let indices = faceVertexIndices;
let triPattern = null;
if ( faceVertexCounts && faceVertexCounts.length > 0 ) {
const result = this._triangulateIndicesWithPattern( faceVertexIndices, faceVertexCounts, points, holeMap );
indices = result.indices;
triPattern = result.pattern;
}
let positions = points;
if ( indices && indices.length > 0 ) {
positions = this._expandAttribute( points, indices, 3 );
}
geometry.setAttribute( 'position', new BufferAttribute( new Float32Array( positions ), 3 ) );
const normals = fields[ 'normals' ] || fields[ 'primvars:normals' ];
const normalIndicesRaw = fields[ 'normals:indices' ] || fields[ 'primvars:normals:indices' ];
if ( normals && normals.length > 0 ) {
let normalData = normals;
if ( normalIndicesRaw && normalIndicesRaw.length > 0 && triPattern ) {
// Indexed normals - apply triangulation pattern to indices
const triangulatedNormalIndices = this._applyTriangulationPattern( normalIndicesRaw, triPattern );
normalData = this._expandAttribute( normals, triangulatedNormalIndices, 3 );
} else if ( normals.length === points.length ) {
// Per-vertex normals
if ( indices && indices.length > 0 ) {
normalData = this._expandAttribute( normals, indices, 3 );
}
} else if ( triPattern ) {
// Per-face-vertex normals (no separate indices) - use same triangulation pattern
const normalIndices = this._applyTriangulationPattern(
Array.from( { length: normals.length / 3 }, ( _, i ) => i ),
triPattern
);
normalData = this._expandAttribute( normals, normalIndices, 3 );
}
geometry.setAttribute( 'normal', new BufferAttribute( new Float32Array( normalData ), 3 ) );
} else {
// Compute vertex normals from the original indexed topology where
// vertices are shared, then expand them like positions.
const vertexNormals = this._computeVertexNormals( points, indices );
geometry.setAttribute( 'normal', new BufferAttribute( new Float32Array(
this._expandAttribute( vertexNormals, indices, 3 )
), 3 ) );
}
const { uvs, uvIndices } = this._findUVPrimvar( fields );
const numFaceVertices = faceVertexIndices ? faceVertexIndices.length : 0;
if ( uvs && uvs.length > 0 ) {
let uvData = uvs;
if ( uvIndices && uvIndices.length > 0 && triPattern ) {
const triangulatedUvIndices = this._applyTriangulationPattern( uvIndices, triPattern );
uvData = this._expandAttribute( uvs, triangulatedUvIndices, 2 );
} else if ( indices && uvs.length / 2 === points.length / 3 ) {
uvData = this._expandAttribute( uvs, indices, 2 );
} else if ( triPattern && uvs.length / 2 === numFaceVertices ) {
// Per-face-vertex UVs (faceVarying, no separate indices)
const uvIndicesFromPattern = this._applyTriangulationPattern(
Array.from( { length: numFaceVertices }, ( _, i ) => i ),
triPattern
);
uvData = this._expandAttribute( uvs, uvIndicesFromPattern, 2 );
}
geometry.setAttribute( 'uv', new BufferAttribute( new Float32Array( uvData ), 2 ) );
}
// Second UV set (st1) for lightmaps/AO
const { uvs2, uv2Indices } = this._findUV2Primvar( fields );
if ( uvs2 && uvs2.length > 0 ) {
let uv2Data = uvs2;
if ( uv2Indices && uv2Indices.length > 0 && triPattern ) {
const triangulatedUv2Indices = this._applyTriangulationPattern( uv2Indices, triPattern );
uv2Data = this._expandAttribute( uvs2, triangulatedUv2Indices, 2 );
} else if ( indices && uvs2.length / 2 === points.length / 3 ) {
uv2Data = this._expandAttribute( uvs2, indices, 2 );
} else if ( triPattern && uvs2.length / 2 === numFaceVertices ) {
// Per-face-vertex UV2 (faceVarying, no separate indices)
const uv2IndicesFromPattern = this._applyTriangulationPattern(
Array.from( { length: numFaceVertices }, ( _, i ) => i ),
triPattern
);
uv2Data = this._expandAttribute( uvs2, uv2IndicesFromPattern, 2 );
}
geometry.setAttribute( 'uv1', new BufferAttribute( new Float32Array( uv2Data ), 2 ) );
}
// Add skinning attributes
if ( hasSkinning ) {
const jointIndices = fields[ 'primvars:skel:jointIndices' ];
const jointWeights = fields[ 'primvars:skel:jointWeights' ];
const elementSize = fields[ 'primvars:skel:jointIndices:elementSize' ] || 4;
if ( jointIndices && jointWeights ) {
const numVertices = positions.length / 3;
let skinIndexData, skinWeightData;
if ( indices && indices.length > 0 ) {
skinIndexData = this._expandAttribute( jointIndices, indices, elementSize );
skinWeightData = this._expandAttribute( jointWeights, indices, elementSize );
} else {
skinIndexData = jointIndices;
skinWeightData = jointWeights;
}
const skinIndices = new Uint16Array( numVertices * 4 );
const skinWeights = new Float32Array( numVertices * 4 );
this._selectTopWeights( skinIndexData, skinWeightData, elementSize, numVertices, skinIndices, skinWeights );
geometry.setAttribute( 'skinIndex', new BufferAttribute( skinIndices, 4 ) );
geometry.setAttribute( 'skinWeight', new BufferAttribute( skinWeights, 4 ) );
}
}
return geometry;
}
_buildGeometryWithSubsets( fields, geomSubsets, hasSkinning = false ) {
const geometry = new BufferGeometry();
const points = fields[ 'points' ];
if ( ! points || points.length === 0 ) return geometry;
const faceVertexIndices = fields[ 'faceVertexIndices' ];
const faceVertexCounts = fields[ 'faceVertexCounts' ];
if ( ! faceVertexCounts || faceVertexCounts.length === 0 ) return geometry;
const polygonHoles = fields[ 'primvars:arnold:polygon_holes' ];
const holeMap = this._buildHoleMap( polygonHoles );
const holeFaces = holeMap.holeFaces;
const parentToHoles = holeMap.parentToHoles;
const { uvs, uvIndices } = this._findUVPrimvar( fields );
const { uvs2, uv2Indices } = this._findUV2Primvar( fields );
const normals = fields[ 'normals' ] || fields[ 'primvars:normals' ];
const normalIndicesRaw = fields[ 'normals:indices' ] || fields[ 'primvars:normals:indices' ];
const jointIndices = hasSkinning ? fields[ 'primvars:skel:jointIndices' ] : null;
const jointWeights = hasSkinning ? fields[ 'primvars:skel:jointWeights' ] : null;
const elementSize = fields[ 'primvars:skel:jointIndices:elementSize' ] || 4;
// Build face-to-triangle mapping (accounting for holes)
const faceTriangleOffset = [];
let triangleCount = 0;
for ( let i = 0; i < faceVertexCounts.length; i ++ ) {
faceTriangleOffset.push( triangleCount );
// Skip hole faces - they're triangulated with their parent
if ( holeFaces.has( i ) ) continue;
const count = faceVertexCounts[ i ];
const holes = parentToHoles.get( i );
if ( holes && holes.length > 0 ) {
// For faces with holes, count triangles based on total vertices
// Earcut produces (total_vertices - 2) triangles for any polygon including holes
let totalVerts = count;
for ( const holeIdx of holes ) {
totalVerts += faceVertexCounts[ holeIdx ];
}
triangleCount += totalVerts - 2;
} else if ( count >= 3 ) {
triangleCount += count - 2;
}
}
const triangleToSubset = new Int32Array( triangleCount ).fill( - 1 );
for ( let si = 0; si < geomSubsets.length; si ++ ) {
const subset = geomSubsets[ si ];
for ( let i = 0; i < subset.indices.length; i ++ ) {
const faceIdx = subset.indices[ i ];
if ( faceIdx >= faceVertexCounts.length ) continue;
const triStart = faceTriangleOffset[ faceIdx ];
const triCount = faceVertexCounts[ faceIdx ] - 2;
for ( let t = 0; t < triCount; t ++ ) {
triangleToSubset[ triStart + t ] = si;
}
}
}
// Sort triangles by subset
const sortedTriangles = [];
for ( let tri = 0; tri < triangleCount; tri ++ ) {
sortedTriangles.push( { original: tri, subset: triangleToSubset[ tri ] } );
}
sortedTriangles.sort( ( a, b ) => a.subset - b.subset );
const groups = [];
let currentSubset = sortedTriangles.length > 0 ? sortedTriangles[ 0 ].subset : - 1;
let groupStart = 0;
for ( let i = 0; i < sortedTriangles.length; i ++ ) {
if ( sortedTriangles[ i ].subset !== currentSubset ) {
if ( currentSubset >= 0 ) {
groups.push( {
start: groupStart * 3,
count: ( i - groupStart ) * 3,
materialIndex: currentSubset
} );
}
currentSubset = sortedTriangles[ i ].subset;
groupStart = i;
}
}
if ( currentSubset >= 0 && sortedTriangles.length > groupStart ) {
groups.push( {
start: groupStart * 3,
count: ( sortedTriangles.length - groupStart ) * 3,
materialIndex: currentSubset
} );
}
for ( const group of groups ) {
geometry.addGroup( group.start, group.count, group.materialIndex );
}
// Triangulate original data using consistent pattern
const { indices: origIndices, pattern: triPattern } = this._triangulateIndicesWithPattern( faceVertexIndices, faceVertexCounts, points, holeMap );
const numFaceVertices = faceVertexCounts.reduce( ( a, b ) => a + b, 0 );
const faceVaryingIdentity = ( uvs && ! uvIndices && uvs.length / 2 === numFaceVertices ) ||
( uvs2 && ! uv2Indices && uvs2.length / 2 === numFaceVertices )
? this._applyTriangulationPattern( Array.from( { length: numFaceVertices }, ( _, i ) => i ), triPattern )
: null;
const origUvIndices = uvIndices
? this._applyTriangulationPattern( uvIndices, triPattern )
: ( uvs && uvs.length / 2 === numFaceVertices ? faceVaryingIdentity : null );
const origUv2Indices = uv2Indices
? this._applyTriangulationPattern( uv2Indices, triPattern )
: ( uvs2