Search in sources :

Example 1 with TextureDefinition

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

the class TextureLoader method load.

public TextureDefinition load(int id, byte[] b) {
    TextureDefinition def = new TextureDefinition();
    InputStream is = new InputStream(b);
    def.field1777 = is.readUnsignedShort();
    def.field1778 = is.readByte() != 0;
    def.setId(id);
    int count = is.readUnsignedByte();
    int[] files = new int[count];
    for (int i = 0; i < count; ++i) files[i] = is.readUnsignedShort();
    def.setFileIds(files);
    if (count > 1) {
        def.field1780 = new int[count - 1];
        for (int var3 = 0; var3 < count - 1; ++var3) {
            def.field1780[var3] = is.readUnsignedByte();
        }
    }
    if (count > 1) {
        def.field1781 = new int[count - 1];
        for (int var3 = 0; var3 < count - 1; ++var3) {
            def.field1781[var3] = is.readUnsignedByte();
        }
    }
    def.field1786 = new int[count];
    for (int var3 = 0; var3 < count; ++var3) {
        def.field1786[var3] = is.readInt();
    }
    def.field1783 = is.readUnsignedByte();
    def.field1782 = is.readUnsignedByte();
    return def;
}
Also used : InputStream(net.runelite.cache.io.InputStream) TextureDefinition(net.runelite.cache.definitions.TextureDefinition)

Example 2 with TextureDefinition

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

the class TextureManager method load.

public void load() throws IOException {
    Storage storage = store.getStorage();
    Index index = store.getIndex(IndexType.TEXTURES);
    Archive archive = index.getArchive(0);
    byte[] archiveData = storage.loadArchive(archive);
    ArchiveFiles files = archive.getFiles(archiveData);
    TextureLoader loader = new TextureLoader();
    for (FSFile file : files.getFiles()) {
        TextureDefinition texture = loader.load(file.getFileId(), file.getContents());
        textures.add(texture);
    }
}
Also used : Storage(net.runelite.cache.fs.Storage) Archive(net.runelite.cache.fs.Archive) ArchiveFiles(net.runelite.cache.fs.ArchiveFiles) TextureLoader(net.runelite.cache.definitions.loaders.TextureLoader) Index(net.runelite.cache.fs.Index) TextureDefinition(net.runelite.cache.definitions.TextureDefinition) FSFile(net.runelite.cache.fs.FSFile)

Example 3 with TextureDefinition

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

the class CacheController method getItemImage.

@RequestMapping(path = "item/{itemId}/image", produces = "image/png")
public ResponseEntity<byte[]> getItemImage(@PathVariable int itemId, @RequestParam(defaultValue = "1") int quantity, @RequestParam(defaultValue = "1") int border, @RequestParam(defaultValue = "3153952") int shadowColor) throws IOException {
    final CacheEntry cache = cacheService.findMostRecent();
    ItemProvider itemProvider = new ItemProvider() {

        @Override
        public ItemDefinition provide(int itemId) {
            try {
                return getItem(itemId);
            } catch (IOException ex) {
                log.warn(null, ex);
                return null;
            }
        }
    };
    ModelProvider modelProvider = new ModelProvider() {

        @Override
        public ModelDefinition provide(int modelId) throws IOException {
            IndexEntry indexEntry = cacheService.findIndexForCache(cache, IndexType.MODELS.getNumber());
            ArchiveEntry archiveEntry = cacheService.findArchiveForIndex(indexEntry, modelId);
            byte[] archiveData = Container.decompress(cacheService.getArchive(archiveEntry), null).data;
            return new ModelLoader().load(modelId, archiveData);
        }
    };
    SpriteProvider spriteProvider = new SpriteProvider() {

        @Override
        public SpriteDefinition provide(int spriteId, int frameId) {
            try {
                IndexEntry indexEntry = cacheService.findIndexForCache(cache, IndexType.SPRITES.getNumber());
                ArchiveEntry archiveEntry = cacheService.findArchiveForIndex(indexEntry, spriteId);
                byte[] archiveData = Container.decompress(cacheService.getArchive(archiveEntry), null).data;
                SpriteDefinition[] defs = new SpriteLoader().load(spriteId, archiveData);
                return defs[frameId];
            } catch (Exception ex) {
                log.warn(null, ex);
                return null;
            }
        }
    };
    TextureProvider textureProvider2 = new TextureProvider() {

        @Override
        public TextureDefinition[] provide() {
            try {
                IndexEntry indexEntry = cacheService.findIndexForCache(cache, IndexType.TEXTURES.getNumber());
                ArchiveEntry archiveEntry = cacheService.findArchiveForIndex(indexEntry, 0);
                ArchiveFiles archiveFiles = cacheService.getArchiveFiles(archiveEntry);
                TextureLoader loader = new TextureLoader();
                TextureDefinition[] defs = new TextureDefinition[archiveFiles.getFiles().size()];
                int i = 0;
                for (FSFile file : archiveFiles.getFiles()) {
                    TextureDefinition def = loader.load(file.getFileId(), file.getContents());
                    defs[i++] = def;
                }
                return defs;
            } catch (Exception ex) {
                log.warn(null, ex);
                return null;
            }
        }
    };
    BufferedImage itemImage = ItemSpriteFactory.createSprite(itemProvider, modelProvider, spriteProvider, textureProvider2, itemId, quantity, border, shadowColor, false);
    ByteArrayOutputStream bao = new ByteArrayOutputStream();
    ImageIO.write(itemImage, "png", bao);
    return ResponseEntity.ok(bao.toByteArray());
}
Also used : ItemProvider(net.runelite.cache.definitions.providers.ItemProvider) TextureLoader(net.runelite.cache.definitions.loaders.TextureLoader) ModelProvider(net.runelite.cache.definitions.providers.ModelProvider) SpriteDefinition(net.runelite.cache.definitions.SpriteDefinition) IndexEntry(net.runelite.http.service.cache.beans.IndexEntry) ArchiveEntry(net.runelite.http.service.cache.beans.ArchiveEntry) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CacheEntry(net.runelite.http.service.cache.beans.CacheEntry) TextureProvider(net.runelite.cache.definitions.providers.TextureProvider) IOException(java.io.IOException) NotFoundException(net.runelite.http.service.util.exception.NotFoundException) BufferedImage(java.awt.image.BufferedImage) FSFile(net.runelite.cache.fs.FSFile) ModelLoader(net.runelite.cache.definitions.loaders.ModelLoader) ArchiveFiles(net.runelite.cache.fs.ArchiveFiles) TextureDefinition(net.runelite.cache.definitions.TextureDefinition) SpriteLoader(net.runelite.cache.definitions.loaders.SpriteLoader) SpriteProvider(net.runelite.cache.definitions.providers.SpriteProvider) RequestMapping(org.springframework.web.bind.annotation.RequestMapping)

Example 4 with TextureDefinition

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

the class ObjExporter method export.

public void export(PrintWriter objWriter, PrintWriter mtlWriter) {
    model.computeNormals();
    model.computeTextureUVCoordinates();
    objWriter.println("mtllib " + model.id + ".mtl");
    objWriter.println("o runescapemodel");
    for (int i = 0; i < model.vertexCount; ++i) {
        objWriter.println("v " + model.vertexPositionsX[i] + " " + model.vertexPositionsY[i] * -1 + " " + model.vertexPositionsZ[i] * -1);
    }
    if (model.faceTextures != null) {
        float[][] u = model.faceTextureUCoordinates;
        float[][] v = model.faceTextureVCoordinates;
        for (int i = 0; i < model.faceCount; ++i) {
            objWriter.println("vt " + u[i][0] + " " + v[i][0]);
            objWriter.println("vt " + u[i][1] + " " + v[i][1]);
            objWriter.println("vt " + u[i][2] + " " + v[i][2]);
        }
    }
    for (VertexNormal normal : model.vertexNormals) {
        objWriter.println("vn " + normal.x + " " + normal.y + " " + normal.z);
    }
    for (int i = 0; i < model.faceCount; ++i) {
        int x = model.faceVertexIndices1[i] + 1;
        int y = model.faceVertexIndices2[i] + 1;
        int z = model.faceVertexIndices3[i] + 1;
        objWriter.println("usemtl m" + i);
        if (model.faceTextures != null) {
            objWriter.println("f " + x + "/" + (i * 3 + 1) + " " + y + "/" + (i * 3 + 2) + " " + z + "/" + (i * 3 + 3));
        } else {
            objWriter.println("f " + x + " " + y + " " + z);
        }
        objWriter.println("");
    }
    // Write material
    for (int i = 0; i < model.faceCount; ++i) {
        short textureId = -1;
        if (model.faceTextures != null) {
            textureId = model.faceTextures[i];
        }
        mtlWriter.println("newmtl m" + i);
        if (textureId == -1) {
            Color color = rs2hsbToColor(model.faceColors[i]);
            double r = color.getRed() / 255.0;
            double g = color.getGreen() / 255.0;
            double b = color.getBlue() / 255.0;
            mtlWriter.println("Kd " + r + " " + g + " " + b);
        } else {
            TextureDefinition texture = textureManager.findTexture(textureId);
            assert texture != null;
            mtlWriter.println("map_Kd sprite/" + texture.getFileIds()[0] + "-0.png");
        }
        int alpha = 0;
        if (model.faceAlphas != null) {
            alpha = model.faceAlphas[i] & 0xFF;
        }
        if (alpha != 0) {
            mtlWriter.println("d " + (alpha / 255.0));
        }
    }
}
Also used : Color(java.awt.Color) TextureDefinition(net.runelite.cache.definitions.TextureDefinition)

Example 5 with TextureDefinition

use of net.runelite.cache.definitions.TextureDefinition 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)

Aggregations

TextureDefinition (net.runelite.cache.definitions.TextureDefinition)7 BufferedImage (java.awt.image.BufferedImage)2 IOException (java.io.IOException)2 TextureLoader (net.runelite.cache.definitions.loaders.TextureLoader)2 ArchiveFiles (net.runelite.cache.fs.ArchiveFiles)2 FSFile (net.runelite.cache.fs.FSFile)2 Gson (com.google.gson.Gson)1 Color (java.awt.Color)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 InputStreamReader (java.io.InputStreamReader)1 PrintWriter (java.io.PrintWriter)1 ByteBuffer (java.nio.ByteBuffer)1 ModelDefinition (net.runelite.cache.definitions.ModelDefinition)1 SpriteDefinition (net.runelite.cache.definitions.SpriteDefinition)1 ModelLoader (net.runelite.cache.definitions.loaders.ModelLoader)1 SpriteLoader (net.runelite.cache.definitions.loaders.SpriteLoader)1 ItemProvider (net.runelite.cache.definitions.providers.ItemProvider)1