├── .gitignore ├── src ├── shaders │ ├── time │ │ ├── mod.rs │ │ ├── time.vert │ │ ├── time.frag │ │ └── main.rs │ ├── blend │ │ ├── mod.rs │ │ ├── blend.vert │ │ ├── blend.frag │ │ └── main.rs │ ├── candy │ │ ├── mod.rs │ │ ├── candy.vert │ │ ├── candy.frag │ │ └── main.rs │ ├── fire │ │ ├── mod.rs │ │ ├── fire.vert │ │ ├── fire.frag │ │ └── main.rs │ ├── normal │ │ ├── mod.rs │ │ ├── normal.frag │ │ ├── normal.vert │ │ └── main.rs │ ├── spawn │ │ ├── mod.rs │ │ ├── spawn.vert │ │ ├── spawn.frag │ │ └── main.rs │ ├── uv │ │ ├── mod.rs │ │ ├── uv.frag │ │ ├── uv.vert │ │ └── main.rs │ └── mod.rs └── lib.rs ├── assets └── fire.png ├── rustfmt.toml ├── glsl ├── test.frag ├── circle.frag ├── time_demo.frag ├── sin_time.frag ├── bottom_clamp.frag ├── move_circle.frag ├── polygon.frag ├── vertical_displacement.frag ├── rect.frag ├── simple_noise_2d.frag ├── cellular.frag ├── classic_perlin.frag ├── classic_perlin_animated.frag └── magma.frag ├── README.md ├── Cargo.toml ├── LICENSE ├── .cargo └── config.toml └── Cargo.lock /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | -------------------------------------------------------------------------------- /src/shaders/time/mod.rs: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /src/lib.rs: -------------------------------------------------------------------------------- 1 | pub mod shaders; 2 | -------------------------------------------------------------------------------- /src/shaders/blend/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod main; 2 | -------------------------------------------------------------------------------- /src/shaders/candy/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod main; 2 | -------------------------------------------------------------------------------- /src/shaders/fire/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod main; 2 | -------------------------------------------------------------------------------- /src/shaders/normal/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod main; 2 | -------------------------------------------------------------------------------- /src/shaders/spawn/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod main; 2 | -------------------------------------------------------------------------------- /src/shaders/uv/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod main; 2 | -------------------------------------------------------------------------------- /assets/fire.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/wilk10/shader_practice/HEAD/assets/fire.png -------------------------------------------------------------------------------- /rustfmt.toml: -------------------------------------------------------------------------------- 1 | imports_granularity = "Crate" 2 | use_field_init_shorthand = true 3 | wrap_comments = true 4 | -------------------------------------------------------------------------------- /src/shaders/mod.rs: -------------------------------------------------------------------------------- 1 | pub mod blend; 2 | pub mod candy; 3 | pub mod fire; 4 | pub mod normal; 5 | pub mod spawn; 6 | pub mod time; 7 | pub mod uv; 8 | -------------------------------------------------------------------------------- /glsl/test.frag: -------------------------------------------------------------------------------- 1 | #ifdef GL_ES 2 | precision mediump float; 3 | #endif 4 | 5 | void main() { 6 | vec3 colour = vec3(1.0, 0.6, 0.3); 7 | 8 | gl_FragColor = vec4(colour, 1.0); 9 | } 10 | -------------------------------------------------------------------------------- /src/shaders/normal/normal.frag: -------------------------------------------------------------------------------- 1 | #version 450 2 | 3 | layout(location = 0) in vec3 normal; 4 | layout(location = 0) out vec4 o_Target; 5 | 6 | void main() { 7 | o_Target = vec4(normal, 1.0); 8 | } 9 | -------------------------------------------------------------------------------- /src/shaders/uv/uv.frag: -------------------------------------------------------------------------------- 1 | #version 450 2 | 3 | layout(location = 0) in vec2 uv; 4 | layout(location = 0) out vec4 o_Target; 5 | 6 | void main() 7 | { 8 | o_Target = vec4(uv, 0.0, 1.0); 9 | } 10 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Shader Practice 2 | 3 | This repo is a collection of shader examples, to practice making shaders via [Bevy Engine](https://github.com/bevyengine/bevy). 4 | 5 | ## Run examples 6 | To run each example separately use: 7 | ```sh 8 | cargo run --example [example_name] 9 | ``` 10 | for example: 11 | ```sh 12 | cargo run --example fire 13 | ``` -------------------------------------------------------------------------------- /glsl/circle.frag: -------------------------------------------------------------------------------- 1 | #ifdef GL_ES 2 | precision mediump float; 3 | #endif 4 | 5 | uniform vec2 u_resolution; 6 | 7 | float circleShape(vec2 position, float radius) { 8 | return step(radius, length(position - vec2(0.5))); 9 | } 10 | 11 | void main() { 12 | vec2 position = gl_FragCoord.xy / u_resolution; 13 | float circle = circleShape(position, 0.2); 14 | vec3 colour = vec3(circle); 15 | gl_FragColor = vec4(colour, 1.0); 16 | } 17 | -------------------------------------------------------------------------------- /src/shaders/uv/uv.vert: -------------------------------------------------------------------------------- 1 | #version 450 2 | 3 | layout(location = 0) in vec3 Vertex_Position; 4 | layout(location = 1) in vec2 Vertex_Uv; 5 | layout(location = 0) out vec2 uv; 6 | 7 | layout(set = 0, binding = 0) uniform CameraViewProj { 8 | mat4 ViewProj; 9 | }; 10 | 11 | layout(set = 1, binding = 0) uniform Transform { 12 | mat4 Model; 13 | }; 14 | 15 | void main() { 16 | gl_Position = ViewProj * Model * vec4(Vertex_Position, 1.0); 17 | uv = Vertex_Uv; 18 | } 19 | -------------------------------------------------------------------------------- /src/shaders/blend/blend.vert: -------------------------------------------------------------------------------- 1 | #version 450 2 | 3 | layout(location = 0) in vec3 Vertex_Position; 4 | layout(location = 1) in vec2 Vertex_Uv; 5 | layout(location = 0) out vec2 uv; 6 | 7 | layout(set = 0, binding = 0) uniform CameraViewProj { 8 | mat4 ViewProj; 9 | }; 10 | 11 | layout(set = 1, binding = 0) uniform Transform { 12 | mat4 Model; 13 | }; 14 | 15 | void main() { 16 | gl_Position = ViewProj * Model * vec4(Vertex_Position, 1.0); 17 | uv = Vertex_Uv; 18 | } 19 | -------------------------------------------------------------------------------- /src/shaders/candy/candy.vert: -------------------------------------------------------------------------------- 1 | #version 450 2 | 3 | layout(location = 0) in vec3 Vertex_Position; 4 | layout(location = 1) in vec2 Vertex_Uv; 5 | layout(location = 0) out vec2 uv; 6 | 7 | layout(set = 0, binding = 0) uniform CameraViewProj { 8 | mat4 ViewProj; 9 | }; 10 | 11 | layout(set = 1, binding = 0) uniform Transform { 12 | mat4 Model; 13 | }; 14 | 15 | void main() { 16 | gl_Position = ViewProj * Model * vec4(Vertex_Position, 1.0); 17 | uv = Vertex_Uv; 18 | } 19 | -------------------------------------------------------------------------------- /src/shaders/fire/fire.vert: -------------------------------------------------------------------------------- 1 | #version 450 2 | 3 | layout(location = 0) in vec3 Vertex_Position; 4 | layout(location = 1) in vec2 Vertex_Uv; 5 | layout(location = 0) out vec2 uv; 6 | 7 | layout(set = 0, binding = 0) uniform CameraViewProj { 8 | mat4 ViewProj; 9 | }; 10 | 11 | layout(set = 1, binding = 0) uniform Transform { 12 | mat4 Model; 13 | }; 14 | 15 | void main() { 16 | gl_Position = ViewProj * Model * vec4(Vertex_Position, 1.0); 17 | uv = Vertex_Uv; 18 | } 19 | -------------------------------------------------------------------------------- /src/shaders/spawn/spawn.vert: -------------------------------------------------------------------------------- 1 | #version 450 2 | 3 | layout(location = 0) in vec3 Vertex_Position; 4 | layout(location = 1) in vec2 Vertex_Uv; 5 | layout(location = 0) out vec2 uv; 6 | 7 | layout(set = 0, binding = 0) uniform CameraViewProj { 8 | mat4 ViewProj; 9 | }; 10 | 11 | layout(set = 1, binding = 0) uniform Transform { 12 | mat4 Model; 13 | }; 14 | 15 | void main() { 16 | gl_Position = ViewProj * Model * vec4(Vertex_Position, 1.0); 17 | uv = Vertex_Uv; 18 | } 19 | -------------------------------------------------------------------------------- /src/shaders/time/time.vert: -------------------------------------------------------------------------------- 1 | #version 450 2 | 3 | layout(location = 0) in vec3 Vertex_Position; 4 | layout(location = 1) in vec2 Vertex_Uv; 5 | layout(location = 0) out vec2 uv; 6 | 7 | layout(set = 0, binding = 0) uniform CameraViewProj { 8 | mat4 ViewProj; 9 | }; 10 | 11 | layout(set = 1, binding = 0) uniform Transform { 12 | mat4 Model; 13 | }; 14 | 15 | void main() { 16 | gl_Position = ViewProj * Model * vec4(Vertex_Position, 1.0); 17 | uv = Vertex_Uv; 18 | } 19 | -------------------------------------------------------------------------------- /glsl/time_demo.frag: -------------------------------------------------------------------------------- 1 | #ifdef GL_ES 2 | precision mediump float; 3 | #endif 4 | 5 | uniform vec2 u_resolution; 6 | uniform float u_time; 7 | 8 | void main() { 9 | vec2 uv = gl_FragCoord.xy / u_resolution; 10 | float translation = sin(u_time / 2.0); 11 | float threshold = uv.x + translation * 0.5; 12 | 13 | vec3 red = vec3(1.,0.,0.); 14 | vec3 blue = vec3(0.,0.,1.); 15 | vec3 mixed_colors = mix(red, blue, threshold); 16 | 17 | gl_FragColor = vec4(mixed_colors, 1.0); 18 | } 19 | -------------------------------------------------------------------------------- /src/shaders/normal/normal.vert: -------------------------------------------------------------------------------- 1 | #version 450 2 | 3 | layout(location = 0) in vec3 Vertex_Position; 4 | layout(location = 1) in vec3 Vertex_Normal; 5 | layout(location = 0) out vec3 normal; 6 | 7 | layout(set = 0, binding = 0) uniform CameraViewProj { 8 | mat4 ViewProj; 9 | }; 10 | 11 | layout(set = 1, binding = 0) uniform Transform { 12 | mat4 Model; 13 | }; 14 | 15 | void main() { 16 | gl_Position = ViewProj * Model * vec4(Vertex_Position, 1.0); 17 | normal = Vertex_Normal; 18 | } 19 | -------------------------------------------------------------------------------- /glsl/sin_time.frag: -------------------------------------------------------------------------------- 1 | #ifdef GL_ES 2 | precision mediump float; 3 | #endif 4 | 5 | uniform vec2 u_resolution; 6 | uniform float u_time; 7 | 8 | float circleShape(vec2 position, float radius) { 9 | return step(radius, length(position - vec2(0.5))); 10 | } 11 | 12 | void main() { 13 | vec2 position = gl_FragCoord.xy / u_resolution; 14 | vec2 translation = vec2(sin(u_time / 2.0), 0.0); 15 | position += translation * 0.5; 16 | float circle = circleShape(position, 0.25); 17 | vec3 colour = vec3(circle); 18 | gl_FragColor = vec4(colour, 1.0); 19 | } 20 | -------------------------------------------------------------------------------- /glsl/bottom_clamp.frag: -------------------------------------------------------------------------------- 1 | #ifdef GL_ES 2 | precision mediump float; 3 | #endif 4 | 5 | uniform vec2 u_resolution; 6 | uniform float u_time; 7 | 8 | void main() { 9 | vec2 uv = gl_FragCoord.xy / u_resolution.xy; 10 | uv.x *= u_resolution.x / u_resolution.y; 11 | // uv = vec2(uv.x, 1. - uv.y); 12 | 13 | float bottom_threshold = -0.5; 14 | vec2 squared_uv = sqrt(uv); 15 | float adjusted_bottom = 2. * (1. - squared_uv.y + bottom_threshold); 16 | float clamped_bottom = clamp(adjusted_bottom, 0., 1.); 17 | vec3 result = vec3(1. - clamped_bottom); 18 | 19 | gl_FragColor = vec4(result, 1.0); 20 | } 21 | -------------------------------------------------------------------------------- /src/shaders/time/time.frag: -------------------------------------------------------------------------------- 1 | #version 450 2 | 3 | layout(location = 0) in vec2 uv; 4 | layout(location = 0) out vec4 o_Target; 5 | 6 | layout(set = 2, binding = 0) uniform TimeComponent_value { 7 | float time; 8 | }; 9 | 10 | precision mediump float; 11 | 12 | void main() { 13 | float speed = 0.7; 14 | float translation = sin(time * speed); 15 | float percentage_extent = 0.6; 16 | float threshold = uv.x + translation * percentage_extent; 17 | 18 | vec3 red = vec3(1.,0.,0.); 19 | vec3 blue = vec3(0.,0.,1.); 20 | vec3 mixed_colors = mix(red, blue, threshold); 21 | 22 | o_Target = vec4(mixed_colors, 1.0); 23 | } 24 | -------------------------------------------------------------------------------- /glsl/move_circle.frag: -------------------------------------------------------------------------------- 1 | #ifdef GL_ES 2 | precision mediump float; 3 | #endif 4 | 5 | uniform vec2 u_resolution; 6 | uniform float u_time; 7 | 8 | float makeCircle(in vec2 _st, in float _radius){ 9 | vec2 dist = _st-vec2(0.5); 10 | return 1.-smoothstep( 11 | _radius-(_radius*0.01), 12 | _radius+(_radius*0.01), 13 | dot(dist,dist)*4.0 14 | ); 15 | } 16 | 17 | void main() { 18 | vec2 uv = gl_FragCoord.xy / u_resolution.xy; 19 | 20 | vec2 translate = vec2(sin(u_time), cos(u_time)); 21 | uv += translate * 0.3; 22 | 23 | vec3 circle = 1. - vec3(makeCircle(uv, 0.1)); 24 | 25 | gl_FragColor = vec4(circle, 1.0); 26 | } 27 | -------------------------------------------------------------------------------- /glsl/polygon.frag: -------------------------------------------------------------------------------- 1 | #ifdef GL_ES 2 | precision mediump float; 3 | #endif 4 | 5 | const float PI = 3.1415926535; 6 | 7 | uniform vec2 u_resolution; 8 | 9 | float polygonShape(vec2 position, float radius, float nSides) { 10 | position = position * 2.0 - 1.0; 11 | float angle = atan(position.x, position.y); 12 | float slice = PI * 2.0 / nSides; 13 | return step(radius, cos(floor(0.5 + angle / slice) * slice - angle) * length(position)); 14 | } 15 | 16 | void main() { 17 | vec2 position = gl_FragCoord.xy / u_resolution; 18 | float polygon = polygonShape(position, 0.5, 7.0); 19 | vec3 colour = vec3(0.0); 20 | colour = vec3(polygon); 21 | gl_FragColor = vec4(colour, 1.0); 22 | } 23 | -------------------------------------------------------------------------------- /src/shaders/blend/blend.frag: -------------------------------------------------------------------------------- 1 | #version 450 2 | 3 | layout(location = 0) in vec2 uv; 4 | layout(location = 0) out vec4 o_Target; 5 | 6 | layout(set = 2, binding = 0) uniform BlendColors { 7 | vec4 color_a; 8 | vec4 color_b; 9 | float start_lerp; 10 | float end_lerp; 11 | }; 12 | 13 | precision mediump float; 14 | 15 | float inverseLerp(float from, float to, float value) { 16 | return (value - from) / (to - from); 17 | } 18 | 19 | void main() 20 | { 21 | float inv_lerped_uvx = inverseLerp(start_lerp, end_lerp, uv.x); 22 | float min_value = 0.0; 23 | float max_value = 1.0; 24 | float gradient = clamp(inv_lerped_uvx, min_value, max_value); 25 | o_Target = mix(color_a, color_b, gradient); 26 | } 27 | -------------------------------------------------------------------------------- /Cargo.toml: -------------------------------------------------------------------------------- 1 | [package] 2 | name = "shader_practice" 3 | version = "0.1.0" 4 | authors = ["Anselmo Sampietro "] 5 | edition = "2018" 6 | 7 | [dependencies] 8 | bevy = "0.5.0" 9 | 10 | [[example]] 11 | name = "blend" 12 | path = "src/shaders/blend/main.rs" 13 | 14 | [[example]] 15 | name = "candy" 16 | path = "src/shaders/candy/main.rs" 17 | 18 | [[example]] 19 | name = "fire" 20 | path = "src/shaders/fire/main.rs" 21 | 22 | [[example]] 23 | name = "normal" 24 | path = "src/shaders/normal/main.rs" 25 | 26 | [[example]] 27 | name = "spawn" 28 | path = "src/shaders/spawn/main.rs" 29 | 30 | [[example]] 31 | name = "time" 32 | path = "src/shaders/time/main.rs" 33 | 34 | [[example]] 35 | name = "uv" 36 | path = "src/shaders/uv/main.rs" 37 | -------------------------------------------------------------------------------- /glsl/vertical_displacement.frag: -------------------------------------------------------------------------------- 1 | #ifdef GL_ES 2 | precision mediump float; 3 | #endif 4 | 5 | uniform vec2 u_resolution; 6 | uniform float u_time; 7 | 8 | vec2 animateVertically(vec2 input_uv, float time, float factor) { 9 | vec2 vertical_animation = vec2(0., sin(factor * time)); 10 | vec2 displace_vertically = input_uv + vertical_animation; 11 | // vec2 clamp_displacement = clamp(displace_vertically, 0., 1.); 12 | return displace_vertically; 13 | } 14 | 15 | void main() { 16 | vec2 uv = gl_FragCoord.xy / u_resolution.xy; 17 | // uv = vec2(uv.x, 1. - uv.y); 18 | 19 | // vec2 result = uv; 20 | vec2 result = animateVertically(uv, u_time, 0.5); 21 | // result = clamp(result, 0., 1.); 22 | 23 | gl_FragColor = vec4(result, 0.0, 1.0); 24 | } 25 | -------------------------------------------------------------------------------- /src/shaders/candy/candy.frag: -------------------------------------------------------------------------------- 1 | #version 450 2 | 3 | layout(location = 0) in vec2 uv; 4 | layout(location = 0) out vec4 o_Target; 5 | 6 | layout(set = 2, binding = 0) uniform Candy { 7 | vec4 color_a; 8 | vec4 color_b; 9 | float start_lerp; 10 | float end_lerp; 11 | }; 12 | 13 | float inverseLerp(float from, float to, float value) { 14 | return (value - from) / (to - from); 15 | } 16 | 17 | void main() 18 | { 19 | float tau = 6.283185307179586; 20 | float lerped_coords = inverseLerp(start_lerp, end_lerp, (uv.x + uv.y)); 21 | float repetitions = 5.0; 22 | float pattern_minus1to1 = cos(lerped_coords * tau * repetitions); 23 | float pattern_from0to1 = pattern_minus1to1 * 0.5 + 0.5; 24 | o_Target = mix(color_a, color_b, pattern_from0to1); 25 | } 26 | -------------------------------------------------------------------------------- /glsl/rect.frag: -------------------------------------------------------------------------------- 1 | #ifdef GL_ES 2 | precision mediump float; 3 | #endif 4 | 5 | uniform vec2 u_resolution; 6 | 7 | float rectShape(vec2 position, vec2 dim) { 8 | float scalingFactor = 0.5; 9 | dim = vec2(scalingFactor) - dim * scalingFactor; 10 | vec2 shaper = vec2(step(dim.x, position.x), step(dim.y, position.y)); 11 | shaper *= vec2(step(dim.x, 1.0 - position.x), step(dim.y, 1.0 - position.y)); 12 | return shaper.x * shaper.y; 13 | } 14 | 15 | void main() { 16 | vec2 position = gl_FragCoord.xy / u_resolution; 17 | float width = 0.3; 18 | float height = 0.5; 19 | vec2 dimensions = vec2(width, height); 20 | float rectangle = rectShape(position, dimensions); 21 | vec3 colour = vec3(0.0); 22 | colour = vec3(rectangle); 23 | gl_FragColor = vec4(colour, 1.0); 24 | } 25 | -------------------------------------------------------------------------------- /src/shaders/spawn/spawn.frag: -------------------------------------------------------------------------------- 1 | #version 450 2 | 3 | layout(location = 0) in vec2 uv; 4 | layout(location = 0) out vec4 o_Target; 5 | 6 | layout(set = 2, binding = 0) uniform SpawnVfx { 7 | vec4 color_a; 8 | vec4 color_b; 9 | float start_lerp; 10 | float end_lerp; 11 | }; 12 | 13 | layout(set = 3, binding = 0) uniform TimeComponent_value { 14 | float time; 15 | }; 16 | 17 | void main() 18 | { 19 | float tau = 6.283185307179586; 20 | 21 | float horizontal_repetitions = 8.0; 22 | float dampen_factor = 0.01; 23 | float offset = cos(uv.x * tau * horizontal_repetitions) * dampen_factor; 24 | float time_adjustment = 0.2; 25 | float wiggle = uv.y + offset + time * time_adjustment; 26 | 27 | float vertical_repetitions = 5.0; 28 | float pattern_minus1to1 = cos(wiggle * tau * vertical_repetitions); 29 | float pattern_from0to1 = pattern_minus1to1 * 0.5 + 0.5; 30 | 31 | float fade = pattern_from0to1 * uv.y; 32 | vec4 effect = mix(color_a, color_b, fade); 33 | o_Target = effect; 34 | } 35 | -------------------------------------------------------------------------------- /glsl/simple_noise_2d.frag: -------------------------------------------------------------------------------- 1 | // From Patricio Gonzalez Vivo: 2 | // https://gist.github.com/patriciogonzalezvivo/670c22f3966e662d2f83 3 | 4 | #ifdef GL_ES 5 | precision mediump float; 6 | #endif 7 | 8 | uniform vec2 u_resolution; 9 | uniform float u_time; 10 | 11 | float hash(vec2 p) { return fract(1e4 * sin(17.0 * p.x + p.y * 0.1) * (0.1 + abs(sin(p.y * 13.0 + p.x)))); } 12 | 13 | float noiseFunc(vec2 x) { 14 | vec2 i = floor(x); 15 | vec2 f = fract(x); 16 | 17 | // Four corners in 2D of a tile 18 | float a = hash(i); 19 | float b = hash(i + vec2(1.0, 0.0)); 20 | float c = hash(i + vec2(0.0, 1.0)); 21 | float d = hash(i + vec2(1.0, 1.0)); 22 | 23 | vec2 u = f * f * (3.0 - 2.0 * f); 24 | return mix(a, b, u.x) + (c - a) * u.y * (1.0 - u.x) + (d - b) * u.x * u.y; 25 | } 26 | 27 | void main() { 28 | vec2 uv = gl_FragCoord.xy / u_resolution.xy; 29 | uv.x *= u_resolution.x / u_resolution.y; 30 | 31 | float scale = 500.; 32 | uv *= scale; 33 | 34 | vec3 noise = vec3(noiseFunc(uv)); 35 | 36 | vec3 base_color = vec3(0.5, 0.5, 0.5); 37 | noise += base_color; 38 | 39 | gl_FragColor = vec4(noise, 1.0); 40 | } 41 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Anselmo Sampietro 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /.cargo/config.toml: -------------------------------------------------------------------------------- 1 | # Add the contents of this file to `config.toml` to enable "fast build" configuration. Please read the notes below. 2 | 3 | # NOTE: For maximum performance, build using a nightly compiler 4 | # If you are using rust stable, remove the "-Zshare-generics=y" below (as well as "-Csplit-debuginfo=unpacked" when building on macOS). 5 | 6 | [target.x86_64-unknown-linux-gnu] 7 | linker = "/usr/bin/clang" 8 | rustflags = ["-Clink-arg=-fuse-ld=lld", "-Zshare-generics=y"] 9 | 10 | # NOTE: you must manually install https://github.com/michaeleisel/zld on mac. you can easily do this with the "brew" package manager: 11 | # `brew install michaeleisel/zld/zld` 12 | [target.x86_64-apple-darwin] 13 | rustflags = ["-C", "link-arg=-fuse-ld=/usr/local/bin/zld", "-Zshare-generics=y", "-Csplit-debuginfo=unpacked"] 14 | 15 | [target.x86_64-pc-windows-msvc] 16 | linker = "rust-lld.exe" 17 | rustflags = ["-Zshare-generics=y"] 18 | 19 | # Optional: Uncommenting the following improves compile times, but reduces the amount of debug info to 'line number tables only' 20 | # In most cases the gains are negligible, but if you are on macos and have slow compile times you should see significant gains. 21 | [profile.dev] 22 | debug = 1 23 | -------------------------------------------------------------------------------- /glsl/cellular.frag: -------------------------------------------------------------------------------- 1 | // From Patricio Gonzalez Vivo: https://thebookofshaders.com/12/ 2 | 3 | #ifdef GL_ES 4 | precision mediump float; 5 | #endif 6 | 7 | uniform vec2 u_resolution; 8 | uniform float u_time; 9 | 10 | vec2 random2( vec2 p ) { 11 | return fract( 12 | sin( 13 | vec2( 14 | dot(p, vec2(127.1, 311.7)), 15 | dot(p, vec2(269.5, 183.3)) 16 | ) 17 | ) * 43758.5453 18 | ); 19 | } 20 | 21 | float cellularNoise(vec2 uv) { 22 | // Tile the space 23 | vec2 i_st = floor(uv); 24 | vec2 f_st = fract(uv); 25 | 26 | float m_dist = 1.; // minimum distance 27 | 28 | for (int y= -1; y <= 1; y++) { 29 | for (int x= -1; x <= 1; x++) { 30 | // Neighbor place in the grid 31 | vec2 neighbor = vec2(float(x),float(y)); 32 | 33 | // Random position from current + neighbor place in the grid 34 | vec2 point = random2(i_st + neighbor); 35 | 36 | // Animate the point 37 | point = 0.5 + 0.5*sin(u_time + 6.2831*point); 38 | 39 | // Vector between the pixel and the point 40 | vec2 diff = neighbor + point - f_st; 41 | 42 | // Distance to the point 43 | float dist = length(diff); 44 | 45 | // Keep the closer distance 46 | m_dist = min(m_dist, dist); 47 | } 48 | } 49 | return m_dist; 50 | } 51 | 52 | void main() 53 | { 54 | vec2 uv = gl_FragCoord.xy / u_resolution.xy; 55 | uv.x *= u_resolution.x / u_resolution.y; 56 | 57 | float scale = 15.; 58 | uv *= scale; 59 | 60 | vec3 noise = vec3(cellularNoise(uv)); 61 | 62 | vec3 base_color = vec3(0., 0., 0.); 63 | noise += base_color; 64 | 65 | gl_FragColor = vec4(noise, 1.0); 66 | } 67 | -------------------------------------------------------------------------------- /src/shaders/normal/main.rs: -------------------------------------------------------------------------------- 1 | use bevy::{ 2 | prelude::*, 3 | render::{ 4 | mesh::shape, 5 | pipeline::{PipelineDescriptor, RenderPipeline}, 6 | shader::{ShaderStage, ShaderStages}, 7 | }, 8 | }; 9 | 10 | struct Rotator; 11 | 12 | pub fn main() { 13 | App::build() 14 | .insert_resource(Msaa { samples: 4 }) 15 | .add_plugins(DefaultPlugins) 16 | .add_startup_system(setup.system()) 17 | .add_system(rotate_sphere.system()) 18 | .run(); 19 | } 20 | 21 | fn setup( 22 | mut commands: Commands, 23 | mut pipelines: ResMut>, 24 | mut shaders: ResMut>, 25 | mut meshes: ResMut>, 26 | ) { 27 | let pipeline_handle = pipelines.add(PipelineDescriptor::default_config(ShaderStages { 28 | vertex: shaders.add(Shader::from_glsl( 29 | ShaderStage::Vertex, 30 | include_str!("normal.vert"), 31 | )), 32 | fragment: Some(shaders.add(Shader::from_glsl( 33 | ShaderStage::Fragment, 34 | include_str!("normal.frag"), 35 | ))), 36 | })); 37 | 38 | commands 39 | .spawn_bundle(MeshBundle { 40 | mesh: meshes.add(Mesh::from(shape::Icosphere { 41 | radius: 1.0, 42 | subdivisions: 10, 43 | })), 44 | render_pipelines: RenderPipelines::from_pipelines(vec![RenderPipeline::new( 45 | pipeline_handle, 46 | )]), 47 | transform: Transform::from_xyz(0.0, 0.0, 0.0), 48 | ..Default::default() 49 | }) 50 | .insert(Rotator); 51 | 52 | commands.spawn_bundle(PerspectiveCameraBundle { 53 | transform: Transform::from_xyz(0.0, 0.0, -8.0).looking_at(Vec3::ZERO, -Vec3::Y), 54 | ..Default::default() 55 | }); 56 | } 57 | 58 | fn rotate_sphere(time: Res