Unity Shader从内置渲染管线迁移到URP

Unity 在URP中将shader更新为了HLSL语言,使用build-in shader 无法直接在URP中使用
这里讲一下关于shader的更新方法

参考 From Built-in to URP

更新步骤

  1. Tags 添加
    "RenderPipeline" = "UniversalPipeline"

  2. CGPROGRAM ENDCG 改变为 HLSLPROGRAM ENDHLSL

  3. #include "UnityCG.cginc"
    更改为 #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

  4. fixed4 类型更改为 half4

  5. 函数替换对照

Build-in RP URP
UnityObjectToClipPos(v.vertex); TransformObjectToHClip(v.vertex.xyz);
mul(unity_ObjectToWorld,v.vertex).xyz TransformObjectToWorld(v.vertex.xyz)
mul(float4(v.normal, 0.0),unity_WorldToObject).xyz /UnityObjectToWorldNormal(v.normal) TransformObjectToWorldNormal(v.normal)

代码

上代码 一个简单的uv流动shader
内置渲染管线

// Upgrade NOTE: replaced '_Object2World' with 'unity_ObjectToWorld'
// Upgrade NOTE: replaced '_World2Object' with 'unity_WorldToObject'

Shader "Custom/liuguang"
{
    Properties
    {
        _FlowTex("Texture", 2D) = "white" {}
        _FlowTilling("Flow Tilling",Vector) = (1,1,0,0)
        _FlowSpeed("Flow Speed",Vector) = (1,1,0,0)

    }
    SubShader
    {
        Tags { "Queue" = "Transparent" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
        // make fog work


            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 texcoord : TEXCOORD0;
                float3 normal : NORMAL;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 pos : SV_POSITION;
                float3 pos_world : TEXCOORD1;
                float3 normal_world : TEXCOORD2;
                float3 pivot_world : TEXCOORD3;
            };


            sampler2D _FlowTex;
            float4 _FlowTilling;
            float4 _FlowSpeed;
    

            v2f vert(appdata v)
            {
                v2f o;
                o.pos = UnityObjectToClipPos(v.vertex);
                float3 normal_world = mul(float4(v.normal, 0.0),unity_WorldToObject).xyz;
                o.normal_world = normalize(normal_world);
                o.pos_world = mul(unity_ObjectToWorld,v.vertex).xyz;
                o.pivot_world = mul(unity_ObjectToWorld, float4(0, 0, 0, 1)).xyz;
                o.uv = v.texcoord;
                return o;
            }

            fixed4 frag(v2f i) : SV_Target
            {
                half2 uv_flow = (i.pos_world.xy - i.pivot_world.xy) * _FlowTilling.xy;
                uv_flow = uv_flow + _Time.y * _FlowSpeed.xy;
                float4 flow_rgba = tex2D(_FlowTex, uv_flow);
                return flow_rgba;
            }
            ENDCG
        }
    }
}

URP

Shader "Custom/flowlight"
{
    Properties
    {
        _FlowTex("Texture", 2D) = "white" {}
        _FlowTilling("Flow Tilling",Vector) = (1,1,0,0)
        _FlowSpeed("Flow Speed",Vector) = (1,1,0,0)

    }
    SubShader
    {
        Tags { "RenderType" = "Opaque"  "RenderPipeline" = "UniversalPipeline" }
        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;
                float2 texcoord : TEXCOORD0;
                float3 normal : NORMAL;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 pos : SV_POSITION;
                float3 pos_world : TEXCOORD1;
                float3 normal_world : TEXCOORD2;
                float3 pivot_world : TEXCOORD3;
            };


            sampler2D _FlowTex;
            float4 _FlowTilling;
            float4 _FlowSpeed;


            v2f vert(appdata v)
            {
                v2f o;
                o.pos = TransformObjectToHClip(v.vertex.xyz);
                float3 normal_world = TransformObjectToWorldNormal(v.normal);
                o.normal_world = normalize(normal_world);
                o.pos_world = TransformObjectToWorld(v.vertex.xyz);
                o.pivot_world = TransformObjectToWorld(float3(0,0,0));
                o.uv = v.texcoord;
                return o;
            }

            half4  frag(v2f i) : SV_Target
            {
                half2 uv_flow = (i.pos_world.xy - i.pivot_world.xy) * _FlowTilling.xy;
                uv_flow = uv_flow + _Time.y * _FlowSpeed.xy;
                float4 flow_rgba = tex2D(_FlowTex, uv_flow);
                return flow_rgba;
            }
            ENDHLSL
        }
    }
}

多pass的情况

对于跟光照相关的shader ,迁移到urp可能会只执行第一个pass
需要通过lightmode 锁定到pass上
urp最多支持三个pass ,按如下的顺序为pass 添加tag即可
Tags{"LightMode" = "SRPDefaultUnlit"}
Tags{"LightMode" = "UniversalForward"}
Tags{"LightMode" = "LightweightForward"}文章来源地址https://uudwc.com/A/wpGj

原文地址:https://blog.csdn.net/qq_35649669/article/details/125938480

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请联系站长进行投诉反馈,一经查实,立即删除!

上一篇 2023年06月17日 05:39
联盟链 Hyperledger Fabric 应用场景
下一篇 2023年06月17日 05:40