Skip to content

Commit

Permalink
satesfied sky
Browse files Browse the repository at this point in the history
  • Loading branch information
fredrickbancan committed Nov 21, 2020
1 parent 47e72f0 commit 560c12d
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 76 deletions.
Binary file modified RabbetGameEngine.dll
Binary file not shown.
7 changes: 3 additions & 4 deletions Res/Shaders/Planet/MoonGlow.shader
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ void main()
vColor = spriteColor;
float d = 1 - (dot(sunDir, spritePos.xyz) + 1) * 0.5F;
vColor.a = pow(d, 4);
float h = (sunDir.y + 1) * 0.5F;
vColor.a *= 1 - clamp(h * h * 2.0, 0, 1);
}

#shader fragment
Expand All @@ -59,11 +61,8 @@ uniform sampler2D ditherTex;
void main()
{
float coordLength = dot(coords, coords);
if (coordLength > 0.72)
{
discard;
}
color = vColor;
color.a *= pow(sqrt(1.15 - coordLength * 0.5), 31);
color.a += texture2D(ditherTex, gl_FragCoord.xy / 8.0).r / 32.0 - (1.0 / 128.0);//dithering
if (color.a <= 0.005) discard;
}
4 changes: 3 additions & 1 deletion Res/Shaders/Planet/Moons.shader
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ vec4 lookAtZeroRotationNoFlip(float rad)
vec4 endPos = vec4(corner.x * rad * 2, corner.y * rad * 2, 0, 0);
mat4 result = mat4(0);
vec3 dir = normalize(-spritePos.xyz);
vec3 up = vec3(0, 1, 0);
vec3 up = vec3(axis.x, 0, -axis.y);
vec3 xAxis = cross(up, dir);
xAxis = normalize(xAxis);
vec3 yAxis = cross(dir, xAxis);
Expand Down Expand Up @@ -49,6 +49,8 @@ void main()
vColor = spriteColor;
float d = 1 - (dot(sunDir, spritePos.xyz) + 1) * 0.5F;
vColor.a = (d * d) * 1.25F;
float h = (sunDir.y + 1) * 0.5F;
vColor.a *= 1 - h * h;
gl_Position = projectionMatrix * viewMatrix * lookAtZeroRotationNoFlip(spriteScale.x);
}

Expand Down
19 changes: 9 additions & 10 deletions Res/Shaders/Planet/Skybox.shader
Original file line number Diff line number Diff line change
Expand Up @@ -37,19 +37,18 @@ void main()
if (fragDir.y > 0)
{
vec3 skyModified = skyTop;
skyModified = mix(skyAmbient, skyTop, (dot(sunDir, fragDir) + 1) * 0.5);

vec3 skyHorizonModified = skyHorizon;
float horizonSunDot = dot(sunDir.xz, fragDir.xz);

float ratio = 1 - (horizonSunDot + 1) * 0.5F;
ratio += fragDir.y * 0.75F;
float sunDirDot = dot(vec3(0, 1, 0), sunDir);
float fragDirDot = (dot(sunDir, fragDir) + 1) * 0.5;
skyModified = mix(skyAmbient, skyTop, fragDirDot * fragDirDot);

color.rgb = mix(skyHorizonModified, skyModified, ratio);
float horizonRatio = 1 - (fragDirDot + 1) * 0.5;
vec3 skyHorizonModified = mix(skyHorizon, skyModified, clamp(horizonRatio*2, 0, 1));
horizonRatio += fragDir.y;
horizonRatio += clamp(-sunDir.y, 0, 1) * 2;//make horizon color fade to nothing when sun goes over horizon
color.rgb = mix(skyHorizonModified, skyModified, clamp(horizonRatio,0,1));

float sunDirDot = dot(vec3(0, 1, 0), sunDir);
color.rgb *= 1 - (fragDir.y * 1 - ((sunDirDot + 1 )* 0.5));
color.rgb *= 0.5;
color = 1.0 - exp(-1.0 * color);//"exposure"
color += vec4(texture2D(ditherTex, gl_FragCoord.xy / 8.0).r / 32.0 - (1.0/128.0));//dithering
color.a = 1;
}
Expand Down
5 changes: 3 additions & 2 deletions Res/Shaders/Planet/Stars.shader
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ void main()
gl_PointSize = viewPortSize.y * radius;
vColor = pointColor;
float r = 1.0 - (dot((modelMatrix * position).xyz, sunDir) + 1.0) * 0.5;
vColor.a = r * r;
vColor.a *= 1 - sunDir.y;
vColor.a = r;
float h = (sunDir.y + 1) * 0.5F;
vColor.a *= 1 - clamp(h * 4,0,1);
}


Expand Down
3 changes: 2 additions & 1 deletion Res/Shaders/Planet/Sun.shader
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ uniform vec3 sunPos;
uniform vec2 viewPortSize;
void main()
{
float sunRadius = 2;
float sunRadius = 0.75;
gl_Position = projectionMatrix * viewMatrix * vec4(sunPos, 1);
gl_PointSize = viewPortSize.y * sunRadius;
}
Expand All @@ -25,4 +25,5 @@ void main()
float fade = pow(sqrt(1.05 - coordLength), 32);
color = vec4(sunColor, fade);
color.a += texture2D(ditherTex, gl_FragCoord.xy / 8.0).r / 32.0 - (1.0 / 128.0);//dithering
if (color.a <= 0.005) discard;
}
3 changes: 1 addition & 2 deletions Src/Rendering/RenderObjects/SkyMoon.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,7 @@ public void onTick()
float rotX = MathF.Cos(skyAngle);
float rotY = MathF.Sin(skyAngle);
float rotZ = -MathF.Cos(skyAngle);
//skyDirection = new Vector3(rotX * orbitDirection.X, rotY, rotZ * orbitDirection.Y).Normalized();
skyDirection = new Vector3(MathF.Cos(skyAngle), MathF.Sin(skyAngle), 0).Normalized();
skyDirection = new Vector3(rotX * orbitDirection.X, rotY, rotZ * orbitDirection.Y).Normalized();
sprite.position = skyDirection * orbitScale;
}
}
Expand Down
1 change: 1 addition & 0 deletions Src/Rendering/Renderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ public enum RenderType
spriteCylinder
}

//TODO: Find cause of mysterious 1x1 screenspace quad with text texture behind skybox :/
public static class Renderer
{
private static int privateTotalDrawCallCount;
Expand Down
18 changes: 10 additions & 8 deletions Src/Rendering/SkyboxRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,18 +101,19 @@ public static void setSkyboxToDraw(Planet p)
}

moonBuffer = new Sprite3D[p.totalMoons];
for(int i = 0; i < p.totalMoons; i++)
SkyMoon[] m = p.getMoons();
for (int i = 0; i < p.totalMoons; i++)
{
moonBuffer[i] = p.getMoons()[i].sprite;
moonBuffer[(p.totalMoons - 1) - i] = m[i].sprite;//reverse order to prevent alpha blending of overlapping moons
}

Vector2[] axies = new Vector2[p.totalMoons];

for (int i = 0; i < p.totalMoons; i++)
{
Vector2 dir = p.getMoons()[i].orbitDirection;
Vector2 dir = m[i].orbitDirection;
Vector2 axis = new Vector2(dir.Y, -dir.X);
axies[i] = axis;
axies[p.totalMoons-1-i] = axis;
}
moonsVAO = new VertexArrayObject();
moonsVAO.beginBuilding();
Expand All @@ -131,6 +132,7 @@ public static void setSkyboxToDraw(Planet p)
moonsVAO.finishBuilding();
}

//TODO: Fix z fighting on moons
public static void drawSkybox(Matrix4 viewMatrix)
{
if(skyboxToDraw == null)
Expand All @@ -156,7 +158,7 @@ public static void drawSkybox(Matrix4 viewMatrix)
horizonShroudShader.use();
horizonShroudShader.setUniformMat4F("projectionMatrix", proj);
horizonShroudShader.setUniformMat4F("viewMatrix", view);
GL.DepthRange(0.99998f, 0.99999f);
GL.DepthRange(0.99995f, 0.99995f);
GL.DrawElements(PrimitiveType.Triangles, shroudModel.indices.Length, DrawElementsType.UnsignedInt, 0);
Renderer.totalDraws++;

Expand All @@ -166,12 +168,12 @@ public static void drawSkybox(Matrix4 viewMatrix)
moonsShader.use();
moonsShader.setUniformMat4F("projectionMatrix", proj);
moonsShader.setUniformMat4F("viewMatrix", view);
GL.DepthRange(0.99998f, 0.999994f);
GL.DepthRange(0.99996f, 0.999995f);
GL.DrawArraysInstanced(PrimitiveType.TriangleStrip, 0, 4, skyboxToDraw.totalMoons);
Renderer.totalDraws++;

GL.DepthMask(false);

GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.One);
//drawing stars
starsVAO.bind();
starsShader.use();
Expand All @@ -197,7 +199,7 @@ public static void drawSkybox(Matrix4 viewMatrix)
GL.DrawArraysInstanced(PrimitiveType.TriangleStrip, 0, 4, skyboxToDraw.totalMoons);
GL.DepthRange(0.999994f, 0.0999941f);
Renderer.totalDraws++;

GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
GL.DepthMask(true);
GL.DepthRange(0, 1);
}
Expand Down
63 changes: 15 additions & 48 deletions Src/Universe/Planet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public class Planet
/// <summary>
/// How many minutes a day night cycle will take
/// </summary>
private int dayNightCycleMinutes = 1;
private int dayNightCycleMinutes = 5;

/// <summary>
/// Total number of ticks in a day night cycle from start to finish
Expand Down Expand Up @@ -69,7 +69,7 @@ public class Planet
private float fogEnd;
private float drawDistance = 0;
public Planet(long seed)
{
{//TODO: Change fog color to match horizon color
random = Rand.CreateJavaRandom(seed);
horizonColor = CustomColor.lightOrange;
horizonColorDawn = CustomColor.lightOrange;
Expand All @@ -80,7 +80,7 @@ public Planet(long seed)
sunColor = CustomColor.lightYellow;
sunColorDawn = CustomColor.lightYellow;
sunColorDusk = CustomColor.flame;
dayNightCycleMinutes = rand.Next(15,61);
//dayNightCycleMinutes = rand.Next(15,61);
totalDayNightTicks = (int)TicksAndFrames.getNumOfTicksForSeconds(dayNightCycleMinutes * 60);
dayNightTicks = totalDayNightTicks / 4;
setDrawDistanceAndFog(150.0F);
Expand All @@ -92,11 +92,11 @@ public Planet(long seed)

private void buildMoons()
{
totalMoons = rand.Next(1, 101);
totalMoons = rand.Next(1, 4);
moons = new SkyMoon[totalMoons];
float moonColorStrength = 0.072F;
float maxMoonRadius = 0.075F;
float minMoonRadius = 0.05F;
float maxMoonRadius = 0.045F;
float minMoonRadius = 0.02F;
float spacing = 1.0F / (float)totalMoons * 0.2F;
for (int i = 0; i < totalMoons; i++)
{
Expand All @@ -108,7 +108,7 @@ private void buildMoons()
(float)rand.NextDouble() * maxMoonRadius + minMoonRadius,
(float)rand.NextDouble() * 360.0F,
rand.Next(0, SkyMoon.totalMoonTextures),
1/*rand.Next(5,15)*/,
rand.Next(5,15),
orbitScale);
}
}
Expand All @@ -127,7 +127,7 @@ public SkyMoon[] getMoons()

private void buildStars()//must be done before building skybox.
{
totalStars = rand.Next(3000,3501);
totalStars = rand.Next(4000,4501);
PointParticle[] points = new PointParticle[totalStars];
float starColorStrength = 0.3072F;
float maxStarRadius = 0.01F;
Expand All @@ -136,7 +136,7 @@ public SkyMoon[] getMoons()
Vector3 pos = new Vector3(0.5F - (float)rand.NextDouble(), 0.5F - (float)rand.NextDouble(), 0.5F - (float)rand.NextDouble());
points[i] = new PointParticle(
pos.Normalized(),
new Vector4(1.0F - (float)rand.NextDouble() * starColorStrength, 1.0F - (float)rand.NextDouble() * starColorStrength, 1.0F - (float)rand.NextDouble() * starColorStrength, 1.0F),
new Vector4(1.0F - (float)rand.NextDouble() * starColorStrength, 1.0F - (float)rand.NextDouble() * starColorStrength, 1.0F - (float)rand.NextDouble() * starColorStrength, 1.0F - (float)rand.NextDouble() * (starColorStrength * 1.5F)),
(float)rand.NextDouble() * maxStarRadius + 0.0025F,
false
);
Expand All @@ -162,20 +162,20 @@ public PointCloudModel getStars()

public Vector3 getFogColor()
{
return fogColor.setBrightPercent(getGlobalBrightness()).toNormalVec3();
return new Vector3();
}
public Vector3 getHorizonColor()
{
return horizonColor.mix(CustomColor.white, MathUtil.normalizeClamped(0.5F, 0.75F, sunHeight * sunHeight)).setBrightPercent(MathHelper.Clamp(sunHeight * 2F, 0, 1)).toNormalVec3();
return horizonColor.mix(CustomColor.white, MathUtil.normalizeClamped(0.5F, 1, sunHeight * sunHeight * sunHeight)).setBrightPercent(MathHelper.Clamp(sunHeight * 4F, 0, 1)).toNormalVec3();
}
public Vector3 getSkyColor()
{
return skyColor.setBrightPercent(sunHeight + (0.5F * 1 - sunHeight * 0.5F)).toNormalVec3();
return skyColor.setBrightPercent(MathHelper.Clamp(sunHeight * 1.5F,0,1)).toNormalVec3();
}

public Vector3 getSkyAmbientColor()
{
return skyAmbientColor.mix(skyColor, MathUtil.normalizeClamped(0.5F, 0.75F, sunHeight * sunHeight)).setBrightPercent(1- sunHeight * sunHeight * 1.25F).toNormalVec3();
return skyAmbientColor.mix(skyColor, MathUtil.normalizeClamped(0.5F, 0.75F, sunHeight * sunHeight)).setBrightPercent(MathHelper.Clamp(sunHeight * sunHeight + ambientBrightness * 10, 0, 1)).toNormalVec3();
}
public Vector3 getSunDirection()
{
Expand All @@ -192,7 +192,7 @@ public Vector3 getSunColor()
/// </summary>
public float getGlobalBrightness()
{
return MathHelper.Clamp(MathF.Pow(sunHeight, 16) + ambientBrightness, 0, 1);
return MathHelper.Clamp(MathF.Pow(sunHeight, 4) + ambientBrightness, 0, 1);
}


Expand All @@ -207,11 +207,7 @@ public bool isDawn()
private void generateWorld()//creates the playground and world colliders
{
float groundHeight = 0;
float playgroundLength = 100;//"playground" is the term i am using for the playable area of the world
float playgroundWidth = 100;
float wallHeight = 20;
Model[] unbatchedGroundQuads = new Model[4101];//all ground and wall quads, walls are divided into 4 quads each.
Model[] unbatchedWallQuads = new Model[16];

//Generating ground quads
for (int x = 0; x < 64; x++)
Expand Down Expand Up @@ -239,42 +235,13 @@ public bool isDawn()
//posX face
unbatchedGroundQuads[4100] = PlanePrefab.copyModel().scaleVerticesAndUV(new Vector3(1, 1, 2)).rotateVertices(new Vector3(0, 0, 90)).translateVertices(new Vector3(1f, 0.5F, 0)).setColor(new Vector4(0.65F, 0.65F, 0.65F, 1.0F));

//build negZ wall
for (int i = 0; i < 4; i++)
{
unbatchedWallQuads[i] = QuadPrefab.copyModel().scaleVertices(new Vector3(playgroundWidth / 4, wallHeight, 1)).scaleUV(new Vector2(playgroundWidth / (wallHeight * 4), 1)).translateVertices(new Vector3((-playgroundWidth / 2) + ((playgroundWidth / 4) / 2) + ((playgroundWidth / 4) * i), groundHeight + wallHeight / 2, -playgroundLength / 2)).setColor(new Vector4(0.7F, 0.7F, 0.7F, 1.0F));
}

//build posZ wall
for (int i = 0; i < 4; i++)
{
unbatchedWallQuads[4 + i] = QuadPrefab.copyModel().scaleVertices(new Vector3(playgroundWidth / 4, wallHeight, 1)).scaleUV(new Vector2(playgroundWidth / (wallHeight * 4), 1)).rotateVertices(new Vector3(0, 180, 0)).translateVertices(new Vector3((playgroundWidth / 2) - ((playgroundWidth / 4) / 2) - ((playgroundWidth / 4) * i), groundHeight + wallHeight / 2, playgroundLength / 2)).setColor(new Vector4(0.8F, 0.8F, 0.8F, 1.0F)); ;
}

//build negX wall
for (int i = 0; i < 4; i++)
{
unbatchedWallQuads[8 + i] = QuadPrefab.copyModel().scaleVertices(new Vector3(playgroundLength / 4, wallHeight, 1)).scaleUV(new Vector2(playgroundLength / (wallHeight * 4), 1)).rotateVertices(new Vector3(0, -90, 0)).translateVertices(new Vector3(-playgroundWidth / 2, groundHeight + wallHeight / 2, (-playgroundLength / 2) + ((playgroundLength / 4) / 2) + ((playgroundLength / 4) * i))).setColor(new Vector4(0.65F, 0.65F, 0.65F, 1.0F));
}

//build posX wall
for (int i = 0; i < 4; i++)
{
unbatchedWallQuads[12 + i] = QuadPrefab.copyModel().scaleVertices(new Vector3(playgroundLength / 4, wallHeight, 1)).scaleUV(new Vector2(playgroundLength / (wallHeight * 4), 1)).rotateVertices(new Vector3(0, 90, 0)).translateVertices(new Vector3(playgroundWidth / 2, groundHeight + wallHeight / 2, (playgroundLength / 2) - ((playgroundLength / 4) / 2) - ((playgroundLength / 4) * i))).setColor(new Vector4(0.9F, 0.9F, 0.9F, 1.0F));
}

Model batchedGround = TriangleCombiner.combineData(unbatchedGroundQuads);
Model batchedWalls = TriangleCombiner.combineData(unbatchedWallQuads);

Renderer.addStaticDrawTriangles("ground", groundTextureName, batchedGround);
Renderer.addStaticDrawTriangles("walls", wallTextureName, batchedWalls);

//adding world colliders
this.addWorldAABB(new AABB(new Vector3(-(playgroundWidth * 0.5F), -2, -(playgroundLength * 0.5F)), new Vector3(playgroundWidth * 0.5F, 0, playgroundLength * 0.5F)));//AABB for ground
this.addWorldAABB(new AABB(new Vector3(-(playgroundWidth * 0.5F), 0, playgroundLength * 0.5F), new Vector3(playgroundWidth * 0.5F, wallHeight, playgroundLength * 0.5F + 2)));//AABB for pos Z wall
this.addWorldAABB(new AABB(new Vector3(-(playgroundWidth * 0.5F), 0, -(playgroundLength * 0.5F) - 2), new Vector3(playgroundWidth * 0.5F, wallHeight, -(playgroundLength * 0.5F))));//AABB for neg Z wall
this.addWorldAABB(new AABB(new Vector3(playgroundWidth * 0.5F, 0, -(playgroundLength * 0.5F)), new Vector3(playgroundWidth * 0.5F + 2, wallHeight, playgroundLength * 0.5F)));//AABB for pos X wall
this.addWorldAABB(new AABB(new Vector3(-(playgroundWidth * 0.5F) - 2, 0, -(playgroundLength * 0.5F)), new Vector3(-(playgroundWidth * 0.5F), wallHeight, playgroundLength * 0.5F)));//AABB for neg X wall
this.addWorldAABB(new AABB(new Vector3(-1000, -2, -1000), new Vector3(1000, 0, 1000)));//AABB for ground
this.addWorldAABB(new AABB(new Vector3(-1, 0, -1), new Vector3(1, 1, 1)));//2x1x2 lump in middle of playground
}

Expand Down

0 comments on commit 560c12d

Please sign in to comment.