Skip to content

Instantly share code, notes, and snippets.

@mojontwins
Created October 18, 2022 10:15
Show Gist options
  • Save mojontwins/184e3f0fc51f6c7402b9b0ae020befc0 to your computer and use it in GitHub Desktop.
Save mojontwins/184e3f0fc51f6c7402b9b0ae020befc0 to your computer and use it in GitHub Desktop.
EntityRenderer.renderSnow refactoring / optimization
/*
* This is a optimization via simple code refactoring of the renderSnow method in EntityRenderer.java in Minecraft a1.1.X,
* probably works in other versions. Gains some fps in my i3, so may be interesting.
*/
private void renderSnow(float renderPartialTick) {
EntityPlayerSP entityPlayerSP = this.mc.thePlayer;
World world = this.mc.theWorld;
// player block coordinates
int playerX = MathHelper.floor_double(entityPlayerSP.posX);
int playerY = MathHelper.floor_double(entityPlayerSP.posY);
int playerZ = MathHelper.floor_double(entityPlayerSP.posZ);
// Prepare tessellator & texture
Tessellator tessellator = Tessellator.instance;
GL11.glDisable(GL11.GL_CULL_FACE);
GL11.glNormal3f(0.0F, 1.0F, 0.0F);
GL11.glEnable(GL11.GL_BLEND);
GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
GL11.glBindTexture(GL11.GL_TEXTURE_2D, this.mc.renderEngine.getTexture("/snow.png"));
double interpolatedX = entityPlayerSP.lastTickPosX + (entityPlayerSP.posX - entityPlayerSP.lastTickPosX) * (double)renderPartialTick;
double interpolatedY = entityPlayerSP.lastTickPosY + (entityPlayerSP.posY - entityPlayerSP.lastTickPosY) * (double)renderPartialTick;
double interpolatedZ = entityPlayerSP.lastTickPosZ + (entityPlayerSP.posZ - entityPlayerSP.lastTickPosZ) * (double)renderPartialTick;
byte radius = 5;
if(this.mc.options.fancyGraphics) {
radius = 10;
}
float f21 = (float)this.rendererUpdateCount + renderPartialTick;
float f22 = 2.0F * (((float)(this.rendererUpdateCount & 511) + renderPartialTick) / 512.0F);
float fAux1, fAux2, fAux3;
for(int x = playerX - radius; x <= playerX + radius; ++x) {
long baseForRand = x * x * 3121 + x * 45238971;
int y1 = playerY - radius;
int y2 = playerY + radius;
double d25 = (double)((float)x + 0.5F) - entityPlayerSP.posX;
for(int z = playerZ - radius; z <= playerZ + radius; ++z) {
int y = world.getTopSolidOrLiquidBlock(x, z);
if(y < 0) {
y = 0;
}
if(y1 < y) {
y1 = y;
}
if(y2 < y) {
y2 = y;
}
if(y1 != y2) {
this.rand.setSeed((long)(baseForRand + z * z * 418711 + z * 13761));
float f23 = this.rand.nextFloat() + f21 * 0.01F * (float)this.rand.nextGaussian();
float f24 = this.rand.nextFloat() + f21 * (float)this.rand.nextGaussian() * 0.001F;
double d27 = (double)((float)z + 0.5F) - entityPlayerSP.posZ;
float f29 = MathHelper.sqrt_double(d25 * d25 + d27 * d27) / (float)radius;
tessellator.startDrawingQuads();
float f30 = world.getBrightness(x, 128, z);
GL11.glColor4f(f30, f30, f30, (1.0F - f29 * f29) * 0.7F);
tessellator.setTranslationD(-interpolatedX, -interpolatedY, -interpolatedZ);
fAux1 = f22 + f24;
fAux2 = (float)y1 / 4.0F + fAux1;
fAux3 = (float)y2 / 4.0F + fAux1;
tessellator.addVertexWithUV((double)(x + 0), (double)y1, (double)(z + 0), (double)( f23), (double)(fAux2));
tessellator.addVertexWithUV((double)(x + 1), (double)y1, (double)(z + 1), (double)(2.0F + f23), (double)(fAux2));
tessellator.addVertexWithUV((double)(x + 1), (double)y2, (double)(z + 1), (double)(2.0F + f23), (double)(fAux3));
tessellator.addVertexWithUV((double)(x + 0), (double)y2, (double)(z + 0), (double)( f23), (double)(fAux3));
tessellator.addVertexWithUV((double)(x + 0), (double)y1, (double)(z + 1), (double)( f23), (double)(fAux2));
tessellator.addVertexWithUV((double)(x + 1), (double)y1, (double)(z + 0), (double)(2.0F + f23), (double)(fAux2));
tessellator.addVertexWithUV((double)(x + 1), (double)y2, (double)(z + 0), (double)(2.0F + f23), (double)(fAux3));
tessellator.addVertexWithUV((double)(x + 0), (double)y2, (double)(z + 1), (double)( f23), (double)(fAux3));
tessellator.setTranslationD(0.0D, 0.0D, 0.0D);
tessellator.draw();
}
}
}
GL11.glEnable(GL11.GL_CULL_FACE);
GL11.glDisable(GL11.GL_BLEND);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment