added shaders

This commit is contained in:
gbrochar 2020-11-20 20:22:47 +01:00
parent 158a659dbe
commit dec067d532
21 changed files with 1409 additions and 4 deletions

4
.gitignore vendored
View File

@ -3,9 +3,11 @@
*.swo
*~
# build and dev
# build and dev deprecated
build
dev
# new build dir
dist
# Logs
logs

5
package-lock.json generated
View File

@ -4807,6 +4807,11 @@
"resolved": "https://registry.npmjs.org/setprototypeof/-/setprototypeof-1.1.1.tgz",
"integrity": "sha512-JvdAWfbXeIGaZ9cILp38HntZSFSo3mWg6xGcJJsd+d4aRMOqauag1C63dJfDw7OaMYwEbHMOxEZ1lqVRYP2OAw=="
},
"shadertoy-react": {
"version": "1.1.1",
"resolved": "https://registry.npmjs.org/shadertoy-react/-/shadertoy-react-1.1.1.tgz",
"integrity": "sha512-svmsR7gWbsi4WKMJtkHvhegyuXZpz3QoBUgnmaUe2WVG8ICSLPGDRvh3bhlXi/ofe+h7i7mV75twV5NAoZDHvg=="
},
"shallow-clone": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/shallow-clone/-/shallow-clone-3.0.1.tgz",

View File

@ -64,6 +64,7 @@
"ejs": "^3.1.5",
"express": "^4.17.1",
"react": "^17.0.1",
"react-dom": "^17.0.1"
"react-dom": "^17.0.1",
"shadertoy-react": "^1.1.1"
}
}

37
public/css/style.css Normal file
View File

@ -0,0 +1,37 @@
html {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
body {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
overflow: hidden;
}
.shader {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
.tile {
display: inline-block;
vertical-align: top;
padding: 0;
margin: 0;
height: 25%;
width: 25%;
}
#root {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}

View File

@ -0,0 +1,13 @@
import React from 'react';
// @ts-ignore
import ShadertoyReact from 'shadertoy-react';
// @ts-ignore
import fs from '../shaders/baseRaymarching';
/**
* @return {jsx} a shader
*/
export default function BaseRaymarching() {
return (
<ShadertoyReact fs={fs} />
);
};

View File

@ -0,0 +1,14 @@
import React from 'react';
// @ts-ignore
import ShadertoyReact from 'shadertoy-react';
// @ts-ignore
import fs from '../shaders/collapse';
/**
* @return {jsx} a shader
*/
export default function Collapse() {
return (
<ShadertoyReact fs={fs} />
);
};

View File

@ -0,0 +1,14 @@
import React from 'react';
// @ts-ignore
import ShadertoyReact from 'shadertoy-react';
// @ts-ignore
import fs from '../shaders/droplets';
/**
* @return {jsx} a shader
*/
export default function Droplets() {
return (
<ShadertoyReact fs={fs} />
);
};

View File

@ -0,0 +1,14 @@
import React from 'react';
// @ts-ignore
import ShadertoyReact from 'shadertoy-react';
// @ts-ignore
import fs from '../shaders/inception';
/**
* @return {jsx} a shader
*/
export default function Inception() {
return (
<ShadertoyReact fs={fs} />
);
};

View File

@ -0,0 +1,15 @@
import React from 'react';
// @ts-ignore
import ShadertoyReact from 'shadertoy-react';
// @ts-ignore
import fs from '../shaders/menstom';
/**
* @return {jsx} a shader
*/
export default function Menstom() {
return (
<ShadertoyReact fs={fs} />
);
};

View File

@ -1,10 +1,25 @@
import React from 'react';
import Shader from './Shader';
/**
* @return {jsx} The root component
*/
export default function Root() {
const Shaders = [
'BaseRaymarching',
'Collapse',
'Droplets',
'Inception',
'Mentstom',
'Torus',
'Triangle',
];
return (
<div>Hello World !</div>
<div id='root'>
<div className='tile'><Shader name={Shaders[0]} /></div>
<div className='tile'><Shader name={Shaders[1]} /></div>
<div className='tile'><Shader name={Shaders[2]} /></div>
</div>
);
}

View File

@ -0,0 +1,48 @@
import React from 'react';
// @ts-ignore
import Inception from './Inception';
// @ts-ignore
import Droplets from './Droplets';
// @ts-ignore
import Triangle from './Triangle';
// @ts-ignore
import BaseRaymarching from './BaseRaymarching';
// @ts-ignore
import Menstom from './Menstom';
// @ts-ignore
import Collapse from './Collapse';
// @ts-ignore
import Torus from './Torus';
interface ShaderProps {
name: string;
}
/**
* @return {jsx} a shader
*/
export default function Shader({name}: ShaderProps) {
let Shader: any = name;
if (name == 'Inception') {
Shader = <Inception />;
} else if (name == 'Droplets') {
Shader = <Droplets />;
} else if (name == 'Triangle') {
Shader = <Triangle />;
} else if (name == 'BaseRaymarching') {
Shader = <BaseRaymarching />;
} else if (name == 'Menstom') {
Shader = <Menstom />;
} else if (name == 'Collapse') {
Shader = <Collapse />;
} else if (name == 'Torus') {
Shader = <Torus />;
}
return (
<div className='shader'>
{Shader}
</div>
);
}

View File

@ -0,0 +1,14 @@
import React from 'react';
// @ts-ignore
import ShadertoyReact from 'shadertoy-react';
// @ts-ignore
import fs from '../shaders/torus';
/**
* @return {jsx} a shader
*/
export default function Torus() {
return (
<ShadertoyReact fs={fs} />
);
};

View File

@ -0,0 +1,14 @@
import React from 'react';
// @ts-ignore
import ShadertoyReact from 'shadertoy-react';
// @ts-ignore
import fs from '../shaders/triangle';
/**
* @return {jsx} a shader
*/
export default function Triangle() {
return (
<ShadertoyReact fs={fs} />
);
};

View File

@ -0,0 +1,77 @@
export default `#define MAX_STEPS 1280
#define MAX_DIST 1280.
#define E 0.00001
#define PI 3.141592
vec3 rotX(vec3 v, float a) {
return vec3(v.x, v.y * cos(a) - v.z * sin(a), v.y * sin(a) + v.z * cos(a));
}
vec3 rotY(vec3 v, float a) {
return vec3(v.x * cos(a) + v.z * sin(a), v.y, v.z * cos(a) - v.x * sin(a));
}
vec3 rotZ(vec3 v, float a) {
return vec3(v.x * cos(a) - v.y * sin(a), v.x * sin(a) + v.y * cos(a), v.z);
}
float sdfSphere(vec3 p, float r) {
return length(p) - r;
}
float scene(vec3 p) {
int index = -1;
float d = 10000.;
float sd1 = sdfSphere(p, 1.);
d = min(sd1, d);
return d;
}
vec2 march(vec3 ro, vec3 rd) {
float dO = 0.;
float mind = 10000.;
float index;
for (int steps = 0; steps < MAX_STEPS; steps++) {
vec3 p = ro + rd * dO;
float dS = scene(p);
if (dS < mind)
mind = dS;
dO += dS;
if (dS < E || dO > MAX_DIST)
break;
}
return vec2(dO, mind);
}
vec3 normal(vec3 p) {
float d = scene(p);
vec2 e = vec2(0.001, 0);
vec3 n = d - vec3(
scene(p - e.xyy),
scene(p - e.yxy),
scene(p - e.yyx));
return (normalize(n));
}
float light(vec3 p, vec3 n) {
vec3 lightPos = vec3(-5. * sin(iTime * 4.), -5. * sin(iTime * 3.), -5. * cos(iTime * 2.));
vec3 l = normalize(lightPos - p);
float dif = clamp(dot(l, n), 0., 1.);
vec2 d = march(p+n*E * 30., l);
if (d.x < length(lightPos - p))
dif *= 0.1;
return (dif);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord ) {
vec2 uv = ((fragCoord - .5 * iResolution.xy) / iResolution.y);
vec3 ro = vec3(0., 0., -4.);
vec3 rd = normalize(vec3(uv.x, uv.y, 1.));
vec3 col = vec3(0);
vec2 d = march(ro, rd);
vec3 p = ro + rd * d.x;
vec3 n = normal(p);
p += n * E * 10.;
float dif = light(p, n);
col += vec3(dif);
fragColor = vec4(col, 1.0);
}`;

View File

@ -0,0 +1,204 @@
export default `
#define MAX_STEPS 1280
#define MAX_DIST 1280.
#define E 0.01
#define PI 3.141592
#define MYTIME iTime * 1.
// Turn to 1 to activate displacement mapping
#define DISPLACEMENT_MAPPING 1
vec3 rotX(vec3 v, float a)
{
return vec3(v.x, v.y * cos(a) - v.z * sin(a), v.y * sin(a) + v.z * cos(a));
}
vec3 rotY(vec3 v, float a)
{
return vec3(v.x * cos(a) + v.z * sin(a), v.y, v.z * cos(a) - v.x * sin(a));
}
vec3 rotZ(vec3 v, float a)
{
return vec3(v.x * cos(a) - v.y * sin(a), v.x * sin(a) + v.y * cos(a), v.z);
}
vec3 modSDF(vec3 p, vec3 c)
{
return mod(p + 0.5 * c, c) - 0.5 * c;
}
vec3 modSDFlim( in vec3 p, in float c, in vec3 l)
{
return p-c*vec3(clamp(floor(p.x/c + 0.5),-l.x,l.x), clamp(floor(p.y/c + 0.5),-l.y,l.y), clamp(floor(p.z/c + 0.5),-l.z,l.z));
}
vec3 twistSDF(in vec3 p, float k )
{
float c = cos(k*p.y);
float s = sin(k*p.y);
mat2 m = mat2(c,-s,s,c);
vec3 q = vec3(m*p.xz,p.y);
return q;
}
float sdfBox( vec3 p, vec3 b )
{
vec3 q = abs(p) - b;
return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
}
// blackbody by aiekick : https://www.shadertoy.com/view/lttXDn
// -------------blackbody----------------- //
// return color from temperature
//http://www.physics.sfasu.edu/astro/color/blackbody.html
//http://www.vendian.org/mncharity/dir3/blackbody/
//http://www.vendian.org/mncharity/dir3/blackbody/UnstableURLs/bbr_color.html
vec3 blackbody(float Temp)
{
vec3 col = vec3(255.);
col.x = 56100000. * pow(Temp,(-3. / 2.)) + 148.;
col.y = 100.04 * log(Temp) - 623.6;
if (Temp > 6500.) col.y = 35200000. * pow(Temp,(-3. / 2.)) + 184.;
col.z = 194.18 * log(Temp) - 1448.6;
col = clamp(col, 0., 255.)/255.;
if (Temp < 1000.) col *= Temp/1000.;
return col;
}
// -------------blackbody----------------- //
float scene(vec3 p)
{
float d = 10000.;
float bd1 = sdfBox(
twistSDF(
modSDFlim(
twistSDF(p - vec3(0., 12., 0.), smoothstep(0.30, 0.20, abs(sin(MYTIME))) * 0.02),
12., vec3(1. + smoothstep(0.45, 0.55, abs(sin(MYTIME))) * floor(abs(mod(MYTIME / (PI), 16.) - 8.)), 1. + smoothstep(0.45, 0.55, abs(sin(MYTIME))) * floor(abs(mod(MYTIME / (PI), 16.) - 8.)), 1. + smoothstep(0.45, 0.55, abs(sin(MYTIME))))), smoothstep(0.70, 0.80, abs(sin(MYTIME))) * -0.1),
vec3(4. + smoothstep(0.25, 0.75, abs(sin(MYTIME * 2.))),4. + smoothstep(0.25, 0.75, abs(sin(MYTIME * 2.))),4. + smoothstep(0.25, 0.75, abs(sin(MYTIME * 2.)))));
if (DISPLACEMENT_MAPPING == 1)
bd1 += sin(p.x / 2. * abs(mod(iTime, 6.) - 3.)) * 1. * sin(p.y / 2. * abs(mod(iTime, 6.) - 3.)) * 1. * sin(p.z / 2. * abs(mod(iTime, 6.) - 3.)) * 1.;
float bd2 = sdfBox(modSDF(p - vec3(0., -24., 0.), vec3(10., 0., 10.)), vec3(4.5,4.5,4.5)) * 1.;
d = min(bd1, d);
d = min(bd2, d);
return d;
}
vec2 march(vec3 ro, vec3 rd)
{
float dO = 0.;
float mind = 10000.;
for (int steps = 0; steps < MAX_STEPS; steps++)
{
vec3 p = ro + rd * dO;
float dS = scene(p);
if (steps == 3)
mind = dS;
dO += dS;
if (dS < E || dO > MAX_DIST)
break;
}
return vec2(dO, mind);
}
vec3 normal(vec3 p)
{
float d = scene(p);
vec2 e = vec2(0.001, 0);
vec3 n = d - vec3(
scene(p - e.xyy),
scene(p - e.yxy),
scene(p - e.yyx));
return (normalize(n));
}
float light(vec3 p, vec3 n)
{
vec3 lightPos = vec3(-250. * sin(MYTIME), 75., -250. * cos(MYTIME));
vec3 l = normalize(lightPos - p);
float dif = clamp(dot(l, n), 0., 1.);
//vec2 d = march(p+n*E * 30., l);
//if (d.x < length(lightPos - p))
// dif *= 0.1;
return (dif*0.3);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = ((fragCoord - .5 * iResolution.xy) / iResolution.y);
vec3 ro = vec3(-235. * sin(iTime / 2.), 75. + sin(iTime / 2.) * 15., -235. * cos(iTime / 2.));
vec3 rd = normalize(vec3(uv.x, uv.y, 1.));
rd = rotX(rd, PI /8.);
rd = rotY(rd, iTime / 2.);
vec2 d = march(ro, rd);
vec3 p = ro + rd * d.x;
vec3 n = normal(p);
p += n * E * 10.;
float dif = light(p, n);
vec3 col = vec3(0);
if (d.x < MAX_DIST * 0.9)
{
col = blackbody(d.y* (p.y + 19.2) * 2. * (50. - abs(mod(p.x * p.z / 30. + 25. + MYTIME * 50., 100.) - 50.)));
col.xyz = col.zyx;
col.xy *= 0.5;
col.x *= uv.x;
col.y *= uv.y;
if (col.z > 0.001)
{
col = 0.5 * (1. - col);
col = vec3(col.z, col.z, (col.x + col.y) / 2.);
col.x = col.x / 2. + col.z * uv.x;
col.y = col.y / 2. + col.z * uv.y;
}
col /= 2.;
}
col += vec3(dif / 2.,dif / 2., dif / 2.);
vec3 rr = rd - (2. * n * (dot(rd, n)));
vec2 d2 = march(p, rr);
vec3 p2 = p + rr * d2.x;
vec3 n2 = normal(p2);
dif = light(p2, n2);
if (d2.x < MAX_DIST * 0.9)
{
vec3 col2 = blackbody(d2.y* (p2.y + 19.2) * 2. * (50. - abs(mod(p2.x * p2.z / 30. + 25. + MYTIME * 50., 100.) - 50.)));
col2.xyz = col2.zyx;
col2.xy *= 0.5;
col2.x *= uv.x;
col2.y *= uv.y;
if (col2.z > 0.001)
{
col2 = 0.5 * (1. - col2);
col2 = vec3(col.z, col2.z, (col2.x + col2.y) / 2.);
col2.x = col2.x / 2. + col2.z * uv.x;
col2.y = col2.y / 2. + col2.z * uv.y;
}
col2 /= 2.;
col += col2;
}
if (d.x < MAX_DIST * 0.9)
{
vec3 col2 = blackbody(d2.y* (p.y + 19.2) * 2. * (50. - abs(mod(p.x * p.z / 30. + 25. + MYTIME * 50., 100.) - 50.)));
col += col2.yxz;
}
if (d2.x < MAX_DIST * 0.9)
{
vec3 col2 = blackbody(d2.y* (p2.y + 19.2) * 2. * (50. - abs(mod(p2.x * p2.z / 30. + 25. + MYTIME * 50., 100.) - 50.)));
col += col2.yxz;
}
col += vec3(dif / 2., dif / 2., dif / 2.);
//col = pow(col, vec3(0.454545));
fragColor = vec4(col, 1.0);
}
`

View File

@ -0,0 +1,223 @@
export default `#define MAX_STEPS 1280
#define MAX_DIST 1024.
#define E 0.0001
#define PI 3.141592
vec3 rotX(vec3 v, float a)
{
return vec3(v.x, v.y * cos(a) - v.z * sin(a), v.y * sin(a) + v.z * cos(a));
}
vec3 rotY(vec3 v, float a)
{
return vec3(v.x * cos(a) + v.z * sin(a), v.y, v.z * cos(a) - v.x * sin(a));
}
vec3 rotZ(vec3 v, float a)
{
return vec3(v.x * cos(a) - v.y * sin(a), v.x * sin(a) + v.y * cos(a), v.z);
}
int hash(ivec2 p) {
return (p.x * 7919 + p.y) * 6563;
}
vec3 modSDF(vec3 p, vec3 c)
{
return mod(p + 0.5 * c, c) - 0.5 * c;
}
float pMod1(inout float p, float size)
{
float halfsize = size * 0.5;
float c = floor((p+halfsize) / size);
p = mod(p + halfsize, size) - halfsize;
return c;
}
vec3 pMod3(inout vec3 p, vec3 size)
{
vec3 c;
c.x = pMod1(p.x, size.x);
c.y = pMod1(p.y, size.y);
c.z = pMod1(p.z, size.z);
return (c);
}
vec3 modlimSDF( in vec3 p, in float c, in vec3 l)
{
return p-c*vec3(clamp(floor(p.x/c + 0.5),-l.x,l.x), clamp(floor(p.y/c + 0.5),-l.y,l.y), clamp(floor(p.z/c + 0.5),-l.z,l.z));
}
vec3 twistSDF(in vec3 p, float k )
{
float c = cos(k*p.y);
float s = sin(k*p.y);
mat2 m = mat2(c,-s,s,c);
vec3 q = vec3(m*p.xz,p.y);
return q;
}
float sdfSphere(vec3 p, float r)
{
return length(p) - r;
}
float rand21(vec2 n) {
return fract(sin(dot(n, vec2(12.9898, 4.1414))) * 43758.5453);
}
float rand11(float n){return fract(sin(n) * 43758.5453123);}
float sdfCone( vec3 p, vec2 c, float h )
{
float q = length(p.xz);
return max(dot(c.xy,vec2(q,p.y)),-h-p.y);
}
float opSmoothUnion( float d1, float d2, float k )
{
float abs = (d1-d2) > 0. ? d1-d2 : d2-d1;
float h = max(k-abs,0.0);
return min(d1, d2) - h*h*0.25/k;
}
// blackbody by aiekick : https://www.shadertoy.com/view/lttXDn
// -------------blackbody----------------- //
// return color from temperature
//http://www.physics.sfasu.edu/astro/color/blackbody.html
//http://www.vendian.org/mncharity/dir3/blackbody/
//http://www.vendian.org/mncharity/dir3/blackbody/UnstableURLs/bbr_color.html
vec3 blackbody(float Temp)
{
vec3 col = vec3(255.);
col.x = 56100000. * pow(Temp,(-3. / 2.)) + 148.;
col.y = 100.04 * log(Temp) - 623.6;
if (Temp > 6500.) col.y = 35200000. * pow(Temp,(-3. / 2.)) + 184.;
col.z = 194.18 * log(Temp) - 1448.6;
col = clamp(col, 0., 255.)/255.;
if (Temp < 1000.) col *= Temp/1000.;
return col;
}
// -------------blackbody----------------- //
float scene(vec3 p)
{
float Time = iTime / 2.;
float sd1 = 2000.;
float cd1 = 2000.;
int ret = 0;
vec2 cell;
cell.x = pMod1(p.x, 5.);
cell.y = pMod1(p.z, 5.);
//p.y += mod(100. * iTime * rand11(cell.x) * rand11(cell.y), 100.) - 50.;
//p.y += rand11(cell.x) * rand11(cell.y) * 100. - 50.;
//p.y += mod(100. * iTime * 0.5, 100.) - 50.;
float r21 = rand21(cell) * 10.;
int sec = int(floor(mod(Time * 100., 10.)));
int abso = int(mod(Time, 10.)) - 5;
if (abso < 0)
abso = -abso;
if (int(floor(r21)) < 1 + abso && fract(r21) > abs(sin(Time / 3.)) - 0.3)
{
r21 -= 0.90;
r21 *= 10.;
p.y += sin(cell.x / 3. + sin(Time * 1.456) * PI) * 3. * sin(cell.y / 3. + sin(Time * 0.756) * PI) * 3.;
pMod1(p.y, sin(Time * 1.234) * 20. + 70.);
//p.z += 0.1 * (rand11(cell.y) - 0.5);
//p.y += r21 * 100.;
//p.y += iTime * 100. * r21;
//p.y = mod(p.y, 200.) - 100.;
//sd1 = sdfSphere(p, rand11(cell.x) * rand11(cell.y) * 1.4) * 0.1;
sd1 = sdfSphere(p , 2.) * 0.05;
//cd1 = sdfCone(p - vec3(0., 5.0, 0.), vec2(sin(PI/2.6), cos(PI/2.6 )), 4.) * 1.;
ret = 1;
}
//return min(sd1, cd1);
if (ret == 1)
//return min(sd1, cd1);
return opSmoothUnion(sd1, cd1, 0.2);
return 1.;
}
vec2 march(vec3 ro, vec3 rd)
{
float dO = 0.;
float mind = 10000.;
float index;
for (int steps = 0; steps < MAX_STEPS; steps++)
{
vec3 p = ro + rd * dO;
float dS = scene(p);
if (dS < mind)
mind = dS;
dO += dS;
if (dS < E || dO > MAX_DIST)
break;
}
if (dO > MAX_DIST)
return vec2(-1., mind);
return vec2(dO, mind);
}
vec3 normal(vec3 p)
{
float d = scene(p);
vec2 e = vec2(0.001, 0);
vec3 n = d - vec3(
scene(p - e.xyy),
scene(p - e.yxy),
scene(p - e.yyx));
return (normalize(n));
}
float light(vec3 p, vec3 n, vec3 ro)
{
vec3 lightPos = vec3(-10. * sin(iTime * 2.), 10., -10. * cos(iTime * 2.)) + ro;// + vec3(sin(iTime * 1.) * 1000.);
vec3 l = normalize(lightPos - p);
float dif = clamp(dot(l, n), 0., 1.);
//vec2 d = march(p+n*E * 30., l);
//if (d.x < length(lightPos - p))
// dif *= 0.1;
return (dif);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = ((fragCoord - .5 * iResolution.xy) / iResolution.y);
vec3 ro = vec3(0., 30., 0.);
vec3 rd = normalize(vec3(uv.x, uv.y, 1.));
rd = rotX(rd, PI / 6.);
rd = rotY(rd, iTime / 3.);
ro.x += iTime * 10.;
vec3 col = vec3(0);
vec2 d = march(ro, rd);
if (d.x > 0.)
{
vec3 p = ro + rd * d.x;
vec3 n = normal(p);
p += n * E * 10.;
float dif = light(p, n, ro);
col += vec3(0.6118 * dif, 0.7275 * dif, 0.9588 * dif);
//61.18% red, 82.75% green and 85.88%
}
else
{
vec3 bb = blackbody(d.y * 3000.);
bb = vec3(bb.z + bb.x / abs(sin(iTime / 3.)), bb.y + bb.x / abs(cos(iTime / 2.5)), bb.x);
bb = vec3(bb.x / 2., bb.y / 2., bb.z);
col += bb;
}
fragColor = vec4(col, 1.0);
}`;

View File

@ -0,0 +1,106 @@
export default `#define MAX_STEPS 1000
#define MAX_DIST 500.
#define E 0.000001
float sdCapsule(vec3 p, vec3 a, vec3 b, float r)
{
vec3 ab = b - a;
vec3 ap = p - a;
float t = dot(ab, ap) / dot(ab, ab);
t = clamp(t, 0., 1.);
vec3 c = a + t * ab;
return length(p - c) - r;
}
float sdTorus(vec3 p, vec2 r)
{
float x = length(p.xz) - r.x;
return length(vec2(x, p.y)) - r.y;
}
float sdBox(vec3 p, vec3 s)
{
return length(max(abs(p) - s, 0.));
}
float GetDist(vec3 p)
{
vec4 s = vec4(0, 0, 0, 2);
p.x += iTime;
float s_dist = (length(mod(p, 10.) - s.xyz) - s.w);
float b_dist1 = sdBox(mod(p + 5., 10.) - vec3(5.) - vec3(0., sin(p.x * 0.2) * 2., sin(p.x * 0.2) * 2.), vec3(10., 0.5, 0.5)) * 0.3;
float b_dist2 = sdBox(mod(p + 5., 10.) - vec3(5.) - vec3(sin(p.y * 0.2) * 2., 0., sin(p.y * 0.2) * 2.), vec3(0.5, 10., 0.5)) * 0.3;
float b_dist3 = sdBox(mod(p + 5., 10.) - vec3(5.) - vec3(sin(p.z * 0.2) * 2., sin(p.z * 0.2) * 2., 0.), vec3(0.5, 0.5, 10.)) * 0.3;
float d = min(b_dist1, b_dist2);
d = min(d, b_dist3);
return d;
}
float RayMarch(vec3 ro, vec3 rd) {
float dO = 0.;
for (int i = 0; i < MAX_STEPS; i++)
{
vec3 p = ro + rd * dO;
float dS = GetDist(p);
dO += dS;
if (dO > MAX_DIST || dS < E)
break;
}
return dO;
}
vec3 GetNormal(vec3 p)
{
float d = GetDist(p);
vec2 e = vec2(0.3, 0);
vec3 n = d - vec3(
GetDist(p - e.xyy),
GetDist(p - e.yxy),
GetDist(p - e.yyx));
return (normalize(n));
}
float GetLight(vec3 p)
{
vec3 lightPos = vec3(0, 10, iTime* 3.);
lightPos.xz += vec2(sin(iTime), cos(iTime)) * 5.;
vec3 l = normalize(lightPos - p);
vec3 n = GetNormal(p);
float dif = clamp(dot(l, n), 0., 1.);
//float d = RayMarch(p+n*E * 30., l);
//if (d < length(lightPos - p))
// dif *= 0.1;
return (dif);
}
vec2 rotate(vec2 p, float theta)
{
return vec2(cos(p.x) - sin(p.y), sin(p.x) + cos(p.y));
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = ((fragCoord - .5 * iResolution.xy) / iResolution.y);
vec3 up = normalize(vec3(cos(iTime), sin(iTime), 0));
vec3 ro = vec3(5. + cos(iTime), 5. + sin(iTime), iTime* 3.);
vec3 rd = normalize(vec3(uv.x + 1., uv.y + 1., 1 + 1));
float d = RayMarch(ro, rd);
vec3 p = ro + rd * d;
float dif = GetLight(p);
vec3 col = vec3(abs(mod((p.x + sin(iTime))/ 10., 1.) - 0.5) * dif, abs(mod((p.y + sin(iTime)) / 10., 1.) - 0.5) * dif, abs(mod((p.z + sin(iTime)) / 10., 1.) - 0.5) * dif);
//col = myvec;
fragColor = vec4(col,1.0);
}`;

View File

@ -0,0 +1,230 @@
export default `#define MAX_STEPS 1280
#define MAX_DIST 1280.
#define E 0.00001
#define PI 3.141592
vec3 rotX(vec3 v, float a) {
return vec3(v.x, v.y * cos(a) - v.z * sin(a), v.y * sin(a) + v.z * cos(a));
}
vec3 rotY(vec3 v, float a) {
return vec3(v.x * cos(a) + v.z * sin(a), v.y, v.z * cos(a) - v.x * sin(a));
}
vec3 rotZ(vec3 v, float a) {
return vec3(v.x * cos(a) - v.y * sin(a), v.x * sin(a) + v.y * cos(a), v.z);
}
float sdfSphere(vec3 p, float r) {
return length(p) - r;
}
//##########################################
// Space folding
//##########################################
void planeFold(inout vec4 z, vec3 n, float d) {
z.xyz -= 2.0 * min(0.0, dot(z.xyz, n) - d) * n;
}
void sierpinskiFold(inout vec4 z) {
z.xy -= min(z.x + z.y, 0.0);
z.xz -= min(z.x + z.z, 0.0);
z.yz -= min(z.y + z.z, 0.0);
}
void mengerFold(inout vec4 z) {
float a = min(z.x - z.y, 0.0);
z.x -= a;
z.y += a;
a = min(z.x - z.z, 0.0);
z.x -= a;
z.z += a;
a = min(z.y - z.z, 0.0);
z.y -= a;
z.z += a;
}
void boxFold(inout vec4 z, vec3 r) {
z.xyz = clamp(z.xyz, -r, r) * 2.0 - z.xyz;
}
//##########################################
// Primitive DEs
//##########################################
float de_sphere(vec4 p, float r) {
return (length(p.xyz) - r) / p.w;
}
float de_box(vec4 p, vec3 s) {
vec3 a = abs(p.xyz) - s;
return (min(max(max(a.x, a.y), a.z), 0.0) + length(max(a, 0.0))) / p.w;
}
float de_tetrahedron(vec4 p, float r) {
float md = max(max(-p.x - p.y - p.z, p.x + p.y - p.z),
max(-p.x + p.y + p.z, p.x - p.y + p.z));
return (md - r) / (p.w * sqrt(3.0));
}
float de_capsule(vec4 p, float h, float r) {
p.y -= clamp(p.y, -h, h);
return (length(p.xyz) - r) / p.w;
}
vec3 test = vec3(0.);
//##########################################
// Main DEs
//##########################################
float de_fractal(vec4 p) {
for (int i = 0; i < 8; ++i) {
p.xyz = abs(p.xyz);
p.xyz = rotZ(p.xyz, iTime * 0.1);
mengerFold(p);
p.xyz = rotX(p.xyz, iTime * 0.637 * 0.1);
sierpinskiFold(p);
p.xyz = rotX(p.xyz, iTime * 0.337 * 0.1);
p *= vec4(1.);
p.xyz += vec3(0.);
}
return de_box(p, vec3(6.0));
}
vec3 modlimSDF( in vec3 p, in float c, in vec3 l)
{
return p-c*vec3(clamp(floor(p.x/c + 0.5),-l.x,l.x), clamp(floor(p.y/c + 0.5),-l.y,l.y), clamp(floor(p.z/c + 0.5),-l.z,l.z));
}
float sdfCross( in vec3 p )
{
float a = 1.4 + 0.3*(sin(iTime * 1.));
float da = de_box(vec4(p.xyz, 1.),vec3(1000.,a*1.0,a*1.0));
float db = de_box(vec4(p.yzx, 1.),vec3(a*1.0,1000.,a*1.0));
float dc = de_box(vec4(p.zxy, 1.),vec3(a*1.0,a*1.0,1000.));
return min(da,min(db,dc));
}
float sdfFractal( in vec3 p )
{
float d = de_fractal(vec4(p, 1.));
float s = 1.0;
for( int m=0; m<3; m++ )
{
vec3 a = modlimSDF(p*s, 0.5, vec3(1.));
vec3 a2 = mod(p*s, 2.) - 1.;
s *= 3.0;
vec3 r = 1.0 - 3.0*abs(a);
vec3 r2 = 1.0 - 3.0*abs(a2);
float c = de_box(vec4(r, 1.), vec3(.5))/s;
d = max(d, -c);
c = de_box(vec4(r, 1.), vec3(.10))/s;
d = max(d, -c);
c = -sdfCross(r2)/s;
d = max(d, -c);
c = de_box(vec4(r.xyz + vec3(sign(r.x) / 5., sign(r.y) / 5.,sign(r.z) / 5.), 1.), vec3(.5))/s;
d = min(d, c);
if (m == 2)
test.x = length(d);
if (m == 3)
test.y = length(d);
if (m == 4)
test.z = length(d);
}
return d;
}
float scene(vec3 p)
{
int index = -1;
float d = 10000.;
float sd2 = sdfFractal(p);
d = min(sd2 * 0.2, d);
return d;
}
bool hit = false;
vec2 march(vec3 ro, vec3 rd)
{
float dO = 0.;
float mind = 10000.;
float index;
int steps = 0;
for (int isteps = 0; isteps < MAX_STEPS; isteps++)
{
steps = isteps;
vec3 p = ro + rd * dO;
float dS = scene(p);
if (dS < mind)
mind = dS;
dO += dS;
if (dS < E || dO > MAX_DIST)
break;
}
if (steps == MAX_STEPS || dO > MAX_DIST)
hit = false;
else
hit = true;
return vec2(dO, mind);
}
vec2 march_light(vec3 ro, vec3 rd)
{
float dO = 0.;
float mind = 10000.;
float index;
for (int steps = 0; steps < MAX_STEPS; steps++)
{
vec3 p = ro + rd * dO;
float dS = scene(p);
if (dS < mind)
mind = dS;
dO += dS;
if (dS < E || dO > MAX_DIST)
break;
}
return vec2(dO, mind);
}
vec3 normal(vec3 p)
{
float d = scene(p);
vec2 e = vec2(0.001, 0);
vec3 n = d - vec3(
scene(p - e.xyy),
scene(p - e.yxy),
scene(p - e.yyx));
return (normalize(n));
}
float light(vec3 p, vec3 n)
{
vec3 lightPos = vec3(-30. * sin(iTime), 0., -30. * cos(iTime));
vec3 l = normalize(lightPos - p);
float dif = clamp(dot(l, n), 0., 1.);
//vec2 d = march_light(p+n*E * 30., l);
//if (d.x < length(lightPos - p))
// dif *= 0.1;
return (dif);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec2 uv = ((fragCoord - .5 * iResolution.xy) / iResolution.y);
vec3 ro = vec3(-30. * sin(iTime), 0., -30. * cos(iTime));
vec3 rd = normalize(vec3(uv.x, uv.y, 1.));
rd = rotY(rd, iTime);
vec3 col = vec3(0);
vec2 d = march(ro, rd);
vec3 p = ro + rd * d.x;
vec3 n = normal(p);
p += n * E * 10.;
float dif = light(p, n);
if (hit)
col += dif;
fragColor = vec4(col, 1.0);
}`;

206
src/client/shaders/torus.js Normal file
View File

@ -0,0 +1,206 @@
export default `
#define MAX_STEPS 12800
#define MAX_DIST 128.
#define E 0.001
#define PI 3.141592
vec3 rotX(vec3 v, float a)
{
return vec3(v.x, v.y * cos(a) - v.z * sin(a), v.y * sin(a) + v.z * cos(a));
}
vec3 rotY(vec3 v, float a)
{
return vec3(v.x * cos(a) + v.z * sin(a), v.y, v.z * cos(a) - v.x * sin(a));
}
vec3 rotZ(vec3 v, float a)
{
return vec3(v.x * cos(a) - v.y * sin(a), v.x * sin(a) + v.y * cos(a), v.z);
}
vec3 modSDF(vec3 p, vec3 c)
{
return mod(p + 0.5 * c, c) - 0.5 * c;
}
vec3 modSDFlim( in vec3 p, in float c, in vec3 l)
{
return p-c*vec3(clamp(floor(p.x/c + 0.5),-l.x,l.x), clamp(floor(p.y/c + 0.5),-l.y,l.y), clamp(floor(p.z/c + 0.5),-l.z,l.z));
}
vec3 elongateSDF(in vec3 p, in vec3 h )
{
vec3 q = abs(p)-h;
return max(q,0.0) + min(max(q.x,max(q.y,q.z)), 0.0);
}
vec3 twistSDF(in vec3 p, float k )
{
float c = cos(k*p.y);
float s = sin(k*p.y);
mat2 m = mat2(c,-s,s,c);
vec3 q = vec3(m*p.xz,p.y);
return q;
}
float sdfBox( vec3 p, vec3 b )
{
vec3 q = abs(p) - b;
return length(max(q,0.0)) + min(max(q.x,max(q.y,q.z)),0.0);
}
float sdfTorus(vec3 p, vec2 r)
{
float x = length(p.xz) - r.x;
return length(vec2(x, p.y)) - r.y;
}
float sdfSphere(vec3 p, float r)
{
return length(p) - r;
}
// blackbody by aiekick : https://www.shadertoy.com/view/lttXDn
// -------------blackbody----------------- //
// return color from temperature
//http://www.physics.sfasu.edu/astro/color/blackbody.html
//http://www.vendian.org/mncharity/dir3/blackbody/
//http://www.vendian.org/mncharity/dir3/blackbody/UnstableURLs/bbr_color.html
vec3 blackbody(float Temp)
{
vec3 col = vec3(255.);
col.x = 56100000. * pow(Temp,(-3. / 2.)) + 148.;
col.y = 100.04 * log(Temp) - 623.6;
if (Temp > 6500.) col.y = 35200000. * pow(Temp,(-3. / 2.)) + 184.;
col.z = 194.18 * log(Temp) - 1448.6;
col = clamp(col, 0., 255.)/255.;
if (Temp < 1000.) col *= Temp/1000.;
return col;
}
// -------------blackbody----------------- //
vec2 scene(vec3 p)
{
float d = 10000.;
float sd1 = 10000.;
//float td1 = sdfTorus(elongateSDF(
// twistSDF(
// rotX(p, PI / 2.), sin(iTime) * 1.5), vec3(1., 1.,sin(iTime) * 2. + 1.))
// , vec2(abs(sin(iTime * 0.3723)) * 2. + 1.2, 0.2))
// + 0.1 * sin(p.x * 20.) * sin(p.y * 20.) * sin(p.z * 20.);
float td1 = sdfTorus(
twistSDF(
rotX(p, PI / 2.), sin(iTime) * 1.5)
, vec2(abs(sin(iTime * 0.3723)) * 2. + 1.2, 0.2))
+ 0.1 * sin(p.x * 20.) * sin(p.y * 20.) * sin(p.z * 20.);
//float td1 = 10000.;
//float td2 = sdfTorus(twistSDF(rotX(p, PI / 2.), cos(iTime) * 3.), vec2(abs(sin(iTime * 0.3723)) * 1. + 0.2, 0.1))
// + 0.1 * sin(p.x * 20.) * sin(p.y * 20.) * sin(p.z * 20.);
float td2 = 10000.;
td2 = sdfTorus(
twistSDF(
rotX(p, PI / 2.), cos(iTime) * 1.5)
, vec2(abs(sin(iTime * 0.3723)) * 2. + 2.2, 0.2))
+ 0.1 * sin(p.x * 20.) * sin(p.y * 20.) * sin(p.z * 20.);
//float td1 = 10000.;
sd1 = sdfSphere(p - vec3(0.,0.,0.),abs(sin(iTime * 0.3723)) * 2. + 0.4) * 0.19;
td1 *= 0.2;
td2 *= 0.2;
d = min(td1, d);
d = min(td2, d);
d = min(sd1, d);
if (sd1 > d - E && sd1 < d + E)
return vec2(d, 1.);
if ((td1 > d - E && td1 < d + E) || (td2 > d - E && td2 < d + E))
return vec2(d, 2.);
return vec2(d, -1.);
}
vec3 march(vec3 ro, vec3 rd)
{
float dO = 0.;
float mind = 10000.;
float object = -1.;
for (int steps = 0; steps < MAX_STEPS; steps++)
{
vec3 p = ro + rd * dO;
vec2 dS = scene(p);
object = dS.y;
if (dS.x < mind)
mind = dS.x;
dO += dS.x;
if (dS.x < E || dO > MAX_DIST)
break;
}
return vec3(dO, mind, object);
}
vec3 normal(vec3 p)
{
vec2 d = scene(p);
vec2 e = vec2(0.0001, 0);
vec3 n = d.x - vec3(
scene(p - e.xyy).x,
scene(p - e.yxy).x,
scene(p - e.yyx).x);
return (normalize(n));
}
float light(vec3 p, vec3 n)
{
vec3 lightPos = vec3(-12. * sin(iTime), 0., -12. * cos(iTime));
vec3 l = normalize(lightPos - p);
float dif = clamp(dot(l, n), 0., 1.);
vec3 d = march(p+n, l);
if (d.x < length(lightPos - p))
dif *= 0.1;
return (dif);
}
void mainImage( out vec4 fragColor, in vec2 fragCoord )
{
vec3 col = vec3(0);
vec2 uv = ((fragCoord - .5 * iResolution.xy) / iResolution.y);
vec3 ro = vec3(-12. * sin(iTime), 0., -12. * cos(iTime));
vec3 rd = normalize(vec3(uv.x, uv.y, 1.));
rd = rotY(rd, iTime);
vec3 d = march(ro, rd);
vec3 p = ro + rd * d.x;
vec3 n = normal(p);
p += n * E * 10.;
float dif = light(p, n);
if (d.z > 1. + E || d.z < 1. - E)
col = blackbody(d.y * 10000.);
if (d.x < MAX_DIST * 0.9)
{
if (d.z > 2. - E && d.z < 2. +E)
col += vec3(dif * abs(p.x - 0.5), dif * abs(p.y - 0.5), dif * abs(p.z - 0.5));
vec3 rr = rd - (2. * n * (dot(rd, n)));
vec3 d2 = march(p, rr);
vec3 p2 = p + rr * d2.x;
vec3 n2 = normal(p2);
dif = light(p2, n2);
if (d.z > 1. - E && d.z < 1. + E)
{
col += vec3(dif * abs(p2.x - 0.5), dif * abs(p2.y - 0.5), dif * abs(p2.z - 0.5));
}
}
col = pow(col, vec3(0.454545));
fragColor = vec4(col, 1.);
}
`

View File

@ -0,0 +1,153 @@
export default `#define MAX_STEPS 1280
#define MAX_DIST 128.
#define E 0.0001
#define PI 3.141592
vec3 rotX(vec3 v, float a)
{
return vec3(v.x, v.y * cos(a) - v.z * sin(a), v.y * sin(a) + v.z * cos(a));
}
vec3 rotY(vec3 v, float a)
{
return vec3(v.x * cos(a) + v.z * sin(a), v.y, v.z * cos(a) - v.x * sin(a));
}
vec3 rotZ(vec3 v, float a)
{
return vec3(v.x * cos(a) - v.y * sin(a), v.x * sin(a) + v.y * cos(a), v.z);
}
float sdfSphere(vec3 p, float r)
{
return length(p) - r;
}
float sdfPyramid( vec3 p, float h)
{
float m2 = h*h + 0.25;
p.xz = abs(p.xz);
p.xz = (p.z>p.x) ? p.zx : p.xz;
p.xz -= 0.5;
vec3 q = vec3( p.z, h*p.y - 0.5*p.x, h*p.x + 0.5*p.y);
float s = max(-q.x,0.0);
float t = clamp( (q.y-0.5*p.z)/(m2+0.25), 0.0, 1.0 );
float a = m2*(q.x+s)*(q.x+s) + q.y*q.y;
float b = m2*(q.x+0.5*t)*(q.x+0.5*t) + (q.y-m2*t)*(q.y-m2*t);
float d2 = min(q.y,-q.x*m2-q.y*0.5) > 0.0 ? 0.0 : min(a,b);
return sqrt( (d2+q.z*q.z)/m2 ) * sign(max(q.z,-p.y));
}
float sdfTriPrism( 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 SDFmod( in vec3 p, in vec3 c)
{
return (mod(p+0.5*c,c)-0.5*c);
return (p);