From 963e36c274dab82ffaea5d5416e4f2ba7b6b30d2 Mon Sep 17 00:00:00 2001 From: Yoshman29 <81191621+YoshiCrafter29@users.noreply.github.com> Date: Fri, 9 Dec 2022 22:11:34 +0100 Subject: [PATCH] some away3d stuff im never going to touch again --- buildnumber.txt | 2 +- source/funkin/away3d/Flx3DObject.hx | 5 ++ source/funkin/away3d/Flx3DView.hx | 113 ++++++++++++++++++++++++++++ source/funkin/away3d/FlxMesh.hx | 30 ++++++++ source/funkin/away3d/README.txt | 1 + source/funkin/system/Main.hx | 2 + source/funkin/system/Paths.hx | 4 + 7 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 source/funkin/away3d/Flx3DObject.hx create mode 100644 source/funkin/away3d/Flx3DView.hx create mode 100644 source/funkin/away3d/FlxMesh.hx create mode 100644 source/funkin/away3d/README.txt diff --git a/buildnumber.txt b/buildnumber.txt index 9d8d09f2..7f06f5c9 100644 --- a/buildnumber.txt +++ b/buildnumber.txt @@ -1 +1 @@ -1438 \ No newline at end of file +1445 \ No newline at end of file diff --git a/source/funkin/away3d/Flx3DObject.hx b/source/funkin/away3d/Flx3DObject.hx new file mode 100644 index 00000000..b1643625 --- /dev/null +++ b/source/funkin/away3d/Flx3DObject.hx @@ -0,0 +1,5 @@ +package funkin.away3d; + +interface Flx3DObject { + public function destroy():Void; +} \ No newline at end of file diff --git a/source/funkin/away3d/Flx3DView.hx b/source/funkin/away3d/Flx3DView.hx new file mode 100644 index 00000000..f3cc868f --- /dev/null +++ b/source/funkin/away3d/Flx3DView.hx @@ -0,0 +1,113 @@ +package funkin.away3d; + +import away3d.loaders.parsers.OBJParser; +import away3d.events.Asset3DEvent; +import openfl.utils.ByteArray; +import flixel.util.typeLimit.OneOfTwo; +import away3d.entities.Mesh; +import openfl.utils.Assets; +import hscript.Bytes; +import openfl.display.Sprite; +import flixel.FlxG; +import flixel.FlxObject; +import away3d.containers.View3D; +import away3d.loaders.Loader3D; +import away3d.loaders.misc.AssetLoaderToken; +import away3d.loaders.misc.AssetLoaderContext; +import openfl.display.BitmapData; +import flixel.FlxSprite; +import haxe.io.Path; + +class Flx3DView extends FlxSprite { + // STATIC STUFF + #if REGION + private static var __sprite:Sprite; + + public static function init() { + __sprite = new Sprite(); + FlxG.stage.addChild(__sprite); + } + #end + + private var __bitmap:BitmapData; + private var __loader:Loader3D = new Loader3D(); + + public var view3d:View3D; + + public var meshes:Array = []; + + public function new(x:Float, y:Float, width:Int = 0, height:Int = 0) { + super(x, y); + __loader.addEventListener(Asset3DEvent.ASSET_COMPLETE, assetComplete); + view3d = new View3D(); + view3d.scene.addChild(__loader); + + if (width <= 0) width = FlxG.width; + if (height <= 0) height = FlxG.height; + + resizeBitmap(width, height); + + __sprite.addChild(view3d); + } + + public function assetComplete(event:Asset3DEvent) { + + } + + public function loadMesh(asset:String, callback:Mesh->Void) { + try { + var meshContent:ByteArrayData = Assets.getBytes(asset); + + if (meshContent == null) + throw "Mesh Asset is null or does not exist."; + + var context = new AssetLoaderContext(); + + var mtlPath = '${Path.withoutExtension(asset)}.mtl'; + trace(Path.withoutDirectory(mtlPath)); + context.mapUrlToData(Path.withoutDirectory(mtlPath), Assets.getBytes(mtlPath)); + + var token = __loader.loadData(meshContent, context, null, new OBJParser()); + token.addEventListener(Asset3DEvent.MESH_COMPLETE, function(ev:Asset3DEvent) { + if (ev.asset is Mesh) { + var mesh:Mesh = cast ev.asset; + addMesh(mesh); + callback(mesh); + } + }); + } catch(e) { + trace(e.details()); + } + } + + public function addMesh(mesh:Mesh) { + if (mesh == null) return; + + meshes.push(mesh); + } + + + private function resizeBitmap(width:Int, height:Int) { + if (__bitmap != null) + __bitmap.dispose(); + __bitmap = new BitmapData(width, height, true, 0); + loadGraphic(__bitmap); + } + + public override function draw() { + var filters = FlxG.game.filters; + FlxG.game.filters = null; + view3d.renderer.queueSnapshot(__bitmap); + view3d.render(); + FlxG.game.filters = filters; + + super.draw(); + } + + public override function destroy() { + __sprite.removeChild(view3d); + + view3d.dispose(); + super.destroy(); + } +} \ No newline at end of file diff --git a/source/funkin/away3d/FlxMesh.hx b/source/funkin/away3d/FlxMesh.hx new file mode 100644 index 00000000..e7e95da3 --- /dev/null +++ b/source/funkin/away3d/FlxMesh.hx @@ -0,0 +1,30 @@ +package funkin.away3d; + +import away3d.core.base.Geometry; +import flixel.util.FlxDestroyUtil.IFlxDestroyable; +import away3d.loaders.Loader3D; +import haxe.io.Bytes; +import flixel.util.typeLimit.OneOfTwo; +import away3d.entities.Mesh; + +class FlxMesh extends Mesh implements IFlxDestroyable implements Flx3DObject { + var __loader:Loader3D = new Loader3D(); + public function new(asset:OneOfTwo, textures:Array = null) { + if (textures == null) textures = []; + + var meshContent:Bytes = null; + if (asset is String) + meshContent = Assets.getBytes(asset); + else if (asset is Bytes) + meshContent = cast(asset, Bytes); + if (meshContent == null) + throw "Mesh Asset is null or does not exist."; + + __loader.loadData(meshContent); + super(__loader.loadData()); + } + + public function destroy() { + loader.dispose(); + } +} \ No newline at end of file diff --git a/source/funkin/away3d/README.txt b/source/funkin/away3d/README.txt new file mode 100644 index 00000000..cfc84409 --- /dev/null +++ b/source/funkin/away3d/README.txt @@ -0,0 +1 @@ +ISNT DONE YET! EARLY BETA! DO NOT USE! \ No newline at end of file diff --git a/source/funkin/system/Main.hx b/source/funkin/system/Main.hx index c1d286c9..7232e0fd 100644 --- a/source/funkin/system/Main.hx +++ b/source/funkin/system/Main.hx @@ -1,5 +1,6 @@ package funkin.system; +import funkin.away3d.Flx3DView; import openfl.utils.AssetLibrary; import openfl.utils.AssetCache; import openfl.text.TextFormat; @@ -135,6 +136,7 @@ class Main extends Sprite Logs.init(); Paths.init(); ModsFolder.init(); + Flx3DView.init(); #if MOD_SUPPORT ModsFolder.switchMod("introMod"); #end diff --git a/source/funkin/system/Paths.hx b/source/funkin/system/Paths.hx index e21306b1..d91d78ff 100644 --- a/source/funkin/system/Paths.hx +++ b/source/funkin/system/Paths.hx @@ -172,6 +172,10 @@ class Paths #end } + inline static public function obj(key:String) { + return getPath('models/$key.obj', BINARY, null); + } + inline static public function getSparrowAtlas(key:String, ?library:String) { return FlxAtlasFrames.fromSparrow(image(key, library), file('images/$key.xml', library));