透光效果模拟

透光效果模拟

四月 03, 2022

向量

1
2
3
4
float3 diffuse_color = _DiffuseColor;
float3 normalDir = normalize(i.normalDir);
float3 viewDir = normalize(_WorldSpaceCameraPos - i.posWorld.xyz);
float3 lightDir = normalize(_WorldSpaceLightPos0.xyz);

漫反射

1
2
3
4
5
float diff_term = max(0.0, dot(normalDir, lightDir));
float3 diffuselight_color = diff_term * diffuse_color * _LightColor0.rgb;
float sky_sphere = (dot(normalDir,float3(0,1,0)) + 1.0) * 0.5;
float3 sky_light = sky_sphere * diffuse_color;
float3 final_diffuse = diffuselight_color + sky_light * _Opacity + _AddColor.xyz;

穿透光

1
2
3
4
5
float3 back_dir = -normalize(lightDir + normalDir * _BasePassDistortion);
float VdotB = max(0.0, dot(viewDir, back_dir));
float backlight_term = max(0.0,pow(VdotB, _BasePassPower)) * _BasePassScale;
float thickness = 1.0 - tex2D(_ThicknessMap, i.uv).r;
float3 backlight = backlight_term * thickness *_LightColor0.xyz * _BasePassColor.xyz;

环境光

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
float3 reflectDir = reflect(-viewDir,normalDir);

half theta = _EnvRotate * UNITY_PI / 180.0f;
float2x2 m_rot = float2x2(cos(theta), -sin(theta), sin(theta),cos(theta));
float2 v_rot = mul(m_rot, reflectDir.xz);
reflectDir = half3(v_rot.x, reflectDir.y, v_rot.y);

float4 cubemap_color = texCUBE(_EnvMap,reflectDir);
half3 env_color = DecodeHDR(cubemap_color, _EnvMap_HDR);

//菲尼尔效应
float fresnel = 1.0 - saturate(dot(normalDir, viewDir));
fresnel = smoothstep(_FresnelMin, _FresnelMax, fresnel);

float3 final_env = env_color * _EnvIntensity * fresnel;

混合

1
2
3
float3 combined_color = final_diffuse + final_env + backlight;
float3 final_color = combined_color;
return float4(final_color,1.0);

前向渲染叠加

只需要将穿透光部分加过来就行。不需要加环境光之类的