Houdini技术体系基础管线(四):UE4植被系统 下篇
图文教程技术文章技术文库3D建模
显示全部 9
2376 1
实名

通过了实名认证的内容创造者

发布于 2019-4-5 14:26:24

您需要 登录 才可以下载或查看,没有账号?注册

x
本帖最后由 悦动的喵 于 2019-4-6 12:04 编辑

Houdini技术体系基础管线(四):UE4植被系统 下篇

本文转自知乎,作者TraceYang

作者相关文章

背景
在上篇中,实现了使用Houdini在UE4里根据地形过程生成植被的最基本的原型。并且支持把植被在UE4里Bake成使用的HierarchicalInstancedStaticMeshComponent的BP形式,一定程度上解决了植被渲染效率的问题。
4c7ee119c9ff6d1c41878c8019a43dc2_v2-ac156ea3b64e54d4d3514e37751c031b_r.jpg
但这种方法在开发效率和运行效率上都还有他的问题:
  • 开发效率方面,这个方案并不支持UE4的Foliage Mode Editor:

    • 每个植被区域都被Bake成BP的形式,场景美术规划阶段就需要格外小心防止区域之间穿插造成植被之间的叠加
    • 当出现比较大的改动需求时,一个BP的范围发生改动就会造成大量BP重新生成的连锁反映。
    • 就像地形生成一样,完全的自动化并不现实,美术需要能通过UE4传统手绘方式来进行修改植被的方式。
    • 一个区域的BP植被需要重新生成时,还要重新绘制一遍之前的生成区域,这个过程除非预先保存,否则很难完全重现上次绘制的区域。

  • 运行效率优化方面,同一类的Instance并不能放到一个InstancedStaticMeshComponent 这样必定会造成一定程度的性能损耗

正因为如此,还有必要Houdini的植被管线与UE4的Foliage Mode编辑的植被系统串联起来。这样Hoduini生成后的内容,美术可以很方便的修改,也可以用Houdini来做二次修正,最终生成的植被也可以使用UE4的植被系统的优化方案。而UE4的Foliage System,其实就是每个Level里有一个AInstancedFoliageActor,每种Foliage Type对应的Instanc Mesh实例都保存在AInstancedFoliageActor的FFoliageMeshInfo里。
512e800bf728dc03ddfb89e56dd599d5_v2-e5df6202ec8453702afbe3fb4ac7e721_r.jpg
如果要把Houdini过程化生成与Foliage Mode衔接起来,那么就需要Houdini Engine Input和Output部分可以支持UE4的Foliage System。也就是每个Level的AInstancedFoliageActor里,概括来说就是:
  • Houdini Input要增加FoliageType的选项,生成实例的对象不再是用Statice Mesh,而是UE4的Foliage Type
  • Houdini Output直接输出植被实例不再Bake到BP里,而是直接Add到UE4的Foliage System的Foliage Instance里

接下来就讲解下如何通过只修改Houdini Engine,不需要触碰UE4引擎源码,来把Houdini植被管线与UE4的植被系统整合到一起的方法。
Houdini Input对FoliageType的选项支持
上篇中也提到过,原生的Houdini Engine的过程化实例放置功能,并没有把植被做特殊的Input处理,而是作为Geometry来对待。首要任务就是在Houdini Engine Input里可以支持Foliage Type。
先进入到HoudiniAssetInput.h里,在EHoudiniAssetInputType的Enum里增加FoliageTypeInput。
  1. <pre style="padding: 0.88889em; word-break: normal; word-wrap: normal; overflow: auto; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-radius: 4px;"><span style="border-radius: 0px;">namespace EHoudiniAssetInputType
  2. {
  3.     enum Enum
  4.     {
  5.         GeometryInput = 0,
  6.         AssetInput,
  7.         CurveInput,
  8.         LandscapeInput,
  9.                 FoliageTypeInput, // Add foliage type input
  10.         WorldInput
  11.     };
  12. }</span></pre>
点击此处复制文本

然后,在UHoudiniAssetInput类的CreateWidgetResources(),ChangeInputType(),CreateWidgetResources(),UploadParameterValue()的函数里,参考EHoudiniAssetInputType中其他的InputType的处理方式,加入对FoliageTypeInput的处理,此外,还要在FHoudiniParameterDetails类的CreateWidgetInput,加入针对FoliageTypeInput的菜单UI,这里可以参考
InParam.ChoiceIndex == EHoudiniAssetInputType::GeometryInput
部分的代码给FoliageTypeInput实现一遍,但要自己实现一下Helper_CreateFoliageWidget
  1. <pre style="padding: 0.88889em; word-break: normal; word-wrap: normal; overflow: auto; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-radius: 4px;"><span style="border-radius: 0px;">for ( int32 Ix = 0; Ix < NumInputs; Ix++ )
  2. {
  3.     UObject* InputObject = InParam.GetInputObject( Ix );
  4.     //Helper_CreateGeometryWidget( InParam, Ix, InputObject, AssetThumbnailPool, VerticalBox );
  5.     Helper_CreateFoliageWidget(InParam, Ix, InputObject, AssetThumbnailPool, VerticalBox);
  6. }
  7. </span></pre>
点击此处复制文本

Helper_CreateFoliageWidget和Helper_CreateGeometryWidget的区别就在与UI里对UObject子类的筛选,把UStaticMesh替换成UFoliageType
  1. <pre style="padding: 0.88889em; word-break: normal; word-wrap: normal; overflow: auto; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-radius: 4px;"><span style="border-radius: 0px;">SNew( SAssetDropTarget )
  2.         .OnIsAssetAcceptableForDrop( SAssetDropTarget::FIsAssetAcceptableForDrop::CreateLambda(
  3.                 []( const UObject* InObject ) {
  4.                     return InObject && InObject->IsA< /*UStaticMesh*/ UFoliageType >();
  5. </span></pre>
点击此处复制文本

这样,HDA里就增加了FoliageTypeInput的选项,并添加到Input里了。这里不得不说Unity写工具界面比UE4的效率高太多了。
虽然通过修改Houdini Engine,把FoliageType的Input读入了,但是Houdini Engine的管线的实例化部分,还是只能对Geometry来进行处理,
这里就需要在FHoudiniEngineUtils::HapiCreateInputNodeForObjects函数里,获取FoliageType对应的Static Mesh再输出给Houdini Input Node了。
  1. <pre style="padding: 0.88889em; word-break: normal; word-wrap: normal; overflow: auto; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-radius: 4px;"><span style="border-radius: 0px;">if (UFoliageType_InstancedStaticMesh * InputFoliageType = Cast<UFoliageType_InstancedStaticMesh>(InputObjects[InputIdx]))
  2. {
  3.     UStaticMesh* InputStaticMesh = InputFoliageType->GetStaticMesh();
  4.     // Creating an Input Node for Static Mesh Data
  5.     if (!HapiCreateInputNodeForStaticMesh(InputStaticMesh, MeshAssetNodeId, OutCreatedNodeIds, nullptr, bExportAllLODs, bExportSockets))
  6.     {
  7.         HOUDINI_LOG_WARNING(TEXT("Error creating input index %d on %d"), InputIdx, ConnectedAssetId);
  8.     }
  9.     SelectInputFoliageTypeArray.Add(InputFoliageType);
  10. }
  11. </span></pre>
点击此处复制文本

这样改造Houdini Engine后,输入FoliageType以及Landscape的Draw SelectRegion,就可以和上篇一样输出植被了。
Houdini Input支持Foliage Type后,接下来要实现的就是Houdini Output到Foliage System的功能了。
Houdini Output与Foliage Editor的关联
Output与FoliageEditor关联方面最基础的需求有以下几点。
  • Houdini输出的Entity Point Cloud所对应的Instance可以直接Add到UE4的Foliage System里。
  • 美术可以通过绘制区域来对已经生成部分再次做过程化生成,或者直接利用FoliageEdit手绘的方式来进行迭代调整。
  • 手绘调整部分和Houdini自动化生成部分可以分Layer保存,可以根据情况选择自动生成部分是否影响到手工调整部分。

FC5里也没有提及第三项的实现方式,所以基础管线部分主要讲解前两项的实现方法,而第三条在后续文章里会参考GDC2017上GHOST RECON的地形工具的方法来实现。
这里先定位到Houdini Engine生成Output到UE4的类函数UHoudiniAssetComponent::CreateObjectGeoPartResources里
  1. <pre style="padding: 0.88889em; word-break: normal; word-wrap: normal; overflow: auto; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-radius: 4px;"><span style="border-radius: 0px;">#if WITH_EDITOR
  2.     if ( FHoudiniEngineUtils::IsHoudiniNodeValid( AssetId ) )
  3.     {
  4.         // Create necessary instance inputs.
  5.         CreateInstanceInputs( FoundInstancers );

  6.         // Create necessary curves.
  7.         CreateCurves( FoundCurves );

  8.         // Create necessary landscapes
  9.         CreateAllLandscapes( FoundVolumes );
  10.     }
  11. #endif</span></pre>
点击此处复制文本

其中CreateInstanceInputs函数功能会迭代关卡里的每一种Instancer,再通过UHoudiniAssetInstanceInput::CreateInstanceInput(),
根据这个Instancer对应的Cloud Point,在UHoudiniAssetComponent::CreateInstanceInputs创建InstancedStaticMesh。
  1. <pre style="padding: 0.88889em; word-break: normal; word-wrap: normal; overflow: auto; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-radius: 4px;"><span style="border-radius: 0px;">for ( const FHoudiniGeoPartObject& GeoPart : Instancers )
  2. {   
  3.      HoudiniAssetInstanceInput->CreateInstanceInput();
  4. }</span></pre>
点击此处复制文本

在UHoudiniAssetInstanceInput::CreateInstanceInput()里,参考FEdModeFoliage::AddInstancesImp的方法,
在当前Level的AInstancedFoliageActor中对应FoliageType的FFoliageMeshInfo里,根据植被在Entity Point Cloud的Tranform信息,来添加Instance。
  1. <pre style="padding: 0.88889em; word-break: normal; word-wrap: normal; overflow: auto; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-radius: 4px;"><span style="border-radius: 0px;">UWorld* World = GEditor->GetEditorWorldContext().World();
  2. ULevel* TargetLevel = World->GetCurrentLevel();

  3. AInstancedFoliageActor* IFA = AInstancedFoliageActor::GetInstancedFoliageActorForLevel(TargetLevel, true);
  4. FFoliageMeshInfo* MeshInfo;
  5. UFoliageType* FoliageSettings = IFA->AddFoliageType(FoliageType, &MeshInfo);

  6. GLevelEditorModeTools().ActivateMode(FBuiltinEditorModes::EM_Foliage);
  7. FEdModeFoliage* FoliageEditMode = (FEdModeFoliage*)GLevelEditorModeTools().GetActiveMode(FBuiltinEditorModes::EM_Foliage);

  8. for (int32 InstanceIdx = 0; InstanceIdx < InstancerPartTransforms.Num(); ++InstanceIdx)
  9. {
  10.     FTransform InstanceTransform;

  11.     FHoudiniEngineUtils::TranslateHapiTransform(InstancerPartTransforms[InstanceIdx], InstanceTransform);
  12.     FFoliageInstance Inst;
  13.     Inst.Location = InstanceTransform.GetLocation();
  14.     Inst.Rotation = InstanceTransform.GetRotation().Rotator();
  15.     MeshInfo->AddInstance(IFA, FoliageSettings, Inst, nullptr, true);
  16. }</span><code class="language-text" style="color: rgb(26, 26, 26); font-size: inherit; background-color: inherit; border-radius: 0px; font-family: Menlo, Monaco, Consolas, &quot;Andale Mono&quot;, &quot;lucida console&quot;, &quot;Courier New&quot;, monospace;">                </code></pre>
点击此处复制文本

              
如下图所示,过程化植被也加入到了Level的AInstancedFoliageActor里。这样生成后也可以使用Foliage Editor来做二次修改。
当场景美术需要做二次修改时,那么首先需要把修改区域的植被先清除掉,再使用Houdini对这块绘制区域重新过程化生成植被。
这就需要Houdini Engine可以支持移除掉绘制区域植被的功能。这个可以参考void FEdModeFoliage::ReapplyInstancesForBrush的方法。
使用MeshInfo->InstanceHash->GetInstancesOverlappingBox来获取美术绘制范围的Instance,
在利用MeshInfo->InstanceHash->RemoveInstance把范围内对应的Foliage Type移除,再使用HDA来重新生成。在上一篇中我们也讲到Select Tool其实绘制的是地表的Mask,
也就是对应地形Tile的X,Y值,所以这里还需要把Landscape的X,Y值转成世界空间的Box,来做判断,这部分的实现代码如下:
  1. <pre style="padding: 0.88889em; word-break: normal; word-wrap: normal; overflow: auto; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-radius: 4px;"><span style="border-radius: 0px;">ULandscapeComponent* SelectLandscapeComponent = FHoudiniLandscapeUtils::SelectLandscapeComponentArray[Index];
  2. int32 MinX = MAX_int32;
  3. int32 MinY = MAX_int32;
  4. int32 MaxX = -MAX_int32;
  5. SelectLandscapeComponent->GetComponentExtent(MinX, MinY, MaxX, MaxY);

  6. ULandscapeInfo* LandscapeInfo = SelectLandscapeComponent->GetLandscapeProxy()->GetLandscapeInfo();
  7. for (int32 X = MinX; X <= MaxX; X++)
  8. {
  9.     for (int32 Y = MinY; Y <= MaxY; Y++)
  10.     {
  11.         float RegionSelect = LandscapeInfo->SelectedRegion.FindRef(FIntPoint(X, Y));
  12.         if (RegionSelect > 0)
  13.         {
  14.             SelectRegionNum++;
  15.             FBoxSphereBounds ComponentBounds =
  16.             SelectLandscapeComponent->CalcBounds(SelectLandscapeComponent->GetComponentTransform());

  17.             FBox CachedLocalBox;
  18.             CachedLocalBox.Min = FVector(X, Y, 0);
  19.             CachedLocalBox.Max = FVector(X+1, Y+1, 0);
  20.             CachedLocalBox.IsValid = 1;
  21.             FBox MyBounds = CachedLocalBox.TransformBy(SelectLandscapeComponent->GetLandscapeProxy()->
  22.             GetLandscapeActor()->GetActorTransform());
  23.             MyBounds.Max.Z = ComponentBounds.GetBox().Max.Z ;
  24.             MyBounds.Min.Z = ComponentBounds.GetBox().Min.Z ;
  25.             FBoxSphereBounds RegionBounds  = FBoxSphereBounds(MyBounds);
  26.             auto TempInstances = MeshInfo->InstanceHash->GetInstancesOverlappingBox(RegionBounds.GetBox());
  27.             for (int32 Idx : TempInstances)
  28.             {
  29.                 if(InInstancesToRemove.Find(Idx) == INDEX_NONE)
  30.                     InInstancesToRemove.Add(Idx);
  31.                         }
  32.                 }
  33.         }
  34. }
  35. MeshInfo->RemoveInstances(IFA, InInstancesToRemove, true);</span></pre>
点击此处复制文本

迭代SelectRegion的每一个绘制点,把这个绘制点根据地形世界变化转换为对应的Box,再判断Box里是否有Instance,再进行移除操作。
如果是基于Landscape Component做再生成就简单很多了,获取这个Component的Box,移除掉Box范围内的植被实例。
分步的看一下修改改后的效果。首先Houdini Engine会把绘制区域的植被全部清除掉。
然后再根据HDA里的Scatter算法,来摆放Instance并加入到关卡的AInstancedFoliageActor里。

之前的代码示例只是移除其中一种FoliageType,如果需要删除掉绘制区域的所有Foliage Type的话,只需要迭代每个FoliageType对应的FFoliageMeshInfo,
再进行删除Instance操作即可。
  1. <pre style="padding: 0.88889em; word-break: normal; word-wrap: normal; overflow: auto; background-image: initial; background-position: initial; background-size: initial; background-repeat: initial; background-attachment: initial; background-origin: initial; background-clip: initial; border-radius: 4px;"><span style="border-radius: 0px;"> TMap<UFoliageType*, FFoliageMeshInfo*> InstancesFoliageType = IFA->GetAllInstancesFoliageType();

  2. for (auto& MeshPair : InstancesFoliageType)
  3. {
  4.     FFoliageMeshInfo* MeshInfo = MeshPair.Value;
  5.     UFoliageType* FoliageSettings = MeshPair.Key;
  6. }</span></pre>
点击此处复制文本

就此,一个最基本的Houdini驱动的UE4植被系统的FoliageType部分就完成了。而一些具体的细节改动和工具开发,会放在内容制作部分再做讲解。
GrassType的对应
上一节也讲到,UE4的植被系统除了Foliage Type外,还有Grass Type,Grass Type除了没有碰撞外,保存和生成方式也不一样,Foliage Type的植被是制作阶段就保存
在AInstancedFoliageActor里,在游戏运行时跟随Level加载后作为Instance来渲染。而Grass Type虽然也是跟Foliage Type一样,使用的
HierarchicalInstancedStaticMeshComponents来进行渲染,区别在于它是在游戏运行时根据相机的视角来生成的。
而GrassType的分布信息,则是根据UE4的地形材质系统来输出的。
就如下图所示,默认的哪种GrassType被布置地面的哪个位置,是通过采样Landsacpe Layer的信息来确定的。
但这种方式就导致了Grass和Layer之前的强制绑定关系。
在一些特殊需求上,比如在一些特定的地标区域生成某种特定的草,或者排除掉某些特定草的需求,使用默认的方案都很难解决。
一种折衷的方案,是像下图这样,自己输入一张全场景的植被布局Mask图,来作为GrassType的采样信息使用,
但这受限于植被的种类,地图大小,很难保障精确,只能作为临时的方案。
还有一种方法就是直接改引擎源码,void ALandscapeProxy::UpdateGrass(const TArray<FVector>& Cameras, bool bForceSync)的部分。
这个就不是光修改Houdini Engine引擎就能解决的了,文章篇幅关系也只能放到后文单独挑出一章节来做讲解。
总结
至此,地形和植被相关的整个管线部分已经基本上打通,但和国外AAA级产品的过程化工具比,还是有很大的差距。
管线上的主要的差距还是在工具易用性和完善程度上,比如幽灵行动:荒野里,通过把过程化生成的地形,道路,铁路,以及手工修改的部分做分层保存,
这样当一层做修改或恢复时,才不会影响到其他的层的修改。
后续的文章中,会逐步的深入到具体的场景地形和植被制作上,届时也会涉及到更多Houdini Enigne管线修改的细节上。



评分

参与人数 1活跃度 +8 展开 理由
诺一 + 8 【点赞】这很有大网气质!

查看全部评分

还没有设置签名!您可以在此展示你的链接,或者个人主页!
使用道具 <
qq_花小书_bdt  发表于 2019-4-5 20:09:38  
2#
每天一遍微元素
回复 收起回复
使用道具
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表