当前位置:网站首页>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
}
}
}
边栏推荐
- VSCode隐藏左边活动栏
- IO进程线程->文件IO->day2
- VsCode connects to the remote server and modifies the file code
- >>> /deep/ ::v-deep 深度作用选择器
- Through the bit operations to convert the characters are case sensitive
- QT weekly skills (2)~~~~~~~~~ interface buttons
- C语言,库函数中qsort的用法,及解释
- ssh script space character conversion
- Acwing刷题第一节
- survivor区对象何时进入老年代(深入理解jvm中表述不准确的地方)
猜你喜欢

QT weekly skills (2)~~~~~~~~~ interface buttons

sizeof和strlen最全区别,以及指针和数组运算解析

MindSpore 提 PR 全流程
![[Punctuality Atom] Learning and use of IIC (unfinished...)](/img/b7/325cad848eacee67c56c6cad321bd0.png)
[Punctuality Atom] Learning and use of IIC (unfinished...)

C language, usage of qsort in library function, and explanation

Antd简单启动一个企业级项目

Kunlun State Screen Production (Serialization 5) --- Basics (serial port reception, text and light display)

i++与 ++i 的区别
![Massive remote sensing data processing and application of GEE cloud computing technology [basic, advanced]](/img/38/239933ac987da762135db2d13902d0.png)
Massive remote sensing data processing and application of GEE cloud computing technology [basic, advanced]

力扣题解7.27
随机推荐
2020-09-03解决pip install安装非常慢[Errno 101] 网络不可达问题
力扣题解
OpenLayers 初学者指南,源码测试可用
一文盘点五款 BLDC 风机参考方案,建议先马
Kunlun state screen production (serial 3) - based article (button serial port to send)
BLDC电机应用持续火爆,“网红神器”筋膜枪前景几何?
Acwing Brush Questions Section 1
Diwen serial screen production (serialization 1) ===== preparation work
删除当前路径下含某个关键字的所有文件
暂时存着阿里云
Kunlun State Screen Production (Serialization 5) --- Basics (serial port reception, text and light display)
迷宫问题----经典回溯法解决
Acwing刷题第一节
HSPF model application
QT weekly skills (3)~~~~~~~~~ serial port addition
The most complete difference between sizeof and strlen, as well as pointer and array operation analysis
查看 word版本号
[Punctuality Atom] Learning and use of IIC (unfinished...)
使用Dva项目作Antd的Demo
xxx is not in the sudoers file.This incident will be reported error