Sayachang.cginc
Unity向けのcgincを試し書きしたもの。
note: Unityを使っていてシェーダーを勉強し始めた人には、自分用のcgincを作成することが役に立つ。
TODO: このコードスニペットにていねいな日本語の説明を書き足す
https://gyazo.com/414cd28fc5d8d723a26158e7ef897456
code:Sayachang.cginc.HLSL
// const
// 定数
// UNITY_PI
// UnityCG.cgincで定義されているUNITY_PIを利用できる。UNITY_TWO_PIというものもある。
// static float UNITY_PI = 3.141592653589793238;
// static float HALF_PI = 1.57079632679;
// static float HALF_PI = UNITY_PI / 2;
// static float TWO_PI = 6.28318530718;
// static float TWO_PI = 2 * UNITY_PI; // これはUNITY_TWO_PIが使える
// 0.01745329251 // pi / 180
// Golden Ratio
static float PHI = 1.618033988749894848;
static float GOLDEN = UNITY_PI * (3.0-sqrt(5.0));
// 0.4545.. // 1/2.2, RECIPROCAL_GAMMA, 輝度=電圧出力率^1/2.2
static float GAMMA_CORRECTION = 0.45454545;
// bit smaller than 1.0
float cos0067() { return cos(0.067); } // 0.997756339504
// bit larger than 0.0
float cos1569() { return cos(1.569); } // 0.00179632582884
// matrices
// 行列
// 2D rotation matrix
// to use mul(rot2DM(_Time.y), p)
float2x2 rot2DM(float angle)
{
float c = cos(angle);
float s = sin(angle);
return float2x2(c, s, -s, c);
}
// scale matrix
float2x2 scale2DM(float2 scale){
return float2x2(scale.x, 0, 0, scale.y);
}
// YUV to RGB matrix
float3x3 YUV2RGBM = float3x3(1.0, 0.0, 1.13983,
1.0, -0.39465, -0.58060,
1.0, 2.03211, 0.0);
// RGB to YUV matrix
float3x3 RGB2YUVM = float3x3(0.2126, 0.7152, 0.0722,
-0.09991, -0.33609, 0.43600,
0.615, -0.5586, -0.05639);
// basic func
// 関数
// GLSL Noise Algorithms
// note: HLSL has no mod() function, so prepare handmade.
// fmod works same as %. under zero, fmod differs from mod.
float mod(float x, float y)
{
return x - y * floor(x / y);
}
float2 mod(float2 x, float2 y)
{
return x - y * floor(x / y);
}
float3 mod(float3 x, float3 y)
{
return x - y * floor(x / y);
}
float4 mod(float4 x, float4 y)
{
return x - y * floor(x / y);
}
// 1D random numbers
float Rand(float n)
{
return frac(sin(n));
}
// 2D random numbers
float2 Rand2(float2 p)
{
return frac(float2(sin(p.x * 1.32 + p.y * 54.077), cos(p.x * 91.32 + p.y * 9.077)));
}
// 1D noise
float Noise1(float p)
{
float fl = floor(p);
float fc = frac(p);
return lerp(Rand(fl), Rand(fl + 1.0), fc);
}
// 2D noise
float Noise2(float2 p) {
return frac(sin(dot(p.xy, float2(12.9898, 78.233))) * 43758.5453);
}
// Taylor approximation of inverse square root
float4 taylorInvSqrt(float4 r)
{
return 1.79284291400159 - 0.85373472095314 * r;
}
// Hash
float hash1(float n)
{
return frac(sin(n)*43758.5453123);
}
float3 hash3(float n)
{
return frac(sin(n + float3(0.0, 13.1, 31.3))*158.5453123);
}
// mod 289
float3 mod289(float3 x) {
return x - floor(x * (1 / 289)) * 289;
}
float4 mod289(float4 x) {
return x - floor(x * (1 / 289)) * 289;
}
// smooth min
// exponential smooth, k=32
float sminEx(float a, float b, float k)
{
float res = exp2(-k * a) + exp2(-k * b);
return -log2(res) / k;
}
// polynomial smooth, k=0.1
float sminPoly(float a, float b, float k)
{
float h = clamp(0.5 + 0.5 * (b - a) / k, 0.0, 1.0);
return lerp(b, a, h) - k * h * (1.0 - h);
}
// power smooth, k=8
float sminPow(float a, float b, float k)
{
a = pow(a, k);
b = pow(b, k);
return pow((a * b) / (a + b), 1.0 / k);
}
// 2D cross product
// note: HLSL cross is 3D method
float cross2(float2 v1, float2 v2) {
return v1.x * v2.y - v1.y * v2.x;
}
// 3D pow
// wrong?? pow(float3, float) works.
// note: HLSL pow is 1D method
float3 pow3(float3 i, float p)
{
return float3(pow(i.x, p), pow(i.y, p), pow(i.z, p));
}
// maxcomp
float maxcomp(float2 v)
{
return max(v.x, v.y);
}
float maxcomp(float3 v)
{
return max(max(v.x, v.y), v.z);
}
// transform
// 移動・回転・拡大縮小
// Translate
float2 translate()
{
return float2(cos(_Time.y), sin(_Time.y));
}
// Rotate
float2 rot(float2 p, float a)
{
return float2(p.x * cos(a) - p.y * sin(a), p.x * sin(a) + p.y * cos(a));
}
float3 rotate(float3 p, float3 v, float a)
{
float4 q = float4(sin(a / 2.) * v, cos(a / 2.)); // quaternion
return cross(cross(p, q.xyz) - q.w * p, q.xyz) * 2. + p;
}
// Zoom
float2 zoom(float2 p, float2 z)
{
return float2(p * z); // float2(p.x*z.x, p.y*z.y) or mul(float2x2(z.x,0,0,z.y), p)
}
// time after time
// 時間
// time multiplier
float sinTimer()
{
return sin(_Time.y);
}
// 0 to 1 sine curve
// sin(x) * 0.5 + 0.5;
float sinHalfTimer()
{
return sin(_Time.y) * 0.5 + 0.5;
}
// draw simple colours
// 色
// Gray
fixed4 Gray(float c) {
return fixed4(c.xxx, 1);
}
// Red
fixed4 Red(float c) {
return fixed4(c, 0, 0, 1);
}
// alters RGB
fixed4 Colorful(float c) {
return fixed4(c * sinTimer(), -c * sinTimer(), c * sinTimer(), 1);
}
// figures
// 形
// draw round
// to call
// float _Rad;
// float c = Round2(i.uv, _Rad);
// return Gray(c);
float Round2(float2 p, float rad)
{
float2 st = p - 0.5;
return step(distance(st, 0), rad);
}
// to call
// return Gray(HaloRing((2*i.uv-1), 0.8 , 3.));
float HaloRing(float2 p, float r, float s)
{
return 1.2-(length(1.-length(abs(p*s))))-r;
}
float dSphere(float3 p, float s)
{
return length(p) - s;
}
// draw line
// to call
// float c = step(Liner2(i.uv, a, b, 0.01), 0.0001);
// return Gray(c);
float Liner2(float2 p, float2 a, float2 b, float r)
{
float2 pa = p - a;
float2 ba = b - a;
float h = clamp(dot(pa, ba) / dot(ba, ba), 0., 1.);
return length(pa - ba * h ) - r;
}
// Rectangle
// to call
// return Gray(dRectangle(i.uv, 0.3), 1);
float dRectangle(float2 p, float size) {
float2 st = p - 0.5;
return step(max(abs(st.x), abs(st.y)), size);
}
// Box
float dBox(float2 p, float2 b) {
return max(abs(p.x) - b.x, abs(p.y) - b.y);
}
// Torus
float dTorus(float3 p, float2 t)
{
return length(float2(length(p.xz) - t.x, p.y)) - t.y;
}
// Ellipsoid
// to call
// return scgColorful2(Ellipsoid((i.uv), float2(0.4, 0.6), float2(0.2, 0.1)));
float Ellipsoid(float2 p, float2 c, float2 r)
{
return (length((p - c) / r) - 1.0) * min(r.x, r.y);
}
// Ellipsoid(3D)
// to call
// return Colorful2(Ellipsoid3(float3(i.uv, abs(sin(_Time.z))), float3(0.4, 0.6, 0.3), float3(0.2, 0.1, 0.1)));
float Ellipsoid3(float3 p, float3 c, float3 r)
{
return (length((p - c) / r) - 1.0) * min(min(r.x, r.y), r.z);
}
// Triangle
// to call
// return float4(Triangle(i.uv).xxx, 1);
float Triangle(float2 p){
float2 st = p - 1.;
st = rot(st, UNITY_PI / 6.);
// Number of sides of your shape
int N = 3;
// Angle and radius from the current pixel
float a = atan2(st.y, st.x) + UNITY_PI;
float r = 2. * UNITY_PI / float(N);
// Shaping function that modulate the distance
float d = cos(floor(0.5 + a / r) * r - a) * length(st);
return 1.0 - smoothstep(0.4, 0.41, d);
}
// Rotating Triangle
// to call
// return Colorful3(RotateTriangle(2 * (i.uv)));
float RotateTriangle(float2 p){
float2 st = p - 1;
st = rot(st, UNITY_PI / 6 + sin(_Time.y));
// Number of sides of your shape
int N = 3;
// Angle and radius from the current pixel
float a = atan2(st.y, st.x) + UNITY_PI;
float r = 2 * UNITY_PI / float(N);
// Shaping function that modulate the distance
float d = cos(floor(0.5 + a / r) * r - a) * length(st);
return 1.0 - smoothstep(0.4, 0.41, d);
}
float dPolygon(float2 p, int vs) {
float r = UNITY_TWO_PI / float(vs);
float2 st = p - 0.5;
float a = atan2(st.x, st.y) + UNITY_PI;
return cos(floor(0.5+a/r)*r-a)*length(st);
}
code:HLSL
// to make polygon
// return step(dPolygon(i.uv, 5), 0.2);
https://gyazo.com/362be511b3bb37b74385f8abcc16e380
code:HLSL
// Pentagram
float dPentagram(float2 p, float r) {
float2 st = 20 * (p - 0.5);
float d = 1;
for (int i=0; i<5; i++) {
float rad1 = TWO_PI * i / 5;
float rad2 = TWO_PI * (i+2) / 5;
float2 v1 = float2(cos(rad1), sin(rad1)) * r;
float2 v2 = float2(cos(rad2), sin(rad2)) * r;
d = min(d, scgLiner2(st, v1, v2, 0.2));
}
return d;
}
code:HLSL
// to make pentagram
// return scgPentagram(i.uv, 8);
https://gyazo.com/d080182846f9abba94eeb3928d2eda75
code:HLSL
// Star
float innerTriangle(float2 p, float2 v1, float2 v2, float2 v3) {
float c1 = cross2(v2-v1, p-v1);
float c2 = cross2(v3-v2, p-v2);
float c3 = cross2(v1-v3, p-v3);
if ((c1>0.0&&c2>0.0&&c3>0.0) || (c1<0.0&&c2<0.0&&c3<0.0))
return 1.;
return 0.;
}
float Star5(float2 p) {
float2 st = 20. * (p - 0.5);
float d = 1.;
for (int i=0; i<5; i++) {
float rad1 = TWO_PI * float(i) / 5.;
float rad2 = TWO_PI * float(i+2.) / 5.;
float2 v1 = float2(cos(rad1), sin(rad1)) * 8.;
float2 v2 = float2(cos(rad2), sin(rad2)) * 8.;
float flg = innerTriangle(st, float2(0., .0), v1, v2);
d = min(d, Liner2(st, v1, v2, 0.01) * (1. - 2. * flg));
}
return d;
}
code:HLSL
// to make star
// return step(Star5(i.uv), 0.4);
https://gyazo.com/3119a1934fae3d2a3cbcfd32218baea2
code:HLSL
// Heart
float Heart(float2 p) {
float2 st = float2(2., 2.6) * p - 1.;
return step(st.x * st.x + pow(st.y - sqrt(abs(st.x)), 2.), 0.7);
}
code:HLSL
// Octahedron
// p is coord float3(x, y, z)
float od = dot(p, normalize(sign(p))) - 0.15;
// lighting(de)
g += 0.0001/(0.0001+d*d);
// then fixed color
return fixed4(col + g * 0.01, 1.0);
https://gyazo.com/4788bd3eb61e30403324cb1c380185fd
https://gyazo.com/311644c876ddfe8cfd5d221200474044
code:HLSL
// Bezier
float det(float2 a, float2 b) { return a.x*b.y - b.x*a.y; }
float3 closest(float2 b0, float2 b1, float2 b2)
{
float a = det(b0, b2);
float b = 2.0*det(b1, b0);
float d = 2.0*det(b2, b1);
float f = b * d - a * a;
float2 d21 = b2 - b1;
float2 d10 = b1 - b0;
float2 d20 = b2 - b0;
float2 gf = 2.0*(b*d21 + d * d10 + a * d20); gf = float2(gf.y, -gf.x);
float2 pp = -f * gf / dot(gf, gf);
float2 d0p = b0 - pp;
float ap = det(d0p, d20);
float bp = 2.0*det(d10, d0p);
float t = clamp((ap + bp) / (2.0*a + b + d), 0.0, 1.0);
return float3(lerp(lerp(b0, b1, t), lerp(b1, b2, t), t), t);
}
float4 bezier(float3 a, float3 b, float3 c, float3 p)
{
float3 w = normalize(cross(c - b, a - b));
float3 u = normalize(c - b);
float3 v = (cross(w, u));
float2 a2 = float2(dot(a - b, u), dot(a - b, v));
float2 b2 = float2(0, 0);
float2 c2 = float2(dot(c - b, u), dot(c - b, v));
float3 p3 = float3(dot(p - b, u), dot(p - b, v), dot(p - b, w));
float3 cp = closest(a2 - p3.xy, b2 - p3.xy, c2 - p3.xy);
return float4(sqrt(dot(cp.xy, cp.xy) + p3.z*p3.z), cp.z, length(cp.xy), p3.z);
}
code:HLSL
// colour
// rgb to gray
// to use
// return RGB2GRAY(scgRGB2GRAY(col) * _GrayMultiplier);
float RGB2GRAY(float3 c) {
return c.x*c.x + c.y*c.y + c.z*c.z;
}
// HSV
// HUE to RGB
float3 HUE2RGB(float h)
{
float r = abs(h * 6 - 3) - 1;
float g = 2 - abs(h * 6 - 2);
float b = 2 - abs(h * 6 - 4);
return saturate(float3(r, g, b));
}
float3 HUE2RGB6(float h, float s)
{
float r = step(abs(h * 6 - 3) - 1, s);
float g = step(2 - abs(h * 6 - 2), s);
float b = step(2 - abs(h * 6 - 4), s);
return saturate(float3(r, g, b));
}
code:HLSL
// to call HUE2RGB6
//float s = frac(i.uv.x);
//return fixed4(HUE2RGB6(s, 0.45 + 0.1 * sin(_Time.z)), 1);
https://gyazo.com/b7946f7f8230aa715a1458ccc5056fea
code:HLSL
// HSV to RGB
float3 HSV2RGB(float3 hsv)
{
float3 col = HUE2RGB(hsv.x);
return ((col - 1) * hsv.y + 1) * hsv.z;
}
float3 HSV2RGB2(float3 c) {
float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * lerp(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y);
}
code:HLSL
// to call HSV2RGB
// float3 c = float3(i.uv.x, scgCos0067(), 1);
// to scroll
// float s = frac(i.uv.x + _Time.y);
// float3 c = float3(s, scgCos0067(), 1);
// return fixed4(HSV2RGB(c), 1);
https://gyazo.com/078a4eacb145041f36f3aa959d114af5
code:HLSL
// BMY Color
// t: _Time.y * multipllier
// s: 1 - sin(_Time.y * multiplier2) * 0.5
float3 BlueSpecial(float t, float s) {
float3 color;
float wavval = 0.05 * ((sin(t * 0.0575) + 1.0) / 2.0) + 0.60;
color = hsv(wavval, 0.88, s);
return color;
}
float3 MagentaSpecial(float t, float s) {
float3 color;
float wavval = 0.05 * ((sin(t * 0.0575) + 1.0) / 2.0) + 0.90;
color = hsv(wavval, 0.88, s);
return color;
}
float3 YellowSpecial(float t, float s) {
float3 color;
float wavval = 0.05 * ((sin(t * 0.0575) + 1.0) / 1.0) + 0.1;
color = hsv(wavval, 1.48, s);
return color;
}
code:HLSL
// effects
// エフェクト
// pulse like distortion
// TODO: FIXME
//float _PulseMultiplier;
float2 scgPulse2(float2 uv, float m) {
float2 oxy = float2(uv * m);
float x = uv.x + m * sin(_Time.x * m * 50) * (1 - frac(oxy.y) * frac(oxy.y));
float y = uv.y + m * sin(_Time.x * m * 50) * (1 - frac(oxy.x) * frac(oxy.x));
return float2(x, y);
}
code:HLSL
// how to morph
// float3 figure1 = getFigure1();
// float3 figure2 = getFigure2();
// return min(figure1, fugure2); // replace min to smooth minimun
code:HLSL
// what is UV scroll
// uv += _Time.x; // uv is texture coord
code:HLSL
// how to repeat
// float2 st = frac(i.uv * _Repeat);
// or
// float d = de(p);
// abs(sin(d * _Multiplier))
// complex
// 組み合わせ
code:HLSL
// Circles
// to call
// float2 st = frac((i.uv - 0.5) * 10 * scgTimer());
// return scgGray(scgCircles2(frac(st * scgTimer()), 5, 0.3));
float scgCircles2(float2 p, float n, float rad)
{
float2 st = p - 0.5;
return step(sin(distance(st, 0) * n), rad);
}
float scgCircles3(float2 p, float n, float rad)
{
float2 st = p - 0.5;
return step(abs(cos(distance(st, 0) * n)), rad);
}
float scgCircles4(float2 p, float n, float rad)
{
float2 st = p - 0.5 + float2(scgTimer() * sin(_Time.x), -scgTimer() * cos(_Time.y));
return step(abs(cos(distance(st, 0) * n)), rad);
}
// n = 5
https://gyazo.com/3057ebdf21d63c84c36eec6ae372ca26
// n = 50
https://gyazo.com/466f1e08854e8a21170c6cbac0de9c53
// alters colors
// return scgColorful(scgRounds2(frac(st * scgTimer()), 50, 0.3));
// return scgColorful2(scgCircles4(st, 20, 0.3));
code:HLSL
/*
// mix1
fixed4 frag (v2f i) : SV_Target
{
float2 st = frac((0.5 - i.uv + float2(scgHalfTimer(), 2 * scgHalfTimer())) * 5 * scgTimer());
float4 h = float4(scgHeart(st), 0, 0, 1);
float4 c = scgColorful3(scgCircles4(i.uv, 50 * scgHalfTimer(), 0.4));
return scgMorph4(h, c);
}
*/
code:HLSL
// mix2
fixed4 frag (v2f i) : SV_Target
{
float2 qr = i.uv + scgTranslate();
float2 st = frac((0.5 - qr + float2(scgHalfTimer(), 2 * scgHalfTimer())) * 5 * scgTimer());
float4 h = float4(scgHeart(st), 0, 0, 1);
float4 c = scgColorful3(scgCircles4(i.uv, 50 * scgHalfTimer(), 0.4));
float4 t = scgColorful3(scgRotateTriangle(3 * i.uv + scgTranslate()));
float4 f = scgColorful2(step(scgPentagram(i.uv - 0.5, 0.1), 0.1));
float4 s = scgColorful3(step(starPolygon(i.uv - 0.5, 5, 2, 0.1), 0.2));
return schInvertPeriods(scgMorph4(scgMorph4(scgMorph4(scgMorph4(h, c), t), f), s));
}
code:HLSL
// Coloured tiles
fixed4 scgColouredTiles(float2 p)
{
return fixed4(1, frac(p.x * 5) * sin(_Time.x * 200), frac(p.y * 10) * sin(_Time.x * 50), 1);
}
https://gyazo.com/baed9a2bb0d6f35b27e38b5b4d587e8d
code:HLSL
// Macedonia
fixed4 scgMacedonia(float2 p)
{
float uv = 0.5 - p;
float a = atan2(p.y, p.x) + _Time.x * sin(_Time.y); // speed increases
return fixed4(1, step(sin(a * 10), 0.01), 0, 1);
}
https://gyazo.com/18c3645861b0ee6e42d92ba44de9ea06
code:HLSL
// Invert
fixed4 scgInvert(fixed4 col)
{
return 1 - col;
}
https://gyazo.com/f154df707c7b255d1bd9be09bb586f4a
code:HLSL
// Mosaic
float2 scgMosaic(float2 p, float mul)
{
float m = mul * scgTimer();
float2 st = floor(p * m) / m;
return st;
}
https://gyazo.com/b335ab5d6eecf88e40aca64cf1ef67db
code:HLSL
// Smooth Mosaic
fixed4 scgSmoothMosaic(sampler2D tex, float2 p, float mul)
{
float d1 = 1 / mul;
float d2 = 1 / mul / mul;
float2 xy = scgMosaic(p, mul);
fixed4 col = fixed4(0, 0, 0, 0);
for (float i = 0.0; i < d1; i += d2) {
for (float j = 0.0; j < d1; j += d2) {
col += tex2D(tex, float2(xy.x + i, xy.y + j));
}
}
col *= d2;
return col;
}
code:HLSL
// RGB Shift
fixed4 frag (v2f i) : SV_Target
{
float t = _Time.y;
fixed4 col = tex2D(_MainTex, i.uv);
col.r = tex2D(_MainTex, i.uv + float2(0.1 * abs(sin(t)), 0));
col.b = tex2D(_MainTex, i.uv - float2(0.1 * abs(sin(t)), 0));
return col;
}
https://gyazo.com/da3a7d3c6bdbbeca9b13aa1ff40f3fd8
code:HLSL
float rand(float2 co) {
return frac(sin(dot(co.xy, float2(12.9898, 78.233))) * 43758.5453);
}
// Strip
float2 p = i.uv;
p.x += rand(float2(floor(p.y * 5), 0));
https://gyazo.com/7c269117daf88d2ed352b1dc1c4fd4f1
code:HLSL
float2 p = i.uv;
p.x += rand(float2(floor(p.y * 5), 0));
p.x += rand(float2(floor(p.x * 5), 0));
p.y += rand(float2(floor(p.x * 5), 0));
p.y += rand(float2(floor(p.y * 5), 0));
https://gyazo.com/58578ebe46f42bc150a656f19355fa0b
code:HLSL
//multiply color and shape
//float s = frac(i.uv.x + frac(_Time.z));
//float4 hue = float4(scgHUE2RGB6(s, 0.45), 1);
//float4 h = float4(scgHeart(i.uv).xxx, 1);
//return h * hue;
code:HLSL
// Morph
float scgMorph(float a, float b) {
return lerp(a, b, sin(_Time.y) * 0.5 + 0.5);
}
code:HLSL
// Dissolve
float4 scgDissolve(float4 c, float d) {
clip(c.rgb - d);
return c;
}
float4 scgDissolveAnim(float4 c) {
clip(c.rgb - scgHalfTimer());
return c;
}
code:HLSL
// pseudo kleinian
float PseudoKleinian(float3 p) {
float3 CSize = float3(0.92436, 0.90756, 0.92436);
float size = 1;
float3 c = float3(0, 0, 0);
float de = 1;
float3 offset = float3(0, 0, 0);
float3 ap = p + 1;
for(int i=0; i<10; i++)
{
ap = p;
p -= 2 * clamp(p, -CSize, CSize);
float r2 = dot(p, p);
float k = max(size / r2, 1);
p *= k;
de *= k;
p += c;
}
float r = abs(0.5 * abs(p.z - offset.z) / de);
return r;
}
code:HLSL
// voronoi distance noise, based on iq's articles
float Voronoi(float2 x)
{
float2 p = floor(x);
float2 f = frac(x);
float2 res = float2(8, 8);
for(int j = -1; j <= 1; j ++)
{
for(int i = -1; i <= 1; i ++)
{
float2 b = float2(i, j);
float2 r = b - f + Rand2(p + b);
// chebyshev distance, one of many ways to do this
float d = max(abs(r.x), abs(r.y));
if(d < res.x)
{
res.y = res.x;
res.x = d;
}
else if(d < res.y)
{
res.y = d;
}
}
}
return res.y - res.x;
}
code:HLSL
// to call voronoi
float o = 0.25;
float3 r = float3(o, 0, 0);
float3 g = float3(0, o, 0);
float3 b = float3(0, 0, o);
float v = scgVoronoi(10 * i.uv);
float3 col = (0, 0, 0);
col = col + step(v, 0.01) * b + step(v, 0.1) * g + step(v, 0.3) * r;
col = col + step(v, 0.01) * b + step(v, 0.1) * g + step(v, 0.3) * r;
col = col + step(v, 0.4) * b + step(v, 0.5) * g + step(v, 0.6) * r;
col = col + step(v, 0.7) * b + step(v, 0.8) * g + step(v, 0.9) * r;
return fixed4(col, 1);
https://gyazo.com/69e2422ffafed6d3504a745029e5afe8
// affects cloth
// 布
code:HLSL
// インスペクターでチェックをつけるとメッシュを非表示にする
// ShaderLABのプロパティ
Properties
{
_MainTex ("Texture", 2D) = "white" {}
}
// HLSL変数の宣言
sampler2D _MainTex;
float4 _MainTex_ST;
float _ClipMesh;
// フラグメントシェーダー
// チェックを入れておくと、clipでピクセルが破棄される
fixed4 frag (v2f i) : SV_Target
{
clip(-_ClipMesh); // ここで処理が中断されて、以降のテクスチャの処理に進まないのでメッシュが非表示になる
fixed4 col = tex2D(_MainTex, i.uv);
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
https://gyazo.com/9ef19e5bbac8d35dc191b14a02656b3a
https://gyazo.com/4263eaab1662debfe31af531107cb1d3
https://gyazo.com/b38989c3c492deda9a1512ee9b8692c0
code:HLSL
// vertex shader
v2f vert (appdata v)
{
v2f o;
float3 n = normalize(v.normal);
v.vertex.xyz += n * sin(_Time.z);
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
// dissolve and burnt edge
https://gyazo.com/f2be5cb20392651d5e57cd4875d2621f
code:HLSL
// TODO: FIXME
/*
fixed4 col = saturate(tex2D(_MainTex, i.uv) + _Amount);
float whiteness = col.r + col.g + col.b;
col *= step(whiteness, 0.5);
UNITY_APPLY_FOG(i.fogCoord, col);
return col = 1 - col;
//saturate(col.r + col.g + col.b) * 0.5;
//whiteness = step(saturate(col.r + col.g + col.b), 0.999999);
//return fixed4 (whiteness.rrr ,1);
*/
// raymarching
code:HLSL
// Folding
// 2D folding
// to call
//fixed4 frag (v2f i) : SV_Target
//{
//float2 p = Fold2D(2 * (i.uv - 0.5));// Y軸方向を中心に空間(p)を折りたたむ
//p -= float2(0.4, 0.0);// 右方向に平行移動
//float color = sign(p);// 距離関数の符号で色分け
//return fixed4(p.xyx * color, 1);
//}
float2 Fold2D(float2 p) {
p.x = abs(p.x);
return p;
}
// 3D folding
// to call
//fixed4 frag (v2f i) : SV_Target
//{
// // 3D tree
// float3 p = float3(2 * (i.uv - 0.5), sin(_Time.y));
// //float d = Tree(p);
// float d = RecurseTree(p);
// float color = sign(d);
// return fixed4((1 - d).xxx * color, 1);
//}
float3 Fold3D(float3 p) {
p.x = abs(p.x);
return p;
}
float Box(float3 p, float3 b)
{
float3 d = abs(p) - b;
return length(max(d,0.0))
+ min(max(d.x,max(d.y,d.z)),0.0); // remove this line for an only partially signed sdf
}
float Tree(float3 p) {
float3 size = float3(0.1, 1.0, 0.1);
float d = Rectangle(p, size);
p = Fold3D(p);
p.y -= 0.1;
p.xy = mul(Rotate2DM(-1.2), p.xy);
d = min(d, Rectangle(p, size));
return d;
}
float RecurseTree(float3 p) {
float scale = 0.8;
float3 size = float3(0.1, 1.0, 0.1);
float d = Box(p, size);
for (int i = 0; i < 256; i++) {
float3 q = Fold3D(p);
q.y -= 0.1;
q.xy = mul(Rotate2DM(-1.2), q.xy);
d = min(d, Box(p, size));
p = q;
size *= scale;
}
return d;
}
code:HLSL
// Menger
float dMenger(float3 z0, float3 offset, float scale) {
float4 z = float4(z0, 1);
for (int i = 0; i < 5; i++) {
z = abs(z);
if (z.x < z.y) z.xy = z.yx;
if (z.x < z.z) z.xz = z.zx;
if (z.y < z.z) z.yz = z.zy;
z *= scale;
z.xyz -= offset * (scale - 1.0);
if (z.z < -0.5 * offset.z * (scale - 1.0)) {
z.z += offset.z * (scale - 1.0);
}
}
return length(max(abs(z.xyz), 0)) / z.w;
}
code:HLSL
// Distance Estimated 3D Fractals (Part I) - Syntopia
float trace(float3 from, float3 direction) {
float totalDistance = 0.0;
int steps;
for (steps = 0; steps < MaximumRaySteps; steps++) {
float3 p = from + totalDistance * direction;
float distance = DistanceEstimator(p);
totalDistance += distance;
if (distance < MinimumDistance) break;
}
return 1.0-float(steps) / float(MaxRaySteps);
}
// Mandelbulb