import MdxModel from './model' ; import { Batch } from './batch' ; import BatchGroup from './batchgroup' ; import EmitterGroup from './emittergroup' ; import GenericObject from './genericobject' ; import ParticleEmitter2Object from './particleemitter2object' ; import RibbonEmitterObject from './ribbonemitterobject' ; import EventObjectEmitterObject from './eventobjectemitterobject' ; function getPrio ( object ) { if ( object instanceof Batch || object instanceof RibbonEmitterObject ) { return object . layer . priorityPlane ; } else if ( object instanceof ParticleEmitter2Object ) { return object . priorityPlane ; } else { return 0 ; } } function matchingGroup ( group , object ) { if ( group instanceof BatchGroup ) { return ( object instanceof Batch ) && ( object . skinningType === group . skinningType ) && ( object . isHd === group . isHd ) ; } else { return ( object instanceof GenericObject ) ; } } function createMatchingGroup ( model , object ) { if ( object instanceof Batch ) { return new BatchGroup ( model , object . skinningType , object . isHd ) ; } else { return new EmitterGroup ( model ) ; } } export default function setupGroups ( model ) { const opaqueBatches = [ ] ; let translucentBatches = [ ] ; for ( const batch of model . batches ) { if ( batch . layer . filterMode < 2 ) { opaqueBatches . push ( batch ) ; } else { translucentBatches . push ( batch ) ; } } const opaqueGroups = model . opaqueGroups ; const translucentGroups = model . translucentGroups ; let currentGroup = null ; for ( const object of opaqueBatches ) { if ( ! currentGroup || ! matchingGroup ( currentGroup , object ) ) { currentGroup = < BatchGroup > createMatchingGroup ( model , object ) ; opaqueGroups . push ( currentGroup ) ; } currentGroup . objects . push ( object . index ) ; } translucentBatches = translucentBatches . sort ( ( a , b ) => a . layer . filterMode - b . layer . filterMode ) ; const objects = [ ... translucentBatches , ... model . eventObjects , ... model . particleEmitters2 , ... model . ribbonEmitters ] . sort ( ( a , b ) => getPrio ( a ) - getPrio ( b ) ) ; currentGroup = null ; for ( const object of objects ) { if ( object instanceof Batch || object . geometryEmitterType !== - 1 ) { if ( ! currentGroup || ! matchingGroup ( currentGroup , object ) ) { currentGroup = createMatchingGroup ( model , object ) ; translucentGroups . push ( currentGroup ) ; } currentGroup . objects . push ( object . index ) ; } } }