当前位置:网站首页>Unity basic lighting model
Unity basic lighting model
2022-06-30 08:20:00 【Carefree young heart】
Shader "Unlit/NewUnlitShader"
{
Properties
{
_Color("color1",Color)=(1,1,0,1)
_BaseMap("Base Map",2D)="white" {
}
_Shininess("shininess",float)=32
}
SubShader
{
Pass //pass You can use more than one pass
{
HLSLPROGRAM //HLSL Can be replaced by GLSL
#pragma vertex Vertex // Declare functions // Vertex shader
#pragma fragment Pixel // Fragment Shader Pixel shader
half4 _Color;// Declare variables
sampler2D _BaseMap;
float _Shininess;
//model Transform the local space of an object into world space
//view Through the change matrix of the camera, from the world space to the camera space
//projection Projection transformation to screen space
#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;// The normal vector is a float3
float3 positionWS : TEXCOORD1;// Define a world space location
};
Varyings Vertex(Attributes IN) // Vertex shader float4 The type of variable entered vertex Variable name
//: Later, we will talk about the clipping space position associated with the vertex shader SV It means system value Values in the system
{
Varyings OUT;
//IN.uv=IN.uv*_CosTime; //UV Over time
OUT.positionCS=TransformObjectToHClip(IN.vertex.xyz);
OUT.normalWS=TransformObjectToWorld(IN.normal);// This is for direct use URP Inside TransformObjectToWorld
OUT.positionWS=mul(UNITY_MATRIX_M,float4(IN.vertex.xyz,1.0));
OUT.uv=IN.uv;// If you don't need to UV Make changes with input uv
// The normal transformation matrix can be omitted Because normals change due to scaling, the normal change matrix is particularly important
return OUT;
}
//lambert The lighting model has only ambient lighting and diffuse reflection
//phong The model has one more highlight
half4 Pixel(Varyings IN) : SV_TARGET //SV It means system value
//SV_TARGET Indicates the color of the last pixel output
{
half4 color;
Light lighting=GetMainLight();// This function can also be used to directly obtain the information of light
float3 normal=normalize(IN.normalWS);
float3 viewDir=-normalize(_WorldSpaceCameraPos.xyz-normal);
float3 reflDir=reflect(lighting.direction,normal);
half3 ambient =SampleSH(normal)*0.02;// This SampleSH Spherical harmonics can sample the information of the surrounding environment
float diffuse=max(0,dot(normal,lighting.direction));// Directly through lighting Function to get the directional light And light color
//float3 normalWS=normalize(IN.normalWS);// Get normal position
//float3 diffuse=max(0,dot(normalWS,_MainLightPosition.xyz));//_MainLightPosition Get the position of the light , We know that the smaller the angle between two unit vectors , The result of their dot multiplication tends to 1, The more affected by the light
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
}
}
}
边栏推荐
- 微信公众号第三方平台开发,零基础入门。想学我教你啊
- Wsl2 using GPU for deep learning
- Cesium learning notes (IV) visual image & Terrain
- Redis design and Implementation (VIII) | transaction
- 【NVMe2.0b 14-2】Create/Delete Queue
- 【NVMe2.0b 14-4】Directive Send/Receive command
- Axure制作菜单栏效果
- [flower carving experience] 12 build the Arduino development environment of esp32c3
- Deploy the cow like customer network project on the ECS
- tp5设置直接下载文件
猜你喜欢

At the end of June, you can start to make preparations, otherwise you won't have a share in such a profitable industry

【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command

电流探头电路分析

【NVMe2.0b 14-3】Doorbell Buffer Config command、Device Self-test command

Opencv image

【NVMe2.0b 14-2】Create/Delete Queue

Do you know the IP protocol?

Redis 的过期数据如何处理,淘汰机制都有那些?

安科瑞高等学校校园建筑节能监管系统建设

Sword finger offer II 075 Array relative sort (custom sort, count sort)
随机推荐
Applet uses QR code plug-in
【NVMe2.0b 14-5】Firmware Download/Commit command
Unity 基础光照模型
What management improvements can CRM bring to enterprises
【NVMe2.0b 14-2】Create/Delete Queue
F12抓包用于做postman接口测试的全过程解析
[tensorflow GPU] building of deep learning environment under windows11
全栈最全性能测试理论-总结
Miracle Mu server rental selection is real and easy to use, stable and intrusion proof
2021-02-18
【NVMe2.0b 14-6】Format NVM、Keep Alive、Lockdown command
php api获取二维码、组合生成图片
国债逆回购绝对安全吗 网上怎么开户
How to handle the expired data of redis and what are the elimination mechanisms?
Opencv image
mysql无法连接内网的数据库
C# Console. Writeline() function output format
At the end of June, you can start to make preparations, otherwise you won't have a share in such a profitable industry
Leetcode47. full arrangement II
[JUC series] overview of fork/join framework