From 8806382c8642c274dae8b5b7cbd464aed5905a08 Mon Sep 17 00:00:00 2001 From: lunarcleint <88809950+lunarcleint@users.noreply.github.com> Date: Mon, 26 Dec 2022 11:59:25 -0700 Subject: [PATCH 1/2] Updating 3D-HaxeFlixel repo --- buildnumber.txt | 2 +- source/flx3d/Flx3DUtil.hx | 24 +++++++- source/flx3d/Flx3DView.hx | 11 ++-- source/flx3d/FlxView3D.hx | 112 +++++++++++++++++++++----------------- 4 files changed, 93 insertions(+), 56 deletions(-) diff --git a/buildnumber.txt b/buildnumber.txt index c7ecf224..02fa9676 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -1796 \ No newline at end of file +1797 \ No newline at end of file diff --git a/source/flx3d/Flx3DUtil.hx b/source/flx3d/Flx3DUtil.hx index 35625e48..0a987344 100644 --- a/source/flx3d/Flx3DUtil.hx +++ b/source/flx3d/Flx3DUtil.hx @@ -1,6 +1,7 @@ -package flx3d; +package flx3D; import away3d.core.managers.Stage3DManager; +import away3d.library.assets.IAsset; import flixel.FlxG; /** @@ -12,11 +13,19 @@ import flixel.FlxG; */ class Flx3DUtil { + /** + * Returns the total amount of 3D stages (not exculding the ones in use) + * @return Int + */ public static inline function getTotal3D():Int { return FlxG.stage.stage3Ds.length; } + /** + * Returns if a Stage3D is available + * @return Bool + */ public static inline function is3DAvailable():Bool { @:privateAccess { @@ -26,4 +35,17 @@ class Flx3DUtil return Stage3DManager._numStageProxies < Stage3DManager._stageProxies.length; } } + + /** + * Calls dispose() (destroy) on a Away3D asset + * @param obj + * @return null + */ + public static inline function dispose(obj:Null):T + { + if (obj != null) + obj.dispose(); + + return null; + } } diff --git a/source/flx3d/Flx3DView.hx b/source/flx3d/Flx3DView.hx index ab1c4cd4..e611a5e5 100644 --- a/source/flx3d/Flx3DView.hx +++ b/source/flx3d/Flx3DView.hx @@ -1,6 +1,7 @@ package flx3d; -import away3d.loaders.misc.AssetLoaderToken; +import flixel.FlxG; +import flx3D.Flx3DUtil; import away3d.library.assets.Asset3DType; import away3d.events.Asset3DEvent; import away3d.loaders.parsers.*; @@ -15,12 +16,14 @@ import away3d.loaders.Loader3D; // 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, scale:Float = 1) { + public function new(x:Float = 0, y:Float = 0, width:Int = -1, height:Int = -1) { if (!Flx3DUtil.is3DAvailable()) - throw "[Flx3DView] 3D is not available on this platform."; - super(x, y, width, height, scale); + throw "[Flx3DView] 3D is not available on this platform. Stages in use: " + Flx3DUtil.getTotal3D() + ", Max stages allowed: " + FlxG.stage.stage3Ds.length + "."; + super(x, y, width, height); } + public function initLoader() { if (_loader != null) return; _loader = new Loader3D(); diff --git a/source/flx3d/FlxView3D.hx b/source/flx3d/FlxView3D.hx index 03ddfaa8..f2726dd4 100644 --- a/source/flx3d/FlxView3D.hx +++ b/source/flx3d/FlxView3D.hx @@ -1,12 +1,7 @@ -package flx3d; +package flx3D; -import away3d.entities.Mesh; -import funkin.system.Main; import away3d.containers.View3D; -import away3d.events.LoaderEvent; import away3d.library.assets.IAsset; -import away3d.loaders.Loader3D; -import away3d.loaders.misc.AssetLoaderContext; import flixel.FlxG; import flixel.FlxSprite; import openfl.display.BitmapData; @@ -20,87 +15,104 @@ import openfl.display.BitmapData; */ class FlxView3D extends FlxSprite { - public var view:View3D; - @:noCompletion private var bmp:BitmapData; - private var _loader:Loader3D; - private var assetLoaderContext:AssetLoaderContext = new AssetLoaderContext(); + /** + * The Away3D View + */ + public var view:View3D; - public function new(x:Float = 0, y:Float = 0, width:Int = -1, height:Int = -1, scale:Float = 1) + /** + * Set this flag to true to force the View3D to update during the `draw()` call. + */ + public var dirty3D:Bool = true; + + /** + * Creates a new instance of a View3D from Away3D and renders it as a FlxSprite + * ! Call Flx3DUtil.is3DAvailable(); to make sure a 3D stage is usable + * @param x + * @param y + * @param width Leave as -1 for screen width + * @param height Leave as -1 for screen height + */ + public function new(x:Float = 0, y:Float = 0, width:Int = -1, height:Int = -1) { super(x, y); - frameRate = 60; - view = new View3D(); view.visible = false; - Main.instance.addChildAt(view, 0); view.width = width == -1 ? FlxG.width : width; view.height = height == -1 ? FlxG.height : height; - view.backgroundAlpha = 0; - view.alpha = 0; - bmp = new BitmapData(Std.int(view.width), Std.int(view.height), true, 0x0); + view.backgroundAlpha = 0; + FlxG.stage.addChildAt(view, 0); + + bmp = new BitmapData(Std.int(view.width), Std.int(view.height), true, 0x0); loadGraphic(bmp); } - private function dispose(obj:Null):T // ! CALL THIS WHEN YOU DESTROY YOUR SCENE + /** + * Disposes (destroys) the asset and returns null + * @param obj + * @return T null + */ + public static function dispose(obj:Null):T { - if (obj != null) - obj.dispose(); - return null; + return Flx3DUtil.dispose(obj); } + /** + * Disposes of all the Away3D assets associated with the FlxView3D + */ override function destroy() { - Main.instance.removeChild(view); - view.dispose(); - _loader.disposeWithChildren(); + FlxG.stage.removeChild(view); super.destroy(); + if (bmp != null) { bmp.dispose(); bmp = null; } - view = null; - assetLoaderContext = null; + + if (view != null) + { + view.dispose(); + view = null; + } + } - @:isVar public var frameRate(default, set):Float; - - public function set_frameRate(value:Float) - { - delay = 1 / value; - return frameRate = value; - } - - public var renderEveryFrame:Bool = true; - - @:noCompletion private var delay:Float = 0; - @:noCompletion private var drawTimer:Float = 0; - - override function update(elapsed:Float) - { - super.update(elapsed); - drawTimer += elapsed; - } - - override function draw() + @:noCompletion override function draw() { super.draw(); - if (renderEveryFrame || drawTimer > delay) + if (dirty3D) { - Main.instance.addChildAt(view, 0); + view.visible = false; + FlxG.stage.addChildAt(view, 0); + var old = FlxG.game.filters; FlxG.game.filters = null; + view.renderer.queueSnapshot(bmp); view.render(); - drawTimer = 0; + FlxG.game.filters = old; - Main.instance.removeChild(view); + FlxG.stage.removeChild(view); } } + + @:noCompletion override function set_width(newWidth:Float):Float + { + super.set_width(newWidth); + return view != null ? view.width = width : width; + } + + @:noCompletion override function set_height(newHeight:Float):Float + { + super.set_height(newHeight); + return view != null ? view.height = height : height; + } } From 15c062b468fff210d24f746d318a41c586136348 Mon Sep 17 00:00:00 2001 From: lunarcleint <88809950+lunarcleint@users.noreply.github.com> Date: Mon, 26 Dec 2022 12:00:48 -0700 Subject: [PATCH 2/2] Fuck you yosh --- source/flx3d/Flx3DUtil.hx | 2 +- source/flx3d/Flx3DView.hx | 2 +- source/flx3d/FlxView3D.hx | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/source/flx3d/Flx3DUtil.hx b/source/flx3d/Flx3DUtil.hx index 0a987344..b4749cc1 100644 --- a/source/flx3d/Flx3DUtil.hx +++ b/source/flx3d/Flx3DUtil.hx @@ -1,4 +1,4 @@ -package flx3D; +package flx3d; import away3d.core.managers.Stage3DManager; import away3d.library.assets.IAsset; diff --git a/source/flx3d/Flx3DView.hx b/source/flx3d/Flx3DView.hx index e611a5e5..abc9f52c 100644 --- a/source/flx3d/Flx3DView.hx +++ b/source/flx3d/Flx3DView.hx @@ -1,7 +1,7 @@ package flx3d; import flixel.FlxG; -import flx3D.Flx3DUtil; +import flx3d.Flx3DUtil; import away3d.library.assets.Asset3DType; import away3d.events.Asset3DEvent; import away3d.loaders.parsers.*; diff --git a/source/flx3d/FlxView3D.hx b/source/flx3d/FlxView3D.hx index f2726dd4..a70713b1 100644 --- a/source/flx3d/FlxView3D.hx +++ b/source/flx3d/FlxView3D.hx @@ -1,4 +1,4 @@ -package flx3D; +package flx3d; import away3d.containers.View3D; import away3d.library.assets.IAsset;