9.2.2 urp多光源

该文章详细描述了如何在Unity中使用UniversalRenderPipeline(URP)实现一个多光源着色器,包括主光源和额外副光源的处理,以及漫反射和环境光的计算。
摘要由CSDN通过智能技术生成

9.2.2 urp多光源

Shader "Custom/chap9.2.2/Multi Light"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _Color("Main Color", Color) = (1,1,1,1)
        // 是否开启多光源效果
        [Toggle(_AdditionalLights)] _AddLights ("AddLights", Float) = 1
    }
    SubShader
    {
        Tags { "RenderPipeLine"="UniversalRenderPipeline" "RenderType"="Opaque" "LightMode"="UniversalForward"   }
        // LOD 100

        HLSLINCLUDE
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
            #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
            #include "Packages/com.unity.render-pipelines.core/ShaderLibrary/SpaceTransforms.hlsl"

            CBUFFER_START(UnityPerMaterial)
            // sampler2D _MainTex; 
            float4 _MainTex_ST;
            half4 _Color;
            CBUFFER_END
        ENDHLSL


        Pass
        {
            Tags { "LightMode"="UniversalForward" }
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            // #pragma multi_compile_fog
            #pragma shader_feature _AdditionalLights 

            TEXTURE2D (_MainTex);
            SAMPLER(sampler_MainTex);

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

            struct v2f
            {
                float2 uv : TEXCOORD0;
                // UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
                float3 worldNormal: TEXCOORD1;
                float3 worldPos: TEXCOORD2;
            };

            v2f vert (appdata v)
            {
                v2f o;
                // 裁剪空间坐标
                o.vertex = TransformObjectToHClip(v.vertex);

                // 世界空间法线
                o.worldNormal = TransformObjectToWorldNormal(v.normal);

                // 世界坐标
                o.worldPos = TransformObjectToWorld(o.vertex).xyz;

                // uv 
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                return o;
            }

            float4 frag (v2f i) : SV_Target
            {
                float3 worldNormal = normalize(i.worldNormal);

                Light light = GetMainLight();

                float4 texColor = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex ,i.uv);

                float3 albedo = texColor.rgb * _Color.rgb;

                // 环境光 
                float3 ambient = _GlossyEnvironmentColor.xyz * albedo;

                float3 worldLightDir = TransformObjectToWorldDir(light.direction);

                // 漫反射光
                float3 diffuse = light.color.rgb * albedo * saturate(dot(worldNormal, worldLightDir) * 0.5 + 0.5); 
                //LightingLambert(light.color.rgb, worldLightDir, worldNormal) * albedo; 
                // float3 diffuse = LightingLambert(light.color.rgb, worldLightDir, worldNormal) * albedo; 

                float3 col = ambient + diffuse;

                #ifdef _AdditionalLights 
                    int pixelLightCount = GetAdditionalLightsCount(); 
                    for(int index = 0; index < pixelLightCount; index++)        //有几次执行几次判断
                    {
                        Light light = GetAdditionalLight(index, i.worldPos);     //获取其它的副光源世界位置
                        half Deputy_NdotL = saturate(dot(worldNormal,light.direction) * 0.5 + 0.5); 
                        //副光源基础颜色  // 
                        half3 Deputy_Radiance = light.color * ((light.distanceAttenuation * light.shadowAttenuation) * Deputy_NdotL) * _Color.rgb;
                        col += Deputy_Radiance; 
                    } 
                #endif
                

                return float4(col, 1.0);
            }
            ENDHLSL
        }
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值