helpers to make 3D easier

This commit is contained in:
Yoshman29
2022-12-12 18:43:40 +01:00
parent 1e7fd15371
commit 53f9389715
2 changed files with 42 additions and 13 deletions
+1 -1
View File
@@ -1 +1 @@
1459
1462
+41 -12
View File
@@ -1,5 +1,6 @@
package flx3d;
import away3d.loaders.misc.AssetLoaderToken;
import away3d.library.assets.Asset3DType;
import away3d.events.Asset3DEvent;
import away3d.loaders.parsers.OBJParser;
@@ -17,35 +18,63 @@ class Flx3DView extends FlxView3D {
public function initLoader() {
if (_loader != null) return;
_loader = new Loader3D();
_loader.addEventListener(Asset3DEvent.ASSET_COMPLETE, onAssetComplete);
view.scene.addChild(_loader);
}
public function addMesh(assetPath:String, callback:Mesh->Void, ?texturePath:String) {
private var meshCallback:Mesh->Void = null;
private var queue:Array<MeshQueueItem> = [];
private function onAssetComplete(event:Asset3DEvent) {
if (event.asset != null && event.asset.assetType == Asset3DType.MESH) {
var mesh:Mesh = cast(event.asset, Mesh);
if (meshCallback != null)
meshCallback(mesh);
meshCallback = null;
if (queue.length > 0) {
var m = queue.shift();
addMesh(m.assetPath, m.callback, m.texturePath);
}
}
}
public function addMesh(assetPath:String, callback:Mesh->Void, ?texturePath:String, smoothTexture:Bool = true) {
initLoader();
var model = Assets.getBytes(assetPath);
if (model == null)
throw 'Model at ${assetPath} was not found.';
if (meshCallback != null) {
queue.push({
assetPath: assetPath,
callback: callback,
texturePath: texturePath
});
return null;
}
var context = new AssetLoaderContext();
var noExt = Path.withoutExtension(assetPath);
trace(noExt);
context.mapUrlToData('${Path.withoutDirectory(noExt)}.mtl', '$noExt.mtl');
var material:TextureMaterial = null;
if (texturePath != null)
material = new TextureMaterial(Cast.bitmapTexture(texturePath));
material = new TextureMaterial(Cast.bitmapTexture(texturePath), smoothTexture);
var token = _loader.loadData(model, context, null, new OBJParser());
token.addEventListener(Asset3DEvent.MESH_COMPLETE, function(event) {
if (event.type != Asset3DEvent.ASSET_COMPLETE) return;
if (event.asset.assetType == Asset3DType.MESH) {
var mesh = cast(event.asset, Mesh);
if (material != null)
mesh.material = material;
callback(mesh);
}
});
meshCallback = function(mesh) {
if (material != null)
mesh.material = material;
callback(mesh);
};
return token;
}
}
typedef MeshQueueItem = {
var assetPath:String;
var callback:Mesh->Void;
var texturePath:String;
}