import { isModClassName , modelConfig , Paintable } from '@lib/definitions/models' ; import { findActorByName } from '@lib/graphics/entityHelper' ; import { Actor , ByteProperty , StructProperty } from 'satisfactory-json' ; import { Color , Material , MeshMatcapMaterial , Texture } from 'three' ; /**
 * Factory that creates and caches materials
 */ export default class ColorFactory { classColors : { [ id ] : string ; } ; materials : { [ id ] : Material ; } = { } ; coloredMaterials : Material [ ] = [ ] ; matcap : Texture ; selectedMaterial : Material ; showCustomPaints : boolean ; constructor ( matcap , showCustomPaints , classColors ) { this . matcap = matcap ; this . showCustomPaints = showCustomPaints ; this . classColors = classColors ; this . selectedMaterial = new MeshMatcapMaterial ( { color : 0xffffff , matcap : this . matcap } ) ; this . setupColoredMaterials ( ) ; this . setupDefaultMaterials ( ) ; } setupDefaultMaterials ( ) { for ( const prop in modelConfig ) { let color ; if ( this . classColors [ prop ] !== undefined ) { color = new Color ( this . classColors [ prop ] ) ; } else { color = new Color ( modelConfig [ prop ] . color ) ; } this . materials [ prop ] = new MeshMatcapMaterial ( { color : color , matcap : this . matcap } ) ; } } setupColoredMaterials ( ) { const defaultColors = [ new Color ( '#fcb26b' ) , new Color ( '#73a9d2' ) , new Color ( '#dd7550' ) , new Color ( '#666375' ) , new Color ( '#e1e1e9' ) , new Color ( '#bfe798' ) , new Color ( '#f890e2' ) , new Color ( '#bbf6ec' ) , new Color ( '#b59c5e' ) , new Color ( '#f9ecd9' ) , new Color ( '#c490f9' ) , new Color ( '#84dbb8' ) , new Color ( '#f5f09e' ) , new Color ( '#97978f' ) , new Color ( '#b048aa' ) , new Color ( '#838283' ) ] ; const buildableSubsystem = findActorByName ( 'Persistent_Level:PersistentLevel.BuildableSubsystem' ) ; if ( buildableSubsystem !== undefined ) { for ( let i = 0 ; i < buildableSubsystem . entity . properties . length ; i ++ ) { const element = buildableSubsystem . entity . properties [ i ] as StructProperty ; if ( element . name === 'mColorSlotsPrimary' ) { defaultColors [ element . index ] = new Color ( element . value . r / 255 , element . value . g / 255 , element . value . b / 255 ) ; } if ( element . name === 'mColorSlotsPrimary_Linear' ) { for ( let j = 0 ; j < element . value . values . length ; j ++ ) { const color = element . value . values [ j ] ; defaultColors [ j ] = new Color ( color . r , color . g , color . b ) ; } break ; } } } for ( let i = 0 ; i < defaultColors . length ; i ++ ) { const color = defaultColors [ i ] ; this . coloredMaterials [ i ] = new MeshMatcapMaterial ( { color : color , matcap : this . matcap } ) ; } } createMaterial ( actor ) { if ( this . showCustomPaints ) { const isPaintable = modelConfig [ actor . className ] !== undefined ? modelConfig [ actor . className ] . paintable : Paintable . FALSE ; for ( let i = 0 ; i < actor . entity . properties . length ; i ++ ) { const element = actor . entity . properties [ i ] as ByteProperty ; if ( element . name === 'mColorSlot' ) { if ( isPaintable === Paintable . FALSE && ! isModClassName ( actor . className ) ) { } return this . coloredMaterials [ element . value . value ! ] ; } } if ( isPaintable === Paintable . TRUE ) { return this . coloredMaterials [ 0 ] ; } } if ( this . materials [ actor . className ] === undefined ) { return this . materials [ 'undefined' ] ; } else { return this . materials [ actor . className ] ; } } getColor ( actor ) { return ( this . createMaterial ( actor ) as MeshMatcapMaterial ) . color ; } getSelectedMaterial ( ) { return this . selectedMaterial ; } }