Turn stl importer into plugin
This commit is contained in:
@@ -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;
|
||||
};
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
|
||||
@@ -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);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -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<String, String->(Dynamic->Void)->Void>();
|
||||
|
||||
Reference in New Issue
Block a user