diff --git a/bin/AlternativaEditor.swf b/bin/AlternativaEditor.swf index aa0788b..f113a59 100644 Binary files a/bin/AlternativaEditor.swf and b/bin/AlternativaEditor.swf differ diff --git a/src/AlternativaEditor.mxml b/src/AlternativaEditor.mxml index 5be88dc..2e5dfb9 100644 --- a/src/AlternativaEditor.mxml +++ b/src/AlternativaEditor.mxml @@ -439,7 +439,8 @@ private function newLevel():void { this.mainScene.clear(); - this.cursorScene.updateMaterial(); + //this.cursorScene.updateMaterial(); + this.cursorScene.clear(); } private function onImportFromTanksClick(param1:NativeMenuItem):void @@ -526,7 +527,8 @@ { this.mainScene.clear(); this.mainScene.changed = true; - this.cursorScene.updateMaterial(); + //this.cursorScene.updateMaterial(); + this.cursorScene.clear(); } private function onSelectAll(param1:NativeMenuItem):void diff --git a/src/alternativa/editor/Preview.as b/src/alternativa/editor/Preview.as index 52891ad..99c69a5 100644 --- a/src/alternativa/editor/Preview.as +++ b/src/alternativa/editor/Preview.as @@ -63,8 +63,7 @@ package alternativa.editor this.cameraContainer.addChild(this.camera); this.camera.setPositionXYZ(0,-100,40); this.scene.root.addChild(this.cameraContainer); - this.view = new View(100,100); - this.camera.view = this.view; + this.view = new View(this.camera,100,100); addChild(this.view); //this.view.graphics.beginFill(16777215); //this.view.graphics.drawRect(0,0,1,1); diff --git a/src/alternativa/editor/scene/CursorScene.as b/src/alternativa/editor/scene/CursorScene.as index 13cbf69..7757b20 100644 --- a/src/alternativa/editor/scene/CursorScene.as +++ b/src/alternativa/editor/scene/CursorScene.as @@ -82,6 +82,7 @@ package alternativa.editor.scene this.containerController.object = this.container; this.containerController.mouseEnabled = false; this.container.addChild(this.mainScene.camera); + this.container.mouseChildren = false; } public function set object(param1:Prop) : void @@ -219,7 +220,7 @@ package alternativa.editor.scene { if(this._object) { - if(this.mainScene.root.getChildByName(this._object.name)) + if(this.mainScene.root.contains(this._object)) { this.mainScene.root.removeChild(this._object); } diff --git a/src/alternativa/editor/scene/EditorScene.as b/src/alternativa/editor/scene/EditorScene.as index dd5fc31..070e0b2 100644 --- a/src/alternativa/editor/scene/EditorScene.as +++ b/src/alternativa/editor/scene/EditorScene.as @@ -65,12 +65,12 @@ package alternativa.editor.scene protected function initScene() : void { root = new Object3DContainer(); + root.name = "EditorRoot"; this.camera = new Camera3D(); this.camera.rotationX = -MathUtils.DEG90 - MathUtils.DEG30; this.camera.setPositionXYZ(250,-7800,4670); //root.addChild(this.camera); - this.view = new View(100,100); - this.camera.view = view; + this.view = new View(this.camera,100,100); this.view.interactive = true; this.view.buttonMode = true; this.view.useHandCursor = false; diff --git a/src/alternativa/engine3d/core/Camera3D.as b/src/alternativa/engine3d/core/Camera3D.as index 20abf08..9e69a62 100644 --- a/src/alternativa/engine3d/core/Camera3D.as +++ b/src/alternativa/engine3d/core/Camera3D.as @@ -52,7 +52,7 @@ package alternativa.engine3d.core private static const constantsVertexBuffer:VertexBufferResource = createConstantsVertexBuffer((constantsMaxTriangles * 3)); private static const constantsIndexBuffer:IndexBufferResource = createConstantsIndexBuffer((constantsMaxTriangles * 3)); - public var view:View; + alternativa3d var view:View; public var fov:Number = 1.5707963267949; public var nearClipping:Number = 1; public var farClipping:Number = 1000000; @@ -197,6 +197,7 @@ package alternativa.engine3d.core this.lastWrapper = this.firstWrapper; this.lastVertex = this.firstVertex; this.lastFace = this.firstFace; + this.mouseEnabled = false; super(); } diff --git a/src/alternativa/engine3d/core/Object3D.as b/src/alternativa/engine3d/core/Object3D.as index 54a1d5a..c3cce6d 100644 --- a/src/alternativa/engine3d/core/Object3D.as +++ b/src/alternativa/engine3d/core/Object3D.as @@ -228,6 +228,13 @@ package alternativa.engine3d.core public function get transformation() : Matrix4 { tA.composeMatrixFromSource(this); + var _local_1:Object3D = this; + while (_local_1._parent != null) + { + _local_1 = _local_1._parent; + tB.composeMatrixFromSource(_local_1); + tA.appendMatrix(tB); + }; return new Matrix4(tA.ma, tA.mb, tA.mc, tA.md, tA.me, tA.mf, tA.mg, tA.mh, tA.mi, tA.mj, tA.mk, tA.ml); } @@ -261,7 +268,7 @@ package alternativa.engine3d.core return (new Matrix3D(Vector.([tA.ma, tA.me, tA.mi, 0, tA.mb, tA.mf, tA.mj, 0, tA.mc, tA.mg, tA.mk, 0, tA.md, tA.mh, tA.ml, 1]))); } - public function localToGlobal(_arg_1:Vector3D):Vector3D + public function localToGlobal(_arg_1:Vector3D, vector:Boolean = false):Vector3D { tA.composeMatrixFromSource(this); var _local_2:Object3D = this; @@ -272,9 +279,15 @@ package alternativa.engine3d.core tA.appendMatrix(tB); }; var _local_3:Vector3D = new Vector3D(); - _local_3.x = ((((tA.ma * _arg_1.x) + (tA.mb * _arg_1.y)) + (tA.mc * _arg_1.z)) + tA.md); - _local_3.y = ((((tA.me * _arg_1.x) + (tA.mf * _arg_1.y)) + (tA.mg * _arg_1.z)) + tA.mh); - _local_3.z = ((((tA.mi * _arg_1.x) + (tA.mj * _arg_1.y)) + (tA.mk * _arg_1.z)) + tA.ml); + _local_3.x = (((tA.ma * _arg_1.x) + (tA.mb * _arg_1.y)) + (tA.mc * _arg_1.z)); + _local_3.y = (((tA.me * _arg_1.x) + (tA.mf * _arg_1.y)) + (tA.mg * _arg_1.z)); + _local_3.z = (((tA.mi * _arg_1.x) + (tA.mj * _arg_1.y)) + (tA.mk * _arg_1.z)); + if(!vector) + { + _local_3.x += tA.md; + _local_3.y += tA.mh; + _local_3.z += tA.ml; + } return (_local_3); } diff --git a/src/alternativa/engine3d/core/Object3DContainer.as b/src/alternativa/engine3d/core/Object3DContainer.as index 7b82151..200f5b9 100644 --- a/src/alternativa/engine3d/core/Object3DContainer.as +++ b/src/alternativa/engine3d/core/Object3DContainer.as @@ -421,25 +421,29 @@ package alternativa.engine3d.core var _local_9:Object3D = this.childrenList; while (_local_9 != null) { - _local_9.composeMatrix(); - _local_9.invertMatrix(); - if (_local_5 == null) + var cont:Object3DContainer = _local_9 as Object3DContainer; + if((cont == null && _local_9.mouseEnabled) || (cont && cont.mouseChildren)) { - _local_5 = new Vector3D(); - _local_6 = new Vector3D(); - }; - _local_5.x = ((((_local_9.ma * _arg_1.x) + (_local_9.mb * _arg_1.y)) + (_local_9.mc * _arg_1.z)) + _local_9.md); - _local_5.y = ((((_local_9.me * _arg_1.x) + (_local_9.mf * _arg_1.y)) + (_local_9.mg * _arg_1.z)) + _local_9.mh); - _local_5.z = ((((_local_9.mi * _arg_1.x) + (_local_9.mj * _arg_1.y)) + (_local_9.mk * _arg_1.z)) + _local_9.ml); - _local_6.x = (((_local_9.ma * _arg_2.x) + (_local_9.mb * _arg_2.y)) + (_local_9.mc * _arg_2.z)); - _local_6.y = (((_local_9.me * _arg_2.x) + (_local_9.mf * _arg_2.y)) + (_local_9.mg * _arg_2.z)); - _local_6.z = (((_local_9.mi * _arg_2.x) + (_local_9.mj * _arg_2.y)) + (_local_9.mk * _arg_2.z)); - _local_10 = _local_9.intersectRay(_local_5, _local_6, _arg_3, _arg_4); - if (((!(_local_10 == null)) && (_local_10.time < _local_8))) - { - _local_8 = _local_10.time; - _local_7 = _local_10; - }; + _local_9.composeMatrix(); + _local_9.invertMatrix(); + if (_local_5 == null) + { + _local_5 = new Vector3D(); + _local_6 = new Vector3D(); + }; + _local_5.x = ((((_local_9.ma * _arg_1.x) + (_local_9.mb * _arg_1.y)) + (_local_9.mc * _arg_1.z)) + _local_9.md); + _local_5.y = ((((_local_9.me * _arg_1.x) + (_local_9.mf * _arg_1.y)) + (_local_9.mg * _arg_1.z)) + _local_9.mh); + _local_5.z = ((((_local_9.mi * _arg_1.x) + (_local_9.mj * _arg_1.y)) + (_local_9.mk * _arg_1.z)) + _local_9.ml); + _local_6.x = (((_local_9.ma * _arg_2.x) + (_local_9.mb * _arg_2.y)) + (_local_9.mc * _arg_2.z)); + _local_6.y = (((_local_9.me * _arg_2.x) + (_local_9.mf * _arg_2.y)) + (_local_9.mg * _arg_2.z)); + _local_6.z = (((_local_9.mi * _arg_2.x) + (_local_9.mj * _arg_2.y)) + (_local_9.mk * _arg_2.z)); + _local_10 = _local_9.intersectRay(_local_5, _local_6, _arg_3, _arg_4); + if (((!(_local_10 == null)) && (_local_10.time < _local_8))) + { + _local_8 = _local_10.time; + _local_7 = _local_10; + }; + } _local_9 = _local_9.next; }; return (_local_7); diff --git a/src/alternativa/engine3d/core/View.as b/src/alternativa/engine3d/core/View.as index a8cdb61..bf13421 100644 --- a/src/alternativa/engine3d/core/View.as +++ b/src/alternativa/engine3d/core/View.as @@ -24,6 +24,12 @@ package alternativa.engine3d.core{ import __AS3__.vec.*; import alternativa.engine3d.alternativa3d; import flash.display3D.Context3D; + import mx.controls.Alert; + import flash.geom.Vector3D; + import alternativa.math.Quaternion; + import alternativa.engine3d.primitives.Box; + import alternativa.engine3d.materials.FillMaterial; + import alternativa.math.Matrix3; use namespace alternativa3d; @@ -39,6 +45,9 @@ package alternativa.engine3d.core{ private static var views:Vector. = new Vector.(); private static var configured:Boolean = false; private static var cleared:Boolean = true; + + private static const tmpVec1:Vector3D = new Vector3D(); + private static const tmpMatrix3:Matrix3 = new Matrix3(); private var presented:Boolean = false; private var globalCoords:Point; @@ -75,10 +84,12 @@ package alternativa.engine3d.core{ private var _interactive:Boolean = false; - public function View(width:Number, height:Number, constrainedMode:Boolean=false){ + public function View(camera:Camera3D, width:Number, height:Number, constrainedMode:Boolean=false){ super(); this._width = width; this._height = height; + this.camera = camera; + camera.view = this; this.constrained = constrainedMode; mouseEnabled = true; mouseChildren = true; @@ -289,6 +300,7 @@ package alternativa.engine3d.core{ } private function onMouseMove(_arg_1:MouseEvent):void{ + return; this.onMouse(_arg_1); this.defineTarget(_arg_1); if (this.target != null) @@ -603,72 +615,66 @@ package alternativa.engine3d.core{ }; } - private function defineTarget(_arg_1:MouseEvent):void{ - var _local_2:Object3D; - var _local_3:Object3D; - var _local_6:Canvas; - var _local_7:DisplayObject; - var _local_8:Object3D; - var _local_9:Object3D; + private function defineTarget(_arg_1:MouseEvent):void + { mouse.x = _arg_1.localX; mouse.y = _arg_1.localY; - var _local_4:Array = ((stage != null) ? stage.getObjectsUnderPoint(localToGlobal(mouse)) : super.getObjectsUnderPoint(mouse)); - var _local_5:int = (_local_4.length - 1); - while (_local_5 >= 0) + + //idk how to make this to work, pls help + + this.target = null; + + var root:Object3DContainer = this.camera.parent; + + while(root != null) { - _local_6 = null; - _local_7 = _local_4[_local_5]; - while (_local_7.parent != stage) - { - _local_6 = (_local_7 as Canvas); - if (_local_6 != null) break; - _local_7 = _local_7.parent; - }; - if (_local_6 != null) - { - if (_local_3 != null) - { - _local_8 = null; - _local_9 = _local_3; - while (_local_9 != null) - { - if (((_local_9 is Object3DContainer) && (!(Object3DContainer(_local_9).mouseChildren)))) - { - _local_8 = null; - }; - if (((_local_8 == null) && (_local_9.mouseEnabled))) - { - _local_8 = _local_9; - }; - _local_9 = _local_9._parent; - }; - if (_local_8 != null) - { - if (this.target != null) - { - _local_9 = _local_8; - while (_local_9 != null) - { - if (_local_9 == this.target) - { - _local_2 = _local_3; - this.target = _local_8; - break; - }; - _local_9 = _local_9._parent; - }; - } - else - { - _local_2 = _local_3; - this.target = _local_8; - }; - if (_local_2 == this.target) break; - }; - }; - }; - _local_5--; - }; + var parent:Object3DContainer = root.parent; + if(parent == null) + break; + if(!parent.mouseChildren) + continue; + root = parent; + } + if(!root.mouseChildren) + return; + + //ErrorHandler.clearMessages(); + + tmpVec1.setTo(mouse.x,mouse.y,0); + var origin:Vector3D = this.camera.projectGlobal(tmpVec1); + + tmpVec1.setTo(0,0,1); + + //ErrorHandler.addText("local dir: " + tmpVec1); + + var dir:Vector3D = camera.localToGlobal(tmpVec1,true); + + //debugging: + /*ErrorHandler.addText("ray: origin="+origin + " ; dir=" + dir); + + var box:Box = new Box(10,10,30); + box.setMaterialToAllFaces(new FillMaterial(0xff0000)); + root.addChild(box); + + tmpMatrix3.setDirectionVector(dir); + var rotation:Vector3D = new Vector3D(); + tmpMatrix3.getEulerAngles(rotation); + + box.setPositionXYZ(origin.x,origin.y,origin.z); + box.setRotationXYZ(rotation.x,rotation.y,rotation.z);*/ + + var rayHit:RayIntersectionData = root.intersectRay(origin, dir, null, this.camera); + + /*setTimeout(function():void{ + ErrorHandler.showWindow(); + },10);*/ + + if(rayHit == null) + return; + + this.target = rayHit.object; + + //ErrorHandler.addText("hit: " + rayHit); } override public function getObjectsUnderPoint(_arg_1:Point):Array{ diff --git a/src/alternativa/math/Matrix3.as b/src/alternativa/math/Matrix3.as new file mode 100644 index 0000000..d4e2980 --- /dev/null +++ b/src/alternativa/math/Matrix3.as @@ -0,0 +1,420 @@ +package alternativa.math +{ + import alternativa.engine3d.core.Object3D; + import flash.geom.Vector3D; + import flash.utils.getQualifiedClassName; + + public class Matrix3 + { + + public static const ZERO:Matrix3 = new Matrix3(0,0,0,0,0,0,0,0,0); + + public static const IDENTITY:Matrix3 = new Matrix3(); + + private static const xAxis:Vector3D = new Vector3D(); + + private static const yAxis:Vector3D = new Vector3D(); + + private static const zAxis:Vector3D = new Vector3D(); + + + public static const V_X_AXIS:Vector3D = new Vector3D(1,0,0); + + public static const V_Y_AXIS:Vector3D = new Vector3D(0,1,0); + + public static const V_Z_AXIS:Vector3D = new Vector3D(0,0,1); + + + public var m00:Number; + + public var m01:Number; + + public var m02:Number; + + public var m10:Number; + + public var m11:Number; + + public var m12:Number; + + public var m20:Number; + + public var m21:Number; + + public var m22:Number; + + public function Matrix3(param1:Number = 1, param2:Number = 0, param3:Number = 0, param4:Number = 0, param5:Number = 1, param6:Number = 0, param7:Number = 0, param8:Number = 0, param9:Number = 1) + { + super(); + this.m00 = param1; + this.m01 = param2; + this.m02 = param3; + this.m10 = param4; + this.m11 = param5; + this.m12 = param6; + this.m20 = param7; + this.m21 = param8; + this.m22 = param9; + } + + public function toIdentity() : Matrix3 + { + this.m00 = this.m11 = this.m22 = 1; + this.m01 = this.m02 = this.m10 = this.m12 = this.m20 = this.m21 = 0; + return this; + } + + public function invert() : Matrix3 + { + var _loc1_:Number = this.m00; + var _loc2_:Number = this.m01; + var _loc3_:Number = this.m02; + var _loc4_:Number = this.m10; + var _loc5_:Number = this.m11; + var _loc6_:Number = this.m12; + var _loc7_:Number = this.m20; + var _loc8_:Number = this.m21; + var _loc9_:Number = this.m22; + var _loc10_:Number = 1 / (-_loc3_ * _loc5_ * _loc7_ + _loc2_ * _loc6_ * _loc7_ + _loc3_ * _loc4_ * _loc8_ - _loc1_ * _loc6_ * _loc8_ - _loc2_ * _loc4_ * _loc9_ + _loc1_ * _loc5_ * _loc9_); + this.m00 = (_loc5_ * _loc9_ - _loc6_ * _loc8_) * _loc10_; + this.m01 = (_loc3_ * _loc8_ - _loc2_ * _loc9_) * _loc10_; + this.m02 = (_loc2_ * _loc6_ - _loc3_ * _loc5_) * _loc10_; + this.m10 = (_loc6_ * _loc7_ - _loc4_ * _loc9_) * _loc10_; + this.m11 = (_loc1_ * _loc9_ - _loc3_ * _loc7_) * _loc10_; + this.m12 = (_loc3_ * _loc4_ - _loc1_ * _loc6_) * _loc10_; + this.m20 = (_loc4_ * _loc8_ - _loc5_ * _loc7_) * _loc10_; + this.m21 = (_loc2_ * _loc7_ - _loc1_ * _loc8_) * _loc10_; + this.m22 = (_loc1_ * _loc5_ - _loc2_ * _loc4_) * _loc10_; + return this; + } + + public function append(param1:Matrix3) : Matrix3 + { + var _loc2_:Number = this.m00; + var _loc3_:Number = this.m01; + var _loc4_:Number = this.m02; + var _loc5_:Number = this.m10; + var _loc6_:Number = this.m11; + var _loc7_:Number = this.m12; + var _loc8_:Number = this.m20; + var _loc9_:Number = this.m21; + var _loc10_:Number = this.m22; + this.m00 = param1.m00 * _loc2_ + param1.m01 * _loc5_ + param1.m02 * _loc8_; + this.m01 = param1.m00 * _loc3_ + param1.m01 * _loc6_ + param1.m02 * _loc9_; + this.m02 = param1.m00 * _loc4_ + param1.m01 * _loc7_ + param1.m02 * _loc10_; + this.m10 = param1.m10 * _loc2_ + param1.m11 * _loc5_ + param1.m12 * _loc8_; + this.m11 = param1.m10 * _loc3_ + param1.m11 * _loc6_ + param1.m12 * _loc9_; + this.m12 = param1.m10 * _loc4_ + param1.m11 * _loc7_ + param1.m12 * _loc10_; + this.m20 = param1.m20 * _loc2_ + param1.m21 * _loc5_ + param1.m22 * _loc8_; + this.m21 = param1.m20 * _loc3_ + param1.m21 * _loc6_ + param1.m22 * _loc9_; + this.m22 = param1.m20 * _loc4_ + param1.m21 * _loc7_ + param1.m22 * _loc10_; + return this; + } + + public function prepend(param1:Matrix3) : Matrix3 + { + var _loc2_:Number = this.m00; + var _loc3_:Number = this.m01; + var _loc4_:Number = this.m02; + var _loc5_:Number = this.m10; + var _loc6_:Number = this.m11; + var _loc7_:Number = this.m12; + var _loc8_:Number = this.m20; + var _loc9_:Number = this.m21; + var _loc10_:Number = this.m22; + this.m00 = _loc2_ * param1.m00 + _loc3_ * param1.m10 + _loc4_ * param1.m20; + this.m01 = _loc2_ * param1.m01 + _loc3_ * param1.m11 + _loc4_ * param1.m21; + this.m02 = _loc2_ * param1.m02 + _loc3_ * param1.m12 + _loc4_ * param1.m22; + this.m10 = _loc5_ * param1.m00 + _loc6_ * param1.m10 + _loc7_ * param1.m20; + this.m11 = _loc5_ * param1.m01 + _loc6_ * param1.m11 + _loc7_ * param1.m21; + this.m12 = _loc5_ * param1.m02 + _loc6_ * param1.m12 + _loc7_ * param1.m22; + this.m20 = _loc8_ * param1.m00 + _loc9_ * param1.m10 + _loc10_ * param1.m20; + this.m21 = _loc8_ * param1.m01 + _loc9_ * param1.m11 + _loc10_ * param1.m21; + this.m22 = _loc8_ * param1.m02 + _loc9_ * param1.m12 + _loc10_ * param1.m22; + return this; + } + + public function prependTransposed(param1:Matrix3) : Matrix3 + { + var _loc2_:Number = this.m00; + var _loc3_:Number = this.m01; + var _loc4_:Number = this.m02; + var _loc5_:Number = this.m10; + var _loc6_:Number = this.m11; + var _loc7_:Number = this.m12; + var _loc8_:Number = this.m20; + var _loc9_:Number = this.m21; + var _loc10_:Number = this.m22; + this.m00 = _loc2_ * param1.m00 + _loc3_ * param1.m01 + _loc4_ * param1.m02; + this.m01 = _loc2_ * param1.m10 + _loc3_ * param1.m11 + _loc4_ * param1.m12; + this.m02 = _loc2_ * param1.m20 + _loc3_ * param1.m21 + _loc4_ * param1.m22; + this.m10 = _loc5_ * param1.m00 + _loc6_ * param1.m01 + _loc7_ * param1.m02; + this.m11 = _loc5_ * param1.m10 + _loc6_ * param1.m11 + _loc7_ * param1.m12; + this.m12 = _loc5_ * param1.m20 + _loc6_ * param1.m21 + _loc7_ * param1.m22; + this.m20 = _loc8_ * param1.m00 + _loc9_ * param1.m01 + _loc10_ * param1.m02; + this.m21 = _loc8_ * param1.m10 + _loc9_ * param1.m11 + _loc10_ * param1.m12; + this.m22 = _loc8_ * param1.m20 + _loc9_ * param1.m21 + _loc10_ * param1.m22; + return this; + } + + public function add(param1:Matrix3) : Matrix3 + { + this.m00 += param1.m00; + this.m01 += param1.m01; + this.m02 += param1.m02; + this.m10 += param1.m10; + this.m11 += param1.m11; + this.m12 += param1.m12; + this.m20 += param1.m20; + this.m21 += param1.m21; + this.m22 += param1.m22; + return this; + } + + public function subtract(param1:Matrix3) : Matrix3 + { + this.m00 -= param1.m00; + this.m01 -= param1.m01; + this.m02 -= param1.m02; + this.m10 -= param1.m10; + this.m11 -= param1.m11; + this.m12 -= param1.m12; + this.m20 -= param1.m20; + this.m21 -= param1.m21; + this.m22 -= param1.m22; + return this; + } + + public function transpose() : Matrix3 + { + var _loc1_:Number = this.m01; + this.m01 = this.m10; + this.m10 = _loc1_; + _loc1_ = this.m02; + this.m02 = this.m20; + this.m20 = _loc1_; + _loc1_ = this.m12; + this.m12 = this.m21; + this.m21 = _loc1_; + return this; + } + + public function transformVector(param1:Vector3D, param2:Vector3D) : void + { + param2.x = this.m00 * param1.x + this.m01 * param1.y + this.m02 * param1.z; + param2.y = this.m10 * param1.x + this.m11 * param1.y + this.m12 * param1.z; + param2.z = this.m20 * param1.x + this.m21 * param1.y + this.m22 * param1.z; + } + + public function transformVectorInverse(param1:Vector3D, param2:Vector3D) : void + { + param2.x = this.m00 * param1.x + this.m10 * param1.y + this.m20 * param1.z; + param2.y = this.m01 * param1.x + this.m11 * param1.y + this.m21 * param1.z; + param2.z = this.m02 * param1.x + this.m12 * param1.y + this.m22 * param1.z; + } + + public function transformVector3To3D(param1:Vector3D, param2:Vector3D) : void + { + param2.x = this.m00 * param1.x + this.m01 * param1.y + this.m02 * param1.z; + param2.y = this.m10 * param1.x + this.m11 * param1.y + this.m12 * param1.z; + param2.z = this.m20 * param1.x + this.m21 * param1.y + this.m22 * param1.z; + } + + public function createSkewSymmetric(param1:Vector3D) : Matrix3 + { + this.m00 = this.m11 = this.m22 = 0; + this.m01 = -param1.z; + this.m02 = param1.y; + this.m10 = param1.z; + this.m12 = -param1.x; + this.m20 = -param1.y; + this.m21 = param1.x; + return this; + } + + public function copy(param1:Matrix3) : Matrix3 + { + this.m00 = param1.m00; + this.m01 = param1.m01; + this.m02 = param1.m02; + this.m10 = param1.m10; + this.m11 = param1.m11; + this.m12 = param1.m12; + this.m20 = param1.m20; + this.m21 = param1.m21; + this.m22 = param1.m22; + return this; + } + + public function setRotationMatrix(param1:Number, param2:Number, param3:Number) : Matrix3 + { + var _loc4_:Number = Math.cos(param1); + var _loc5_:Number = Math.sin(param1); + var _loc6_:Number = Math.cos(param2); + var _loc7_:Number = Math.sin(param2); + var _loc8_:Number = Math.cos(param3); + var _loc9_:Number = Math.sin(param3); + var _loc10_:Number = _loc8_ * _loc7_; + var _loc11_:Number = _loc9_ * _loc7_; + this.m00 = _loc8_ * _loc6_; + this.m01 = _loc10_ * _loc5_ - _loc9_ * _loc4_; + this.m02 = _loc10_ * _loc4_ + _loc9_ * _loc5_; + this.m10 = _loc9_ * _loc6_; + this.m11 = _loc11_ * _loc5_ + _loc8_ * _loc4_; + this.m12 = _loc11_ * _loc4_ - _loc8_ * _loc5_; + this.m20 = -_loc7_; + this.m21 = _loc6_ * _loc5_; + this.m22 = _loc6_ * _loc4_; + return this; + } + + public function setRotationMatrixForObject3D(param1:Object3D) : void + { + this.setRotationMatrix(param1.rotationX,param1.rotationY,param1.rotationZ); + } + + public function fromAxisAngle(param1:Vector3D, param2:Number) : void + { + var _loc3_:Number = Math.cos(param2); + var _loc4_:Number = Math.sin(param2); + var _loc5_:Number = 1 - _loc3_; + var _loc6_:Number = param1.x; + var _loc7_:Number = param1.y; + var _loc8_:Number = param1.z; + this.m00 = _loc5_ * _loc6_ * _loc6_ + _loc3_; + this.m01 = _loc5_ * _loc6_ * _loc7_ - _loc8_ * _loc4_; + this.m02 = _loc5_ * _loc6_ * _loc8_ + _loc7_ * _loc4_; + this.m10 = _loc5_ * _loc6_ * _loc7_ + _loc8_ * _loc4_; + this.m11 = _loc5_ * _loc7_ * _loc7_ + _loc3_; + this.m12 = _loc5_ * _loc7_ * _loc8_ - _loc6_ * _loc4_; + this.m20 = _loc5_ * _loc6_ * _loc8_ - _loc7_ * _loc4_; + this.m21 = _loc5_ * _loc7_ * _loc8_ + _loc6_ * _loc4_; + this.m22 = _loc5_ * _loc8_ * _loc8_ + _loc3_; + } + + public function clone() : Matrix3 + { + return new Matrix3(this.m00,this.m01,this.m02,this.m10,this.m11,this.m12,this.m20,this.m21,this.m22); + } + + public function toString() : String + { + return getQualifiedClassName(this) + " (" + this.m00 + ", " + this.m01 + ", " + this.m02 + "), (" + this.m10 + ", " + this.m11 + ", " + this.m12 + "), (" + this.m20 + ", " + this.m21 + ", " + this.m22 + ")"; + } + + public function getEulerAngles(param1:Vector3D) : void + { + if(-1 < this.m20 && this.m20 < 1) + { + param1.x = Math.atan2(this.m21,this.m22); + param1.y = -Math.asin(this.m20); + param1.z = Math.atan2(this.m10,this.m00); + } + else + { + param1.x = 0; + param1.y = this.m20 <= -1 ? Math.PI : -Math.PI; + param1.y *= 0.5; + param1.z = Math.atan2(-this.m01,this.m11); + } + } + + public function getRight(param1:Vector3D) : void + { + this.getAxis(0,param1); + } + + public function getForward(param1:Vector3D) : void + { + this.getAxis(1,param1); + } + + public function getUp(param1:Vector3D) : void + { + this.getAxis(2,param1); + } + + public function getAxis(param1:int, param2:Vector3D) : void + { + switch(param1) + { + case 0: + param2.setTo(this.m00,this.m10,this.m20); + break; + case 1: + param2.setTo(this.m01,this.m11,this.m21); + break; + case 2: + param2.setTo(this.m02,this.m12,this.m22); + } + } + + public function setDirectionVector(param1:Vector3D) : void + { + yAxis.copyFrom(param1); + yAxis.normalize(); + if(yAxis.dotProduct(V_X_AXIS) < 0.9) + { + zAxis.crossProductToOutput(V_X_AXIS,yAxis); + } + else + { + zAxis.crossProductToOutput(yAxis,V_Y_AXIS); + } + zAxis.normalize(); + xAxis.crossProductToOutput(yAxis,zAxis).normalize(); + this.setAxis(xAxis,yAxis,zAxis); + } + + public function setAxis(param1:Vector3D, param2:Vector3D, param3:Vector3D) : void + { + this.m00 = param1.x; + this.m01 = param2.x; + this.m02 = param3.x; + this.m10 = param1.y; + this.m11 = param2.y; + this.m12 = param3.y; + this.m20 = param1.z; + this.m21 = param2.z; + this.m22 = param3.z; + } + + public function rotationMatrixToQuaternion(param1:Quaternion) : void + { + var _loc3_:Number = NaN; + var _loc2_:Number = this.m00 + this.m11 + this.m22; + if(_loc2_ > 0) + { + _loc3_ = Math.sqrt(_loc2_ + 1) * 2; + param1.w = 0.25 * _loc3_; + param1.x = (this.m21 - this.m12) / _loc3_; + param1.y = (this.m02 - this.m20) / _loc3_; + param1.z = (this.m10 - this.m01) / _loc3_; + } + else if(this.m00 > this.m11 && this.m00 > this.m22) + { + _loc3_ = Math.sqrt(1 + this.m00 - this.m11 - this.m22) * 2; + param1.w = (this.m21 - this.m12) / _loc3_; + param1.x = 0.25 * _loc3_; + param1.y = (this.m01 + this.m10) / _loc3_; + param1.z = (this.m02 + this.m20) / _loc3_; + } + else if(this.m11 > this.m22) + { + _loc3_ = Math.sqrt(1 + this.m11 - this.m00 - this.m22) * 2; + param1.w = (this.m02 - this.m20) / _loc3_; + param1.x = (this.m01 + this.m10) / _loc3_; + param1.y = 0.25 * _loc3_; + param1.z = (this.m12 + this.m21) / _loc3_; + } + else + { + _loc3_ = Math.sqrt(1 + this.m22 - this.m00 - this.m11) * 2; + param1.w = (this.m10 - this.m01) / _loc3_; + param1.x = (this.m02 + this.m20) / _loc3_; + param1.y = (this.m12 + this.m21) / _loc3_; + param1.z = 0.25 * _loc3_; + } + } + } +} diff --git a/src/alternativa/math/Quaternion.as b/src/alternativa/math/Quaternion.as new file mode 100644 index 0000000..e591346 --- /dev/null +++ b/src/alternativa/math/Quaternion.as @@ -0,0 +1,478 @@ +package alternativa.math +{ + import flash.geom.Vector3D; + import flash.utils.getQualifiedClassName; + + public class Quaternion + { + + private static const _q:Quaternion = new Quaternion(); + + + public var w:Number; + + public var x:Number; + + public var y:Number; + + public var z:Number; + + public function Quaternion(param1:Number = 1, param2:Number = 0, param3:Number = 0, param4:Number = 0) + { + super(); + this.w = param1; + this.x = param2; + this.y = param3; + this.z = param4; + } + + public static function multiply(param1:Quaternion, param2:Quaternion, param3:Quaternion) : void + { + param3.w = param1.w * param2.w - param1.x * param2.x - param1.y * param2.y - param1.z * param2.z; + param3.x = param1.w * param2.x + param1.x * param2.w + param1.y * param2.z - param1.z * param2.y; + param3.y = param1.w * param2.y + param1.y * param2.w + param1.z * param2.x - param1.x * param2.z; + param3.z = param1.w * param2.z + param1.z * param2.w + param1.x * param2.y - param1.y * param2.x; + } + + public static function createFromAxisAngle(param1:Vector3D, param2:Number) : Quaternion + { + var _loc3_:Quaternion = new Quaternion(); + _loc3_.setFromAxisAngle(param1,param2); + return _loc3_; + } + + public static function createFromAxisAngleComponents(param1:Number, param2:Number, param3:Number, param4:Number) : Quaternion + { + var _loc5_:Quaternion = new Quaternion(); + _loc5_.setFromAxisAngleComponents(param1,param2,param3,param4); + return _loc5_; + } + + public function reset(param1:Number = 1, param2:Number = 0, param3:Number = 0, param4:Number = 0) : Quaternion + { + this.w = param1; + this.x = param2; + this.y = param3; + this.z = param4; + return this; + } + + public function normalize() : Quaternion + { + var _loc1_:Number = this.w * this.w + this.x * this.x + this.y * this.y + this.z * this.z; + if(_loc1_ == 0) + { + this.w = 1; + } + else + { + _loc1_ = 1 / Math.sqrt(_loc1_); + this.w *= _loc1_; + this.x *= _loc1_; + this.y *= _loc1_; + this.z *= _loc1_; + } + return this; + } + + public function prepend(param1:Quaternion) : Quaternion + { + var _loc2_:Number = this.w * param1.w - this.x * param1.x - this.y * param1.y - this.z * param1.z; + var _loc3_:Number = this.w * param1.x + this.x * param1.w + this.y * param1.z - this.z * param1.y; + var _loc4_:Number = this.w * param1.y + this.y * param1.w + this.z * param1.x - this.x * param1.z; + var _loc5_:Number = this.w * param1.z + this.z * param1.w + this.x * param1.y - this.y * param1.x; + this.w = _loc2_; + this.x = _loc3_; + this.y = _loc4_; + this.z = _loc5_; + return this; + } + + public function append(param1:Quaternion) : Quaternion + { + var _loc2_:Number = param1.w * this.w - param1.x * this.x - param1.y * this.y - param1.z * this.z; + var _loc3_:Number = param1.w * this.x + param1.x * this.w + param1.y * this.z - param1.z * this.y; + var _loc4_:Number = param1.w * this.y + param1.y * this.w + param1.z * this.x - param1.x * this.z; + var _loc5_:Number = param1.w * this.z + param1.z * this.w + param1.x * this.y - param1.y * this.x; + this.w = _loc2_; + this.x = _loc3_; + this.y = _loc4_; + this.z = _loc5_; + return this; + } + + public function rotateByVector(param1:Vector3D) : Quaternion + { + var _loc2_:Number = -param1.x * this.x - param1.y * this.y - param1.z * this.z; + var _loc3_:Number = param1.x * this.w + param1.y * this.z - param1.z * this.y; + var _loc4_:Number = param1.y * this.w + param1.z * this.x - param1.x * this.z; + var _loc5_:Number = param1.z * this.w + param1.x * this.y - param1.y * this.x; + this.w = _loc2_; + this.x = _loc3_; + this.y = _loc4_; + this.z = _loc5_; + return this; + } + + public function addScaledVector(param1:Vector3D, param2:Number) : Quaternion + { + var _loc3_:Number = param1.x * param2; + var _loc4_:Number = param1.y * param2; + var _loc5_:Number = param1.z * param2; + var _loc6_:Number = -this.x * _loc3_ - this.y * _loc4_ - this.z * _loc5_; + var _loc7_:Number = _loc3_ * this.w + _loc4_ * this.z - _loc5_ * this.y; + var _loc8_:Number = _loc4_ * this.w + _loc5_ * this.x - _loc3_ * this.z; + var _loc9_:Number = _loc5_ * this.w + _loc3_ * this.y - _loc4_ * this.x; + this.w += 0.5 * _loc6_; + this.x += 0.5 * _loc7_; + this.y += 0.5 * _loc8_; + this.z += 0.5 * _loc9_; + var _loc10_:Number = this.w * this.w + this.x * this.x + this.y * this.y + this.z * this.z; + if(_loc10_ == 0) + { + this.w = 1; + } + else + { + _loc10_ = 1 / Math.sqrt(_loc10_); + this.w *= _loc10_; + this.x *= _loc10_; + this.y *= _loc10_; + this.z *= _loc10_; + } + return this; + } + + /*fpublic function toMatrix3(param1:Matrix3) : Quaternion + { + var _loc7_:Number = NaN; + var _loc8_:Number = NaN; + var _loc9_:Number = NaN; + var _loc2_:Number = 2 * this.x * this.x; + var _loc3_:Number = 2 * this.y * this.y; + var _loc4_:Number = 2 * this.z * this.z; + var _loc5_:Number = 2 * this.x * this.y; + var _loc6_:Number = 2 * this.y * this.z; + _loc7_ = 2 * this.z * this.x; + _loc8_ = 2 * this.w * this.x; + _loc9_ = 2 * this.w * this.y; + var _loc10_:Number = 2 * this.w * this.z; + param1.m00 = 1 - _loc3_ - _loc4_; + param1.m01 = _loc5_ - _loc10_; + param1.m02 = _loc7_ + _loc9_; + param1.m10 = _loc5_ + _loc10_; + param1.m11 = 1 - _loc2_ - _loc4_; + param1.m12 = _loc6_ - _loc8_; + param1.m20 = _loc7_ - _loc9_; + param1.m21 = _loc6_ + _loc8_; + param1.m22 = 1 - _loc2_ - _loc3_; + return this; + }*/ + + public function getYAxis(param1:Vector3D) : Vector3D + { + var _loc5_:Number = NaN; + var _loc2_:Number = 2 * this.x * this.x; + var _loc3_:Number = 2 * this.z * this.z; + var _loc4_:Number = 2 * this.x * this.y; + _loc5_ = 2 * this.y * this.z; + var _loc6_:Number = 2 * this.w * this.x; + var _loc7_:Number = 2 * this.w * this.z; + param1.x = _loc4_ - _loc7_; + param1.y = 1 - _loc2_ - _loc3_; + param1.z = _loc5_ + _loc6_; + return param1; + } + + public function getZAxis(param1:Vector3D) : Vector3D + { + var _loc2_:Number = NaN; + _loc2_ = 2 * this.x * this.x; + var _loc3_:Number = 2 * this.y * this.y; + var _loc4_:Number = 2 * this.y * this.z; + var _loc5_:Number = 2 * this.z * this.x; + var _loc6_:Number = 2 * this.w * this.x; + var _loc7_:Number = 2 * this.w * this.y; + param1.x = _loc5_ + _loc7_; + param1.y = _loc4_ - _loc6_; + param1.z = 1 - _loc2_ - _loc3_; + return param1; + } + + /*public function toMatrix4(param1:Matrix4) : Quaternion + { + var _loc2_:Number = NaN; + var _loc4_:Number = NaN; + var _loc6_:Number = NaN; + var _loc8_:Number = NaN; + _loc2_ = 2 * this.x * this.x; + var _loc3_:Number = 2 * this.y * this.y; + _loc4_ = 2 * this.z * this.z; + var _loc5_:Number = 2 * this.x * this.y; + _loc6_ = 2 * this.y * this.z; + var _loc7_:Number = 2 * this.z * this.x; + _loc8_ = 2 * this.w * this.x; + var _loc9_:Number = 2 * this.w * this.y; + var _loc10_:Number = 2 * this.w * this.z; + param1.m00 = 1 - _loc3_ - _loc4_; + param1.m01 = _loc5_ - _loc10_; + param1.m02 = _loc7_ + _loc9_; + param1.m10 = _loc5_ + _loc10_; + param1.m11 = 1 - _loc2_ - _loc4_; + param1.m12 = _loc6_ - _loc8_; + param1.m20 = _loc7_ - _loc9_; + param1.m21 = _loc6_ + _loc8_; + param1.m22 = 1 - _loc2_ - _loc3_; + return this; + }*/ + + public function length() : Number + { + return Math.sqrt(this.w * this.w + this.x * this.x + this.y * this.y + this.z * this.z); + } + + public function lengthSqr() : Number + { + return this.w * this.w + this.x * this.x + this.y * this.y + this.z * this.z; + } + + public function setFromAxisAngle(param1:Vector3D, param2:Number) : Quaternion + { + this.w = Math.cos(0.5 * param2); + var _loc3_:Number = Math.sin(0.5 * param2) / Math.sqrt(param1.x * param1.x + param1.y * param1.y + param1.z * param1.z); + this.x = param1.x * _loc3_; + this.y = param1.y * _loc3_; + this.z = param1.z * _loc3_; + return this; + } + + public function setFromAxisAngleComponents(param1:Number, param2:Number, param3:Number, param4:Number) : Quaternion + { + this.w = Math.cos(0.5 * param4); + var _loc5_:Number = Math.sin(0.5 * param4) / Math.sqrt(param1 * param1 + param2 * param2 + param3 * param3); + this.x = param1 * _loc5_; + this.y = param2 * _loc5_; + this.z = param3 * _loc5_; + return this; + } + + public function toAxisVector(param1:Vector3D = null) : Vector3D + { + var _loc2_:Number = NaN; + var _loc3_:Number = NaN; + if(this.w < -1 || this.w > 1) + { + this.normalize(); + } + if(param1 == null) + { + param1 = new Vector3D(); + } + if(this.w > -1 && this.w < 1) + { + if(this.w == 0) + { + param1.x = this.x; + param1.y = this.y; + param1.z = this.z; + } + else + { + _loc2_ = 2 * Math.acos(this.w); + _loc3_ = 1 / Math.sqrt(1 - this.w * this.w); + param1.x = this.x * _loc3_ * _loc2_; + param1.y = this.y * _loc3_ * _loc2_; + param1.z = this.z * _loc3_ * _loc2_; + } + } + else + { + param1.x = 0; + param1.y = 0; + param1.z = 0; + } + return param1; + } + + public function getEulerAngles(param1:Vector3D) : Vector3D + { + var _loc2_:Number = 2 * this.x * this.x; + var _loc3_:Number = 2 * this.y * this.y; + var _loc4_:Number = 2 * this.z * this.z; + var _loc5_:Number = 2 * this.x * this.y; + var _loc6_:Number = 2 * this.y * this.z; + var _loc7_:Number = 2 * this.z * this.x; + var _loc8_:Number = 2 * this.w * this.x; + var _loc9_:Number = 2 * this.w * this.y; + var _loc10_:Number = 2 * this.w * this.z; + var _loc11_:Number = 1 - _loc3_ - _loc4_; + var _loc12_:Number = _loc5_ - _loc10_; + var _loc13_:Number = _loc5_ + _loc10_; + var _loc14_:Number = 1 - _loc2_ - _loc4_; + var _loc15_:Number = _loc7_ - _loc9_; + var _loc16_:Number = _loc6_ + _loc8_; + var _loc17_:Number = 1 - _loc2_ - _loc3_; + if(-1 < _loc15_ && _loc15_ < 1) + { + if(param1 == null) + { + param1 = new Vector3D(Math.atan2(_loc16_,_loc17_),-Math.asin(_loc15_),Math.atan2(_loc13_,_loc11_)); + } + else + { + param1.x = Math.atan2(_loc16_,_loc17_); + param1.y = -Math.asin(_loc15_); + param1.z = Math.atan2(_loc13_,_loc11_); + } + } + else if(param1 == null) + { + param1 = new Vector3D(0,0.5 * (_loc15_ <= -1 ? Math.PI : -Math.PI),Math.atan2(-_loc12_,_loc14_)); + } + else + { + param1.x = 0; + param1.y = _loc15_ <= -1 ? Math.PI : -Math.PI; + param1.y *= 0.5; + param1.z = Math.atan2(-_loc12_,_loc14_); + } + return param1; + } + + public function setFromEulerAnglesXYZ(param1:Number, param2:Number, param3:Number) : void + { + this.setFromAxisAngleComponents(1,0,0,param1); + _q.setFromAxisAngleComponents(0,1,0,param2); + this.append(_q); + this.normalize(); + _q.setFromAxisAngleComponents(0,0,1,param3); + this.append(_q); + this.normalize(); + } + + public function setFromEulerAngles(param1:Vector3D) : void + { + this.setFromEulerAnglesXYZ(param1.x,param1.y,param1.z); + } + + public function conjugate() : void + { + this.x = -this.x; + this.y = -this.y; + this.z = -this.z; + } + + public function nlerp(param1:Quaternion, param2:Quaternion, param3:Number) : Quaternion + { + var _loc4_:Number = 1 - param3; + this.w = param1.w * _loc4_ + param2.w * param3; + this.x = param1.x * _loc4_ + param2.x * param3; + this.y = param1.y * _loc4_ + param2.y * param3; + this.z = param1.z * _loc4_ + param2.z * param3; + _loc4_ = this.w * this.w + this.x * this.x + this.y * this.y + this.z * this.z; + if(_loc4_ == 0) + { + this.w = 1; + } + else + { + _loc4_ = 1 / Math.sqrt(_loc4_); + this.w *= _loc4_; + this.x *= _loc4_; + this.y *= _loc4_; + this.z *= _loc4_; + } + return this; + } + + public function subtract(param1:Quaternion) : Quaternion + { + this.w -= param1.w; + this.x -= param1.x; + this.y -= param1.y; + this.z -= param1.z; + return this; + } + + public function diff(param1:Quaternion, param2:Quaternion) : Quaternion + { + this.w = param2.w - param1.w; + this.x = param2.x - param1.x; + this.y = param2.y - param1.y; + this.z = param2.z - param1.z; + return this; + } + + public function copy(param1:Quaternion) : Quaternion + { + this.w = param1.w; + this.x = param1.x; + this.y = param1.y; + this.z = param1.z; + return this; + } + + public function toVector3D(param1:Vector3D) : Vector3D + { + param1.x = this.x; + param1.y = this.y; + param1.z = this.z; + param1.w = this.w; + return param1; + } + + public function clone() : Quaternion + { + return new Quaternion(this.w,this.x,this.y,this.z); + } + + public function toString() : String + { + return getQualifiedClassName(this) + "(" + this.w + ", " + this.x + ", " + this.y + ", " + this.z + ")"; + } + + public function slerp(param1:Quaternion, param2:Quaternion, param3:Number) : Quaternion + { + var _loc6_:Number = NaN; + var _loc7_:Number = NaN; + var _loc8_:Number = NaN; + var _loc9_:Number = NaN; + var _loc10_:Number = NaN; + var _loc11_:Number = NaN; + var _loc4_:Number = 1; + var _loc5_:Number = param1.w * param2.w + param1.x * param2.x + param1.y * param2.y + param1.z * param2.z; + if(_loc5_ < 0) + { + _loc5_ = -_loc5_; + _loc4_ = -1; + } + if(1 - _loc5_ < 0.001) + { + _loc6_ = 1 - param3; + _loc7_ = param3 * _loc4_; + this.w = param1.w * _loc6_ + param2.w * _loc7_; + this.x = param1.x * _loc6_ + param2.x * _loc7_; + this.y = param1.y * _loc6_ + param2.y * _loc7_; + this.z = param1.z * _loc6_ + param2.z * _loc7_; + this.normalize(); + } + else + { + _loc8_ = Math.acos(_loc5_); + _loc9_ = Math.sin(_loc8_); + _loc10_ = Math.sin((1 - param3) * _loc8_) / _loc9_; + _loc11_ = Math.sin(param3 * _loc8_) / _loc9_ * _loc4_; + this.w = param1.w * _loc10_ + param2.w * _loc11_; + this.x = param1.x * _loc10_ + param2.x * _loc11_; + this.y = param1.y * _loc10_ + param2.y * _loc11_; + this.z = param1.z * _loc10_ + param2.z * _loc11_; + } + return this; + } + + public function isFiniteQuaternion() : Boolean + { + return isFinite(this.w) && isFinite(this.x) && isFinite(this.y) && isFinite(this.z); + } + } +}