Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ported to HLSL #91

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/shaders/hlsl/cloud.fragment.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#include "ShaderConstants.fxh"
#include "natural-mystic-config.fxh"

struct PS_Input
{
float4 position : SV_Position;
float4 color : COLOR;
};

struct PS_Output
{
float4 color : SV_Target;
};

ROOT_SIGNATURE
void main(in PS_Input PSInput, out PS_Output PSOutput)
{
#if defined(ENABLE_FBM_CLOUDS)
/* We completely disable the vanilla clouds. It's impossible to
* improve it. Instead we render clouds with sky shaders. */
discard;
#else
PSOutput.color = PSInput.color;
#endif /* ENABLE_FBM_CLOUDS */
}

// Local Variables:
// mode: hlsl
// indent-tabs-mode: nil
// End:
68 changes: 68 additions & 0 deletions src/shaders/hlsl/cloud.vertex.hlsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "ShaderConstants.fxh"
#include "natural-mystic-config.fxh"

struct VS_Input
{
float3 position : POSITION;
float4 color : COLOR;
#ifdef INSTANCEDSTEREO
uint instanceID : SV_InstanceID;
#endif

};


struct PS_Input
{
float4 position : SV_Position;
float4 color : COLOR;
#ifdef GEOMETRY_INSTANCEDSTEREO
uint instanceID : SV_InstanceID;
#endif
#ifdef VERTEXSHADER_INSTANCEDSTEREO
uint renTarget_id : SV_RenderTargetArrayIndex;
#endif
};

static const float fogNear = 0.9;

static const float3 inverseLightDirection = float3( 0.62, 0.78, 0.0 );
static const float ambient = 0.7;

ROOT_SIGNATURE
void main(in VS_Input VSInput, out PS_Input PSInput)
{
#ifdef INSTANCEDSTEREO
int i = VSInput.instanceID;
PSInput.position = mul( WORLDVIEWPROJ_STEREO[i], float4( VSInput.position, 1 ) );
float3 worldPos = mul(WORLD_STEREO, float4(VSInput.position, 1));
#else
PSInput.position = mul(WORLDVIEWPROJ, float4(VSInput.position, 1));
float3 worldPos = mul(WORLD, float4(VSInput.position, 1));
#endif
#ifdef GEOMETRY_INSTANCEDSTEREO
PSInput.instanceID = VSInput.instanceID;
#endif
#ifdef VERTEXSHADER_INSTANCEDSTEREO
PSInput.renTarget_id = VSInput.instanceID;
#endif

#if defined(ENABLE_FBM_CLOUDS)
/* We completely disable the vanilla clouds. It's impossible to
* improve it. Instead we render clouds with sky shaders. */
PSInput.color = 0.0;
#else
PSInput.color = VSInput.color * CURRENT_COLOR;

float depth = length(worldPos) / RENDER_DISTANCE;

float fog = max( depth - fogNear, 0.0 );

PSInput.color.a *= 1.0 - fog;
#endif /* ENABLE_FBM_CLOUDS */
}

// Local Variables:
// mode: hlsl
// indent-tabs-mode: nil
// End:
23 changes: 23 additions & 0 deletions src/shaders/hlsl/natural-mystic-cloud.fxh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// -*- hlsl -*-
#if !defined(NATURAL_MYSTIC_CLOUD_FXH_INCLUDED)
#define NATURAL_MYSTIC_CLOUD_FXH_INCLUDED 1

#include "natural-mystic-noise.fxh"

/* Generate a pattern of clouds based on a world position. */
float cloudMap(int octaves, float lowerBound, float upperBound, float time, float3 pos) {
static const float2 resolution = float2(1.4, 1.4);

float2 st = pos.xz / resolution;
/* The inverse of the speed (512) should be a power of two in
* order to avoid a precision loss.
*/
st.y += time / 512.0;

/* We intentionally throw away some
* of the precision so we get somewhat sparse noise.
*/
return fBM(octaves, lowerBound, upperBound, st * 3.0);
}

#endif /* !defined(NATURAL_MYSTIC_CLOUD_FXH_INCLUDED) */
121 changes: 121 additions & 0 deletions src/shaders/hlsl/natural-mystic-color.fxh
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
// -*- hlsl -*-
#if !defined(NATURAL_MYSTIC_COLOR_FXH_INCLUDED)
#define NATURAL_MYSTIC_COLOR_FXH_INCLUDED 1

/* Calculate the luma of a color in the linear RGB color space. */
float rgb2luma(float3 color) {
return dot(color, float3(0.22, 0.707, 0.071));
}

/* Desaturate a color in the linear RGB color space. The parameter
* "degree" should be in [0,1] where 0 being no desaturation and 1
* being full desaturation (completely gray). Note that the result of
* the function is usually to be multiplied by the color of the
* ambient light, or otherwise a violation of the law of conservation
* of energy will happen (#30).
*/
float3 desaturate(float3 baseColor, float degree) {
float luma = rgb2luma(baseColor);
return lerp(baseColor, luma, degree);
}

/* Convert linear RGB to HSV. The x component of the result will be
* the hue [0, 1], y will be the saturation, and z will be the
* value. See http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
*/
float3 rgb2hsv(float3 c) {
static const float4 K = float4(0.0, -1.0 / 3.0, 2.0 / 3.0, -1.0);
float4 p = c.g < c.b ? float4(c.bg, K.wz) : float4(c.gb, K.xy);
float4 q = c.r < p.x ? float4(p.xyw, c.r) : float4(c.r, p.yzx);

float d = q.x - min(q.w, q.y);
static const float e = 1.0e-10;
return float3(abs(q.z + (q.w - q.y) / (6.0 * d + e)), d / (q.x + e), q.x);
}

/* Convert HSV to linear RGB. See
* http://lolengine.net/blog/2013/07/27/rgb-to-hsv-in-glsl
*/
float3 hsv2rgb(float3 c) {
static const float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
float3 p = abs(frac(c.xxx + K.xyz) * 6.0 - K.www);
return c.z * lerp(K.xxx, saturate(p - K.xxx), c.y);
}

/* Calculate the color of the ambient light based on some color,
* usually the fog color, by normalizing the RGB components so at
* least one component becomes 1.0.
*/
float3 brighten(float3 color) {
float rgbMax = max(color.r, max(color.g, color.b));
float delta = 1.0 - rgbMax;
return color + delta;
}

/* Apply Uncharted 2 tone mapping to the original fragment "frag".
* See: http://filmicworlds.com/blog/filmic-tonemapping-operators/
*/
float3 uncharted2ToneMap_(float3 x) {
static const float A = 0.015; // Shoulder strength
static const float B = 0.50; // Linear strength
static const float C = 0.10; // Linear angle
static const float D = 0.010; // Toe strength
static const float E = 0.02; // Toe numerator
static const float F = 0.30; // Toe denominator

return ((x * (A * x + C * B) + D * E) / (x * (A * x + B) + D * F)) - E / F;
}
float3 uncharted2ToneMap(float3 frag, float whiteLevel, float exposureBias) {
float3 curr = uncharted2ToneMap_(exposureBias * frag);
float3 whiteScale = 1.0 / uncharted2ToneMap_(whiteLevel);
float3 color = curr * whiteScale;

return saturate(color);
}

/* Apply ACES filmic tone mapping to the original fragment "x". See:
* https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/
* [Currently unused]
*/
float3 acesFilmicToneMap(float3 x) {
static const float a = 2.51;
static const float b = 0.03;
static const float c = 2.43;
static const float d = 0.59;
static const float e = 0.14;

return saturate((x * (a * x + b)) / (x * (c * x + d) + e));
}

/* Apply a contrast filter on some LDR linear RGB color. The contrast
* must be in [0, 2]. Note that this function modifies both the
* saturation and the luminance, which means if you are to decrease
* the contrast you should multiply the result with the color of the
* ambient light, or otherwise you'll get gray when it's
* inappropriate.
*/
float3 contrastFilter(float3 color, float contrast) {
float t = 0.5 - contrast * 0.5;
return saturate(color * contrast + t);
}

/* Apply a contrast filter on some LDR luminance. The contrast must be
* in [0, 2].
*/
float contrastFilter(float lum, float contrast) {
float t = 0.5 - contrast * 0.5;
return saturate(lum * contrast + t);
}

/* Apply an HDR exposure filter to the original LDR fragment
* "frag". The resulting image will be HDR, and need to be tone-mapped
* back to LDR at the last stage. [Currently unused] */
float3 hdrExposure(float3 frag, float overExposure, float underExposure) {
float3 overExposed = frag / overExposure;
float3 normalExposed = frag;
float3 underExposed = frag * underExposure;

return lerp(overExposed, underExposed, normalExposed);
}

#endif /* NATURAL_MYSTIC_COLOR_FXH_INCLUDED */
96 changes: 96 additions & 0 deletions src/shaders/hlsl/natural-mystic-config.fxh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/* src/shaders/glsl/natural-mystic-config.fxh. Generated from natural-mystic-config.fxh.in by configure. */
/* src/shaders/glsl/natural-mystic-config.fxh.in. Generated from configure.ac by autoheader. */

#if !defined(NATURAL_MYSTIC_CONFIG_FXH_INCLUDED)
#define NATURAL_MYSTIC_CONFIG_FXH_INCLUDED 1

#define FOG_TYPE_LINEAR 1
#define FOG_TYPE_EXP 2
#define FOG_TYPE_EXP2 3

/* Define to show the fog color. This is not compatible with other debug
options. */
/* #undef DEBUG_SHOW_FOG_COLOR */

/* Define to show the fog control parameters. This is not compatible with
other debug options. */
/* #undef DEBUG_SHOW_FOG_CONTROL */

/* Define to show the ambient occlusion factor. This is not compatible with
other debug options. */
/* #undef DEBUG_SHOW_OCCLUSION_FACTOR */

/* Define to show the terrain-dependent sunlight level in grayscale. This is
not compatible with other debug options. */
/* #undef DEBUG_SHOW_SUNLIGHT_LEVEL */

/* Define to show the color of vertices. This is not compatible with other
debug options. */
/* #undef DEBUG_SHOW_VERTEX_COLOR */

/* Define to enable a thin fog that always affects the scene, not only when
it's raining. */
#define ENABLE_BASE_FOG 1

/* Define to enable highlight and shade on shader-generated clouds. */
#define ENABLE_CLOUD_SHADE 1

/* Define to enable fancy water rendering. */
#define ENABLE_FANCY_WATER 1

/* Define to enable shader-generated clouds. */
#define ENABLE_FBM_CLOUDS 1

/* Define to enable fake shadows generated from the ambient occlusion */
#define ENABLE_OCCLUSION_SHADOWS 1

/* Define to introduce randomness in the brighness of stars. */
#define ENABLE_RANDOM_STARS 1

/* Define to enable water ripple animation that appears on the ground when
it's raining. */
#define ENABLE_RIPPLES 1

/* Define to enable the shader-generated sun and the moon. */
#define ENABLE_SHADER_SUN_MOON 1

/* Define to enable specular lighting. */
#define ENABLE_SPECULAR 1

/* Define to enable torch light flickering effect. */
#define ENABLE_TORCH_FLICKER 1

/* Define to enable waves of water and leaves. */
#define ENABLE_WAVES 1

/* Define to one of FOG_TYPE_LINEAR and FOG_TYPE_EXP2 to choose a fog type to
use. */
#define FOG_TYPE FOG_TYPE_EXP2

/* Name of package */
#define PACKAGE "natural-mystic-shaders"

/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT "https://github.com/depressed-pho/natural-mystic-shaders/issues"

/* Define to the full name of this package. */
#define PACKAGE_NAME "Natural Mystic Shaders"

/* Define to the full name and version of this package. */
#define PACKAGE_STRING "Natural Mystic Shaders 1.9.0"

/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "natural-mystic-shaders"

/* Define to the home page for this package. */
#define PACKAGE_URL "https://github.com/depressed-pho/natural-mystic-shaders"

/* Define to the version of this package. */
#define PACKAGE_VERSION "1.9.0"

/* Version number of package */
/* NOTE: A macro of "VERSION" is already used in hlsl shaders, so it had
changed to "NUM_VERSION" on GLSL to HLSL convert. */
#define NUM_VERSION "1.9.0"

#endif /* NATURAL_MYSTIC_CONFIG_FXH_INCLUDED */
Loading