Unity Built-in转URP速查表
转载自
https://cuihongzhi1991.github.io/blog/2020/05/27/builtinttourp/
本篇文章转自Teofilo Dutra编写的《From Built-in to URP》,其中有很多在写URP管线Shader时需要用到的函数,作为备忘速查表非常实用,所以记录于此。本文经过精简和翻译,不一定适用于大家,可以点击上方链接跳转至作者原文。本文是基于7.3版本的URP编写的,有些暂时还不支持的内容可能在后续版本更新迭代。
结构
首先要在SubShader的Tags中添加”RenderPipeline” = “UniversalPipeline”,并且使用HLSL的宏代替旧版的CG语言宏。
| Built-in | URP |
|---|---|
| CGPROGRAM / HLSLPROGRAM | HLSLPROGRAM |
| ENDCG / ENDHLSL | ENDHLSL |
| CGINCLUDE / HLSLINCLUDE | HLSLINCLUDE |
Include文件的改动
| Content | Built-in | URP |
|---|---|---|
| Core | Unity.cginc | Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl |
| Light | AutoLight.cginc | Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl |
| Shadows | AutoLight.cginc | Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl |
| Surface shaders | Lighting.cginc | 无 |
其他常用的include文件:
- Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl
- Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl
- Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl
- Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl
- Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl
- Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl
- Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareOpaqueTextue.hlsl
光照模式
| Built-in | URP |
|---|---|
| ForwardBase | UniversalForward |
| ForwardAdd | 无 |
| Deferred and related | UniversalGBuffer seems to have just been added to URP |
| Vertex and related | 无 |
| ShadowCaster | ShadowCaster |
| MotionVectors | 暂不支持 |
URP其他支持的光照模式:
- DepthOnly
- Meta (用于烘焙光照贴图)
- Universal2D
变体(Variants)
URP支持着色器的变体,可以使用#pragma multi_compile宏实现编译不同需求下的着色器,常见的内置关键字有:
- _MAIN_LIGHT_SHADOWS
- _MAIN_LIGHT_SHADOWS_CASCADE
- _ADDITIONAL_LIGHTS_VERTEX
- _ADDITIONAL_LIGHTS
- _ADDITIONAL_LIGHT_SHADOWS
- _SHADOWS_SOFT
- _MIXED_LIGHTING_SUBTRACTIVE
预定义的着色器预处理宏
辅助宏(Helpers)
| Built-in | URP |
|---|---|
| UNITY_PROJ_COORD(a) | 无,使用 a.xy/a.w 来代替 |
| UNITY_INITIALIZE_OUTPUT(type, name) | ZERO_INITIALIZE(type, name) |
阴影贴图
需要包含 Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl
| Built-in | URP |
|---|---|
| UNITY_DECLARE_SHADOWMAP(tex) | TEXTURE2D_SHADOW_PARAM(textureName, samplerName) |
| UNITY_SAMPLE_SHADOW(tex, uv) | SAMPLE_TEXTURE2D_SHADOW(textureName, samplerName, coord3) |
| UNITY_SAMPLE_SHADOW_PROJ(tex, uv) | SAMPLE_TEXTURE2D_SHADOW(textureName, samplerName, coord4.xyz/coord4.w) |
纹理/采样器的声明宏
| Built-in | URP |
|---|---|
| UNITY_DECLARE_TEX2D(name) | TEXTURE2D(textureName); SAMPLER(samplerName); |
| UNITY_DECLARE_TEX2D_NOSAMPLER(name) | TEXTURE2D(textureName); |
| UNITY_DECLARE_TEX2DARRAY(name) | TEXTURE2D_ARRAY(textureName); SAMPLER(samplerName); |
| UNITY_SAMPLE_TEX2D(name, uv) | SAMPLE_TEXTURE2D(textureName, samplerName, coord2) |
| UNITY_SAMPLE_TEX2D_SAMPLER(name, samplername, uv) | SAMPLE_TEXTURE2D(textureName, samplerName, coord2) |
| UNITY_SAMPLE_TEX2DARRAY(name, uv) | SAMPLE_TEXTURE2D_ARRAY(textureName, samplerName, coord2, index) |
| UNITY_SAMPLE_TEX2DARRAY_LOD(name, uv, lod) | SAMPLE_TEXTURE2D_ARRAY_LOD(textureName, samplerName, coord2, index, lod) |
内置的着色器辅助函数
可以在 Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl 看到下方的所有函数
顶点变换函数
| Built-in | URP |
|---|---|
| float4 UnityObjectToClipPos(float3 pos) | float4 TransformObjectToHClip(float3 positionOS) |
| float3 UnityObjectToViewPos(float3 pos) | TransformWorldToView(TransformObjectToWorld(positionOS)) |
泛用的辅助函数
| Built-in | URP | Include |
|---|---|---|
| float3 WorldSpaceViewDir (float4 v) | float3 GetWorldSpaceViewDir(float3 positionWS) | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl” |
| float3 ObjSpaceViewDir (float4 v) | 无,使用 TransformWorldToObject(GetCameraPositionWS()) - objectSpacePosition; | |
| float2 ParallaxOffset (half h, half height, half3 viewDir) | 可能没有,从 UnityCG.cginc 复制 | |
| fixed Luminance (fixed3 c) | real Luminance(real3 linearRgb) | Include “Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl” |
| fixed3 DecodeLightmap (fixed4 color) | real3 DecodeLightmap(real4 encodedIlluminance, real4 decodeInstructions) | Include “Packages/com.unity.render-pipelines.core/ShaderLibrary/EntityLighting.hlsl” URP中decodeInstructions 为 half4(LIGHTMAP_HDR_MULTIPLIER, LIGHTMAP_HDR_EXPONENT, 0.0h, 0.0h) |
| float4 EncodeFloatRGBA (float v) | 可能没有, 从 UnityCG.cginc 复制 | |
| float DecodeFloatRGBA (float4 enc) | 可能没有, 从 UnityCG.cginc 复制 | |
| float2 EncodeFloatRG (float v) | 可能没有, 从 UnityCG.cginc 复制 | |
| float DecodeFloatRG (float2 enc) | 可能没有, 从 UnityCG.cginc 复制 | |
| float2 EncodeViewNormalStereo (float3 n) | 可能没有, 从 UnityCG.cginc 复制 | |
| float3 DecodeViewNormalStereo (float4 enc4) | 可能没有, 从 UnityCG.cginc 复制 |
前向渲染辅助函数
| Built-in | URP | Include |
|---|---|---|
| float3 WorldSpaceLightDir (float4 v) | _MainLightPosition.xyz - TransformObjectToWorld(objectSpacePosition) | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl” |
| float3 ObjSpaceLightDir (float4 v) | TransformWorldToObject(_MainLightPosition.xyz) - objectSpacePosition | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl” |
| float3 Shade4PointLights (…) | 无,可尝试用half3 VertexLighting(float3 positionWS, half3 normalWS) | include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
屏幕空间辅助函数
| Built-in | URP | Include |
|---|---|---|
| float4 ComputeScreenPos (float4 clipPos) | float4 ComputeScreenPos(float4 positionCS) | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl” |
| float4 ComputeGrabScreenPos (float4 clipPos) | 无 |
顶点光照的辅助函数
| Built-in | URP | Include |
|---|---|---|
| float3 ShadeVertexLights (float4 vertex, float3 normal) | 无,可尝试用 UNITY_LIGHTMODEL_AMBIENT.xyz + VertexLighting(…) | include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
可以在 Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl 中找到一些通用函数
内置的着色器变量
| Built-in | URP | Include |
|---|---|---|
| _LightColor0 | _MainLightColor | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl” |
| _WorldSpaceLightPos0 | _MainLightPosition | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl” |
| _LightMatrix0 | 可能还不支持 | |
| unity_4LightPosX0, unity_4LightPosY0, unity_4LightPosZ0 | URP中,额外的灯光存储在一个数组或缓冲中(取决于平台),使用Light GetAdditionalLight(uint i, float3 positionWS)获取光照信息 | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
| unity_4LightAtten0 | URP中,额外的灯光存储在一个数组或缓冲中(取决于平台),使用Light GetAdditionalLight(uint i, float3 positionWS)获取光照信息 | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
| unity_LightColor | URP中,额外的灯光存储在一个数组或缓冲中(取决于平台),使用Light GetAdditionalLight(uint i, float3 positionWS)获取光照信息 | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
| unity_WorldToShadow | float4x4 _MainLightWorldToShadow[MAX_SHADOW_CASCADES + 1] or _AdditionalLightsWorldToShadow[MAX_VISIBLE_LIGHTS] | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl” |
可以使用GetAdditionalLight(…)获取额外的光源,也可以使用GetAdditionalLightsCount()查询额外的光源数量。
其他方法
阴影
更多阴影相关函数可以查看 Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl
| Built-in | URP |
|---|---|
| UNITY_SHADOW_COORDS(x) | 可能没有,可以写作float4 shadowCoord : TEXCOORD0; |
| TRANSFER_SHADOW(a) | a.shadowCoord = TransformWorldToShadowCoord(worldSpacePosition) |
| SHADOWS_SCREEN | 暂不支持 |
雾
更多雾相关的函数可以查看 Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl
| Built-in | URP |
|---|---|
| UNITY_FOG_COORDS(x) | 可能没有,可以写作float fogCoord : TEXCOORD0; |
| UNITY_TRANSFER_FOG(o, outpos) | o.fogCoord = ComputeFogFactor(clipSpacePosition.z); |
| UNITY_APPLY_FOG(coord, col) | color = MixFog(color, i.fogCoord); |
深度
可以包含 “Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl” 并使用 _CameraDepthTexture来调用深度纹理。也可以使用SampleSceneDepth(…) 和 LoadSceneDepth(…)。
| Built-in | URP | Include |
|---|---|---|
| LinearEyeDepth(sceneZ) | LinearEyeDepth(sceneZ, _ZBufferParams) | Include “Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl” |
| Linear01Depth(sceneZ) | Linear01Depth(sceneZ, _ZBufferParams) | Include “Packages/com.unity.render-pipelines.core/ShaderLibrary/Common.hlsl” |
其他
| Built-in | URP | Include |
|---|---|---|
| ShadeSH9(normal) | SampleSH(normal) | Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl” |
| unity_ColorSpaceLuminance | 无,使用Luminance() | Include “Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl” |
后期/特效
URP不支持OnPreCull, OnPreRender, OnPostRender 和 OnRenderImage. 支持 OnRenderObject 和 OnWillRenderObject。RenderPipelineManager提供了渲染管线中注入的位置:
- beginCameraRendering(ScriptableRenderContext context, Camera camera)
- endCameraRendering(ScriptableRenderContext context, Camera camera)
- beginFrameRendering(ScriptableRenderContext context,Camera[] cameras)
- endFrameRendering(ScriptableRenderContext context,Camera[] cameras)
例如:
1 |
void OnEnable() |
另外,可以创建ScriptableRendererFeature来实现后期处理效果。可以在管线的不同阶段注入ScriptableRenderPasses:
- BeforeRendering
- BeforeRenderingShadows
- AfterRenderingShadows
- BeforeRenderingPrepasses
- AfterRenderingPrePasses
- BeforeRenderingOpaques
- AfterRenderingOpaques
- BeforeRenderingSkybox
- AfterRenderingSkybox
- BeforeRenderingTransparents
- AfterRenderingTransparents
- BeforeRenderingPostProcessing
- AfterRenderingPostProcessing
- AfterRendering
下面是一个示例:
1 |
public class CustomRenderPassFeature : ScriptableRendererFeature |
Unity Built-in转URP速查表的更多相关文章
- numpy, pandas, scikit-learn cheat sheet (速查表)
1. scikit-learn cheat sheet 官方链接如下:http://scikit-learn.org/stable/tutorial/machine_learning_map/ Oft ...
- 简明 Git 命令速查表(中文版)
原文引用地址:https://github.com/flyhigher139/Git-Cheat-Sheet/blob/master/Git%20Cheat%20Sheet-Zh.md在Github上 ...
- .htaccess下Flags速查表
Flags是可选参数,当有多个标志同时出现时,彼此间以逗号分隔. 速查表: RewirteRule 标记 含义 描述 R Redirect 发出一个HTTP重定向 F Forbidden 禁止对URL ...
- Markdown 语法速查表
Markdown 语法速查表 1 标题与文字格式 标题 # 这是 H1 <一级标题> ## 这是 H2 <二级标题> ###### 这是 H6 <六级标题> 文 ...
- jQuery API 3.1.0 速查表-打印版
jQuery API 3.1.0 速查表-打印图,(API来自:http://jquery.cuishifeng.cn/index.html)
- java-Mysql-SQLServer数据类型匹配速查表
java-Mysql-SQLServer数据类型匹配速查表 Mysql ************************************ 当前列 ClassName ColumnType Di ...
- python 下的数据结构与算法---2:大O符号与常用算法和数据结构的复杂度速查表
目录: 一:大O记法 二:各函数高阶比较 三:常用算法和数据结构的复杂度速查表 四:常见的logn是怎么来的 一:大O记法 算法复杂度记法有很多种,其中最常用的就是Big O notation(大O记 ...
- HTML5速查表
HTML5速查表 标签 描述 版本 属性 <!--...--> 定义注释 4 / 5 none <!DOCTYPE> 定义文档类型 4 / 5 none <a> 定 ...
- [翻译]Django速查表
原文在此:https://code.djangoproject.com/wiki/DjangoCheatSheet Django速查表Django教程已经非常好了.这个速查表的作用是创建一个快速开始指 ...
- Java, C#, Swift语法对比速查表
原文:Java, C#, Swift语法对比速查表 Java 8 C# 6 Swift 变量 类型 变量名; 类型 变量名; var 变量名 : 类型; 变量(类型推断) N/A var 变量名= ...
随机推荐
- 【记录】JS和C++的Base64编码解码(支持中文)
JS 解决方法来源于知乎新码笔记的文章 function b64Encode(str) { return btoa(unescape(encodeURIComponent(str))); } func ...
- js技术之“input输入框转成下拉框,且保留可输入能力”
一.jsp前端控件 <td class="form-label"> <label class="control-label">供货商货号 ...
- openEuler 20.03 LTS安装单病种前置机
# 下载配置文件包 cd /opt wget https://interface-soft.oss-cn-hangzhou.aliyuncs.com/manual-package/config.tar ...
- 异步之舞:FastAPI与MongoDB的深度协奏
title: 异步之舞:FastAPI与MongoDB的深度协奏 date: 2025/05/18 19:09:08 updated: 2025/05/18 19:09:08 author: cmdr ...
- 极简版秒表(java GUI)
package javaPractice; import javax.swing.*; import java.text.*; import java.util.*; import java.awt. ...
- JAVA的那些数据结构实现总结,实现,扩容说明
能沉淀下来的东西,往往都很基础,整理了下JAVA中遇到的数据结构 目录大纲: 到目前接触到的 有几个说明: 可扩容数组 ArrayList 扩容数组的实现, 满了后扩容,扩容在1.5倍,通过copy过 ...
- Postgresql12主备流复制操作过程以及原理【pg_basebackup】
原理介绍:流复制(Streaming Replication).流复制提供了将 WAL 记录连续发送并应用到从服务器以使其保持最新状态的功能.通过流复制,从服务器不断从主服务器同步相应的数据, 同时, ...
- Spring 注解之@RequestBody和@PostMapping
@RequestBody的使用 注解@RequestBody用于接收前端传递给后端的.JSON对象的字符串,这些数据位于请求体中,适合处理的数据为非Content-Type: applicatio ...
- IDEA 调试Java代码的两个技巧
本文介绍两个使用IDEA 调试Java代码的两个技巧: 修改变量值 使用RuntimeException终止代码执行 修改变量值 在Java代码调试过程中,我们可以修改变量值,使其达到走指定分 ...
- Golang相关环境变量
GOROOT: GO语言的安装路径,linux系统下一般是/usr/local/go GOPATH: 程序员自己的go源码路径,比如开发一个Helloworld的项目,那么它的代码文件夹就应该放在GO ...