当前位置:网站首页>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
}
}
}
边栏推荐
猜你喜欢

Final review -php learning notes 2-php language foundation

An example of a single service in a cloud project driven by a domain

【NVMe2.0b 14】NVMe Admin Command Set

Why don't you know what to do after graduation from university?

Using typera+picgo to realize automatic uploading of markdown document pictures

Full stack performance testing theory - Summary

Cesium learning notes (V) custom geometry and appearance

Redis设计与实现(八)| 事务

电流探头的干扰源电流谱测试

示波器探头对测量电容负荷有影响吗?
随机推荐
Dlib library blink
微信公众号第三方平台开发,零基础入门。想学我教你啊
Hit the industry directly | the flying propeller launched the industry's first model selection tool
MySQL加索引语句不加锁:ALGORITHM=INPLACE, LOCK=NONE
Experiment 3 remote control
Niuke White Moon race 52
【NVMe2.0b 14-4】Directive Send/Receive command
Emoji icons supported by markdown
电流探头电路分析
Cesium learning notes (II) uploading data using rest API
Dlib database face
安科瑞高等学校校园建筑节能监管系统建设
Redis design and Implementation (VIII) | transaction
Qqquickpainteditem implements graffiti program drawing board
1162 Postfix Expression
跳槽字节跳动很难嘛?掌握这些技巧,你也能轻松通过
Full stack performance testing theory - Summary
Sword finger offer II 075 Array relative sort (custom sort, count sort)
【NVMe2.0b 14-5】Firmware Download/Commit command
MIME type Encyclopedia