-
-
Save Phyllostachys/4a68bad5064e1af8eb6431491db580f8 to your computer and use it in GitHub Desktop.
An attempt at a basic pearlescent shader for Unity.
This file contains 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) | |
_Iterations("Iridescence Function Iterations (Power)", Range(0,10)) = 5 | |
} | |
SubShader { | |
Tags { "RenderType"="Opaque" } | |
LOD 200 | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
// include file that contains UnityObjectToWorldNormal helper function | |
#include "UnityCG.cginc" | |
fixed4 _Color; | |
fixed4 _ColorDirect; | |
float _Iterations; | |
struct v2f { | |
float4 pos : SV_POSITION; | |
float3 viewDir : TEXCOORD0; | |
float3 worldNormal : TEXCOORD1; | |
}; | |
v2f vert (float4 vertex : POSITION, float3 normal : NORMAL) | |
{ | |
v2f o; | |
o.pos = UnityObjectToClipPos(vertex); | |
o.viewDir = WorldSpaceViewDir(vertex); | |
o.worldNormal = UnityObjectToWorldNormal(normal); | |
return o; | |
} | |
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) | |
{ | |
float count = 1; | |
float num = x; | |
while (count < _Iterations) | |
{ | |
count++; | |
num *= x; | |
} | |
return num; | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
float amount = dot(normalize(i.viewDir), normalize(i.worldNormal)); | |
fixed4 c = 0; | |
c = mix(_Color, _ColorDirect, iridFunc(amount)); | |
return c; | |
} | |
ENDCG | |
} | |
} | |
FallBack "Diffuse" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment