Auto-generate envmaps.

This commit is contained in:
Lubos Lenco 2016-04-08 11:05:55 +02:00
parent c769095a1d
commit 648b9543e5
4 changed files with 129 additions and 28 deletions

BIN
Assets/envmap_brdf.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.6 KiB

View file

@ -4,6 +4,7 @@ from bpy.props import *
import os
import sys
import json
import subprocess
def register():
pass
@ -56,36 +57,135 @@ def buildNodeTree(world_name, node_group, shader_references, asset_references):
for node in node_group.nodes:
# Env map included
if node.bl_idname == 'ShaderNodeTexEnvironment': # Just look for env texture for now
texture.name = node.image.name.rsplit('.', 1)[0] # Remove extension
image_name = node.image.name
texture.name = image_name.rsplit('.', 1)[0] # Remove extension
# Add resources to khafie
asset_references.append('compiled/ShaderResources/env_map/env_map.json')
shader_references.append('compiled/Shaders/env_map/env_map')
# Generate prefiltered envmaps
#generate_envmaps()
generate_envmaps(image_name, image_name.endswith('.jpg'))
with open(path + material_name + '.json', 'w') as f:
f.write(output.to_JSON())
def generate_envmaps():
def generate_envmaps(image_name, disable_hdr):
if not os.path.exists('Assets/generated/envmaps'):
os.makedirs('Assets/generated/envmaps')
# Get paths
haxelib_path = "haxelib"
if platform.system() == 'Darwin':
haxelib_path = "/usr/local/bin/haxelib"
# haxelib_path = "haxelib"
# if platform.system() == 'Darwin':
# haxelib_path = "/usr/local/bin/haxelib"
output = subprocess.check_output([haxelib_path + " path cyclesgame"], shell=True)
output = str(output).split("\\n")[0].split("'")[1]
cmft_path = output[:-8] + "tools/cmft/"
# output = subprocess.check_output([haxelib_path + " path cyclesgame"], shell=True)
# output = str(output).split("\\n")[0].split("'")[1]
# cmft_path = output[:-8] + "tools/cmft/"
cmft_path = 'Libraries/cyclesgame/tools/cmft/'
kraffiti_path = 'Kha/Kore/Tools/kraffiti/'
generated_files = []
output_gama_numerator = '1.0' if disable_hdr else '2.2'
base_name = image_name.rsplit('.', 1)[0]
input_file = 'Assets/textures/' + image_name
# Set dir
s = bpy.data.filepath.split(os.path.sep)
name = s.pop()
name = name.split(".")
name = name[0]
fp = os.path.sep.join(s)
os.chdir(fp)
# Get input size
output = subprocess.check_output([ \
kraffiti_path + 'kraffiti-osx' + \
' from=' + input_file + \
' donothing'], shell=True)
# #%ix%i
image_w = str(output).split("'")[1]
image_w = image_w[1:]
image_w = image_w.split('x')[0]
image_w = int(image_w)
# 4096 = 256 face - 6 mips
# 2048 = 128 face - 5 mips
# 1024 = 64 face - 4 mips
# 512 = 32 face - 3 mips
# 256 = 16 face - 2 mips
# 128 = 8 face - 1 mip
mip_count = 1
num = 128
while num < image_w:
num *= 2
mip_count += 1
face_size = image_w / 16
src_face_size = str(face_size)
dst_face_size = str(face_size)
# Generate irradiance
output_file = 'Assets/generated/envmaps/' + base_name + '_irradiance'
subprocess.call([ \
cmft_path + 'cmft-osx' + \
' --input ' + input_file + \
' --filter irradiance' + \
' --dstFaceSize ' + dst_face_size + \
' --inputGammaNumerator 2.2' + \
' --inputGammaDenominator 1.0' + \
' --outputGammaNumerator ' + output_gama_numerator + \
' --outputGammaDenominator 1.0' + \
' --outputNum 1' + \
' --output0 ' + output_file + \
' --output0params hdr,rgbe,latlong'], shell=True)
generated_files.append(output_file)
# Generate radiance
output_file = 'Assets/generated/envmaps/' + base_name + '_radiance'
outformat = 'jpg' if disable_hdr else 'hdr'
output = subprocess.check_output([ \
kraffiti_path + 'kraffiti-osx' + \
' from=' + input_file + \
' to=' + output_file + '.' + outformat + \
' scale=0.5'], shell=True)
subprocess.call([ \
cmft_path + 'cmft-osx' + \
' --input ' + input_file + \
' --filter radiance' + \
' --dstFaceSize ' + dst_face_size + \
' --srcFaceSize ' + src_face_size + \
' --excludeBase false' + \
' --mipCount ' + str(mip_count) + \
' --glossScale 7' + \
' --glossBias 3' + \
' --lightingModel blinnbrdf' + \
' --edgeFixup none' + \
' --numCpuProcessingThreads 4' + \
' --useOpenCL true' + \
' --clVendor anyGpuVendor' + \
' --deviceType gpu' + \
' --deviceIndex 0' + \
' --generateMipChain false' + \
' --inputGammaNumerator 2.2' + \
' --inputGammaDenominator 1.0' + \
' --outputGammaNumerator ' + output_gama_numerator + \
' --outputGammaDenominator 1.0' + \
' --outputNum 1' + \
' --output0 ' + output_file + \
' --output0params hdr,rgbe,latlong'], shell=True)
for i in range(0, mip_count):
generated_files.append(output_file + '_' + str(i))
# Convert to jpgs
if disable_hdr is True:
for f in generated_files:
subprocess.call([ \
kraffiti_path + 'kraffiti-osx' + \
' from=' + f + '.hdr' + \
' to=' + f + '.jpg' + \
' format=jpg'], shell=True)
os.remove(f + '.hdr')
# Scale from (32x16 to 1x1>
for i in range (0, 5):
last = generated_files[-1]
out = output_file + '_' + str(mip_count + i)
subprocess.call([ \
kraffiti_path + 'kraffiti-osx' + \
' from=' + last + '.' + outformat + \
' to=' + out + '.' + outformat + \
' scale=0.5' + \
' format=' + outformat], shell=True)
generated_files.append(out)
# Generate maps
call([cmft_path + "cmft-osx", "--input", "filename"])

Binary file not shown.

View file

@ -29,7 +29,7 @@ CMFT=./cmft-osx
# --output0 "okretnica"
# Typical parameters for radiance filter.
# eval $CMFT $@ --input "okretnica.tga" \
# eval $CMFT $@ --input "envmap.jpg" \
# ::Filter options \
# --filter radiance \
# --srcFaceSize 256 \
@ -51,13 +51,13 @@ CMFT=./cmft-osx
# --inputGammaDenominator 1.0 \
# --outputGammaNumerator 1.0 \
# --outputGammaDenominator 2.2 \
# --generateMipChain false \
# --generateMipChain true \
# ::Output \
# --outputNum 2 \
# --output0 "okretnica_pmrem" \
# --output0params dds,bgra8,cubemap \
# --output1 "okretnica_pmrem" \
# --output1params ktx,rgba8,cubemap
# --outputNum 1 \
# --output0 "envmap_rad" \
# --output0params hdr,rgbe,latlong \
# --output1 "okretnica_pmrem" \
# --output1params ktx,rgba8,cubemap
# Cmft can also be run without any processing filter. This can be used for performing image manipulations or exporting different image format.
#eval $CMFT $@ --input "okretnica.tga" \
@ -167,9 +167,10 @@ CMFT=./cmft-osx
# --output3 "cmft_facelist" --output3params tga,bgra8,facelist \
# --output4 "cmft_latlong" --output4params hdr,rgbe,latlong
eval $CMFT $@ --input "memorial.hdr" \
eval $CMFT $@ --input "shrine.hdr" \
--filter none \
::Output \
--outputNum 1 \
--output0 "memorial_hdr" \
--output0params hdr,rgbe,latlong \
--dstFaceSize 0 \
--output0 "test" \
--output0params hdr,rgbe,latlong