Last active
May 26, 2026 07:15
-
-
Save NamPhuThuy/7f89fcb63bb97565ee85f2363a681272 to your computer and use it in GitHub Desktop.
URP_Fresnel.shader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Shader "Custom/URP_Fresnel" | |
| { | |
| Properties | |
| { | |
| [Header(Base Settings)] | |
| _BaseColor ("Base Color", Color) = (0.1, 0.1, 0.15, 1.0) | |
| [Header(Fresnel Glow Settings)] | |
| [HDR] _GlowColor ("Glow Color", Color) = (0.0, 0.75, 1.0, 1.0) | |
| _FresnelPower ("Fresnel Power", Range(0.1, 10.0)) = 3.0 | |
| } | |
| SubShader | |
| { | |
| Tags | |
| { | |
| "RenderType"="Opaque" | |
| "RenderPipeline"="UniversalPipeline" | |
| "Queue"="Geometry" | |
| } | |
| Pass | |
| { | |
| Name "ForwardLit" | |
| Tags { "LightMode"="UniversalForward" } | |
| HLSLPROGRAM | |
| #pragma vertex vert | |
| #pragma fragment frag | |
| // URP Core Library | |
| #include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl" | |
| struct Attributes | |
| { | |
| float4 positionOS : POSITION; | |
| float3 normalOS : NORMAL; | |
| }; | |
| struct Varyings | |
| { | |
| float4 positionCS : SV_POSITION; | |
| float3 worldNormal : TEXCOORD0; | |
| float3 worldViewDir : TEXCOORD1; | |
| }; | |
| // Uniforms inside the SRP Batcher CBUFFER for optimization | |
| CBUFFER_START(UnityPerMaterial) | |
| float4 _BaseColor; | |
| float4 _GlowColor; | |
| float _FresnelPower; | |
| CBUFFER_END | |
| Varyings vert(Attributes input) | |
| { | |
| Varyings output = (Varyings)0; | |
| float3 worldPos = TransformObjectToWorld(input.positionOS.xyz); | |
| output.positionCS = TransformWorldToHClip(worldPos); | |
| output.worldNormal = TransformObjectToWorldNormal(input.normalOS); | |
| output.worldViewDir = GetWorldSpaceViewDir(worldPos); | |
| return output; | |
| } | |
| float4 frag(Varyings input) : SV_Target | |
| { | |
| // 1. Normalize interpolated vectors | |
| float3 N_vector = normalize(input.worldNormal); | |
| float3 V_vector = normalize(input.worldViewDir); | |
| // 2. Calculate the Dot Product between Normal and View directions | |
| // Saturate: clamps the dot product to 0.0 - 1.0 range | |
| float n_dot_v = saturate(dot(N_vector, V_vector)); | |
| // 3. Fresnel formula: Invert and raise to exponential power | |
| float fresnel_value = pow(1.0 - n_dot_v, _FresnelPower); | |
| // 4. Lerp between Base color and Glow color based on the Fresnel term | |
| float final_color = lerp(_BaseColor, _GlowColor, fresnel_value); | |
| return final_color; | |
| } | |
| ENDHLSL | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment