Skip to content

Commit

Permalink
Zenith and Nadir colors now provided as uniform input to fragment shader
Browse files Browse the repository at this point in the history
  • Loading branch information
photex committed Nov 6, 2018
1 parent ec03cda commit c8fd94a
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion amethyst_renderer/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ pub use {
mtl::{Material, MaterialDefaults, MaterialTextureSet, TextureOffset},
pass::{
get_camera, set_vertex_args, DebugLinesParams, DrawDebugLines, DrawFlat, DrawFlatSeparate,
DrawPbm, DrawPbmSeparate, DrawShaded, DrawShadedSeparate, DrawSprite, DrawSkybox
DrawPbm, DrawPbmSeparate, DrawShaded, DrawShadedSeparate, DrawSprite, DrawSkybox, SkyboxColor
},
pipe::{
ColorBuffer, Data, DepthBuffer, DepthMode, Effect, EffectBuilder, Init, Meta, NewEffect,
Expand Down
7 changes: 4 additions & 3 deletions amethyst_renderer/src/pass/shaders/fragment/skybox.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ in VertexData {

out vec4 out_color;

vec3 zenith_color = vec3(0.75, 1.0, 1.0);
vec3 nadir_color = vec3(0.2, 0.4, 0.45);
uniform vec3 zenith_color;
uniform vec3 nadir_color;

void main() {
vec3 horizon_color = mix(nadir_color, zenith_color, smoothstep(-1., 1., vertex.position.y));
vec3 normalized_position = normalize(vertex.position.xyz);
vec3 horizon_color = mix(nadir_color, zenith_color, smoothstep(-1., 1., normalized_position.y));
out_color = vec4(horizon_color, 1.0f);
}
9 changes: 7 additions & 2 deletions amethyst_renderer/src/pass/skybox/interleaved.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use gfx::pso::buffer::ElemStride;
use glsl_layout::{mat4, Uniform};
use std::marker::PhantomData;

use super::{VERT_SRC, FRAG_SRC};
use super::{VERT_SRC, FRAG_SRC, SkyboxColor};

#[repr(C, align(16))]
#[derive(Clone, Copy, Debug, Uniform)]
Expand Down Expand Up @@ -62,6 +62,7 @@ where
Option<Read<'a, ActiveCamera>>,
ReadStorage<'a, Camera>,
ReadStorage<'a, GlobalTransform>,
Read<'a, SkyboxColor>,
);
}

Expand All @@ -85,6 +86,8 @@ where
PosNormTex::ATTRIBUTES, PosNormTex::size() as ElemStride, 0
)
.with_raw_global("camera_position")
.with_raw_global("zenith_color")
.with_raw_global("nadir_color")
.with_output("color", Some(DepthMode::LessEqualWrite))
.build()
}
Expand All @@ -94,7 +97,7 @@ where
encoder: &mut Encoder,
effect: &mut Effect,
mut _factory: Factory,
(active, camera, global): <Self as PassData<'a>>::Data,
(active, camera, global, skybox_color): <Self as PassData<'a>>::Data,
) {
let camera = get_camera(active, &camera, &global);

Expand All @@ -110,6 +113,8 @@ where
return;
}

effect.update_global("zenith_color", Into::<[f32; 3]>::into(skybox_color.zenith));
effect.update_global("nadir_color", Into::<[f32; 3]>::into(skybox_color.nadir));
effect.draw(mesh.slice(), encoder);
effect.clear();
}
Expand Down
17 changes: 17 additions & 0 deletions amethyst_renderer/src/pass/skybox/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
pub use self::interleaved::DrawSkybox;

use color::Rgba;

mod interleaved;

static VERT_SRC: &[u8] = include_bytes!("../shaders/vertex/skybox.glsl");
static FRAG_SRC: &[u8] = include_bytes!("../shaders/fragment/skybox.glsl");

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct SkyboxColor {
pub zenith: Rgba,
pub nadir: Rgba,
}

impl Default for SkyboxColor {
fn default() -> SkyboxColor {
SkyboxColor {
zenith: Rgba(0.75, 1.0, 1.0, 1.0),
nadir: Rgba(0.1, 0.3, 0.35, 1.0),
}
}
}

0 comments on commit c8fd94a

Please sign in to comment.