Class MaterialRepository

Repository class for managing material types and instances. Handles registration, creation, and lifecycle management of materials.

Constructors

Methods

  • Internal

    Invalidates all shader programs for all registered materials. This method is typically called when global shader settings change and all materials need to recompile their shaders.

    Returns void

  • Creates a new material instance of the specified type. The material type must be registered before calling this method. This method handles instance counting and initialization.

    Parameters

    • materialTypeName: string

      The name of the registered material type

    • materialNode: AbstractMaterialContent

      The material content definition for this specific instance

    Returns Material

    A new Material instance with proper initialization

    Error if the material type is not registered or maximum instances exceeded

  • Forcibly registers a material type, overwriting any existing registration. This method bypasses the duplicate check and always performs the registration. Use with caution as it can replace existing material type definitions.

    Parameters

    • materialTypeName: string

      The unique name identifier for the material type

    • materialNode: AbstractMaterialContent

      The material content definition containing shader semantics and properties

    • maxInstanceNumber: number = Config.maxMaterialInstanceForEachType

      The maximum number of instances that can be created for this material type

    Returns boolean

    Always returns true as the registration is forced

  • Gets all currently active material instances from the repository. Returns an array of WeakRef objects that may contain undefined values if materials have been garbage collected.

    Returns WeakRef<Material>[]

    Array of WeakRef objects pointing to all registered materials

  • Gets the memory location offset for a specific property of a material type. The offset is calculated in 16-byte aligned units for GPU buffer access. This is used for efficient GPU memory access in shaders.

    Parameters

    • materialTypeName: string

      The name of the material type

    • propertyName: string

      The shader semantic name of the property

    Returns number

    The byte offset divided by 16 (IndexOf16Bytes) for the property location

  • Retrieves a material instance by its unique identifier. Returns undefined if the material doesn't exist or has been garbage collected.

    Parameters

    • materialUid: number

      The unique identifier of the material to retrieve

    Returns undefined | Material

    The material instance if found and still alive, undefined otherwise

  • Checks if the maximum number of instances for a material type has been reached or exceeded. This is useful for preventing memory overflow by limiting material instance creation.

    Parameters

    • materialTypeName: string

      The name of the material type to check

    Returns boolean

    True if the material type has reached or exceeded its maximum instance count

  • Determines if a new material node is compatible with an existing material. Compatibility is checked by comparing shader semantics information arrays. Materials are compatible if they have identical shader semantic structures.

    Parameters

    • currentMaterial: Material

      The existing material to compare against

    • newMaterialNode: AbstractMaterialContent

      The new material node to check for compatibility

    Returns boolean

    True if the materials are compatible, false otherwise

  • Checks if a material type is already registered in the repository.

    Parameters

    • materialTypeName: string

      The name of the material type to check

    Returns boolean

    True if the material type is registered, false otherwise

  • Registers a new material type with the repository. This method creates the necessary data structures and allocates memory for the material type. If the material type is already registered, the registration will be skipped.

    Parameters

    • materialTypeName: string

      The unique name identifier for the material type

    • materialNode: AbstractMaterialContent

      The material content definition containing shader semantics and properties

    • maxInstanceNumber: number = Config.maxMaterialInstanceForEachType

      The maximum number of instances that can be created for this material type

    Returns boolean

    True if the material type was successfully registered, false if it was already registered