基础的Phong模型
Shader "Custom/Phong"
{
Properties
{
_BaseColor ("BaseColor", Color) = (1,1,1,1)
_Gloss("Gloss",float) = 1
}
SubShader
{
Tags {
"RenderPipeline"="UniversalRenderPipline"
"RenderType"="Opaque"
}
HLSLINCLUDE
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
CBUFFER_START(UnityMaterial)
half4 _BaseColor;
float _Gloss;
CBUFFER_END
struct appdata
{
float4 vertex:POSITION;
float3 normal:NORMAL;
float4 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex:SV_POSITION;
float3 normal:NORMAL;
float2 uv:TEXCOORD0;
float3 PosWS:TEXCOORD1;
};
ENDHLSL
pass{
HLSLPROGRAM
#pragma vertex VERT
#pragma fragment FRAG
v2f VERT( appdata i)
{
v2f o;
o.vertex = TransformObjectToHClip(i.vertex.xyz);
o.uv = i.uv;
o.normal = i.normal;
o.PosWS = TransformObjectToWorld(i.vertex.xyz);
return o;
}
half4 FRAG(v2f i):SV_TARGET
{
Light light = GetMainLight();
//准备向量
//世界空间法线
float3 nDir = normalize(TransformObjectToWorldNormal(i.normal));
//世界空间灯光方向(注意要乘以负一,因为unity本身获取的灯光方向是以物体的反射为主方向 所以实际上和本身的灯光方向刚好相反)
float3 lDir = normalize(light.direction) * -1;
//世界空间视角向量 摄像机坐标减去顶点坐标
float3 vDir = normalize(_WorldSpaceCameraPos.xyz - i.PosWS);
//求反射光向量 frelect(灯光向量,法线向量)
float3 rlDir = normalize (reflect(lDir,nDir));
//dot视角和反射光 注意一定要max0 不然出现负数会产生不可预测的情况(实测会曝光)
float3 Scapular = pow(max(0,dot(rlDir,vDir)),_Gloss);
return float4(Scapular.rgbb);
}
ENDHLSL
}
}
// FallBack "Diffuse"
}