UE4的材质编辑器的每个节点实际上是一个代码片段,通过连线拼接起来,然后放到一个shader模板里面,生成一个shaderTemplate文件。
shader模板文件是事先写好的,MaterialTemplate.ush。
HLSLTranslator文件里可以看到相关的代码,包含template文件,包含一些材质节点的函数。感觉hlsltranslator是用来把节点代码包括自带的节点代码转换成hlsl代码的一个翻译的东西。
我们写自己的ush文件,如下:
添加到模板中,如下:
重新生成工程,然后我们在材质编辑器中创建一个custom节点,在这个节点的code处输入如下代码:
在custom节点里就能够引用我们自己写的函数了。
我们在hlslTranslator文件里添加这两行代码
在生成的hlsl代码里就能够找到这句。
而ue4材质编辑器自带的表达式都是一个个类,
Expression类会调用COmpiler,比如:
那FMaterialCompiler干了啥?
比如texture函数是干啥的?
virtual int32 Texture(UTexture* InTexture,int32& TextureReferenceIndex,ESamplerSourceMode SamplerSource=SSM_FromTextureAsset, ETextureMipValueMode MipValueMode = TMVM_None) override
{
if (FeatureLevel == ERHIFeatureLevel::ES2 && ShaderFrequency == SF_Vertex)
{
if (MipValueMode != TMVM_MipLevel)
{
Errorf(TEXT("Sampling from vertex textures requires an absolute mip level on feature level ES2"));
return INDEX_NONE;
}
}
else if (ShaderFrequency != SF_Pixel
&& ErrorUnlessFeatureLevelSupported(ERHIFeatureLevel::ES3_1) == INDEX_NONE)
{
return INDEX_NONE;
}
//材质类型
EMaterialValueType ShaderType = InTexture->GetMaterialType();
//贴图id
TextureReferenceIndex = Material->GetReferencedTextures().Find(InTexture);
#if DO_CHECK
// UE-3518: Additional pre-assert logging to help determine the cause of this failure.
if (TextureReferenceIndex == INDEX_NONE)
{
const TArray<UTexture*>& ReferencedTextures = Material->GetReferencedTextures();
UE_LOG(LogMaterial, Error, TEXT("Compiler->Texture() failed to find texture '%s' in referenced list of size '%i':"), *InTexture->GetName(), ReferencedTextures.Num());
for (int32 i = 0; i < ReferencedTextures.Num(); ++i)
{
UE_LOG(LogMaterial, Error, TEXT("%i: '%s'"), i, ReferencedTextures[i] ? *ReferencedTextures[i]->GetName() : TEXT("nullptr"));
}
}
#endif
checkf(TextureReferenceIndex != INDEX_NONE, TEXT("Material expression called Compiler->Texture() without implementing UMaterialExpression::GetReferencedTexture properly"));
return AddUniformExpression(new FMaterialUniformExpressionTexture(TextureReferenceIndex, SamplerSource),ShaderType,TEXT(""));
}
AddUniformExpression会创建一块shader代码块
// AddUniformExpression - Adds an input to the Code array and returns its index.
int32 AddUniformExpression(FMaterialUniformExpression* UniformExpression,EMaterialValueType Type,const TCHAR* Format,...)
{
if (Type == MCT_Unknown)
{
return INDEX_NONE;
}
check(UniformExpression);
// Only a texture uniform expression can have MCT_Texture type
if ((Type & MCT_Texture) && !UniformExpression->GetTextureUniformExpression() && !UniformExpression->GetExternalTextureUniformExpression())
{
return Errorf(TEXT("Operation not supported on a Texture"));
}
// External textures must have an external texture uniform expression
if ((Type & MCT_TextureExternal) && !UniformExpression->GetExternalTextureUniformExpression())
{
return Errorf(TEXT("Operation not supported on an external texture"));
}
if (Type == MCT_StaticBool)
{
return Errorf(TEXT("Operation not supported on a Static Bool"));
}
if (Type == MCT_MaterialAttributes)
{
return Errorf(TEXT("Operation not supported on a MaterialAttributes"));
}
bool bFoundExistingExpression = false;
// Search for an existing code chunk with the same uniform expression in the array of all uniform expressions used by this material.
for (int32 ExpressionIndex = 0; ExpressionIndex < UniformExpressions.Num() && !bFoundExistingExpression; ExpressionIndex++)
{
FMateria