Last active
September 18, 2016 23:08
-
-
Save chuckbergeron/a9518c103a93318ffb092c2f4aa87133 to your computer and use it in GitHub Desktop.
Shader code and camera Monobehaviour to render the example on The Book of Shaders pg. 5 in Unity 5.4.1 -- https://thebookofshaders.com/05/ -- https://gist.github.com/chuckbergeron/50adf7f5099bc85a25c9606246aca963
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/PlotShader" | |
{ | |
Properties | |
{ | |
_MainTex ("Texture", 2D) = "white" {} | |
} | |
SubShader | |
{ | |
// No culling or depth | |
Cull Off ZWrite Off ZTest Always | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
struct appdata | |
{ | |
float4 vertex : POSITION; | |
float2 uv : TEXCOORD0; | |
}; | |
struct v2f | |
{ | |
float2 uv : TEXCOORD0; | |
float4 vertex : SV_POSITION; | |
}; | |
v2f vert (appdata v) | |
{ | |
v2f o; | |
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex); | |
o.uv = v.uv; | |
return o; | |
} | |
sampler2D _MainTex; | |
// Plot a line on Y using a value between 0.0-1.0 | |
float plot(fixed2 st, float pct) | |
{ | |
return smoothstep( pct-0.02, pct, st.y) - | |
smoothstep( pct, pct+0.02, st.y); | |
} | |
fixed4 frag (v2f i) : SV_Target | |
{ | |
float2 st = i.vertex.xy/_ScreenParams.xy; | |
float y = st.x; | |
float3 color = float3(y, y, y); | |
// Plot a line | |
float pct = plot(st, y); | |
color = (1.0 - pct) * color + pct * float3(0.0, 1.0, 0.0); | |
return float4(color, 1.0); | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment