当前位置:网站首页>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
}
}
}
边栏推荐
- 二、1稀疏sparsearray数组
- 快速排序学习记录
- led闪烁
- [Punctuality Atom] Simple application of sys.c, sys.h bit-band operations
- 关于 PCB 多层板制程能力不得不说的那些事儿
- This beta version of Typora is expired, please download and install a newer; workaround
- 表格比手机屏幕宽时不压缩,可左右滚动,格子内容不换行
- 租用服务器训练yolov3模型
- [Jiangsu University Self-Chemistry Association stm32F103c8t6] Notes [Introduction to 32 MCUs and Using TIM Output to Compare and Configure PWM]
- lcd1602调试
猜你喜欢

BLDC电机应用持续火爆,“网红神器”筋膜枪前景几何?
![[Punctuality Atom] Simple application of sys.c, sys.h bit-band operations](/img/7f/d9f480ab9a1e542e4fa1fda7978e4c.png)
[Punctuality Atom] Simple application of sys.c, sys.h bit-band operations

矩阵键盘

The most complete difference between sizeof and strlen, as well as pointer and array operation analysis

VSCode隐藏左边活动栏

干货 | 什么是FOC?一文带你看BLDC电机驱动芯片及解决方案

Cannnot download sources不能下载源码百分百超详细解决方案

Real-time waveform display of CAN communication data based on QT (serial eight) ==== "Sub function or new class calls ui control"

C语言,库函数中qsort的用法,及解释

Kunlun state screen production (serial 3) - based article (button serial port to send)
随机推荐
信号链模拟芯片是什么?
IO进程线程->文件IO->day2
QT weekly skills (2)~~~~~~~~~ interface buttons
二进制到汇编:进制,原码反码补码,位运算,通用寄存器,内存一套打通
【Exhibition of some projects】
Explore the efficiency of make_shared
删除当前路径下含某个关键字的所有文件
Vim find character
Through the bit operations to convert the characters are case sensitive
矩阵键盘
js 替换字符串中所有 “ 引号 —— 数据处理
-----博客声明
vs compile boost library script
HSPF model application
openssl 1.1.1 compile statement
ES6 syntax notes (ES6~ES11)
About map custom sorting of keys
Anaconda 安装低版本tensorflow
[Jiangsu University Automation Association stm32F103c8t6] Notes [Initial 32 MCU and EXTI External Interrupt Initialization Parameter Configuration]
The most complete difference between sizeof and strlen, as well as pointer and array operation analysis