Search in sources :

Example 1 with ModelDefinition

use of net.runelite.cache.definitions.ModelDefinition 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();
        }
    }
}
Also used : VertexNormal(net.runelite.cache.models.VertexNormal) FileNotFoundException(java.io.FileNotFoundException) ModelDefinition(net.runelite.cache.definitions.ModelDefinition) TextureDefinition(net.runelite.cache.definitions.TextureDefinition) File(net.runelite.cache.fs.File) PrintWriter(java.io.PrintWriter)

Example 2 with ModelDefinition

use of net.runelite.cache.definitions.ModelDefinition in project runelite by runelite.

the class ObjExporterTest method testExport.

@Test
@Ignore
public void testExport() throws Exception {
    try (Store store = new Store(StoreLocation.LOCATION)) {
        store.load();
        TextureManager tm = new TextureManager(store);
        tm.load();
        ModelLoader loader = new ModelLoader();
        ModelDefinition model = loader.load(9638, Files.readAllBytes(new File("D:\\rs\\07\\cache\\models\\9638.model").toPath()));
        ObjExporter exporter = new ObjExporter(tm, model);
        try (PrintWriter objWriter = new PrintWriter(new FileWriter(new File("D:\\rs\\07\\temp\\9638.obj")));
            PrintWriter mtlWriter = new PrintWriter(new FileWriter(new File("D:\\rs\\07\\temp\\9638.mtl")))) {
            exporter.export(objWriter, mtlWriter);
        }
    }
}
Also used : TextureManager(net.runelite.cache.TextureManager) ModelLoader(net.runelite.cache.definitions.loaders.ModelLoader) FileWriter(java.io.FileWriter) ModelDefinition(net.runelite.cache.definitions.ModelDefinition) Store(net.runelite.cache.fs.Store) File(java.io.File) PrintWriter(java.io.PrintWriter) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 3 with ModelDefinition

use of net.runelite.cache.definitions.ModelDefinition in project runelite by runelite.

the class ItemSpriteFactoryTest method test.

@Test
@Ignore
public void test() throws IOException {
    File base = StoreLocation.LOCATION, outDir = folder.newFolder();
    int count = 0;
    try (Store store = new Store(base)) {
        store.load();
        ItemManager itemManager = new ItemManager(store);
        itemManager.load();
        ModelProvider modelProvider = new ModelProvider() {

            @Override
            public ModelDefinition provide(int modelId) throws IOException {
                Index models = store.getIndex(IndexType.MODELS);
                Archive archive = models.getArchive(modelId);
                byte[] data = archive.decompress(store.getStorage().loadArchive(archive));
                ModelDefinition inventoryModel = new ModelLoader().load(modelId, data);
                return inventoryModel;
            }
        };
        SpriteManager spriteManager = new SpriteManager(store);
        spriteManager.load();
        TextureManager textureManager = new TextureManager(store);
        textureManager.load();
        for (ItemDefinition itemDef : itemManager.getItems()) {
            if (itemDef.name == null || itemDef.name.equalsIgnoreCase("null")) {
                continue;
            }
            try {
                BufferedImage sprite = ItemSpriteFactory.createSprite(itemManager, modelProvider, spriteManager, textureManager, itemDef.id, 1, 1, 3153952, false);
                File out = new File(outDir, itemDef.id + ".png");
                BufferedImage img = sprite;
                ImageIO.write(img, "PNG", out);
                ++count;
            } catch (Exception ex) {
                log.warn("error dumping item {}", itemDef.id, ex);
            }
        }
    }
    log.info("Dumped {} item images to {}", count, outDir);
}
Also used : Archive(net.runelite.cache.fs.Archive) ItemManager(net.runelite.cache.ItemManager) ModelProvider(net.runelite.cache.definitions.providers.ModelProvider) ItemDefinition(net.runelite.cache.definitions.ItemDefinition) Store(net.runelite.cache.fs.Store) Index(net.runelite.cache.fs.Index) SpriteManager(net.runelite.cache.SpriteManager) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) ModelLoader(net.runelite.cache.definitions.loaders.ModelLoader) TextureManager(net.runelite.cache.TextureManager) ModelDefinition(net.runelite.cache.definitions.ModelDefinition) File(java.io.File) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 4 with ModelDefinition

use of net.runelite.cache.definitions.ModelDefinition in project runelite by runelite.

the class ModelLoader method load.

public ModelDefinition load(int modelId, byte[] b) {
    ModelDefinition def = new ModelDefinition();
    def.id = modelId;
    if (b[b.length - 1] == -1 && b[b.length - 2] == -1) {
        this.load1(def, b);
    } else {
        this.load2(def, b);
    }
    def.computeNormals();
    def.computeTextureUVCoordinates();
    return def;
}
Also used : ModelDefinition(net.runelite.cache.definitions.ModelDefinition)

Example 5 with ModelDefinition

use of net.runelite.cache.definitions.ModelDefinition in project runelite by runelite.

the class ModelViewer method drawLocations.

private static void drawLocations(Region region) {
    for (Location location : region.getLocations()) {
        int id = location.getId();
        ObjectDefinition object = getObject(id);
        if (object == null || object.getObjectModels() == null) {
            continue;
        }
        Position objectPos = location.getPosition();
        if (location.getPosition().getZ() != 0) {
            continue;
        }
        int regionX = objectPos.getX() - region.getBaseX();
        int regionY = objectPos.getY() - region.getBaseY();
        int height = -region.getTileHeight(objectPos.getZ(), regionX, regionY) / HEIGHT_MOD;
        GL11.glMatrixMode(GL11.GL_MODELVIEW);
        // TILE_SCALE/2 to draw the object from the center of the tile it is on
        GL11.glTranslatef(regionX * TILE_SCALE + (TILE_SCALE / 2), height, -regionY * TILE_SCALE - (TILE_SCALE / 2));
        for (int i = 0; i < object.getObjectModels().length; ++i) {
            ModelDefinition md = ModelManager.getModel(object.getObjectModels()[i], object, location);
            if (object.getObjectTypes() != null && object.getObjectTypes()[i] != location.getType()) {
                continue;
            }
            drawModel(md, object.getRecolorToFind(), object.getRecolorToReplace());
        }
        GL11.glTranslatef(-regionX * TILE_SCALE - (TILE_SCALE / 2), -height, regionY * TILE_SCALE + (TILE_SCALE / 2));
        GL11.glPopMatrix();
    }
}
Also used : Position(net.runelite.cache.region.Position) ModelDefinition(net.runelite.cache.definitions.ModelDefinition) ObjectDefinition(net.runelite.cache.definitions.ObjectDefinition) Location(net.runelite.cache.region.Location)

Aggregations

ModelDefinition (net.runelite.cache.definitions.ModelDefinition)8 File (java.io.File)3 IOException (java.io.IOException)3 ModelLoader (net.runelite.cache.definitions.loaders.ModelLoader)3 BufferedImage (java.awt.image.BufferedImage)2 FileNotFoundException (java.io.FileNotFoundException)2 PrintWriter (java.io.PrintWriter)2 TextureManager (net.runelite.cache.TextureManager)2 ObjectDefinition (net.runelite.cache.definitions.ObjectDefinition)2 TextureDefinition (net.runelite.cache.definitions.TextureDefinition)2 Store (net.runelite.cache.fs.Store)2 VertexNormal (net.runelite.cache.models.VertexNormal)2 Location (net.runelite.cache.region.Location)2 Position (net.runelite.cache.region.Position)2 Ignore (org.junit.Ignore)2 Test (org.junit.Test)2 Gson (com.google.gson.Gson)1 Color (java.awt.Color)1 FileInputStream (java.io.FileInputStream)1 FileWriter (java.io.FileWriter)1