当前位置:网站首页>Unity 基础光照模型
Unity 基础光照模型
2022-06-30 08:07:00 【悠然少年心】
Shader "Unlit/NewUnlitShader"
{
Properties
{
_Color("color1",Color)=(1,1,0,1)
_BaseMap("Base Map",2D)="white" {
}
_Shininess("shininess",float)=32
}
SubShader
{
Pass //pass可以用多个pass
{
HLSLPROGRAM //HLSL可以进行替换替换成GLSL
#pragma vertex Vertex //声明函数 //顶点着色器
#pragma fragment Pixel //片段着色器 像素着色器
half4 _Color;//声明变量
sampler2D _BaseMap;
float _Shininess;
//model将物体的局部空间变换到世界空间
//view 通过摄像机的变化矩阵从世界空间变换到摄像机的空间中
//projection 投影变换变换到屏幕空间
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
struct Attributes
{
float4 vertex:POSITION;
float2 uv : TEXCOORD0;
float3 normal: NORMAL;
};
struct Varyings
{
float4 positionCS:SV_POSITION;
float2 uv : TEXCOORD0;
float3 normalWS: NORMAL;//法线向量是一个float3
float3 positionWS : TEXCOORD1;//定义一个世界空间位置
};
Varyings Vertex(Attributes IN) //顶点着色器 float4输入的变量类型 vertex变量名字
//:后面说的是顶点着色器关联的裁切空间位置 SV含义为system value系统中的值
{
Varyings OUT;
//IN.uv=IN.uv*_CosTime; //UV随时间变化
OUT.positionCS=TransformObjectToHClip(IN.vertex.xyz);
OUT.normalWS=TransformObjectToWorld(IN.normal);//这里是直接用的URP里的TransformObjectToWorld
OUT.positionWS=mul(UNITY_MATRIX_M,float4(IN.vertex.xyz,1.0));
OUT.uv=IN.uv;//如果你不需要UV进行改变就用输入进来的uv
//可以省略法线变换矩阵 因为法线会因为缩放而变化所以法线变化矩阵尤其重要
return OUT;
}
//lambert光照模型只有环境光照和漫反射
//phong模型多了一个高光
half4 Pixel(Varyings IN) : SV_TARGET //SV含义为system value
//SV_TARGET表示最后输出一个像素颜色
{
half4 color;
Light lighting=GetMainLight();//也可以通过这个函数直接获取光线的信息
float3 normal=normalize(IN.normalWS);
float3 viewDir=-normalize(_WorldSpaceCameraPos.xyz-normal);
float3 reflDir=reflect(lighting.direction,normal);
half3 ambient =SampleSH(normal)*0.02;//这个SampleSH球谐函数可以采样到周围的环境信息
float diffuse=max(0,dot(normal,lighting.direction));//直接通过lighting函数获取平行光 和光线颜色
//float3 normalWS=normalize(IN.normalWS);//获取法线位置
//float3 diffuse=max(0,dot(normalWS,_MainLightPosition.xyz));//_MainLightPosition获取灯光的位置,我们知道两个单位向量的夹角越小,它们点乘的结果越倾向于1,受到光照影响越大
float3 spec=max(0,dot(viewDir,reflDir));
spec=pow(spec,32);
color.rgb=tex2D(_BaseMap,IN.uv).rgb*_Color*diffuse*lighting.color.rgb+ambient+spec;
color.a=1.0;
return color;
}
ENDHLSL
}
}
}
边栏推荐
- 【花雕体验】14 行空板pinpong库测试外接传感器模块(之一)
- Deep learning vocabulary representation
- 【花雕体验】13 搭建ESP32C3之PlatformIO IDE开发环境
- 亚马逊测评术语有哪些?
- Want to change careers, but don't know what to do? This article is written for you who are confused
- Common tools installation, configuration, compilation, link, etc
- End-to-end 3D Point Cloud Instance Segmentation without Detection
- The counting tool of combinatorial mathematics -- generating function
- 深度学习——网络中的网络以及1x1卷积
- 【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
猜你喜欢
【NVMe2.0b 14-5】Firmware Download/Commit command
【NVMe2.0b 14-1】Abort、Asynchronous Event Request、Capacity Management command
Use of nested loops and output instances
[flower carving experience] 14 line blank board pingpong library test external sensor module (one)
Cesium learning notes (III) creating instances
【花雕体验】14 行空板pinpong库测试外接传感器模块(之一)
How to handle the expired data of redis and what are the elimination mechanisms?
More, faster, better and cheaper. Here comes the fastdeploy beta of the low threshold AI deployment tool!
全栈最全性能测试理论-总结
Opencv4.2.0+vs2015 configuration
随机推荐
牛客小白月賽52
Do you know the IP protocol?
【NVMe2.0b 14-5】Firmware Download/Commit command
Introduction to opencv (I): image reading and display
微信公众号第三方平台开发,零基础入门。想学我教你啊
Want to ask, how to choose securities companies for stock speculation? Is it safe to open an account online?
Deep learning -- language model and sequence generation
C. Fishingprince Plays With Array
2021-05-17
C# Console. Writeline() function output format
Markdown支持的emoji图标
Game 280 problem2: minimum operands to turn an array into an alternating array
Redis设计与实现(二)| 数据库(删除策略&过期淘汰策略)
Deep learning -- sequence model and mathematical symbols
[flower carving experience] 12 build the Arduino development environment of esp32c3
Deep learning vocabulary representation
Lexicographic order -- full arrangement in bell sound
Unit Test
[nvme2.0b 14 - 5] commande de téléchargement / commande du logiciel
Construction of energy conservation supervision system for campus buildings of ankery University