Skip to content

Instantly share code, notes, and snippets.

@haslersn
Created September 15, 2019 19:27
Show Gist options
  • Save haslersn/b9c6f201613bf8f30332845af46785a3 to your computer and use it in GitHub Desktop.
Save haslersn/b9c6f201613bf8f30332845af46785a3 to your computer and use it in GitHub Desktop.
diff --git a/assets/voxygen/shaders/postprocess-frag.glsl b/assets/voxygen/shaders/postprocess-frag.glsl
index f653f1d3..9392a3c0 100644
--- a/assets/voxygen/shaders/postprocess-frag.glsl
+++ b/assets/voxygen/shaders/postprocess-frag.glsl
@@ -3,6 +3,7 @@
#include <globals.glsl>
uniform sampler2D src_color;
+uniform sampler2D src_normal;
in vec2 f_pos;
@@ -173,5 +174,7 @@ void main() {
vec4 final_color = fxaa_color;
//vec4 final_color = vec4(hsv2rgb(hsva_color.rgb), hsva_color.a);
- tgt_color = vec4(final_color.rgb, 1);
+ // tgt_color = vec4(final_color.rgb, 1);
+ vec3 norm = texture(src_normal, uv).xyz * 2.0 - 1.0;
+ tgt_color = vec4(-norm, 1.0);
}
diff --git a/assets/voxygen/shaders/terrain-frag.glsl b/assets/voxygen/shaders/terrain-frag.glsl
index 5a067105..abad4d1f 100644
--- a/assets/voxygen/shaders/terrain-frag.glsl
+++ b/assets/voxygen/shaders/terrain-frag.glsl
@@ -13,6 +13,7 @@ uniform u_locals {
};
out vec4 tgt_color;
+out vec4 tgt_normal;
#include <sky.glsl>
#include <light.glsl>
@@ -26,4 +27,6 @@ void main() {
vec3 color = mix(surf_color, fog_color, fog_level);
tgt_color = vec4(color, 1.0);
+ vec3 norm = normalize(f_norm);
+ tgt_normal = vec4(norm * 0.5 + 0.5, 1.0);
}
diff --git a/voxygen/src/render/mod.rs b/voxygen/src/render/mod.rs
index 787a3687..b02e0ba9 100644
--- a/voxygen/src/render/mod.rs
+++ b/voxygen/src/render/mod.rs
@@ -28,7 +28,7 @@ pub use self::{
},
Globals, Light,
},
- renderer::{Renderer, TgtColorFmt, TgtDepthFmt, WinColorFmt, WinDepthFmt},
+ renderer::{Renderer, TgtColorFmt, TgtNormalFmt, TgtDepthFmt, WinColorFmt, WinNormalFmt, WinDepthFmt},
texture::Texture,
};
diff --git a/voxygen/src/render/pipelines/postprocess.rs b/voxygen/src/render/pipelines/postprocess.rs
index d168d4fd..d1aaeaee 100644
--- a/voxygen/src/render/pipelines/postprocess.rs
+++ b/voxygen/src/render/pipelines/postprocess.rs
@@ -1,5 +1,5 @@
use super::{
- super::{Mesh, Pipeline, Tri, WinColorFmt, WinDepthFmt},
+ super::{Mesh, Pipeline, Tri, WinColorFmt, WinNormalFmt, WinDepthFmt},
Globals,
};
use gfx::{
@@ -28,7 +28,8 @@ gfx_defines! {
locals: gfx::ConstantBuffer<Locals> = "u_locals",
globals: gfx::ConstantBuffer<Globals> = "u_globals",
- src_sampler: gfx::TextureSampler<<WinColorFmt as gfx::format::Formatted>::View> = "src_color",
+ src_color_sampler: gfx::TextureSampler<<WinColorFmt as gfx::format::Formatted>::View> = "src_color",
+ src_normal_sampler: gfx::TextureSampler<<WinNormalFmt as gfx::format::Formatted>::View> = "src_normal",
tgt_color: gfx::RenderTarget<WinColorFmt> = "tgt_color",
tgt_depth: gfx::DepthTarget<WinDepthFmt> = gfx::preset::depth::PASS_TEST,
diff --git a/voxygen/src/render/pipelines/terrain.rs b/voxygen/src/render/pipelines/terrain.rs
index d28aa3ac..7d92e33f 100644
--- a/voxygen/src/render/pipelines/terrain.rs
+++ b/voxygen/src/render/pipelines/terrain.rs
@@ -1,5 +1,5 @@
use super::{
- super::{Pipeline, TgtColorFmt, TgtDepthFmt},
+ super::{Pipeline, TgtColorFmt, TgtNormalFmt, TgtDepthFmt},
Globals, Light,
};
use gfx::{
@@ -33,6 +33,7 @@ gfx_defines! {
lights: gfx::ConstantBuffer<Light> = "u_lights",
tgt_color: gfx::RenderTarget<TgtColorFmt> = "tgt_color",
+ tgt_normal: gfx::RenderTarget<TgtNormalFmt> = "tgt_normal",
tgt_depth: gfx::DepthTarget<TgtDepthFmt> = gfx::preset::depth::LESS_EQUAL_WRITE,
}
}
diff --git a/voxygen/src/render/renderer.rs b/voxygen/src/render/renderer.rs
index 021ab169..245bf344 100644
--- a/voxygen/src/render/renderer.rs
+++ b/voxygen/src/render/renderer.rs
@@ -20,16 +20,22 @@ use vek::*;
/// Represents the format of the pre-processed color target.
pub type TgtColorFmt = gfx::format::Srgba8;
+/// Represents the format of the pre-processed normal target.
+pub type TgtNormalFmt = gfx::format::Srgba8;
/// Represents the format of the pre-processed depth target.
pub type TgtDepthFmt = gfx::format::Depth;
/// Represents the format of the window's color target.
pub type WinColorFmt = gfx::format::Srgba8;
+/// Represents the format of the window's normal target.
+pub type WinNormalFmt = gfx::format::Rgba32F;
/// Represents the format of the window's depth target.
pub type WinDepthFmt = gfx::format::Depth;
/// A handle to a pre-processed color target.
pub type TgtColorView = gfx::handle::RenderTargetView<gfx_backend::Resources, TgtColorFmt>;
+/// A handle to a pre-processed normal target.
+pub type TgtNormalView = gfx::handle::RenderTargetView<gfx_backend::Resources, TgtNormalFmt>;
/// A handle to a pre-processed depth target.
pub type TgtDepthView = gfx::handle::DepthStencilView<gfx_backend::Resources, TgtDepthFmt>;
@@ -43,6 +49,11 @@ pub type TgtColorRes = gfx::handle::ShaderResourceView<
gfx_backend::Resources,
<TgtColorFmt as gfx::format::Formatted>::View,
>;
+/// A handle to a render normal target as a resource.
+pub type TgtNormalRes = gfx::handle::ShaderResourceView<
+ gfx_backend::Resources,
+ <TgtNormalFmt as gfx::format::Formatted>::View,
+>;
/// A type that encapsulates rendering state. `Renderer` is central to Voxygen's rendering
/// subsystem and contains any state necessary to interact with the GPU, along with pipeline state
@@ -56,9 +67,11 @@ pub struct Renderer {
win_depth_view: WinDepthView,
tgt_color_view: TgtColorView,
+ tgt_normal_view: TgtNormalView,
tgt_depth_view: TgtDepthView,
tgt_color_res: TgtColorRes,
+ tgt_normal_res: TgtNormalRes,
sampler: Sampler<gfx_backend::Resources>,
@@ -94,7 +107,7 @@ impl Renderer {
) = create_pipelines(&mut factory, &mut shader_reload_indicator)?;
let dims = win_color_view.get_dimensions();
- let (tgt_color_view, tgt_depth_view, tgt_color_res) =
+ let (tgt_color_view, tgt_normal_view, tgt_depth_view, tgt_color_res, tgt_normal_res) =
Self::create_rt_views(&mut factory, (dims.0, dims.1))?;
let sampler = factory.create_sampler_linear();
@@ -108,9 +121,11 @@ impl Renderer {
win_depth_view,
tgt_color_view,
+ tgt_normal_view,
tgt_depth_view,
tgt_color_res,
+ tgt_normal_res,
sampler,
skybox_pipeline,
@@ -155,11 +170,13 @@ impl Renderer {
// Avoid panics when creating texture with w,h of 0,0.
if dims.0 != 0 && dims.1 != 0 {
- let (tgt_color_view, tgt_depth_view, tgt_color_res) =
+ let (tgt_color_view, tgt_normal_view, tgt_depth_view, tgt_color_res, tgt_normal_res) =
Self::create_rt_views(&mut self.factory, (dims.0, dims.1))?;
- self.tgt_color_res = tgt_color_res;
self.tgt_color_view = tgt_color_view;
+ self.tgt_normal_view = tgt_normal_view;
self.tgt_depth_view = tgt_depth_view;
+ self.tgt_color_res = tgt_color_res;
+ self.tgt_normal_res = tgt_normal_res;
}
Ok(())
@@ -168,14 +185,17 @@ impl Renderer {
fn create_rt_views(
factory: &mut gfx_device_gl::Factory,
size: (u16, u16),
- ) -> Result<(TgtColorView, TgtDepthView, TgtColorRes), RenderError> {
+ ) -> Result<(TgtColorView, TgtNormalView, TgtDepthView, TgtColorRes, TgtNormalRes), RenderError> {
let (_, tgt_color_res, tgt_color_view) = factory
.create_render_target::<TgtColorFmt>(size.0, size.1)
.map_err(RenderError::CombinedError)?;;
+ let (_, tgt_normal_res, tgt_normal_view) = factory
+ .create_render_target::<TgtNormalFmt>(size.0, size.1)
+ .map_err(RenderError::CombinedError)?;;
let tgt_depth_view = factory
.create_depth_stencil_view_only::<TgtDepthFmt>(size.0, size.1)
.map_err(RenderError::CombinedError)?;;
- Ok((tgt_color_view, tgt_depth_view, tgt_color_res))
+ Ok((tgt_color_view, tgt_normal_view, tgt_depth_view, tgt_color_res, tgt_normal_res))
}
/// Get the resolution of the render target.
@@ -437,6 +457,7 @@ impl Renderer {
globals: globals.buf.clone(),
lights: lights.buf.clone(),
tgt_color: self.tgt_color_view.clone(),
+ tgt_normal: self.tgt_normal_view.clone(),
tgt_depth: self.tgt_depth_view.clone(),
},
);
@@ -553,7 +574,8 @@ impl Renderer {
vbuf: model.vbuf.clone(),
locals: locals.buf.clone(),
globals: globals.buf.clone(),
- src_sampler: (self.tgt_color_res.clone(), self.sampler.clone()),
+ src_color_sampler: (self.tgt_color_res.clone(), self.sampler.clone()),
+ src_normal_sampler: (self.tgt_normal_res.clone(), self.sampler.clone()),
tgt_color: self.win_color_view.clone(),
tgt_depth: self.win_depth_view.clone(),
},
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment