Skip to content

Commit 8e79f98

Browse files
committed
Use core lib constants.
1 parent 8f346e1 commit 8e79f98

16 files changed

+177
-51
lines changed

shaders/src/bubble_buckey_balls.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,9 @@
88
//! // Tested on Nvidia GTX 780 Windows 7
99
//! ```
1010
11-
use crate::{ConstantColor, RgbCube, SampleCube};
11+
use core::f32::consts::{FRAC_1_PI, PI};
12+
13+
use crate::{constants::TWO_PI, ConstantColor, RgbCube, SampleCube};
1214
use shared::*;
1315
use spirv_std::glam::{vec2, vec3, Vec2, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles};
1416

@@ -76,10 +78,6 @@ impl<C0, C1> State<C0, C1> {
7678
// **************************************************************************
7779
// CONSTANTS
7880

79-
const PI: f32 = 3.14159;
80-
const TWO_PI: f32 = 6.28318;
81-
const _PI_OVER_TWO: f32 = 1.570796;
82-
const ONE_OVER_PI: f32 = 0.318310;
8381
const GR: f32 = 1.61803398;
8482

8583
const SMALL_FLOAT: f32 = 0.0001;
@@ -471,10 +469,10 @@ impl<C0: SampleCube, C1: SampleCube> State<C0, C1> {
471469
if ndl > 0. {
472470
let frk: f32 = 0.5 + 2.0 * costd * costd * surf.roughness;
473471
let diff: Vec3 = surf.basecolor
474-
* ONE_OVER_PI
472+
* FRAC_1_PI
475473
* (1. + (frk - 1.) * pow5(1. - costl))
476474
* (1. + (frk - 1.) * pow5(1. - costv));
477-
//let diff: Vec3 = surf.basecolor * ONE_OVER_PI; // lambert
475+
//let diff: Vec3 = surf.basecolor * FRAC_1_PI; // lambert
478476

479477
// D(h) factor
480478
// using the GGX approximation where the gamma factor is 2.

shaders/src/constants.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
pub const TWO_PI: f32 = 2.0 * core::f32::consts::PI;
2+
pub const SQRT3: f32 = 1.7320508075688772;

shaders/src/geodesic_tiling.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use spirv_std::glam::{mat2, vec2, vec3, Mat2, Mat3, Vec2, Vec3, Vec3Swizzles, Ve
88
#[cfg(target_arch = "spirv")]
99
use spirv_std::num_traits::Float;
1010

11-
use crate::{ShaderDefinition, ShaderInput, ShaderResult};
11+
use crate::{constants::SQRT3, ShaderDefinition, ShaderInput, ShaderResult};
1212

1313
pub const SHADER_DEFINITION: ShaderDefinition = ShaderDefinition {
1414
name: "Geodesic Tiling",
@@ -147,15 +147,11 @@ impl State {
147147
// Adapted from mattz https://www.shadertoy.com/view/4d2GzV
148148
// --------------------------------------------------------
149149

150-
const SQRT3: f32 = 1.7320508075688772;
151150
const I3: f32 = 0.5773502691896258;
152151

153152
const CART2HEX: Mat2 = mat2(vec2(1.0, 0.0), vec2(I3, 2.0 * I3));
154153
const HEX2CART: Mat2 = mat2(vec2(1.0, 0.0), vec2(-0.5, 0.5 * SQRT3));
155154

156-
const _PHI: f32 = 1.618033988749895;
157-
const _TAU: f32 = 6.283185307179586;
158-
159155
struct TriPoints {
160156
a: Vec2,
161157
b: Vec2,

shaders/src/heart.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//! // License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
77
//! ```
88
9+
use core::f32::consts::PI;
10+
911
use shared::*;
1012
use spirv_std::glam::{vec2, vec3, Vec2, Vec3, Vec3Swizzles, Vec4};
1113

@@ -36,7 +38,7 @@ pub struct Inputs {
3638
impl Inputs {
3739
pub fn main_image(&self, frag_color: &mut Vec4, frag_coord: Vec2) {
3840
let mut p: Vec2 =
39-
(2.0 * frag_coord - self.resolution.xy()) / (self.resolution.y.min(self.resolution.x));
41+
(2.0 * frag_coord - self.resolution.xy()) / (self.resolution.xy().min_element());
4042

4143
// background color
4244
let bcol: Vec3 = vec3(1.0, 0.8, 0.7 - 0.07 * p.y) * (1.0 - 0.25 * p.length());
@@ -58,7 +60,7 @@ impl Inputs {
5860
d = 0.5;
5961
} else {
6062
p.y -= 0.25;
61-
let a: f32 = p.x.atan2(p.y) / 3.141593;
63+
let a: f32 = p.x.atan2(p.y) / PI;
6264
r = p.length();
6365
let h: f32 = a.abs();
6466
d = (13.0 * h - 22.0 * h * h + 10.0 * h * h * h) / (6.0 - 5.0 * h);

shaders/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ pub mod apollonian;
1212
pub mod atmosphere_system_test;
1313
pub mod bubble_buckey_balls;
1414
pub mod clouds;
15+
pub mod constants;
1516
pub mod filtering_procedurals;
1617
pub mod flappy_bird;
1718
pub mod galaxy_of_universes;
@@ -75,7 +76,7 @@ pub struct ShaderResult {
7576
}
7677

7778
pub struct ShaderDefinition {
78-
name: &'static str,
79+
pub name: &'static str,
7980
}
8081

8182
macro_rules! match_index {
@@ -106,7 +107,8 @@ macro_rules! render_shader_macro {
106107
}
107108

108109
render_shader_macro!(
109-
28,
110+
29,
111+
loading_repeating_circles,
110112
two_tweets,
111113
heart,
112114
clouds,
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
//! Created by raldone01 :D
2+
3+
use core::f32::consts::PI;
4+
5+
use shared::*;
6+
use spirv_std::glam::{vec2, vec3, vec4, Vec2, Vec3, Vec3Swizzles, Vec4};
7+
8+
// Note: This cfg is incorrect on its surface, it really should be "are we compiling with std", but
9+
// we tie #[no_std] above to the same condition, so it's fine.
10+
#[cfg(target_arch = "spirv")]
11+
use spirv_std::num_traits::Float;
12+
13+
use crate::{ShaderDefinition, ShaderInput, ShaderResult};
14+
15+
pub const SHADER_DEFINITION: ShaderDefinition = ShaderDefinition {
16+
name: "Loading Repeating Circles",
17+
};
18+
19+
pub fn shader_fn(render_instruction: &ShaderInput, render_result: &mut ShaderResult) {
20+
let color = &mut render_result.color;
21+
let (resolution, time, frag_coord) = (
22+
render_instruction.resolution,
23+
render_instruction.time,
24+
render_instruction.frag_coord,
25+
);
26+
Inputs { resolution, time }.main_image(color, frag_coord)
27+
}
28+
29+
pub struct Inputs {
30+
pub resolution: Vec3,
31+
pub time: f32,
32+
}
33+
34+
fn circle_outline(uv: Vec2, center: Vec2, radius: f32, thickness: f32) -> f32 {
35+
// Compute distance from pixel to circle center.
36+
let dist = (uv - center).length();
37+
// Half thickness for symmetric band.
38+
let half_th = thickness * 0.5;
39+
// Return 1 if pixel is within the thickness band.
40+
return (dist - radius).abs().step(half_th);
41+
}
42+
43+
/// The speed is in degrees per second.
44+
fn rotating_discrete_circle(
45+
center: Vec2,
46+
radius: f32,
47+
time: f32,
48+
speed: f32,
49+
num_circles: i32,
50+
cirle_index: i32,
51+
) -> Vec2 {
52+
// convert speed from degrees/sec to radians/sec
53+
let speed_rad = speed * PI / 180.0;
54+
// angle step between discrete circles
55+
let angle_step = 2.0 * PI / num_circles as f32;
56+
// base angle for this circle index
57+
let base_angle = angle_step * cirle_index as f32;
58+
// total rotation angle
59+
let angle = base_angle + time * speed_rad;
60+
// compute offset from center
61+
let offset = vec2(angle.cos(), angle.sin()) * radius;
62+
// return world‐space position
63+
center + offset
64+
}
65+
66+
impl Inputs {
67+
pub fn main_image(&self, frag_color: &mut Vec4, frag_coord: Vec2) {
68+
// Get screen dimensions as Vec2.
69+
let screen_xy = self.resolution.xy();
70+
// Determine the shorter dimension of the screen.
71+
let shorter_dim = screen_xy.min_element();
72+
73+
// Compute normalized pixel coordinates.
74+
// This maps the center of the screen to (0,0) and the shortest side to [-1,1].
75+
// Aspect ratio is preserved.
76+
let uv = (frag_coord - screen_xy * 0.5) / shorter_dim * 2.0;
77+
78+
let aspect = screen_xy / shorter_dim;
79+
80+
let mut combined_mask: f32 = 0.0;
81+
82+
let center = vec2(0.0, 0.0);
83+
let bottom_middle = vec2(0.0, -aspect.y);
84+
let top_middle = vec2(0.0, aspect.y);
85+
let left_middle = vec2(-aspect.x, 0.0);
86+
let right_middle = vec2(aspect.x, 0.0);
87+
88+
// Define circle radius and outline thickness.
89+
// These values are relative to the shorter screen dimension.
90+
let radius = 0.3;
91+
let thickness = 0.01;
92+
93+
// Outline masks for center + four midpoints
94+
let m_center = circle_outline(uv, center, radius, thickness);
95+
combined_mask = combined_mask.max(m_center);
96+
let m_bot = circle_outline(uv, bottom_middle, radius, thickness);
97+
combined_mask = combined_mask.max(m_bot);
98+
let m_top = circle_outline(uv, top_middle, radius, thickness);
99+
combined_mask = combined_mask.max(m_top);
100+
let m_left = circle_outline(uv, left_middle, radius, thickness);
101+
combined_mask = combined_mask.max(m_left);
102+
let m_right = circle_outline(uv, right_middle, radius, thickness);
103+
combined_mask = combined_mask.max(m_right);
104+
105+
// rotating circles
106+
let num_circles = 8;
107+
let speed = 2.0;
108+
for i in 0..num_circles {
109+
// Compute the position of the circle based on the angle and radius.
110+
let pos = rotating_discrete_circle(bottom_middle, 0.5, self.time, speed, num_circles, i);
111+
// Compute the outline mask for the current circle.
112+
let m = circle_outline(uv, pos, radius, thickness);
113+
// Combine masks using max to create a single mask.
114+
combined_mask = combined_mask.max(m);
115+
}
116+
117+
// Mix white and black based on mask.
118+
// With current mask: outside band (mask=1) is black, inside band (mask=0) is white.
119+
// This produces a white outline on a black background.
120+
let color_rgb = mix(Vec3::ONE, Vec3::ZERO, combined_mask);
121+
122+
// Output final pixel color with alpha = 1.0.
123+
*frag_color = color_rgb.extend(1.0);
124+
}
125+
}

shaders/src/luminescence.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
//! // Twitter: @The_ArtOfCode
1818
//! ```
1919
20+
use core::f32::consts::PI;
21+
2022
use shared::*;
2123
use spirv_std::glam::{vec2, vec3, Mat3, Vec2, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles};
2224

@@ -25,7 +27,7 @@ use spirv_std::glam::{vec2, vec3, Mat3, Vec2, Vec3, Vec3Swizzles, Vec4, Vec4Swiz
2527
#[cfg(target_arch = "spirv")]
2628
use spirv_std::num_traits::Float;
2729

28-
use crate::{ShaderDefinition, ShaderInput, ShaderResult};
30+
use crate::{constants::TWO_PI, ShaderDefinition, ShaderInput, ShaderResult};
2931

3032
pub const SHADER_DEFINITION: ShaderDefinition = ShaderDefinition {
3133
name: "Luminescence",
@@ -99,10 +101,6 @@ const _LF: Vec3 = vec3(1.0, 0.0, 0.0);
99101
const UP: Vec3 = vec3(0.0, 1.0, 0.0);
100102
const _FW: Vec3 = vec3(0.0, 0.0, 1.0);
101103

102-
const _HALF_PI: f32 = 1.570796326794896619;
103-
const PI: f32 = 3.141592653589793238;
104-
const TWO_PI: f32 = 6.283185307179586;
105-
106104
const ACCENT_COLOR1: Vec3 = vec3(1.0, 0.1, 0.5);
107105
const SECOND_COLOR1: Vec3 = vec3(0.1, 0.5, 1.0);
108106
const ACCENT_COLOR2: Vec3 = vec3(1.0, 0.5, 0.1);

shaders/src/morphing.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
//! // License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
77
//! ```
88
9+
use core::f32::consts::PI;
10+
911
use shared::*;
1012
use spirv_std::glam::{
1113
vec2, vec3, Mat2, Vec2, Vec2Swizzles, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles,
@@ -16,7 +18,7 @@ use spirv_std::glam::{
1618
#[cfg(target_arch = "spirv")]
1719
use spirv_std::num_traits::Float;
1820

19-
use crate::{ShaderDefinition, ShaderInput, ShaderResult};
21+
use crate::{constants::TWO_PI, ShaderDefinition, ShaderInput, ShaderResult};
2022

2123
pub const SHADER_DEFINITION: ShaderDefinition = ShaderDefinition {
2224
name: "Morphing Teapot",
@@ -161,7 +163,7 @@ impl State {
161163

162164
// Distance to other shapes ---------------------------------------------
163165
let mut d_shape: f32;
164-
let id_morph: i32 = ((0.5 + (self.inputs.time) / (2.0 * 3.141592658)).floor() % 3.0) as i32;
166+
let id_morph: i32 = ((0.5 + (self.inputs.time) / (TWO_PI)).floor() % 3.0) as i32;
165167

166168
if id_morph == 1 {
167169
p = (self.mat2_rot.transpose() * p.xz()).extend(p.y).xzy();
@@ -213,7 +215,7 @@ impl State {
213215
}
214216

215217
pub fn main_image(&mut self, frag_color: &mut Vec4, frag_coord: Vec2) {
216-
let aa: f32 = 3.14159 / 4.0;
218+
let aa: f32 = PI / 4.0;
217219
self.mat2_rot = Mat2::from_cols_array(&[aa.cos(), aa.sin(), -aa.sin(), aa.cos()]);
218220

219221
// Morphing step

shaders/src/on_off_spikes.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
//! // On/Off Spikes, fragment shader by movAX13h, oct 2014
66
//! ```
77
8+
use core::f32::consts::FRAC_PI_2;
9+
810
use shared::*;
911
use spirv_std::glam::{
1012
vec2, vec3, Mat2, Mat3, Vec2, Vec2Swizzles, Vec3, Vec3Swizzles, Vec4, Vec4Swizzles,
@@ -15,7 +17,7 @@ use spirv_std::glam::{
1517
#[cfg(target_arch = "spirv")]
1618
use spirv_std::num_traits::Float;
1719

18-
use crate::{ShaderDefinition, ShaderInput, ShaderResult};
20+
use crate::{constants::TWO_PI, ShaderDefinition, ShaderInput, ShaderResult};
1921

2022
pub const SHADER_DEFINITION: ShaderDefinition = ShaderDefinition {
2123
name: "On/Off Spikes",
@@ -86,13 +88,8 @@ const TENTACLE_COL: Vec3 = vec3(0.06, 0.06, 0.06);
8688

8789
const GAMMA: f32 = 2.2;
8890

89-
//---
90-
const PI2: f32 = 6.283185307179586476925286766559;
91-
const PIH: f32 = 1.5707963267949;
92-
9391
// Using the nebula function of the "Star map shader" by morgan3d
9492
// as environment map and light sphere texture (https://www.shadertoy.com/view/4sBXzG)
95-
const _PI: f32 = 3.1415927;
9693
const NUM_OCTAVES: i32 = 4;
9794
fn hash(n: f32) -> f32 {
9895
(n.sin() * 1e4).fract_gl()
@@ -232,7 +229,7 @@ impl State {
232229
let mut a: f32 = q.z.atan2(q.x);
233230
a += 0.4 * (r - self.inputs.time).sin();
234231

235-
q = vec3(a * NUM_TENTACLES as f32 / PI2, q.y, q.xz().length()); // circular domain
232+
q = vec3(a * NUM_TENTACLES as f32 / TWO_PI, q.y, q.xz().length()); // circular domain
236233
q = vec3(q.x.rem_euclid(1.0) - 0.5 * 1.0, q.y, q.z); // repetition
237234

238235
d3 = sd_capped_cylinder(
@@ -353,9 +350,9 @@ impl State {
353350
let mrel: Vec2 = self.inputs.mouse.xy() / self.inputs.resolution.xy() - Vec2::splat(0.5);
354351
let mdis: f32 = 8.0 + 6.0 * mrel.y;
355352
cp = vec3(
356-
mdis * (-mrel.x * PIH).cos(),
353+
mdis * (-mrel.x * FRAC_PI_2).cos(),
357354
4.0 * mrel.y,
358-
mdis * (-mrel.x * PIH).sin(),
355+
mdis * (-mrel.x * FRAC_PI_2).sin(),
359356
);
360357
}
361358

shaders/src/phantom_star.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use core::f32::consts::PI;
99
#[cfg(target_arch = "spirv")]
1010
use {shared::FloatExt, spirv_std::num_traits::Float};
1111

12-
use crate::{ShaderDefinition, ShaderInput, ShaderResult};
12+
use crate::{constants::TWO_PI, ShaderDefinition, ShaderInput, ShaderResult};
1313

1414
pub const SHADER_DEFINITION: ShaderDefinition = ShaderDefinition {
1515
name: "Phantom Star",
@@ -36,13 +36,9 @@ fn rot(a: f32) -> Mat2 {
3636
Mat2::from_cols_array(&[c, s, -s, c])
3737
}
3838

39-
//const pi: f32 = (-1.0).acos();
40-
const PI_: f32 = PI;
41-
const PI2: f32 = PI_ * 2.0;
42-
4339
fn pmod(p: Vec2, r: f32) -> Vec2 {
44-
let mut a: f32 = p.x.atan2(p.y) + PI_ / r;
45-
let n: f32 = PI2 / r;
40+
let mut a: f32 = p.x.atan2(p.y) + PI / r;
41+
let n: f32 = TWO_PI / r;
4642
a = (a / n).floor() * n;
4743
rot(-a).transpose() * p
4844
}

0 commit comments

Comments
 (0)