Skip to content
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
14 changes: 14 additions & 0 deletions src/main/resources/rs117/hd/scene_frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#define DISPLAY_SHADOWS 0
#define DISPLAY_LIGHTING 0

#define NEAR_PLANE_DITHER_START 0.2

#include <uniforms/global.glsl>
#include <uniforms/world_views.glsl>
#include <uniforms/materials.glsl>
Expand All @@ -55,6 +57,9 @@ flat in ivec3 fTerrainData;
#endif

in FragmentData {
#if ZONE_RENDERER
vec4 positionCS;
#endif
vec3 position;
vec2 uv;
vec3 normal;
Expand Down Expand Up @@ -84,6 +89,15 @@ vec2 worldUvs(float scale) {

void main() {
vec3 downDir = vec3(0, -1, 0);
#if ZONE_RENDERER
float viewZ = 1.0 - (0.5 + (IN.positionCS.z / IN.positionCS.w) * 0.5);
if(viewZ < NEAR_PLANE_DITHER_START) {
float fadeAmount = 1.0 - saturate(viewZ / NEAR_PLANE_DITHER_START);
if(orderedDither(gl_FragCoord.xy, pow(fadeAmount, 1.5) - 0.01, 1.75))
discard;
}
#endif

// View & light directions are from the fragment to the camera/light
vec3 viewDir = normalize(cameraPos - IN.position);

Expand Down
9 changes: 5 additions & 4 deletions src/main/resources/rs117/hd/scene_vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ layout (location = 0) in vec3 vPosition;
#endif

out FragmentData {
vec4 positionCS;
vec3 position;
vec2 uv;
vec3 normal;
Expand Down Expand Up @@ -109,12 +110,12 @@ layout (location = 0) in vec3 vPosition;
fFlatNormal = worldNormal;
#endif

vec4 clipPosition = projectionMatrix * vec4(worldPosition, 1.0);
int depthBias = (alphaBiasHsl >> 16) & 0xff;
if (projectionMatrix[2][3] != 0) // Disable depth bias for orthographic projection
clipPosition.z += depthBias / 128.0;
OUT.positionCS = projectionMatrix * vec4(worldPosition, 1.0);
if (projectionMatrix[2][3] != 0) // Disable depth bias for orthographic projection
OUT.positionCS.z += depthBias / 128.0;

gl_Position = clipPosition;
gl_Position = OUT.positionCS;
}
#else
out vec3 gPosition;
Expand Down
19 changes: 19 additions & 0 deletions src/main/resources/rs117/hd/utils/misc.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,25 @@ void undoVanillaShading(inout int hsl, vec3 unrotatedNormal) {
}
#endif

const int DITHER_MAP_LEN = 4;
const float DITHER_MAP_SCALE = 16.0;

const float DITHER_MAP[DITHER_MAP_LEN * DITHER_MAP_LEN] = float[](
0.0 / DITHER_MAP_SCALE, 8.0 / DITHER_MAP_SCALE, 2.0 / DITHER_MAP_SCALE, 10.0 / DITHER_MAP_SCALE,
12.0 / DITHER_MAP_SCALE, 4.0 / DITHER_MAP_SCALE, 14.0 / DITHER_MAP_SCALE, 6.0 / DITHER_MAP_SCALE,
3.0 / DITHER_MAP_SCALE, 11.0 / DITHER_MAP_SCALE, 1.0 / DITHER_MAP_SCALE, 9.0 / DITHER_MAP_SCALE,
15.0 / DITHER_MAP_SCALE, 7.0 / DITHER_MAP_SCALE, 13.0 / DITHER_MAP_SCALE, 5.0 / DITHER_MAP_SCALE
);

// ------------------------------------------------------------
// Based on https://www.shadertoy.com/view/4t2cRt
// Returns a dither value (0.0 or 1.0) based on coords + opacity
// ------------------------------------------------------------
bool orderedDither(vec2 pixelCoord, float opacity, float scaleFactor) {
ivec2 coord = ivec2(pixelCoord / scaleFactor) & (DITHER_MAP_LEN - 1);
return DITHER_MAP[coord.x + coord.y * DITHER_MAP_LEN] < clamp(opacity, 0.0, 1.0);
}

// 2D Random
float hash(in vec2 st) {
return fract(sin(dot(st.xy, vec2(12.9898, 78.233))) * 43758.5453123);
Expand Down