Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Peridot TechShowCase #1: Euphorizer #42

Draft
wants to merge 31 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
fc11681
add: euphorizer ray generation
Pctg-x8 May 5, 2019
0b9e74e
fix: ray generation considering aspect ratio
Pctg-x8 May 8, 2019
b39222a
fix: screen resizing
Pctg-x8 May 8, 2019
580675a
add: menger sponge
Pctg-x8 May 9, 2019
96dcf85
fix: normalize ray
Pctg-x8 May 9, 2019
4be6369
fix: menger functions
Pctg-x8 May 9, 2019
92479fe
fix: synchronize rendering work
Pctg-x8 May 10, 2019
0b2c01e
fix: syncing user code
Pctg-x8 May 10, 2019
e7b0061
fix: imageplane following bedrock api updates
Pctg-x8 May 10, 2019
0474fdb
Merge branch 'dev' into tsc-euphorizer
Pctg-x8 May 15, 2019
be3eb06
fix: asset autobuild file searching
Pctg-x8 May 15, 2019
a16e6a8
fix: uncomment hdr asset
Pctg-x8 May 15, 2019
3517d89
Merge branch 'dev' into tsc-euphorizer
Pctg-x8 May 15, 2019
9bab8f6
Merge branch 'dev' into tsc-euphorizer
Pctg-x8 Jun 1, 2019
2341ff5
fix: receive deltatime in update
Pctg-x8 Jun 1, 2019
5aec1c6
fix: line length
Pctg-x8 Jun 1, 2019
50777bb
add: repetation and time
Pctg-x8 Jun 19, 2019
d8589e6
add: color gradient by time
Pctg-x8 Jun 19, 2019
72e314b
fix: foldrot
Pctg-x8 Jun 20, 2019
62b7609
add: camera rotation morph
Pctg-x8 Jun 21, 2019
bbfd076
fix: denoising
Pctg-x8 Jun 22, 2019
5a48a9e
Merge branch 'tsc-euphorizer' of github.com:Pctg-x8/peridot into tsc-…
Pctg-x8 Jun 22, 2019
c5ffa1b
terrain
Pctg-x8 Jun 30, 2019
31ecd5d
Merge branch 'dev' into tsc-euphorizer
Pctg-x8 Sep 10, 2019
ba192d8
add: FeatureRequests
Pctg-x8 Sep 10, 2019
7aaa793
ex: randomfold
Pctg-x8 Sep 12, 2019
3eee4db
fix: add verbose mode to shaderbuild and toolbuild for ps1 env
Pctg-x8 Sep 12, 2019
ee77c9a
ex: pseudo instancing by fold
Pctg-x8 Sep 14, 2019
97da454
Merge branch 'dev' into tsc-euphorizer
Pctg-x8 Sep 21, 2019
388510f
fix: merging failure
Pctg-x8 Sep 21, 2019
632edcf
fix: logging and missing dependency
Pctg-x8 Oct 12, 2019
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified cradle/android/build.sh
100755 → 100644
Empty file.
4 changes: 2 additions & 2 deletions cradle/mac/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ while [ $# -gt 0 ]; do
shift 2
;;
*)
if [ -z ${USERLIB_DIRECTORY+x} ]; then USERLIB_DIRECTORY=$1; fi
if [ -z ${USERLIB_DIRECTORY+x} ]; then USERLIB_DIRECTORY=$(realpath $1); fi
shift
;;
esac
Expand All @@ -49,5 +49,5 @@ FEATURES="bedrock/VK_EXT_debug_report,bedrock/VK_MVK_macos_surface"
echo "💎 $SCRIPT_PATH/peridot-cradle/build/Debug/peridot-cradle.app"

if [ $AFTER_RUN -ne 0 ]; then
lldb $SCRIPT_PATH/peridot-cradle/build/Debug/peridot-cradle.app
lldb -o run $SCRIPT_PATH/peridot-cradle/build/Debug/peridot-cradle.app
fi
12 changes: 12 additions & 0 deletions examples/euphorizer/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "euphorizer"
version = "0.1.0"
authors = ["S.Percentage <[email protected]>"]
edition = "2018"

[dependencies]
peridot = { path = "../../" }
bedrock = { git = "https://github.com/Pctg-x8/bedrock", features = ["Presentation", "Implements"] }
log = "0.4"

peridot-vertex-processing-pack = { path = "../../vertex-processing-pack" }
257 changes: 257 additions & 0 deletions examples/euphorizer/assets/main.csh
Original file line number Diff line number Diff line change
@@ -0,0 +1,257 @@
VertexInput {
Binding 0 [PerVertex] { pos: vec2; }
}
VertexShader {
RasterPosition = vec4(pos, 0.0, 1.0);
vpos = pos * vec2(aspect_wh, 1.0);
}
Varyings VertexShader -> FragmentShader {
vpos: vec2;
}
Header[FragmentShader] {
const float PI = 3.1415926;

vec2 noize2(vec2 st)
{
vec2 stt = vec2(dot(st, vec2(1370.23f, 1923.3f)), dot(st.yx, vec2(305.0f, 253.0f)));
return fract(sin(stt) * vec2(23.50f, 431.0f));
}
float perlin(vec2 st)
{
vec2 iv = floor(st);
vec2 fv = fract(st);
vec2 u = fv * fv * (3.0f - 2.0f * fv);

float n00 = dot(noize2(iv + vec2(0.0f, 0.0f)), fv - vec2(0.0f, 0.0f));
float n10 = dot(noize2(iv + vec2(1.0f, 0.0f)), fv - vec2(1.0f, 0.0f));
float n01 = dot(noize2(iv + vec2(0.0f, 1.0f)), fv - vec2(0.0f, 1.0f));
float n11 = dot(noize2(iv + vec2(1.0f, 1.0f)), fv - vec2(1.0f, 1.0f));

return mix(mix(n00, n10, u.x), mix(n01, n11, u.x), u.y);
}
float perlin_fractal(vec2 st, int octaves)
{
float d = 0.0f;
float amp = 0.5f;
for (int i = 0; i < octaves; i++)
{
d += perlin(st) * amp;
// fbm modulation from Elevated: https://www.shadertoy.com/view/MdX3Rr
// UVを回転させてる?
st = mat2(0.8, -0.6, 0.6, 0.8) * st * 2.0f;
amp *= 0.5f;
}
return d;
}

// Modified Version of https://www.geeks3d.com/20140201/glsl-menger-sponge-raymarching-code-samples-updated/
float maxcomp3(in vec3 v) { return max(max(v.x, v.y), v.z); }
float dist_box(in vec3 p, in vec3 size)
{
vec3 dv = abs(p) - size;
return min(maxcomp3(dv), length(max(dv, 0.0)));
}
float dist_box2(in vec2 p, in vec2 size)
{
vec2 dv = abs(p) - size;
return min(max(dv.x, dv.y), length(max(dv, 0.0)));
}
float dist_cube(in vec3 p, in float size)
{
vec3 dv = abs(p) - vec3(size);
return min(maxcomp3(dv), length(max(dv, 0.0)));
}
float dist_cross(in vec3 p, in float param)
{
float dx = dist_box2(p.xy, vec2(param));
float dy = dist_box2(p.yz, vec2(param));
float dz = dist_box2(p.zx, vec2(param));
return min(min(dx, dy), dz);
}
float dist_menger2(vec3 p)
{
float d0 = dist_box(p, vec3(1.0));
float d1 = dist_cross(p * 3.0, 1.0) / 3.0;
return max(d0, -d1);
}
float dist_menger(vec3 p)
{
float d = dist_box(p, vec3(1.0));
float s = 1.0;
for (int _ = 0; _ < 4; _++)
{
vec3 a = mod(p * s, 2.0) - 1.0;
s *= 3.0;
vec3 r = 1.0 - 3.0 * abs(a);

float c = dist_cross(r, mix(0.48, 1.5, pow(cos(time_sec * 0.8) * 0.5 + 0.5, 4.0))) / s;
d = max(d, c);
}
return d;
}
mat2 rot(float rad)
{
float s = sin(rad), c = cos(rad);
return mat2(c, s, -s, c);
}
vec3 pmod_xy(vec3 pin, float n)
{
float nr = 2.0 * PI / n;
float rbase = atan(pin.y, pin.x) - PI / (n * 2.0);
float r = floor(rbase / nr) * nr;

return vec3(pin.xy * rot(r), pin.z);
}
vec3 fold3(in vec3 p, in vec3 rate)
{
return mod(p, rate) - 0.5f * rate;
}
mat3 rotx(float rad)
{
float s = sin(rad), c = cos(rad);
return mat3(1, 0, 0, 0, c, s, 0, -s, c);
}
mat3 roty(float rad)
{
float s = sin(rad), c = cos(rad);
return mat3(c, 0, -s, 0, 1, 0, s, 0, c);
}
mat3 rotz(float rad)
{
float s = sin(rad), c = cos(rad);
return mat3(c, -s, 0, s, c, 0, 0, 0, 1);
}
vec3 rep_centralized(in vec3 p, float divspace)
{
return mod(p - divspace * 0.5, divspace) - divspace * 0.5;
}

float dist_scene(vec3 p)
{
/*float d = 10000000.0;
for (float i = 0; i < 6; i++)
{
vec3 pp = vec3(rot((i / 6.0) * PI * 2.0) * p.xy, p.z);
d = min(d, dist_menger(mod(pp, 9.0)));
}
return d;*/
/*vec3 pp = pmod_xy(p, 6.0);
pp = rep_centralized(pp, 3.0);
return dist_menger(pp);*/
return abs(p.y - (0.5f - 4.0f * perlin_fractal(p.xz / 5.0f, 9)));
}
float dist_scene_randomfold(vec3 p)
{
const float FoldZ = dot(noize2(floor((0.5f * vpos.xy + 0.5f) * vec2(4.0f, 5.0f))), vec2(0.5f)) * 0.001f + 0.3f;
const vec3 ModInstanceIndex = floor(p / vec3(0.025f, 0.025f, FoldZ));
const float ZL = dot(noize2(ModInstanceIndex.xz * 30.0f), vec2(0.5f)) * 0.008f + 0.004f;
const float ZD = dot(noize2(ModInstanceIndex.xy), vec2(0.5f)) * 0.16f - 0.08f;
const mat3 r =
rotx(dot(noize2(ModInstanceIndex.yx * 30.0f), vec2(0.5f)) * PI * 0.8 - PI * 0.4) *
rotz(dot(noize2(ModInstanceIndex.zy * 40.0f), vec2(0.5f)) * PI * 0.8 - PI * 0.4);
return dist_box(r * (fold3(p, vec3(0.025f, 0.025f, FoldZ)) - vec3(0.0f, 0.0f, ZD)), vec3(0.008f, 0.008f, ZL) * 0.7f);
}
#define DIST_SCENE dist_scene_randomfold

// vec3 input: h, s, v
vec3 hsv2rgb(vec3 c)
{
const vec4 k = vec4(1.0, 1.0 / 3.0, 2.0 / 3.0, 3.0);
vec3 p = abs(fract(c.xxx + k.xzy) * 6.0 - k.www);
return c.z * mix(k.xxx, clamp(p - k.xxx, 0.0, 1.0), c.y);
}

bool cast_ray(in vec3 origin, in vec3 dir, out float dist)
{
const float maxd = 10.0;
const int iterate_max = 512;
dist = 0.01;

for (int _ = 0; _ < iterate_max && dist <= maxd; _++)
{
vec3 p = fma(dir, vec3(dist), origin);
float d = DIST_SCENE(p);
if (abs(d) < 0.002 * dist) return true;
dist += d * 0.4f;
}
return false;
}

vec3 apply_fast_half_lambert(in vec3 surface_color, in vec3 normal, in vec3 light_dir)
{
const float factor = dot(normal, -light_dir) * 0.75f + 0.25f;
return surface_color * factor;
}

vec4 iterate_ray(vec3 eyepos, vec3 raydir)
{
float f;
const bool first_hit = cast_ray(eyepos, raydir, f);

const float irrad = (1.0f - fract(time_sec * (102.0f / 60.0f))) * 0.4f + 1.0f;

const vec3 fog_color = vec3(0.0, 0.0, 0.0);
// vec3 mesh_color = hsv2rgb(vec3(time_sec * 0.5, 0.5, 0.7));
vec3 mesh_color = vec3(0.1f, 0.2f, 0.5f);
const vec3 light_dir = normalize(vec3(0.0, 0.0, 1.0));
if (first_hit)
{
const vec3 p = fma(raydir, vec3(f), eyepos);
const vec2 e = vec2(1.0, -1.0);
const float h = 0.0001 * 0.5773;
// return vec4(vec3(pow(1.0 - f / maxd, 4.0)), 1.0);
// http://iquilezles.org/www/articles/normalsSDF/normalsSDF.htm
vec3 nrm = normalize(
e.xyy * DIST_SCENE(p + e.xyy * h) +
e.yyx * DIST_SCENE(p + e.yyx * h) +
e.yxy * DIST_SCENE(p + e.yxy * h) +
e.xxx * DIST_SCENE(p + e.xxx * h)
);
// return vec4(nrm * 0.5 + 0.5, 1.0);

vec4 surface_color = vec4(apply_fast_half_lambert(mesh_color, nrm, light_dir), 1.0f);

float f2;
const vec3 refdir = reflect(raydir, nrm);
const bool first_reflection = cast_ray(p, refdir, f2);
if (first_reflection)
{
vec3 p2 = fma(refdir, vec3(f2), p);
vec3 nrm2 = normalize(
e.xyy * DIST_SCENE(p2 + e.xyy * h) +
e.yyx * DIST_SCENE(p2 + e.yyx * h) +
e.yxy * DIST_SCENE(p2 + e.yxy * h) +
e.xxx * DIST_SCENE(p2 + e.xxx * h)
);
vec4 col2 = vec4(apply_fast_half_lambert(mesh_color, nrm2, light_dir), 1.0);
surface_color = mix(col2, surface_color, f2 * 0.25 + 0.75);
}

float fog_level = min(pow(f * 0.8, 2.0f), 1.0f);
vec4 final_color = mix(surface_color * irrad, vec4(fog_color, 1.0f), fog_level);
return vec4(final_color.xyz * final_color.a, final_color.a);
}
else { return vec4(fog_color, 1.0); }
}
}
FragmentShader {
vec3 eyepos = vec3(0.0, 0.0, -FOCAL_LENGTH);
vec3 raydir = normalize(vec3(vpos, 0.0) - eyepos);
float rx = -0.14;
float ry = sin(time_sec * 0.25 - 0.5) * 0.09;

// vec3 camera_pos = (roty(-ry) * normalize(vec3(0.0, -2.0, 5.0))) * 5.0;
// vec3 rd = roty(ry) * rotx(rx) * raydir;
vec3 camera_pos = normalize(vec3(0.0, -3.0, -5.0)) * 0.2 + vec3(0.0, 0.0, 0.08) * time_sec;
vec3 rd = raydir;

Target[0] = iterate_ray(eyepos + camera_pos, normalize(rd));
}

SpecConstant[FragmentShader](0) FOCAL_LENGTH: float = 5.0;
PushConstant[VertexShader] ScreenInfo {
float aspect_wh;
}
Uniform[FragmentShader](0, 0) DynamicParams {
float time_sec;
}
Loading