Download

Exsisting material editor functions(where to read)

Where can I read the HLSL code of the currently present Material editor functions?

I want to write a few custom nodes but, I don’t have time to learn HLSL as for now. So I just want to check how some pre-exsisting material function codes are written and learn from there.

I tried to find the material function codes but I couldn’t.

You’ll want to go have a look at the classes in Source\Runtime\Engine\Classes\Materials. These are the existing material nodes.

Already searched, they contain just the header files. I traced the functions to the MaterialExpression.cpp file but couldn’t find anything there.

If you want to see the guts of where the HLSL actually gets generated for the nodes. Then you will need to look at Source\Runtime\Engine\Private\Materials\HLSLMaterialTranslator.h start looking around Line 1510. You will see different functions for the different nodes, which are called from the MaterialExpressions.cpp classes. So if you wanted to add your own. You would:

  1. create a new class based on UMaterialExpression overriding the Compile function at a minimum

  2. From the compile function you will want to call Compiler->YourNewFunction( Input.Compile( Compiler ) );

  3. Add a new function to FMaterialCompiler in MaterialCompiler.h matching the name you used above

  4. Implement the function in FProxyMaterialCompiler in the same file

  5. Implement the function in FHLSLMaterialTranslator in HLSLMaterialTranslator.h

And that should be it. I have done this in the past, but am going off of memory here, so if I missed a step, sorry.

Thanks, I did find some info on a few functions although I didn’t understand much. I want to add a few custom noise generating functions so I wanted to see its already existing noise function.

But all I found on the existing noise function other than this


virtual int32 Noise(int32 Position, float Scale, int32 Quality, uint8 NoiseFunction, bool bTurbulence, int32 Levels, float OutputMin, float OutputMax, float LevelScale, int32 FilterWidth) override
	{
		if (ErrorUnlessFeatureLevelSupported(ERHIFeatureLevel::SM4) == INDEX_NONE)
		{
			return INDEX_NONE;
		}

		if(Position == INDEX_NONE || FilterWidth == INDEX_NONE)
		{
			return INDEX_NONE;
		}

		// to limit performance problems due to values outside reasonable range
		Levels = FMath::Clamp(Levels, 1, 10);

		int32 ScaleConst = Constant(Scale);
		int32 QualityConst = Constant(Quality);
		int32 NoiseFunctionConst = Constant(NoiseFunction);
		int32 TurbulenceConst = Constant(bTurbulence);
		int32 LevelsConst = Constant(Levels);
		int32 OutputMinConst = Constant(OutputMin);
		int32 OutputMaxConst = Constant(OutputMax);
		int32 LevelScaleConst = Constant(LevelScale);

		return AddCodeChunk(MCT_Float, 
			TEXT("MaterialExpressionNoise(%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"), 
			*GetParameterCode(Position),
			*GetParameterCode(ScaleConst),
			*GetParameterCode(QualityConst),
			*GetParameterCode(NoiseFunctionConst),
			*GetParameterCode(TurbulenceConst),
			*GetParameterCode(LevelsConst),
			*GetParameterCode(OutputMinConst),
			*GetParameterCode(OutputMaxConst),
			*GetParameterCode(LevelScaleConst),
			*GetParameterCode(FilterWidth));
	}

That particular node, is adding a function call in the shader to MaterialExpressionNoise which can be found in Common.usf. So if you wanted to add your own noise functions. You could model off that same Noise function and just add your additional custom noise functions to Common.usf

If you look at MaterialExpressionNoise.h and MaterialExpressions.cpp. You will see that the compile function for that class calls Compiler->Noise. Which is the function your looking at above. So I would base my class off of the UMaterialExpressionNoise class.

Thanks was really helpful… Any advantage of using this method instead of adding a custom material node?