Skip to content

Commit

Permalink
Scale textures. Make animated if appropriate.
Browse files Browse the repository at this point in the history
  • Loading branch information
Griefed committed Nov 7, 2023
1 parent 4827651 commit e8368c6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package de.griefed.generation.blocks;

import java.awt.*;
import java.awt.image.BufferedImage;

public class TextureScaler {
public static BufferedImage getScaledInstance(BufferedImage sourceImage) {
Dimension imgSize = new Dimension(sourceImage.getWidth(), sourceImage.getHeight());
double widthRatio;
Image scaledImage;
BufferedImage output;
int newWidth = 16;
int newHeight;
widthRatio = (double) newWidth / imgSize.width;
newHeight = (int) (sourceImage.getHeight() * widthRatio);
while (newHeight % 16 != 0) {
newHeight++;
}
scaledImage = sourceImage.getScaledInstance(newWidth, newHeight, Image.SCALE_SMOOTH);
output = new BufferedImage(scaledImage.getWidth(null), scaledImage.getHeight(null), BufferedImage.TYPE_INT_ARGB);
output.getGraphics().drawImage(scaledImage,0, 0, null);
return output;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
import com.fasterxml.jackson.databind.node.ObjectNode;
import de.griefed.generation.blocks.BlockDefinition;
import de.griefed.generation.blocks.BlockDefinitionParser;
import de.griefed.generation.blocks.TextureScaler;
import org.gradle.api.Project;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
Expand Down Expand Up @@ -212,6 +215,8 @@ public void createBlockFiles() throws IOException {
File itemBlockModel;
File blockTexture;
File textureSource;
File textureMCMeta;
BufferedImage texture;
for (BlockDefinition block : blockDefinitionParser.getBlocks()) {
//blockstate
blockstate = new File(blockstatesDir, blockstatesTemp.replace("BLOCKID", block.getId()));
Expand All @@ -225,6 +230,21 @@ public void createBlockFiles() throws IOException {
//block texture
blockTexture = new File(blockTexturesDir, blockTextureTemp.replace("BLOCKID", block.getId()));
textureSource = new File(blockDefinitionParser.getAssetsDirectory(), block.getId() + ".png");
texture = ImageIO.read(textureSource);
if (texture.getWidth() > 16 && texture.getHeight() > 16) {
texture = TextureScaler.getScaledInstance(texture);
ImageIO.write(texture,"png",textureSource);
}
if (texture.getWidth() < texture.getHeight()) {
textureMCMeta = new File(blockTexture.getParent(), blockTexture.getName() + ".mcmeta");
textureMCMeta.createNewFile();
writeToFile(textureMCMeta,
"""
{
"animation": {}
}
""");
}
Files.copy(textureSource.toPath(), blockTexture.toPath(), StandardCopyOption.REPLACE_EXISTING);
}
}
Expand Down

0 comments on commit e8368c6

Please sign in to comment.