#ifndef UNIVERSAL_LIT_INPUT_INCLUDED  //使用#ifndef 指令判断该文件是否已经引入,防止在编写Shader过程中重复引入该文件,作用类同C++中的头文件定义方式
#define UNIVERSAL_LIT_INPUT_INCLUDED  //如果没有则引入该文件,#define与#endif之间为文件代码//引入这五个文件,后面会用到这些文件中定义的结构体和函数
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/CommonMaterial.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/SurfaceInput.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/ParallaxMapping.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DBuffer.hlsl"#if defined(_DETAIL_MULX2) || defined(_DETAIL_SCALED)//看看是否有Detail map
#define _DETAIL
#endif// NOTE: Do not ifdef the properties here as SRP batcher can not handle different layouts.
CBUFFER_START(UnityPerMaterial)  //声明属性变量,其位于常量缓冲区中Constant Buffer,改CBuffer名字为UnityPerMaterial
float4 _BaseMap_ST;              //常量缓冲区是位于GPU内的一块用于存储常量的区域,只读,可以同时被多个Shader使用
float4 _DetailAlbedoMap_ST;
half4 _BaseColor;
half4 _SpecColor;
half4 _EmissionColor;
half _Cutoff;
half _Smoothness;
half _Metallic;
half _BumpScale;
half _Parallax;
half _OcclusionStrength;
half _ClearCoatMask;
half _ClearCoatSmoothness;
half _DetailAlbedoMapScale;
half _DetailNormalMapScale;
half _Surface;
CBUFFER_END// NOTE: Do not ifdef the properties for dots instancing, but ifdef the actual usage.
// Otherwise you might break CPU-side as property constant-buffer offsets change per variant.
// NOTE: Dots instancing is orthogonal to the constant buffer above.
#ifdef UNITY_DOTS_INSTANCING_ENABLED
UNITY_DOTS_INSTANCING_START(MaterialPropertyMetadata)UNITY_DOTS_INSTANCED_PROP(float4, _BaseColor)UNITY_DOTS_INSTANCED_PROP(float4, _SpecColor)UNITY_DOTS_INSTANCED_PROP(float4, _EmissionColor)UNITY_DOTS_INSTANCED_PROP(float , _Cutoff)UNITY_DOTS_INSTANCED_PROP(float , _Smoothness)UNITY_DOTS_INSTANCED_PROP(float , _Metallic)UNITY_DOTS_INSTANCED_PROP(float , _BumpScale)UNITY_DOTS_INSTANCED_PROP(float , _Parallax)UNITY_DOTS_INSTANCED_PROP(float , _OcclusionStrength)UNITY_DOTS_INSTANCED_PROP(float , _ClearCoatMask)UNITY_DOTS_INSTANCED_PROP(float , _ClearCoatSmoothness)UNITY_DOTS_INSTANCED_PROP(float , _DetailAlbedoMapScale)UNITY_DOTS_INSTANCED_PROP(float , _DetailNormalMapScale)UNITY_DOTS_INSTANCED_PROP(float , _Surface)
UNITY_DOTS_INSTANCING_END(MaterialPropertyMetadata)#define _BaseColor              UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4 , Metadata_BaseColor)
#define _SpecColor              UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4 , Metadata_SpecColor)
#define _EmissionColor          UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float4 , Metadata_EmissionColor)
#define _Cutoff                 UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_Cutoff)
#define _Smoothness             UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_Smoothness)
#define _Metallic               UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_Metallic)
#define _BumpScale              UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_BumpScale)
#define _Parallax               UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_Parallax)
#define _OcclusionStrength      UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_OcclusionStrength)
#define _ClearCoatMask          UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_ClearCoatMask)
#define _ClearCoatSmoothness    UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_ClearCoatSmoothness)
#define _DetailAlbedoMapScale   UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_DetailAlbedoMapScale)
#define _DetailNormalMapScale   UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_DetailNormalMapScale)
#define _Surface                UNITY_ACCESS_DOTS_INSTANCED_PROP_FROM_MACRO(float  , Metadata_Surface)
#endif//纹理声明,这些纹理属性在Lit.Shader文件中声明过   //纹理使用的采样器声明,sampler_textureName表示使用面板中设置的采样方式,这个属性的值通过材质面板传过来
TEXTURE2D(_ParallaxMap);        SAMPLER(sampler_ParallaxMap);
TEXTURE2D(_OcclusionMap);       SAMPLER(sampler_OcclusionMap);
TEXTURE2D(_DetailMask);         SAMPLER(sampler_DetailMask);
TEXTURE2D(_DetailAlbedoMap);    SAMPLER(sampler_DetailAlbedoMap);
TEXTURE2D(_DetailNormalMap);    SAMPLER(sampler_DetailNormalMap);
TEXTURE2D(_MetallicGlossMap);   SAMPLER(sampler_MetallicGlossMap);
TEXTURE2D(_SpecGlossMap);       SAMPLER(sampler_SpecGlossMap);
TEXTURE2D(_ClearCoatMap);       SAMPLER(sampler_ClearCoatMap);#ifdef _SPECULAR_SETUP //高光设置,这个关键字在Lit.Shader中声明过。如果在面板上设置了高光,则走高光工作流对_SpecGlossMap采样,否则对_MetallicGlossMap采样#define SAMPLE_METALLICSPECULAR(uv) SAMPLE_TEXTURE2D(_SpecGlossMap, sampler_SpecGlossMap, uv)
#else#define SAMPLE_METALLICSPECULAR(uv) SAMPLE_TEXTURE2D(_MetallicGlossMap, sampler_MetallicGlossMap, uv)
#endifhalf4 SampleMetallicSpecGloss(float2 uv, half albedoAlpha) //金属和高光采样函数,参数为二维浮点数精度的uv坐标、半浮点数的透明度通道,返回值为四维的半浮点数精度的颜色
{half4 specGloss;#ifdef _METALLICSPECGLOSSMAP //Lit.Shader中定义的材质关键字,如果在材质面板上给了贴图specGloss = half4(SAMPLE_METALLICSPECULAR(uv));//采样,SAMPLE_METALLICSPECULAR为上面定义的#ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A //Lit.Shader中定义的材质关键字,对应面板上的source选择,意思是透明度通道来自哪specGloss.a = albedoAlpha * _Smoothness;//来自Albedo,随后乘了粗糙度,为什么乘以这个,看URP Channel packing的说明#elsespecGloss.a *= _Smoothness;//来自Metallic Map或者Specular Map#endif
#else // _METALLICSPECGLOSSMAP //如果没给贴图#if _SPECULAR_SETUPspecGloss.rgb = _SpecColor.rgb;  //对于高光工作流,采样结果的颜色值使用设置的高光的RPG#elsespecGloss.rgb = _Metallic.rrr;  //对于金属工作流,采样结果的颜色值是使用slider的值,范围为0~1,是个灰度的#endif#ifdef _SMOOTHNESS_TEXTURE_ALBEDO_CHANNEL_A//同上specGloss.a = albedoAlpha * _Smoothness;#elsespecGloss.a = _Smoothness;#endif
#endifreturn specGloss;
}half SampleOcclusion(float2 uv)//采样AO图,即环境遮挡贴图,参数为二维浮点数精度的uv坐标,返回值为半浮点数精度的值
{#ifdef _OCCLUSIONMAP //Lit.Shader中定义的材质关键字,如果在材质面板上给了贴图// TODO: Controls things like these by exposing SHADER_QUALITY levels (low, medium, high)#if defined(SHADER_API_GLES)//如果调用的API是OpenGLESreturn SAMPLE_TEXTURE2D(_OcclusionMap, sampler_OcclusionMap, uv).g;//取的G通道的值,为什么取G,看URP Channel packing的说明#elsehalf occ = SAMPLE_TEXTURE2D(_OcclusionMap, sampler_OcclusionMap, uv).g;return LerpWhiteTo(occ, _OcclusionStrength);//可以写成Lerp(1, occ, _OcclusionStrength),做了个插值#endif#elsereturn half(1.0); //没有给图,直接返回1#endif
}// Returns clear coat parameters
// .x/.r == mask
// .y/.g == smoothness
half2 SampleClearCoat(float2 uv)//采样透明漆,车漆相关的东西
{
#if defined(_CLEARCOAT) || defined(_CLEARCOATMAP)half2 clearCoatMaskSmoothness = half2(_ClearCoatMask, _ClearCoatSmoothness);#if defined(_CLEARCOATMAP)clearCoatMaskSmoothness *= SAMPLE_TEXTURE2D(_ClearCoatMap, sampler_ClearCoatMap, uv).rg;
#endifreturn clearCoatMaskSmoothness;
#elsereturn half2(0.0, 1.0);
#endif  // _CLEARCOAT
}void ApplyPerPixelDisplacement(half3 viewDirTS, inout float2 uv)//inout关键字表示改uv既用于输入,又用于输出,这个方法在用到的时候再说
{
#if defined(_PARALLAXMAP)uv += ParallaxMapping(TEXTURE2D_ARGS(_ParallaxMap, sampler_ParallaxMap), viewDirTS, _Parallax, uv);
#endif
}// Used for scaling detail albedo. Main features:
// - Depending if detailAlbedo brightens or darkens, scale magnifies effect.
// - No effect is applied if detailAlbedo is 0.5.
half3 ScaleDetailAlbedo(half3 detailAlbedo, half scale)
{// detailAlbedo = detailAlbedo * 2.0h - 1.0h;// detailAlbedo *= _DetailAlbedoMapScale;// detailAlbedo = detailAlbedo * 0.5h + 0.5h;// return detailAlbedo * 2.0f;// A bit more optimizedreturn half(2.0) * detailAlbedo * scale - scale + half(1.0);//不清楚为什么这样算
}half3 ApplyDetailAlbedo(float2 detailUv, half3 albedo, half detailMask)//添加detailBaseMap的漫反射值
{
#if defined(_DETAIL)half3 detailAlbedo = SAMPLE_TEXTURE2D(_DetailAlbedoMap, sampler_DetailAlbedoMap, detailUv).rgb;//根据贴图、采样方法、uv得到采样结果// In order to have same performance as builtin, we do scaling only if scale is not 1.0 (Scaled version has 6 additional instructions)
#if defined(_DETAIL_SCALED)detailAlbedo = ScaleDetailAlbedo(detailAlbedo, _DetailAlbedoMapScale);//缩放漫反射值
#elsedetailAlbedo = half(2.0) * detailAlbedo;
#endifreturn albedo * LerpWhiteTo(detailAlbedo, detailMask);
#elsereturn albedo;//没有细节贴图直接返回
#endif
}half3 ApplyDetailNormal(float2 detailUv, half3 normalTS, half detailMask)//添加detail normal map得值
{
#if defined(_DETAIL)
#if BUMP_SCALE_NOT_SUPPORTEDhalf3 detailNormalTS = UnpackNormal(SAMPLE_TEXTURE2D(_DetailNormalMap, sampler_DetailNormalMap, detailUv));//unpackNormal是将通道值从0到1恢复到-1到1
#elsehalf3 detailNormalTS = UnpackNormalScale(SAMPLE_TEXTURE2D(_DetailNormalMap, sampler_DetailNormalMap, detailUv), _DetailNormalMapScale);
#endif// With UNITY_NO_DXT5nm unpacked vector is not normalized for BlendNormalRNM// For visual consistancy we going to do in all casesdetailNormalTS = normalize(detailNormalTS);return lerp(normalTS, BlendNormalRNM(normalTS, detailNormalTS), detailMask); // todo: detailMask should lerp the angle of the quaternion rotation, not the normals
#elsereturn normalTS;
#endif
}inline void InitializeStandardLitSurfaceData(float2 uv, out SurfaceData outSurfaceData)//初始化Lit.Shader的表面数据,inline关键字和C++中的inline的作用一样
{                                                                                       //SurfaceData是结构体,在SurfaceData.hlsl中定义的,在其他文件中引入的half4 albedoAlpha = SampleAlbedoAlpha(uv, TEXTURE2D_ARGS(_BaseMap, sampler_BaseMap));//采样BaseMap获取漫反射的透明通道outSurfaceData.alpha = Alpha(albedoAlpha.a, _BaseColor, _Cutoff);//再根据BaseColor和Cutoff得到表面最终的透明通道值,这两个属性再Lit.shader中定义了half4 specGloss = SampleMetallicSpecGloss(uv, albedoAlpha.a);//采样漫反射,见上面的解释outSurfaceData.albedo = albedoAlpha.rgb * _BaseColor.rgb;//得到表面数据漫反射值#if _SPECULAR_SETUP//高光设置outSurfaceData.metallic = half(1.0);outSurfaceData.specular = specGloss.rgb;
#elseoutSurfaceData.metallic = specGloss.r;outSurfaceData.specular = half3(0.0, 0.0, 0.0);
#endifoutSurfaceData.smoothness = specGloss.a;//粗糙度outSurfaceData.normalTS = SampleNormal(uv, TEXTURE2D_ARGS(_BumpMap, sampler_BumpMap), _BumpScale);//法线的TSoutSurfaceData.occlusion = SampleOcclusion(uv);//采样遮挡,见上面的解释outSurfaceData.emission = SampleEmission(uv, _EmissionColor.rgb, TEXTURE2D_ARGS(_EmissionMap, sampler_EmissionMap));//采样自发光#if defined(_CLEARCOAT) || defined(_CLEARCOATMAP)//如果有clearcoathalf2 clearCoat = SampleClearCoat(uv);outSurfaceData.clearCoatMask       = clearCoat.r;outSurfaceData.clearCoatSmoothness = clearCoat.g;
#elseoutSurfaceData.clearCoatMask       = half(0.0);outSurfaceData.clearCoatSmoothness = half(0.0);
#endif#if defined(_DETAIL)//如果有细节图half detailMask = SAMPLE_TEXTURE2D(_DetailMask, sampler_DetailMask, uv).a;//获取detailMask的透明通道float2 detailUv = uv * _DetailAlbedoMap_ST.xy + _DetailAlbedoMap_ST.zw;//算uv,一般就这样算outSurfaceData.albedo = ApplyDetailAlbedo(detailUv, outSurfaceData.albedo, detailMask);outSurfaceData.normalTS = ApplyDetailNormal(detailUv, outSurfaceData.normalTS, detailMask);
#endif
}#endif // UNIVERSAL_INPUT_SURFACE_PBR_INCLUDED

URP Lit Shader解析(2)—LitInput.hlsl相关推荐

  1. URP Lit Shader解析(1)

    [说明] URP 版本为12.1.1,Lit Shader最复杂,明白了这个Shader,其他的都可以搞明白. 会拆分成一步步地解析,所有解析都写在源码中. 如有错误,欢迎指正! Shader &qu ...

  2. 深入URP之Shader篇8: SimpleLit Shader分析(4)

    Simple Lit Forward Pass 本篇继续 Fragment shader 函数 InitializeInputData InputData inputData; InitializeI ...

  3. URP渲染管线初步解析

    URP渲染管线初步解析 LWRP现在在unity2019已经成为默认的管线了,并更名为URP(通用渲染管线),替代了原来的builtin管线,以下是应用阶段的渲染流程顺序. 步骤分析 URP整个渲染的 ...

  4. 深入URP之Shader篇5: SimpleLit Shader分析(1)

    SimpleLit.shader 本篇开始分析simple lit shader.我们通过分析unlit shader了解了URP shader的结构,以及一些基础功能.而simple lit sha ...

  5. URP/LWRP Shader实现描边效果

    2021.1.11 更新: 我觉得我写得比较老了,可以看看下面新整理的文章 LWRP/URP/HDRP中的描边shader:https://zhuanlan.zhihu.com/p/354190065 ...

  6. [HDRP] PBR Lit shader(一.基础用法篇)

    本文转自Unity Connect博主 樱花兔 借助这默认的HDRP Shader,小伙伴们可以轻松地在HDRP中创建逼真的物理材质.它包括一些效果选项,例如次表面散射,虹彩,顶点或像素位移等各种表现 ...

  7. ase支持 urp不_urp管线的自学hlsl之路 第一篇 序言

    入坑unity学习已经有一年半了,shader很早就在用连连看学习和使用(lwrp hdrp的shadergraph和build in的ase).但是随着对于画质和效果的追求和提高,使用手写shade ...

  8. 深入URP之Shader篇3: Unlit Shader分析[下]

    Unlit shader 上篇中我们分析了Unlit shader的Properties在ShaderGUI中的处理,接下来看Sub Shader. SubShader unlit shader以及其 ...

  9. Unreal Engine 4 —— Post Process Shader练手(HLSL)

    UE4的材质大多数都是通过节点连线的,使用节点连线效率更高,对艺术家也更友好.但是用久了之后也会怀念当年写HLSL着色器代码的青葱岁月(注:这篇博客原本写于2015年10月,那段青葱岁月应该指的是倒腾 ...

最新文章

  1. linux 目录说明
  2. python创建一个有序链表_算法2-2:生成递增有序链表+两个链表合并
  3. Socket 进行发送
  4. 文本挖掘预处理流程总结(2)— 英文
  5. 【NLP】NLP 语义匹配:经典前沿方案整理
  6. 2018中国C++大会精彩回顾
  7. mongodb 文档服务器,mongodb服务器
  8. C++单元测试框架的比较(zz)
  9. git clone error port 443: Timed out
  10. 国内首部智能设备安全报告:预计 2019 年漏洞增长率超 20%
  11. spring 容器的理论知识
  12. Android异步批量下载图片并缓存
  13. 手机组态软件_安卓平板组态软件​_移动端组态
  14. 如何使用 JavaScript 快速构建一个二维码生成器
  15. 要重复多少次变成潜意识_量变到质变,首先要做的是不断重复
  16. 进击的速溶咖啡:当中国AI开始玩工业化
  17. 【转】如何设置服务器网卡千兆变万兆
  18. java实现pdf转为word
  19. 关于Keil编译程序出现“File has been changed outside the editor,reload?”的解决方法
  20. Java的基础语法(二)

热门文章

  1. 参考:ODBC Drivers error(ODBC数据库驱动错误代码)原因解释
  2. SpringBoot单元测试Mock静态方法
  3. 有哪些有效解决程序员中年危机的方法?
  4. 教你用单片机表白2019(文末送代码)
  5. Java数据结构-图
  6. 【Wayland】Weston多屏显示
  7. vue中axios利用blob实现文件浏览器下载
  8. 计算两幅图像的PSNR和SSIM以及python代码实现
  9. 网站出现问题,如何诊断?
  10. 【应用】1200PLC实现三层电梯模拟控制