use of net.runelite.cache.fs.File in project ostracker by OSTracker.
the class ModelConverter method convert.
public void convert(int modelId, boolean overwriteFiles) {
File f = modelFiles.get(modelId);
if (f == null) {
throw new NullPointerException("A file for model " + modelId + " could not be found in the cache");
}
java.io.File materialsFile = new java.io.File(OSTracker.MODEL_DUMP_ROOT, modelId + "/materials.mtl");
java.io.File modelFile = new java.io.File(OSTracker.MODEL_DUMP_ROOT, modelId + "/model.obj");
if (!materialsFile.exists() || !modelFile.exists() || overwriteFiles) {
materialsFile.getParentFile().mkdirs();
byte[] modelBytes = f.getContents();
ModelDefinition definition = modelLoader.load(modelId, modelBytes);
definition.computeNormals();
definition.computeTextureUVCoordinates();
LOGGER.info("Dumping " + materialsFile);
try (PrintWriter writer = new PrintWriter(materialsFile)) {
for (int i = 0; i < definition.faceCount; i++) {
short textureId = -1;
if (definition.faceTextures != null) {
textureId = definition.faceTextures[i];
}
writer.println("newmtl m" + i);
if (textureId == -1) {
int hsb = definition.faceColors[i] & 0xFFFF;
int hue = (hsb >> 10) & 0x3F;
int saturation = (hsb >> 7) & 0x7;
int brightness = hsb & 0x7F;
int rgb = Color.HSBtoRGB(hue / 63F, saturation / 7F, brightness / 127F);
int red = (rgb >> 16) & 0xFF;
int green = (rgb >> 8) & 0xFF;
int blue = rgb & 0xFF;
double r = red / 255F;
double g = green / 255F;
double b = blue / 255F;
writer.println(" Kd " + r + " " + g + " " + b);
} else {
File textureFile = textureFiles.get((int) textureId);
if (textureFile != null) {
TextureDefinition textureDefinition = textureLoader.load(textureId, textureFile.getContents());
writer.println(" map_Kd ../../sprites/" + textureDefinition.getFileIds()[0] + "/0.png");
} else {
LOGGER.error("Model " + modelId + " was not dumped, because texture " + textureId + " was missing");
return;
}
}
int alpha = 0;
if (definition.faceAlphas != null) {
alpha = definition.faceAlphas[i] & 0xFF;
}
if (alpha != 0) {
writer.println(" d " + alpha / 255F);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
LOGGER.info("Dumping " + modelFile);
try (PrintWriter writer = new PrintWriter(modelFile)) {
writer.println("mtllib materials.mtl");
for (int i = 0; i < definition.vertexCount; i++) {
writer.println("v " + definition.vertexPositionsX[i] + " " + definition.vertexPositionsY[i] * -1 + " " + definition.vertexPositionsZ[i] * -1);
}
if (definition.faceTextures != null) {
for (int i = 0; i < definition.faceCount; i++) {
writer.println("vt " + definition.faceTextureUCoordinates[i][0] + " " + definition.faceTextureVCoordinates[i][0]);
writer.println("vt " + definition.faceTextureUCoordinates[i][1] + " " + definition.faceTextureVCoordinates[i][1]);
writer.println("vt " + definition.faceTextureUCoordinates[i][2] + " " + definition.faceTextureVCoordinates[i][2]);
}
}
for (int i = 0; i < definition.vertexCount; i++) {
VertexNormal normal = definition.vertexNormals[i];
writer.println("vn " + normal.x + " " + normal.y + " " + normal.z);
}
for (int i = 0; i < definition.faceCount; i++) {
// Those shouldn't be drawn.
if (definition.faceRenderTypes == null || definition.faceRenderTypes[i] != 2) {
int x = definition.faceVertexIndices1[i] + 1;
int y = definition.faceVertexIndices2[i] + 1;
int z = definition.faceVertexIndices3[i] + 1;
writer.println("usemtl m" + i);
if (definition.faceTextures != null) {
writer.println("f " + x + "/" + (i * 3 + 1) + " " + y + "/" + (i * 3 + 2) + " " + z + "/" + (i * 3 + 3));
} else {
writer.println("f " + x + " " + y + " " + z);
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
use of net.runelite.cache.fs.File in project ostracker by OSTracker.
the class ItemDefinitionSerializer method dump.
public void dump(int itemId, boolean overwriteFiles) {
File f = itemFiles.get(itemId);
if (f == null) {
throw new NullPointerException("A file for item " + itemId + " could not be found in the cache");
}
java.io.File definitionFile = new java.io.File(OSTracker.ITEM_DUMP_ROOT, itemId + "/def.json");
if (!definitionFile.exists() || overwriteFiles) {
definitionFile.getParentFile().mkdirs();
ItemDefinition definition = itemLoader.load(itemId, f.getContents());
LOGGER.info("Dumping " + definitionFile);
try (Writer writer = new FileWriter(definitionFile)) {
GSON.toJson(definition, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of net.runelite.cache.fs.File in project ostracker by OSTracker.
the class NpcDefinitionSerializer method dump.
public void dump(int npcId, boolean overwriteFiles) {
File f = npcFiles.get(npcId);
if (f == null) {
throw new NullPointerException("A file for NPC " + npcId + " could not be found in the cache");
}
java.io.File definitionFile = new java.io.File(OSTracker.NPC_DUMP_ROOT, npcId + "/def.json");
if (!definitionFile.exists() || overwriteFiles) {
definitionFile.getParentFile().mkdirs();
NpcDefinition definition = npcLoader.load(npcId, f.getContents());
LOGGER.info("Dumping " + definitionFile);
try (Writer writer = new FileWriter(definitionFile)) {
GSON.toJson(definition, writer);
} catch (IOException e) {
e.printStackTrace();
}
}
}
use of net.runelite.cache.fs.File in project ostracker by OSTracker.
the class SpriteDumper method dump.
public void dump(int spriteId, boolean overwriteFiles) {
File f = spriteFiles.get(spriteId);
if (f == null) {
throw new NullPointerException("A file for sprite " + spriteId + " could not be found in the cache");
}
SpriteDefinition[] spriteDefinitions = spriteLoader.load(spriteId, f.getContents());
for (SpriteDefinition definition : spriteDefinitions) {
if (definition.getWidth() > 0 && definition.getHeight() > 0) {
java.io.File frameFile = new java.io.File(OSTracker.SPRITE_DUMP_ROOT, spriteId + "/" + definition.getFrame() + ".png");
if (!frameFile.exists() || overwriteFiles) {
frameFile.getParentFile().mkdirs();
BufferedImage bufferedImage = new BufferedImage(definition.getWidth(), definition.getHeight(), BufferedImage.TYPE_INT_ARGB);
bufferedImage.setRGB(0, 0, definition.getWidth(), definition.getHeight(), definition.getPixels(), 0, definition.getWidth());
LOGGER.info("Dumping " + frameFile);
FileUtil.writeImage(bufferedImage, "png", frameFile);
}
}
}
}
Aggregations