Created
April 28, 2015 23:26
-
-
Save nanomobile/1de05ae7daf7775a8d49 to your computer and use it in GitHub Desktop.
Unity Cg shader "Nano Shader/Examples/Refraction"
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 "Nano Shader/Examples/Refraction" { | |
Properties { | |
_RefractionIntensity ("Refraction Intensity", Range(0, 1)) = 0.1 | |
_Refraction ("Refraction", 2D) = "bump" {} | |
[HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5 | |
} | |
SubShader { | |
Tags { | |
"Queue"="Transparent" | |
"RenderType"="Transparent" | |
} | |
GrabPass{ } | |
Pass { | |
Name "ForwardBase" | |
Tags { | |
"LightMode"="ForwardBase" | |
} | |
Blend SrcAlpha OneMinusSrcAlpha | |
Cull Off | |
Fog {Mode Off} | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#define UNITY_PASS_FORWARDBASE | |
#include "UnityCG.cginc" | |
#include "AutoLight.cginc" | |
#pragma multi_compile_fwdbase_fullshadows | |
#pragma exclude_renderers gles xbox360 ps3 flash | |
#pragma target 3.0 | |
uniform float4 _LightColor0; | |
uniform sampler2D _GrabTexture; | |
uniform float _RefractionIntensity; | |
uniform sampler2D _Refraction; uniform float4 _Refraction_ST; | |
struct VertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
float4 tangent : TANGENT; | |
float2 texcoord0 : TEXCOORD0; | |
}; | |
struct VertexOutput { | |
float4 pos : SV_POSITION; | |
float2 uv0 : TEXCOORD0; | |
float4 posWorld : TEXCOORD1; | |
float3 normalDir : TEXCOORD2; | |
float3 tangentDir : TEXCOORD3; | |
float3 binormalDir : TEXCOORD4; | |
float4 screenPos : TEXCOORD5; | |
LIGHTING_COORDS(6,7) | |
}; | |
VertexOutput vert (VertexInput v) { | |
VertexOutput o; | |
o.uv0 = v.texcoord0; | |
o.normalDir = mul(_Object2World, float4(v.normal,0)).xyz; | |
o.tangentDir = normalize( mul( _Object2World, float4( v.tangent.xyz, 0.0 ) ).xyz ); | |
o.binormalDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w); | |
o.posWorld = mul(_Object2World, v.vertex); | |
float3 lightColor = _LightColor0.rgb; | |
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); | |
o.screenPos = o.pos; | |
TRANSFER_VERTEX_TO_FRAGMENT(o) | |
return o; | |
} | |
fixed4 frag(VertexOutput i) : COLOR { | |
#if UNITY_UV_STARTS_AT_TOP | |
float grabSign = -_ProjectionParams.x; | |
#else | |
float grabSign = _ProjectionParams.x; | |
#endif | |
i.screenPos = float4( i.screenPos.xy / i.screenPos.w, 0, 0 ); | |
i.screenPos.y *= _ProjectionParams.x; | |
float2 node_27 = (i.uv0*1.0); | |
float3 _Refraction_var = UnpackNormal(tex2D(_Refraction,TRANSFORM_TEX(node_27, _Refraction))); | |
float2 sceneUVs = float2(1,grabSign)*i.screenPos.xy*0.5+0.5 + (_Refraction_var.rgb.rg*(_RefractionIntensity*0.2)); | |
float4 sceneColor = tex2D(_GrabTexture, sceneUVs); | |
float3x3 tangentTransform = float3x3( i.tangentDir, i.binormalDir, i.normalDir); | |
/////// Vectors: | |
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz); | |
float3 normalLocal = lerp(float3(0,0,1),_Refraction_var.rgb,_RefractionIntensity); | |
float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals | |
float nSign = sign( dot( viewDirection, i.normalDir ) ); // Reverse normal if this is a backface | |
i.normalDir *= nSign; | |
normalDirection *= nSign; | |
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz); | |
float3 lightColor = _LightColor0.rgb; | |
float3 halfDirection = normalize(viewDirection+lightDirection); | |
////// Lighting: | |
float attenuation = LIGHT_ATTENUATION(i); | |
float3 attenColor = attenuation * _LightColor0.xyz; | |
///////// Gloss: | |
float gloss = 0.8; | |
float specPow = exp2( gloss * 10.0+1.0); | |
////// Specular: | |
float NdotL = max(0, dot( normalDirection, lightDirection )); | |
float node_75 = 6.0; | |
float3 specularColor = float3(node_75,node_75,node_75); | |
float3 directSpecular = (floor(attenuation) * _LightColor0.xyz) * pow(max(0,dot(halfDirection,normalDirection)),specPow); | |
float3 specular = directSpecular * specularColor; | |
/////// Diffuse: | |
NdotL = dot( normalDirection, lightDirection ); | |
float node_29 = 1.0; | |
float3 w = float3(node_29,node_29,node_29)*0.5; // Light wrapping | |
float3 NdotLWrap = NdotL * ( 1.0 - w ); | |
float3 forwardLight = max(float3(0.0,0.0,0.0), NdotLWrap + w ); | |
float3 backLight = max(float3(0.0,0.0,0.0), -NdotLWrap + w ) * float3(node_29,node_29,node_29); | |
float3 directDiffuse = (forwardLight+backLight) * attenColor; | |
float node_219 = lerp(0.02,0.2,(1.0-max(0,dot(normalDirection, viewDirection)))); | |
float3 diffuse = directDiffuse * float3(node_219,node_219,node_219); | |
/// Final Color: | |
float3 finalColor = diffuse + specular; | |
return fixed4(lerp(sceneColor.rgb, finalColor,0.3),1); | |
} | |
ENDCG | |
} | |
Pass { | |
Name "ForwardAdd" | |
Tags { | |
"LightMode"="ForwardAdd" | |
} | |
Blend One One | |
Cull Off | |
Fog { Color (0,0,0,0) } | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#define UNITY_PASS_FORWARDADD | |
#include "UnityCG.cginc" | |
#include "AutoLight.cginc" | |
#pragma multi_compile_fwdadd_fullshadows | |
#pragma exclude_renderers gles xbox360 ps3 flash | |
#pragma target 3.0 | |
uniform float4 _LightColor0; | |
uniform sampler2D _GrabTexture; | |
uniform float _RefractionIntensity; | |
uniform sampler2D _Refraction; uniform float4 _Refraction_ST; | |
struct VertexInput { | |
float4 vertex : POSITION; | |
float3 normal : NORMAL; | |
float4 tangent : TANGENT; | |
float2 texcoord0 : TEXCOORD0; | |
}; | |
struct VertexOutput { | |
float4 pos : SV_POSITION; | |
float2 uv0 : TEXCOORD0; | |
float4 posWorld : TEXCOORD1; | |
float3 normalDir : TEXCOORD2; | |
float3 tangentDir : TEXCOORD3; | |
float3 binormalDir : TEXCOORD4; | |
float4 screenPos : TEXCOORD5; | |
LIGHTING_COORDS(6,7) | |
}; | |
VertexOutput vert (VertexInput v) { | |
VertexOutput o; | |
o.uv0 = v.texcoord0; | |
o.normalDir = mul(_Object2World, float4(v.normal,0)).xyz; | |
o.tangentDir = normalize( mul( _Object2World, float4( v.tangent.xyz, 0.0 ) ).xyz ); | |
o.binormalDir = normalize(cross(o.normalDir, o.tangentDir) * v.tangent.w); | |
o.posWorld = mul(_Object2World, v.vertex); | |
float3 lightColor = _LightColor0.rgb; | |
o.pos = mul(UNITY_MATRIX_MVP, v.vertex); | |
o.screenPos = o.pos; | |
TRANSFER_VERTEX_TO_FRAGMENT(o) | |
return o; | |
} | |
fixed4 frag(VertexOutput i) : COLOR { | |
#if UNITY_UV_STARTS_AT_TOP | |
float grabSign = -_ProjectionParams.x; | |
#else | |
float grabSign = _ProjectionParams.x; | |
#endif | |
i.screenPos = float4( i.screenPos.xy / i.screenPos.w, 0, 0 ); | |
i.screenPos.y *= _ProjectionParams.x; | |
float2 node_27 = (i.uv0*1.0); | |
float3 _Refraction_var = UnpackNormal(tex2D(_Refraction,TRANSFORM_TEX(node_27, _Refraction))); | |
float2 sceneUVs = float2(1,grabSign)*i.screenPos.xy*0.5+0.5 + (_Refraction_var.rgb.rg*(_RefractionIntensity*0.2)); | |
float4 sceneColor = tex2D(_GrabTexture, sceneUVs); | |
float3x3 tangentTransform = float3x3( i.tangentDir, i.binormalDir, i.normalDir); | |
/////// Vectors: | |
float3 viewDirection = normalize(_WorldSpaceCameraPos.xyz - i.posWorld.xyz); | |
float3 normalLocal = lerp(float3(0,0,1),_Refraction_var.rgb,_RefractionIntensity); | |
float3 normalDirection = normalize(mul( normalLocal, tangentTransform )); // Perturbed normals | |
float nSign = sign( dot( viewDirection, i.normalDir ) ); // Reverse normal if this is a backface | |
i.normalDir *= nSign; | |
normalDirection *= nSign; | |
float3 lightDirection = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - i.posWorld.xyz,_WorldSpaceLightPos0.w)); | |
float3 lightColor = _LightColor0.rgb; | |
float3 halfDirection = normalize(viewDirection+lightDirection); | |
////// Lighting: | |
float attenuation = LIGHT_ATTENUATION(i); | |
float3 attenColor = attenuation * _LightColor0.xyz; | |
///////// Gloss: | |
float gloss = 0.8; | |
float specPow = exp2( gloss * 10.0+1.0); | |
////// Specular: | |
float NdotL = max(0, dot( normalDirection, lightDirection )); | |
float node_75 = 6.0; | |
float3 specularColor = float3(node_75,node_75,node_75); | |
float3 directSpecular = attenColor * pow(max(0,dot(halfDirection,normalDirection)),specPow); | |
float3 specular = directSpecular * specularColor; | |
/////// Diffuse: | |
NdotL = dot( normalDirection, lightDirection ); | |
float node_29 = 1.0; | |
float3 w = float3(node_29,node_29,node_29)*0.5; // Light wrapping | |
float3 NdotLWrap = NdotL * ( 1.0 - w ); | |
float3 forwardLight = max(float3(0.0,0.0,0.0), NdotLWrap + w ); | |
float3 backLight = max(float3(0.0,0.0,0.0), -NdotLWrap + w ) * float3(node_29,node_29,node_29); | |
float3 directDiffuse = (forwardLight+backLight) * attenColor; | |
float node_219 = lerp(0.02,0.2,(1.0-max(0,dot(normalDirection, viewDirection)))); | |
float3 diffuse = directDiffuse * float3(node_219,node_219,node_219); | |
/// Final Color: | |
float3 finalColor = diffuse + specular; | |
return fixed4(finalColor * 0.3,0); | |
} | |
ENDCG | |
} | |
} | |
FallBack "Diffuse" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment