Skip to content

Instantly share code, notes, and snippets.

@KenneyNL
Created December 6, 2017 00:29
Show Gist options
  • Save KenneyNL/ae6e0ee3ce2298138214536d62347a5d to your computer and use it in GitHub Desktop.
Save KenneyNL/ae6e0ee3ce2298138214536d62347a5d to your computer and use it in GitHub Desktop.
// Shader created with Shader Forge v1.38
// Shader Forge (c) Neat Corporation / Joachim Holmer - http://www.acegikmo.com/shaderforge/
// Note: Manually altering this data may prevent you from opening it in Shader Forge
/*SF_DATA;ver:1.38;sub:START;pass:START;ps:flbk:,iptp:0,cusa:False,bamd:0,cgin:,lico:1,lgpr:1,limd:1,spmd:1,trmd:0,grmd:0,uamb:True,mssp:True,bkdf:False,hqlp:False,rprd:False,enco:False,rmgx:True,imps:True,rpth:0,vtps:0,hqsc:True,nrmq:1,nrsp:0,vomd:0,spxs:False,tesm:0,olmd:1,culm:0,bsrc:0,bdst:1,dpts:2,wrdp:True,dith:0,atcv:False,rfrpo:True,rfrpn:Refraction,coma:15,ufog:True,aust:True,igpj:False,qofs:0,qpre:2,rntp:3,fgom:False,fgoc:False,fgod:False,fgor:False,fgmd:0,fgcr:1,fgcg:1,fgcb:1,fgca:1,fgde:0.01,fgrn:5,fgrf:15,stcl:False,atwp:False,stva:128,stmr:255,stmw:255,stcp:6,stps:0,stfa:0,stfz:0,ofsf:0,ofsu:0,f2p0:False,fnsp:False,fnfb:False,fsmp:False;n:type:ShaderForge.SFN_Final,id:4904,x:33076,y:32617,varname:node_4904,prsc:2|diff-5996-OUT,clip-8651-A;n:type:ShaderForge.SFN_Color,id:848,x:32186,y:32549,ptovrint:False,ptlb:ColorBottom,ptin:_ColorBottom,varname:_Color,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:0.5,c2:0.5,c3:0.5,c4:1;n:type:ShaderForge.SFN_Color,id:3661,x:32020,y:32549,ptovrint:False,ptlb:Color,ptin:_Color,varname:_Color_bottom,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,c1:0.5,c2:0.5,c3:0.5,c4:1;n:type:ShaderForge.SFN_NormalVector,id:816,x:31904,y:32778,prsc:2,pt:False;n:type:ShaderForge.SFN_Vector3,id:8126,x:31904,y:32934,varname:node_8126,prsc:2,v1:0,v2:-1,v3:0;n:type:ShaderForge.SFN_Dot,id:8650,x:32098,y:32825,varname:node_8650,prsc:2,dt:1|A-816-OUT,B-8126-OUT;n:type:ShaderForge.SFN_Lerp,id:7135,x:32388,y:32654,varname:node_7135,prsc:2|A-848-RGB,B-3661-RGB,T-7377-OUT;n:type:ShaderForge.SFN_Blend,id:7377,x:32388,y:32898,varname:node_7377,prsc:2,blmd:8,clmp:True|SRC-8650-OUT,DST-3879-OUT;n:type:ShaderForge.SFN_Tex2d,id:8651,x:32342,y:32420,ptovrint:False,ptlb:Main,ptin:_Main,varname:_Main,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,ntxv:0,isnm:False;n:type:ShaderForge.SFN_Blend,id:5996,x:32643,y:32554,varname:node_5996,prsc:2,blmd:0,clmp:True|SRC-8651-RGB,DST-7135-OUT;n:type:ShaderForge.SFN_Slider,id:2949,x:31630,y:33263,ptovrint:False,ptlb:Gradient,ptin:_Gradient,varname:_Gradient,prsc:2,glob:False,taghide:False,taghdr:False,tagprd:False,tagnsco:False,tagnrm:False,min:0,cur:1,max:10;n:type:ShaderForge.SFN_Dot,id:6406,x:31630,y:33076,varname:node_6406,prsc:2,dt:0|A-5749-Y,B-7526-OUT;n:type:ShaderForge.SFN_Vector3,id:7526,x:31445,y:33140,varname:node_7526,prsc:2,v1:0,v2:1,v3:0;n:type:ShaderForge.SFN_FragmentPosition,id:5749,x:31445,y:33003,varname:node_5749,prsc:2;n:type:ShaderForge.SFN_Divide,id:3879,x:32006,y:33151,varname:node_3879,prsc:2|A-6406-OUT,B-2949-OUT;proporder:8651-3661-848-2949;pass:END;sub:END;*/
Shader "Kenney/Gradient" {
Properties {
_Main ("Main", 2D) = "white" {}
_Color ("Color", Color) = (0.5,0.5,0.5,1)
_ColorBottom ("ColorBottom", Color) = (0.5,0.5,0.5,1)
_Gradient ("Gradient", Range(0, 10)) = 1
[HideInInspector]_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}
SubShader {
Tags {
"Queue"="AlphaTest"
"RenderType"="TransparentCutout"
}
LOD 200
Pass {
Name "FORWARD"
Tags {
"LightMode"="ForwardBase"
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDBASE
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#pragma multi_compile_fwdbase_fullshadows
#pragma multi_compile_fog
#pragma only_renderers d3d9 d3d11 glcore gles
#pragma target 3.0
uniform float4 _LightColor0;
uniform float4 _ColorBottom;
uniform float4 _Color;
uniform sampler2D _Main; uniform float4 _Main_ST;
uniform float _Gradient;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalDir : TEXCOORD2;
LIGHTING_COORDS(3,4)
UNITY_FOG_COORDS(5)
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.normalDir = UnityObjectToWorldNormal(v.normal);
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
float3 lightColor = _LightColor0.rgb;
o.pos = UnityObjectToClipPos( v.vertex );
UNITY_TRANSFER_FOG(o,o.pos);
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
float4 frag(VertexOutput i) : COLOR {
i.normalDir = normalize(i.normalDir);
float3 normalDirection = i.normalDir;
float4 _Main_var = tex2D(_Main,TRANSFORM_TEX(i.uv0, _Main));
clip(_Main_var.a - 0.5);
float3 lightDirection = normalize(_WorldSpaceLightPos0.xyz);
float3 lightColor = _LightColor0.rgb;
////// Lighting:
float attenuation = LIGHT_ATTENUATION(i);
float3 attenColor = attenuation * _LightColor0.xyz;
/////// Diffuse:
float NdotL = max(0.0,dot( normalDirection, lightDirection ));
float3 directDiffuse = max( 0.0, NdotL) * attenColor;
float3 indirectDiffuse = float3(0,0,0);
indirectDiffuse += UNITY_LIGHTMODEL_AMBIENT.rgb; // Ambient Light
float3 diffuseColor = saturate(min(_Main_var.rgb,lerp(_ColorBottom.rgb,_Color.rgb,saturate((max(0,dot(i.normalDir,float3(0,-1,0)))+(dot(i.posWorld.g,float3(0,1,0))/_Gradient))))));
float3 diffuse = (directDiffuse + indirectDiffuse) * diffuseColor;
/// Final Color:
float3 finalColor = diffuse;
fixed4 finalRGBA = fixed4(finalColor,1);
UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
return finalRGBA;
}
ENDCG
}
Pass {
Name "FORWARD_DELTA"
Tags {
"LightMode"="ForwardAdd"
}
Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDADD
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#pragma multi_compile_fwdadd_fullshadows
#pragma multi_compile_fog
#pragma only_renderers d3d9 d3d11 glcore gles
#pragma target 3.0
uniform float4 _LightColor0;
uniform float4 _ColorBottom;
uniform float4 _Color;
uniform sampler2D _Main; uniform float4 _Main_ST;
uniform float _Gradient;
struct VertexInput {
float4 vertex : POSITION;
float3 normal : NORMAL;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
float4 pos : SV_POSITION;
float2 uv0 : TEXCOORD0;
float4 posWorld : TEXCOORD1;
float3 normalDir : TEXCOORD2;
LIGHTING_COORDS(3,4)
UNITY_FOG_COORDS(5)
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.normalDir = UnityObjectToWorldNormal(v.normal);
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
float3 lightColor = _LightColor0.rgb;
o.pos = UnityObjectToClipPos( v.vertex );
UNITY_TRANSFER_FOG(o,o.pos);
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
float4 frag(VertexOutput i) : COLOR {
i.normalDir = normalize(i.normalDir);
float3 normalDirection = i.normalDir;
float4 _Main_var = tex2D(_Main,TRANSFORM_TEX(i.uv0, _Main));
clip(_Main_var.a - 0.5);
float3 lightDirection = normalize(lerp(_WorldSpaceLightPos0.xyz, _WorldSpaceLightPos0.xyz - i.posWorld.xyz,_WorldSpaceLightPos0.w));
float3 lightColor = _LightColor0.rgb;
////// Lighting:
float attenuation = LIGHT_ATTENUATION(i);
float3 attenColor = attenuation * _LightColor0.xyz;
/////// Diffuse:
float NdotL = max(0.0,dot( normalDirection, lightDirection ));
float3 directDiffuse = max( 0.0, NdotL) * attenColor;
float3 diffuseColor = saturate(min(_Main_var.rgb,lerp(_ColorBottom.rgb,_Color.rgb,saturate((max(0,dot(i.normalDir,float3(0,-1,0)))+(dot(i.posWorld.g,float3(0,1,0))/_Gradient))))));
float3 diffuse = directDiffuse * diffuseColor;
/// Final Color:
float3 finalColor = diffuse;
fixed4 finalRGBA = fixed4(finalColor * 1,0);
UNITY_APPLY_FOG(i.fogCoord, finalRGBA);
return finalRGBA;
}
ENDCG
}
Pass {
Name "ShadowCaster"
Tags {
"LightMode"="ShadowCaster"
}
Offset 1, 1
Cull Back
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_SHADOWCASTER
#include "UnityCG.cginc"
#include "Lighting.cginc"
#pragma fragmentoption ARB_precision_hint_fastest
#pragma multi_compile_shadowcaster
#pragma multi_compile_fog
#pragma only_renderers d3d9 d3d11 glcore gles
#pragma target 3.0
uniform sampler2D _Main; uniform float4 _Main_ST;
struct VertexInput {
float4 vertex : POSITION;
float2 texcoord0 : TEXCOORD0;
};
struct VertexOutput {
V2F_SHADOW_CASTER;
float2 uv0 : TEXCOORD1;
};
VertexOutput vert (VertexInput v) {
VertexOutput o = (VertexOutput)0;
o.uv0 = v.texcoord0;
o.pos = UnityObjectToClipPos( v.vertex );
TRANSFER_SHADOW_CASTER(o)
return o;
}
float4 frag(VertexOutput i) : COLOR {
float4 _Main_var = tex2D(_Main,TRANSFORM_TEX(i.uv0, _Main));
clip(_Main_var.a - 0.5);
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
}
FallBack "Diffuse"
CustomEditor "ShaderForgeMaterialInspector"
}
@KenneyNL
Copy link
Author

KenneyNL commented Dec 6, 2017

Feel free to copy, use or improve on this shader.

@HugoPB
Copy link

HugoPB commented Apr 11, 2019

Thanks man ;)

@HugoPB
Copy link

HugoPB commented Apr 27, 2019

should i apply this to all world, or just in one object ?

@bstheone
Copy link

Thanks man, great shader.
Just one question: When I move the object up in space, colors changes. It seems like gradient works in world space. How can I make gradient local to object?
(Sorry I don't know much about shader programming)

@quer
Copy link

quer commented Oct 5, 2019

Do you have a version to Threejs?

Copy link

ghost commented Sep 5, 2020

BS6one, just make an .shader file and apply them on new material.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment