Skip to content
This repository has been archived by the owner on Oct 9, 2019. It is now read-only.

Commit

Permalink
Improved block picker and new sky rendering
Browse files Browse the repository at this point in the history
  • Loading branch information
Guerra24 committed Apr 25, 2017
1 parent b0e931c commit 556856f
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
import net.luxvacuos.voxel.client.rendering.api.glfw.Window;
import net.luxvacuos.voxel.client.resources.CastRay;
import net.luxvacuos.voxel.client.util.Maths;
import net.luxvacuos.voxel.universal.core.GlobalVariables;
import net.luxvacuos.voxel.universal.ecs.Components;
import net.luxvacuos.voxel.universal.ecs.components.AABB;
import net.luxvacuos.voxel.universal.ecs.components.ChunkLoader;
Expand Down Expand Up @@ -75,7 +76,7 @@ public class PlayerCamera extends CameraEntity {
private static Vector3 tmp = new Vector3();

private static List<BoundingBox> blocks = new ArrayList<>();
private static final int MAX_INTERATION = 8;
private static final int MAX_INTERATION = 64;
private static final float PRECISION = 16f;
private Vector3d normalTMP = new Vector3d();
private double depthTMP;
Expand Down Expand Up @@ -166,21 +167,24 @@ else if (kbh.isCtrlPressed())

private void setBlock(IBlock block, IDimension dimension, float delta) {

if (GlobalVariables.TEST_MODE)
return;

Ray ray = castRay.getRay();
BoundingBox box = new BoundingBox();
Vector3d org = new Vector3d(ray.origin);
Vector3d dir = new Vector3d(ray.direction);
Vector3d dir1 = new Vector3d(ray.direction);
dir.div(PRECISION);
Vector3d dir = new Vector3d();
Vector3d incr = new Vector3d(ray.direction);
incr.div(PRECISION);
Vector3d pos = new Vector3d();
int it = 0;
double bcx = 0, bcy = 0, bcz = 0;
CAST: while (true) {
Vector3d.add(dir, dir1, dir);
Vector3d.add(dir, incr, dir);
pos.set(org);
Vector3d.add(pos, dir, pos);
box.set(new Vector3(pos.x - 0.1, pos.y - 0.1, pos.z - 0.1),
new Vector3(pos.x + 0.1, pos.y + 0.1, pos.z + 0.1));
box.set(new Vector3(pos.x - 0.1f, pos.y - 0.1f, pos.z - 0.1f),
new Vector3(pos.x + 0.1f, pos.y + 0.1f, pos.z + 0.1f));
blocks = dimension.getGlobalBoundingBox(box);
for (BoundingBox boundingBox : blocks) {
if (Maths.intersectRayBounds(ray, boundingBox, tmp)) {
Expand Down
193 changes: 121 additions & 72 deletions client/src/main/resources/assets/voxel/shaders/F_Skybox.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -30,88 +30,137 @@ uniform float time;
uniform vec3 fogColour;
uniform vec3 lightPosition;

#define LOWER_LIMIT -0.8
#define UPPER_LIMIT 0.2
#define PI 3.141592
#define iSteps 16
#define jSteps 8

vec2 rsi(vec3 r0, vec3 rd, float sr) {
// ray-sphere intersection that assumes
// the sphere is centered at the origin.
// No intersection when result.x > result.y
float a = dot(rd, rd);
float b = 2.0 * dot(rd, r0);
float c = dot(r0, r0) - (sr * sr);
float d = (b*b) - 4.0*a*c;
if (d < 0.0) return vec2(1e5,-1e5);
return vec2(
(-b - sqrt(d))/(2.0*a),
(-b + sqrt(d))/(2.0*a)
);
}

#define SUN_LOWER_LIMIT -0.1
#define SUN_UPPER_LIMIT 0.0
vec3 atmosphere(vec3 r, vec3 r0, vec3 pSun, float iSun, float rPlanet, float rAtmos, vec3 kRlh, float kMie, float shRlh, float shMie, float g) {
// Normalize the sun and view directions.
pSun = normalize(pSun);
r = normalize(r);

#define CLOUD_COVER 0.55
#define CLOUD_SHARPNESS 0.005
// Calculate the step size of the primary ray.
vec2 p = rsi(r0, r, rAtmos);
if (p.x > p.y) return vec3(0,0,0);
p.y = min(p.y, rsi(r0, r, rPlanet).x);
float iStepSize = (p.y - p.x) / float(iSteps);

float hash( float n )
{
return fract(sin(n)*43758.5453);
}
// Initialize the primary ray time.
float iTime = 0.0;

float noise( in vec2 x )
{
vec2 p = floor(x);
vec2 f = fract(x);
f = f*f*(3.0-2.0*f);
float n = p.x + p.y*57.0;
float res = mix(mix( hash(n+ 0.0), hash(n+ 1.0),f.x), mix( hash(n+ 57.0), hash(n+ 58.0),f.x),f.y);
return res;
}
// Initialize accumulators for Rayleigh and Mie scattering.
vec3 totalRlh = vec3(0,0,0);
vec3 totalMie = vec3(0,0,0);

float fbm( vec2 p )
{
float f = 0.0;
f += 0.50000*noise( p ); p = p*2.02;
f += 0.25000*noise( p ); p = p*2.03;
f += 0.12500*noise( p ); p = p*2.01;
f += 0.06250*noise( p ); p = p*2.04;
f += 0.03125*noise( p );
return f/0.984375;
}
// Initialize optical depth accumulators for the primary ray.
float iOdRlh = 0.0;
float iOdMie = 0.0;

// Calculate the Rayleigh and Mie phases.
float mu = dot(r, pSun);
float mumu = mu * mu;
float gg = g * g;
float pRlh = 3.0 / (16.0 * PI) * (1.0 + mumu);
float pMie = 3.0 / (8.0 * PI) * ((1.0 - gg) * (mumu + 1.0)) / (pow(1.0 + gg - 2.0 * mu * g, 1.5) * (2.0 + gg));

// Sample the primary ray.
for (int i = 0; i < iSteps; i++) {

// Calculate the primary ray sample position.
vec3 iPos = r0 + r * (iTime + iStepSize * 0.5);

// Calculate the height of the sample.
float iHeight = length(iPos) - rPlanet;

// Calculate the optical depth of the Rayleigh and Mie scattering for this step.
float odStepRlh = exp(-iHeight / shRlh) * iStepSize;
float odStepMie = exp(-iHeight / shMie) * iStepSize;

// Accumulate optical depth.
iOdRlh += odStepRlh;
iOdMie += odStepMie;

// Calculate the step size of the secondary ray.
float jStepSize = rsi(iPos, pSun, rAtmos).y / float(jSteps);

// Initialize the secondary ray time.
float jTime = 0.0;

float clouds(float tt){
vec2 wind_vec = vec2(0.001 + tt*0.01, 0.003 + tt * 0.01);

// Set up domain
vec2 q = (textureCoords.xz);
vec2 p = -1.0 + 3.0 * q + wind_vec;

float f = fbm( 2.0*p );

float cover = CLOUD_COVER;
float sharpness = CLOUD_SHARPNESS;

float c = f - (1.0 - cover);
if ( c < 0.0 )
c = 0.0;
f = 1.0 - (pow(sharpness, c));
return f;
// Initialize optical depth accumulators for the secondary ray.
float jOdRlh = 0.0;
float jOdMie = 0.0;

// Sample the secondary ray.
for (int j = 0; j < jSteps; j++) {

// Calculate the secondary ray sample position.
vec3 jPos = iPos + pSun * (jTime + jStepSize * 0.5);

// Calculate the height of the sample.
float jHeight = length(jPos) - rPlanet;

// Accumulate the optical depth.
jOdRlh += exp(-jHeight / shRlh) * jStepSize;
jOdMie += exp(-jHeight / shMie) * jStepSize;

// Increment the secondary ray time.
jTime += jStepSize;
}

// Calculate attenuation.
vec3 attn = exp(-(kMie * (iOdMie + jOdMie) + kRlh * (iOdRlh + jOdRlh)));

// Accumulate scattering.
totalRlh += odStepRlh * attn;
totalMie += odStepMie * attn;

// Increment the primary ray time.
iTime += iStepSize;

}

// Calculate and return the final color.
return iSun * (pRlh * kRlh * totalRlh + pMie * kMie * totalMie);
}


void main(void){
/*
float tt = time / 10;
float f = clouds(tt);
*/
float factor = (LOWER_LIMIT - textureCoords.y) / (LOWER_LIMIT - UPPER_LIMIT);
float factorSun = clamp((textureCoords.y - SUN_LOWER_LIMIT) / (SUN_UPPER_LIMIT - SUN_LOWER_LIMIT), 0.0, 1.0);
vec4 finalColour = vec4(fogColour, 1.0);

vec4 skyTop = vec4(fogColour.r *0.4, fogColour.g *0.4, fogColour.b, 0.0);

finalColour = mix(finalColour, skyTop, factor);

vec3 V = normalize(pass_normal);
vec3 L = normalize(lightPosition);

float vl = dot(V, L);

finalColour *= max(dot(vec3(0,1,0),L),0.002);
finalColour = mix (vec4(0.0), finalColour, factorSun);
if(vl > 0.999)
finalColour = mix(finalColour, mix(finalColour, vec4(1.0), (0.999 - vl) / (0.999 - 0.9991)), factorSun);


out_Color[0] = finalColour;

vec3 color = atmosphere(
normalize(pass_normal), // normalized ray direction
vec3(0,6372e3,0), // ray origin
lightPosition, // position of the sun
22.0, // intensity of the sun
6371e3, // radius of the planet in meters
6471e3, // radius of the atmosphere in meters
vec3(5.5e-6, 13.0e-6, 22.4e-6), // Rayleigh scattering coefficient
21e-6, // Mie scattering coefficient
8e3, // Rayleigh scale height
1.2e3, // Mie scale height
0.758 // Mie preferred scattering direction
);

color = 1.0 - exp(-1.0 * color);

out_Color[0].rgb = color.rgb;
out_Color[0].a = 1;
out_Color[1] = vec4(pass_position.xyz * 10, 0);
out_Color[2] = vec4(0.0);
out_Color[3] = vec4(0.0);
out_Color[4] = vec4(1 * ((0.9 - vl) / (0.9 - 0.9991)),0,0,1);
out_Color[4] = vec4(0,0,0,1);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ uniform sampler2D composite0;
uniform sampler2D composite1;
uniform float exposure;

#define GAMMA 2.2
#define GAMMA 2.2

void main(void){
vec2 texcoord = textureCoords;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ void main(void){

vec2 texcoord = textureCoords;
vec4 image = vec4(0.0);
vec4 data1 = texture(gMask, texcoord);
if(data1.r > 0 && data1.a == 1) {
image = mix(texture(gDiffuse, texcoord), vec4(1.0), 0.2) * data1.r;
vec4 mask = texture(gMask, texcoord);
if(mask.a == 1) {
image = texture(gDiffuse, texcoord);
}
out_Color = image;

Expand Down
Binary file modified client/src/main/resources/assets/voxel/textures/blocks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified client/src/main/resources/assets/voxel/textures/blocks_r.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 556856f

Please sign in to comment.