From 318b034019bec2e4c1b116d09be99510e629ae57 Mon Sep 17 00:00:00 2001 From: luboslenco Date: Wed, 25 Dec 2019 13:00:22 +0100 Subject: [PATCH] Turn stl importer into plugin --- Assets/plugins/import_stl.js | 74 +++++++++++++++++++++++++++++++++ Sources/arm/Plugin.hx | 1 + Sources/arm/format/StlParser.hx | 65 ----------------------------- Sources/arm/io/ImportMesh.hx | 1 - Sources/arm/io/ImportStl.hx | 20 --------- Sources/arm/sys/Path.hx | 2 +- 6 files changed, 76 insertions(+), 87 deletions(-) create mode 100644 Assets/plugins/import_stl.js delete mode 100644 Sources/arm/format/StlParser.hx delete mode 100644 Sources/arm/io/ImportStl.hx diff --git a/Assets/plugins/import_stl.js b/Assets/plugins/import_stl.js new file mode 100644 index 00000000..96009d7b --- /dev/null +++ b/Assets/plugins/import_stl.js @@ -0,0 +1,74 @@ + +let import_stl = function(path, done) { + iron.Data.getBlob(path, function(b) { + let input = new arm.BytesInput(b.bytes); + let header = input.read(80); + if (header.getString(0, 5) == "solid") { + return; // ascii not supported + } + let faces = input.readInt32(); + let posTemp = new Float32Array(faces * 9); + let norTemp = new Float32Array(faces * 3); + for (let i = 0; i < faces; ++i) { + let i3 = i * 3; + norTemp[i3 ] = input.readFloat(); + norTemp[i3 + 1] = input.readFloat(); + norTemp[i3 + 2] = input.readFloat(); + let i9 = i * 9; + posTemp[i9 ] = input.readFloat(); + posTemp[i9 + 1] = input.readFloat(); + posTemp[i9 + 2] = input.readFloat(); + posTemp[i9 + 3] = input.readFloat(); + posTemp[i9 + 4] = input.readFloat(); + posTemp[i9 + 5] = input.readFloat(); + posTemp[i9 + 6] = input.readFloat(); + posTemp[i9 + 7] = input.readFloat(); + posTemp[i9 + 8] = input.readFloat(); + input.readInt16(); // attribute + } + + let scalePos = 0.0; + for (let i = 0; i < posTemp.length; ++i) { + let f = Math.abs(posTemp[i]); + if (scalePos < f) scalePos = f; + } + let inv = 32767 * (1 / scalePos); + + let verts = posTemp.length / 3; + let posa = new Int16Array(verts * 4); + let nora = new Int16Array(verts * 2); + let inda = new Uint32Array(verts); + for (let i = 0; i < verts; ++i) { + posa[i * 4 ] = posTemp[i * 3 ] * inv; + posa[i * 4 + 1] = -posTemp[i * 3 + 2] * inv; + posa[i * 4 + 2] = posTemp[i * 3 + 1] * inv; + nora[i * 2 ] = norTemp[i ] * 32767; + nora[i * 2 + 1] = -norTemp[i + 2] * 32767; + posa[i * 4 + 3] = norTemp[i + 1] * 32767; + inda[i] = i; + } + + let name = path.split("\\").pop().split("/").pop().split(".").shift(); + done({ + name: name, + posa: posa, + nora: nora, + inda: inda, + scale_pos: scalePos, + scale_tex: 1.0 + }); + + iron.Data.deleteBlob(path); + }); +} + +let plugin = new arm.Plugin(); +let formats = arm.Path.meshFormats; +let importers = arm.Path.meshImporters; +formats.push("stl"); +importers.h["stl"] = import_stl; + +plugin.delete = function() { + formats.splice(formats.indexOf("stl"), 1); + importers.h["stl"] = null; +}; diff --git a/Sources/arm/Plugin.hx b/Sources/arm/Plugin.hx index fa77587d..a00bd577 100644 --- a/Sources/arm/Plugin.hx +++ b/Sources/arm/Plugin.hx @@ -61,6 +61,7 @@ class ArmBridge { public static var ReflectSetField = Reflect.setField; public static var StdIs = Std.is; public static var Bytes = haxe.io.Bytes; + public static var BytesInput = haxe.io.BytesInput; public static var Blob = kha.Blob; public static var Image = kha.Image; public static var Scheduler = kha.Scheduler; diff --git a/Sources/arm/format/StlParser.hx b/Sources/arm/format/StlParser.hx deleted file mode 100644 index bb78b295..00000000 --- a/Sources/arm/format/StlParser.hx +++ /dev/null @@ -1,65 +0,0 @@ -package arm.format; - -class StlParser { - - public var posa: kha.arrays.Int16Array = null; - public var nora: kha.arrays.Int16Array = null; - public var texa: kha.arrays.Int16Array = null; - public var inda: kha.arrays.Uint32Array = null; - - public var scalePos = 1.0; - public var scaleTex = 1.0; - public var name = ""; - public var hasNext = false; - public var pos = 0; - - public function new(blob: kha.Blob) { - var bytes = blob.bytes; - var input = new haxe.io.BytesInput(bytes); - var header = input.read(80); - if (header.getString(0, 5) == "solid") { - return; // ascii not supported - } - var faces = input.readInt32(); - var posTemp = new kha.arrays.Float32Array(faces * 9); - var norTemp = new kha.arrays.Float32Array(faces * 3); - for (i in 0...faces) { - var i3 = i * 3; - norTemp[i3 ] = input.readFloat(); - norTemp[i3 + 1] = input.readFloat(); - norTemp[i3 + 2] = input.readFloat(); - var i9 = i * 9; - posTemp[i9 ] = input.readFloat(); - posTemp[i9 + 1] = input.readFloat(); - posTemp[i9 + 2] = input.readFloat(); - posTemp[i9 + 3] = input.readFloat(); - posTemp[i9 + 4] = input.readFloat(); - posTemp[i9 + 5] = input.readFloat(); - posTemp[i9 + 6] = input.readFloat(); - posTemp[i9 + 7] = input.readFloat(); - posTemp[i9 + 8] = input.readFloat(); - input.readInt16(); // attribute - } - - scalePos = 0.0; - for (i in 0...posTemp.length) { - var f = Math.abs(posTemp[i]); - if (scalePos < f) scalePos = f; - } - var inv = 32767 * (1 / scalePos); - - var verts = Std.int(posTemp.length / 3); - posa = new kha.arrays.Int16Array(verts * 4); - nora = new kha.arrays.Int16Array(verts * 2); - inda = new kha.arrays.Uint32Array(verts); - for (i in 0...verts) { - posa[i * 4 ] = Std.int( posTemp[i * 3 ] * inv); - posa[i * 4 + 1] = Std.int(-posTemp[i * 3 + 2] * inv); - posa[i * 4 + 2] = Std.int( posTemp[i * 3 + 1] * inv); - nora[i * 2 ] = Std.int( norTemp[i ] * 32767); - nora[i * 2 + 1] = Std.int(-norTemp[i + 2] * 32767); - posa[i * 4 + 3] = Std.int( norTemp[i + 1] * 32767); - inda[i] = i; - } - } -} diff --git a/Sources/arm/io/ImportMesh.hx b/Sources/arm/io/ImportMesh.hx index 14115b5d..3ada3dba 100644 --- a/Sources/arm/io/ImportMesh.hx +++ b/Sources/arm/io/ImportMesh.hx @@ -35,7 +35,6 @@ class ImportMesh { var p = path.toLowerCase(); if (p.endsWith(".obj")) ImportObj.run(path); else if (p.endsWith(".fbx")) ImportFbx.run(path); - else if (p.endsWith(".stl")) ImportStl.run(path); else if (p.endsWith(".blend")) ImportBlend.run(path); else { var ext = path.substr(path.lastIndexOf(".") + 1); diff --git a/Sources/arm/io/ImportStl.hx b/Sources/arm/io/ImportStl.hx deleted file mode 100644 index 8ed6f8a0..00000000 --- a/Sources/arm/io/ImportStl.hx +++ /dev/null @@ -1,20 +0,0 @@ -package arm.io; - -import kha.Blob; -import iron.data.Data; -import arm.format.StlParser; -using StringTools; - -class ImportStl { - - public static function run(path: String) { - Data.getBlob(path, function(b: Blob) { - var obj = new StlParser(b); - var p = path.replace("\\", "/"); - var index = p.lastIndexOf("/"); - obj.name = p.substring(index >= 0 ? index + 1 : 0, p.length - 4); - ImportMesh.makeMesh(obj, path); - Data.deleteBlob(path); - }); - } -} diff --git a/Sources/arm/sys/Path.hx b/Sources/arm/sys/Path.hx index cfcc7003..7908bcdd 100644 --- a/Sources/arm/sys/Path.hx +++ b/Sources/arm/sys/Path.hx @@ -11,7 +11,7 @@ class Path { public static var sep = "/"; #end - public static var meshFormats = ["obj", "fbx", "stl", "blend"]; + public static var meshFormats = ["obj", "fbx", "blend"]; public static var textureFormats = ["jpg", "jpeg", "png", "tga", "bmp", "psd", "gif", "hdr"]; public static var meshImporters = new Map(Dynamic->Void)->Void>();