# Image Shader
只使用着色器在你的屏幕上显示图片～  
**注意：编译着色器会需要一些时间！**

![](https://count.getloli.com/@myueqf?name=Lantern-shader&theme=love-and-deepspace&padding=7&scale=1&darkmode=auto)
![A Minecraft Screenshot](https://cdn.modrinth.com/data/cached_images/d9b5a9d9e90d1b2127f6b31b6c14b63c00853b09.png)


### 关于自定义图片的方法～
首先，需要使用ffmpeg（或者任意工具）把你的图片调整到**160x160像素**大小。

```bash
使用 ffmpeg ：
ffmpeg -i Input.png -vf scale=160:160 image.png
```

然后把调整好的图片放到下面的 Python Script **相同的目录**下。
运行脚本你会得到一个 `image_chunks` 目录，把里面**16个glsl**覆盖到 `shaders/data/image_chunks` (需要先解压shader pack)
然后进入游戏，到着色器设置里开启自定义图片选项，等待编译着色器完成就可以啦～。

<details>
<summary>A Python Script</summary>
  
```python  
from PIL import Image
import os

def generate_glsl_chunks(image_path, output_dir="image_chunks"):
    try:
        img = Image.open(image_path).convert('RGBA')
    except FileNotFoundError:
        print(f"Error: Image file not found at {image_path}")
        return

    width, height = img.size
    if width != 160 or height != 160:
        print(f"Error: Image must be 160x160 pixels, but it is {width}x{height}.")
        print(f"You can use:")
        print(f"ffmpeg -i image.png -vf scale=160:160 output.png")
        print(f"to adjust the image resolution.")
        return
        
    os.makedirs(output_dir, exist_ok=True)
    chunk_size = 40
    num_chunks_x = width // chunk_size
    num_chunks_y = height // chunk_size

    for chunk_y in range(num_chunks_y):
        for chunk_x in range(num_chunks_x):
            left = chunk_x * chunk_size
            upper = chunk_y * chunk_size
            right = left + chunk_size
            lower = upper + chunk_size
            sub_img = img.crop((left, upper, right, lower))
            pixel_data = list(sub_img.getdata())

            array_name = f"image_data_{chunk_y}_{chunk_x}"
            output_file_path = os.path.join(output_dir, f"{array_name}.glsl")

            glsl_array = f"vec4 {array_name}[{chunk_size * chunk_size}] = vec4[{chunk_size * chunk_size}](\n"
            
            for i, pixel in enumerate(pixel_data):
                r, g, b, a = pixel
                glsl_array += f"    vec4({r/255.0}, {g/255.0}, {b/255.0}, {a/255.0})"
                if i < len(pixel_data) - 1:
                    glsl_array += ","
                if (i + 1) % 4 == 0:
                    glsl_array += "\n"
            
            glsl_array += ");\n"

            with open(output_file_path, 'w') as f:
                f.write(glsl_array)
            
            print(f"Generated file: {output_file_path}")

# --- Config ---
image_file = "image.png"
output_directory = "image_chunks"

generate_glsl_chunks(image_file, output_directory)

```
