Import stl
This commit is contained in:
@@ -168,7 +168,7 @@ class Context {
|
||||
public static function importMesh() {
|
||||
UIFiles.show = true;
|
||||
UIFiles.isSave = false;
|
||||
UIFiles.filters = "obj,fbx,blend,arm";
|
||||
UIFiles.filters = "obj,fbx,stl,blend,arm";
|
||||
UIFiles.filesDone = function(path:String) {
|
||||
Importer.importFile(path);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,65 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package arm.io;
|
||||
|
||||
import kha.Blob;
|
||||
import iron.data.Data;
|
||||
import arm.format.StlParser;
|
||||
import arm.ui.UITrait;
|
||||
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);
|
||||
Importer.makeMesh(obj, path);
|
||||
Data.deleteBlob(path);
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -70,6 +70,7 @@ class Importer {
|
||||
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);
|
||||
|
||||
if (Context.mergedObject != null) {
|
||||
|
||||
@@ -442,7 +442,7 @@ class UITrait {
|
||||
else if (Operator.shortcut(Config.keymap.import_assets)) {
|
||||
UIFiles.show = true;
|
||||
UIFiles.isSave = false;
|
||||
UIFiles.filters = "jpg,png,tga,bmp,psd,gif,hdr,obj,fbx,blend,arm";
|
||||
UIFiles.filters = "jpg,png,tga,bmp,psd,gif,hdr,obj,fbx,stl,blend,arm";
|
||||
UIFiles.filesDone = function(path:String) {
|
||||
Importer.importFile(path);
|
||||
}
|
||||
|
||||
@@ -33,6 +33,7 @@ class Path {
|
||||
var p = path.toLowerCase();
|
||||
return p.endsWith(".obj") ||
|
||||
p.endsWith(".fbx") ||
|
||||
p.endsWith(".stl") ||
|
||||
p.endsWith(".blend");
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user