Search in sources :

Example 1 with Hexahedron

use of com.loohp.blockmodelrenderer.render.Hexahedron in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class ModelRenderer method generateStandardRenderModel.

@SuppressWarnings("SuspiciousNameCombination")
private Model generateStandardRenderModel(BlockModel blockModel, ResourceManager manager, Map<String, TextureResource> providedTextures, TintIndexData tintIndexData, boolean enchanted, boolean skin) {
    Map<String, BufferedImage> cachedResize = new ConcurrentHashMap<>();
    List<ModelElement> elements = new ArrayList<>(blockModel.getElements());
    List<Hexahedron> hexahedrons = new ArrayList<>(elements.size());
    Queue<Future<Hexahedron>> tasks = new ConcurrentLinkedQueue<>();
    Iterator<ModelElement> itr = elements.iterator();
    while (itr.hasNext()) {
        ModelElement element = itr.next();
        tasks.add(modelResolvingService.submit(() -> {
            ModelElementRotation rotation = element.getRotation();
            boolean ignoreZFight = false;
            if (rotation != null) {
                ignoreZFight = rotation.isRescale();
            }
            Hexahedron hexahedron = Hexahedron.fromCorners(new Point3D(element.getFrom().getX(), element.getFrom().getY(), element.getFrom().getZ()), new Point3D(element.getTo().getX(), element.getTo().getY(), element.getTo().getZ()), ignoreZFight, new BufferedImage[6]);
            BufferedImage[] images = new BufferedImage[6];
            BufferedImage[] overlayImages = new BufferedImage[6];
            int i = 0;
            for (ModelFaceSide side : ModelFaceSide.values()) {
                ModelFace faceData = element.getFace(side);
                if (faceData == null) {
                    images[i] = null;
                } else {
                    TextureUV uv = faceData.getUV();
                    TextureResource resource = providedTextures.get(faceData.getTexture());
                    if (resource == null) {
                        resource = manager.getTextureManager().getTexture(faceData.getTexture(), false);
                    }
                    if (resource == null || !resource.isTexture()) {
                        images[i] = null;
                    } else if (uv != null && (uv.getXDiff() == 0 || uv.getYDiff() == 0)) {
                        images[i] = null;
                    } else {
                        BufferedImage cached = cachedResize.get(faceData.getTexture());
                        if (cached == null) {
                            cached = resource.getTexture();
                            if (resource.hasTextureMeta()) {
                                TextureMeta meta = resource.getTextureMeta();
                                if (meta.hasProperties()) {
                                    TextureProperties properties = meta.getProperties();
                                    if (properties.isBlur()) {
                                        cached = ImageUtils.applyGaussianBlur(cached);
                                    }
                                }
                                if (meta.hasAnimation()) {
                                    TextureAnimation animation = meta.getAnimation();
                                    if (animation.hasWidth() && animation.hasHeight()) {
                                        cached = ImageUtils.copyAndGetSubImage(cached, 0, 0, animation.getWidth(), animation.getHeight());
                                    } else {
                                        cached = ImageUtils.copyAndGetSubImage(cached, 0, 0, cached.getWidth(), cached.getWidth());
                                    }
                                }
                            }
                            if (cached.getWidth() > cached.getHeight()) {
                                cached = ImageUtils.resizeImageFillWidth(cached, skin ? SKIN_RESOLUTION : TEXTURE_RESOLUTION);
                            } else {
                                cached = ImageUtils.resizeImageFillHeight(cached, skin ? SKIN_RESOLUTION : TEXTURE_RESOLUTION);
                            }
                            cachedResize.put(faceData.getTexture(), cached);
                        }
                        images[i] = ImageUtils.copyImage(cached);
                        if (uv == null) {
                            Point3D[] points;
                            switch(side) {
                                case DOWN:
                                    points = hexahedron.getDownFace().getPoints();
                                    break;
                                case EAST:
                                    points = hexahedron.getEastFace().getPoints();
                                    break;
                                case NORTH:
                                    points = hexahedron.getNorthFace().getPoints();
                                    break;
                                case SOUTH:
                                    points = hexahedron.getSouthFace().getPoints();
                                    break;
                                case UP:
                                    points = hexahedron.getUpFace().getPoints();
                                    break;
                                case WEST:
                                default:
                                    points = hexahedron.getWestFace().getPoints();
                                    break;
                            }
                            double x1;
                            double y1;
                            double x2;
                            double y2;
                            if (points[0].x == points[2].x) {
                                x1 = points[0].y;
                                y1 = points[0].z;
                                x2 = points[2].y;
                                y2 = points[2].z;
                            } else if (points[0].y == points[2].y) {
                                x1 = points[0].z;
                                y1 = points[0].x;
                                x2 = points[2].z;
                                y2 = points[2].x;
                            } else {
                                x1 = points[0].y;
                                y1 = points[0].x;
                                x2 = points[2].y;
                                y2 = points[2].x;
                            }
                            uv = new TextureUV(Math.min(x1, x2), Math.min(y1, y2), Math.max(x1, x2), Math.max(y1, y2));
                        }
                        uv = uv.getScaled(1, (double) images[i].getHeight() / (double) images[i].getWidth());
                        uv = uv.getScaled((double) images[i].getWidth() / 16.0);
                        int x1 = (int) Math.ceil(uv.getX1());
                        int y1 = (int) Math.ceil(uv.getY1());
                        int dX = Math.abs((int) Math.floor(uv.getX2()) - x1);
                        int dY = Math.abs((int) Math.floor(uv.getY2()) - y1);
                        if (uv.isVerticallyFlipped()) {
                            images[i] = ImageUtils.flipVertically(images[i]);
                            y1 = images[i].getHeight() - y1;
                        }
                        if (uv.isHorizontallyFlipped()) {
                            images[i] = ImageUtils.flipHorizontal(images[i]);
                            x1 = images[i].getWidth() - x1;
                        }
                        images[i] = ImageUtils.copyAndGetSubImage(images[i], x1, y1, Math.max(1, dX), Math.max(1, dY));
                        int rotationAngle = faceData.getRotation();
                        if (rotationAngle % 360 != 0) {
                            images[i] = ImageUtils.rotateImageByDegrees(images[i], rotationAngle);
                        }
                        images[i] = tintIndexData.applyTint(images[i], faceData.getTintindex());
                        if (enchanted) {
                            overlayImages[i] = rawEnchantmentGlintProvider.apply(images[i]);
                        }
                    }
                }
                i++;
            }
            hexahedron.setImage(images);
            hexahedron.setOverlay(overlayImages);
            hexahedron.setOverlayAdditionFactor(OVERLAY_ADDITION_FACTORS);
            if (rotation != null) {
                hexahedron.translate(-rotation.getOrigin().getX(), -rotation.getOrigin().getY(), -rotation.getOrigin().getZ());
                switch(rotation.getAxis()) {
                    case X:
                        hexahedron.rotate(rotation.getAngle(), 0, 0, false);
                        break;
                    case Y:
                        hexahedron.rotate(0, rotation.getAngle(), 0, false);
                        break;
                    case Z:
                    default:
                        hexahedron.rotate(0, 0, rotation.getAngle(), false);
                        break;
                }
                hexahedron.translate(rotation.getOrigin().getX(), rotation.getOrigin().getY(), rotation.getOrigin().getZ());
            }
            return hexahedron;
        }));
        itr.remove();
    }
    while (!tasks.isEmpty() || !elements.isEmpty()) {
        Future<Hexahedron> task = tasks.poll();
        if (task == null) {
            try {
                TimeUnit.MILLISECONDS.sleep(1);
            } catch (InterruptedException e) {
            }
        } else {
            try {
                hexahedrons.add(task.get());
            } catch (InterruptedException | ExecutionException e) {
                e.printStackTrace();
            }
        }
    }
    return new Model(hexahedrons);
}
Also used : TextureResource(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureResource) ArrayList(java.util.ArrayList) BufferedImage(java.awt.image.BufferedImage) ModelElement(com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelElement) TextureUV(com.loohp.interactivechatdiscordsrvaddon.resources.models.TextureUV) ModelFace(com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelFace) Hexahedron(com.loohp.blockmodelrenderer.render.Hexahedron) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ExecutionException(java.util.concurrent.ExecutionException) TextureAnimation(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureAnimation) ModelFaceSide(com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelFace.ModelFaceSide) ModelElementRotation(com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelElement.ModelElementRotation) TextureProperties(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureProperties) Point3D(com.loohp.blockmodelrenderer.render.Point3D) TextureMeta(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureMeta) Model(com.loohp.blockmodelrenderer.render.Model) BlockModel(com.loohp.interactivechatdiscordsrvaddon.resources.models.BlockModel) Future(java.util.concurrent.Future) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue)

Example 2 with Hexahedron

use of com.loohp.blockmodelrenderer.render.Hexahedron in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class ModelRenderer method generateItemRenderModel.

private Model generateItemRenderModel(double width, double height, double depth, BufferedImage image) {
    int w = image.getWidth();
    int h = image.getHeight();
    double intervalX = (1.0 / (double) w) * width;
    double intervalY = (1.0 / (double) h) * height;
    double z = depth / 2 - 0.5;
    List<Hexahedron> hexahedrons = new ArrayList<>();
    int[] colors = image.getRGB(0, 0, w, h, null, 0, w);
    hexahedrons.add(Hexahedron.fromCorners(new Point3D(0, 0, z), new Point3D(width, height, z + 1), false, new BufferedImage[] { null, null, ImageUtils.copyImage(image), null, ImageUtils.copyImage(image), null }));
    for (int y = 0; y < h; y++) {
        for (int x = 0; x < w; x++) {
            int color = colors[y * w + x];
            if (ColorUtils.getAlpha(color) > 0) {
                BufferedImage pixel = new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
                pixel.setRGB(0, 0, color);
                BufferedImage[] imageArray = new BufferedImage[6];
                // u
                imageArray[0] = ColorUtils.getAlpha(ImageUtils.getRGB(colors, x, y + 1, w, h)) <= 0 ? ImageUtils.copyImage(pixel) : null;
                // d
                imageArray[1] = ColorUtils.getAlpha(ImageUtils.getRGB(colors, x, y - 1, w, h)) <= 0 ? ImageUtils.copyImage(pixel) : null;
                // n
                imageArray[2] = null;
                // e
                imageArray[3] = ColorUtils.getAlpha(ImageUtils.getRGB(colors, x + 1, y, w, h)) <= 0 ? ImageUtils.copyImage(pixel) : null;
                // s
                imageArray[4] = null;
                // w
                imageArray[5] = ColorUtils.getAlpha(ImageUtils.getRGB(colors, x - 1, y, w, h)) <= 0 ? ImageUtils.copyImage(pixel) : null;
                if (!CustomArrayUtils.allNull(imageArray)) {
                    double scaledX = (double) x * intervalX;
                    double scaledY = height - (double) y * intervalY;
                    hexahedrons.add(Hexahedron.fromCorners(new Point3D(scaledX, scaledY, z), new Point3D(scaledX + intervalX, scaledY - intervalY, z + 1), false, imageArray));
                }
            }
        }
    }
    return new Model(hexahedrons);
}
Also used : Point3D(com.loohp.blockmodelrenderer.render.Point3D) Hexahedron(com.loohp.blockmodelrenderer.render.Hexahedron) ArrayList(java.util.ArrayList) Model(com.loohp.blockmodelrenderer.render.Model) BlockModel(com.loohp.interactivechatdiscordsrvaddon.resources.models.BlockModel) BufferedImage(java.awt.image.BufferedImage)

Aggregations

Hexahedron (com.loohp.blockmodelrenderer.render.Hexahedron)2 Model (com.loohp.blockmodelrenderer.render.Model)2 Point3D (com.loohp.blockmodelrenderer.render.Point3D)2 BlockModel (com.loohp.interactivechatdiscordsrvaddon.resources.models.BlockModel)2 BufferedImage (java.awt.image.BufferedImage)2 ArrayList (java.util.ArrayList)2 ModelElement (com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelElement)1 ModelElementRotation (com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelElement.ModelElementRotation)1 ModelFace (com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelFace)1 ModelFaceSide (com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelFace.ModelFaceSide)1 TextureUV (com.loohp.interactivechatdiscordsrvaddon.resources.models.TextureUV)1 TextureAnimation (com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureAnimation)1 TextureMeta (com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureMeta)1 TextureProperties (com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureProperties)1 TextureResource (com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureResource)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 ConcurrentLinkedQueue (java.util.concurrent.ConcurrentLinkedQueue)1 ExecutionException (java.util.concurrent.ExecutionException)1 Future (java.util.concurrent.Future)1