当前位置:网站首页>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
}
}
}
边栏推荐
- C # about Net cognition
- The counting tool of combinatorial mathematics -- generating function
- Summary and common applications of direction and angle operators in Halcon
- 【NVMe2.0b 14-8】Set Features(下篇)
- Deep learning -- recurrent neural network
- [flower carving experience] 13 build the platformio ide development environment of esp32c3
- tp5设置直接下载文件
- 深度学习——目标定位
- 2021-05-17
- Experiment 4 QT
猜你喜欢

深度学习——特征点检测和目标检测

【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command

1. Problems related to OpenGL window and environment configuration

Opencv4.2.0+vs2015 configuration

Wechat official account third-party platform development, zero foundation entry. I want to teach you

【NVMe2.0b 14-7】Set Features(上篇)

Combinatorial mathematics Chapter 1 Notes

Full stack performance testing theory - Summary

亚马逊测评术语有哪些?

微信公众号第三方平台开发,零基础入门。想学我教你啊
随机推荐
Niuke Xiaobai month race 52
Combinatorial mathematics Chapter 1 Notes
Do you know the IP protocol?
Tue Jun 28 2022 15:30:29 gmt+0800 (China standard time) date formatting
Oracle expansion table space installed in docker
C # about Net cognition
Deep learning vocabulary representation
MySQL quotation sentence is unlocked: algorithm=insert, lock=none
[flower carving experience] 14 line blank board pingpong library test external sensor module (one)
Simple application of generating function
Wechat applet reports errors using vant web app
Redis设计与实现(五)| Sentinel哨兵
多快好省,低门槛AI部署工具FastDeploy测试版来了!
【NVMe2.0b 14-5】Firmware Download/Commit command
Experiment 2 LED button PWM 2021/11/15
Cesium learning notes (I)
F12 packet capture is used for the whole process analysis of postman interface test
小程序使用二维码插件
What management improvements can CRM bring to enterprises
Deep learning - brnn and DRNN