6.4.2 逐片元光照

6.4.2 逐片元光照

Shader "Custom/Chap6.4.2/lambert frament Shader"
{
  Properties {
    // 声明一个Color类型的属性
    _Color ("Color Tint", Color) = (1.0,1.0,1.0,1.0) 
    _Diffuse("Diffuse", Color) = (1.0,1.0,1.0,1.0)
  }

  SubShader {
    pass 
    {
      // 定义光照模式
      // Tags { "LightMode"="ForwardBase" }
      // urp 中定义 RenderPipeline 和 lightmode 定义如下
      Tags { "RenderPipeline" = "UniversalRenderPipeline" "RenderType" = "Opaqua" "LightMode" = "UniversalForward"}

      HLSLPROGRAM

      #pragma vertex vert
      #pragma fragment frag

      // 使用光照内置文件
      //   #include "Lighting.cginc"
      //   #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityInput.hlsl"

      // 使用urp光照文件
      #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"


      // 将材质属性声明在一个名为UnityPerMaterial的CBUFFER块中,这样才能保证该Shader能够兼容SRP的批处理
      CBUFFER_START(UnityPerMaterial)
      // 需要定义一个与 properties 中定义的 _Color 相符的变量来接收
      float4 _Color;
      float4 _Diffuse;
      CBUFFER_END


      struct a2v {
        float4 vertex: POSITION; // 顶点坐标
        float3 normal: NORMAL;   // 法线
      };

      struct v2f {
        // SV_POSITION语义告诉Unity, pos里包含了顶点在裁剪空间中的位置信息
        float4 pos: SV_POSITION;
        // COLORO语义可以用于存储颜色信息
        float3 color: COLOR0;
        // 世界法线坐标
        float3 worldNormal: TEXCOORD0;
      };

      v2f vert(a2v v) {
        v2f o;
        // 裁剪坐标 - urp 方式 
        o.pos = TransformObjectToHClip(v.vertex);
        // Urp 中法向量变换成世界坐标
        o.worldNormal = TransformObjectToWorldNormal(v.normal);
        return o;
      }

      float4 frag(v2f i): SV_Target {
        // 世界光照
        Light worldLight = GetMainLight();

        half3 worldLightDir = TransformObjectToWorldDir(worldLight.direction);

        // 漫反射光
        half3 diffuse = LightingLambert(worldLight.color.rgb, worldLightDir, i.worldNormal) * _Diffuse.rgb;
        // urp中环境光
        half3 ambient = _GlossyEnvironmentColor.xyz;
        i.color = ambient + diffuse; 
        float3 c = i.color;
        // 将插值后的i.color显示到屏幕上
        // c *= i.worldNormal * 0.5 + half3(0.5,0.5,0.5);// _Color.rgb;
        c *= _Color;// _Color.rgb;
        return float4(c, 1.0);
      }

      ENDHLSL
    }
  }	
}

// Light GetMainLight()
// {
//   Light light;
//   light.direction = _MainLightPosition.xyz;
//   // unity_LightData.z is 1 when not culled by the culling mask, otherwise 0.
//   light.distanceAttenuation = unity_LightData.z;
// #if defined(LIGHTMAP_ON) || defined(_MIXED_LIGHTING_SUBTRACTIVE)
//   // unity_ProbesOcclusion.x is the mixed light probe occlusion data
//   light.distanceAttenuation *= unity_ProbesOcclusion.x;
// #endif
//   light.shadowAttenuation = 1.0;
//   light.color = _MainLightColor.rgb;

//   return light;
// }
  • 7
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值