import { Material } from 'three' import matches, { Validator } from 'ts-matches' import { defineAction, defineState, dispatchAction, getState, StateDefinition, useState } from '@xrengine/hyperflux' import { MaterialComponentType } from './components/MaterialComponent' import { MaterialPrototypeComponentType } from './components/MaterialPrototypeComponent' import { MaterialSource, MaterialSourceComponentType } from './components/MaterialSource' import MeshBasicMaterial from './constants/material-prototypes/MeshBasicMaterial.mat' import MeshLambertMaterial from './constants/material-prototypes/MeshLambertMaterial.mat' import MeshMatcapMaterial from './constants/material-prototypes/MeshMatcapMaterial.mat' import MeshPhongMaterial from './constants/material-prototypes/MeshPhongMaterial.mat' import MeshPhysicalMaterial from './constants/material-prototypes/MeshPhysicalMaterial.mat' import MeshStandardMaterial from './constants/material-prototypes/MeshStandardMaterial.mat' import MeshToonMaterial from './constants/material-prototypes/MeshToonMaterial.mat' import { ShaderMaterial } from './constants/material-prototypes/ShaderMaterial.mat' import { ShadowMaterial } from './constants/material-prototypes/ShadowMaterial.mat' import { registerMaterialPrototype } from './functions/MaterialLibraryFunctions' export type MaterialLibraryType = { prototypes: Record materials: Record sources: Record } export const MaterialLibraryState: StateDefinition = defineState({ name: 'MaterialLibraryState', initial: { prototypes: {}, materials: {}, sources: {} } as MaterialLibraryType }) export const getMaterialLibrary = () => getState(MaterialLibraryState) export const useMaterialLibrary = () => useState(getMaterialLibrary()) export const MaterialLibraryActions = { RegisterMaterial: defineAction({ type: 'xre.assets.MaterialLibrary.REGISTER_MATERIAL', material: matches.object as Validator, src: matches.object as Validator }), RegisterPrototype: defineAction({ type: 'xre.assets.MaterialLibrary.REGISTER_PROTOTYPE', $prototype: matches.object as Validator }), RemoveSource: defineAction({ type: 'xre.assets.MaterialLibrary.REMOVE_SOURCE', src: matches.object as Validator }) } export function initializeMaterialLibrary() { //load default prototypes from source ;[ MeshBasicMaterial, MeshStandardMaterial, MeshMatcapMaterial, MeshPhysicalMaterial, MeshLambertMaterial, MeshPhongMaterial, MeshToonMaterial, ShaderMaterial, ShadowMaterial ].map(registerMaterialPrototype) }