程序化内容生成框架(PCG) 是一个用于在虚幻引擎中创建你自己的程序化内容及工具的工具集。借助PCG,技术美术师、设计师和程序员能够构建任意复杂度的快速迭代式工具和内容,从资产工具(如建筑物或群系生成等)到整个世界,不一而足。
重要概念和术语
- 点(Points) :3D空间中的坐标点,由PCG图表生成,常用于生成网格体。点包含变换、边界、颜色、密度、陡度和种子等信息。可以为它们分配由用户自定义的属性值。
- Point Density: Value used by a variety of graph nodes. This is represented in the debug view as a gradient on each point and represents the probability of the point to exist at that position. A density of 0 is black, and a density of 1 is white.
必需设置
程序化内容生成框架需要在你的项目中启用 程序化内容生成框架(Procedural Content Generation Framework) 插件。如需详细了解如何启用插件,请参阅使用插件。
需要 程序化内容生成框架几何体脚本交互(Procedural Content Generation Framework Geometry Script Interop) 插件,才能在静态网格体上对点取样。
程序化节点图表
程序化节点图表是程序化内容生成框架的核心部分。
类似材质编辑器,关卡中PCG组件的空间数据会传入图表,然后用于生成点。然后,点会经过一系列节点的筛选和修改,并输出实时更新的结果。生成的点可用于生成各种资产。
创建PCG图表资产
要创建PCG图表资产,请执行下面的步骤:
- 右键点击 内容侧滑菜单(Content Drawer) 或 内容浏览器(Content Browser) ,找到 创建高级资产(Create Advanced Asset)> PCG ,并选择 PCG图表(PCG Graph) 。
- 选择新资产的名称,然后按 Enter 键。
编辑PCG图表
在PCG图表编辑器中,你可以配置并编辑PCG图表资产。该编辑器的操作方式与蓝图或材质编辑器相似。它还包含一些只有PCG才有的工具和面板。
数字 | 说明 |
---|---|
1 | Toolbar |
2 | Node Palette |
3 | 视口 |
4 | 细节面板 |
5 | Debug Tree |
6 | Attributes list |
你可以像使用蓝图那样将节点添加到图表中,方法是从节点控制板将其拖入视口中,或通过右键菜单添加。
当PCG图表被指定到PCG组件并且已用于生成内容时,对该图表所做的更改会在编辑器视口中实时更新。
PCG节点
PCG图表由一系列PCG节点构成,每个节点执行对最终结果有贡献的操作。
这些节点划分为以下类别:
类别 | 说明 |
---|---|
蓝图(Blueprint) | 包含与蓝图相关的节点。这包括用于执行从 PCGBlueprintElement 派生的用户蓝图的通用节点。 |
Control Flow | Contains nodes for controlling the flow of logic through the graph. |
调试(Debug) | 包含帮助调试的节点。 |
密度(Density) | 包含影响点密度的节点。 |
筛选器(Filter) | 包含基于条件或按点筛选数据的节点。 |
Generic | Contains nodes that affect data, but not spatial data. |
Hierarchical Generation | Contains nodes for controlling the Hierarchical Generation mode. |
Input Output | Contains nodes for loading Alembic and other external data. |
IO | 包含能控制与外部数据交互的节点。 |
元数据(Metadata) | 包含与属性交互的节点,无论是点上还是属性集上的属性。 |
参数(Param) | 包含能控制如何从Actor或蓝图变量检索参数的节点。 |
Point Ops | Contains nodes that affect points and their properties. |
取样器(Sampler) | 包含从空间数据源(例如体积、表面和网格体)生成点的节点。 |
空间(Spatial) | 包含能在数据之间创建空间关系、更改其内部空间数据或检索数据的节点。 |
生成器(Spawner) | 包含在给定点位置创建新数据或放置Actor的节点。 |
子图表(Subgraph) | 包含能处理子图表用法的节点。 |
你可以像使用蓝图那样添加 注释(Comments) 和 重新路由节点(Reroute Nodes) ,使图表更易于辨识。
Attributes and Metadata
Attributes are similar to variables and store data defined by its name and type. There are two types of Attributes:
- Static Attributes: Attributes that are fixed and always present. These start with a
$
, such as$Position
. - Dynamic Attributes: Attributes that are created at runtime, and stored as part of the Metadata of the graph data.
Attribute Selector
Some PCG graph nodes can be used to provide interoperability between static attributes and dynamic attributes through the Attribute Selector.
The Attribute Selector provides you with a list of attributes that are available to use with the selected node. The Attribute Selector uses the following naming rules:
- Names starting with a $ are static attributes, while names without are dynamic attributes.
@Last
represents the last dynamic attribute that was manipulated by the previous node.
For example, the Math node is used to perform mathematical operations on static and dynamic attributes:
The Attribute Selector name field can also be used to extract data from components:
In the image above, $Position.ZYX provides the inverse of the components of $Position. Below are the components that can be manipulated in this way and their types:
Component | Type |
---|---|
Vectors | |
X, Y, Z, W, x, y, z, w | Double. Can’t be mixed with RGBA. |
R, G, B, A, r, g, b, a | Double. Can’t be mixed with XYZW. |
Length, Size | Double. Returns vector length. |
Transforms | |
Location, Position | Vector3 |
Scale, Scale3D | Vector3 |
Rotation | Quaternion |
Rotators | |
Pitch, Yaw, Roll | Double |
Forward, Right, Up | Vector3 |
Quaternions | |
Support Vector extractor | Vector |
Support Rotator extractor | Rotator |
C++ Settings Overrides
Some settings are marked as PCG_Overridable in the C++ property metadata. For Blueprint nodes, variables that are visible and instance editable are overridable.
Once overridden, pins are automatically added to the node as advanced pins. There are two types of pins:
- Global override: Accepts any number of attributes and overrides all settings for each attribute that is an exact match with the setting name.
- Single override: Accepts any number of attributes and overrides the specific setting if an attribute is found with an exact match with the setting name, or any name if there is only one attribute.
Attribute types need to match but some types can be converted.
To know the exact name or type, use the tooltip on the override pin:
Graph Parameters
Similar to parameters in the Material editor, PCG Graph Parameters are user created overridable values and can help create customizable graphs for a variety of situations. To create a new parameter:
- Open the PCG Graph Settings.
- Click the + button next to Parameters. This creates a new parameter.
- Click the down arrow next to the new parameter. Rename it and select the type.
To change the value of a parameter on a PCG Graph:
Change the value in the Graph Parameters or in the Details panel of a PCG asset.
To change the value of a parameter on a PCG Graph Instance:
Open the asset in the Content Browser and change the value or in the Details panel of a PCG asset.
Graph Instances
PCG Graph Instances work similar to Material Instances and utilize Graph Parameters to help you reuse your existing graphs as instances or PCG Subgraphs:
To create a PCG Graph instance:
- Select a PCG asset in your level.
- Select the PCG component in the Details panel
- Click the Save Instance button to create a new instance.
- Name your new graph instance and press Enter.
When using an instance as a PCG Subgraph, parameters can be overridden using the override pins on the subgraph node.
PCG组件
程序化节点图表可以通过PCG组件对你的关卡取样。此组件可保存程序化节点图表的实例,并在编辑器中以及在运行时管理程序化内容的生成。 PCG组件添加为Actor的组件,或用作PCG体积的一部分,这是一种基本体积,适合用于快速设置程序化内容。
要将PCG图表连接到PCG组件,请执行下面的步骤:
- 在编辑器视口或 大纲视图(Outliner) 中,选择你想连接的 PCG体积(PCG Volume) 或 蓝图类(Blueprint Class) 。
-
在 细节(Details) 面板中,点击 PCG组件(PCG Component) 。
-
点击 图表(Graph) 下拉菜单,并选择你想使用的PCG节点图表。
-
点击 生成(Generate) 按钮查看你的结果。
在PCG中调试
调试是PCG工作流程中的基本部分。
每个节点都有各种调试选项,可用于直观地显示PCG图表每个步骤中的点数据:
- 调试渲染
- 启用/禁用节点
- 检查
在节点的 细节(Details) 面板中选中 调试(Debug) 的复选框或按 D 键,可切换每个节点的调试渲染。
在节点的 细节(Details) 面板中选中 启用(Enabled) 的复选框或按 E 键,可打开和关闭每个节点。
你还可以检查节点,以此在 属性(Attributes) 列表中显示节点生成的所有点。
- Select your PCG component from the Debug Tree.
- Right-click the node you want to inspect.
- Select Inspect. Alternatively, you can press the A key.
创建简单森林体积
程序化生成工具的常见用例是开放世界环境中的群系生成。
要创建基本森林群系生成器,请执行下面的步骤。
此示例使用的材质和静态网格体来自使用商城下载的Megascans树木资产包:欧洲鹅耳枥集合。
创建关卡
创建PCG体积
- 返回 选择(Selection) 模式并启用 放置Actor(Place Actors) 窗口(如果它目前不可见)。
-
使用 搜索类(Search Classes) 框查找 PCG体积(PCG Volume) 并添加一个到你的关卡。
- 将PCG体积缩放为X=8.0, Y=8.0, Z=8.0
创建PCG图表资产
- 右键点击 内容侧滑菜单(Content Drawer) 或 内容浏览器(Content Browser) ,找到 创建高级资产(Create Advanced Asset)> PCG ,并选择 PCG图表(PCG Graph) 。
- 将新资产命名为 PCG_ForestGen 并按 Enter 键。
- 双击 PCG_ForestGen 打开PCG图表编辑器。
连接PCG组件
- 在编辑器视口或 大纲视图(Outliner) 中,选择 PCG体积(PCG Volume) 。
- 在 细节(Details) 面板中,点击 PCG组件(PCG Component) 。
-
点击 图表(Graph) 下拉菜单并从列表选择 PCG_ForestGen 。
创建点
- 在PCG图表编辑器窗口中,将 Surface Sampler 节点添加到图表。
-
点击 Input 节点上的向下箭头将其展开,然后将 地形(Landscape) 输出连接到Surface Sampler上的 表面(Surface) 输入。
- 选择Surface Sampler并按 D 键切换调试渲染。
- 返回编辑器窗口,选择PCG体积,然后点击细节面板中的 生成(Generate) 按钮。
现在你可以在编辑器视口中看到正在生成的点。这些点符合地形的形状。
添加变化
- 在PCG图表编辑器中,选择Surface Sampler。
- 在细节面板中调整 每平方米点数(Points Per Square Meter) 、 点范围(Points Extents) 和 松散度(Looseness) 属性,添加更多点。
- 将 每平方米点数(Points Per Square Meter) 调整为 0.15 ,将更多点添加到空间。
- 点范围(Points Extents) 属性将控制每个点的边界大小。将 X 、 Y 和 Z 值更改为 0.5 。
- 松散度属性将确定生成的点贴近网格形状的程度。将 松散度(Looseness) 值保留为 1.0 。
-
接下来,添加 Transform Points 节点。此节点将向你的点添加定义范围内的额外移动、旋转和缩放变化。将Surface Sampler节点的 输出(Output) 引脚连接到Transform Points节点的 输入(Input) 引脚。
- 在Surface Sampler节点上禁用调试渲染,并在Transform Points节点上启用它。
-
要添加一些旋转变化,请将 最大旋转(Max Rotation) 的 Z 值更改为 360 。这样所有点都会获得0到360度之间的随机旋转。
- PCG图表会生成点并将其旋转以符合地形的法线方向。选中 绝对旋转(Absolute Rotation) 的复选框可禁用此额外旋转。
-
要添加一些大小变换,请将 X 、 Y 和 Z 缩放最小值(Scale Min) 设置为 0.5 。将 X 、 Y 和 Z 缩放最大值(Scale Max) 更改为 1.2 。
最终结果是有不少变化的一组点。
生成静态网格体
-
在PCG图表编辑器中,将 Static Mesh Spawner 节点添加到图表视口。将Transform Points节点的 输出(Output) 引脚连接到Static Mesh Spawner的 输入(Input) 引脚。
- 选择Static Mesh Spawner。
- 在 细节(Details) 面板中,找到 网格体条目(Mesh Entries) 选项并点击 + 按钮以添加要生成的静态网格体。
- 点击 网格体条目(Mesh Entries) 旁边的下拉箭头,打开数组。
- 点击 Index [0] 旁边的下拉箭头。
- 点击 描述符 旁边的下拉箭头。
-
点击 静态网格体(Static Mesh) 的下拉菜单并选择你想生成的树。此示例使用 SM_EuropeanHornbeam_Forest_01 。
你可以将权重属性用于数组中的每个网格体条目,添加更多静态网格体并平衡多样性。虚幻引擎添加了所有静态网格体条目的权重值,并将该数字除以每个单独的权重以确定每个条目生成的概率。