Skip to content

Commit

Permalink
fix up mipmapping with leaves
Browse files Browse the repository at this point in the history
  • Loading branch information
Pannoniae committed Jul 17, 2024
1 parent 79f0ec1 commit 197331e
Show file tree
Hide file tree
Showing 7 changed files with 64 additions and 48 deletions.
3 changes: 3 additions & 0 deletions shaders/shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ void main() {
if (colour.a <= 0) {
discard;
}
// make it always opaque (for mipmapping)
colour.a = max(colour.a, 1);

// mix the fog colour between it and the sky
vec4 mixedFogColour = mix(fogColour, skyColour, ratio);
// mix fog
Expand Down
1 change: 1 addition & 0 deletions shaders/simpleBlock.frag
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ void main() {
if (color.a <= 0) {
discard;
}
color.a = max(color.a, 1);
}
30 changes: 24 additions & 6 deletions src/render/Particle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,44 @@
namespace BlockGame;

public class Particle : Entity {
public Color4b tint;

/// <summary>
/// The texture coordinates of the particle.
/// </summary>
public float u;

/// <summary>
/// The texture coordinates of the particle.
/// </summary>
public float v;

/// <summary>
/// The texture the particle uses.
/// </summary>
public string texture;

/// <summary>
/// The size of the particle. (world coords)
/// </summary>
public double size;

/// <summary>
/// The time-to-live of the particle in ticks.
/// </summary>
public int ttl;
public int maxLife;

/// <summary>
/// Is this particle valid?
/// </summary>
public bool active;

public Particle(Vector3D<double> position, Color4b tint, double size, int ttl) {
public Particle(Vector3D<double> position, string texture, float u, float v, double size, int ttl) {
this.position = position;
this.tint = tint;
this.texture = texture;
this.u = u;
this.v = v;
this.size = size;
this.ttl = ttl;
maxLife = ttl;
active = true;
}

Expand All @@ -36,7 +54,7 @@ public void update(double dt) {
}
}

public void render(double interp) {
public void render(double dt, double interp) {
if (active) {
//var pos = Vector3D<int>.Lerp(prevPosition, position, (float)interp);
//var col = new Vector4(tint, ttl / maxLife);
Expand Down
55 changes: 22 additions & 33 deletions src/render/ParticleManager.cs
Original file line number Diff line number Diff line change
@@ -1,51 +1,40 @@
using Silk.NET.Maths;
using TrippyGL;
using Silk.NET.OpenGL;

namespace BlockGame;

public class ParticleManager {
private Particle[] particles;
private readonly List<Particle> particles = [];

public ParticleManager(int maxParticles) {
particles = new Particle[maxParticles];
public void add(Particle particle) {
particles.Add(particle);
}

public void update(double dt) {
for (int i = 0; i < particles.Length; i++) {
if (particles[i] != null) {
particles[i].update(dt);
if (particles[i].ttl <= 0) {
particles[i].active = false;
}
for (var i = 0; i < particles.Count; i++) {
var particle = particles[i];
particle.update(dt);

if (!particle.active) {
particles.RemoveAt(i);
// don't skip the next particle
i--;
}
}
}

public void render(double interp) {
for (int i = 0; i < particles.Length; i++) {
if (particles[i].active) {
particles[i].render(interp);
}
}
}
public void render(double dt, double interp) {

// TODO implement pooling
public Particle newParticle(Vector3D<double> position, Color4b color, double size, int ttl) {
return new Particle(position, color, size, ttl);
}
var currentTexture = "textures/blocks.png";
Game.GL.ActiveTexture(TextureUnit.Texture0);
Game.GL.BindTexture(TextureTarget.Texture2D, Game.textureManager.blockTexture.handle);

public void addParticle(Particle particle) {
for (int i = 0; i < particles.Length; i++) {
if (!particles[i].active) {
particles[i] = particle;
return;
foreach (var particle in particles) {
if (particle.texture != currentTexture) {
Game.textureManager.load(particle.texture, particle.texture);
var tex = Game.textureManager.get(particle.texture);
Game.GL.BindTexture(TextureTarget.Texture2D, tex.Handle);
}
}
}

public void clear() {
for (int i = 0; i < particles.Length; i++) {
particles[i].reset();
particle.render(dt, interp);
}
}
}
9 changes: 9 additions & 0 deletions src/render/TerrainParticle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using Silk.NET.Maths;

namespace BlockGame;

public class TerrainParticle : Particle {

public TerrainParticle(Vector3D<double> position, string texture, float u, float v, double size, int ttl) : base(position, texture, u, v, size, ttl) {
}
}
3 changes: 2 additions & 1 deletion src/ui/menu/SettingsMenu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,9 @@ public SettingsMenu() {
renderDistance.topCentre();
renderDistance.tooltip = "The maximum distance at which blocks are rendered.\nHigher values may reduce performance.";
renderDistance.applied += () => {
var old = settings.renderDistance;
settings.renderDistance = (int)renderDistance.value;
remeshIfRequired((int)renderDistance.value);
remeshIfRequired(old);
};
renderDistance.getText = value => "Render Distance: " + value;
settingElements.Add(renderDistance);
Expand Down
11 changes: 3 additions & 8 deletions src/ui/screen/GameScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public override void onKeyDown(IKeyboard keyboard, Key key, int scancode) {
if (!world.inMenu && !world.paused) {
pause();
}
else {
else if (currentMenu != Menu.SETTINGS) {
backToGame();
}
}
Expand Down Expand Up @@ -241,13 +241,8 @@ public void remeshWorld(int oldRenderDist) {
foreach (var chunk in world.chunks.Values) {
// don't set chunk if not loaded yet, else we will have broken chunkgen/lighting errors
if (chunk.status >= ChunkStatus.MESHED) {
if (oldRenderDist < Settings.instance.renderDistance) {
// just unload everything
chunk.status = ChunkStatus.MESHED - 1;
}
else {
chunk.meshChunk();
}
// just unload everything
chunk.status = ChunkStatus.MESHED - 1;
}
}
world.player.loadChunksAroundThePlayer(Settings.instance.renderDistance);
Expand Down

0 comments on commit 197331e

Please sign in to comment.