当前位置:网站首页>[TA frost wolf \u may- hundred people plan] Introduction to basic rendering lighting (I)
[TA frost wolf \u may- hundred people plan] Introduction to basic rendering lighting (I)
2022-07-19 04:36:00 【zczplus】
【TA- Frost Wolf _may-《 Hundred people plan 》】 Introduction to basic rendering lighting ( One
【TA- Frost Wolf _may-《 Hundred people plan 》】 Introduction to basic rendering lighting ( One
The complete code block is as follows , Play it from beginning to end , I feel like stringing everything before ! It's all right. Turn it out and have a look !
Shader "Custom/100 learning/myOwnBaseShaser"
{
Properties
{
_Diffuse("Diffuse", Color) = (1,1,1,1)
_MainTex("Main Tex", 2D) = "white" {
}
_Specular("Specular", Color) = (1,1,1,1)
_Gloss("Gloss", Range(1.0, 255)) = 20
_NormalMap("Normal Map", 2D) = "white" {
}
_LocalNormalSild("Local Normal", Range(0,1)) = 0
_CubeMap("Enviroment Cube Map", Cube) ="" {
}
_MipScale("Mip Scale", Range(0,10)) = 0
_EnvScale("Enviroment Scale", Range(0,1)) = 1
}
SubShader
{
Pass{
// Definition Tags
Tags{
"LightingMode" = "FOrwardBase"}
CGPROGRAM
// Import header file
#include "Lighting.cginc"
// Defined function
#pragma vertex vert
#pragma fragment frag
// Define and receive corresponding variables
float4 _Diffuse;
float4 _Specular;
float _Gloss;
float _LocalNormalSild;
uniform sampler2D _NormalMap; uniform float4 _NormalMap_ST;
sampler2D _MainTex; float4 _MainTex_ST;
samplerCUBE _CubeMap;
float _MipScale;
float _EnvScale;
// Defining structure
struct a2v{
float4 vertex:POSITION;
float3 normal:NORMAL;
float4 tangent:TANGENT;
float2 texcoord0 : TEXCOORD0;
};
struct v2f{
float4 pos : SV_POSITION;
float3 worldNormal:NORMAL;
float2 uv0 : TEXCOORD0;
float3 normalDir : TEXCOORD1;
float3 tangentDir : TEXCOORD2;
float3 bitangentDir : TEXCOORD3;
float3 worldPos: TEXCOORD4;
};
v2f vert(a2v v){
v2f o;
o.uv0 = v.texcoord0;
o.pos = UnityObjectToClipPos(v.vertex);
// Convert normals to world space
// Old style :o.worldNormal = normalize(mul(v.normal,(float3x3)unity_WorldToObject));
o.worldNormal = UnityObjectToWorldNormal(v.normal);
// The vertex position is converted to world space
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
// Calculating normals 、 Tangent line 、 Sub tangent line
o.normalDir = UnityObjectToWorldNormal(v.normal);
o.tangentDir = normalize(mul(unity_ObjectToWorld,float4(v.tangent.xyz,0.0)).xyz);
o.bitangentDir = normalize(cross(o.normalDir,o.tangentDir)*v.tangent.w);
return o;
}
fixed4 frag(v2f i): SV_Target{
float4 mainTex = tex2D(_MainTex, TRANSFORM_TEX(i.uv0,_MainTex));
// TBN Transformation matrix
float3x3 tangentTransform = float3x3 (i.tangentDir, i.bitangentDir,i.normalDir);
// The ambient light
float3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * _Diffuse * mainTex.rgb;
// Normalized illumination direction
float3 worldLight = normalize(_WorldSpaceLightPos0.xyz);
// Normalize again worldNormal
float3 worldNormal = normalize(i.worldNormal);
i.normalDir = worldNormal;
// Get mapped normal map data
float3 normalLocal = UnpackNormal(tex2D(_NormalMap, TRANSFORM_TEX(i.uv0, _NormalMap)));
float3 normalWorld = normalize(mul(normalLocal.rgb, tangentTransform));
float3 finiNormal = lerp(worldNormal, normalWorld, _LocalNormalSild);
// Calculate diffuse reflection lambert The formula
float NdotL = max(0.0, dot(finiNormal, worldLight));
// redundant :float NdotLNormalTex = dot(finiNormal, worldLight);
// diffuse
float3 diffuse = lerp(ambient.rgb * _Diffuse.rgb * mainTex.rgb, _LightColor0.rgb * _Diffuse.rgb * mainTex.rgb,NdotL);
//=====Phong=====
// Calculate the reflection direction R,worldLight Indicates the direction of the light source , The direction of the incident light is -worldLight, adopt reflect function ( Incident direction , Normal direction )
float3 reflectDir = normalize(reflect(-worldLight,finiNormal));
// Calculate the corresponding position of the pixel ( The vertex is calculated and transmitted to the pixel. After interpolation )finiNormalV, Camera coordinates - Pixel coordinates
// This is a little wrong to write in the slice shader float3 viewDir = normalize(UnityWorldSpaceViewDir(i.pos));
float3 viewDir = normalize(_WorldSpaceCameraPos.xyz - i.worldPos.xyz);
// The angle between the reflected light direction and the observation direction dot(V,R)
float VdotR = max(0.0, dot(reflectDir, viewDir));
// Calculate the highlight value , The reflection value calculated according to the reflection coefficient is pow(VdotR, Gloss)
float3 specular = _LightColor0.rgb * _Specular.rgb * pow(VdotR, _Gloss);
// The final color is obtained
float3 color = ambient + diffuse + specular;
//=====Blinn-Phong=====
// Calculate the half angle vector ( Optical direction + Direction of view , The results were normalized )
float3 halfDir = normalize(worldLight + viewDir);
// The angle between the half angle vector and the normal direction dot(V,R)
float NdotH = saturate(dot(halfDir,finiNormal));
// Environment mapping
float3 worldRef = normalize(reflect(-viewDir, finiNormal));
float4 reflcol = texCUBElod( _CubeMap, float4(worldRef.rgb, (255-_Gloss)*8/(255))) * _EnvScale;
// Calculation Specular ( stay Blinn-Phong Next )
specular = _LightColor0.rgb * _Specular.rgb * pow(NdotH, _Gloss);
specular = lerp(diffuse * specular, specular, _Gloss/255);
reflcol.rgb = lerp(reflcol * diffuse.rgb, reflcol, _Gloss/255);
color =diffuse + reflcol.rgb + specular;
return fixed4(color.rgb,1.0);
}
ENDCG
}
}
FallBack "Diffuse"
}

边栏推荐
- [ruoyi Vue plus] learning notes 30 - redisson (VI) bounded blocking queue (redisson source code + Lua script)
- tf.AUTO_REUSE的作用
- Common errors in golang compilation
- 复旦微FMQL(国产Zynq) 【IAR裸机开发之PS】——非字节对齐访问
- [pits]packets go:428: busy buffer invalid connection
- Sphinx遇到的问题
- Policy mode replaces if else
- Thesis research NLP
- The author of surging issued the pressure test results
- 使用Everything清理垃圾文件
猜你喜欢
随机推荐
OSPF basic optimization
Fetch request - simple record
北斗时钟服务器(NTP服务器)让高考时间更加精准
‘ionic‘ 不是内部或外部命令,也不是可运行的程序或批处理文件。
Week 2022/7/16
Cannot find module ‘process‘ or its corresponding type declarations.
PyTorch Image Models(timm)库
The author of surging issued the pressure test results
Rk1126 realizes picture in picture function picture in picture for rk 1126
Sg90 actuator experiment based on STM32, including code (HAL Library)
JS中Class类的静态属性和静态方法
Architecture and application of secure multiparty computing
As a result, all system modifications of the company will be upgraded
CAD video course recommendation station B
VB. Net plug-in development - extract files
HCR Huichen is walking on the north slope, a giant beast swimming into digital marketing services
Avplayer adds playback progress monitoring
Codeforces Round #807 (Div. 2) A~D
VirtualLab基础实验教程-7.偏振(3)
Policy mode replaces if else









