File size: 10,822 Bytes
5fc38dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9af2c2
 
5fc38dc
9b237bf
b9af2c2
 
 
 
 
 
 
 
 
5a902b9
b9af2c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba2ef65
 
 
 
 
 
 
 
 
 
 
 
b9af2c2
 
 
 
 
 
 
 
 
 
 
 
 
ba2ef65
 
 
b9af2c2
 
 
 
ba2ef65
 
b9af2c2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ba2ef65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b9af2c2
 
 
da271e5
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
---
pretty_name: Sking
language:
- en
license: agpl-3.0
task_categories:
- text-to-image
- image-to-image
tags:
- minecraft
- 3d
- voxel
- minecraft-skin
- texture
- dataset-generation
---

# Sking: Minecraft Skin 3D Rendering & Dataset Preprocessing Pipeline


This repository is utilized for training and fine-tuning the **Sking Model** ([Hugging Face Model](https://huggingface.co/EntropyDrop/Sking)). 

Along with the dataset assets, it contains the complete automated pipeline for dual-layer 3D voxel rendering, multi-view layout baking, automatic skin format conversion (Alex to Steve), voxel edge texture consistency resolution, and a RESTful FastAPI service for extracting the actual usable 64x64 RGBA skin UV map from composite training targets.

---

## 🔗 Resources

* **🗂️ Dataset**: [Hugging Face Sking Dataset](https://huggingface.co/datasets/EntropyDrop/Sking)
* **🧠 Trained Model**: [Hugging Face Sking Model](https://huggingface.co/EntropyDrop/Sking)
* **📝 Technical Report**: [Technical Architecture](https://entropydrop.com/skin/public/article/skingen)
* **🌐 Online Demo**: [EntropyDrop Web Portal](https://entropydrop.com)

---

## 🚀 Key Features

* **🎮 3D Voxel Rendering (`mc_render.py`)**
  - Powered by `PyVista` (VTK) to translate 2D skin textures into 3D voxel character meshes.
  - Supports dual-layer rendering: Core layer (base body) and Decor layer (jacket, sleeves, pants, hat).
  - Fully parameterizable limb articulation supporting rotation (Pitch/Yaw/Roll) and spatial offset configurations.
  - Interactive lighting mode with real-time viewport keyboard controls (W/S/A/D/Q/E for light position, R/F for intensity).
  - Supports orthographic and perspective projections, wireframe overlays, custom backgrounds, and alpha-transparent exports.

* **🧩 Voxel Texture Consistency Resolver (`mc_voxel_texture_resolver.py`)**
  - Solves the visual gap artifact caused by transparent adjacent faces in dual-layer skins during 3D voxelization.
  - Implements a 3D voxel projection mapping algorithm that automatically fills missing/transparent adjacent faces according to a priority sequence (`Front -> Back -> Top -> Bottom -> Left -> Right`) for 3D renders.

* **🔄 Layout Standardization: Alex-to-Steve (`alice_to_steve.py`)**
  - Automatically detects slim-armed (Alex, 3px arm width) skin formats.
  - Applies a pixel-column projection and replication mapping to expand slim arms to standard (Steve, 4px arm width) format to standardize features across the dataset.

* **📸 Multi-View Layout Baking (`build_target_img.py`)**
  - Upsamples 64x64 flat textures via box filtering and injects 2x2 white indicators on background regions to represent alpha transparency values as conditioning cues for generative models.
  - Bakes a composite layout image at `768x768` resolution featuring the 2D skin texture along with **17 distinct, pre-configured 3D render viewports** (incorporating walking, idle, back-view, close-ups, and layer-toggled angles).

* **⚡ Batch Processing Pipeline (`build_target_imgs.py`)**
  - Leverages a multi-threaded execution pool (`ThreadPoolExecutor`) managing isolated sub-processes for batch rendering.
  - Supports parallel processing on multi-core systems.

* **🔓 Skin UV Map Extraction (`extract_skin.py`)**
  - **Extraction Engine**: Crops and extracts the actual usable 64x64 skin UV map from the 2D layout area of synthesized training target images, resolving alpha transparency indicators to recover clean texture maps.
  - **FastAPI Microservice**: Exposes the UV map extractor as a REST API alongside its command-line execution mode.

* **📐 Conditioning Image Formatting (`force_resize_control_imgs.py`)**
  - Automatically resamples and centers control images inside a standard `1024x1024` alpha-transparent canvas utilizing Lanczos interpolation, ensuring compatibility with advanced ControlNet training frameworks.

* **🔄 Skin UV & View Port Horizontal Flipping (`ext_flip_img.py`)**
  - Performs horizontal flipping of character front/back views in control images and correctly flips the corresponding 64x64 skin UV map.
  - Specifically handles 3D voxel box flipping (swapping left and right faces, horizontally flipping each face, and swapping left/right limbs/sleeves/pants/legs for Steve and Alex models).

* **✂️ Front-View Dataset Slicing (`make_half_dataset.py`)**
  - Generates a front-view-only subset by horizontally splitting composite layout/control images in half, retaining only the left (front) side.
  - Automatically copies and standardizes associated files (PNG images, txt descriptions, and control maps) prefixing them with `half_`.

* **🧬 Skin Merging Utility (`merge_skins.py`)**
  - Combines two skins by extracting the head and head decoration (decor) layers from Skin A and copying them onto the body and limbs of Skin B.
  - Utilizes head mapping coordinates matching standard UV layouts.

---

## 📂 Project Structure

```bash
Sking/
├── mc_render.py                  # Core 3D engine (PyVista viewport & headless off-screen rendering)
├── mc_voxel_texture_resolver.py # Voxel texture patcher & visual difference analyzer
├── alice_to_steve.py             # Alex-to-Steve (3px-to-4px arm width) mapping algorithm
├── build_target_img.py           # Single-skin multi-view layout baker (generates 768x768 composite)
├── build_target_imgs.py          # Concurrent batch rendering pipeline
├── extract_skin.py               # Skin UV map extraction CLI & FastAPI server
├── force_resize_control_imgs.py  # Control map resampling & canvas standardization (1024x1024)
├── ext_flip_img.py               # Flip front/back views and horizontally mirror skin UV layouts
├── make_half_dataset.py          # Create front-view subset by slicing images in half horizontally
├── merge_skins.py                # Combine head of skin A with body/limbs of skin B
├── skin-mask.png                 # Pixel-level spatial mapping mask for Core layer
├── skin-decor-mask.png           # Pixel-level spatial mapping mask for Decor layer
├── skins/                        # Directory for raw input skins (.png)
├── target_imgs_v73/              # Output directory for baked multi-view layouts
├── control_imgs/                 # Processing directory for conditioning control maps
└── control_imgs_v2/              # Directory for multi-view layout control maps
```

---

## 🛠️ System Requirements & Installation

### 1. Prerequisites
- Python 3.8 or higher.
- A virtual environment configuration is highly recommended.

### 2. Installation
Install all required packages:
```bash
pip install pyvista numpy pillow fastapi uvicorn opencv-python pydantic
```
> **Note for Headless Environments**: Since `PyVista` utilizes VTK under the hood, running it on headless servers (e.g., Linux instances without GUI support) requires a virtual frame buffer configuration. Please wrap the execution using a utility like `xvfb-run`.

---

## 📖 Command Line & API Reference

### A. Bake Multi-View Layout for a Single Skin
```bash
python build_target_img.py skins/steve.png target_imgs_v73/steve.png
```
This utility automatically executes the following pipeline sequence:
1. Validates texture dimension (64x64) and format integrity (RGBA).
2. Performs slim-arm check and converts Alex formats to Steve formats dynamically if needed.
3. Patches voxel edge-transparency conflicts.
4. Generates a `768x768` composite layout containing the 2D layout and 17 distinct 3D viewports.

### B. Batch Processing
```bash
python build_target_imgs.py
```
Batch scans the `skins/` directory and processes them concurrently using parallel workers, exporting the output files into `target_imgs_v73/`.

### C. Interactive 3D Rendering & Viewing
To examine or visually debug the 3D rendering configuration of a specific skin:
```bash
python mc_render.py skins/my_skin.png --interact
```
- **Real-Time Light Position & Intensity Keyboard Controls**:
  - `W` / `S`: Translate light position along the Z-axis (Forward / Backward).
  - `A` / `D`: Translate light position along the X-axis (Left / Right).
  - `Q` / `E`: Translate light position along the Y-axis (Up / Down).
  - `R` / `F`: Increment / Decrement light intensity.
  - `P`: Print current light coordinate vector and intensity scalar to terminal output.
- **Custom Articulation & Off-Screen Export Example**:
  ```bash
  python mc_render.py skins/my_skin.png --rot-head 15 30 0 --rot-arm-right -45 0 0 --save output.png
  ```

### D. Skin UV Map Extraction API
#### CLI Execution Mode
```bash
python extract_skin.py --img target_imgs_v73/steve.png --output restored_steve.png
```
#### API Server Mode
Run the FastAPI microservice:
```bash
python extract_skin.py --server True
```
The microservice launches locally on `http://0.0.0.0:10010`.

- **Endpoint**: `POST /extract`
  - **Request Body Format**:
    ```json
    {
      "img": "<base64_encoded_synthesized_image>"
    }
    ```
  - **Response Body Format**:
    ```json
    {
      "img": "<base64_encoded_64x64_restored_skin_png>"
    }
    ```

### E. Horizontal Flipping of Views and UV Maps
This script horizontally flips character front and back views separately for a ControlNet image, and mirrors the 64x64 skin UV map by swapping and mirroring corresponding limbs and details.
```bash
python ext_flip_img.py <original_id> <new_id>
```
- Processes `control_imgs_v2/{original_id}.png` to generate flipped version at `control_imgs_v2/{new_id}.png`.
- Flips `img_label/{original_id}.png` (UV map) to generate `img_label/{new_id}.png`.
- Copies the corresponding text description from `img_label/{original_id}.txt` to `img_label/{new_id}.txt`.

### F. Front-View Dataset Slicing
This script processes the dataset by horizontally cutting all layout/control images in half (keeping only the left half, which corresponds to the character's front view) to create a front-view-only training subset.
```bash
python make_half_dataset.py
```
- Automatically processes files in `img_label/` and `control_imgs_v2/`.
- Saves sliced files with the prefix `half_` (e.g. `half_{original_name}.png`, `half_{original_name}.txt`).
- Proactively skips already processed files.

### G. Skin Merging Utility (Head of A + Body/Limbs of B)
This script merges two skins by taking the head and hat decoration layers from Skin A and the body and limbs from Skin B.
```bash
python merge_skins.py <path_to_skin_a> <path_to_skin_b> -o <output_path>
```
- Extracts head region UVs (including outer decors) from skin A.
- Pastes them over skin B's head region, outputting the newly combined skin.

---

## 📄 License

This project is licensed under the GNU Affero General Public License v3.0 - see the [LICENSE](LICENSE) file for details.