-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add shaders * Update documents
- Loading branch information
Showing
6 changed files
with
279 additions
and
43 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
/* | ||
* name: Volume to color saturation mapper | ||
* type: filter | ||
*/ | ||
uniform sampler2D texture; | ||
uniform ivec2 resolution; | ||
|
||
uniform vec3 soundLevel; | ||
|
||
vec3 rgb2hsv(vec3 c) { | ||
// [The Book of Shaders by Patricio Gonzalez Vivo & Jen Lowe](https://thebookofshaders.com/06/) | ||
vec4 K = vec4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0); | ||
vec4 p = mix(vec4(c.bg, K.wz), vec4(c.gb, K.xy), step(c.b, c.g)); | ||
vec4 q = mix(vec4(p.xyw, c.r), vec4(c.r, p.yzx), step(p.x, c.r)); | ||
|
||
float d = q.x - min(q.w, q.y); | ||
float e = 1.0e-10; | ||
return vec3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x); | ||
} | ||
|
||
vec3 hsb2rgb(float h, float s, float v) { | ||
/* | ||
vec4 t = vec4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); | ||
vec3 p = abs(fract(vec3(h) + t.xyz) * 6.0 - vec3(t.w)); | ||
return v * mix(vec3(t.x), clamp(p - vec3(t.x), 0.0, 1.0), s); | ||
*/ | ||
// [[汎用関数]HSV2RGB 関数](https://qiita.com/keim_at_si/items/c2d1afd6443f3040e900) | ||
return ((clamp(abs(fract(h + vec3(0, 2, 1) / 3.) * 6. - 3.) - 1., 0., 1.) - 1.) * s + 1.) * v; | ||
} | ||
|
||
void main() { | ||
vec4 color = texture(texture, gl_FragCoord.xy / resolution); | ||
vec3 hsb = rgb2hsv(color.rgb); | ||
gl_FragColor = vec4(hsb2rgb(hsb.x, hsb.y * soundLevel.z, hsb.z), 1); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/* | ||
* name: ray marching study 201910 | ||
* type: shader | ||
* references: | ||
* - [GLSL SandBoxで手軽にレイマーチングで遊ぼう](https://hackerslab.aktsk.jp/2018/12/01/131928) | ||
* - [魔法使いになりたい人のためのシェーダーライブコーディング入門](https://qiita.com/kaneta1992/items/21149c78159bd27e0860) | ||
* - [Phantom Mode](https://www.shadertoy.com/view/MtScWW) | ||
* - [Live Coding Using Phantom Mode](https://www.shadertoy.com/view/wl2GWG) | ||
* - [distance functions](https://iquilezles.org/www/articles/distfunctions/distfunctions.htm) | ||
*/ | ||
|
||
uniform ivec2 resolution; | ||
uniform float time; | ||
uniform float progress; | ||
uniform vec3 soundLevel; | ||
|
||
uniform float kick; | ||
|
||
const float PI = acos(-1); | ||
const float TWO_PI = PI * 2; | ||
|
||
vec3 hsb2rgb(float h, float s, float b) { | ||
// [[汎用関数]HSV2RGB 関数](https://qiita.com/keim_at_si/items/c2d1afd6443f3040e900) | ||
return ((clamp(abs(fract(h + vec3(0, 2, 1) / 3.) * 6. - 3.) - 1., 0., 1.) - 1.) * s + 1.) * b; | ||
} | ||
|
||
|
||
float sdHexPrism( vec3 p, vec2 h ) | ||
{ | ||
const vec3 k = vec3(-0.8660254, 0.5, 0.57735); | ||
p = abs(p); | ||
p.xy -= 2.0*min(dot(k.xy, p.xy), 0.0)*k.xy; | ||
vec2 d = vec2( | ||
length(p.xy-vec2(clamp(p.x,-k.z*h.x,k.z*h.x), h.x))*sign(p.y-h.x), | ||
p.z-h.y ); | ||
return min(max(d.x,d.y),0.0) + length(max(d,0.0)); | ||
} | ||
|
||
float sdTriPrism( vec3 p, vec2 h ) { | ||
vec3 q = abs(p); | ||
return max(q.z-h.y,max(q.x*0.866025+p.y*0.5,-p.y)-h.x*0.5); | ||
} | ||
|
||
vec3 repeat(vec3 p, float interval) { | ||
return mod(p, interval) - 2.0 * progress; | ||
} | ||
|
||
float dist(vec3 p) { | ||
vec3 pos = repeat(p - 2.0 * progress, 4.0); | ||
return sdHexPrism(pos, vec2(0.5, 2.0 * progress)); | ||
} | ||
|
||
void main() { | ||
vec2 uv = (gl_FragCoord.xy * 2.0 - resolution.xy) / min(resolution.x, resolution.y); | ||
|
||
vec3 cameraUp = normalize(vec3(-sin(progress * progress * 5 * TWO_PI), cos(progress * progress * 5 * TWO_PI), 0.0)); | ||
vec3 cameraDir = vec3(0.0, 0.0, 2.0 * progress); | ||
vec3 cameraSide = normalize(cross(cameraUp, cameraDir)); | ||
vec3 rayDir = normalize((uv.x * cameraSide + uv.y * cameraUp) + cameraDir); | ||
|
||
float t = 0.01; | ||
float ac = 0.0; | ||
for (int i = 0; i < 48; ++i) { | ||
float d = dist(rayDir * t); | ||
d = max(abs(d), 0.02); | ||
ac += exp(-d * 3.0); | ||
t += d * 0.5; | ||
} | ||
|
||
float h = soundLevel.x * ac * 0.2; | ||
float s = ac * 0.02 * soundLevel.y; | ||
float b = ac * 0.02; | ||
gl_FragColor = vec4(hsb2rgb(h, s, b), 1.0); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
/* | ||
* name: ray marching template | ||
* type: shader | ||
* references: | ||
* - [GLSL SandBoxで手軽にレイマーチングで遊ぼう](https://hackerslab.aktsk.jp/2018/12/01/131928) | ||
*/ | ||
|
||
// | ||
// Uniform variables | ||
// | ||
|
||
uniform ivec2 resolution; | ||
|
||
// | ||
// Constants | ||
// | ||
|
||
const float PI = acos(-1); | ||
const float TWO_PI = PI * 2; | ||
const float DELTA = 0.001; | ||
|
||
// | ||
// Types | ||
// | ||
|
||
struct Camera { | ||
vec3 position; | ||
vec3 up; | ||
vec3 direction; | ||
vec3 side; | ||
}; | ||
|
||
struct Ray { | ||
vec3 position; | ||
vec3 direction; | ||
}; | ||
|
||
struct Light { | ||
vec3 direction; | ||
vec3 color; | ||
}; | ||
|
||
// | ||
// Functions | ||
// | ||
|
||
float sdSphere(vec3 pos, float r, vec3 rayPos) { | ||
return length(rayPos - pos) - r; | ||
} | ||
|
||
float calcDistance(vec3 rayPos) { | ||
const vec3 pos = vec3(0.0, 0.0, 3.0); | ||
const float r = 1.0; | ||
return sdSphere(pos, r, rayPos); | ||
} | ||
|
||
vec3 calcNormalVector(vec3 rayPos) { | ||
float d = calcDistance(rayPos); | ||
return normalize( | ||
vec3( | ||
calcDistance(rayPos - vec3(DELTA, 0.0, 0.0)) - d, | ||
calcDistance(rayPos - vec3(0.0, DELTA, 0.0)) - d, | ||
calcDistance(rayPos - vec3(0.0, 0.0, DELTA)) - d | ||
) | ||
); | ||
} | ||
|
||
vec3 calcLuminanceIntensity( | ||
vec3 normalVector, | ||
vec3 pointLightPos, | ||
vec3 pointLightColor | ||
) { | ||
return dot(normalVector, pointLightPos) * pointLightColor; | ||
} | ||
|
||
void main() { | ||
vec2 uv = (gl_FragCoord.xy * 2.0 - resolution.xy) / max(resolution.x, resolution.y); | ||
|
||
// Set camera | ||
Camera camera; | ||
camera.position = vec3(0.0, 0.0, -4.0); | ||
camera.up = normalize(vec3(0.0, 1.0, 0.0)); | ||
camera.direction = normalize(vec3(0.0, 0.0, 1.0)); | ||
camera.side = normalize(cross(camera.up, camera.direction)); | ||
|
||
// Initialize ray | ||
Ray ray; | ||
ray.position = camera.position; | ||
ray.direction = normalize((uv.x * camera.side) + (uv.y * camera.up) + camera.direction); | ||
|
||
// Calc distance between camera and target | ||
float t = 0.0; | ||
const int MAX_STEP = 64; | ||
int step = 0; | ||
while (++step <= MAX_STEP) { | ||
float d = calcDistance(ray.position); | ||
if (d < DELTA) { | ||
break; | ||
} | ||
t += d; | ||
ray.position = camera.position + t * ray.direction; | ||
} | ||
if (MAX_STEP < step) { | ||
gl_FragColor = vec4(0); | ||
return; | ||
} | ||
|
||
// Calc normal vector | ||
vec3 normalVector = calcNormalVector(ray.position); | ||
|
||
// Calc color | ||
Light surfaceLight; | ||
surfaceLight.direction = normalize(vec3(1.0, -1.0, 1.0)); | ||
surfaceLight.color = vec3(1.0, 1.0, 1.0); | ||
vec3 luminanceIntensity = calcLuminanceIntensity( | ||
normalVector, | ||
surfaceLight.direction, | ||
surfaceLight.color | ||
); | ||
|
||
// Render | ||
gl_FragColor = vec4(luminanceIntensity, 1.0); | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,23 @@ | ||
Examples | ||
====== | ||
# Examples | ||
|
||
[shader-study-201910.frag](../data/shader-study-201910.frag) | ||
------ | ||
## [shader-study-202001.frag](../data/shader-study-202001.frag) | ||
|
||
[](https://www.youtube.com/ILZz3aaolQ0) | ||
[ by Sad Juno")](https://www.youtube.com/watch?v=VvSWr_wXQx4) | ||
|
||
[shader-study-201909.frag](../data/shader-study-201909.frag) | ||
------ | ||
## [shader-study-201910.frag](../data/shader-study-201910.frag) | ||
|
||
[ by Sad Juno")](https://www.youtube.com/JOH3b3gxxdA) | ||
[](https://www.youtube.com/watch?v=ILZz3aaolQ0) | ||
|
||
## [shader-study-201909.frag](../data/shader-study-201909.frag) | ||
|
||
[ by Sad Juno")](https://www.youtube.com/watch?v=JOH3b3gxxdA) | ||
|
||
(with other source) | ||
|
||
[shader-study-20190804.frag](../data/shader-study-20190804.frag) | ||
------ | ||
## [shader-study-20190804.frag](../data/shader-study-20190804.frag) | ||
|
||
[")](https://www.youtube.com/qMwQ23na1Ek) | ||
[")](https://www.youtube.com/watch?v=qMwQ23na1Ek) | ||
|
||
[shader-ray-marching-study-01.frag](../data/shader-ray-marching-study-01.frag) | ||
------ | ||
## [shader-ray-marching-study-01.frag](../data/shader-ray-marching-study-01.frag) | ||
|
||
[")](https://www.youtube.com/8bA4d6-tRKE) | ||
[")](https://www.youtube.com/watch?v=8bA4d6-tRKE) |