Equirectangular to cubemap bash script

I was working on another try to use blend4web for a personal application, and I remembered that if you want to use an environment map it has to be a Blender specific 2 rows cubemap ( 2*3 cubes faces).

blender cubemap
Blender cubemap

I’ve found ways to make it with gimp, after using Bruno Postle’s panotools script ( erect2cubic Erect2cubic ), or directly inside Blender (link 1: Blendswap cubemap maker , from : cubemap making inside blender).




But all those ways needed to treat each panos one after the other; too long when you have to transform 20 panoramas…
So I wrote a little bash script to transform equirectangular panoramas initially to those specific cubemaps, then I extended it to match further types of cubemaps ( it seems that unity uses cross styled cubemaps ).

#!/bin/sh
# 2016 11 03 sh script
# This script aim to transform an equirectangular pano to a cubemap image used for environment mapping
# it uses : Hugin, Bruno Postle's panotools scripts (perl) ,imagemagick's "convert" and montage command line tools and zenity for gui.
# Find it useful ? Any bugs, ideas?, mail me at : mail@fabkzo.com
# Author: FabKzo www.fabkzo.com

pano=0
order=0
front=cube0000.tif
right=cube0001.tif
back=cube0002.tif
left=cube0003.tif
up=cube0004.tif
down=cube0005.tif

createPano (){
                    #create pto file
                    erect2cubic --erect=$pano --ptofile=cube.pto;
    
                    #create each cube faces
                    nona -o cube cube.pto;
                    imgsize=$(identify -format %h cube0000.tif);
                    echo $imgsize;
                    }
blendercubeMap (){
                    convert $left $back $right +append cubeUp.tif;
                    convert $down $up $front +append cubeDown.tif;
                    convert cubeUp.tif cubeDown.tif -append toTo.tif;
}

horizontalcross (){
                    montage null: $up null: null: $left $front $right $back null: $down null: null: -geometry $imgsize -background none -tile 4x3 toTo.tif;
}

verticalcross (){
                    convert -rotate 180 $back $back 
                    montage null: $up null: $left $front $right null: $down null: null: $back null: -geometry $imgsize -background none -tile 3x4 toTo.tif;
}

horizontalline (){
                    convert $right $left $up $down $front $back +append toTo.tif;
}

verticalline (){
                    convert $right $left $up $down $front $back -append toTo.tif;
}
                    
finalsize=` zenity --entry --title="Enter Cubemap final height in px" --text="height" --entry-text="4096" `
final=` zenity --list --radiolist --title="3D software cubemap type" --column="Choice" --column="Cubemap"\
                        TRUE "Blender" \
                        FALSE "horizontal-cross" \
                        FALSE "vertical-cross" \
                        FALSE "horizontal-line"\
                        FALSE "vertical-line"`

                case $final in
                        "Blender")
                        order="blendercubeMap";;
                        "horizontal-cross")
                        order="horizontalcross";;
                        "vertical-cross")
                        order="verticalcross";;
                        "horizontal-line")
                        order="horizontalline";;
                        "vertical-line")
                        order="verticalline";;
                        
                        *)
                    esac

while [ $# -gt 0 ]; do
    pano=$1
    echo $pano
    echo "createPano"
    
    createPano
    
    
    #takes each face to create the final image
    $order
    
    convert -resize x$finalsize toTo.tif ${pano%.*}-cubemap.png
    
    rm cubeUp.tif cubeDown.tif toTo.tif cube????.tif cube.pto
    
    shift
    done
    zenity --info --text="DONE"

 

Pass it your panos, then it should result in a lot of .PNG cubemaps ready to use to create game assets inside blend4web or blender game engine ( first option ):

examples :

Equirectangular pano example
Equirectangular pano example
Blender cubemap
Cubemap horizontal cross Blender cubemap
cubemap vertical cross
cubemap vertical cross

 

 

cubemap horizontal line
cubemap horizontal line





Feel free to use those examples and tell me back if I’ve done an error on the other outputs or if you encounter problems to use it . Linux only . Enjoy it , share it .

Laisser un commentaire

Votre adresse de messagerie ne sera pas publiée. Les champs obligatoires sont indiqués avec *

AntiBot * Time limit is exhausted. Please reload CAPTCHA.