Files
UShaderLab/Source/UShaderLab/Public/ShaderLabMaterialInstanceConstant.h
2026-07-02 15:14:13 +08:00

51 lines
2.1 KiB
C++

// Copyright UShaderLab. All Rights Reserved.
#pragma once
#include "CoreMinimal.h"
#include "Materials/MaterialInstanceConstant.h"
#include "ShaderLabMaterialInstanceConstant.generated.h"
/**
* A Material Instance Constant parented (directly or transitively) to a ShaderLab in-memory
* /Script base material.
*
* ShaderLab base materials live as pure in-memory /Script objects with NO serialized shader map.
* To render an instance of one without serializing the base's shader map (and the engine changes
* that would require), this subclass forces a *self-contained* static permutation — i.e. flips
* UMaterialInstance::bHasStaticPermutationResource to true so the instance compiles + cooks its OWN
* shader map through the standard pipeline — but ONLY when its immediate parent is one of those
* /Script base materials (a "root" instance).
*
* When the parent is another material instance (a "child"), it falls through to stock behavior so
* the child DEFERS to and SHARES the root's shader map. That keeps the shader-map count bounded:
* many color/parameter variants off one root share a single compiled map instead of each compiling
* its own (no combinatorial explosion). See ShaderLabMICShaderMapSpikeTest for the proof.
*/
UCLASS()
class USHADERLAB_API UShaderLabMaterialInstanceConstant : public UMaterialInstanceConstant
{
GENERATED_BODY()
public:
//~ UMaterialInstance interface
virtual bool HasOverridenBaseProperties() const override;
//~ End UMaterialInstance interface
#if WITH_EDITOR
//~ UObject interface
virtual void GetAssetRegistryTags(FAssetRegistryTagsContext Context) const override;
//~ End UObject interface
#endif
/** Asset-registry tag key exposing the source `.usl` virtual path for Content Browser filtering. */
static const FName ShaderLabPathTagName;
/**
* Resolve the source `.usl` virtual path for a material by walking up the parent chain to the
* ShaderLab base material and reading its UShaderLabMaterialAssetUserData. Empty if not a ShaderLab
* material. Works for any instance depth (child instances inherit the root's path).
*/
static FString GetShaderLabPath(const UMaterialInterface* Material);
};