Create Foliage in C++

Hi guys,

I am working on some procedural terrain and with that I wanted to spawn foliage.
The only hint about spawning foliage I have found was this - Manually Create Foliage C++ - Asset Creation - Unreal Engine Forums , but

A) only including #include "Runtime/Foliage/Public/InstancedFoliageActor.h" creates about 40 errors (mostly syntax and Error E:\Unreal\Epic Games\4.13\Engine\Source\Runtime/Foliage/Public/InstancedFoliageActor.h(11) : fatal error C1083: Cannot open include file: 'InstancedFoliageActor.generated.h': No such file or directory

B) I have managed to re-create it in Blueprint, but the performance is very poor (can render about 600 instances, but using Foliage paint tool about 30k)

Can anyone please give some code/advice how to properly spawn foliage in c++? It would be great to have same culling setting as in Foliage paint, since setting “Set Max Draw Distance” does not have the nice fade in and out animation and using “Set Cull Distances” instead of “Set Max Draw Distance” did absolutely nothing.

Thanks for any help

Crispy

Hi Bro.

I am the one that answered the question of the link that you mention.

I did a small example project about foliage creation with C ++, it’s really small indeed.

[Download][1]

The code is found in the LevelScriptor Class which is the base class of the Level Blueprint, yo can change to the new one in this window:

The problem was that I forgot to mention this Note:
you need add Foliage to the PublicDependencyModuleNames.AddRange (new string [] { “Core”, “CoreUObject”, “Engine”, “InputCore”, “Foliage”} ); in YourProjec.Build.cs File.

Well this is the example. Enjoy it

Regards

Sir, you are genius. I thank you kindly for this great help.

Agreed! Thank you Horacio - very helpful. cheers

So i made it working but I have problems saving and further adjusting the foliage.
Say i have some datatables that i want to use to populate the foliagetool. The foliage is not saved except when i add at least one instance via the foliage tool after running the c++ code; then it is somehow marked dirty i assume and it can be saved without being discarded when loading a different level or reopening the project. But I can never access the foliage again with the foliage tool as it is invisible to it, only via code.

Any hints?

Best regards

void UMyBlueprintFunctionLibrary::Test(UObject * WorldContextObject, UStaticMesh* StaticMesh, UFoliageType* FoliageType)
{
UWorld* World = WorldContextObject->GetWorld();

	AInstancedFoliageActor* FoliageActor = AInstancedFoliageActor::GetInstancedFoliageActorForCurrentLevel(World, false);

	if (FoliageActor != nullptr)
	{
		FFoliageInfo* FoliageInfo = FoliageActor->FindInfo(FoliageType);
		if (FoliageInfo != nullptr)
		{
			FFoliageInstance NewFoliageInstance = FFoliageInstance();
			/*	
			*	From FFoliageInstancePlacementInfo
			*	Set your desired transform here.
			*/
			NewFoliageInstance.Location = FVector(0.0f, 0.0f, 500.0f);
			NewFoliageInstance.Location = FVector(0.0f, 90.0f, 0.0f);

			FoliageInfo->AddInstance(FoliageActor, FoliageType, NewFoliageInstance);
		}
	}
}

Found a solution. Better use the ‘AddInstance’ function to properly add it to your desired FoliageType. I think you have to trigger some notify function after adding all instances but when you use this code the instances are correctly saved and when you restart the foliagemode you have the updated instance count and you can also edit your placed instances with the foliagetool.

2 Likes

Thank you Sir!!!