Use webassembly for ammo+krom

This commit is contained in:
luboslenco 2019-11-16 14:11:32 +01:00
parent 405da0e10e
commit f310776985
2 changed files with 51 additions and 15 deletions

View file

@ -5,7 +5,7 @@ import kha.WindowOptions;
class Starter {
static var tasks:Int;
#if arm_loadscreen
public static var drawLoading:kha.graphics2.Graphics->Int->Int->Void = null;
public static var numAssets:Int;
@ -15,7 +15,7 @@ class Starter {
function start() {
if (tasks > 0) return;
if (armory.data.Config.raw == null) armory.data.Config.raw = {};
var c = armory.data.Config.raw;
@ -28,20 +28,21 @@ class Starter {
if (c.window_scale == null) c.window_scale = 1.0;
if (c.window_msaa == null) c.window_msaa = msaa;
if (c.window_vsync == null) c.window_vsync = vsync;
armory.object.Uniforms.register();
var windowMode = c.window_mode == 0 ? kha.WindowMode.Windowed : kha.WindowMode.Fullscreen;
var windowFeatures = None;
if (c.window_resizable) windowFeatures |= FeatureResizable;
if (c.window_maximizable) windowFeatures |= FeatureMaximizable;
if (c.window_minimizable) windowFeatures |= FeatureMinimizable;
#if (kha_webgl && (!arm_legacy))
try {
#end
kha.System.start({title: Main.projectName, width: c.window_w, height: c.window_h, window: {mode: windowMode, windowFeatures: windowFeatures}, framebuffer: {samplesPerPixel: c.window_msaa, verticalSync: c.window_vsync}}, function(window:kha.Window) {
kha.System.start({title: Main.projectName, width: c.window_w, height: c.window_h, window: {mode: windowMode, windowFeatures: windowFeatures}, framebuffer: {samplesPerPixel: c.window_msaa, verticalSync: c.window_vsync}}, function(window:kha.Window) {
iron.App.init(function() {
#if arm_loadscreen
function load(g:kha.graphics2.Graphics) {
@ -78,7 +79,18 @@ class Starter {
var print = function(s:String) { trace(s); };
var loaded = function() { tasks--; start(); };
untyped __js__("(1, eval)({0})", b.toString());
#if kha_krom
var instantiateWasm = function(imports, successCallback) {
var wasmbin = Krom.loadBlob("ammo.wasm.wasm");
var module = new js.lib.webassembly.Module(wasmbin);
var inst = new js.lib.webassembly.Instance(module, imports);
successCallback(inst);
return inst.exports;
};
untyped __js__("Ammo({print:print, instantiateWasm:instantiateWasm}).then(loaded)");
#else
untyped __js__("Ammo({print:print}).then(loaded)");
#end
});
}
#end
@ -94,9 +106,27 @@ class Starter {
#end
tasks = 1;
#if (js && arm_bullet) tasks++; loadLibAmmo("ammo.js"); #end
#if (js && arm_navigation) tasks++; loadLib("recast.js"); #end
#if (arm_config) tasks++; armory.data.Config.load(function() { tasks--; start(); }); #end
#if (js && arm_bullet)
tasks++;
#if kha_krom
loadLibAmmo("ammo.wasm.js");
#else
loadLibAmmo("ammo.js");
#end
#end
#if (js && arm_navigation)
tasks++;
loadLib("recast.js");
#end
#if (arm_config)
tasks++;
armory.data.Config.load(function() { tasks--; start(); });
#end
tasks--; start();
}
}

View file

@ -79,7 +79,7 @@ project.addSources('Sources');
for lib in libs:
if os.path.isdir('Libraries/' + lib):
f.write('project.addLibrary("{0}");\n'.format(lib.replace('//', '/')))
# Subprojects, merge this with libraries
if os.path.exists('Subprojects'):
libs = os.listdir('Subprojects')
@ -98,11 +98,17 @@ project.addSources('Sources');
assets.add_khafile_def('arm_bullet')
if not os.path.exists('Libraries/haxebullet'):
f.write(add_armory_library(sdk_path + '/lib/', 'haxebullet', rel_path=rel_path))
if state.target.startswith('krom') or state.target == 'html5' or state.target == 'node':
if state.target.startswith('krom'):
ammojs_path = sdk_path + '/lib/haxebullet/ammo/ammo.wasm.js'
ammojs_path = ammojs_path.replace('\\', '/').replace('//', '/')
f.write(add_assets(ammojs_path, rel_path=rel_path))
ammojs_wasm_path = sdk_path + '/lib/haxebullet/ammo/ammo.wasm.wasm'
ammojs_wasm_path = ammojs_wasm_path.replace('\\', '/').replace('//', '/')
f.write(add_assets(ammojs_wasm_path, rel_path=rel_path))
elif state.target == 'html5' or state.target == 'node':
ammojs_path = sdk_path + '/lib/haxebullet/ammo/ammo.js'
ammojs_path = ammojs_path.replace('\\', '/').replace('//', '/')
f.write(add_assets(ammojs_path, rel_path=rel_path))
# haxe.macro.Compiler.includeFile(ammojs_path)
elif wrd.arm_physics_engine == 'Oimo':
assets.add_khafile_def('arm_oimo')
if not os.path.exists('Libraries/oimo'):
@ -189,7 +195,7 @@ project.addSources('Sources');
dest += ', destination: "data/{name}"'
f.write('project.addAssets("' + assets_path + '", { notinlist: true ' + dest + '});\n')
f.write('project.addAssets("' + assets_path_sh + '", { notinlist: true ' + dest + '});\n')
shader_data_references = sorted(list(set(assets.shader_datas)))
for ref in shader_data_references:
ref = ref.replace('\\', '/').replace('//', '/')
@ -228,7 +234,7 @@ project.addSources('Sources');
p = p.replace('//', '/')
f.write(add_assets(p.replace('\\', '/'), use_data_dir=use_data_dir, rel_path=rel_path))
assets.add_khafile_def('arm_ui')
if wrd.arm_minimize == False:
assets.add_khafile_def('arm_json')
@ -585,7 +591,7 @@ const int compoChromaticSamples = """ + str(rpdat.arm_chromatic_aberration_sampl
if len(bpy.data.cameras) > 0 and bpy.data.cameras[0].dof.use_dof:
focus_distance = bpy.data.cameras[0].dof.focus_distance
fstop = bpy.data.cameras[0].dof.aperture_fstop
if focus_distance > 0.0:
f.write(
"""const float compoDOFDistance = """ + str(round(focus_distance * 100) / 100) + """;