Files
armorpaint/Sources/arm/io/ImportEnvmap.hx
T

129 lines
3.9 KiB
Haxe
Raw Normal View History

2019-06-19 18:45:36 +02:00
package arm.io;
2019-06-20 11:23:01 +02:00
import kha.Image;
import kha.Blob;
import kha.graphics4.TextureFormat;
import kha.arrays.Float32Array;
import iron.data.Data;
import iron.Scene;
2019-06-19 18:45:36 +02:00
import arm.ui.UITrait;
2019-06-20 12:30:31 +02:00
using StringTools;
2019-06-19 18:45:36 +02:00
class ImportEnvmap {
2019-12-09 00:28:10 +01:00
public static function run(path: String, image: Image) {
var p = Krom.getFilesLocation() + "/" + Data.dataPath;
2019-06-19 18:45:36 +02:00
#if krom_windows
var cmft = p + "/cmft.exe";
#elseif krom_linux
var cmft = p + "/cmft-linux64";
#else
var cmft = p + "/cmft-osx";
#end
2019-12-09 00:28:10 +01:00
var tmp = Krom.getFilesLocation() + "/" + Data.dataPath;
2019-06-19 18:45:36 +02:00
// Irr
2019-09-15 20:52:34 +02:00
var cmd = cmft;
2019-06-19 18:45:36 +02:00
cmd += ' --input "' + path + '"';
2019-12-09 00:28:10 +01:00
cmd += " --filter shcoeffs";
cmd += " --outputNum 1";
2019-06-19 18:45:36 +02:00
cmd += ' --output0 "' + tmp + 'tmp_irr"';
2019-09-15 20:52:34 +02:00
#if krom_windows
2019-12-09 00:28:10 +01:00
cmd = cmd.replace("/", "\\");
2019-09-15 20:52:34 +02:00
#end
2019-06-19 18:45:36 +02:00
Krom.sysCommand(cmd);
// Rad
var faceSize = Std.int(image.width / 8);
cmd = cmft;
cmd += ' --input "' + path + '"';
2019-12-09 00:28:10 +01:00
cmd += " --filter radiance";
cmd += " --dstFaceSize " + faceSize;
cmd += " --srcFaceSize " + faceSize;
cmd += " --excludeBase false";
cmd += " --glossScale 8";
cmd += " --glossBias 3";
cmd += " --lightingModel blinnbrdf";
cmd += " --edgeFixup none";
cmd += " --numCpuProcessingThreads 4";
cmd += " --useOpenCL true";
cmd += " --clVendor anyGpuVendor";
cmd += " --deviceType gpu";
cmd += " --deviceIndex 0";
cmd += " --generateMipChain true";
cmd += " --inputGammaNumerator 1.0";
cmd += " --inputGammaDenominator 2.2";
cmd += " --outputGammaNumerator 1.0";
cmd += " --outputGammaDenominator 1.0";
cmd += " --outputNum 1";
2019-06-19 18:45:36 +02:00
cmd += ' --output0 "' + tmp + 'tmp_rad"';
2019-12-09 00:28:10 +01:00
cmd += " --output0params hdr,rgbe,latlong";
2019-09-15 20:52:34 +02:00
#if krom_windows
2019-12-09 00:28:10 +01:00
cmd = cmd.replace("/", "\\");
2019-09-15 20:52:34 +02:00
#end
2019-06-19 18:45:36 +02:00
Krom.sysCommand(cmd);
// Load irr
2019-12-09 00:28:10 +01:00
Data.getBlob("tmp_irr.c", function(blob: Blob) {
2019-06-19 18:45:36 +02:00
var lines = blob.toString().split("\n");
var band0 = lines[5];
var band1 = lines[6];
var band2 = lines[7];
band0 = band0.substring(band0.indexOf("{"), band0.length);
band1 = band1.substring(band1.indexOf("{"), band1.length);
band2 = band2.substring(band2.indexOf("{"), band2.length);
var band = band0 + band1 + band2;
2019-06-20 12:30:31 +02:00
band = band.replace("{", "");
band = band.replace("}", "");
2019-06-19 18:45:36 +02:00
var ar = band.split(",");
2019-09-15 20:52:34 +02:00
var buf = new Float32Array(28); // Align to mult of 4 - 27->28
for (i in 0...27) buf[i] = Std.parseFloat(ar[i]);
2019-06-20 11:23:01 +02:00
Scene.active.world.probe.irradiance = buf;
2019-06-23 14:50:41 +02:00
Context.ddirty = 2;
2019-09-15 20:52:34 +02:00
Data.deleteBlob("tmp_irr.c");
2019-06-19 18:45:36 +02:00
});
// World envmap
2019-09-15 20:52:34 +02:00
Scene.active.world.probe.raw.strength = 1.0;
2019-06-20 11:23:01 +02:00
Scene.active.world.envmap = image;
2019-10-22 12:05:39 +02:00
Scene.active.world.raw.envmap = path;
2019-06-19 18:45:36 +02:00
UITrait.inst.savedEnvmap = image;
UITrait.inst.showEnvmapHandle.selected = UITrait.inst.showEnvmap = true;
2019-09-15 20:52:34 +02:00
// Load envmap clone and set mipmaps
Data.cachedImages.remove(path);
@:privateAccess Data.loadingImages.remove(path);
2019-12-09 00:28:10 +01:00
Data.getImage(path, function(image: kha.Image) {
2019-09-15 20:52:34 +02:00
// Load mips
var mipsCount = 6 + Std.int(image.width / 1024);
var mipsLoaded = 0;
2019-12-09 00:28:10 +01:00
var mips: Array<Image> = [];
2019-09-15 20:52:34 +02:00
while (mips.length < mipsCount + 2) mips.push(null);
var mw = Std.int(image.width / 2);
var mh = Std.int(image.width / 4);
for (i in 0...mipsCount) {
2019-12-09 00:28:10 +01:00
Data.getImage("tmp_rad_" + i + "_" + mw + "x" + mh + ".hdr", function(mip: Image) {
2019-09-15 20:52:34 +02:00
mips[i] = mip;
mipsLoaded++;
if (mipsLoaded == mipsCount) {
// 2x1 and 1x1 mips
var b2x1 = haxe.io.Bytes.alloc(2 * 1 * 4 * 4); // w * h * channels * format_size
var b1x1 = haxe.io.Bytes.alloc(1 * 1 * 4 * 4);
mips[mipsCount] = Image.fromBytes(b2x1, 2, 1, TextureFormat.RGBA128, kha.graphics4.Usage.DynamicUsage);
mips[mipsCount + 1] = Image.fromBytes(b1x1, 1, 1, TextureFormat.RGBA128, kha.graphics4.Usage.DynamicUsage);
// Set radiance
image.setMipmaps(mips);
Scene.active.world.probe.radiance = image;
Scene.active.world.probe.radianceMipmaps = mips;
Context.ddirty = 2;
}
}, true); // Readable
mw = Std.int(mw / 2);
mh = Std.int(mh / 2);
}
});
2019-06-19 18:45:36 +02:00
}
}