upd: Improve POM
Browse files- README.md +131 -22
- app.py +27 -0
- chord/minecraft_pbr.py +22 -2
- chord/normal_utils.py +76 -6
README.md
CHANGED
|
@@ -1,44 +1,153 @@
|
|
| 1 |
---
|
| 2 |
-
title: Chord Demo
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.49.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
python_version: 3.12
|
|
|
|
| 11 |
---
|
| 12 |
|
| 13 |
<div align="center">
|
| 14 |
-
<h1>Chord:
|
| 15 |
|
| 16 |
<a href="https://arxiv.org/abs/2509.09952"><img src="https://img.shields.io/badge/arXiv-2509.09952-B31B1B?logo=arxiv&logoColor=white&style=flat-square" alt="arXiv"></a>
|
| 17 |
<a href="https://ubisoft-laforge.github.io/world/chord/"><img src="https://img.shields.io/badge/Project-Page-brightgreen?logo=ubisoft&logoColor=white&style=flat-square" alt="Project Page"></a>
|
|
|
|
| 18 |
|
| 19 |
-
[Zhi Ying](https://orcid.org/0009-0008-8390-3366)\*, [Boxiang Rong](https://ribosome-rbx.github.io/)\*, [Jingyu Wang](https://ccetaw.github.io/), [Maoyuan Xu](https://ultraman-blazar.github.io/)
|
| 20 |
</div>
|
| 21 |
|
| 22 |
-
|
| 23 |
|
| 24 |
-
|
| 25 |
|
| 26 |
-
|
| 27 |
|
| 28 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 29 |
|
| 30 |
-
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
```
|
| 33 |
-
@misc{ying2025chord,
|
| 34 |
-
title={Chord: Chain of Rendering Decomposition for PBR Material Estimation from Generated Texture Images},
|
| 35 |
-
author={Zhi Ying and Boxiang Rong and Jingyu Wang and Maoyuan Xu},
|
| 36 |
-
year={2025},
|
| 37 |
-
eprint={2509.09952},
|
| 38 |
-
archivePrefix={arXiv},
|
| 39 |
-
primaryClass={cs.GR},
|
| 40 |
-
url={https://arxiv.org/abs/2509.09952},
|
| 41 |
-
}
|
| 42 |
-
```
|
| 43 |
|
| 44 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
---
|
| 2 |
+
title: Chord Demo for Minecraft Shaders
|
| 3 |
+
emoji: 🎸
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: green
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 5.49.1
|
| 8 |
app_file: app.py
|
| 9 |
pinned: false
|
| 10 |
python_version: 3.12
|
| 11 |
+
short_description: Chord with labPBR and RTX/VV texture output
|
| 12 |
---
|
| 13 |
|
| 14 |
<div align="center">
|
| 15 |
+
<h1>Chord: PBR Material Estimation</h1>
|
| 16 |
|
| 17 |
<a href="https://arxiv.org/abs/2509.09952"><img src="https://img.shields.io/badge/arXiv-2509.09952-B31B1B?logo=arxiv&logoColor=white&style=flat-square" alt="arXiv"></a>
|
| 18 |
<a href="https://ubisoft-laforge.github.io/world/chord/"><img src="https://img.shields.io/badge/Project-Page-brightgreen?logo=ubisoft&logoColor=white&style=flat-square" alt="Project Page"></a>
|
| 19 |
+
<a href="https://huggingface.co/spaces/ksangk/chord-demo"><img src="https://img.shields.io/badge/Original-Demo-yellow?logo=huggingface&logoColor=white&style=flat-square" alt="Original Demo"></a>
|
| 20 |
|
|
|
|
| 21 |
</div>
|
| 22 |
|
| 23 |
+
A Gradio demo for **Chord**, a deep learning model that estimates PBR (Physically Based Rendering) materials from texture images.
|
| 24 |
|
| 25 |
+
> This fork adds **Minecraft shader format exports** for both Java Edition (LabPBR 1.3) and Bedrock Edition (RTX & Vibrant Visuals).
|
| 26 |
|
| 27 |
+
## Features
|
| 28 |
|
| 29 |
+
- **PBR Material Estimation**: Predicts basecolor, normal, roughness, and metalness maps from a single input image
|
| 30 |
+
- **Minecraft Shader Export**: Outputs textures compatible with popular Minecraft shaders
|
| 31 |
+
- **Height Map Derivation**: Generates height maps for Parallax Occlusion Mapping (POM) using Frankot-Chellappa integration
|
| 32 |
+
- **AO Derivation**: Computes ambient occlusion from normal map divergence
|
| 33 |
+
- **Advanced Material Properties**: Porosity, subsurface scattering (SSS), and emission extraction
|
| 34 |
|
| 35 |
+
## Note
|
| 36 |
|
| 37 |
+
This generates _predictions_ and _estimations_ of the PBR output. You may need to modify the output to improve the appearance in various shaders.
|
| 38 |
+
|
| 39 |
+
## Installation
|
| 40 |
+
|
| 41 |
+
```bash
|
| 42 |
+
# Clone the repository
|
| 43 |
+
git clone https://huggingface.co/spaces/jasong/chord-minecraft-pbr
|
| 44 |
+
cd chord-demo
|
| 45 |
+
|
| 46 |
+
# Install dependencies (requires CUDA)
|
| 47 |
+
pip install -r requirements.txt
|
| 48 |
+
|
| 49 |
+
# Set up Hugging Face token for model download
|
| 50 |
+
export HF_TOKEN=your_huggingface_token
|
| 51 |
+
|
| 52 |
+
# Run the demo
|
| 53 |
+
python app.py
|
| 54 |
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
|
| 56 |
+
Alternatively, place `chord_v1.safetensors` in the project root to skip the download.
|
| 57 |
+
|
| 58 |
+
## Output Formats
|
| 59 |
+
|
| 60 |
+
### LabPBR 1.3 (Java Edition)
|
| 61 |
+
|
| 62 |
+
For shaders like Iris, OptiFine, Complementary, etc.
|
| 63 |
+
|
| 64 |
+
| Texture | Channels | Description |
|
| 65 |
+
|---------|----------|-------------|
|
| 66 |
+
| **Albedo** | RGB | Basecolor texture |
|
| 67 |
+
| **Specular (_s)** | R | Perceptual smoothness (`1 - sqrt(roughness)`) |
|
| 68 |
+
| | G | F0/Metal ID (10=dielectric, 230-237=metals, 255=custom) |
|
| 69 |
+
| | B | Porosity (0-64) or SSS (65-255) |
|
| 70 |
+
| | A | Emission (1-254, where 0 and 255 = no emission) |
|
| 71 |
+
| **Normal (_n)** | R | Normal X |
|
| 72 |
+
| | G | Normal Y (DirectX, Y-down) |
|
| 73 |
+
| | B | Ambient Occlusion |
|
| 74 |
+
| | A | Height/POM (0.25=25% depth, 1.0=surface) |
|
| 75 |
+
|
| 76 |
+
### Bedrock
|
| 77 |
+
|
| 78 |
+
For Minecraft Bedrock Edition with Vibrant Visuals, RTX, or BetterRTX.
|
| 79 |
+
|
| 80 |
+
| Texture | Channels | Description |
|
| 81 |
+
|---------|----------|-------------|
|
| 82 |
+
| **Albedo** | RGB | Basecolor texture |
|
| 83 |
+
| **Normal (_normal)** | RGB | DirectX normal map |
|
| 84 |
+
| **MER (_mer)** | R | Metalness |
|
| 85 |
+
| | G | Emissive level |
|
| 86 |
+
| | B | Roughness |
|
| 87 |
+
| **MERS (_mers)** | RGBA | MER + Alpha for SSS thickness |
|
| 88 |
+
|
| 89 |
+
## Settings Reference
|
| 90 |
+
|
| 91 |
+
### General
|
| 92 |
+
- **Seamless/Tileable**: Enable for textures that should tile seamlessly
|
| 93 |
+
|
| 94 |
+
### AO & Height Derivation (LabPBR)
|
| 95 |
+
- **AO Strength**: Contrast multiplier for ambient occlusion (default: 2.0)
|
| 96 |
+
- **AO Blur**: Gaussian blur radius for AO smoothing (default: 5)
|
| 97 |
+
- **Include Height Map**: Enable POM height derivation (disable for flat surfaces)
|
| 98 |
+
|
| 99 |
+
### Height Relief Balance
|
| 100 |
+
Fine-tune the height map frequency response:
|
| 101 |
+
- **Low Frequencies**: Large-scale height variations (default: 1.0)
|
| 102 |
+
- **Mid Frequencies**: Medium surface features (default: 1.0)
|
| 103 |
+
- **High Frequencies**: Fine surface detail (default: 1.0)
|
| 104 |
+
- **Global Intensity**: Overall height strength, 0 = flat (default: 1.0)
|
| 105 |
+
|
| 106 |
+
### Porosity (LabPBR Blue 0-64)
|
| 107 |
+
- **Compute Porosity**: Calculate from AO, smoothness, and F0
|
| 108 |
+
- **Normalize Porosity**: Normalize to full range before scaling
|
| 109 |
+
|
| 110 |
+
### Subsurface Scattering
|
| 111 |
+
- **Compute SSS**: Calculate thickness from normal curvature
|
| 112 |
+
- **Curvature Weight**: Contribution from surface curvature (default: 0.7)
|
| 113 |
+
- **AO Weight**: Contribution from inverted AO (default: 0.3)
|
| 114 |
+
- **SSS Blur**: Softness of SSS effect (default: 2)
|
| 115 |
+
|
| 116 |
+
### Emission
|
| 117 |
+
- **Compute Emission**: Extract from bright basecolor regions
|
| 118 |
+
- **Emission Threshold**: Luminance threshold (default: 0.85)
|
| 119 |
+
- **Emission Knee**: Soft transition width (default: 0.1)
|
| 120 |
+
- **Emission Bloom**: Optional blur for bloom effect (default: 0)
|
| 121 |
+
|
| 122 |
+
### Hardcoded Metals (LabPBR)
|
| 123 |
+
Select predefined metal types for accurate F0 values:
|
| 124 |
+
- None (Auto), Custom (255), Iron (230), Gold (231), Aluminum (232), Chrome (233), Copper (234), Lead (235), Platinum (236), Silver (237)
|
| 125 |
+
|
| 126 |
+
## Technical Notes
|
| 127 |
+
|
| 128 |
+
### Height Map for Minecraft POM
|
| 129 |
+
The height map is scaled to [0.25, 1.0] where:
|
| 130 |
+
- **Black (0.25)** = 25% block depth (deepest point)
|
| 131 |
+
- **White (1.0)** = Surface level (no displacement)
|
| 132 |
+
|
| 133 |
+
This follows Minecraft shader conventions where pure black represents 25% of a block's depth, not absolute zero.
|
| 134 |
+
|
| 135 |
+
### Frequency Filtering
|
| 136 |
+
Height derivation uses FFT-based Frankot-Chellappa integration with configurable frequency band weighting:
|
| 137 |
+
- Low frequencies: 0-20% of Nyquist
|
| 138 |
+
- Mid frequencies: 20-50% of Nyquist
|
| 139 |
+
- High frequencies: 50-100% of Nyquist
|
| 140 |
+
|
| 141 |
+
Smooth hermite interpolation blends between bands.
|
| 142 |
+
|
| 143 |
+
## Credits
|
| 144 |
+
|
| 145 |
+
This demo is based on the [original Chord demo](https://huggingface.co/spaces/ksangk/chord-demo) by Ubisoft La Forge.
|
| 146 |
+
|
| 147 |
+
**Original Authors**: [Zhi Ying](https://orcid.org/0009-0008-8390-3366)\*, [Boxiang Rong](https://ribosome-rbx.github.io/)\*, [Jingyu Wang](https://ccetaw.github.io/), [Maoyuan Xu](https://ultraman-blazar.github.io/)
|
| 148 |
+
|
| 149 |
+
**Paper**: "Chord: Chain of Rendering Decomposition for PBR Material Estimation from Generated Texture Images"
|
| 150 |
+
|
| 151 |
+
---
|
| 152 |
+
|
| 153 |
+
This demo is not affiliated with or endorsed by Mojang, Microsoft, NVIDIA, Ubisoft or the original Chord project authors.
|
app.py
CHANGED
|
@@ -91,6 +91,11 @@ def inference(
|
|
| 91 |
seamless,
|
| 92 |
ao_strength,
|
| 93 |
ao_blur,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
compute_porosity,
|
| 95 |
normalize_porosity,
|
| 96 |
compute_sss,
|
|
@@ -166,6 +171,11 @@ def inference(
|
|
| 166 |
roughness=roughness,
|
| 167 |
metalness=metalness,
|
| 168 |
derive_ao_height=True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 169 |
seamless=seamless,
|
| 170 |
ao_strength=ao_strength,
|
| 171 |
ao_blur=int(ao_blur),
|
|
@@ -221,6 +231,18 @@ Upload an image to estimate PBR materials and export for Minecraft shaders.
|
|
| 221 |
label="AO Strength", info="AO contrast multiplier")
|
| 222 |
ao_blur = gr.Slider(minimum=0, maximum=15, value=5, step=1,
|
| 223 |
label="AO Blur", info="Gaussian blur radius for AO smoothing")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 224 |
|
| 225 |
gr.Markdown("#### Porosity (LabPBR Blue 0-64)")
|
| 226 |
compute_porosity = gr.Checkbox(label="Compute Porosity", value=False,
|
|
@@ -296,6 +318,11 @@ Upload an image to estimate PBR materials and export for Minecraft shaders.
|
|
| 296 |
seamless,
|
| 297 |
ao_strength,
|
| 298 |
ao_blur,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 299 |
compute_porosity,
|
| 300 |
normalize_porosity,
|
| 301 |
compute_sss,
|
|
|
|
| 91 |
seamless,
|
| 92 |
ao_strength,
|
| 93 |
ao_blur,
|
| 94 |
+
include_height,
|
| 95 |
+
height_low_freq,
|
| 96 |
+
height_mid_freq,
|
| 97 |
+
height_high_freq,
|
| 98 |
+
height_intensity,
|
| 99 |
compute_porosity,
|
| 100 |
normalize_porosity,
|
| 101 |
compute_sss,
|
|
|
|
| 171 |
roughness=roughness,
|
| 172 |
metalness=metalness,
|
| 173 |
derive_ao_height=True,
|
| 174 |
+
include_height=include_height,
|
| 175 |
+
height_low_freq=height_low_freq,
|
| 176 |
+
height_mid_freq=height_mid_freq,
|
| 177 |
+
height_high_freq=height_high_freq,
|
| 178 |
+
height_intensity=height_intensity,
|
| 179 |
seamless=seamless,
|
| 180 |
ao_strength=ao_strength,
|
| 181 |
ao_blur=int(ao_blur),
|
|
|
|
| 231 |
label="AO Strength", info="AO contrast multiplier")
|
| 232 |
ao_blur = gr.Slider(minimum=0, maximum=15, value=5, step=1,
|
| 233 |
label="AO Blur", info="Gaussian blur radius for AO smoothing")
|
| 234 |
+
include_height = gr.Checkbox(label="Include Height Map (for POM)", value=True,
|
| 235 |
+
info="Derive height from normal map for parallax occlusion mapping. Disable for flat surfaces.")
|
| 236 |
+
|
| 237 |
+
gr.Markdown("#### Height Relief Balance (LabPBR only)")
|
| 238 |
+
height_low_freq = gr.Slider(minimum=0.0, maximum=2.0, value=1.0, step=0.01,
|
| 239 |
+
label="Low Frequencies", info="Large-scale height variations")
|
| 240 |
+
height_mid_freq = gr.Slider(minimum=0.0, maximum=2.0, value=1.0, step=0.01,
|
| 241 |
+
label="Mid Frequencies", info="Medium surface features")
|
| 242 |
+
height_high_freq = gr.Slider(minimum=0.0, maximum=2.0, value=1.0, step=0.01,
|
| 243 |
+
label="High Frequencies", info="Fine surface detail")
|
| 244 |
+
height_intensity = gr.Slider(minimum=0.0, maximum=1.0, value=1.0, step=0.01,
|
| 245 |
+
label="Global Intensity", info="Overall height map strength (0 = flat)")
|
| 246 |
|
| 247 |
gr.Markdown("#### Porosity (LabPBR Blue 0-64)")
|
| 248 |
compute_porosity = gr.Checkbox(label="Compute Porosity", value=False,
|
|
|
|
| 318 |
seamless,
|
| 319 |
ao_strength,
|
| 320 |
ao_blur,
|
| 321 |
+
include_height,
|
| 322 |
+
height_low_freq,
|
| 323 |
+
height_mid_freq,
|
| 324 |
+
height_high_freq,
|
| 325 |
+
height_intensity,
|
| 326 |
compute_porosity,
|
| 327 |
normalize_porosity,
|
| 328 |
compute_sss,
|
chord/minecraft_pbr.py
CHANGED
|
@@ -598,8 +598,10 @@ def create_normal_texture(
|
|
| 598 |
|
| 599 |
# A: Height map
|
| 600 |
# When not provided, caller should derive from normal map via derive_ao_and_height()
|
|
|
|
|
|
|
| 601 |
if height is None:
|
| 602 |
-
height = torch.full((b, 1, h, w),
|
| 603 |
|
| 604 |
normal_tex = torch.cat([normal_x, normal_y, ao, height], dim=1)
|
| 605 |
|
|
@@ -646,6 +648,11 @@ def convert_to_labpbr(
|
|
| 646 |
flip_normal_y: bool = True,
|
| 647 |
swap_normal_xy: bool = False,
|
| 648 |
derive_ao_height: bool = True,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 649 |
compute_porosity: bool = False,
|
| 650 |
normalize_porosity: bool = True,
|
| 651 |
compute_sss: bool = False,
|
|
@@ -678,6 +685,12 @@ def convert_to_labpbr(
|
|
| 678 |
flip_normal_y: Flip Y channel for DirectX convention (default True)
|
| 679 |
swap_normal_xy: Swap X/Y channels if model outputs [Y,X,Z] (default False)
|
| 680 |
derive_ao_height: If True, derive AO and height from normal map when not provided
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 681 |
compute_porosity: If True, calculate porosity from AO, smoothness, and F0 when not provided
|
| 682 |
normalize_porosity: If True, normalize porosity to full 0-1 range before LabPBR scaling
|
| 683 |
compute_sss: If True, calculate SSS thickness from normal curvature and AO when not provided
|
|
@@ -720,17 +733,24 @@ def convert_to_labpbr(
|
|
| 720 |
metalness = metalness.unsqueeze(0)
|
| 721 |
|
| 722 |
# Derive AO and height from normal map if not provided
|
|
|
|
| 723 |
if derive_ao_height and (ao is None or height is None):
|
| 724 |
derived_ao, derived_height = derive_ao_and_height(
|
| 725 |
normal,
|
| 726 |
seamless=seamless,
|
| 727 |
ao_strength=ao_strength,
|
| 728 |
ao_blur=ao_blur,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 729 |
)
|
| 730 |
if ao is None:
|
| 731 |
ao = derived_ao
|
| 732 |
-
if height is None:
|
| 733 |
height = derived_height
|
|
|
|
| 734 |
|
| 735 |
# Calculate porosity from AO, smoothness, and F0 if requested
|
| 736 |
# Note: Both porosity and SSS can coexist - SSS takes precedence per-pixel
|
|
|
|
| 598 |
|
| 599 |
# A: Height map
|
| 600 |
# When not provided, caller should derive from normal map via derive_ao_and_height()
|
| 601 |
+
# Default to 1.0 (surface level) for no POM displacement
|
| 602 |
+
# In Minecraft POM: 1.0 = surface, 0.25 = 25% block depth
|
| 603 |
if height is None:
|
| 604 |
+
height = torch.full((b, 1, h, w), 1.0, device=device) # Surface level (no depth)
|
| 605 |
|
| 606 |
normal_tex = torch.cat([normal_x, normal_y, ao, height], dim=1)
|
| 607 |
|
|
|
|
| 648 |
flip_normal_y: bool = True,
|
| 649 |
swap_normal_xy: bool = False,
|
| 650 |
derive_ao_height: bool = True,
|
| 651 |
+
include_height: bool = True,
|
| 652 |
+
height_low_freq: float = 1.0,
|
| 653 |
+
height_mid_freq: float = 1.0,
|
| 654 |
+
height_high_freq: float = 1.0,
|
| 655 |
+
height_intensity: float = 1.0,
|
| 656 |
compute_porosity: bool = False,
|
| 657 |
normalize_porosity: bool = True,
|
| 658 |
compute_sss: bool = False,
|
|
|
|
| 685 |
flip_normal_y: Flip Y channel for DirectX convention (default True)
|
| 686 |
swap_normal_xy: Swap X/Y channels if model outputs [Y,X,Z] (default False)
|
| 687 |
derive_ao_height: If True, derive AO and height from normal map when not provided
|
| 688 |
+
include_height: If True, include derived height map in normal alpha channel (for POM).
|
| 689 |
+
If False, uses neutral height (0.5). Only affects output when derive_ao_height=True.
|
| 690 |
+
height_low_freq: Weight for low frequency height details (large-scale, default 1.0)
|
| 691 |
+
height_mid_freq: Weight for mid frequency height details (medium features, default 1.0)
|
| 692 |
+
height_high_freq: Weight for high frequency height details (fine detail, default 1.0)
|
| 693 |
+
height_intensity: Global height intensity/opacity (0.0 = flat, 1.0 = full height)
|
| 694 |
compute_porosity: If True, calculate porosity from AO, smoothness, and F0 when not provided
|
| 695 |
normalize_porosity: If True, normalize porosity to full 0-1 range before LabPBR scaling
|
| 696 |
compute_sss: If True, calculate SSS thickness from normal curvature and AO when not provided
|
|
|
|
| 733 |
metalness = metalness.unsqueeze(0)
|
| 734 |
|
| 735 |
# Derive AO and height from normal map if not provided
|
| 736 |
+
# Height is scaled to [0.25, 1.0] for Minecraft POM (black = 25% block depth)
|
| 737 |
if derive_ao_height and (ao is None or height is None):
|
| 738 |
derived_ao, derived_height = derive_ao_and_height(
|
| 739 |
normal,
|
| 740 |
seamless=seamless,
|
| 741 |
ao_strength=ao_strength,
|
| 742 |
ao_blur=ao_blur,
|
| 743 |
+
height_low_freq=height_low_freq,
|
| 744 |
+
height_mid_freq=height_mid_freq,
|
| 745 |
+
height_high_freq=height_high_freq,
|
| 746 |
+
height_intensity=height_intensity,
|
| 747 |
+
height_min=0.25, # Minecraft POM: black = 25% block depth
|
| 748 |
)
|
| 749 |
if ao is None:
|
| 750 |
ao = derived_ao
|
| 751 |
+
if height is None and include_height:
|
| 752 |
height = derived_height
|
| 753 |
+
# If include_height is False, height stays None and create_normal_texture uses neutral 0.5
|
| 754 |
|
| 755 |
# Calculate porosity from AO, smoothness, and F0 if requested
|
| 756 |
# Note: Both porosity and SSS can coexist - SSS takes precedence per-pixel
|
chord/normal_utils.py
CHANGED
|
@@ -39,7 +39,13 @@ def normal_to_gradient(normal: torch.Tensor) -> tuple[torch.Tensor, torch.Tensor
|
|
| 39 |
return nx, ny
|
| 40 |
|
| 41 |
|
| 42 |
-
def frankot_chellappa(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
"""
|
| 44 |
Reconstruct height map from gradients using Frankot-Chellappa algorithm.
|
| 45 |
|
|
@@ -49,6 +55,9 @@ def frankot_chellappa(grad_x: torch.Tensor, grad_y: torch.Tensor) -> torch.Tenso
|
|
| 49 |
Args:
|
| 50 |
grad_x: X gradient (B, 1, H, W)
|
| 51 |
grad_y: Y gradient (B, 1, H, W)
|
|
|
|
|
|
|
|
|
|
| 52 |
|
| 53 |
Returns:
|
| 54 |
Height map (B, 1, H, W), normalized to [0, 1]
|
|
@@ -65,6 +74,25 @@ def frankot_chellappa(grad_x: torch.Tensor, grad_y: torch.Tensor) -> torch.Tenso
|
|
| 65 |
u_grid = torch.fft.ifftshift(u_grid)
|
| 66 |
v_grid = torch.fft.ifftshift(v_grid)
|
| 67 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 68 |
# FFT of gradients
|
| 69 |
grad_x_F = torch.fft.fft2(grad_x.squeeze(1))
|
| 70 |
grad_y_F = torch.fft.fft2(grad_y.squeeze(1))
|
|
@@ -76,6 +104,9 @@ def frankot_chellappa(grad_x: torch.Tensor, grad_y: torch.Tensor) -> torch.Tenso
|
|
| 76 |
Z_F = numerator / denominator
|
| 77 |
Z_F[:, 0, 0] = 0.0 # Set DC component to zero
|
| 78 |
|
|
|
|
|
|
|
|
|
|
| 79 |
# Inverse FFT to get height
|
| 80 |
Z = torch.real(torch.fft.ifft2(Z_F)).unsqueeze(1)
|
| 81 |
|
|
@@ -124,6 +155,11 @@ def make_seamless_gradients(grad_x: torch.Tensor, grad_y: torch.Tensor) -> tuple
|
|
| 124 |
def normal_to_height(
|
| 125 |
normal: torch.Tensor,
|
| 126 |
seamless: bool = False,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 127 |
) -> torch.Tensor:
|
| 128 |
"""
|
| 129 |
Convert normal map to height map using Frankot-Chellappa algorithm.
|
|
@@ -131,9 +167,15 @@ def normal_to_height(
|
|
| 131 |
Args:
|
| 132 |
normal: Normal map (B, 3, H, W) or (3, H, W), range [0, 1]
|
| 133 |
seamless: If True, input is assumed tileable and output will tile seamlessly
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
|
| 135 |
Returns:
|
| 136 |
-
Height map (B, 1, H, W) or (1, H, W), range [
|
| 137 |
"""
|
| 138 |
squeeze = normal.dim() == 3
|
| 139 |
if squeeze:
|
|
@@ -148,13 +190,23 @@ def normal_to_height(
|
|
| 148 |
if not seamless:
|
| 149 |
grad_x, grad_y = make_seamless_gradients(grad_x, grad_y)
|
| 150 |
|
| 151 |
-
# Reconstruct height via FFT integration
|
| 152 |
-
height = frankot_chellappa(-grad_x, grad_y)
|
| 153 |
|
| 154 |
# Crop back to original size if we expanded
|
| 155 |
if not seamless:
|
| 156 |
height = height[:, :, :h, :w]
|
| 157 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 158 |
if squeeze:
|
| 159 |
height = height.squeeze(0)
|
| 160 |
|
|
@@ -269,6 +321,11 @@ def derive_ao_and_height(
|
|
| 269 |
seamless: bool = False,
|
| 270 |
ao_strength: float = 2.0,
|
| 271 |
ao_blur: int = 5,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 273 |
"""
|
| 274 |
Derive both AO and height from a normal map.
|
|
@@ -278,12 +335,25 @@ def derive_ao_and_height(
|
|
| 278 |
seamless: Whether the texture should tile seamlessly
|
| 279 |
ao_strength: AO intensity multiplier
|
| 280 |
ao_blur: Gaussian blur radius for AO smoothing
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 281 |
|
| 282 |
Returns:
|
| 283 |
ao: Ambient occlusion (1.0 = no occlusion)
|
| 284 |
-
height: Height map (
|
| 285 |
"""
|
| 286 |
ao = normal_to_ao(normal, strength=ao_strength, blur_radius=ao_blur)
|
| 287 |
-
height = normal_to_height(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 288 |
|
| 289 |
return ao, height
|
|
|
|
| 39 |
return nx, ny
|
| 40 |
|
| 41 |
|
| 42 |
+
def frankot_chellappa(
|
| 43 |
+
grad_x: torch.Tensor,
|
| 44 |
+
grad_y: torch.Tensor,
|
| 45 |
+
low_freq: float = 1.0,
|
| 46 |
+
mid_freq: float = 1.0,
|
| 47 |
+
high_freq: float = 1.0,
|
| 48 |
+
) -> torch.Tensor:
|
| 49 |
"""
|
| 50 |
Reconstruct height map from gradients using Frankot-Chellappa algorithm.
|
| 51 |
|
|
|
|
| 55 |
Args:
|
| 56 |
grad_x: X gradient (B, 1, H, W)
|
| 57 |
grad_y: Y gradient (B, 1, H, W)
|
| 58 |
+
low_freq: Weight for low frequency details (large-scale height variations)
|
| 59 |
+
mid_freq: Weight for mid frequency details (medium features)
|
| 60 |
+
high_freq: Weight for high frequency details (fine surface detail)
|
| 61 |
|
| 62 |
Returns:
|
| 63 |
Height map (B, 1, H, W), normalized to [0, 1]
|
|
|
|
| 74 |
u_grid = torch.fft.ifftshift(u_grid)
|
| 75 |
v_grid = torch.fft.ifftshift(v_grid)
|
| 76 |
|
| 77 |
+
# Compute radial frequency (normalized 0-1, where 1 is Nyquist)
|
| 78 |
+
freq_radius = torch.sqrt(u_grid ** 2 + v_grid ** 2)
|
| 79 |
+
freq_radius = freq_radius / (freq_radius.max() + 1e-8)
|
| 80 |
+
|
| 81 |
+
# Create frequency band weights using smooth transitions
|
| 82 |
+
# Low: 0-0.2, Mid: 0.2-0.5, High: 0.5-1.0
|
| 83 |
+
# Using smooth hermite interpolation for band transitions
|
| 84 |
+
low_mask = 1.0 - torch.clamp((freq_radius - 0.1) / 0.15, 0.0, 1.0)
|
| 85 |
+
high_mask = torch.clamp((freq_radius - 0.35) / 0.25, 0.0, 1.0)
|
| 86 |
+
mid_mask = 1.0 - low_mask - high_mask
|
| 87 |
+
mid_mask = torch.clamp(mid_mask, 0.0, 1.0)
|
| 88 |
+
|
| 89 |
+
# Apply smoothstep for smoother transitions
|
| 90 |
+
low_mask = low_mask * low_mask * (3.0 - 2.0 * low_mask)
|
| 91 |
+
high_mask = high_mask * high_mask * (3.0 - 2.0 * high_mask)
|
| 92 |
+
|
| 93 |
+
# Combine frequency weights
|
| 94 |
+
freq_weight = low_mask * low_freq + mid_mask * mid_freq + high_mask * high_freq
|
| 95 |
+
|
| 96 |
# FFT of gradients
|
| 97 |
grad_x_F = torch.fft.fft2(grad_x.squeeze(1))
|
| 98 |
grad_y_F = torch.fft.fft2(grad_y.squeeze(1))
|
|
|
|
| 104 |
Z_F = numerator / denominator
|
| 105 |
Z_F[:, 0, 0] = 0.0 # Set DC component to zero
|
| 106 |
|
| 107 |
+
# Apply frequency band weighting
|
| 108 |
+
Z_F = Z_F * freq_weight
|
| 109 |
+
|
| 110 |
# Inverse FFT to get height
|
| 111 |
Z = torch.real(torch.fft.ifft2(Z_F)).unsqueeze(1)
|
| 112 |
|
|
|
|
| 155 |
def normal_to_height(
|
| 156 |
normal: torch.Tensor,
|
| 157 |
seamless: bool = False,
|
| 158 |
+
low_freq: float = 1.0,
|
| 159 |
+
mid_freq: float = 1.0,
|
| 160 |
+
high_freq: float = 1.0,
|
| 161 |
+
intensity: float = 1.0,
|
| 162 |
+
min_height: float = 0.25,
|
| 163 |
) -> torch.Tensor:
|
| 164 |
"""
|
| 165 |
Convert normal map to height map using Frankot-Chellappa algorithm.
|
|
|
|
| 167 |
Args:
|
| 168 |
normal: Normal map (B, 3, H, W) or (3, H, W), range [0, 1]
|
| 169 |
seamless: If True, input is assumed tileable and output will tile seamlessly
|
| 170 |
+
low_freq: Weight for low frequency details (large-scale height, default 1.0)
|
| 171 |
+
mid_freq: Weight for mid frequency details (medium features, default 1.0)
|
| 172 |
+
high_freq: Weight for high frequency details (fine detail, default 1.0)
|
| 173 |
+
intensity: Global height intensity/opacity (0.0 = flat, 1.0 = full height)
|
| 174 |
+
min_height: Minimum height value (default 0.25 for Minecraft POM where
|
| 175 |
+
black=25% block depth, white=surface). Set to 0.0 for full 0-1 range.
|
| 176 |
|
| 177 |
Returns:
|
| 178 |
+
Height map (B, 1, H, W) or (1, H, W), range [min_height, 1.0]
|
| 179 |
"""
|
| 180 |
squeeze = normal.dim() == 3
|
| 181 |
if squeeze:
|
|
|
|
| 190 |
if not seamless:
|
| 191 |
grad_x, grad_y = make_seamless_gradients(grad_x, grad_y)
|
| 192 |
|
| 193 |
+
# Reconstruct height via FFT integration with frequency weighting
|
| 194 |
+
height = frankot_chellappa(-grad_x, grad_y, low_freq, mid_freq, high_freq)
|
| 195 |
|
| 196 |
# Crop back to original size if we expanded
|
| 197 |
if not seamless:
|
| 198 |
height = height[:, :, :h, :w]
|
| 199 |
|
| 200 |
+
# Scale from [0, 1] to [min_height, 1.0] for Minecraft POM
|
| 201 |
+
# In Minecraft POM: black (min_height) = deepest point, white (1.0) = surface
|
| 202 |
+
height_range = 1.0 - min_height
|
| 203 |
+
height = min_height + height * height_range
|
| 204 |
+
|
| 205 |
+
# Apply global intensity (blend toward midpoint of output range)
|
| 206 |
+
if intensity < 1.0:
|
| 207 |
+
midpoint = (min_height + 1.0) / 2.0
|
| 208 |
+
height = midpoint + (height - midpoint) * intensity
|
| 209 |
+
|
| 210 |
if squeeze:
|
| 211 |
height = height.squeeze(0)
|
| 212 |
|
|
|
|
| 321 |
seamless: bool = False,
|
| 322 |
ao_strength: float = 2.0,
|
| 323 |
ao_blur: int = 5,
|
| 324 |
+
height_low_freq: float = 1.0,
|
| 325 |
+
height_mid_freq: float = 1.0,
|
| 326 |
+
height_high_freq: float = 1.0,
|
| 327 |
+
height_intensity: float = 1.0,
|
| 328 |
+
height_min: float = 0.25,
|
| 329 |
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 330 |
"""
|
| 331 |
Derive both AO and height from a normal map.
|
|
|
|
| 335 |
seamless: Whether the texture should tile seamlessly
|
| 336 |
ao_strength: AO intensity multiplier
|
| 337 |
ao_blur: Gaussian blur radius for AO smoothing
|
| 338 |
+
height_low_freq: Weight for low frequency height details (default 1.0)
|
| 339 |
+
height_mid_freq: Weight for mid frequency height details (default 1.0)
|
| 340 |
+
height_high_freq: Weight for high frequency height details (default 1.0)
|
| 341 |
+
height_intensity: Global height intensity/opacity (0.0 = flat, 1.0 = full)
|
| 342 |
+
height_min: Minimum height value (default 0.25 for Minecraft POM)
|
| 343 |
|
| 344 |
Returns:
|
| 345 |
ao: Ambient occlusion (1.0 = no occlusion)
|
| 346 |
+
height: Height map (height_min = deepest, 1.0 = surface)
|
| 347 |
"""
|
| 348 |
ao = normal_to_ao(normal, strength=ao_strength, blur_radius=ao_blur)
|
| 349 |
+
height = normal_to_height(
|
| 350 |
+
normal,
|
| 351 |
+
seamless=seamless,
|
| 352 |
+
low_freq=height_low_freq,
|
| 353 |
+
mid_freq=height_mid_freq,
|
| 354 |
+
high_freq=height_high_freq,
|
| 355 |
+
intensity=height_intensity,
|
| 356 |
+
min_height=height_min,
|
| 357 |
+
)
|
| 358 |
|
| 359 |
return ao, height
|