Search in sources :

Example 1 with ModelDisplay

use of com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelDisplay in project InteractiveChat-DiscordSRV-Addon by LOOHP.

the class ModelRenderer method renderPlayer.

public RenderResult renderPlayer(int width, int height, ResourceManager manager, boolean post1_8, boolean slim, Map<String, TextureResource> providedTextures, TintIndexData tintIndexData, Map<PlayerModelItemPosition, PlayerModelItem> modelItems) {
    String cacheKey = cacheKey(width, height, manager.getUuid(), slim, cacheKeyModelItems(modelItems), cacheKeyProvidedTextures(providedTextures));
    Cache<?> cachedRender = Cache.getCache(cacheKey);
    if (cachedRender != null) {
        RenderResult cachedResult = (RenderResult) cachedRender.getObject();
        if (cachedResult.isSuccessful()) {
            return cachedResult;
        }
    }
    BlockModel playerModel = manager.getModelManager().resolveBlockModel(slim ? PLAYER_MODEL_SLIM_RESOURCELOCATION : PLAYER_MODEL_RESOURCELOCATION, InteractiveChat.version.isOld(), Collections.emptyMap());
    if (playerModel == null) {
        return new RenderResult(MODEL_NOT_FOUND, null);
    }
    Model playerRenderModel = generateStandardRenderModel(playerModel, manager, providedTextures, tintIndexData, false, true);
    for (PlayerModelItem playerModelItem : modelItems.values()) {
        BlockModel itemBlockModel = playerModelItem.getModelKey() == null ? null : manager.getModelManager().resolveBlockModel(playerModelItem.getModelKey(), InteractiveChat.version.isOld(), playerModelItem.getPredicate());
        Model itemRenderModel = null;
        if (itemBlockModel != null) {
            if (itemBlockModel.getRawParent() == null || !itemBlockModel.getRawParent().contains("/")) {
                itemRenderModel = generateStandardRenderModel(itemBlockModel, manager, playerModelItem.getProvidedTextures(), playerModelItem.getTintIndexData(), playerModelItem.isEnchanted(), false);
            } else if (itemBlockModel.getRawParent().equals(ModelManager.ITEM_BASE)) {
                BufferedImage image = new BufferedImage(INTERNAL_W, INTERNAL_H, BufferedImage.TYPE_INT_ARGB);
                Graphics2D g = image.createGraphics();
                g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR);
                for (int i = 0; itemBlockModel.getTextures().containsKey(ModelManager.ITEM_BASE_LAYER + i); i++) {
                    String resourceLocation = itemBlockModel.getTextures().get(ModelManager.ITEM_BASE_LAYER + i);
                    if (!resourceLocation.contains(":")) {
                        resourceLocation = ResourceRegistry.DEFAULT_NAMESPACE + ":" + resourceLocation;
                    }
                    TextureResource resource = playerModelItem.getProvidedTextures().get(resourceLocation);
                    if (resource == null) {
                        resource = manager.getTextureManager().getTexture(resourceLocation);
                    }
                    BufferedImage texture = resource.getTexture();
                    texture = tintIndexData.applyTint(texture, 1);
                    if (resource.hasTextureMeta()) {
                        TextureMeta meta = resource.getTextureMeta();
                        if (meta.hasProperties()) {
                            TextureProperties properties = meta.getProperties();
                            if (properties.isBlur()) {
                                texture = ImageUtils.applyGaussianBlur(texture);
                            }
                        }
                        if (meta.hasAnimation()) {
                            TextureAnimation animation = meta.getAnimation();
                            if (animation.hasWidth() && animation.hasHeight()) {
                                texture = ImageUtils.copyAndGetSubImage(texture, 0, 0, animation.getWidth(), animation.getHeight());
                            } else {
                                texture = ImageUtils.copyAndGetSubImage(texture, 0, 0, texture.getWidth(), texture.getWidth());
                            }
                        }
                    }
                    if (resourceLocation.equals(ResourceRegistry.MAP_MARKINGS_LOCATION)) {
                        ImageUtils.xor(image, ImageUtils.resizeImageAbs(texture, image.getWidth(), image.getHeight()), 200);
                    } else {
                        g.drawImage(texture, 0, 0, image.getWidth(), image.getHeight(), null);
                    }
                }
                g.dispose();
                image = tintIndexData.applyTint(image, 0);
                if (playerModelItem.isEnchanted()) {
                    image = enchantmentGlintProvider.apply(image);
                }
                itemRenderModel = generateItemRenderModel(16, 16, 16, image);
            }
        }
        if (itemRenderModel != null) {
            itemRenderModel.translate(-16 / 2.0, -16 / 2.0, -16 / 2.0);
            ModelDisplay displayData = itemBlockModel.getRawDisplay().get(playerModelItem.getPosition().getModelDisplayPosition());
            boolean flipX = playerModelItem.getPosition().isLiteralFlipped();
            boolean isMirror = false;
            if (displayData == null && playerModelItem.getPosition().getModelDisplayPosition().hasFallback()) {
                displayData = itemBlockModel.getRawDisplay().get(playerModelItem.getPosition().getModelDisplayPosition().getFallback());
                isMirror = true;
            }
            if (isMirror || !flipX) {
                if (displayData != null) {
                    Coordinates3D scale = displayData.getScale();
                    itemRenderModel.scale(scale.getX(), scale.getY(), scale.getZ());
                    Coordinates3D rotation = displayData.getRotation();
                    itemRenderModel.rotate(rotation.getX(), rotation.getY(), rotation.getZ() + (post1_8 ? 10 : 0), false);
                    Coordinates3D transform = displayData.getTranslation();
                    itemRenderModel.translate(transform.getX(), transform.getY() + (post1_8 ? -10 : 0), transform.getZ() + (post1_8 ? -2.75 : 0));
                }
                if (flipX) {
                    itemRenderModel.flipAboutPlane(false, true, true);
                }
            } else {
                if (displayData != null) {
                    Coordinates3D scale = displayData.getScale();
                    itemRenderModel.scale(scale.getX(), scale.getY(), scale.getZ());
                    Coordinates3D rotation = displayData.getRotation();
                    itemRenderModel.rotate(rotation.getX(), -rotation.getY(), -rotation.getZ(), false);
                    Coordinates3D transform = displayData.getTranslation();
                    itemRenderModel.translate(-transform.getX(), transform.getY(), transform.getZ());
                }
            }
            if (playerModelItem.getPosition().yIsZAxis()) {
                if (post1_8) {
                    itemRenderModel.rotate(180, 180, 0, false);
                } else {
                    itemRenderModel.rotate(90, 0, 0, true);
                }
            }
            double scale = playerModelItem.getPosition().getScale();
            itemRenderModel.scale(scale, scale, scale);
            itemRenderModel.translate((16 * scale) / 2, (16 * scale) / 2, (16 * scale) / 2);
            Coordinates3D defaultTranslation = playerModelItem.getPosition().getDefaultTranslate();
            itemRenderModel.translate(defaultTranslation.getX(), defaultTranslation.getY(), defaultTranslation.getZ());
            playerRenderModel.append(itemRenderModel);
        }
    }
    playerRenderModel.translate(-16 / 2.0, -16 / 2.0, -16 / 2.0);
    playerRenderModel.rotate(0, 180, 0, false);
    playerRenderModel.translate(16 / 2.0, 16 / 2.0, 16 / 2.0);
    BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    renderPlayerModel(playerRenderModel, image, playerModel.getGUILight());
    RenderResult result = new RenderResult(image, null);
    Cache.putCache(cacheKey, result, cacheTimeoutSupplier.getAsLong());
    return result;
}
Also used : TextureResource(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureResource) Coordinates3D(com.loohp.interactivechatdiscordsrvaddon.resources.models.Coordinates3D) TextureAnimation(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureAnimation) BufferedImage(java.awt.image.BufferedImage) Graphics2D(java.awt.Graphics2D) TextureProperties(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureProperties) TextureMeta(com.loohp.interactivechatdiscordsrvaddon.resources.textures.TextureMeta) Model(com.loohp.blockmodelrenderer.render.Model) BlockModel(com.loohp.interactivechatdiscordsrvaddon.resources.models.BlockModel) BlockModel(com.loohp.interactivechatdiscordsrvaddon.resources.models.BlockModel) ModelDisplay(com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelDisplay)

Aggregations

Model (com.loohp.blockmodelrenderer.render.Model)1 BlockModel (com.loohp.interactivechatdiscordsrvaddon.resources.models.BlockModel)1 Coordinates3D (com.loohp.interactivechatdiscordsrvaddon.resources.models.Coordinates3D)1 ModelDisplay (com.loohp.interactivechatdiscordsrvaddon.resources.models.ModelDisplay)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 Graphics2D (java.awt.Graphics2D)1 BufferedImage (java.awt.image.BufferedImage)1