URP/HDRP/Built-in 管线的选择与优化
·
管线特性对比
Unity 提供了三种主要渲染管线:
- Built-in 管线:Unity 传统的默认管线,功能全面但缺乏现代图形 API 的优化,适合初学者和中小项目。
- URP(通用渲染管线):轻量级且高度可定制,针对移动平台优化,支持 Shader Graph,适合跨平台项目,包括鸿蒙设备。
- HDRP(高清渲染管线):提供电影级画质,支持实时光线追踪和高级后处理,适合高端 PC 和主机项目。
在鸿蒙 5 及以上系统中,考虑到设备性能差异较大,推荐使用 URP 作为默认选择,既能保证良好的视觉效果,又能在中低端设备上保持流畅运行。
鸿蒙设备优化策略
针对鸿蒙设备的特性,优化 URP 时需注意:
- 降低渲染分辨率:通过
Camera组件的Render Texture或 URP 的Renderer Features降低分辨率,提升性能。 - 简化后处理效果:避免使用复杂的全屏后处理,如 Bloom、Depth of Field 等,可使用简化版或选择性开启。
- 使用 GPU Instancing:对于大量重复物体,启用 GPU Instancing 减少 Draw Call。
以下是在 URP 中启用 GPU Instancing 的代码示例csharp
using UnityEngine;
public class EnableGPUInstancing : MonoBehaviour
{
void Start()
{
// 获取所有 Mesh Renderer 组件
MeshRenderer[] renderers = FindObjectsOfType<MeshRenderer>();
foreach (MeshRenderer renderer in renderers)
{
// 启用 GPU Instancing
Material material = renderer.material;
material.enableInstancing = true;
}
}
}
Shader 编写入门 / 进阶
Shader Graph 基础
Shader Graph 是 Unity 的可视化 Shader 创作工具,适合零基础开发者。以下是创建简单 PBR 材质的步骤:
- 在 Project 窗口右键选择
Create > Shader > URP > PBR Shader Graph。 - 打开 Shader Graph 编辑器,连接
Base Color、Metallic、Smoothness等节点。 - 添加
Normal Map节点增强细节。 - 保存并应用到材质。在鸿蒙设备上,使用 Shader Graph 生成的 Shader 会自动优化,确保在移动平台上高效运行。
代码 Shader 进阶
对于更复杂的效果,可编写代码 Shader。以下是一个简单的 URP 表面 Shader 示例:
Shader "Custom/URP_SimpleSurface"
{
Properties
{
_BaseColor ("Base Color", Color) = (1,1,1,1)
_Metallic ("Metallic", Range(0,1)) = 0.5
_Smoothness ("Smoothness", Range(0,1)) = 0.5
}
SubShader
{
Tags { "RenderType"="Opaque" "RenderPipeline" = "UniversalRenderPipeline" }
LOD 100
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 tangent : TANGENT;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float3 normal : NORMAL;
float2 uv : TEXCOORD0;
float4 worldPos : TEXCOORD1;
};
half4 _BaseColor;
half _Metallic;
half _Smoothness;
v2f vert (appdata v)
{
v2f o;
o.vertex = TransformObjectToHClip(v.vertex.xyz);
o.normal = TransformObjectToWorldNormal(v.normal);
o.uv = v.uv;
o.worldPos = mul(unity_ObjectToWorld, v.vertex);
return o;
}
half4 frag (v2f i) : SV_Target
{
// 基础颜色
half4 baseColor = _BaseColor;
// 光照计算
Light mainLight = GetMainLight();
half3 normalDirection = normalize(i.normal);
half3 lightDirection = mainLight.direction;
half ndotl = saturate(dot(normalDirection, lightDirection));
// 漫反射
half3 diffuse = baseColor.rgb * mainLight.color * ndotl;
// PBR 光照模型简化
half3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
half3 halfDirection = normalize(lightDirection + viewDirection);
half ndoth = saturate(dot(normalDirection, halfDirection));
// 金属度与粗糙度
half metallic = _Metallic;
half roughness = 1.0 - _Smoothness;
// 简单的镜面反射
half3 specular = mainLight.color * pow(ndoth, 1.0 / max(0.001, roughness)) * metallic;
// 最终颜色
half3 finalColor = diffuse + specular + mainLight.color * 0.1; // 添加环境光
return half4(finalColor, baseColor.a);
}
ENDHLSL
}
}
}
光照烘焙技巧
基础光照烘焙流程
- 设置场景中的静态物体:在 Inspector 窗口勾选
Static。 - 配置 Light 组件:选择合适的光照模式(Baked、Mixed、Realtime)。
- 打开 Lighting 窗口(Window > Rendering > Lighting)。
- 调整光照设置:
- Baked Global Illumination:启用全局光照。
- Lightmap Resolution:根据场景复杂度调整(通常 10-50)。
- Lightmap Size:选择合适的尺寸(通常 1024-4096)。
- 点击
Generate Lighting开始烘焙。
鸿蒙设备光照优化
在鸿蒙设备上,为平衡画质与性能,可采用以下策略:
- 使用 Enlighten 实时全局光照:对于高端鸿蒙设备,可启用 Enlighten 提供更真实的光照效果。
- 光照贴图压缩:在 Lighting 窗口的 Advanced Settings 中,选择 ASTC 或 ETC2 等移动平台优化的压缩格式。
- 光照探针(Light Probes):合理放置光照探针捕捉动态物体的光照信息,减少烘焙负担。
后处理效果的实现与性能考量
URP 后处理配置
- 在 URP 配置文件中启用后处理:
- 打开
Assets/Rendering/UniversalRenderPipelineAsset。 - 在
Renderer Features列表中添加Volume组件。
- 打开
- 创建后处理体积(Volume):
- 在场景中创建空 GameObject,添加
Volume组件。 - 设置 Volume 的 Profile,添加需要的后处理效果(如 Bloom、Color Adjustments)。
- 在场景中创建空 GameObject,添加
性能优化策略
在鸿蒙设备上实现后处理效果时,需注意:
- 避免全屏后处理:使用
Volume的Is Global选项限制后处理范围。 - 降低后处理质量:调整 Bloom 的
Threshold和Intensity,避免使用高分辨率的后处理效果。 - 选择性启用效果:根据设备性能动态启用或禁用后处理效果。
以下是根据设备性能动态调整后处理效果的代码示例:
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;
public class PostProcessingController : MonoBehaviour
{
[SerializeField] private Volume postProcessingVolume;
[SerializeField] private float lowEndMemoryThreshold = 2048f; // MB
private Bloom bloom;
private DepthOfField depthOfField;
private ChromaticAberration chromaticAberration;
void Start()
{
// 获取后处理效果组件
if (postProcessingVolume.profile.TryGet<Bloom>(out bloom)) bloom.active = true;
if (postProcessingVolume.profile.TryGet<DepthOfField>(out depthOfField)) depthOfField.active = true;
if (postProcessingVolume.profile.TryGet<ChromaticAberration>(out chromaticAberration)) chromaticAberration.active = true;
// 根据设备性能调整后处理
AdjustPostProcessingForDevice();
}
void AdjustPostProcessingForDevice()
{
// 获取设备内存信息(在鸿蒙系统中可通过其他方式获取更准确信息)
float systemMemory = SystemInfo.systemMemorySize;
// 低端设备优化
if (systemMemory < lowEndMemoryThreshold)
{
if (bloom != null) bloom.active = false;
if (depthOfField != null) depthOfField.active = false;
if (chromaticAberration != null) chromaticAberration.active = false;
Debug.Log("Post-processing optimized for low-end device");
}
else
{
// 高端设备可启用更多效果
if (bloom != null) bloom.active = true;
if (depthOfField != null) depthOfField.active = true;
if (chromaticAberration != null) chromaticAberration.active = true;
Debug.Log("Full post-processing enabled for high-end device");
}
}
}
更多推荐


所有评论(0)