Last active
March 18, 2025 08:35
-
-
Save cancinconn/a111f4bf1e865c585d0fa93f12f08bcd to your computer and use it in GitHub Desktop.
An attempt at a basic pearlescent shader for Unity.
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/Pearlescent" { | |
Properties { | |
_Color ("Color", Color) = (1,1,1,1) | |
_ColorDirect("Indirect Color", Color) = (1,1,1,1) | |
//_MainTex ("Albedo (RGB)", 2D) = "white" {} | |
_Glossiness ("Smoothness", Range(0,1)) = 0.5 | |
_Metallic ("Metallic", Range(0,1)) = 0.0 | |
_Iterations("Iridescence Function Iterations (Power)", Range(0,20)) = 5 | |
} | |
SubShader { | |
Tags { "RenderType"="Opaque" } | |
LOD 200 | |
CGPROGRAM | |
// Physically based Standard lighting model, and enable shadows on all light types | |
#pragma surface surf Standard fullforwardshadows | |
// Use shader model 3.0 target, to get nicer looking lighting | |
#pragma target 3.0 | |
sampler2D _MainTex; | |
struct Input { | |
float2 uv_MainTex; | |
float3 viewDir; | |
}; | |
half _Glossiness; | |
half _Metallic; | |
fixed4 _Color; | |
fixed4 _ColorDirect; | |
float _Iterations; | |
// Add instancing support for this shader. You need to check 'Enable Instancing' on materials that use the shader. | |
// See https://docs.unity3d.com/Manual/GPUInstancing.html for more information about instancing. | |
// #pragma instancing_options assumeuniformscaling | |
UNITY_INSTANCING_CBUFFER_START(Props) | |
// put more per-instance properties here | |
UNITY_INSTANCING_CBUFFER_END | |
float map(float val, float min, float max, float newMin, float newMax) | |
{ | |
float ratio = abs(val - min) / abs(max - min); | |
return newMin + ratio * abs(newMax - newMin); | |
} | |
float4 mix(float4 col1, float4 col2, float mixAmount) | |
{ | |
return float4(col1.r * mixAmount + col2.r * (1 - mixAmount), col1.g * mixAmount + col2.g * (1 - mixAmount), col1.b * mixAmount + col2.b * (1 - mixAmount), col1.a * mixAmount + col2.a * (1 - mixAmount)); | |
} | |
float efficientIridFunc(float x) { | |
return x*x*x*x*x; | |
} | |
float iridFunc(float x) | |
{ | |
if (x < 0) x = 0; //get rid of the black outline caused by a thin layer of almost-perpendicular pixels | |
float result = pow(x, _Iterations); | |
return result; | |
} | |
void surf (Input IN, inout SurfaceOutputStandard o) { | |
float3 worldNormal = WorldNormalVector(IN, o.Normal); | |
float dotProduct = dot(worldNormal, IN.viewDir); //will always be between 1 and 0 for the pixels that we care about because normals facing away will be 0 to -1. I hope I don't have these numbers the wrong way around... | |
float rampPos = map(dotProduct, -1, 1, 0, 1); | |
float4 col; | |
//col = mix(_Color, _ColorDirect, efficientIridFunc(dotProduct)); | |
col = mix(_Color, _ColorDirect, iridFunc(dotProduct)); | |
o.Albedo = col; | |
// Metallic and smoothness come from slider variables | |
o.Metallic = _Metallic; | |
o.Smoothness = _Glossiness; | |
//o.Alpha = c.a; | |
} | |
ENDCG | |
} | |
FallBack "Diffuse" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment