9
0
mirror of https://github.com/Kas-tle/java2bedrock.sh.git synced 2025-12-19 14:59:13 +00:00

Parallelize atlas gen and parent search

This commit is contained in:
Kas-tle
2022-10-22 21:11:32 +00:00
committed by GitHub
parent 9e4e9600ac
commit 02f8db6ed5
2 changed files with 138 additions and 70 deletions

5
.gitignore vendored
View File

@@ -14,4 +14,7 @@ item_mappings.json
item_texture.json item_texture.json
spritesheet/ spritesheet/
sprites.json sprites.json
sprite_hashmap.json sprite_hashmap.json
nongenerated.json
item_textures.json
atlases.json

View File

@@ -457,73 +457,123 @@ printf "\r\e[37m█\e[m \e[37m${_fill// /█}\e[m\e[37m${_empty// /•}\e[m \e[3
# first, deal with parented models # first, deal with parented models
while IFS=, read -r file gid parental namespace model_path model_name path_hash while IFS=, read -r file gid parental namespace model_path model_name path_hash
do do
cur_pos=$((cur_pos+1)) resolve_parental () {
elements="$(jq -rc '.elements' ${file} | tee elements.temp)" local file=${1}
element_parent=${file} local gid=${2}
textures="$(jq -rc '.textures' ${file} | tee textures.temp)" local parental=${3}
display="$(jq -rc '.display' ${file} | tee display.temp)" local namespace=${4}
status_message process "Locating parental info for child model with GeyserID ${gid}" local model_path=${5}
local model_name=${6}
local path_hash=${7}
# itterate through parented models until they all have geometry, display, and textures local elements="$(jq -rc '.elements' ${file} | tee ${gid}.elements.temp)"
until [[ ${elements} != null && ${textures} != null && ${display} != null ]] || [[ ${parental} = "./assets/minecraft/models/builtin/generated.json" ]] || [[ ${parental} = null ]] local element_parent=${file}
do local textures="$(jq -rc '.textures' ${file} | tee ${gid}.textures.temp)"
if [[ ${elements} = null ]] local display="$(jq -rc '.display' ${file} | tee ${gid}.display.temp)"
then status_message process "Locating parental info for child model with GeyserID ${gid}"
elements="$(jq -rc '.elements' ${parental} 2> /dev/null | tee elements.temp || (echo && echo null))"
element_parent=${parental}
fi
if [[ ${textures} = null ]]
then
textures="$(jq -rc '.textures' ${parental} 2> /dev/null | tee textures.temp || (echo && echo null))"
fi
if [[ ${display} = null ]]
then
display="$(jq -rc '.display' ${parental} 2> /dev/null | tee display.temp || (echo && echo null))"
fi
parental="$(jq -rc 'def namespace: if contains(":") then sub("\\:(.+)"; "") else "minecraft" end; ("./assets/" + (.parent? | namespace) + "/models/" + ((.parent? // empty) | sub("(.*?)\\:"; "")) + ".json") // "null"' ${parental} 2> /dev/null || (echo && echo null))"
texture_0="$(jq -rc 'def namespace: if contains(":") then sub("\\:(.+)"; "") else "minecraft" end; ("./assets/" + ([.[]][0]? | namespace) + "/textures/" + (([.[]][0]? // empty) | sub("(.*?)\\:"; "")) + ".png") // "null"' textures.temp)"
done
# if we can, generate a model now # itterate through parented models until they all have geometry, display, and textures
if [[ ${elements} != null && ${textures} != null ]] until [[ ${elements} != null && ${textures} != null && ${display} != null ]] || [[ ${parental} = "./assets/minecraft/models/builtin/generated.json" ]] || [[ ${parental} = null ]]
then do
jq -n --slurpfile jelements elements.temp --slurpfile jtextures textures.temp --slurpfile jdisplay display.temp ' if [[ ${elements} = null ]]
{ then
"textures": ($jtextures[]), local elements="$(jq -rc '.elements' ${parental} 2> /dev/null | tee ${gid}.elements.temp || (echo && echo null))"
"elements": ($jelements[]) local element_parent=${parental}
} + (if $jdisplay then ({"display": ($jdisplay[])}) else {} end) fi
' | sponge ${file} if [[ ${textures} = null ]]
jq --arg gid "${gid}" '.[$gid].generated |= "false"' config.json | sponge config.json then
status_message completion "Located all parental info for Child ${gid}" local textures="$(jq -rc '.textures' ${parental} 2> /dev/null | tee ${gid}.textures.temp || (echo && echo null))"
ProgressBar ${cur_pos} ${_end} fi
echo if [[ ${display} = null ]]
# check if this is a 2d item dervived from ./assets/minecraft/models/builtin/generated then
elif [[ ${textures} != null && ${parental} = "./assets/minecraft/models/builtin/generated.json" && -f "${texture_0}" ]] local display="$(jq -rc '.display' ${parental} 2> /dev/null | tee ${gid}.display.temp || (echo && echo null))"
then fi
jq -n --slurpfile jelements elements.temp --slurpfile jtextures textures.temp --slurpfile jdisplay display.temp ' local parental="$(jq -rc 'def namespace: if contains(":") then sub("\\:(.+)"; "") else "minecraft" end; ("./assets/" + (.parent? | namespace) + "/models/" + ((.parent? // empty) | sub("(.*?)\\:"; "")) + ".json") // "null"' ${parental} 2> /dev/null || (echo && echo null))"
{ local texture_0="$(jq -rc 'def namespace: if contains(":") then sub("\\:(.+)"; "") else "minecraft" end; ("./assets/" + ([.[]][0]? | namespace) + "/textures/" + (([.[]][0]? // empty) | sub("(.*?)\\:"; "")) + ".png") // "null"' ${gid}.textures.temp)"
"textures": ([$jtextures[]][0]) done
} + (if $jdisplay then ({"display": ($jdisplay[])}) else {} end)
' | sponge ${file} # if we can, generate a model now
jq --arg gid "${gid}" '.[$gid].generated |= "true"' config.json | sponge config.json if [[ ${elements} != null && ${textures} != null ]]
# copy texture directly to the rp then
mkdir -p "./target/rp/textures/geyser/geyser_custom/${namespace}/${model_path}" jq -n --slurpfile jelements ${gid}.elements.temp --slurpfile jtextures ${gid}.textures.temp --slurpfile jdisplay ${gid}.display.temp '
cp "${texture_0}" "./target/rp/textures/geyser/geyser_custom/${namespace}/${model_path}/${model_name}.png" {
# add texture to item atlas "textures": ($jtextures[]),
jq --arg path_hash "${path_hash}" --arg namespace "${namespace}" --arg model_path "${model_path}" --arg model_name "${model_name}" '.texture_data += {($path_hash): {"textures": ("textures/geyser/geyser_custom/" + $namespace + "/" + $model_path + "/" + $model_name)}}' ./target/rp/textures/item_texture.json | sponge ./target/rp/textures/item_texture.json "elements": ($jelements[])
status_message completion "Located all parental info for 2D Child ${gid}" } + (if $jdisplay then ({"display": ($jdisplay[])}) else {} end)
ProgressBar ${cur_pos} ${_end} ' | sponge ${file}
echo #jq --arg gid "${gid}" '.[$gid].generated |= "false"' config.json | sponge config.json
# otherwise, remove it from our config echo >> count.csv
else local tot_pos=$(wc -l < count.csv)
status_message plain "Suitable parent information was not availbile for ${gid}..." status_message completion "Located all parental info for Child ${gid}\n$(ProgressBar ${tot_pos} ${_end})"
status_message critical "Deleting ${gid} from config" echo
ProgressBar ${cur_pos} ${_end} # check if this is a 2d item dervived from ./assets/minecraft/models/builtin/generated
echo elif [[ ${textures} != null && ${parental} = "./assets/minecraft/models/builtin/generated.json" && -f "${texture_0}" ]]
jq --arg gid "${gid}" 'del(.[$gid])' config.json | sponge config.json then
fi jq -n --slurpfile jelements ${gid}.elements.temp --slurpfile jtextures ${gid}.textures.temp --slurpfile jdisplay ${gid}.display.temp '
{
"textures": ([$jtextures[]][0])
} + (if $jdisplay then ({"display": ($jdisplay[])}) else {} end)
' | sponge ${file}
#jq --arg gid "${gid}" '.[$gid].generated |= "true"' config.json | sponge config.json
# copy texture directly to the rp
mkdir -p "./target/rp/textures/geyser/geyser_custom/${namespace}/${model_path}"
cp "${texture_0}" "./target/rp/textures/geyser/geyser_custom/${namespace}/${model_path}/${model_name}.png"
# add texture to item atlas
#jq --arg path_hash "${path_hash}" --arg namespace "${namespace}" --arg model_path "${model_path}" --arg model_name "${model_name}" '.texture_data += {($path_hash): {"textures": ("textures/geyser/geyser_custom/" + $namespace + "/" + $model_path + "/" + $model_name)}}' ./target/rp/textures/item_texture.json | sponge ./target/rp/textures/item_texture.json
echo "${path_hash},textures/geyser/geyser_custom/${namespace}/${model_path}/${model_name}" >> icons.csv
echo "${gid}" >> generated.csv
echo >> count.csv
local tot_pos=$(wc -l < count.csv)
status_message completion "Located all parental info for 2D Child ${gid}\n$(ProgressBar ${tot_pos} ${_end})"
echo
# otherwise, remove it from our config
else
echo "${gid}" >> deleted.csv
echo >> count.csv
local tot_pos=$(wc -l < count.csv)
status_message critical "Deleting ${gid} from config as no suitable parent information was found\n$(ProgressBar ${tot_pos} ${_end})"
echo
#jq --arg gid "${gid}" 'del(.[$gid])' config.json | sponge config.json
fi
rm -f ${gid}.elements.temp ${gid}.textures.temp ${gid}.display.temp
}
wait_for_jobs
resolve_parental "${file}" "${gid}" "${parental}" "${namespace}" "${model_path}" "${model_name}" "${path_hash}" &
done < pa.csv done < pa.csv
wait # wait for all the jobs to finish
# update generated models in config
if [[ -f generated.csv ]]
then
jq -cR 'split(",")' generated.csv | jq -s 'map({(.[0]): "true"}) | add' > generated.json
jq -s '
.[0] as $generated_models
| .[1]
| map_values(
.geyserID as $gid
| .generated = ($generated_models[($gid)] // "false")
)
' generated.json config.json | sponge config.json
fi
# add icon textures to item atlas
if [[ -f icons.csv ]]
then
jq -cR 'split(",")' icons.csv | jq -s 'map({(.[0]): {"textures": .[1]}}) | add' > icons.json
jq -s '
.[0] as $icons
| .[1]
| .texture_data += $icons
' icons.json ./target/rp/textures/item_texture.json | sponge ./target/rp/textures/item_texture.json
fi
# delete unsuitable models
if [[ -f deleted.csv ]]
then
jq -cR 'split(",")' deleted.csv | jq -s '.' > deleted.json
jq -s '.[0] as $deleted | .[1] | delpaths($deleted)' deleted.json config.json | sponge config.json
fi
status_message process "Compiling final model list" status_message process "Compiling final model list"
# get our final 3d model list from the config # get our final 3d model list from the config
@@ -558,12 +608,27 @@ mkdir spritesheet
status_message process "Generating $((1+${total_union_atlas})) sprite sheets..." status_message process "Generating $((1+${total_union_atlas})) sprite sheets..."
for i in $(seq 0 ${total_union_atlas}) for i in $(seq 0 ${total_union_atlas})
do do
# find the union of all texture files listed in this atlas and all texture files in our resource pack generate_atlas () {
texture_list=( $(jq -s --arg index "${i}" -r '(.[1][($index | tonumber)] - .[0] | length > 0) as $fallback_needed | ((.[1][($index | tonumber)] - (.[1][($index | tonumber)] - .[0])) + (if $fallback_needed then ["./assets/minecraft/textures/0.png"] else [] end)) | .[]' all_textures.temp union_atlas.temp) ) # find the union of all texture files listed in this atlas and all texture files in our resource pack
status_message process "Generating sprite sheet ${i} of ${total_union_atlas}" local texture_list=( $(jq -s --arg index "${1}" -r '(.[1][($index | tonumber)] - .[0] | length > 0) as $fallback_needed | ((.[1][($index | tonumber)] - (.[1][($index | tonumber)] - .[0])) + (if $fallback_needed then ["./assets/minecraft/textures/0.png"] else [] end)) | .[]' all_textures.temp union_atlas.temp) )
spritesheet-js -f json --name spritesheet/${i} --fullpath ${texture_list[@]} > /dev/null 2>&1 status_message process "Generating sprite sheet ${1} of ${total_union_atlas}"
jq --arg atlas_index "${i}" '.texture_data += {("gmdl_atlas_" + $atlas_index): {"textures": ("textures/geyser/geyser_custom/" + $atlas_index)}}' ./target/rp/textures/terrain_texture.json | sponge ./target/rp/textures/terrain_texture.json spritesheet-js -f json --name spritesheet/${1} --fullpath ${texture_list[@]} > /dev/null 2>&1
echo ${1} >> atlases.csv
#jq --arg atlas_index "${i}" '.texture_data += {("gmdl_atlas_" + $atlas_index): {"textures": ("textures/geyser/geyser_custom/" + $atlas_index)}}' ./target/rp/textures/terrain_texture.json | sponge ./target/rp/textures/terrain_texture.json
}
wait_for_jobs
generate_atlas "${i}" &
done done
wait # wait for all the jobs to finish
# generate terrain texture atlas
jq -cR 'split(",")' atlases.csv | jq -s 'map({("gmdl_atlas_" + .[0]): {"textures": ("textures/geyser/geyser_custom/" + .[0])}}) | add' > atlases.json
jq -s '
.[0] as $atlases
| .[1]
| .texture_data += $atlases
' atlases.json ./target/rp/textures/terrain_texture.json | sponge ./target/rp/textures/terrain_texture.json
status_message completion "All sprite sheets generated" status_message completion "All sprite sheets generated"
mv spritesheet/*.png ./target/rp/textures/geyser/geyser_custom mv spritesheet/*.png ./target/rp/textures/geyser/geyser_custom
@@ -1058,7 +1123,7 @@ fi
# cleanup # cleanup
status_message critical "Deleting scratch files" status_message critical "Deleting scratch files"
rm -rf spritesheet && rm -rf assets && rm -f pack.mcmeta && rm -f pack.png && rm -f parents.json && rm -f all.csv && rm -f pa.csv && rm -f *.temp && rm -f item_mappings.json && rm -f item_texture.json && rm paths.csv && rm hashes.csv && rm hashmap.json && rm count.csv rm -rf spritesheet && rm -rf assets && rm -f pack.mcmeta && rm -f pack.png && rm -f parents.json && rm -f all.csv && rm -f pa.csv && rm -f *.temp && rm -f item_mappings.json && rm -f item_texture.json && rm paths.csv && rm hashes.csv && rm hashmap.json && rm count.csv && rm -f atlases.csv && rm -f atlases.json && rm -f generated.csv && rm -f generated.json && rm -f icons.csv && rm -f icons.json && rm -f deleted.csv && rm -f deleted.json
status_message process "Compressing output packs" status_message process "Compressing output packs"
mkdir ./target/packaged mkdir ./target/packaged