Flx3DView addModel
This commit is contained in:
+1
-1
@@ -1 +1 @@
|
||||
1797
|
||||
1812
|
||||
+57
-45
@@ -1,5 +1,10 @@
|
||||
package flx3d;
|
||||
|
||||
import away3d.library.Asset3DLibraryBundle;
|
||||
import away3d.events.LoaderEvent;
|
||||
import away3d.loaders.AssetLoader;
|
||||
import away3d.loaders.misc.AssetLoaderToken;
|
||||
import funkin.system.Logs;
|
||||
import flixel.FlxG;
|
||||
import flx3d.Flx3DUtil;
|
||||
import away3d.library.assets.Asset3DType;
|
||||
@@ -12,11 +17,11 @@ import away3d.loaders.misc.AssetLoaderContext;
|
||||
import openfl.Assets;
|
||||
import away3d.entities.Mesh;
|
||||
import away3d.loaders.Loader3D;
|
||||
import funkin.windows.WindowsAPI.ConsoleColor;
|
||||
|
||||
// FlxView3D with helpers for easier updating
|
||||
|
||||
class Flx3DView extends FlxView3D {
|
||||
public var _loader:Loader3D;
|
||||
|
||||
public function new(x:Float = 0, y:Float = 0, width:Int = -1, height:Int = -1) {
|
||||
if (!Flx3DUtil.is3DAvailable())
|
||||
@@ -24,45 +29,12 @@ class Flx3DView extends FlxView3D {
|
||||
super(x, y, width, height);
|
||||
}
|
||||
|
||||
public function initLoader() {
|
||||
if (_loader != null) return;
|
||||
_loader = new Loader3D();
|
||||
_loader.addEventListener(Asset3DEvent.ASSET_COMPLETE, onAssetComplete);
|
||||
view.scene.addChild(_loader);
|
||||
}
|
||||
|
||||
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();
|
||||
public function addModel(assetPath:String, callback:Asset3DEvent->Void, ?texturePath:String, smoothTexture:Bool = true) {
|
||||
|
||||
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);
|
||||
@@ -72,24 +44,64 @@ class Flx3DView extends FlxView3D {
|
||||
if (texturePath != null)
|
||||
material = new TextureMaterial(Cast.bitmapTexture(texturePath), smoothTexture);
|
||||
|
||||
var token = _loader.loadData(model, context, null, switch(Path.extension(assetPath).toLowerCase()) {
|
||||
return loadData(model, context, switch(Path.extension(assetPath).toLowerCase()) {
|
||||
case "dae": new DAEParser();
|
||||
case "md2": new MD2Parser();
|
||||
case "md5": new MD5MeshParser();
|
||||
case "awd": new AWDParser();
|
||||
default: new OBJParser();
|
||||
}, (event:Asset3DEvent) -> {
|
||||
if (event.asset != null && event.asset.assetType == Asset3DType.MESH) {
|
||||
var mesh:Mesh = cast(event.asset, Mesh);
|
||||
if (material != null)
|
||||
mesh.material = material;
|
||||
}
|
||||
callback(event);
|
||||
});
|
||||
meshCallback = function(mesh) {
|
||||
if (material != null)
|
||||
mesh.material = material;
|
||||
callback(mesh);
|
||||
};
|
||||
return token;
|
||||
}
|
||||
|
||||
@:deprecated public function addMesh(assetPath:String, callback:Mesh->Void, ?texturePath:String, smoothTexture:Bool = true) {
|
||||
Logs.trace('The addMesh() function is deprecated!', ERROR, RED);
|
||||
}
|
||||
|
||||
private var _loaders:Map<Asset3DLibraryBundle, AssetLoaderToken> = [];
|
||||
|
||||
private function loadData(data:Dynamic, context:AssetLoaderContext, parser:ParserBase, onAssetCallback:Asset3DEvent->Void):AssetLoaderToken {
|
||||
var token:AssetLoaderToken;
|
||||
|
||||
var lib:Asset3DLibraryBundle;
|
||||
lib = Asset3DLibraryBundle.getInstance(null);
|
||||
token = lib.loadData(data, context, null, parser);
|
||||
|
||||
token.addEventListener(Asset3DEvent.ASSET_COMPLETE, onAssetCallback);
|
||||
|
||||
token.addEventListener(LoaderEvent.RESOURCE_COMPLETE, (_) -> { // Dispose loader when done
|
||||
trace("Disposing Loader...");
|
||||
_loaders.remove(lib);
|
||||
|
||||
lib = null;
|
||||
token = null;
|
||||
});
|
||||
|
||||
_loaders.set(lib,token);
|
||||
|
||||
return token;
|
||||
}
|
||||
|
||||
override function destroy() {
|
||||
super.destroy();
|
||||
|
||||
for (loader => token in _loaders) {
|
||||
_loaders.remove(loader);
|
||||
|
||||
loader = null;
|
||||
token = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
typedef MeshQueueItem = {
|
||||
typedef ModelQueueItem = {
|
||||
var assetPath:String;
|
||||
var callback:Mesh->Void;
|
||||
var callback:Asset3DEvent->Void;
|
||||
var texturePath:String;
|
||||
}
|
||||
Reference in New Issue
Block a user