当前位置:网站首页>Unity Shader标准光照模型——高光反射
Unity Shader标准光照模型——高光反射
2022-07-30 05:47:00 【Misaki_Me】
高光反射
顶点高光反射
Shader "Unlit/HighLightReflection"
{
Properties
{
_diffuse("diffuse",Color) = (1,1,1,1)
_specular("Specular",Color) = (1,1,1,1)
_gloss("Gloss",Range(1,5)) = 5
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _diffuse;
fixed4 _specular;
float _gloss;
struct v2f
{
float4 vertex : SV_POSITION;
fixed3 color : Color;
};
v2f vert (appdata_base v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
//将法线转换到世界坐标下的法线
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
//光源方向
//fixed3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
fixed3 worldLight = normalize(WorldSpaceLightDir(v.vertex)); //unity自己提供的一个计算光源方向的函数
//兰伯特公式
fixed3 diffuse = _LightColor0.rgb * _diffuse.rgb * saturate(dot(worldNormal,worldLight));
//计算反射方向,reflect方法就是计算基于法线方向的入射光线的反射光线的计算。由于方法计算与unity的世界光照方向相反,所以需要加个-值
fixed3 reflectDir = normalize(reflect(-worldLight,worldNormal));
//视角方向,V向量
//fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - UnityObjectToWorldDir(v.vertex));
fixed3 viewDir = normalize(WorldSpaceViewDir(v.vertex)); //unity自己提供的一个计算视角 方向的函数
//phong高光反射公式,gloss为调整高光程度的一个次幂级数
fixed3 specular = _LightColor0.rgb * _specular.rgb * pow(max(0,dot(reflectDir,viewDir)),_gloss);
o.color = diffuse + ambient + specular;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
return fixed4(i.color,1);
}
ENDCG
}
}
}
片元高光反射
Shader "Unlit/FragmentHighLight"
{
Properties
{
_diffuse("diffuse",Color) = (1,1,1,1)
_specular("Specular",Color) = (1,1,1,1)
_gloss("Gloss",Range(1,5)) = 5
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _diffuse;
fixed4 _specular;
float _gloss;
struct v2f
{
float4 vertex : SV_POSITION;
fixed3 worldNormal : TEXCOORD0;
fixed3 worldPos : TEXCOORD1;
};
v2f vert (appdata_base v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
//将法线转换到世界坐标下的法线
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
//光源方向
o.worldNormal = worldNormal;
o.worldPos = UnityObjectToWorldDir(v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
//光源方向
//fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
fixed3 worldLightDir = normalize(WorldSpaceLightDir(i.vertex));
//兰伯特公式
fixed3 diffuse = _LightColor0.rgb * _diffuse.rgb * max(0,dot(worldLightDir,i.worldNormal));
//计算反射方向,reflect方法就是计算基于法线方向的入射光线的反射光线的计算。由于方法计算与unity的世界光照方向相反,所以需要加个-值
fixed3 reflectDir = normalize(reflect(-worldLightDir,i.worldNormal));
//视角方向,V向量
//fixed3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
fixed3 viewDir = normalize(WorldSpaceViewDir(i.vertex)); //unity自己提供的一个计算视角 方向的函数
//phong高光反射公式,gloss为调整高光程度的一个次幂级数
fixed3 specular = _LightColor0.rgb * _specular.rgb * pow(max(0,dot(reflectDir,viewDir)),_gloss);
fixed3 color = ambient + diffuse + specular;
return fixed4(color,1);
}
ENDCG
}
}
}
Blinn_phong高光反射公式
Shader "Unlit/BlinnPhongHighLight"
{
Properties
{
_diffuse("diffuse",Color) = (1,1,1,1)
_specular("Specular",Color) = (1,1,1,1)
_gloss("Gloss",Range(1,5)) = 5
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
fixed4 _diffuse;
fixed4 _specular;
float _gloss;
struct v2f
{
float4 vertex : SV_POSITION;
fixed3 worldNormal : TEXCOORD0;
fixed3 worldPos : TEXCOORD1;
};
v2f vert (appdata_base v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
//将法线转换到世界坐标下的法线
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
//光源方向
o.worldNormal = worldNormal;
o.worldPos = UnityObjectToWorldDir(v.vertex);
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//环境光
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz;
//光源方向
//fixed3 worldLightDir = normalize(_WorldSpaceLightPos0.xyz);
fixed3 worldLightDir = normalize(WorldSpaceLightDir(i.vertex));
//兰伯特公式
fixed3 diffuse = _LightColor0.rgb * _diffuse.rgb * max(0,dot(worldLightDir,i.worldNormal));
//计算反射方向,reflect方法就是计算基于法线方向的入射光线的反射光线的计算。由于方法计算与unity的世界光照方向相反,所以需要加个-值
//fixed3 reflectDir = normalize(reflect(-worldLightDir,i.worldNormal));
//视角方向,V向量
fixed3 viewDir = normalize(WorldSpaceViewDir(i.vertex)); //unity自己提供的一个计算视角 方向的函数
//计算半角向量
fixed3 halfDir = normalize(worldLightDir + viewDir);
//blinn_phong高光反射公式,gloss为调整高光程度的一个次幂级数
fixed3 specular = _LightColor0.rgb * _specular.rgb * pow(max(0,dot(i.worldNormal,halfDir)),_gloss);
fixed3 color = ambient + diffuse + specular;
return fixed4(color,1);
}
ENDCG
}
}
}
边栏推荐
- 使用Dva项目作Antd的Demo
- (*(void (*)())0)() Interpretation
- js advanced study notes (detailed)
- antd table Summary总结栏置顶
- 测试题第三个
- 2020-09-03 Solve the very slow installation of pip install [Errno 101] Network unreachable problem
- Word使用中常用的快捷键
- [Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Entry 32 MCU and GPIO initialization parameter configuration]
- VSCode隐藏左边活动栏
- Antd简单启动一个企业级项目
猜你喜欢
[Quick MSP430f149] Notes on learning MSP430f149 during the game
进制详解(二进制、八进制、十进制、十六进制详解及相互转换,位运算)
The most complete difference between sizeof and strlen, as well as pointer and array operation analysis
VSCode hides the left activity bar
动态规划入门 JS
IO进程线程->标准IO->day1
多层板的层数,为啥选项都是偶数?就不能选奇数?
数码管动态显示及模块化编程
VsCode connects to the remote server and modifies the file code
[Punctuality Atom] Learning and use of IIC (unfinished...)
随机推荐
QT Weekly Skills (1) ~~~~~~~~~ Running Icon
租用服务器训练yolov3模型
openssl 1.1.1 compile statement
【部分项目展示】
c语言编程练习
二、2队列
51数码管显示
led闪烁
实现二叉树--实现删除
2020-09-03 Solve the very slow installation of pip install [Errno 101] Network unreachable problem
工程师必看:常见的PCB检测方法有哪些?
BLDC电机应用持续火爆,“网红神器”筋膜枪前景几何?
IO进程线程->目录IO->day3
单片机第一步
华秋第八届硬创赛与安创加速器达成战略合作,助力硬科技项目成长
Kunlun State Screen Production (Serialization 2)---Basic Chapter (setting and display, serial transmission)
jvm之方法区
(*(void (*)())0)()的解读
ipconfig Command Guide
NS3报错 fatal error: ns3/opengym-module.h: No such file or directory