通用Shader在LWRP/URP下失效

文章详细介绍了如何将Unity的内置Shader迁移到UniversalRenderPipeline(URP),主要涉及LightMode的更改、Tag的添加、HLSL代码的调整以及各种辅助函数和宏的对应转换。此外,还提到了URP对阴影、雾、深度和变体的支持情况。
摘要由CSDN通过智能技术生成

主要就一点:

如果原版Shader下含有:"LightMode" = "ForwardBase"

需要更改为:"LightMode" = "UniversalForward"

再是,需要在Tag里添加"RenderPipeline" = "UniversalPipeline"

Built-in到URP函数对照整理

整体结构

  • 需要在Tag里添加"RenderPipeline" = "UniversalPipeline"
  • 所有的URP Shader代码均包含在HLSLPROGRAM/ENDHLSL 内
Built-inURP
CGPROGRAM
HLSLPROGRAM
HLSLPROGRAM
ENDCG
ENDHLSL
ENDHLSL
CGINCLUDE
HLSLINCLUDE
HLSLINCLUDE

Include文件:

ContentBuilt-inURP
CoreUnity.cgincPackages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl
LightAutoLight.cgincPackages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl
ShadowsAutoLight.cgincPackages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl
Surface shadersLighting.cgincURP内不支持

所有的HLSL代码均在Packages/Universal RP内,可以翻看详细源码

https://github.com/Unity-Technologies/Graphics/tree/master/Packages/com.unity.render-pipelines.universal/ShaderLibrary​github.com/Unity-Technologies/Graphics/tree/master/Packages/com.unity.render-pipelines.universal/ShaderLibrary

Light Modes(照明模式)

Built-inURP
ForwardBaseUniversalForward
ForwardAdd移除
Deferred and related尚未支持
Vertex and related移除
ShadowCasterShadowCaster
MotionVectors尚未支持

还有一些其他的Light Modes:

  • DepthOnly
  • Meta (for lightmap baking)
  • 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

预定义的着色器宏

Built-inURP
UNITY_PROJ_COORD(a)使用a.xy / a.w代替
UNITY_INITIALIZE_OUTPUT(type, name)ZERO_INITIALIZE(type,name)

Shadow Mapping(阴影贴图)

#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl"
Built-inURP
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)

Texture/Sampler Declaration Macros(纹理贴图采样)

这里仅列举一些常用的,更多的可以查API includes

Built-inURP
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)

 

Shader辅助函数

代码文件可以查看:

Graphics/SpaceTransforms.hlsl at master · Unity-Technologies/Graphics​github.com/Unity-Technologies/Graphics/blob/master/Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl正在上传…重新上传取消

顶点转换函数

Built-inURP
float4 UnityObjectToClipPos(float3 pos)float4 TransformObjectToHClip(float3 positionOS)
float3 UnityObjectToViewPos(float3 pos)TransformWorldToView(TransformObjectToWorld(positionOS))

法线转换函数

Built-inURP
float4 UnityObjectToWorldNormal(float3 pos)float4 TransformObjectToWorldNormal(float3 normalOS)

通用辅助函数

Built-inURP
float3 WorldSpaceViewDir (float4 v)float3 GetWorldSpaceViewDir(float3 positionWS)Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl”
float3 UnityObjectToWorldDir(float4 v)TransformObjectToWorldDir(real3 dirOS)
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-inURP
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) - objectSpacePositionInclude “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl”
float3 Shade4PointLights (…)可以使用half3 VertexLighting(float3 positionWS, half3 normalWS)对于VertexLighting(...) include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl”

屏幕空间辅助函数

Built-inURP
float4 ComputeScreenPos (float4 clipPos)float4 ComputeScreenPos(float4 positionCS)Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/ShaderVariablesFunctions.hlsl”
float4 ComputeGrabScreenPos (float4 clipPos)移除了

顶点照明辅助函数

Built-inURP
loat3 ShadeVertexLights (float4 vertex, float3 normal)移除了,可以尝试使用UNITY_LIGHTMODEL_AMBIENT.xyz + VertexLighting(...)对于VertexLighting(...) include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl”

内置着色器变量

除了光照相关的变量外,其他的变量名都基本没变

Lighting(照明)

Built-inURP
_LightColor0_MainLightColorInclude “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl”
_WorldSpaceLightPos0_MainLightPositionInclude “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl”
_LightMatrix0移除了。目前尚不支持Cookie
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_WorldToShadowfloat4x4 _MainLightWorldToShadow[MAX_SHADOW_CASCADES + 1] 或者_AdditionalLightsWorldToShadow[MAX_VISIBLE_LIGHTS]Include “Packages/com.unity.render-pipelines.universal/ShaderLibrary/Shadows.hlsl”

如果要使用循环所有其他灯光GetAdditionalLight(...)GetAdditionalLightsCount()可以使用来查询其他灯光计数。

其他

Shadows(阴影)

Built-inURP
UNITY_SHADOW_COORDS(x)移除了。DIY,例如float4 shadowCoord : TEXCOORD0;
TRANSFER_SHADOW(a)a.shadowCoord = TransformWorldToShadowCoord(worldSpacePosition)
SHADOWS_SCREEN移除了

Fog(雾)

Built-inURP
UNITY_FOG_COORDS(x)移除了。DIY,例如float fogCoord : TEXCOORD0;
UNITY_TRANSFER_FOG(o*,outpos)o.fogCoord = ComputeFogFactor(clipSpacePosition.z);
UNITY_APPLY_FOG(coord,col)color = MixFog(color,i.fogCoord);

Depth(深度)

要使用相机深度纹理,需要include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl" ,然后会自动声明_CameraDepthTexture,也会包含辅助函数SampleSceneDepth(...)LoadSceneDepth(...)

Built-inURP
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-inURP
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”

基本上Shader代码对照上述整理的内容,做完更替后即可完成升级。

节选自:Unity 手把手教你从Built-in升级URP - 知乎

Unity引擎中,Shader(着色器)是用于控制游戏物体表面光照、颜色和纹理表现的关键元素。如果你遇到ShaderURP(Universal Render Pipeline)中变紫的问题,这可能是由于以下几个原因: 1. **颜色溢出**:如果Shader中的颜色计算导致了超出预期的颜色范围(例如从0到1的RGB值),可能会显示紫色,尤其是在没有正确设置Alpha通道的情况下。 2. **混合模式问题**:检查你的混合模式设置,特别是在使用Alpha或Cutout时,错误的混合可能会影响颜色输出。 3. **UV映射或贴图问题**:如果贴图的UV映射不正确,或者贴图本身有问题,也可能影响到最终的渲染结果。 4. **材质参数调整**:检查你的Shader中的参数,如颜色乘法(ColorMultiply)或光照相关的参数,它们可能被意外地设置为紫色。 5. **URP特定问题**:URP的着色器系统对某些类型的操作可能有不同的处理方式,确保你的Shader针对URP做了适当的优化和兼容性调整。 6. **光照或环境影响**:强烈的环境光或者全局光照可能导致颜色看起来不自然,特别是对于非线性渲染管道如URP。 要解决这个问题,你可以尝试以下步骤: - **调试代码**:检查Shader脚本,确认颜色计算是否正确。 - **使用示例代码**:如果可能,参考官方文档中关于URPShader示例,确保你的代码没有遗漏关键部分。 - **分步测试**:逐个修改Shader中的参数,看哪一步导致了紫色。 - **使用Unity的工具**:利用Unity的Inspector或者Shader Graph工具检查实时效果,更容易发现问题所在。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值