惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CERT Recently Published Vulnerability Notes
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
Stack Overflow Blog
Stack Overflow Blog
aimingoo的专栏
aimingoo的专栏
C
Check Point Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
T
Tor Project blog
P
Proofpoint News Feed
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Latest news
Latest news
L
LINUX DO - 热门话题
罗磊的独立博客
T
Tenable Blog
The Hacker News
The Hacker News
美团技术团队
N
Netflix TechBlog - Medium
V
Vulnerabilities – Threatpost
阮一峰的网络日志
阮一峰的网络日志
Last Week in AI
Last Week in AI
博客园 - 司徒正美
Jina AI
Jina AI
Cyberwarzone
Cyberwarzone
云风的 BLOG
云风的 BLOG
S
Secure Thoughts
Cloudbric
Cloudbric
S
Security @ Cisco Blogs
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Microsoft Security Blog
Microsoft Security Blog
Spread Privacy
Spread Privacy
U
Unit 42
雷峰网
雷峰网
C
CXSECURITY Database RSS Feed - CXSecurity.com
Webroot Blog
Webroot Blog
爱范儿
爱范儿
博客园 - 【当耐特】
Know Your Adversary
Know Your Adversary
P
Privacy International News Feed
P
Palo Alto Networks Blog
Google Online Security Blog
Google Online Security Blog
The Last Watchdog
The Last Watchdog
博客园 - 聂微东
Help Net Security
Help Net Security
Hacker News: Ask HN
Hacker News: Ask HN
F
Full Disclosure
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
S
Security Affairs
Project Zero
Project Zero

博客园 - kingBook

git patch git 修改最后一次提交的日期 Win10 修改特定格式文件的右键快捷菜单 TypeScript async、 await、Promise LayaAir3.x 侦听程序退出 旋转力学例子 旋转力学公式 凹多边形碰撞检测 LayaAir3.x 侦听键盘事件 TypeScript 里的 override TypeScript 类的自身类型 Unity 二维数组序列化 C# 匿名对象、动态属性 Cocos Creator 安卓模拟器中无法运行 Unity Editor 保存图片、缩放纹理 LayaAir3.2.0-beta.2 设置2d刚体线性速度,在不同设备(分辨率)下,表现不一致的问题 LayaAir3.x 物理2D碰撞事件 TypeScirpt 声明Map类型变量 TypeScript 声明函数类型变量
URP 阴影
kingBook · 2025-01-02 · via 博客园 - kingBook

使用URP自带的ShadowCaster
在 .shader 文件中加入以下代码,

UsePass "Universal Render Pipeline/Lit/ShadowCaster"

或者

pass {
    Tags{ "LightMode" = "ShadowCaster" }
    HLSLPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"

    struct appdata
    {
        float4 vertex : POSITION;
    };

    struct v2f
    {
        float4 pos : SV_POSITION;
    };

    sampler2D _MainTex;
    float4 _MainTex_ST;

    v2f vert(appdata v)
    {
        v2f o;
        o.pos = mul(UNITY_MATRIX_MVP,v.vertex);
        return o;
    }
    float4 frag(v2f i) : SV_Target
    {
        float4 color;
        color.xyz = float3(0.0, 0.0, 0.0);
        return color;
    }
    ENDHLSL
}

完整阴影实现代码

Shader "Example/StandardLight" {
    Properties {
        _BaseColor("Base Color", Color)=(1, 1, 1, 1) // 内置胶囊体的颜色默认为 0x808080
        _Gloss("Gloss",Range(8,20)) = 8.0
    }

    SubShader {
        Tags {
            "RenderType"="Opaque" "RenderPipeline"="UniversalPipeline"
        }

        Pass {
            HLSLPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            
            // 阴影
            #pragma multi_compile _ _MAIN_LIGHT_SHADOWS
            //#pragma multi_compile _ _MAIN_LIGHT_SHADOWS_SCREEN // 屏幕空间阴影
            #pragma multi_compile _ _MAIN_LIGHT_SHADOWS_CASCADE
            #pragma multi_compile _ _SHADOWS_SOFT

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

            CBUFFER_START(UnityPerMaterial)

            half4 _BaseColor;
            half _Gloss;
            CBUFFER_END

            struct Attributes
            {
                float4 positionOS : POSITION;
                half3 normal:NORMAL;
            };

            struct Varyings
            {
                float4 positionCS : SV_POSITION;
                half3 worldNormal:TEXCOORD0;
                float3 worldPos:TEXCOORD1;
            };

            Varyings vert(Attributes input)
            {
                Varyings output;
                // 从对象空间到裁剪空间
                output.positionCS = TransformObjectToHClip(input.positionOS.xyz);
                output.worldNormal = TransformObjectToWorldNormal(input.normal);
                output.worldPos = TransformObjectToWorld(input.positionOS.xyz);
                return output;
            }

            half4 frag(Varyings input): SV_Target
            {
                Light mainLight = GetMainLight();

                half3 diffuse = LightingLambert(mainLight.color, mainLight.direction, input.worldNormal);

                // 计算高光 
                half3 viewDir = normalize(_WorldSpaceCameraPos.xyz - input.worldPos);
                half3 reflectDir = normalize(reflect(mainLight.direction, input.worldNormal));
                float spec = pow(saturate(dot(viewDir, -reflectDir)), _Gloss);
                half3 specular = _BaseColor.xyz * spec;

                // 获取环境光方式多种,且得到效果不一
                //half3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
                //half3 ambient = (glstate_lightmodel_ambient).xyz;
                //half3 ambient = i.vertexSH.xyz;
                //half3 ambient = SampleSH(worldNormal);
                half3 ambient = _GlossyEnvironmentColor;
                //half3 ambient = half3(unity_SHAr.w, unity_SHAg.w, unity_SHAb.w) ;
                //half3 ambient = SAMPLE_GI(i.lightmapUV, i.vertexSH, worldNormal);
                //half3 ambient =unity_AmbientEquator; 
                
                // 阴影
                float4 shadowCoord = TransformWorldToShadowCoord(input.worldPos);
                Light mainLightShadow = GetMainLight(shadowCoord);
                half shadow = mainLightShadow.shadowAttenuation;
                diffuse*=shadow;
                
                half4 output = _BaseColor * half4(diffuse + ambient + specular, 1); //将表面颜色,漫反射强度混合。
                
                return output;
            }
            ENDHLSL
        }
        
        UsePass "Universal Render Pipeline/Lit/ShadowCaster"
    }
}

参考:
https://zhuanlan.zhihu.com/p/648293957
https://blog.csdn.net/zakerhero/article/details/106274331