-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #72 from alphastrata/shader/fbmCloud
shader/fbmCloud
- Loading branch information
Showing
3 changed files
with
306 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
/// ***************************** /// | ||
/// This is a port fo the FBM quick example in the little book of shaders: Author @patriciogv - 2015 http://patriciogonzalezvivo.com | ||
/// Ours looks a lot like theirs at sufficently small resolutions, but to dream a little larger there's a custom gussianBlur added. | ||
/// ***************************** /// | ||
|
||
#import bevy_sprite::mesh2d_view_bindings::globals | ||
#import shadplay::shader_utils::common::{NEG_HALF_PI, shader_toy_default, rotate2D, TWO_PI} | ||
#import bevy_render::view::View | ||
#import bevy_pbr::forward_io::VertexOutput; | ||
|
||
@group(0) @binding(0) var<uniform> view: View; | ||
|
||
const SPEED:f32 = 1.0; | ||
const NUM_OCTAVES: i32 = 8; | ||
|
||
@fragment | ||
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> { | ||
var uv = in.uv; | ||
let resolution = view.viewport.zw; | ||
let time = globals.time * SPEED; | ||
uv *= rotate2D(NEG_HALF_PI); | ||
|
||
|
||
// Slapping in a gaussian blur: | ||
// let blurRadius: f32 = 1.0; // Adjust the radius to control the blur amount, maybe don't go too HIGH! | ||
// var blurredColor: vec4<f32> = vec4(0.0, 0.0, 0.0, 0.0); | ||
// var totalWeight: f32 = 0.0; | ||
// let intRadius: i32 = i32(blurRadius); | ||
|
||
// for (var x: i32 = -intRadius; x <= intRadius; x++) { | ||
// for (var y: i32 = -intRadius; y <= intRadius; y++) { | ||
// var sampleUv: vec2<f32> = uv + vec2<f32>(f32(x), f32(y)) / resolution; | ||
// var sampleColor: vec4<f32> = fmb_cloud(sampleUv, time, resolution); | ||
// var weight: f32 = exp(-f32(x * x + y * y) / (2.0 * blurRadius * blurRadius)); | ||
// blurredColor += sampleColor * weight; | ||
// totalWeight += weight; | ||
// } | ||
// } | ||
|
||
// blurredColor /= totalWeight; | ||
// return blurredColor; | ||
|
||
// or, the vanilla port: | ||
return fmb_cloud(uv, time, resolution); | ||
} | ||
|
||
|
||
fn fmb_cloud(uv: vec2f, time: f32, resolution: vec2f)->vec4f{ | ||
var color: vec3<f32> = vec3<f32>(0.); | ||
|
||
var q: vec2<f32> = vec2<f32>(0.); | ||
q.x = fbm(uv + 0. * time); | ||
q.y = fbm(uv + vec2<f32>(1.)); | ||
|
||
var r: vec2<f32> = vec2<f32>(0.); | ||
r.x = fbm(uv + 1. * q + vec2<f32>(1.7, 9.2) + 0.15 * time); | ||
r.y = fbm(uv + 1. * q + vec2<f32>(8.3, 2.8) + 0.126 * time); | ||
|
||
let f: f32 = fbm(uv + r); | ||
|
||
color = mix(vec3<f32>(0.101961, 0.619608, 0.666667), vec3<f32>(0.666667, 0.666667, 0.498039), clamp(f * f * 4., 0., 1.)); | ||
color = mix(color, vec3<f32>(0., 0., 0.164706), clamp(length(q), 0., 1.)); | ||
color = mix(color, vec3<f32>(0.666667, 1., 1.), clamp(length(r.x), 0., 1.)); | ||
|
||
return vec4<f32>((f * f * f + 0.6 * f * f + 0.5 * f) * color, 1.0); | ||
} | ||
|
||
fn random(uv: vec2<f32>) -> f32 { | ||
return fract(sin(dot(uv.xy, vec2<f32>(12.9898, 78.233))) * 43758.547); | ||
} | ||
|
||
fn noise(uv: vec2<f32>) -> f32 { | ||
var i: vec2<f32> = floor(uv); | ||
var f: vec2<f32> = fract(uv); | ||
var a: f32 = random(i); | ||
let b: f32 = random(i + vec2<f32>(1., 0.)); | ||
let c: f32 = random(i + vec2<f32>(0., 1.)); | ||
let d: f32 = random(i + vec2<f32>(1., 1.)); | ||
let u: vec2<f32> = f * f * (3. - 2. * f); | ||
return mix(a, b, u.x) + (c - a) * u.y * (1. - u.x) + (d - b) * u.x * u.y; | ||
} | ||
|
||
fn fbm(_uv: vec2<f32>) -> f32 { | ||
var uv = _uv; | ||
var v: f32 = 0.; | ||
var a: f32 = 0.5; | ||
let shift: vec2<f32> = vec2<f32>(100.); | ||
let rot: mat2x2<f32> = mat2x2<f32>(cos(0.5), sin(0.5), -sin(0.5), cos(0.5)); | ||
|
||
for (var i: i32 = 0; i < NUM_OCTAVES; i++) { | ||
v = v + (a * noise(uv)); | ||
uv = rot * uv * 2. + shift; | ||
a = a * (0.5); | ||
} | ||
|
||
return v; | ||
} | ||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,208 @@ | ||
/// ***************************** /// | ||
/// This is a prot of WaterPool by rubaotree, on shadertoy: https://www.shadertoy.com/view/ctcBRn | ||
/// ***************************** /// | ||
|
||
#import bevy_sprite::mesh2d_view_bindings::globals | ||
#import shadplay::shader_utils::common::{NEG_HALF_PI, rotate2D, TWO_PI} | ||
#import bevy_render::view::View | ||
#import bevy_pbr::forward_io::VertexOutput; | ||
|
||
@group(0) @binding(0) var<uniform> view: View; | ||
|
||
const SPEED:f32 = 0.25; | ||
|
||
@fragment | ||
fn fragment(in: VertexOutput) -> @location(0) vec4<f32> { | ||
var uv = (in.uv * 2.0) - 1.0; | ||
let resolution = view.viewport.zw; | ||
let t = globals.time * SPEED; | ||
uv.x *= resolution.x / resolution.y; | ||
|
||
let uvt = vec3<f32>(uv.x * 1.6, uv.y * 2.0 + t * 0.3, t * 0.3); | ||
let height:f32 = get_height(uvt); | ||
let shake:vec3f = get_gradient(uvt * 0.4) * 0.005; | ||
let dlight:vec3f = normalize(vec3<f32>(0.0, -0.8, 0.9)); | ||
let normal:vec3f = normalize(vec3<f32>(get_gradient(uvt * 2.0))); | ||
let lightness:f32 = dot(dlight, normal); | ||
|
||
var col = poolColor((uv + shake.xy) * 0.5 + 0.25); | ||
|
||
let vorValue = voronoi(vec3<f32>(uv.x * 0.8, uv.y, t * 0.5), 4.0); | ||
let cutValue = voronoi_cut(vorValue); | ||
col += cutValue * 0.3; | ||
|
||
col += vec3<f32>(1.0) * step(1.2, lightness + height) * 0.9; | ||
col += vec3<f32>(clamp(height - 0.3, -0.3, 1.0) * 0.5); | ||
|
||
return vec4<f32>(col, 1.0); | ||
} | ||
|
||
fn ring_curve(t: f32) -> f32 { | ||
return convex_and_clip((abs(1.0 / sin(t)) - 1.0) * 0.05, 1.0); | ||
} | ||
|
||
fn light_mix(col: vec3<f32>, lightness: f32) -> vec3<f32> { | ||
return col * (lightness * 1.2 + 0.3); | ||
} | ||
|
||
fn coord_to_uv(coord: vec2<f32>, iResolution: vec2<f32>) -> vec2<f32> { | ||
return coord / max(iResolution.x, iResolution.y); | ||
} | ||
|
||
fn voronoi(p: vec3<f32>, density: f32) -> f32 { | ||
var id = floor(p * density); | ||
var min_dist = 1.0; | ||
|
||
for (var dy: i32 = -1; dy <= 1; dy++) { | ||
for (var dx: i32 = -1; dx <= 1; dx++) { | ||
var neighbor = id + vec3<f32>(f32(dx), f32(dy), 0.0); | ||
var point = neighbor + random2to3(neighbor); // Assuming a random2to3 function | ||
var dist = length(point - p * density); | ||
min_dist = min(min_dist, dist); | ||
} | ||
} | ||
return min_dist; | ||
} | ||
|
||
fn voronoi_cut(t: f32) -> f32 { | ||
return t * 1.4; | ||
} | ||
|
||
fn convex_and_clip(t: f32, ind: f32) -> f32 { | ||
if (t <= 0.0) { return 0.0; } | ||
if (t >= 1.0) { return 1.0; } | ||
return 1.0 - abs(pow(t - 1.0, ind)); | ||
} | ||
|
||
// Dummy random function (replace with a better one) | ||
fn random2to3(p: vec3<f32>) -> vec3<f32> { | ||
return fract(sin(vec3<f32>(dot(p, vec3<f32>(127.1, 311.7, 74.7)), | ||
dot(p, vec3<f32>(269.5, 183.3, 246.1)), | ||
dot(p, vec3<f32>(113.5, 271.9, 124.6)))) * 43758.5453); | ||
} | ||
|
||
fn poolColor(uv: vec2<f32>) -> vec3<f32> { | ||
return vec3<f32>(uv.x, uv.y, 1.0 - uv.x * uv.y); | ||
} | ||
|
||
fn get_gradient(uvt: vec3<f32>) -> vec3<f32> { | ||
return normalize(vec3<f32>(sin(uvt.x), cos(uvt.y), sin(uvt.z))); | ||
} | ||
|
||
fn get_height(uvt: vec3<f32>) -> f32 { | ||
return sin(uvt.x * 10.0) * cos(uvt.y * 10.0) * 0.5; | ||
} | ||
|
||
|
||
fn hash11(_p: f32) -> f32 { | ||
var p = fract(_p * 0.1031); | ||
p *= p + 33.33; | ||
p *= p + p; | ||
return fract(p); | ||
} | ||
fn hash21(_p: vec2<f32>) -> f32 { | ||
var p3 = fract(vec3<f32>(_p.x, _p.y, _p.x) * 0.1031); | ||
p3 += dot(p3, p3.yzx + 33.33); | ||
return fract((p3.x + p3.y) * p3.z); | ||
} | ||
fn hash31(_p3: vec3<f32>) -> f32 { | ||
var p = fract(_p3 * 0.1031); | ||
p += dot(p, p.zyx + 31.32); | ||
return fract((p.x + p.y) * p.z); | ||
} | ||
|
||
fn hash12(_p: f32) -> vec2<f32> { | ||
var p3 = fract(vec3<f32>(_p) * vec3<f32>(0.1031, 0.1030, 0.0973)); | ||
p3 += dot(p3, p3.yzx + 33.33); | ||
return fract((p3.xx + p3.yz) * p3.zy); | ||
} | ||
|
||
fn hash22(_p: vec2<f32>) -> vec2<f32> { | ||
var p3 = fract(vec3<f32>(_p.x, _p.y, _p.x) * vec3<f32>(0.1031, 0.1030, 0.0973)); | ||
p3 += dot(p3, p3.yzx + 33.33); | ||
return fract((p3.xx + p3.yz) * p3.zy); | ||
} | ||
|
||
fn hash32(_p3: vec3<f32>) -> vec2<f32> { | ||
var p = fract(_p3 * vec3<f32>(0.1031, 0.1030, 0.0973)); | ||
p += dot(p, p.yzx + 33.33); | ||
return fract((p.xx + p.yz) * p.zy); | ||
} | ||
|
||
fn luminance(_col: vec3<f32>) -> f32 { | ||
return dot(vec3<f32>(0.2125, 0.7154, 0.0721), _col); | ||
} | ||
|
||
fn rgb2hsv(_col: vec3<f32>) -> vec3<f32> { | ||
let min_val = min(min(_col.r, _col.g), _col.b); | ||
let max_val = max(max(_col.r, _col.g), _col.b); | ||
var h: f32 = 0.0; | ||
var s: f32 = 0.0; | ||
let v: f32 = max_val; | ||
|
||
let delta = max_val - min_val; | ||
if (max_val != 0.0) { | ||
s = delta / max_val; | ||
} else { | ||
// r = g = b = 0 | ||
s = 0.0; | ||
h = -1.0; | ||
return vec3<f32>(h, s, v); | ||
} | ||
|
||
if (_col.r == max_val) { | ||
h = (_col.g - _col.b) / delta; | ||
} else if (_col.g == max_val) { | ||
h = 2.0 + (_col.b - _col.r) / delta; | ||
} else { | ||
h = 4.0 + (_col.r - _col.g) / delta; | ||
} | ||
|
||
h *= 60.0; | ||
if (h < 0.0) { | ||
h += 360.0; | ||
} | ||
|
||
return vec3<f32>(h / 360.0, s, v); | ||
} | ||
|
||
fn hsv2rgb(_c: vec3<f32>) -> vec3<f32> { | ||
let K = vec4<f32>(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); | ||
var p = abs(fract(vec3<f32>(_c.x) + K.xyz) * 6.0 - K.www); | ||
p = clamp(p - K.xxx, vec3<f32>(0.0, 0.0, 0.0), vec3<f32>(1.0, 1.0, 1.0)); | ||
return _c.z * mix(K.xxx, p, _c.y); | ||
} | ||
|
||
|
||
fn smooth_curve(_x: f32) -> f32 { | ||
// return 6.0 * _x * _x * _x * _x * _x - 15.0 * _x * _x * _x * _x + 10.0 * _x * _x * _x; | ||
return 6.0 * pow(_x, 5.0) - 15.0 * pow(_x, 4.0) + 10.0 * pow(_x, 3.0); | ||
} | ||
|
||
fn Gauss(_dist: f32) -> f32 { | ||
return exp(-10.0 * _dist * _dist); | ||
} | ||
|
||
fn Gauss_sq(_dist_sq: f32) -> f32 { | ||
return exp(-10.0 * _dist_sq); | ||
} | ||
|
||
fn palette(_t: f32, _a: vec3<f32>, _b: vec3<f32>, _c: vec3<f32>, _d: vec3<f32>) -> vec3<f32> { | ||
return _a + _b * cos(6.28318 * (_c * _t + _d)); | ||
} | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|