math.vector.ts 202 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341
  1. import { Scalar } from "./math.scalar";
  2. import { Epsilon } from './math.constants';
  3. import { Viewport } from './math.viewport';
  4. import { DeepImmutable, Nullable, FloatArray, float } from "../types";
  5. import { ArrayTools } from '../Misc/arrayTools';
  6. import { IPlaneLike } from './math.like';
  7. import { _TypeStore } from '../Misc/typeStore';
  8. /**
  9. * Class representing a vector containing 2 coordinates
  10. */
  11. export class Vector2 {
  12. /**
  13. * Creates a new Vector2 from the given x and y coordinates
  14. * @param x defines the first coordinate
  15. * @param y defines the second coordinate
  16. */
  17. constructor(
  18. /** defines the first coordinate */
  19. public x: number = 0,
  20. /** defines the second coordinate */
  21. public y: number = 0) {
  22. }
  23. /**
  24. * Gets a string with the Vector2 coordinates
  25. * @returns a string with the Vector2 coordinates
  26. */
  27. public toString(): string {
  28. return "{X: " + this.x + " Y:" + this.y + "}";
  29. }
  30. /**
  31. * Gets class name
  32. * @returns the string "Vector2"
  33. */
  34. public getClassName(): string {
  35. return "Vector2";
  36. }
  37. /**
  38. * Gets current vector hash code
  39. * @returns the Vector2 hash code as a number
  40. */
  41. public getHashCode(): number {
  42. let hash = this.x | 0;
  43. hash = (hash * 397) ^ (this.y | 0);
  44. return hash;
  45. }
  46. // Operators
  47. /**
  48. * Sets the Vector2 coordinates in the given array or Float32Array from the given index.
  49. * @param array defines the source array
  50. * @param index defines the offset in source array
  51. * @returns the current Vector2
  52. */
  53. public toArray(array: FloatArray, index: number = 0): Vector2 {
  54. array[index] = this.x;
  55. array[index + 1] = this.y;
  56. return this;
  57. }
  58. /**
  59. * Copy the current vector to an array
  60. * @returns a new array with 2 elements: the Vector2 coordinates.
  61. */
  62. public asArray(): number[] {
  63. var result = new Array<number>();
  64. this.toArray(result, 0);
  65. return result;
  66. }
  67. /**
  68. * Sets the Vector2 coordinates with the given Vector2 coordinates
  69. * @param source defines the source Vector2
  70. * @returns the current updated Vector2
  71. */
  72. public copyFrom(source: DeepImmutable<Vector2>): Vector2 {
  73. this.x = source.x;
  74. this.y = source.y;
  75. return this;
  76. }
  77. /**
  78. * Sets the Vector2 coordinates with the given floats
  79. * @param x defines the first coordinate
  80. * @param y defines the second coordinate
  81. * @returns the current updated Vector2
  82. */
  83. public copyFromFloats(x: number, y: number): Vector2 {
  84. this.x = x;
  85. this.y = y;
  86. return this;
  87. }
  88. /**
  89. * Sets the Vector2 coordinates with the given floats
  90. * @param x defines the first coordinate
  91. * @param y defines the second coordinate
  92. * @returns the current updated Vector2
  93. */
  94. public set(x: number, y: number): Vector2 {
  95. return this.copyFromFloats(x, y);
  96. }
  97. /**
  98. * Add another vector with the current one
  99. * @param otherVector defines the other vector
  100. * @returns a new Vector2 set with the addition of the current Vector2 and the given one coordinates
  101. */
  102. public add(otherVector: DeepImmutable<Vector2>): Vector2 {
  103. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  104. }
  105. /**
  106. * Sets the "result" coordinates with the addition of the current Vector2 and the given one coordinates
  107. * @param otherVector defines the other vector
  108. * @param result defines the target vector
  109. * @returns the unmodified current Vector2
  110. */
  111. public addToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  112. result.x = this.x + otherVector.x;
  113. result.y = this.y + otherVector.y;
  114. return this;
  115. }
  116. /**
  117. * Set the Vector2 coordinates by adding the given Vector2 coordinates
  118. * @param otherVector defines the other vector
  119. * @returns the current updated Vector2
  120. */
  121. public addInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  122. this.x += otherVector.x;
  123. this.y += otherVector.y;
  124. return this;
  125. }
  126. /**
  127. * Gets a new Vector2 by adding the current Vector2 coordinates to the given Vector3 x, y coordinates
  128. * @param otherVector defines the other vector
  129. * @returns a new Vector2
  130. */
  131. public addVector3(otherVector: Vector3): Vector2 {
  132. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  133. }
  134. /**
  135. * Gets a new Vector2 set with the subtracted coordinates of the given one from the current Vector2
  136. * @param otherVector defines the other vector
  137. * @returns a new Vector2
  138. */
  139. public subtract(otherVector: Vector2): Vector2 {
  140. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  141. }
  142. /**
  143. * Sets the "result" coordinates with the subtraction of the given one from the current Vector2 coordinates.
  144. * @param otherVector defines the other vector
  145. * @param result defines the target vector
  146. * @returns the unmodified current Vector2
  147. */
  148. public subtractToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  149. result.x = this.x - otherVector.x;
  150. result.y = this.y - otherVector.y;
  151. return this;
  152. }
  153. /**
  154. * Sets the current Vector2 coordinates by subtracting from it the given one coordinates
  155. * @param otherVector defines the other vector
  156. * @returns the current updated Vector2
  157. */
  158. public subtractInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  159. this.x -= otherVector.x;
  160. this.y -= otherVector.y;
  161. return this;
  162. }
  163. /**
  164. * Multiplies in place the current Vector2 coordinates by the given ones
  165. * @param otherVector defines the other vector
  166. * @returns the current updated Vector2
  167. */
  168. public multiplyInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  169. this.x *= otherVector.x;
  170. this.y *= otherVector.y;
  171. return this;
  172. }
  173. /**
  174. * Returns a new Vector2 set with the multiplication of the current Vector2 and the given one coordinates
  175. * @param otherVector defines the other vector
  176. * @returns a new Vector2
  177. */
  178. public multiply(otherVector: DeepImmutable<Vector2>): Vector2 {
  179. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  180. }
  181. /**
  182. * Sets "result" coordinates with the multiplication of the current Vector2 and the given one coordinates
  183. * @param otherVector defines the other vector
  184. * @param result defines the target vector
  185. * @returns the unmodified current Vector2
  186. */
  187. public multiplyToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  188. result.x = this.x * otherVector.x;
  189. result.y = this.y * otherVector.y;
  190. return this;
  191. }
  192. /**
  193. * Gets a new Vector2 set with the Vector2 coordinates multiplied by the given floats
  194. * @param x defines the first coordinate
  195. * @param y defines the second coordinate
  196. * @returns a new Vector2
  197. */
  198. public multiplyByFloats(x: number, y: number): Vector2 {
  199. return new Vector2(this.x * x, this.y * y);
  200. }
  201. /**
  202. * Returns a new Vector2 set with the Vector2 coordinates divided by the given one coordinates
  203. * @param otherVector defines the other vector
  204. * @returns a new Vector2
  205. */
  206. public divide(otherVector: Vector2): Vector2 {
  207. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  208. }
  209. /**
  210. * Sets the "result" coordinates with the Vector2 divided by the given one coordinates
  211. * @param otherVector defines the other vector
  212. * @param result defines the target vector
  213. * @returns the unmodified current Vector2
  214. */
  215. public divideToRef(otherVector: DeepImmutable<Vector2>, result: Vector2): Vector2 {
  216. result.x = this.x / otherVector.x;
  217. result.y = this.y / otherVector.y;
  218. return this;
  219. }
  220. /**
  221. * Divides the current Vector2 coordinates by the given ones
  222. * @param otherVector defines the other vector
  223. * @returns the current updated Vector2
  224. */
  225. public divideInPlace(otherVector: DeepImmutable<Vector2>): Vector2 {
  226. return this.divideToRef(otherVector, this);
  227. }
  228. /**
  229. * Gets a new Vector2 with current Vector2 negated coordinates
  230. * @returns a new Vector2
  231. */
  232. public negate(): Vector2 {
  233. return new Vector2(-this.x, -this.y);
  234. }
  235. /**
  236. * Multiply the Vector2 coordinates by scale
  237. * @param scale defines the scaling factor
  238. * @returns the current updated Vector2
  239. */
  240. public scaleInPlace(scale: number): Vector2 {
  241. this.x *= scale;
  242. this.y *= scale;
  243. return this;
  244. }
  245. /**
  246. * Returns a new Vector2 scaled by "scale" from the current Vector2
  247. * @param scale defines the scaling factor
  248. * @returns a new Vector2
  249. */
  250. public scale(scale: number): Vector2 {
  251. let result = new Vector2(0, 0);
  252. this.scaleToRef(scale, result);
  253. return result;
  254. }
  255. /**
  256. * Scale the current Vector2 values by a factor to a given Vector2
  257. * @param scale defines the scale factor
  258. * @param result defines the Vector2 object where to store the result
  259. * @returns the unmodified current Vector2
  260. */
  261. public scaleToRef(scale: number, result: Vector2): Vector2 {
  262. result.x = this.x * scale;
  263. result.y = this.y * scale;
  264. return this;
  265. }
  266. /**
  267. * Scale the current Vector2 values by a factor and add the result to a given Vector2
  268. * @param scale defines the scale factor
  269. * @param result defines the Vector2 object where to store the result
  270. * @returns the unmodified current Vector2
  271. */
  272. public scaleAndAddToRef(scale: number, result: Vector2): Vector2 {
  273. result.x += this.x * scale;
  274. result.y += this.y * scale;
  275. return this;
  276. }
  277. /**
  278. * Gets a boolean if two vectors are equals
  279. * @param otherVector defines the other vector
  280. * @returns true if the given vector coordinates strictly equal the current Vector2 ones
  281. */
  282. public equals(otherVector: DeepImmutable<Vector2>): boolean {
  283. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  284. }
  285. /**
  286. * Gets a boolean if two vectors are equals (using an epsilon value)
  287. * @param otherVector defines the other vector
  288. * @param epsilon defines the minimal distance to consider equality
  289. * @returns true if the given vector coordinates are close to the current ones by a distance of epsilon.
  290. */
  291. public equalsWithEpsilon(otherVector: DeepImmutable<Vector2>, epsilon: number = Epsilon): boolean {
  292. return otherVector && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon) && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon);
  293. }
  294. /**
  295. * Gets a new Vector2 from current Vector2 floored values
  296. * @returns a new Vector2
  297. */
  298. public floor(): Vector2 {
  299. return new Vector2(Math.floor(this.x), Math.floor(this.y));
  300. }
  301. /**
  302. * Gets a new Vector2 from current Vector2 floored values
  303. * @returns a new Vector2
  304. */
  305. public fract(): Vector2 {
  306. return new Vector2(this.x - Math.floor(this.x), this.y - Math.floor(this.y));
  307. }
  308. // Properties
  309. /**
  310. * Gets the length of the vector
  311. * @returns the vector length (float)
  312. */
  313. public length(): number {
  314. return Math.sqrt(this.x * this.x + this.y * this.y);
  315. }
  316. /**
  317. * Gets the vector squared length
  318. * @returns the vector squared length (float)
  319. */
  320. public lengthSquared(): number {
  321. return (this.x * this.x + this.y * this.y);
  322. }
  323. // Methods
  324. /**
  325. * Normalize the vector
  326. * @returns the current updated Vector2
  327. */
  328. public normalize(): Vector2 {
  329. var len = this.length();
  330. if (len === 0) {
  331. return this;
  332. }
  333. this.x /= len;
  334. this.y /= len;
  335. return this;
  336. }
  337. /**
  338. * Gets a new Vector2 copied from the Vector2
  339. * @returns a new Vector2
  340. */
  341. public clone(): Vector2 {
  342. return new Vector2(this.x, this.y);
  343. }
  344. // Statics
  345. /**
  346. * Gets a new Vector2(0, 0)
  347. * @returns a new Vector2
  348. */
  349. public static Zero(): Vector2 {
  350. return new Vector2(0, 0);
  351. }
  352. /**
  353. * Gets a new Vector2(1, 1)
  354. * @returns a new Vector2
  355. */
  356. public static One(): Vector2 {
  357. return new Vector2(1, 1);
  358. }
  359. /**
  360. * Gets a new Vector2 set from the given index element of the given array
  361. * @param array defines the data source
  362. * @param offset defines the offset in the data source
  363. * @returns a new Vector2
  364. */
  365. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Vector2 {
  366. return new Vector2(array[offset], array[offset + 1]);
  367. }
  368. /**
  369. * Sets "result" from the given index element of the given array
  370. * @param array defines the data source
  371. * @param offset defines the offset in the data source
  372. * @param result defines the target vector
  373. */
  374. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Vector2): void {
  375. result.x = array[offset];
  376. result.y = array[offset + 1];
  377. }
  378. /**
  379. * Gets a new Vector2 located for "amount" (float) on the CatmullRom spline defined by the given four Vector2
  380. * @param value1 defines 1st point of control
  381. * @param value2 defines 2nd point of control
  382. * @param value3 defines 3rd point of control
  383. * @param value4 defines 4th point of control
  384. * @param amount defines the interpolation factor
  385. * @returns a new Vector2
  386. */
  387. public static CatmullRom(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>, value3: DeepImmutable<Vector2>, value4: DeepImmutable<Vector2>, amount: number): Vector2 {
  388. var squared = amount * amount;
  389. var cubed = amount * squared;
  390. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  391. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  392. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  393. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  394. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  395. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  396. return new Vector2(x, y);
  397. }
  398. /**
  399. * Returns a new Vector2 set with same the coordinates than "value" ones if the vector "value" is in the square defined by "min" and "max".
  400. * If a coordinate of "value" is lower than "min" coordinates, the returned Vector2 is given this "min" coordinate.
  401. * If a coordinate of "value" is greater than "max" coordinates, the returned Vector2 is given this "max" coordinate
  402. * @param value defines the value to clamp
  403. * @param min defines the lower limit
  404. * @param max defines the upper limit
  405. * @returns a new Vector2
  406. */
  407. public static Clamp(value: DeepImmutable<Vector2>, min: DeepImmutable<Vector2>, max: DeepImmutable<Vector2>): Vector2 {
  408. var x = value.x;
  409. x = (x > max.x) ? max.x : x;
  410. x = (x < min.x) ? min.x : x;
  411. var y = value.y;
  412. y = (y > max.y) ? max.y : y;
  413. y = (y < min.y) ? min.y : y;
  414. return new Vector2(x, y);
  415. }
  416. /**
  417. * Returns a new Vector2 located for "amount" (float) on the Hermite spline defined by the vectors "value1", "value3", "tangent1", "tangent2"
  418. * @param value1 defines the 1st control point
  419. * @param tangent1 defines the outgoing tangent
  420. * @param value2 defines the 2nd control point
  421. * @param tangent2 defines the incoming tangent
  422. * @param amount defines the interpolation factor
  423. * @returns a new Vector2
  424. */
  425. public static Hermite(value1: DeepImmutable<Vector2>, tangent1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>, tangent2: DeepImmutable<Vector2>, amount: number): Vector2 {
  426. var squared = amount * amount;
  427. var cubed = amount * squared;
  428. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  429. var part2 = (-2.0 * cubed) + (3.0 * squared);
  430. var part3 = (cubed - (2.0 * squared)) + amount;
  431. var part4 = cubed - squared;
  432. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  433. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  434. return new Vector2(x, y);
  435. }
  436. /**
  437. * Returns a new Vector2 located for "amount" (float) on the linear interpolation between the vector "start" adn the vector "end".
  438. * @param start defines the start vector
  439. * @param end defines the end vector
  440. * @param amount defines the interpolation factor
  441. * @returns a new Vector2
  442. */
  443. public static Lerp(start: DeepImmutable<Vector2>, end: DeepImmutable<Vector2>, amount: number): Vector2 {
  444. var x = start.x + ((end.x - start.x) * amount);
  445. var y = start.y + ((end.y - start.y) * amount);
  446. return new Vector2(x, y);
  447. }
  448. /**
  449. * Gets the dot product of the vector "left" and the vector "right"
  450. * @param left defines first vector
  451. * @param right defines second vector
  452. * @returns the dot product (float)
  453. */
  454. public static Dot(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): number {
  455. return left.x * right.x + left.y * right.y;
  456. }
  457. /**
  458. * Returns a new Vector2 equal to the normalized given vector
  459. * @param vector defines the vector to normalize
  460. * @returns a new Vector2
  461. */
  462. public static Normalize(vector: DeepImmutable<Vector2>): Vector2 {
  463. var newVector = vector.clone();
  464. newVector.normalize();
  465. return newVector;
  466. }
  467. /**
  468. * Gets a new Vector2 set with the minimal coordinate values from the "left" and "right" vectors
  469. * @param left defines 1st vector
  470. * @param right defines 2nd vector
  471. * @returns a new Vector2
  472. */
  473. public static Minimize(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): Vector2 {
  474. var x = (left.x < right.x) ? left.x : right.x;
  475. var y = (left.y < right.y) ? left.y : right.y;
  476. return new Vector2(x, y);
  477. }
  478. /**
  479. * Gets a new Vecto2 set with the maximal coordinate values from the "left" and "right" vectors
  480. * @param left defines 1st vector
  481. * @param right defines 2nd vector
  482. * @returns a new Vector2
  483. */
  484. public static Maximize(left: DeepImmutable<Vector2>, right: DeepImmutable<Vector2>): Vector2 {
  485. var x = (left.x > right.x) ? left.x : right.x;
  486. var y = (left.y > right.y) ? left.y : right.y;
  487. return new Vector2(x, y);
  488. }
  489. /**
  490. * Gets a new Vector2 set with the transformed coordinates of the given vector by the given transformation matrix
  491. * @param vector defines the vector to transform
  492. * @param transformation defines the matrix to apply
  493. * @returns a new Vector2
  494. */
  495. public static Transform(vector: DeepImmutable<Vector2>, transformation: DeepImmutable<Matrix>): Vector2 {
  496. let r = Vector2.Zero();
  497. Vector2.TransformToRef(vector, transformation, r);
  498. return r;
  499. }
  500. /**
  501. * Transforms the given vector coordinates by the given transformation matrix and stores the result in the vector "result" coordinates
  502. * @param vector defines the vector to transform
  503. * @param transformation defines the matrix to apply
  504. * @param result defines the target vector
  505. */
  506. public static TransformToRef(vector: DeepImmutable<Vector2>, transformation: DeepImmutable<Matrix>, result: Vector2) {
  507. const m = transformation.m;
  508. var x = (vector.x * m[0]) + (vector.y * m[4]) + m[12];
  509. var y = (vector.x * m[1]) + (vector.y * m[5]) + m[13];
  510. result.x = x;
  511. result.y = y;
  512. }
  513. /**
  514. * Determines if a given vector is included in a triangle
  515. * @param p defines the vector to test
  516. * @param p0 defines 1st triangle point
  517. * @param p1 defines 2nd triangle point
  518. * @param p2 defines 3rd triangle point
  519. * @returns true if the point "p" is in the triangle defined by the vertors "p0", "p1", "p2"
  520. */
  521. public static PointInTriangle(p: DeepImmutable<Vector2>, p0: DeepImmutable<Vector2>, p1: DeepImmutable<Vector2>, p2: DeepImmutable<Vector2>) {
  522. let a = 1 / 2 * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
  523. let sign = a < 0 ? -1 : 1;
  524. let s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
  525. let t = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;
  526. return s > 0 && t > 0 && (s + t) < 2 * a * sign;
  527. }
  528. /**
  529. * Gets the distance between the vectors "value1" and "value2"
  530. * @param value1 defines first vector
  531. * @param value2 defines second vector
  532. * @returns the distance between vectors
  533. */
  534. public static Distance(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): number {
  535. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  536. }
  537. /**
  538. * Returns the squared distance between the vectors "value1" and "value2"
  539. * @param value1 defines first vector
  540. * @param value2 defines second vector
  541. * @returns the squared distance between vectors
  542. */
  543. public static DistanceSquared(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): number {
  544. var x = value1.x - value2.x;
  545. var y = value1.y - value2.y;
  546. return (x * x) + (y * y);
  547. }
  548. /**
  549. * Gets a new Vector2 located at the center of the vectors "value1" and "value2"
  550. * @param value1 defines first vector
  551. * @param value2 defines second vector
  552. * @returns a new Vector2
  553. */
  554. public static Center(value1: DeepImmutable<Vector2>, value2: DeepImmutable<Vector2>): Vector2 {
  555. var center = value1.add(value2);
  556. center.scaleInPlace(0.5);
  557. return center;
  558. }
  559. /**
  560. * Gets the shortest distance (float) between the point "p" and the segment defined by the two points "segA" and "segB".
  561. * @param p defines the middle point
  562. * @param segA defines one point of the segment
  563. * @param segB defines the other point of the segment
  564. * @returns the shortest distance
  565. */
  566. public static DistanceOfPointFromSegment(p: DeepImmutable<Vector2>, segA: DeepImmutable<Vector2>, segB: DeepImmutable<Vector2>): number {
  567. let l2 = Vector2.DistanceSquared(segA, segB);
  568. if (l2 === 0.0) {
  569. return Vector2.Distance(p, segA);
  570. }
  571. let v = segB.subtract(segA);
  572. let t = Math.max(0, Math.min(1, Vector2.Dot(p.subtract(segA), v) / l2));
  573. let proj = segA.add(v.multiplyByFloats(t, t));
  574. return Vector2.Distance(p, proj);
  575. }
  576. }
  577. /**
  578. * Class used to store (x,y,z) vector representation
  579. * A Vector3 is the main object used in 3D geometry
  580. * It can represent etiher the coordinates of a point the space, either a direction
  581. * Reminder: js uses a left handed forward facing system
  582. */
  583. export class Vector3 {
  584. private static _UpReadOnly = Vector3.Up() as DeepImmutable<Vector3>;
  585. private static _ZeroReadOnly = Vector3.Zero() as DeepImmutable<Vector3>;
  586. /**
  587. * Creates a new Vector3 object from the given x, y, z (floats) coordinates.
  588. * @param x defines the first coordinates (on X axis)
  589. * @param y defines the second coordinates (on Y axis)
  590. * @param z defines the third coordinates (on Z axis)
  591. */
  592. constructor(
  593. /**
  594. * Defines the first coordinates (on X axis)
  595. */
  596. public x: number = 0,
  597. /**
  598. * Defines the second coordinates (on Y axis)
  599. */
  600. public y: number = 0,
  601. /**
  602. * Defines the third coordinates (on Z axis)
  603. */
  604. public z: number = 0
  605. ) {
  606. }
  607. /**
  608. * Creates a string representation of the Vector3
  609. * @returns a string with the Vector3 coordinates.
  610. */
  611. public toString(): string {
  612. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  613. }
  614. /**
  615. * Gets the class name
  616. * @returns the string "Vector3"
  617. */
  618. public getClassName(): string {
  619. return "Vector3";
  620. }
  621. /**
  622. * Creates the Vector3 hash code
  623. * @returns a number which tends to be unique between Vector3 instances
  624. */
  625. public getHashCode(): number {
  626. let hash = this.x | 0;
  627. hash = (hash * 397) ^ (this.y | 0);
  628. hash = (hash * 397) ^ (this.z | 0);
  629. return hash;
  630. }
  631. // Operators
  632. /**
  633. * Creates an array containing three elements : the coordinates of the Vector3
  634. * @returns a new array of numbers
  635. */
  636. public asArray(): number[] {
  637. var result: number[] = [];
  638. this.toArray(result, 0);
  639. return result;
  640. }
  641. /**
  642. * Populates the given array or Float32Array from the given index with the successive coordinates of the Vector3
  643. * @param array defines the destination array
  644. * @param index defines the offset in the destination array
  645. * @returns the current Vector3
  646. */
  647. public toArray(array: FloatArray, index: number = 0): Vector3 {
  648. array[index] = this.x;
  649. array[index + 1] = this.y;
  650. array[index + 2] = this.z;
  651. return this;
  652. }
  653. /**
  654. * Converts the current Vector3 into a quaternion (considering that the Vector3 contains Euler angles representation of a rotation)
  655. * @returns a new Quaternion object, computed from the Vector3 coordinates
  656. */
  657. public toQuaternion(): Quaternion {
  658. return Quaternion.RotationYawPitchRoll(this.y, this.x, this.z);
  659. }
  660. /**
  661. * Adds the given vector to the current Vector3
  662. * @param otherVector defines the second operand
  663. * @returns the current updated Vector3
  664. */
  665. public addInPlace(otherVector: DeepImmutable<Vector3>): Vector3 {
  666. return this.addInPlaceFromFloats(otherVector.x, otherVector.y, otherVector.z);
  667. }
  668. /**
  669. * Adds the given coordinates to the current Vector3
  670. * @param x defines the x coordinate of the operand
  671. * @param y defines the y coordinate of the operand
  672. * @param z defines the z coordinate of the operand
  673. * @returns the current updated Vector3
  674. */
  675. public addInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  676. this.x += x;
  677. this.y += y;
  678. this.z += z;
  679. return this;
  680. }
  681. /**
  682. * Gets a new Vector3, result of the addition the current Vector3 and the given vector
  683. * @param otherVector defines the second operand
  684. * @returns the resulting Vector3
  685. */
  686. public add(otherVector: DeepImmutable<Vector3>): Vector3 {
  687. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  688. }
  689. /**
  690. * Adds the current Vector3 to the given one and stores the result in the vector "result"
  691. * @param otherVector defines the second operand
  692. * @param result defines the Vector3 object where to store the result
  693. * @returns the current Vector3
  694. */
  695. public addToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  696. return result.copyFromFloats(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  697. }
  698. /**
  699. * Subtract the given vector from the current Vector3
  700. * @param otherVector defines the second operand
  701. * @returns the current updated Vector3
  702. */
  703. public subtractInPlace(otherVector: DeepImmutable<Vector3>): Vector3 {
  704. this.x -= otherVector.x;
  705. this.y -= otherVector.y;
  706. this.z -= otherVector.z;
  707. return this;
  708. }
  709. /**
  710. * Returns a new Vector3, result of the subtraction of the given vector from the current Vector3
  711. * @param otherVector defines the second operand
  712. * @returns the resulting Vector3
  713. */
  714. public subtract(otherVector: DeepImmutable<Vector3>): Vector3 {
  715. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  716. }
  717. /**
  718. * Subtracts the given vector from the current Vector3 and stores the result in the vector "result".
  719. * @param otherVector defines the second operand
  720. * @param result defines the Vector3 object where to store the result
  721. * @returns the current Vector3
  722. */
  723. public subtractToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  724. return this.subtractFromFloatsToRef(otherVector.x, otherVector.y, otherVector.z, result);
  725. }
  726. /**
  727. * Returns a new Vector3 set with the subtraction of the given floats from the current Vector3 coordinates
  728. * @param x defines the x coordinate of the operand
  729. * @param y defines the y coordinate of the operand
  730. * @param z defines the z coordinate of the operand
  731. * @returns the resulting Vector3
  732. */
  733. public subtractFromFloats(x: number, y: number, z: number): Vector3 {
  734. return new Vector3(this.x - x, this.y - y, this.z - z);
  735. }
  736. /**
  737. * Subtracts the given floats from the current Vector3 coordinates and set the given vector "result" with this result
  738. * @param x defines the x coordinate of the operand
  739. * @param y defines the y coordinate of the operand
  740. * @param z defines the z coordinate of the operand
  741. * @param result defines the Vector3 object where to store the result
  742. * @returns the current Vector3
  743. */
  744. public subtractFromFloatsToRef(x: number, y: number, z: number, result: Vector3): Vector3 {
  745. return result.copyFromFloats(this.x - x, this.y - y, this.z - z);
  746. }
  747. /**
  748. * Gets a new Vector3 set with the current Vector3 negated coordinates
  749. * @returns a new Vector3
  750. */
  751. public negate(): Vector3 {
  752. return new Vector3(-this.x, -this.y, -this.z);
  753. }
  754. /**
  755. * Multiplies the Vector3 coordinates by the float "scale"
  756. * @param scale defines the multiplier factor
  757. * @returns the current updated Vector3
  758. */
  759. public scaleInPlace(scale: number): Vector3 {
  760. this.x *= scale;
  761. this.y *= scale;
  762. this.z *= scale;
  763. return this;
  764. }
  765. /**
  766. * Returns a new Vector3 set with the current Vector3 coordinates multiplied by the float "scale"
  767. * @param scale defines the multiplier factor
  768. * @returns a new Vector3
  769. */
  770. public scale(scale: number): Vector3 {
  771. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  772. }
  773. /**
  774. * Multiplies the current Vector3 coordinates by the float "scale" and stores the result in the given vector "result" coordinates
  775. * @param scale defines the multiplier factor
  776. * @param result defines the Vector3 object where to store the result
  777. * @returns the current Vector3
  778. */
  779. public scaleToRef(scale: number, result: Vector3): Vector3 {
  780. return result.copyFromFloats(this.x * scale, this.y * scale, this.z * scale);
  781. }
  782. /**
  783. * Scale the current Vector3 values by a factor and add the result to a given Vector3
  784. * @param scale defines the scale factor
  785. * @param result defines the Vector3 object where to store the result
  786. * @returns the unmodified current Vector3
  787. */
  788. public scaleAndAddToRef(scale: number, result: Vector3): Vector3 {
  789. return result.addInPlaceFromFloats(this.x * scale, this.y * scale, this.z * scale);
  790. }
  791. /**
  792. * Returns true if the current Vector3 and the given vector coordinates are strictly equal
  793. * @param otherVector defines the second operand
  794. * @returns true if both vectors are equals
  795. */
  796. public equals(otherVector: DeepImmutable<Vector3>): boolean {
  797. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  798. }
  799. /**
  800. * Returns true if the current Vector3 and the given vector coordinates are distant less than epsilon
  801. * @param otherVector defines the second operand
  802. * @param epsilon defines the minimal distance to define values as equals
  803. * @returns true if both vectors are distant less than epsilon
  804. */
  805. public equalsWithEpsilon(otherVector: DeepImmutable<Vector3>, epsilon: number = Epsilon): boolean {
  806. return otherVector && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon) && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon) && Scalar.WithinEpsilon(this.z, otherVector.z, epsilon);
  807. }
  808. /**
  809. * Returns true if the current Vector3 coordinates equals the given floats
  810. * @param x defines the x coordinate of the operand
  811. * @param y defines the y coordinate of the operand
  812. * @param z defines the z coordinate of the operand
  813. * @returns true if both vectors are equals
  814. */
  815. public equalsToFloats(x: number, y: number, z: number): boolean {
  816. return this.x === x && this.y === y && this.z === z;
  817. }
  818. /**
  819. * Multiplies the current Vector3 coordinates by the given ones
  820. * @param otherVector defines the second operand
  821. * @returns the current updated Vector3
  822. */
  823. public multiplyInPlace(otherVector: DeepImmutable<Vector3>): Vector3 {
  824. this.x *= otherVector.x;
  825. this.y *= otherVector.y;
  826. this.z *= otherVector.z;
  827. return this;
  828. }
  829. /**
  830. * Returns a new Vector3, result of the multiplication of the current Vector3 by the given vector
  831. * @param otherVector defines the second operand
  832. * @returns the new Vector3
  833. */
  834. public multiply(otherVector: DeepImmutable<Vector3>): Vector3 {
  835. return this.multiplyByFloats(otherVector.x, otherVector.y, otherVector.z);
  836. }
  837. /**
  838. * Multiplies the current Vector3 by the given one and stores the result in the given vector "result"
  839. * @param otherVector defines the second operand
  840. * @param result defines the Vector3 object where to store the result
  841. * @returns the current Vector3
  842. */
  843. public multiplyToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  844. return result.copyFromFloats(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  845. }
  846. /**
  847. * Returns a new Vector3 set with the result of the mulliplication of the current Vector3 coordinates by the given floats
  848. * @param x defines the x coordinate of the operand
  849. * @param y defines the y coordinate of the operand
  850. * @param z defines the z coordinate of the operand
  851. * @returns the new Vector3
  852. */
  853. public multiplyByFloats(x: number, y: number, z: number): Vector3 {
  854. return new Vector3(this.x * x, this.y * y, this.z * z);
  855. }
  856. /**
  857. * Returns a new Vector3 set with the result of the division of the current Vector3 coordinates by the given ones
  858. * @param otherVector defines the second operand
  859. * @returns the new Vector3
  860. */
  861. public divide(otherVector: DeepImmutable<Vector3>): Vector3 {
  862. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  863. }
  864. /**
  865. * Divides the current Vector3 coordinates by the given ones and stores the result in the given vector "result"
  866. * @param otherVector defines the second operand
  867. * @param result defines the Vector3 object where to store the result
  868. * @returns the current Vector3
  869. */
  870. public divideToRef(otherVector: DeepImmutable<Vector3>, result: Vector3): Vector3 {
  871. return result.copyFromFloats(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  872. }
  873. /**
  874. * Divides the current Vector3 coordinates by the given ones.
  875. * @param otherVector defines the second operand
  876. * @returns the current updated Vector3
  877. */
  878. public divideInPlace(otherVector: Vector3): Vector3 {
  879. return this.divideToRef(otherVector, this);
  880. }
  881. /**
  882. * Updates the current Vector3 with the minimal coordinate values between its and the given vector ones
  883. * @param other defines the second operand
  884. * @returns the current updated Vector3
  885. */
  886. public minimizeInPlace(other: DeepImmutable<Vector3>): Vector3 {
  887. return this.minimizeInPlaceFromFloats(other.x, other.y, other.z);
  888. }
  889. /**
  890. * Updates the current Vector3 with the maximal coordinate values between its and the given vector ones.
  891. * @param other defines the second operand
  892. * @returns the current updated Vector3
  893. */
  894. public maximizeInPlace(other: DeepImmutable<Vector3>): Vector3 {
  895. return this.maximizeInPlaceFromFloats(other.x, other.y, other.z);
  896. }
  897. /**
  898. * Updates the current Vector3 with the minimal coordinate values between its and the given coordinates
  899. * @param x defines the x coordinate of the operand
  900. * @param y defines the y coordinate of the operand
  901. * @param z defines the z coordinate of the operand
  902. * @returns the current updated Vector3
  903. */
  904. public minimizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  905. if (x < this.x) { this.x = x; }
  906. if (y < this.y) { this.y = y; }
  907. if (z < this.z) { this.z = z; }
  908. return this;
  909. }
  910. /**
  911. * Updates the current Vector3 with the maximal coordinate values between its and the given coordinates.
  912. * @param x defines the x coordinate of the operand
  913. * @param y defines the y coordinate of the operand
  914. * @param z defines the z coordinate of the operand
  915. * @returns the current updated Vector3
  916. */
  917. public maximizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  918. if (x > this.x) { this.x = x; }
  919. if (y > this.y) { this.y = y; }
  920. if (z > this.z) { this.z = z; }
  921. return this;
  922. }
  923. /**
  924. * Due to float precision, scale of a mesh could be uniform but float values are off by a small fraction
  925. * Check if is non uniform within a certain amount of decimal places to account for this
  926. * @param epsilon the amount the values can differ
  927. * @returns if the the vector is non uniform to a certain number of decimal places
  928. */
  929. public isNonUniformWithinEpsilon(epsilon: number) {
  930. let absX = Math.abs(this.x);
  931. let absY = Math.abs(this.y);
  932. if (!Scalar.WithinEpsilon(absX, absY, epsilon)) {
  933. return true;
  934. }
  935. let absZ = Math.abs(this.z);
  936. if (!Scalar.WithinEpsilon(absX, absZ, epsilon)) {
  937. return true;
  938. }
  939. if (!Scalar.WithinEpsilon(absY, absZ, epsilon)) {
  940. return true;
  941. }
  942. return false;
  943. }
  944. /**
  945. * Gets a boolean indicating that the vector is non uniform meaning x, y or z are not all the same
  946. */
  947. public get isNonUniform(): boolean {
  948. let absX = Math.abs(this.x);
  949. let absY = Math.abs(this.y);
  950. if (absX !== absY) {
  951. return true;
  952. }
  953. let absZ = Math.abs(this.z);
  954. if (absX !== absZ) {
  955. return true;
  956. }
  957. if (absY !== absZ) {
  958. return true;
  959. }
  960. return false;
  961. }
  962. /**
  963. * Gets a new Vector3 from current Vector3 floored values
  964. * @returns a new Vector3
  965. */
  966. public floor(): Vector3 {
  967. return new Vector3(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z));
  968. }
  969. /**
  970. * Gets a new Vector3 from current Vector3 floored values
  971. * @returns a new Vector3
  972. */
  973. public fract(): Vector3 {
  974. return new Vector3(this.x - Math.floor(this.x), this.y - Math.floor(this.y), this.z - Math.floor(this.z));
  975. }
  976. // Properties
  977. /**
  978. * Gets the length of the Vector3
  979. * @returns the length of the Vector3
  980. */
  981. public length(): number {
  982. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  983. }
  984. /**
  985. * Gets the squared length of the Vector3
  986. * @returns squared length of the Vector3
  987. */
  988. public lengthSquared(): number {
  989. return (this.x * this.x + this.y * this.y + this.z * this.z);
  990. }
  991. /**
  992. * Normalize the current Vector3.
  993. * Please note that this is an in place operation.
  994. * @returns the current updated Vector3
  995. */
  996. public normalize(): Vector3 {
  997. return this.normalizeFromLength(this.length());
  998. }
  999. /**
  1000. * Reorders the x y z properties of the vector in place
  1001. * @param order new ordering of the properties (eg. for vector 1,2,3 with "ZYX" will produce 3,2,1)
  1002. * @returns the current updated vector
  1003. */
  1004. public reorderInPlace(order: string) {
  1005. order = order.toLowerCase();
  1006. if (order === "xyz") {
  1007. return this;
  1008. }
  1009. MathTmp.Vector3[0].copyFrom(this);
  1010. ["x", "y", "z"].forEach((val, i) => {
  1011. (<any>this)[val] = (<any>MathTmp.Vector3[0])[order[i]];
  1012. });
  1013. return this;
  1014. }
  1015. /**
  1016. * Rotates the vector around 0,0,0 by a quaternion
  1017. * @param quaternion the rotation quaternion
  1018. * @param result vector to store the result
  1019. * @returns the resulting vector
  1020. */
  1021. public rotateByQuaternionToRef(quaternion: Quaternion, result: Vector3) {
  1022. quaternion.toRotationMatrix(MathTmp.Matrix[0]);
  1023. Vector3.TransformCoordinatesToRef(this, MathTmp.Matrix[0], result);
  1024. return result;
  1025. }
  1026. /**
  1027. * Rotates a vector around a given point
  1028. * @param quaternion the rotation quaternion
  1029. * @param point the point to rotate around
  1030. * @param result vector to store the result
  1031. * @returns the resulting vector
  1032. */
  1033. public rotateByQuaternionAroundPointToRef(quaternion: Quaternion, point: Vector3, result: Vector3) {
  1034. this.subtractToRef(point, MathTmp.Vector3[0]);
  1035. MathTmp.Vector3[0].rotateByQuaternionToRef(quaternion, MathTmp.Vector3[0]);
  1036. point.addToRef(MathTmp.Vector3[0], result);
  1037. return result;
  1038. }
  1039. /**
  1040. * Returns a new Vector3 as the cross product of the current vector and the "other" one
  1041. * The cross product is then orthogonal to both current and "other"
  1042. * @param other defines the right operand
  1043. * @returns the cross product
  1044. */
  1045. public cross(other: Vector3) {
  1046. return Vector3.Cross(this, other);
  1047. }
  1048. /**
  1049. * Normalize the current Vector3 with the given input length.
  1050. * Please note that this is an in place operation.
  1051. * @param len the length of the vector
  1052. * @returns the current updated Vector3
  1053. */
  1054. public normalizeFromLength(len: number): Vector3 {
  1055. if (len === 0 || len === 1.0) {
  1056. return this;
  1057. }
  1058. return this.scaleInPlace(1.0 / len);
  1059. }
  1060. /**
  1061. * Normalize the current Vector3 to a new vector
  1062. * @returns the new Vector3
  1063. */
  1064. public normalizeToNew(): Vector3 {
  1065. const normalized = new Vector3(0, 0, 0);
  1066. this.normalizeToRef(normalized);
  1067. return normalized;
  1068. }
  1069. /**
  1070. * Normalize the current Vector3 to the reference
  1071. * @param reference define the Vector3 to update
  1072. * @returns the updated Vector3
  1073. */
  1074. public normalizeToRef(reference: DeepImmutable<Vector3>): Vector3 {
  1075. var len = this.length();
  1076. if (len === 0 || len === 1.0) {
  1077. return reference.copyFromFloats(this.x, this.y, this.z);
  1078. }
  1079. return this.scaleToRef(1.0 / len, reference);
  1080. }
  1081. /**
  1082. * Creates a new Vector3 copied from the current Vector3
  1083. * @returns the new Vector3
  1084. */
  1085. public clone(): Vector3 {
  1086. return new Vector3(this.x, this.y, this.z);
  1087. }
  1088. /**
  1089. * Copies the given vector coordinates to the current Vector3 ones
  1090. * @param source defines the source Vector3
  1091. * @returns the current updated Vector3
  1092. */
  1093. public copyFrom(source: DeepImmutable<Vector3>): Vector3 {
  1094. return this.copyFromFloats(source.x, source.y, source.z);
  1095. }
  1096. /**
  1097. * Copies the given floats to the current Vector3 coordinates
  1098. * @param x defines the x coordinate of the operand
  1099. * @param y defines the y coordinate of the operand
  1100. * @param z defines the z coordinate of the operand
  1101. * @returns the current updated Vector3
  1102. */
  1103. public copyFromFloats(x: number, y: number, z: number): Vector3 {
  1104. this.x = x;
  1105. this.y = y;
  1106. this.z = z;
  1107. return this;
  1108. }
  1109. /**
  1110. * Copies the given floats to the current Vector3 coordinates
  1111. * @param x defines the x coordinate of the operand
  1112. * @param y defines the y coordinate of the operand
  1113. * @param z defines the z coordinate of the operand
  1114. * @returns the current updated Vector3
  1115. */
  1116. public set(x: number, y: number, z: number): Vector3 {
  1117. return this.copyFromFloats(x, y, z);
  1118. }
  1119. /**
  1120. * Copies the given float to the current Vector3 coordinates
  1121. * @param v defines the x, y and z coordinates of the operand
  1122. * @returns the current updated Vector3
  1123. */
  1124. public setAll(v: number): Vector3 {
  1125. this.x = this.y = this.z = v;
  1126. return this;
  1127. }
  1128. // Statics
  1129. /**
  1130. * Get the clip factor between two vectors
  1131. * @param vector0 defines the first operand
  1132. * @param vector1 defines the second operand
  1133. * @param axis defines the axis to use
  1134. * @param size defines the size along the axis
  1135. * @returns the clip factor
  1136. */
  1137. public static GetClipFactor(vector0: DeepImmutable<Vector3>, vector1: DeepImmutable<Vector3>, axis: DeepImmutable<Vector3>, size: number) {
  1138. var d0 = Vector3.Dot(vector0, axis) - size;
  1139. var d1 = Vector3.Dot(vector1, axis) - size;
  1140. var s = d0 / (d0 - d1);
  1141. return s;
  1142. }
  1143. /**
  1144. * Get angle between two vectors
  1145. * @param vector0 angle between vector0 and vector1
  1146. * @param vector1 angle between vector0 and vector1
  1147. * @param normal direction of the normal
  1148. * @return the angle between vector0 and vector1
  1149. */
  1150. public static GetAngleBetweenVectors(vector0: DeepImmutable<Vector3>, vector1: DeepImmutable<Vector3>, normal: DeepImmutable<Vector3>): number {
  1151. const v0: Vector3 = vector0.normalizeToRef(MathTmp.Vector3[1]);
  1152. const v1: Vector3 = vector1.normalizeToRef(MathTmp.Vector3[2]);
  1153. const dot: number = Vector3.Dot(v0, v1);
  1154. const n = MathTmp.Vector3[3];
  1155. Vector3.CrossToRef(v0, v1, n);
  1156. if (Vector3.Dot(n, normal) > 0) {
  1157. return Math.acos(dot);
  1158. }
  1159. return -Math.acos(dot);
  1160. }
  1161. /**
  1162. * Returns a new Vector3 set from the index "offset" of the given array
  1163. * @param array defines the source array
  1164. * @param offset defines the offset in the source array
  1165. * @returns the new Vector3
  1166. */
  1167. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Vector3 {
  1168. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  1169. }
  1170. /**
  1171. * Returns a new Vector3 set from the index "offset" of the given Float32Array
  1172. * @param array defines the source array
  1173. * @param offset defines the offset in the source array
  1174. * @returns the new Vector3
  1175. * @deprecated Please use FromArray instead.
  1176. */
  1177. public static FromFloatArray(array: DeepImmutable<Float32Array>, offset?: number): Vector3 {
  1178. return Vector3.FromArray(array, offset);
  1179. }
  1180. /**
  1181. * Sets the given vector "result" with the element values from the index "offset" of the given array
  1182. * @param array defines the source array
  1183. * @param offset defines the offset in the source array
  1184. * @param result defines the Vector3 where to store the result
  1185. */
  1186. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Vector3): void {
  1187. result.x = array[offset];
  1188. result.y = array[offset + 1];
  1189. result.z = array[offset + 2];
  1190. }
  1191. /**
  1192. * Sets the given vector "result" with the element values from the index "offset" of the given Float32Array
  1193. * @param array defines the source array
  1194. * @param offset defines the offset in the source array
  1195. * @param result defines the Vector3 where to store the result
  1196. * @deprecated Please use FromArrayToRef instead.
  1197. */
  1198. public static FromFloatArrayToRef(array: DeepImmutable<Float32Array>, offset: number, result: Vector3): void {
  1199. return Vector3.FromArrayToRef(array, offset, result);
  1200. }
  1201. /**
  1202. * Sets the given vector "result" with the given floats.
  1203. * @param x defines the x coordinate of the source
  1204. * @param y defines the y coordinate of the source
  1205. * @param z defines the z coordinate of the source
  1206. * @param result defines the Vector3 where to store the result
  1207. */
  1208. public static FromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
  1209. result.copyFromFloats(x, y, z);
  1210. }
  1211. /**
  1212. * Returns a new Vector3 set to (0.0, 0.0, 0.0)
  1213. * @returns a new empty Vector3
  1214. */
  1215. public static Zero(): Vector3 {
  1216. return new Vector3(0.0, 0.0, 0.0);
  1217. }
  1218. /**
  1219. * Returns a new Vector3 set to (1.0, 1.0, 1.0)
  1220. * @returns a new unit Vector3
  1221. */
  1222. public static One(): Vector3 {
  1223. return new Vector3(1.0, 1.0, 1.0);
  1224. }
  1225. /**
  1226. * Returns a new Vector3 set to (0.0, 1.0, 0.0)
  1227. * @returns a new up Vector3
  1228. */
  1229. public static Up(): Vector3 {
  1230. return new Vector3(0.0, 1.0, 0.0);
  1231. }
  1232. /**
  1233. * Gets a up Vector3 that must not be updated
  1234. */
  1235. public static get UpReadOnly(): DeepImmutable<Vector3> {
  1236. return Vector3._UpReadOnly;
  1237. }
  1238. /**
  1239. * Gets a zero Vector3 that must not be updated
  1240. */
  1241. public static get ZeroReadOnly(): DeepImmutable<Vector3> {
  1242. return Vector3._ZeroReadOnly;
  1243. }
  1244. /**
  1245. * Returns a new Vector3 set to (0.0, -1.0, 0.0)
  1246. * @returns a new down Vector3
  1247. */
  1248. public static Down(): Vector3 {
  1249. return new Vector3(0.0, -1.0, 0.0);
  1250. }
  1251. /**
  1252. * Returns a new Vector3 set to (0.0, 0.0, 1.0)
  1253. * @returns a new forward Vector3
  1254. */
  1255. public static Forward(): Vector3 {
  1256. return new Vector3(0.0, 0.0, 1.0);
  1257. }
  1258. /**
  1259. * Returns a new Vector3 set to (0.0, 0.0, -1.0)
  1260. * @returns a new forward Vector3
  1261. */
  1262. public static Backward(): Vector3 {
  1263. return new Vector3(0.0, 0.0, -1.0);
  1264. }
  1265. /**
  1266. * Returns a new Vector3 set to (1.0, 0.0, 0.0)
  1267. * @returns a new right Vector3
  1268. */
  1269. public static Right(): Vector3 {
  1270. return new Vector3(1.0, 0.0, 0.0);
  1271. }
  1272. /**
  1273. * Returns a new Vector3 set to (-1.0, 0.0, 0.0)
  1274. * @returns a new left Vector3
  1275. */
  1276. public static Left(): Vector3 {
  1277. return new Vector3(-1.0, 0.0, 0.0);
  1278. }
  1279. /**
  1280. * Returns a new Vector3 set with the result of the transformation by the given matrix of the given vector.
  1281. * This method computes tranformed coordinates only, not transformed direction vectors (ie. it takes translation in account)
  1282. * @param vector defines the Vector3 to transform
  1283. * @param transformation defines the transformation matrix
  1284. * @returns the transformed Vector3
  1285. */
  1286. public static TransformCoordinates(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>): Vector3 {
  1287. var result = Vector3.Zero();
  1288. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  1289. return result;
  1290. }
  1291. /**
  1292. * Sets the given vector "result" coordinates with the result of the transformation by the given matrix of the given vector
  1293. * This method computes tranformed coordinates only, not transformed direction vectors (ie. it takes translation in account)
  1294. * @param vector defines the Vector3 to transform
  1295. * @param transformation defines the transformation matrix
  1296. * @param result defines the Vector3 where to store the result
  1297. */
  1298. public static TransformCoordinatesToRef(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  1299. Vector3.TransformCoordinatesFromFloatsToRef(vector.x, vector.y, vector.z, transformation, result);
  1300. }
  1301. /**
  1302. * Sets the given vector "result" coordinates with the result of the transformation by the given matrix of the given floats (x, y, z)
  1303. * This method computes tranformed coordinates only, not transformed direction vectors
  1304. * @param x define the x coordinate of the source vector
  1305. * @param y define the y coordinate of the source vector
  1306. * @param z define the z coordinate of the source vector
  1307. * @param transformation defines the transformation matrix
  1308. * @param result defines the Vector3 where to store the result
  1309. */
  1310. public static TransformCoordinatesFromFloatsToRef(x: number, y: number, z: number, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  1311. const m = transformation.m;
  1312. var rx = x * m[0] + y * m[4] + z * m[8] + m[12];
  1313. var ry = x * m[1] + y * m[5] + z * m[9] + m[13];
  1314. var rz = x * m[2] + y * m[6] + z * m[10] + m[14];
  1315. var rw = 1 / (x * m[3] + y * m[7] + z * m[11] + m[15]);
  1316. result.x = rx * rw;
  1317. result.y = ry * rw;
  1318. result.z = rz * rw;
  1319. }
  1320. /**
  1321. * Returns a new Vector3 set with the result of the normal transformation by the given matrix of the given vector
  1322. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  1323. * @param vector defines the Vector3 to transform
  1324. * @param transformation defines the transformation matrix
  1325. * @returns the new Vector3
  1326. */
  1327. public static TransformNormal(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>): Vector3 {
  1328. var result = Vector3.Zero();
  1329. Vector3.TransformNormalToRef(vector, transformation, result);
  1330. return result;
  1331. }
  1332. /**
  1333. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given vector
  1334. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  1335. * @param vector defines the Vector3 to transform
  1336. * @param transformation defines the transformation matrix
  1337. * @param result defines the Vector3 where to store the result
  1338. */
  1339. public static TransformNormalToRef(vector: DeepImmutable<Vector3>, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  1340. this.TransformNormalFromFloatsToRef(vector.x, vector.y, vector.z, transformation, result);
  1341. }
  1342. /**
  1343. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given floats (x, y, z)
  1344. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  1345. * @param x define the x coordinate of the source vector
  1346. * @param y define the y coordinate of the source vector
  1347. * @param z define the z coordinate of the source vector
  1348. * @param transformation defines the transformation matrix
  1349. * @param result defines the Vector3 where to store the result
  1350. */
  1351. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, transformation: DeepImmutable<Matrix>, result: Vector3): void {
  1352. const m = transformation.m;
  1353. result.x = x * m[0] + y * m[4] + z * m[8];
  1354. result.y = x * m[1] + y * m[5] + z * m[9];
  1355. result.z = x * m[2] + y * m[6] + z * m[10];
  1356. }
  1357. /**
  1358. * Returns a new Vector3 located for "amount" on the CatmullRom interpolation spline defined by the vectors "value1", "value2", "value3", "value4"
  1359. * @param value1 defines the first control point
  1360. * @param value2 defines the second control point
  1361. * @param value3 defines the third control point
  1362. * @param value4 defines the fourth control point
  1363. * @param amount defines the amount on the spline to use
  1364. * @returns the new Vector3
  1365. */
  1366. public static CatmullRom(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>, value3: DeepImmutable<Vector3>, value4: DeepImmutable<Vector3>, amount: number): Vector3 {
  1367. var squared = amount * amount;
  1368. var cubed = amount * squared;
  1369. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  1370. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  1371. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  1372. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  1373. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  1374. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  1375. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  1376. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  1377. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  1378. return new Vector3(x, y, z);
  1379. }
  1380. /**
  1381. * Returns a new Vector3 set with the coordinates of "value", if the vector "value" is in the cube defined by the vectors "min" and "max"
  1382. * If a coordinate value of "value" is lower than one of the "min" coordinate, then this "value" coordinate is set with the "min" one
  1383. * If a coordinate value of "value" is greater than one of the "max" coordinate, then this "value" coordinate is set with the "max" one
  1384. * @param value defines the current value
  1385. * @param min defines the lower range value
  1386. * @param max defines the upper range value
  1387. * @returns the new Vector3
  1388. */
  1389. public static Clamp(value: DeepImmutable<Vector3>, min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>): Vector3 {
  1390. const v = new Vector3();
  1391. Vector3.ClampToRef(value, min, max, v);
  1392. return v;
  1393. }
  1394. /**
  1395. * Sets the given vector "result" with the coordinates of "value", if the vector "value" is in the cube defined by the vectors "min" and "max"
  1396. * If a coordinate value of "value" is lower than one of the "min" coordinate, then this "value" coordinate is set with the "min" one
  1397. * If a coordinate value of "value" is greater than one of the "max" coordinate, then this "value" coordinate is set with the "max" one
  1398. * @param value defines the current value
  1399. * @param min defines the lower range value
  1400. * @param max defines the upper range value
  1401. * @param result defines the Vector3 where to store the result
  1402. */
  1403. public static ClampToRef(value: DeepImmutable<Vector3>, min: DeepImmutable<Vector3>, max: DeepImmutable<Vector3>, result: Vector3): void {
  1404. var x = value.x;
  1405. x = (x > max.x) ? max.x : x;
  1406. x = (x < min.x) ? min.x : x;
  1407. var y = value.y;
  1408. y = (y > max.y) ? max.y : y;
  1409. y = (y < min.y) ? min.y : y;
  1410. var z = value.z;
  1411. z = (z > max.z) ? max.z : z;
  1412. z = (z < min.z) ? min.z : z;
  1413. result.copyFromFloats(x, y, z);
  1414. }
  1415. /**
  1416. * Checks if a given vector is inside a specific range
  1417. * @param v defines the vector to test
  1418. * @param min defines the minimum range
  1419. * @param max defines the maximum range
  1420. */
  1421. public static CheckExtends(v: Vector3, min: Vector3, max: Vector3): void {
  1422. min.minimizeInPlace(v);
  1423. max.maximizeInPlace(v);
  1424. }
  1425. /**
  1426. * Returns a new Vector3 located for "amount" (float) on the Hermite interpolation spline defined by the vectors "value1", "tangent1", "value2", "tangent2"
  1427. * @param value1 defines the first control point
  1428. * @param tangent1 defines the first tangent vector
  1429. * @param value2 defines the second control point
  1430. * @param tangent2 defines the second tangent vector
  1431. * @param amount defines the amount on the interpolation spline (between 0 and 1)
  1432. * @returns the new Vector3
  1433. */
  1434. public static Hermite(value1: DeepImmutable<Vector3>, tangent1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>, tangent2: DeepImmutable<Vector3>, amount: number): Vector3 {
  1435. var squared = amount * amount;
  1436. var cubed = amount * squared;
  1437. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  1438. var part2 = (-2.0 * cubed) + (3.0 * squared);
  1439. var part3 = (cubed - (2.0 * squared)) + amount;
  1440. var part4 = cubed - squared;
  1441. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  1442. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  1443. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  1444. return new Vector3(x, y, z);
  1445. }
  1446. /**
  1447. * Returns a new Vector3 located for "amount" (float) on the linear interpolation between the vectors "start" and "end"
  1448. * @param start defines the start value
  1449. * @param end defines the end value
  1450. * @param amount max defines amount between both (between 0 and 1)
  1451. * @returns the new Vector3
  1452. */
  1453. public static Lerp(start: DeepImmutable<Vector3>, end: DeepImmutable<Vector3>, amount: number): Vector3 {
  1454. var result = new Vector3(0, 0, 0);
  1455. Vector3.LerpToRef(start, end, amount, result);
  1456. return result;
  1457. }
  1458. /**
  1459. * Sets the given vector "result" with the result of the linear interpolation from the vector "start" for "amount" to the vector "end"
  1460. * @param start defines the start value
  1461. * @param end defines the end value
  1462. * @param amount max defines amount between both (between 0 and 1)
  1463. * @param result defines the Vector3 where to store the result
  1464. */
  1465. public static LerpToRef(start: DeepImmutable<Vector3>, end: DeepImmutable<Vector3>, amount: number, result: Vector3): void {
  1466. result.x = start.x + ((end.x - start.x) * amount);
  1467. result.y = start.y + ((end.y - start.y) * amount);
  1468. result.z = start.z + ((end.z - start.z) * amount);
  1469. }
  1470. /**
  1471. * Returns the dot product (float) between the vectors "left" and "right"
  1472. * @param left defines the left operand
  1473. * @param right defines the right operand
  1474. * @returns the dot product
  1475. */
  1476. public static Dot(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): number {
  1477. return (left.x * right.x + left.y * right.y + left.z * right.z);
  1478. }
  1479. /**
  1480. * Returns a new Vector3 as the cross product of the vectors "left" and "right"
  1481. * The cross product is then orthogonal to both "left" and "right"
  1482. * @param left defines the left operand
  1483. * @param right defines the right operand
  1484. * @returns the cross product
  1485. */
  1486. public static Cross(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): Vector3 {
  1487. var result = Vector3.Zero();
  1488. Vector3.CrossToRef(left, right, result);
  1489. return result;
  1490. }
  1491. /**
  1492. * Sets the given vector "result" with the cross product of "left" and "right"
  1493. * The cross product is then orthogonal to both "left" and "right"
  1494. * @param left defines the left operand
  1495. * @param right defines the right operand
  1496. * @param result defines the Vector3 where to store the result
  1497. */
  1498. public static CrossToRef(left: Vector3, right: Vector3, result: Vector3): void {
  1499. const x = left.y * right.z - left.z * right.y;
  1500. const y = left.z * right.x - left.x * right.z;
  1501. const z = left.x * right.y - left.y * right.x;
  1502. result.copyFromFloats(x, y, z);
  1503. }
  1504. /**
  1505. * Returns a new Vector3 as the normalization of the given vector
  1506. * @param vector defines the Vector3 to normalize
  1507. * @returns the new Vector3
  1508. */
  1509. public static Normalize(vector: DeepImmutable<Vector3>): Vector3 {
  1510. var result = Vector3.Zero();
  1511. Vector3.NormalizeToRef(vector, result);
  1512. return result;
  1513. }
  1514. /**
  1515. * Sets the given vector "result" with the normalization of the given first vector
  1516. * @param vector defines the Vector3 to normalize
  1517. * @param result defines the Vector3 where to store the result
  1518. */
  1519. public static NormalizeToRef(vector: DeepImmutable<Vector3>, result: Vector3): void {
  1520. vector.normalizeToRef(result);
  1521. }
  1522. /**
  1523. * Project a Vector3 onto screen space
  1524. * @param vector defines the Vector3 to project
  1525. * @param world defines the world matrix to use
  1526. * @param transform defines the transform (view x projection) matrix to use
  1527. * @param viewport defines the screen viewport to use
  1528. * @returns the new Vector3
  1529. */
  1530. public static Project(vector: DeepImmutable<Vector3>, world: DeepImmutable<Matrix>, transform: DeepImmutable<Matrix>, viewport: DeepImmutable<Viewport>): Vector3 {
  1531. var cw = viewport.width;
  1532. var ch = viewport.height;
  1533. var cx = viewport.x;
  1534. var cy = viewport.y;
  1535. var viewportMatrix = MathTmp.Matrix[1];
  1536. Matrix.FromValuesToRef(
  1537. cw / 2.0, 0, 0, 0,
  1538. 0, -ch / 2.0, 0, 0,
  1539. 0, 0, 0.5, 0,
  1540. cx + cw / 2.0, ch / 2.0 + cy, 0.5, 1, viewportMatrix);
  1541. var matrix = MathTmp.Matrix[0];
  1542. world.multiplyToRef(transform, matrix);
  1543. matrix.multiplyToRef(viewportMatrix, matrix);
  1544. return Vector3.TransformCoordinates(vector, matrix);
  1545. }
  1546. /** @hidden */
  1547. public static _UnprojectFromInvertedMatrixToRef(source: DeepImmutable<Vector3>, matrix: DeepImmutable<Matrix>, result: Vector3) {
  1548. Vector3.TransformCoordinatesToRef(source, matrix, result);
  1549. const m = matrix.m;
  1550. var num = source.x * m[3] + source.y * m[7] + source.z * m[11] + m[15];
  1551. if (Scalar.WithinEpsilon(num, 1.0)) {
  1552. result.scaleInPlace(1.0 / num);
  1553. }
  1554. }
  1555. /**
  1556. * Unproject from screen space to object space
  1557. * @param source defines the screen space Vector3 to use
  1558. * @param viewportWidth defines the current width of the viewport
  1559. * @param viewportHeight defines the current height of the viewport
  1560. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  1561. * @param transform defines the transform (view x projection) matrix to use
  1562. * @returns the new Vector3
  1563. */
  1564. public static UnprojectFromTransform(source: Vector3, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, transform: DeepImmutable<Matrix>): Vector3 {
  1565. var matrix = MathTmp.Matrix[0];
  1566. world.multiplyToRef(transform, matrix);
  1567. matrix.invert();
  1568. source.x = source.x / viewportWidth * 2 - 1;
  1569. source.y = -(source.y / viewportHeight * 2 - 1);
  1570. const vector = new Vector3();
  1571. Vector3._UnprojectFromInvertedMatrixToRef(source, matrix, vector);
  1572. return vector;
  1573. }
  1574. /**
  1575. * Unproject from screen space to object space
  1576. * @param source defines the screen space Vector3 to use
  1577. * @param viewportWidth defines the current width of the viewport
  1578. * @param viewportHeight defines the current height of the viewport
  1579. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  1580. * @param view defines the view matrix to use
  1581. * @param projection defines the projection matrix to use
  1582. * @returns the new Vector3
  1583. */
  1584. public static Unproject(source: DeepImmutable<Vector3>, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>): Vector3 {
  1585. let result = Vector3.Zero();
  1586. Vector3.UnprojectToRef(source, viewportWidth, viewportHeight, world, view, projection, result);
  1587. return result;
  1588. }
  1589. /**
  1590. * Unproject from screen space to object space
  1591. * @param source defines the screen space Vector3 to use
  1592. * @param viewportWidth defines the current width of the viewport
  1593. * @param viewportHeight defines the current height of the viewport
  1594. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  1595. * @param view defines the view matrix to use
  1596. * @param projection defines the projection matrix to use
  1597. * @param result defines the Vector3 where to store the result
  1598. */
  1599. public static UnprojectToRef(source: DeepImmutable<Vector3>, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>, result: Vector3): void {
  1600. Vector3.UnprojectFloatsToRef(source.x, source.y, source.z, viewportWidth, viewportHeight, world, view, projection, result);
  1601. }
  1602. /**
  1603. * Unproject from screen space to object space
  1604. * @param sourceX defines the screen space x coordinate to use
  1605. * @param sourceY defines the screen space y coordinate to use
  1606. * @param sourceZ defines the screen space z coordinate to use
  1607. * @param viewportWidth defines the current width of the viewport
  1608. * @param viewportHeight defines the current height of the viewport
  1609. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  1610. * @param view defines the view matrix to use
  1611. * @param projection defines the projection matrix to use
  1612. * @param result defines the Vector3 where to store the result
  1613. */
  1614. public static UnprojectFloatsToRef(sourceX: float, sourceY: float, sourceZ: float, viewportWidth: number, viewportHeight: number, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>, result: Vector3): void {
  1615. var matrix = MathTmp.Matrix[0];
  1616. world.multiplyToRef(view, matrix);
  1617. matrix.multiplyToRef(projection, matrix);
  1618. matrix.invert();
  1619. var screenSource = MathTmp.Vector3[0];
  1620. screenSource.x = sourceX / viewportWidth * 2 - 1;
  1621. screenSource.y = -(sourceY / viewportHeight * 2 - 1);
  1622. screenSource.z = 2 * sourceZ - 1.0;
  1623. Vector3._UnprojectFromInvertedMatrixToRef(screenSource, matrix, result);
  1624. }
  1625. /**
  1626. * Gets the minimal coordinate values between two Vector3
  1627. * @param left defines the first operand
  1628. * @param right defines the second operand
  1629. * @returns the new Vector3
  1630. */
  1631. public static Minimize(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): Vector3 {
  1632. var min = left.clone();
  1633. min.minimizeInPlace(right);
  1634. return min;
  1635. }
  1636. /**
  1637. * Gets the maximal coordinate values between two Vector3
  1638. * @param left defines the first operand
  1639. * @param right defines the second operand
  1640. * @returns the new Vector3
  1641. */
  1642. public static Maximize(left: DeepImmutable<Vector3>, right: DeepImmutable<Vector3>): Vector3 {
  1643. var max = left.clone();
  1644. max.maximizeInPlace(right);
  1645. return max;
  1646. }
  1647. /**
  1648. * Returns the distance between the vectors "value1" and "value2"
  1649. * @param value1 defines the first operand
  1650. * @param value2 defines the second operand
  1651. * @returns the distance
  1652. */
  1653. public static Distance(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>): number {
  1654. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  1655. }
  1656. /**
  1657. * Returns the squared distance between the vectors "value1" and "value2"
  1658. * @param value1 defines the first operand
  1659. * @param value2 defines the second operand
  1660. * @returns the squared distance
  1661. */
  1662. public static DistanceSquared(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>): number {
  1663. var x = value1.x - value2.x;
  1664. var y = value1.y - value2.y;
  1665. var z = value1.z - value2.z;
  1666. return (x * x) + (y * y) + (z * z);
  1667. }
  1668. /**
  1669. * Returns a new Vector3 located at the center between "value1" and "value2"
  1670. * @param value1 defines the first operand
  1671. * @param value2 defines the second operand
  1672. * @returns the new Vector3
  1673. */
  1674. public static Center(value1: DeepImmutable<Vector3>, value2: DeepImmutable<Vector3>): Vector3 {
  1675. var center = value1.add(value2);
  1676. center.scaleInPlace(0.5);
  1677. return center;
  1678. }
  1679. /**
  1680. * Given three orthogonal normalized left-handed oriented Vector3 axis in space (target system),
  1681. * RotationFromAxis() returns the rotation Euler angles (ex : rotation.x, rotation.y, rotation.z) to apply
  1682. * to something in order to rotate it from its local system to the given target system
  1683. * Note: axis1, axis2 and axis3 are normalized during this operation
  1684. * @param axis1 defines the first axis
  1685. * @param axis2 defines the second axis
  1686. * @param axis3 defines the third axis
  1687. * @returns a new Vector3
  1688. */
  1689. public static RotationFromAxis(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>): Vector3 {
  1690. var rotation = Vector3.Zero();
  1691. Vector3.RotationFromAxisToRef(axis1, axis2, axis3, rotation);
  1692. return rotation;
  1693. }
  1694. /**
  1695. * The same than RotationFromAxis but updates the given ref Vector3 parameter instead of returning a new Vector3
  1696. * @param axis1 defines the first axis
  1697. * @param axis2 defines the second axis
  1698. * @param axis3 defines the third axis
  1699. * @param ref defines the Vector3 where to store the result
  1700. */
  1701. public static RotationFromAxisToRef(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>, ref: Vector3): void {
  1702. var quat = MathTmp.Quaternion[0];
  1703. Quaternion.RotationQuaternionFromAxisToRef(axis1, axis2, axis3, quat);
  1704. quat.toEulerAnglesToRef(ref);
  1705. }
  1706. }
  1707. /**
  1708. * Vector4 class created for EulerAngle class conversion to Quaternion
  1709. */
  1710. export class Vector4 {
  1711. /**
  1712. * Creates a Vector4 object from the given floats.
  1713. * @param x x value of the vector
  1714. * @param y y value of the vector
  1715. * @param z z value of the vector
  1716. * @param w w value of the vector
  1717. */
  1718. constructor(
  1719. /** x value of the vector */
  1720. public x: number,
  1721. /** y value of the vector */
  1722. public y: number,
  1723. /** z value of the vector */
  1724. public z: number,
  1725. /** w value of the vector */
  1726. public w: number
  1727. ) { }
  1728. /**
  1729. * Returns the string with the Vector4 coordinates.
  1730. * @returns a string containing all the vector values
  1731. */
  1732. public toString(): string {
  1733. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1734. }
  1735. /**
  1736. * Returns the string "Vector4".
  1737. * @returns "Vector4"
  1738. */
  1739. public getClassName(): string {
  1740. return "Vector4";
  1741. }
  1742. /**
  1743. * Returns the Vector4 hash code.
  1744. * @returns a unique hash code
  1745. */
  1746. public getHashCode(): number {
  1747. let hash = this.x | 0;
  1748. hash = (hash * 397) ^ (this.y | 0);
  1749. hash = (hash * 397) ^ (this.z | 0);
  1750. hash = (hash * 397) ^ (this.w | 0);
  1751. return hash;
  1752. }
  1753. // Operators
  1754. /**
  1755. * Returns a new array populated with 4 elements : the Vector4 coordinates.
  1756. * @returns the resulting array
  1757. */
  1758. public asArray(): number[] {
  1759. var result = new Array<number>();
  1760. this.toArray(result, 0);
  1761. return result;
  1762. }
  1763. /**
  1764. * Populates the given array from the given index with the Vector4 coordinates.
  1765. * @param array array to populate
  1766. * @param index index of the array to start at (default: 0)
  1767. * @returns the Vector4.
  1768. */
  1769. public toArray(array: FloatArray, index?: number): Vector4 {
  1770. if (index === undefined) {
  1771. index = 0;
  1772. }
  1773. array[index] = this.x;
  1774. array[index + 1] = this.y;
  1775. array[index + 2] = this.z;
  1776. array[index + 3] = this.w;
  1777. return this;
  1778. }
  1779. /**
  1780. * Adds the given vector to the current Vector4.
  1781. * @param otherVector the vector to add
  1782. * @returns the updated Vector4.
  1783. */
  1784. public addInPlace(otherVector: DeepImmutable<Vector4>): Vector4 {
  1785. this.x += otherVector.x;
  1786. this.y += otherVector.y;
  1787. this.z += otherVector.z;
  1788. this.w += otherVector.w;
  1789. return this;
  1790. }
  1791. /**
  1792. * Returns a new Vector4 as the result of the addition of the current Vector4 and the given one.
  1793. * @param otherVector the vector to add
  1794. * @returns the resulting vector
  1795. */
  1796. public add(otherVector: DeepImmutable<Vector4>): Vector4 {
  1797. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  1798. }
  1799. /**
  1800. * Updates the given vector "result" with the result of the addition of the current Vector4 and the given one.
  1801. * @param otherVector the vector to add
  1802. * @param result the vector to store the result
  1803. * @returns the current Vector4.
  1804. */
  1805. public addToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  1806. result.x = this.x + otherVector.x;
  1807. result.y = this.y + otherVector.y;
  1808. result.z = this.z + otherVector.z;
  1809. result.w = this.w + otherVector.w;
  1810. return this;
  1811. }
  1812. /**
  1813. * Subtract in place the given vector from the current Vector4.
  1814. * @param otherVector the vector to subtract
  1815. * @returns the updated Vector4.
  1816. */
  1817. public subtractInPlace(otherVector: DeepImmutable<Vector4>): Vector4 {
  1818. this.x -= otherVector.x;
  1819. this.y -= otherVector.y;
  1820. this.z -= otherVector.z;
  1821. this.w -= otherVector.w;
  1822. return this;
  1823. }
  1824. /**
  1825. * Returns a new Vector4 with the result of the subtraction of the given vector from the current Vector4.
  1826. * @param otherVector the vector to add
  1827. * @returns the new vector with the result
  1828. */
  1829. public subtract(otherVector: DeepImmutable<Vector4>): Vector4 {
  1830. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  1831. }
  1832. /**
  1833. * Sets the given vector "result" with the result of the subtraction of the given vector from the current Vector4.
  1834. * @param otherVector the vector to subtract
  1835. * @param result the vector to store the result
  1836. * @returns the current Vector4.
  1837. */
  1838. public subtractToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  1839. result.x = this.x - otherVector.x;
  1840. result.y = this.y - otherVector.y;
  1841. result.z = this.z - otherVector.z;
  1842. result.w = this.w - otherVector.w;
  1843. return this;
  1844. }
  1845. /**
  1846. * Returns a new Vector4 set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  1847. */
  1848. /**
  1849. * Returns a new Vector4 set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  1850. * @param x value to subtract
  1851. * @param y value to subtract
  1852. * @param z value to subtract
  1853. * @param w value to subtract
  1854. * @returns new vector containing the result
  1855. */
  1856. public subtractFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  1857. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  1858. }
  1859. /**
  1860. * Sets the given vector "result" set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  1861. * @param x value to subtract
  1862. * @param y value to subtract
  1863. * @param z value to subtract
  1864. * @param w value to subtract
  1865. * @param result the vector to store the result in
  1866. * @returns the current Vector4.
  1867. */
  1868. public subtractFromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): Vector4 {
  1869. result.x = this.x - x;
  1870. result.y = this.y - y;
  1871. result.z = this.z - z;
  1872. result.w = this.w - w;
  1873. return this;
  1874. }
  1875. /**
  1876. * Returns a new Vector4 set with the current Vector4 negated coordinates.
  1877. * @returns a new vector with the negated values
  1878. */
  1879. public negate(): Vector4 {
  1880. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  1881. }
  1882. /**
  1883. * Multiplies the current Vector4 coordinates by scale (float).
  1884. * @param scale the number to scale with
  1885. * @returns the updated Vector4.
  1886. */
  1887. public scaleInPlace(scale: number): Vector4 {
  1888. this.x *= scale;
  1889. this.y *= scale;
  1890. this.z *= scale;
  1891. this.w *= scale;
  1892. return this;
  1893. }
  1894. /**
  1895. * Returns a new Vector4 set with the current Vector4 coordinates multiplied by scale (float).
  1896. * @param scale the number to scale with
  1897. * @returns a new vector with the result
  1898. */
  1899. public scale(scale: number): Vector4 {
  1900. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  1901. }
  1902. /**
  1903. * Sets the given vector "result" with the current Vector4 coordinates multiplied by scale (float).
  1904. * @param scale the number to scale with
  1905. * @param result a vector to store the result in
  1906. * @returns the current Vector4.
  1907. */
  1908. public scaleToRef(scale: number, result: Vector4): Vector4 {
  1909. result.x = this.x * scale;
  1910. result.y = this.y * scale;
  1911. result.z = this.z * scale;
  1912. result.w = this.w * scale;
  1913. return this;
  1914. }
  1915. /**
  1916. * Scale the current Vector4 values by a factor and add the result to a given Vector4
  1917. * @param scale defines the scale factor
  1918. * @param result defines the Vector4 object where to store the result
  1919. * @returns the unmodified current Vector4
  1920. */
  1921. public scaleAndAddToRef(scale: number, result: Vector4): Vector4 {
  1922. result.x += this.x * scale;
  1923. result.y += this.y * scale;
  1924. result.z += this.z * scale;
  1925. result.w += this.w * scale;
  1926. return this;
  1927. }
  1928. /**
  1929. * Boolean : True if the current Vector4 coordinates are stricly equal to the given ones.
  1930. * @param otherVector the vector to compare against
  1931. * @returns true if they are equal
  1932. */
  1933. public equals(otherVector: DeepImmutable<Vector4>): boolean {
  1934. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  1935. }
  1936. /**
  1937. * Boolean : True if the current Vector4 coordinates are each beneath the distance "epsilon" from the given vector ones.
  1938. * @param otherVector vector to compare against
  1939. * @param epsilon (Default: very small number)
  1940. * @returns true if they are equal
  1941. */
  1942. public equalsWithEpsilon(otherVector: DeepImmutable<Vector4>, epsilon: number = Epsilon): boolean {
  1943. return otherVector
  1944. && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon)
  1945. && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon)
  1946. && Scalar.WithinEpsilon(this.z, otherVector.z, epsilon)
  1947. && Scalar.WithinEpsilon(this.w, otherVector.w, epsilon);
  1948. }
  1949. /**
  1950. * Boolean : True if the given floats are strictly equal to the current Vector4 coordinates.
  1951. * @param x x value to compare against
  1952. * @param y y value to compare against
  1953. * @param z z value to compare against
  1954. * @param w w value to compare against
  1955. * @returns true if equal
  1956. */
  1957. public equalsToFloats(x: number, y: number, z: number, w: number): boolean {
  1958. return this.x === x && this.y === y && this.z === z && this.w === w;
  1959. }
  1960. /**
  1961. * Multiplies in place the current Vector4 by the given one.
  1962. * @param otherVector vector to multiple with
  1963. * @returns the updated Vector4.
  1964. */
  1965. public multiplyInPlace(otherVector: Vector4): Vector4 {
  1966. this.x *= otherVector.x;
  1967. this.y *= otherVector.y;
  1968. this.z *= otherVector.z;
  1969. this.w *= otherVector.w;
  1970. return this;
  1971. }
  1972. /**
  1973. * Returns a new Vector4 set with the multiplication result of the current Vector4 and the given one.
  1974. * @param otherVector vector to multiple with
  1975. * @returns resulting new vector
  1976. */
  1977. public multiply(otherVector: DeepImmutable<Vector4>): Vector4 {
  1978. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  1979. }
  1980. /**
  1981. * Updates the given vector "result" with the multiplication result of the current Vector4 and the given one.
  1982. * @param otherVector vector to multiple with
  1983. * @param result vector to store the result
  1984. * @returns the current Vector4.
  1985. */
  1986. public multiplyToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  1987. result.x = this.x * otherVector.x;
  1988. result.y = this.y * otherVector.y;
  1989. result.z = this.z * otherVector.z;
  1990. result.w = this.w * otherVector.w;
  1991. return this;
  1992. }
  1993. /**
  1994. * Returns a new Vector4 set with the multiplication result of the given floats and the current Vector4 coordinates.
  1995. * @param x x value multiply with
  1996. * @param y y value multiply with
  1997. * @param z z value multiply with
  1998. * @param w w value multiply with
  1999. * @returns resulting new vector
  2000. */
  2001. public multiplyByFloats(x: number, y: number, z: number, w: number): Vector4 {
  2002. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  2003. }
  2004. /**
  2005. * Returns a new Vector4 set with the division result of the current Vector4 by the given one.
  2006. * @param otherVector vector to devide with
  2007. * @returns resulting new vector
  2008. */
  2009. public divide(otherVector: DeepImmutable<Vector4>): Vector4 {
  2010. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  2011. }
  2012. /**
  2013. * Updates the given vector "result" with the division result of the current Vector4 by the given one.
  2014. * @param otherVector vector to devide with
  2015. * @param result vector to store the result
  2016. * @returns the current Vector4.
  2017. */
  2018. public divideToRef(otherVector: DeepImmutable<Vector4>, result: Vector4): Vector4 {
  2019. result.x = this.x / otherVector.x;
  2020. result.y = this.y / otherVector.y;
  2021. result.z = this.z / otherVector.z;
  2022. result.w = this.w / otherVector.w;
  2023. return this;
  2024. }
  2025. /**
  2026. * Divides the current Vector3 coordinates by the given ones.
  2027. * @param otherVector vector to devide with
  2028. * @returns the updated Vector3.
  2029. */
  2030. public divideInPlace(otherVector: DeepImmutable<Vector4>): Vector4 {
  2031. return this.divideToRef(otherVector, this);
  2032. }
  2033. /**
  2034. * Updates the Vector4 coordinates with the minimum values between its own and the given vector ones
  2035. * @param other defines the second operand
  2036. * @returns the current updated Vector4
  2037. */
  2038. public minimizeInPlace(other: DeepImmutable<Vector4>): Vector4 {
  2039. if (other.x < this.x) { this.x = other.x; }
  2040. if (other.y < this.y) { this.y = other.y; }
  2041. if (other.z < this.z) { this.z = other.z; }
  2042. if (other.w < this.w) { this.w = other.w; }
  2043. return this;
  2044. }
  2045. /**
  2046. * Updates the Vector4 coordinates with the maximum values between its own and the given vector ones
  2047. * @param other defines the second operand
  2048. * @returns the current updated Vector4
  2049. */
  2050. public maximizeInPlace(other: DeepImmutable<Vector4>): Vector4 {
  2051. if (other.x > this.x) { this.x = other.x; }
  2052. if (other.y > this.y) { this.y = other.y; }
  2053. if (other.z > this.z) { this.z = other.z; }
  2054. if (other.w > this.w) { this.w = other.w; }
  2055. return this;
  2056. }
  2057. /**
  2058. * Gets a new Vector4 from current Vector4 floored values
  2059. * @returns a new Vector4
  2060. */
  2061. public floor(): Vector4 {
  2062. return new Vector4(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z), Math.floor(this.w));
  2063. }
  2064. /**
  2065. * Gets a new Vector4 from current Vector3 floored values
  2066. * @returns a new Vector4
  2067. */
  2068. public fract(): Vector4 {
  2069. return new Vector4(this.x - Math.floor(this.x), this.y - Math.floor(this.y), this.z - Math.floor(this.z), this.w - Math.floor(this.w));
  2070. }
  2071. // Properties
  2072. /**
  2073. * Returns the Vector4 length (float).
  2074. * @returns the length
  2075. */
  2076. public length(): number {
  2077. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  2078. }
  2079. /**
  2080. * Returns the Vector4 squared length (float).
  2081. * @returns the length squared
  2082. */
  2083. public lengthSquared(): number {
  2084. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  2085. }
  2086. // Methods
  2087. /**
  2088. * Normalizes in place the Vector4.
  2089. * @returns the updated Vector4.
  2090. */
  2091. public normalize(): Vector4 {
  2092. var len = this.length();
  2093. if (len === 0) {
  2094. return this;
  2095. }
  2096. return this.scaleInPlace(1.0 / len);
  2097. }
  2098. /**
  2099. * Returns a new Vector3 from the Vector4 (x, y, z) coordinates.
  2100. * @returns this converted to a new vector3
  2101. */
  2102. public toVector3(): Vector3 {
  2103. return new Vector3(this.x, this.y, this.z);
  2104. }
  2105. /**
  2106. * Returns a new Vector4 copied from the current one.
  2107. * @returns the new cloned vector
  2108. */
  2109. public clone(): Vector4 {
  2110. return new Vector4(this.x, this.y, this.z, this.w);
  2111. }
  2112. /**
  2113. * Updates the current Vector4 with the given one coordinates.
  2114. * @param source the source vector to copy from
  2115. * @returns the updated Vector4.
  2116. */
  2117. public copyFrom(source: DeepImmutable<Vector4>): Vector4 {
  2118. this.x = source.x;
  2119. this.y = source.y;
  2120. this.z = source.z;
  2121. this.w = source.w;
  2122. return this;
  2123. }
  2124. /**
  2125. * Updates the current Vector4 coordinates with the given floats.
  2126. * @param x float to copy from
  2127. * @param y float to copy from
  2128. * @param z float to copy from
  2129. * @param w float to copy from
  2130. * @returns the updated Vector4.
  2131. */
  2132. public copyFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  2133. this.x = x;
  2134. this.y = y;
  2135. this.z = z;
  2136. this.w = w;
  2137. return this;
  2138. }
  2139. /**
  2140. * Updates the current Vector4 coordinates with the given floats.
  2141. * @param x float to set from
  2142. * @param y float to set from
  2143. * @param z float to set from
  2144. * @param w float to set from
  2145. * @returns the updated Vector4.
  2146. */
  2147. public set(x: number, y: number, z: number, w: number): Vector4 {
  2148. return this.copyFromFloats(x, y, z, w);
  2149. }
  2150. /**
  2151. * Copies the given float to the current Vector3 coordinates
  2152. * @param v defines the x, y, z and w coordinates of the operand
  2153. * @returns the current updated Vector3
  2154. */
  2155. public setAll(v: number): Vector4 {
  2156. this.x = this.y = this.z = this.w = v;
  2157. return this;
  2158. }
  2159. // Statics
  2160. /**
  2161. * Returns a new Vector4 set from the starting index of the given array.
  2162. * @param array the array to pull values from
  2163. * @param offset the offset into the array to start at
  2164. * @returns the new vector
  2165. */
  2166. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset?: number): Vector4 {
  2167. if (!offset) {
  2168. offset = 0;
  2169. }
  2170. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  2171. }
  2172. /**
  2173. * Updates the given vector "result" from the starting index of the given array.
  2174. * @param array the array to pull values from
  2175. * @param offset the offset into the array to start at
  2176. * @param result the vector to store the result in
  2177. */
  2178. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Vector4): void {
  2179. result.x = array[offset];
  2180. result.y = array[offset + 1];
  2181. result.z = array[offset + 2];
  2182. result.w = array[offset + 3];
  2183. }
  2184. /**
  2185. * Updates the given vector "result" from the starting index of the given Float32Array.
  2186. * @param array the array to pull values from
  2187. * @param offset the offset into the array to start at
  2188. * @param result the vector to store the result in
  2189. */
  2190. public static FromFloatArrayToRef(array: DeepImmutable<Float32Array>, offset: number, result: Vector4): void {
  2191. Vector4.FromArrayToRef(array, offset, result);
  2192. }
  2193. /**
  2194. * Updates the given vector "result" coordinates from the given floats.
  2195. * @param x float to set from
  2196. * @param y float to set from
  2197. * @param z float to set from
  2198. * @param w float to set from
  2199. * @param result the vector to the floats in
  2200. */
  2201. public static FromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): void {
  2202. result.x = x;
  2203. result.y = y;
  2204. result.z = z;
  2205. result.w = w;
  2206. }
  2207. /**
  2208. * Returns a new Vector4 set to (0.0, 0.0, 0.0, 0.0)
  2209. * @returns the new vector
  2210. */
  2211. public static Zero(): Vector4 {
  2212. return new Vector4(0.0, 0.0, 0.0, 0.0);
  2213. }
  2214. /**
  2215. * Returns a new Vector4 set to (1.0, 1.0, 1.0, 1.0)
  2216. * @returns the new vector
  2217. */
  2218. public static One(): Vector4 {
  2219. return new Vector4(1.0, 1.0, 1.0, 1.0);
  2220. }
  2221. /**
  2222. * Returns a new normalized Vector4 from the given one.
  2223. * @param vector the vector to normalize
  2224. * @returns the vector
  2225. */
  2226. public static Normalize(vector: DeepImmutable<Vector4>): Vector4 {
  2227. var result = Vector4.Zero();
  2228. Vector4.NormalizeToRef(vector, result);
  2229. return result;
  2230. }
  2231. /**
  2232. * Updates the given vector "result" from the normalization of the given one.
  2233. * @param vector the vector to normalize
  2234. * @param result the vector to store the result in
  2235. */
  2236. public static NormalizeToRef(vector: DeepImmutable<Vector4>, result: Vector4): void {
  2237. result.copyFrom(vector);
  2238. result.normalize();
  2239. }
  2240. /**
  2241. * Returns a vector with the minimum values from the left and right vectors
  2242. * @param left left vector to minimize
  2243. * @param right right vector to minimize
  2244. * @returns a new vector with the minimum of the left and right vector values
  2245. */
  2246. public static Minimize(left: DeepImmutable<Vector4>, right: DeepImmutable<Vector4>): Vector4 {
  2247. var min = left.clone();
  2248. min.minimizeInPlace(right);
  2249. return min;
  2250. }
  2251. /**
  2252. * Returns a vector with the maximum values from the left and right vectors
  2253. * @param left left vector to maximize
  2254. * @param right right vector to maximize
  2255. * @returns a new vector with the maximum of the left and right vector values
  2256. */
  2257. public static Maximize(left: DeepImmutable<Vector4>, right: DeepImmutable<Vector4>): Vector4 {
  2258. var max = left.clone();
  2259. max.maximizeInPlace(right);
  2260. return max;
  2261. }
  2262. /**
  2263. * Returns the distance (float) between the vectors "value1" and "value2".
  2264. * @param value1 value to calulate the distance between
  2265. * @param value2 value to calulate the distance between
  2266. * @return the distance between the two vectors
  2267. */
  2268. public static Distance(value1: DeepImmutable<Vector4>, value2: DeepImmutable<Vector4>): number {
  2269. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  2270. }
  2271. /**
  2272. * Returns the squared distance (float) between the vectors "value1" and "value2".
  2273. * @param value1 value to calulate the distance between
  2274. * @param value2 value to calulate the distance between
  2275. * @return the distance between the two vectors squared
  2276. */
  2277. public static DistanceSquared(value1: DeepImmutable<Vector4>, value2: DeepImmutable<Vector4>): number {
  2278. var x = value1.x - value2.x;
  2279. var y = value1.y - value2.y;
  2280. var z = value1.z - value2.z;
  2281. var w = value1.w - value2.w;
  2282. return (x * x) + (y * y) + (z * z) + (w * w);
  2283. }
  2284. /**
  2285. * Returns a new Vector4 located at the center between the vectors "value1" and "value2".
  2286. * @param value1 value to calulate the center between
  2287. * @param value2 value to calulate the center between
  2288. * @return the center between the two vectors
  2289. */
  2290. public static Center(value1: DeepImmutable<Vector4>, value2: DeepImmutable<Vector4>): Vector4 {
  2291. var center = value1.add(value2);
  2292. center.scaleInPlace(0.5);
  2293. return center;
  2294. }
  2295. /**
  2296. * Returns a new Vector4 set with the result of the normal transformation by the given matrix of the given vector.
  2297. * This methods computes transformed normalized direction vectors only.
  2298. * @param vector the vector to transform
  2299. * @param transformation the transformation matrix to apply
  2300. * @returns the new vector
  2301. */
  2302. public static TransformNormal(vector: DeepImmutable<Vector4>, transformation: DeepImmutable<Matrix>): Vector4 {
  2303. var result = Vector4.Zero();
  2304. Vector4.TransformNormalToRef(vector, transformation, result);
  2305. return result;
  2306. }
  2307. /**
  2308. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given vector.
  2309. * This methods computes transformed normalized direction vectors only.
  2310. * @param vector the vector to transform
  2311. * @param transformation the transformation matrix to apply
  2312. * @param result the vector to store the result in
  2313. */
  2314. public static TransformNormalToRef(vector: DeepImmutable<Vector4>, transformation: DeepImmutable<Matrix>, result: Vector4): void {
  2315. const m = transformation.m;
  2316. var x = (vector.x * m[0]) + (vector.y * m[4]) + (vector.z * m[8]);
  2317. var y = (vector.x * m[1]) + (vector.y * m[5]) + (vector.z * m[9]);
  2318. var z = (vector.x * m[2]) + (vector.y * m[6]) + (vector.z * m[10]);
  2319. result.x = x;
  2320. result.y = y;
  2321. result.z = z;
  2322. result.w = vector.w;
  2323. }
  2324. /**
  2325. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given floats (x, y, z, w).
  2326. * This methods computes transformed normalized direction vectors only.
  2327. * @param x value to transform
  2328. * @param y value to transform
  2329. * @param z value to transform
  2330. * @param w value to transform
  2331. * @param transformation the transformation matrix to apply
  2332. * @param result the vector to store the results in
  2333. */
  2334. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, w: number, transformation: DeepImmutable<Matrix>, result: Vector4): void {
  2335. const m = transformation.m;
  2336. result.x = (x * m[0]) + (y * m[4]) + (z * m[8]);
  2337. result.y = (x * m[1]) + (y * m[5]) + (z * m[9]);
  2338. result.z = (x * m[2]) + (y * m[6]) + (z * m[10]);
  2339. result.w = w;
  2340. }
  2341. /**
  2342. * Creates a new Vector4 from a Vector3
  2343. * @param source defines the source data
  2344. * @param w defines the 4th component (default is 0)
  2345. * @returns a new Vector4
  2346. */
  2347. public static FromVector3(source: Vector3, w: number = 0) {
  2348. return new Vector4(source.x, source.y, source.z, w);
  2349. }
  2350. }
  2351. /**
  2352. * Class used to store quaternion data
  2353. * @see https://en.wikipedia.org/wiki/Quaternion
  2354. * @see http://doc.babylonjs.com/features/position,_rotation,_scaling
  2355. */
  2356. export class Quaternion {
  2357. /**
  2358. * Creates a new Quaternion from the given floats
  2359. * @param x defines the first component (0 by default)
  2360. * @param y defines the second component (0 by default)
  2361. * @param z defines the third component (0 by default)
  2362. * @param w defines the fourth component (1.0 by default)
  2363. */
  2364. constructor(
  2365. /** defines the first component (0 by default) */
  2366. public x: number = 0.0,
  2367. /** defines the second component (0 by default) */
  2368. public y: number = 0.0,
  2369. /** defines the third component (0 by default) */
  2370. public z: number = 0.0,
  2371. /** defines the fourth component (1.0 by default) */
  2372. public w: number = 1.0) {
  2373. }
  2374. /**
  2375. * Gets a string representation for the current quaternion
  2376. * @returns a string with the Quaternion coordinates
  2377. */
  2378. public toString(): string {
  2379. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  2380. }
  2381. /**
  2382. * Gets the class name of the quaternion
  2383. * @returns the string "Quaternion"
  2384. */
  2385. public getClassName(): string {
  2386. return "Quaternion";
  2387. }
  2388. /**
  2389. * Gets a hash code for this quaternion
  2390. * @returns the quaternion hash code
  2391. */
  2392. public getHashCode(): number {
  2393. let hash = this.x | 0;
  2394. hash = (hash * 397) ^ (this.y | 0);
  2395. hash = (hash * 397) ^ (this.z | 0);
  2396. hash = (hash * 397) ^ (this.w | 0);
  2397. return hash;
  2398. }
  2399. /**
  2400. * Copy the quaternion to an array
  2401. * @returns a new array populated with 4 elements from the quaternion coordinates
  2402. */
  2403. public asArray(): number[] {
  2404. return [this.x, this.y, this.z, this.w];
  2405. }
  2406. /**
  2407. * Check if two quaternions are equals
  2408. * @param otherQuaternion defines the second operand
  2409. * @return true if the current quaternion and the given one coordinates are strictly equals
  2410. */
  2411. public equals(otherQuaternion: DeepImmutable<Quaternion>): boolean {
  2412. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  2413. }
  2414. /**
  2415. * Clone the current quaternion
  2416. * @returns a new quaternion copied from the current one
  2417. */
  2418. public clone(): Quaternion {
  2419. return new Quaternion(this.x, this.y, this.z, this.w);
  2420. }
  2421. /**
  2422. * Copy a quaternion to the current one
  2423. * @param other defines the other quaternion
  2424. * @returns the updated current quaternion
  2425. */
  2426. public copyFrom(other: DeepImmutable<Quaternion>): Quaternion {
  2427. this.x = other.x;
  2428. this.y = other.y;
  2429. this.z = other.z;
  2430. this.w = other.w;
  2431. return this;
  2432. }
  2433. /**
  2434. * Updates the current quaternion with the given float coordinates
  2435. * @param x defines the x coordinate
  2436. * @param y defines the y coordinate
  2437. * @param z defines the z coordinate
  2438. * @param w defines the w coordinate
  2439. * @returns the updated current quaternion
  2440. */
  2441. public copyFromFloats(x: number, y: number, z: number, w: number): Quaternion {
  2442. this.x = x;
  2443. this.y = y;
  2444. this.z = z;
  2445. this.w = w;
  2446. return this;
  2447. }
  2448. /**
  2449. * Updates the current quaternion from the given float coordinates
  2450. * @param x defines the x coordinate
  2451. * @param y defines the y coordinate
  2452. * @param z defines the z coordinate
  2453. * @param w defines the w coordinate
  2454. * @returns the updated current quaternion
  2455. */
  2456. public set(x: number, y: number, z: number, w: number): Quaternion {
  2457. return this.copyFromFloats(x, y, z, w);
  2458. }
  2459. /**
  2460. * Adds two quaternions
  2461. * @param other defines the second operand
  2462. * @returns a new quaternion as the addition result of the given one and the current quaternion
  2463. */
  2464. public add(other: DeepImmutable<Quaternion>): Quaternion {
  2465. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  2466. }
  2467. /**
  2468. * Add a quaternion to the current one
  2469. * @param other defines the quaternion to add
  2470. * @returns the current quaternion
  2471. */
  2472. public addInPlace(other: DeepImmutable<Quaternion>): Quaternion {
  2473. this.x += other.x;
  2474. this.y += other.y;
  2475. this.z += other.z;
  2476. this.w += other.w;
  2477. return this;
  2478. }
  2479. /**
  2480. * Subtract two quaternions
  2481. * @param other defines the second operand
  2482. * @returns a new quaternion as the subtraction result of the given one from the current one
  2483. */
  2484. public subtract(other: Quaternion): Quaternion {
  2485. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  2486. }
  2487. /**
  2488. * Multiplies the current quaternion by a scale factor
  2489. * @param value defines the scale factor
  2490. * @returns a new quaternion set by multiplying the current quaternion coordinates by the float "scale"
  2491. */
  2492. public scale(value: number): Quaternion {
  2493. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  2494. }
  2495. /**
  2496. * Scale the current quaternion values by a factor and stores the result to a given quaternion
  2497. * @param scale defines the scale factor
  2498. * @param result defines the Quaternion object where to store the result
  2499. * @returns the unmodified current quaternion
  2500. */
  2501. public scaleToRef(scale: number, result: Quaternion): Quaternion {
  2502. result.x = this.x * scale;
  2503. result.y = this.y * scale;
  2504. result.z = this.z * scale;
  2505. result.w = this.w * scale;
  2506. return this;
  2507. }
  2508. /**
  2509. * Multiplies in place the current quaternion by a scale factor
  2510. * @param value defines the scale factor
  2511. * @returns the current modified quaternion
  2512. */
  2513. public scaleInPlace(value: number): Quaternion {
  2514. this.x *= value;
  2515. this.y *= value;
  2516. this.z *= value;
  2517. this.w *= value;
  2518. return this;
  2519. }
  2520. /**
  2521. * Scale the current quaternion values by a factor and add the result to a given quaternion
  2522. * @param scale defines the scale factor
  2523. * @param result defines the Quaternion object where to store the result
  2524. * @returns the unmodified current quaternion
  2525. */
  2526. public scaleAndAddToRef(scale: number, result: Quaternion): Quaternion {
  2527. result.x += this.x * scale;
  2528. result.y += this.y * scale;
  2529. result.z += this.z * scale;
  2530. result.w += this.w * scale;
  2531. return this;
  2532. }
  2533. /**
  2534. * Multiplies two quaternions
  2535. * @param q1 defines the second operand
  2536. * @returns a new quaternion set as the multiplication result of the current one with the given one "q1"
  2537. */
  2538. public multiply(q1: DeepImmutable<Quaternion>): Quaternion {
  2539. var result = new Quaternion(0, 0, 0, 1.0);
  2540. this.multiplyToRef(q1, result);
  2541. return result;
  2542. }
  2543. /**
  2544. * Sets the given "result" as the the multiplication result of the current one with the given one "q1"
  2545. * @param q1 defines the second operand
  2546. * @param result defines the target quaternion
  2547. * @returns the current quaternion
  2548. */
  2549. public multiplyToRef(q1: DeepImmutable<Quaternion>, result: Quaternion): Quaternion {
  2550. var x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  2551. var y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  2552. var z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  2553. var w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  2554. result.copyFromFloats(x, y, z, w);
  2555. return this;
  2556. }
  2557. /**
  2558. * Updates the current quaternion with the multiplication of itself with the given one "q1"
  2559. * @param q1 defines the second operand
  2560. * @returns the currentupdated quaternion
  2561. */
  2562. public multiplyInPlace(q1: DeepImmutable<Quaternion>): Quaternion {
  2563. this.multiplyToRef(q1, this);
  2564. return this;
  2565. }
  2566. /**
  2567. * Conjugates (1-q) the current quaternion and stores the result in the given quaternion
  2568. * @param ref defines the target quaternion
  2569. * @returns the current quaternion
  2570. */
  2571. public conjugateToRef(ref: Quaternion): Quaternion {
  2572. ref.copyFromFloats(-this.x, -this.y, -this.z, this.w);
  2573. return this;
  2574. }
  2575. /**
  2576. * Conjugates in place (1-q) the current quaternion
  2577. * @returns the current updated quaternion
  2578. */
  2579. public conjugateInPlace(): Quaternion {
  2580. this.x *= -1;
  2581. this.y *= -1;
  2582. this.z *= -1;
  2583. return this;
  2584. }
  2585. /**
  2586. * Conjugates in place (1-q) the current quaternion
  2587. * @returns a new quaternion
  2588. */
  2589. public conjugate(): Quaternion {
  2590. var result = new Quaternion(-this.x, -this.y, -this.z, this.w);
  2591. return result;
  2592. }
  2593. /**
  2594. * Gets length of current quaternion
  2595. * @returns the quaternion length (float)
  2596. */
  2597. public length(): number {
  2598. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  2599. }
  2600. /**
  2601. * Normalize in place the current quaternion
  2602. * @returns the current updated quaternion
  2603. */
  2604. public normalize(): Quaternion {
  2605. var len = this.length();
  2606. if (len === 0) {
  2607. return this;
  2608. }
  2609. var inv = 1.0 / len;
  2610. this.x *= inv;
  2611. this.y *= inv;
  2612. this.z *= inv;
  2613. this.w *= inv;
  2614. return this;
  2615. }
  2616. /**
  2617. * Returns a new Vector3 set with the Euler angles translated from the current quaternion
  2618. * @param order is a reserved parameter and is ignore for now
  2619. * @returns a new Vector3 containing the Euler angles
  2620. */
  2621. public toEulerAngles(order = "YZX"): Vector3 {
  2622. var result = Vector3.Zero();
  2623. this.toEulerAnglesToRef(result);
  2624. return result;
  2625. }
  2626. /**
  2627. * Sets the given vector3 "result" with the Euler angles translated from the current quaternion
  2628. * @param result defines the vector which will be filled with the Euler angles
  2629. * @param order is a reserved parameter and is ignore for now
  2630. * @returns the current unchanged quaternion
  2631. */
  2632. public toEulerAnglesToRef(result: Vector3): Quaternion {
  2633. var qz = this.z;
  2634. var qx = this.x;
  2635. var qy = this.y;
  2636. var qw = this.w;
  2637. var sqw = qw * qw;
  2638. var sqz = qz * qz;
  2639. var sqx = qx * qx;
  2640. var sqy = qy * qy;
  2641. var zAxisY = qy * qz - qx * qw;
  2642. var limit = .4999999;
  2643. if (zAxisY < -limit) {
  2644. result.y = 2 * Math.atan2(qy, qw);
  2645. result.x = Math.PI / 2;
  2646. result.z = 0;
  2647. } else if (zAxisY > limit) {
  2648. result.y = 2 * Math.atan2(qy, qw);
  2649. result.x = -Math.PI / 2;
  2650. result.z = 0;
  2651. } else {
  2652. result.z = Math.atan2(2.0 * (qx * qy + qz * qw), (-sqz - sqx + sqy + sqw));
  2653. result.x = Math.asin(-2.0 * (qz * qy - qx * qw));
  2654. result.y = Math.atan2(2.0 * (qz * qx + qy * qw), (sqz - sqx - sqy + sqw));
  2655. }
  2656. return this;
  2657. }
  2658. /**
  2659. * Updates the given rotation matrix with the current quaternion values
  2660. * @param result defines the target matrix
  2661. * @returns the current unchanged quaternion
  2662. */
  2663. public toRotationMatrix(result: Matrix): Quaternion {
  2664. Matrix.FromQuaternionToRef(this, result);
  2665. return this;
  2666. }
  2667. /**
  2668. * Updates the current quaternion from the given rotation matrix values
  2669. * @param matrix defines the source matrix
  2670. * @returns the current updated quaternion
  2671. */
  2672. public fromRotationMatrix(matrix: DeepImmutable<Matrix>): Quaternion {
  2673. Quaternion.FromRotationMatrixToRef(matrix, this);
  2674. return this;
  2675. }
  2676. // Statics
  2677. /**
  2678. * Creates a new quaternion from a rotation matrix
  2679. * @param matrix defines the source matrix
  2680. * @returns a new quaternion created from the given rotation matrix values
  2681. */
  2682. public static FromRotationMatrix(matrix: DeepImmutable<Matrix>): Quaternion {
  2683. var result = new Quaternion();
  2684. Quaternion.FromRotationMatrixToRef(matrix, result);
  2685. return result;
  2686. }
  2687. /**
  2688. * Updates the given quaternion with the given rotation matrix values
  2689. * @param matrix defines the source matrix
  2690. * @param result defines the target quaternion
  2691. */
  2692. public static FromRotationMatrixToRef(matrix: DeepImmutable<Matrix>, result: Quaternion): void {
  2693. var data = matrix.m;
  2694. var m11 = data[0], m12 = data[4], m13 = data[8];
  2695. var m21 = data[1], m22 = data[5], m23 = data[9];
  2696. var m31 = data[2], m32 = data[6], m33 = data[10];
  2697. var trace = m11 + m22 + m33;
  2698. var s;
  2699. if (trace > 0) {
  2700. s = 0.5 / Math.sqrt(trace + 1.0);
  2701. result.w = 0.25 / s;
  2702. result.x = (m32 - m23) * s;
  2703. result.y = (m13 - m31) * s;
  2704. result.z = (m21 - m12) * s;
  2705. } else if (m11 > m22 && m11 > m33) {
  2706. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  2707. result.w = (m32 - m23) / s;
  2708. result.x = 0.25 * s;
  2709. result.y = (m12 + m21) / s;
  2710. result.z = (m13 + m31) / s;
  2711. } else if (m22 > m33) {
  2712. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  2713. result.w = (m13 - m31) / s;
  2714. result.x = (m12 + m21) / s;
  2715. result.y = 0.25 * s;
  2716. result.z = (m23 + m32) / s;
  2717. } else {
  2718. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  2719. result.w = (m21 - m12) / s;
  2720. result.x = (m13 + m31) / s;
  2721. result.y = (m23 + m32) / s;
  2722. result.z = 0.25 * s;
  2723. }
  2724. }
  2725. /**
  2726. * Returns the dot product (float) between the quaternions "left" and "right"
  2727. * @param left defines the left operand
  2728. * @param right defines the right operand
  2729. * @returns the dot product
  2730. */
  2731. public static Dot(left: DeepImmutable<Quaternion>, right: DeepImmutable<Quaternion>): number {
  2732. return (left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w);
  2733. }
  2734. /**
  2735. * Checks if the two quaternions are close to each other
  2736. * @param quat0 defines the first quaternion to check
  2737. * @param quat1 defines the second quaternion to check
  2738. * @returns true if the two quaternions are close to each other
  2739. */
  2740. public static AreClose(quat0: DeepImmutable<Quaternion>, quat1: DeepImmutable<Quaternion>): boolean {
  2741. let dot = Quaternion.Dot(quat0, quat1);
  2742. return dot >= 0;
  2743. }
  2744. /**
  2745. * Creates an empty quaternion
  2746. * @returns a new quaternion set to (0.0, 0.0, 0.0)
  2747. */
  2748. public static Zero(): Quaternion {
  2749. return new Quaternion(0.0, 0.0, 0.0, 0.0);
  2750. }
  2751. /**
  2752. * Inverse a given quaternion
  2753. * @param q defines the source quaternion
  2754. * @returns a new quaternion as the inverted current quaternion
  2755. */
  2756. public static Inverse(q: DeepImmutable<Quaternion>): Quaternion {
  2757. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  2758. }
  2759. /**
  2760. * Inverse a given quaternion
  2761. * @param q defines the source quaternion
  2762. * @param result the quaternion the result will be stored in
  2763. * @returns the result quaternion
  2764. */
  2765. public static InverseToRef(q: Quaternion, result: Quaternion): Quaternion {
  2766. result.set(-q.x, -q.y, -q.z, q.w);
  2767. return result;
  2768. }
  2769. /**
  2770. * Creates an identity quaternion
  2771. * @returns the identity quaternion
  2772. */
  2773. public static Identity(): Quaternion {
  2774. return new Quaternion(0.0, 0.0, 0.0, 1.0);
  2775. }
  2776. /**
  2777. * Gets a boolean indicating if the given quaternion is identity
  2778. * @param quaternion defines the quaternion to check
  2779. * @returns true if the quaternion is identity
  2780. */
  2781. public static IsIdentity(quaternion: DeepImmutable<Quaternion>): boolean {
  2782. return quaternion && quaternion.x === 0 && quaternion.y === 0 && quaternion.z === 0 && quaternion.w === 1;
  2783. }
  2784. /**
  2785. * Creates a quaternion from a rotation around an axis
  2786. * @param axis defines the axis to use
  2787. * @param angle defines the angle to use
  2788. * @returns a new quaternion created from the given axis (Vector3) and angle in radians (float)
  2789. */
  2790. public static RotationAxis(axis: DeepImmutable<Vector3>, angle: number): Quaternion {
  2791. return Quaternion.RotationAxisToRef(axis, angle, new Quaternion());
  2792. }
  2793. /**
  2794. * Creates a rotation around an axis and stores it into the given quaternion
  2795. * @param axis defines the axis to use
  2796. * @param angle defines the angle to use
  2797. * @param result defines the target quaternion
  2798. * @returns the target quaternion
  2799. */
  2800. public static RotationAxisToRef(axis: DeepImmutable<Vector3>, angle: number, result: Quaternion): Quaternion {
  2801. var sin = Math.sin(angle / 2);
  2802. axis.normalize();
  2803. result.w = Math.cos(angle / 2);
  2804. result.x = axis.x * sin;
  2805. result.y = axis.y * sin;
  2806. result.z = axis.z * sin;
  2807. return result;
  2808. }
  2809. /**
  2810. * Creates a new quaternion from data stored into an array
  2811. * @param array defines the data source
  2812. * @param offset defines the offset in the source array where the data starts
  2813. * @returns a new quaternion
  2814. */
  2815. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset?: number): Quaternion {
  2816. if (!offset) {
  2817. offset = 0;
  2818. }
  2819. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  2820. }
  2821. /**
  2822. * Create a quaternion from Euler rotation angles
  2823. * @param x Pitch
  2824. * @param y Yaw
  2825. * @param z Roll
  2826. * @returns the new Quaternion
  2827. */
  2828. public static FromEulerAngles(x: number, y: number, z: number): Quaternion {
  2829. var q = new Quaternion();
  2830. Quaternion.RotationYawPitchRollToRef(y, x, z, q);
  2831. return q;
  2832. }
  2833. /**
  2834. * Updates a quaternion from Euler rotation angles
  2835. * @param x Pitch
  2836. * @param y Yaw
  2837. * @param z Roll
  2838. * @param result the quaternion to store the result
  2839. * @returns the updated quaternion
  2840. */
  2841. public static FromEulerAnglesToRef(x: number, y: number, z: number, result: Quaternion): Quaternion {
  2842. Quaternion.RotationYawPitchRollToRef(y, x, z, result);
  2843. return result;
  2844. }
  2845. /**
  2846. * Create a quaternion from Euler rotation vector
  2847. * @param vec the Euler vector (x Pitch, y Yaw, z Roll)
  2848. * @returns the new Quaternion
  2849. */
  2850. public static FromEulerVector(vec: DeepImmutable<Vector3>): Quaternion {
  2851. var q = new Quaternion();
  2852. Quaternion.RotationYawPitchRollToRef(vec.y, vec.x, vec.z, q);
  2853. return q;
  2854. }
  2855. /**
  2856. * Updates a quaternion from Euler rotation vector
  2857. * @param vec the Euler vector (x Pitch, y Yaw, z Roll)
  2858. * @param result the quaternion to store the result
  2859. * @returns the updated quaternion
  2860. */
  2861. public static FromEulerVectorToRef(vec: DeepImmutable<Vector3>, result: Quaternion): Quaternion {
  2862. Quaternion.RotationYawPitchRollToRef(vec.y, vec.x, vec.z, result);
  2863. return result;
  2864. }
  2865. /**
  2866. * Creates a new quaternion from the given Euler float angles (y, x, z)
  2867. * @param yaw defines the rotation around Y axis
  2868. * @param pitch defines the rotation around X axis
  2869. * @param roll defines the rotation around Z axis
  2870. * @returns the new quaternion
  2871. */
  2872. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Quaternion {
  2873. var q = new Quaternion();
  2874. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, q);
  2875. return q;
  2876. }
  2877. /**
  2878. * Creates a new rotation from the given Euler float angles (y, x, z) and stores it in the target quaternion
  2879. * @param yaw defines the rotation around Y axis
  2880. * @param pitch defines the rotation around X axis
  2881. * @param roll defines the rotation around Z axis
  2882. * @param result defines the target quaternion
  2883. */
  2884. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Quaternion): void {
  2885. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  2886. var halfRoll = roll * 0.5;
  2887. var halfPitch = pitch * 0.5;
  2888. var halfYaw = yaw * 0.5;
  2889. var sinRoll = Math.sin(halfRoll);
  2890. var cosRoll = Math.cos(halfRoll);
  2891. var sinPitch = Math.sin(halfPitch);
  2892. var cosPitch = Math.cos(halfPitch);
  2893. var sinYaw = Math.sin(halfYaw);
  2894. var cosYaw = Math.cos(halfYaw);
  2895. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  2896. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  2897. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  2898. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  2899. }
  2900. /**
  2901. * Creates a new quaternion from the given Euler float angles expressed in z-x-z orientation
  2902. * @param alpha defines the rotation around first axis
  2903. * @param beta defines the rotation around second axis
  2904. * @param gamma defines the rotation around third axis
  2905. * @returns the new quaternion
  2906. */
  2907. public static RotationAlphaBetaGamma(alpha: number, beta: number, gamma: number): Quaternion {
  2908. var result = new Quaternion();
  2909. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  2910. return result;
  2911. }
  2912. /**
  2913. * Creates a new quaternion from the given Euler float angles expressed in z-x-z orientation and stores it in the target quaternion
  2914. * @param alpha defines the rotation around first axis
  2915. * @param beta defines the rotation around second axis
  2916. * @param gamma defines the rotation around third axis
  2917. * @param result defines the target quaternion
  2918. */
  2919. public static RotationAlphaBetaGammaToRef(alpha: number, beta: number, gamma: number, result: Quaternion): void {
  2920. // Produces a quaternion from Euler angles in the z-x-z orientation
  2921. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  2922. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  2923. var halfBeta = beta * 0.5;
  2924. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  2925. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  2926. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  2927. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  2928. }
  2929. /**
  2930. * Creates a new quaternion containing the rotation value to reach the target (axis1, axis2, axis3) orientation as a rotated XYZ system (axis1, axis2 and axis3 are normalized during this operation)
  2931. * @param axis1 defines the first axis
  2932. * @param axis2 defines the second axis
  2933. * @param axis3 defines the third axis
  2934. * @returns the new quaternion
  2935. */
  2936. public static RotationQuaternionFromAxis(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>): Quaternion {
  2937. var quat = new Quaternion(0.0, 0.0, 0.0, 0.0);
  2938. Quaternion.RotationQuaternionFromAxisToRef(axis1, axis2, axis3, quat);
  2939. return quat;
  2940. }
  2941. /**
  2942. * Creates a rotation value to reach the target (axis1, axis2, axis3) orientation as a rotated XYZ system (axis1, axis2 and axis3 are normalized during this operation) and stores it in the target quaternion
  2943. * @param axis1 defines the first axis
  2944. * @param axis2 defines the second axis
  2945. * @param axis3 defines the third axis
  2946. * @param ref defines the target quaternion
  2947. */
  2948. public static RotationQuaternionFromAxisToRef(axis1: DeepImmutable<Vector3>, axis2: DeepImmutable<Vector3>, axis3: DeepImmutable<Vector3>, ref: Quaternion): void {
  2949. var rotMat = MathTmp.Matrix[0];
  2950. Matrix.FromXYZAxesToRef(axis1.normalize(), axis2.normalize(), axis3.normalize(), rotMat);
  2951. Quaternion.FromRotationMatrixToRef(rotMat, ref);
  2952. }
  2953. /**
  2954. * Interpolates between two quaternions
  2955. * @param left defines first quaternion
  2956. * @param right defines second quaternion
  2957. * @param amount defines the gradient to use
  2958. * @returns the new interpolated quaternion
  2959. */
  2960. public static Slerp(left: DeepImmutable<Quaternion>, right: DeepImmutable<Quaternion>, amount: number): Quaternion {
  2961. var result = Quaternion.Identity();
  2962. Quaternion.SlerpToRef(left, right, amount, result);
  2963. return result;
  2964. }
  2965. /**
  2966. * Interpolates between two quaternions and stores it into a target quaternion
  2967. * @param left defines first quaternion
  2968. * @param right defines second quaternion
  2969. * @param amount defines the gradient to use
  2970. * @param result defines the target quaternion
  2971. */
  2972. public static SlerpToRef(left: DeepImmutable<Quaternion>, right: DeepImmutable<Quaternion>, amount: number, result: Quaternion): void {
  2973. var num2;
  2974. var num3;
  2975. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  2976. var flag = false;
  2977. if (num4 < 0) {
  2978. flag = true;
  2979. num4 = -num4;
  2980. }
  2981. if (num4 > 0.999999) {
  2982. num3 = 1 - amount;
  2983. num2 = flag ? -amount : amount;
  2984. }
  2985. else {
  2986. var num5 = Math.acos(num4);
  2987. var num6 = (1.0 / Math.sin(num5));
  2988. num3 = (Math.sin((1.0 - amount) * num5)) * num6;
  2989. num2 = flag ? ((-Math.sin(amount * num5)) * num6) : ((Math.sin(amount * num5)) * num6);
  2990. }
  2991. result.x = (num3 * left.x) + (num2 * right.x);
  2992. result.y = (num3 * left.y) + (num2 * right.y);
  2993. result.z = (num3 * left.z) + (num2 * right.z);
  2994. result.w = (num3 * left.w) + (num2 * right.w);
  2995. }
  2996. /**
  2997. * Interpolate between two quaternions using Hermite interpolation
  2998. * @param value1 defines first quaternion
  2999. * @param tangent1 defines the incoming tangent
  3000. * @param value2 defines second quaternion
  3001. * @param tangent2 defines the outgoing tangent
  3002. * @param amount defines the target quaternion
  3003. * @returns the new interpolated quaternion
  3004. */
  3005. public static Hermite(value1: DeepImmutable<Quaternion>, tangent1: DeepImmutable<Quaternion>, value2: DeepImmutable<Quaternion>, tangent2: DeepImmutable<Quaternion>, amount: number): Quaternion {
  3006. var squared = amount * amount;
  3007. var cubed = amount * squared;
  3008. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  3009. var part2 = (-2.0 * cubed) + (3.0 * squared);
  3010. var part3 = (cubed - (2.0 * squared)) + amount;
  3011. var part4 = cubed - squared;
  3012. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  3013. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  3014. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  3015. var w = (((value1.w * part1) + (value2.w * part2)) + (tangent1.w * part3)) + (tangent2.w * part4);
  3016. return new Quaternion(x, y, z, w);
  3017. }
  3018. }
  3019. /**
  3020. * Class used to store matrix data (4x4)
  3021. */
  3022. export class Matrix {
  3023. private static _updateFlagSeed = 0;
  3024. private static _identityReadOnly = Matrix.Identity() as DeepImmutable<Matrix>;
  3025. private _isIdentity = false;
  3026. private _isIdentityDirty = true;
  3027. private _isIdentity3x2 = true;
  3028. private _isIdentity3x2Dirty = true;
  3029. /**
  3030. * Gets the update flag of the matrix which is an unique number for the matrix.
  3031. * It will be incremented every time the matrix data change.
  3032. * You can use it to speed the comparison between two versions of the same matrix.
  3033. */
  3034. public updateFlag: number = -1;
  3035. private readonly _m: Float32Array = new Float32Array(16);
  3036. /**
  3037. * Gets the internal data of the matrix
  3038. */
  3039. public get m(): DeepImmutable<Float32Array> { return this._m; }
  3040. /** @hidden */
  3041. public _markAsUpdated() {
  3042. this.updateFlag = Matrix._updateFlagSeed++;
  3043. this._isIdentity = false;
  3044. this._isIdentity3x2 = false;
  3045. this._isIdentityDirty = true;
  3046. this._isIdentity3x2Dirty = true;
  3047. }
  3048. /** @hidden */
  3049. private _updateIdentityStatus(isIdentity: boolean, isIdentityDirty: boolean = false, isIdentity3x2: boolean = false, isIdentity3x2Dirty: boolean = true) {
  3050. this.updateFlag = Matrix._updateFlagSeed++;
  3051. this._isIdentity = isIdentity;
  3052. this._isIdentity3x2 = isIdentity || isIdentity3x2;
  3053. this._isIdentityDirty = this._isIdentity ? false : isIdentityDirty;
  3054. this._isIdentity3x2Dirty = this._isIdentity3x2 ? false : isIdentity3x2Dirty;
  3055. }
  3056. /**
  3057. * Creates an empty matrix (filled with zeros)
  3058. */
  3059. public constructor() {
  3060. this._updateIdentityStatus(false);
  3061. }
  3062. // Properties
  3063. /**
  3064. * Check if the current matrix is identity
  3065. * @returns true is the matrix is the identity matrix
  3066. */
  3067. public isIdentity(): boolean {
  3068. if (this._isIdentityDirty) {
  3069. this._isIdentityDirty = false;
  3070. const m = this._m;
  3071. this._isIdentity = (
  3072. m[0] === 1.0 && m[1] === 0.0 && m[2] === 0.0 && m[3] === 0.0 &&
  3073. m[4] === 0.0 && m[5] === 1.0 && m[6] === 0.0 && m[7] === 0.0 &&
  3074. m[8] === 0.0 && m[9] === 0.0 && m[10] === 1.0 && m[11] === 0.0 &&
  3075. m[12] === 0.0 && m[13] === 0.0 && m[14] === 0.0 && m[15] === 1.0
  3076. );
  3077. }
  3078. return this._isIdentity;
  3079. }
  3080. /**
  3081. * Check if the current matrix is identity as a texture matrix (3x2 store in 4x4)
  3082. * @returns true is the matrix is the identity matrix
  3083. */
  3084. public isIdentityAs3x2(): boolean {
  3085. if (this._isIdentity3x2Dirty) {
  3086. this._isIdentity3x2Dirty = false;
  3087. if (this._m[0] !== 1.0 || this._m[5] !== 1.0 || this._m[15] !== 1.0) {
  3088. this._isIdentity3x2 = false;
  3089. } else if (this._m[1] !== 0.0 || this._m[2] !== 0.0 || this._m[3] !== 0.0 ||
  3090. this._m[4] !== 0.0 || this._m[6] !== 0.0 || this._m[7] !== 0.0 ||
  3091. this._m[8] !== 0.0 || this._m[9] !== 0.0 || this._m[10] !== 0.0 || this._m[11] !== 0.0 ||
  3092. this._m[12] !== 0.0 || this._m[13] !== 0.0 || this._m[14] !== 0.0) {
  3093. this._isIdentity3x2 = false;
  3094. } else {
  3095. this._isIdentity3x2 = true;
  3096. }
  3097. }
  3098. return this._isIdentity3x2;
  3099. }
  3100. /**
  3101. * Gets the determinant of the matrix
  3102. * @returns the matrix determinant
  3103. */
  3104. public determinant(): number {
  3105. if (this._isIdentity === true) {
  3106. return 1;
  3107. }
  3108. const m = this._m;
  3109. const m00 = m[0], m01 = m[1], m02 = m[2], m03 = m[3];
  3110. const m10 = m[4], m11 = m[5], m12 = m[6], m13 = m[7];
  3111. const m20 = m[8], m21 = m[9], m22 = m[10], m23 = m[11];
  3112. const m30 = m[12], m31 = m[13], m32 = m[14], m33 = m[15];
  3113. // https://en.wikipedia.org/wiki/Laplace_expansion
  3114. // to compute the deterrminant of a 4x4 Matrix we compute the cofactors of any row or column,
  3115. // then we multiply each Cofactor by its corresponding matrix value and sum them all to get the determinant
  3116. // Cofactor(i, j) = sign(i,j) * det(Minor(i, j))
  3117. // where
  3118. // - sign(i,j) = (i+j) % 2 === 0 ? 1 : -1
  3119. // - Minor(i, j) is the 3x3 matrix we get by removing row i and column j from current Matrix
  3120. //
  3121. // Here we do that for the 1st row.
  3122. const det_22_33 = m22 * m33 - m32 * m23;
  3123. const det_21_33 = m21 * m33 - m31 * m23;
  3124. const det_21_32 = m21 * m32 - m31 * m22;
  3125. const det_20_33 = m20 * m33 - m30 * m23;
  3126. const det_20_32 = m20 * m32 - m22 * m30;
  3127. const det_20_31 = m20 * m31 - m30 * m21;
  3128. const cofact_00 = +(m11 * det_22_33 - m12 * det_21_33 + m13 * det_21_32);
  3129. const cofact_01 = -(m10 * det_22_33 - m12 * det_20_33 + m13 * det_20_32);
  3130. const cofact_02 = +(m10 * det_21_33 - m11 * det_20_33 + m13 * det_20_31);
  3131. const cofact_03 = -(m10 * det_21_32 - m11 * det_20_32 + m12 * det_20_31);
  3132. return m00 * cofact_00 + m01 * cofact_01 + m02 * cofact_02 + m03 * cofact_03;
  3133. }
  3134. // Methods
  3135. /**
  3136. * Returns the matrix as a Float32Array
  3137. * @returns the matrix underlying array
  3138. */
  3139. public toArray(): DeepImmutable<Float32Array> {
  3140. return this._m;
  3141. }
  3142. /**
  3143. * Returns the matrix as a Float32Array
  3144. * @returns the matrix underlying array.
  3145. */
  3146. public asArray(): DeepImmutable<Float32Array> {
  3147. return this._m;
  3148. }
  3149. /**
  3150. * Inverts the current matrix in place
  3151. * @returns the current inverted matrix
  3152. */
  3153. public invert(): Matrix {
  3154. this.invertToRef(this);
  3155. return this;
  3156. }
  3157. /**
  3158. * Sets all the matrix elements to zero
  3159. * @returns the current matrix
  3160. */
  3161. public reset(): Matrix {
  3162. Matrix.FromValuesToRef(
  3163. 0.0, 0.0, 0.0, 0.0,
  3164. 0.0, 0.0, 0.0, 0.0,
  3165. 0.0, 0.0, 0.0, 0.0,
  3166. 0.0, 0.0, 0.0, 0.0,
  3167. this
  3168. );
  3169. this._updateIdentityStatus(false);
  3170. return this;
  3171. }
  3172. /**
  3173. * Adds the current matrix with a second one
  3174. * @param other defines the matrix to add
  3175. * @returns a new matrix as the addition of the current matrix and the given one
  3176. */
  3177. public add(other: DeepImmutable<Matrix>): Matrix {
  3178. var result = new Matrix();
  3179. this.addToRef(other, result);
  3180. return result;
  3181. }
  3182. /**
  3183. * Sets the given matrix "result" to the addition of the current matrix and the given one
  3184. * @param other defines the matrix to add
  3185. * @param result defines the target matrix
  3186. * @returns the current matrix
  3187. */
  3188. public addToRef(other: DeepImmutable<Matrix>, result: Matrix): Matrix {
  3189. const m = this._m;
  3190. const resultM = result._m;
  3191. const otherM = other.m;
  3192. for (var index = 0; index < 16; index++) {
  3193. resultM[index] = m[index] + otherM[index];
  3194. }
  3195. result._markAsUpdated();
  3196. return this;
  3197. }
  3198. /**
  3199. * Adds in place the given matrix to the current matrix
  3200. * @param other defines the second operand
  3201. * @returns the current updated matrix
  3202. */
  3203. public addToSelf(other: DeepImmutable<Matrix>): Matrix {
  3204. const m = this._m;
  3205. const otherM = other.m;
  3206. for (var index = 0; index < 16; index++) {
  3207. m[index] += otherM[index];
  3208. }
  3209. this._markAsUpdated();
  3210. return this;
  3211. }
  3212. /**
  3213. * Sets the given matrix to the current inverted Matrix
  3214. * @param other defines the target matrix
  3215. * @returns the unmodified current matrix
  3216. */
  3217. public invertToRef(other: Matrix): Matrix {
  3218. if (this._isIdentity === true) {
  3219. Matrix.IdentityToRef(other);
  3220. return this;
  3221. }
  3222. // the inverse of a Matrix is the transpose of cofactor matrix divided by the determinant
  3223. const m = this._m;
  3224. const m00 = m[0], m01 = m[1], m02 = m[2], m03 = m[3];
  3225. const m10 = m[4], m11 = m[5], m12 = m[6], m13 = m[7];
  3226. const m20 = m[8], m21 = m[9], m22 = m[10], m23 = m[11];
  3227. const m30 = m[12], m31 = m[13], m32 = m[14], m33 = m[15];
  3228. const det_22_33 = m22 * m33 - m32 * m23;
  3229. const det_21_33 = m21 * m33 - m31 * m23;
  3230. const det_21_32 = m21 * m32 - m31 * m22;
  3231. const det_20_33 = m20 * m33 - m30 * m23;
  3232. const det_20_32 = m20 * m32 - m22 * m30;
  3233. const det_20_31 = m20 * m31 - m30 * m21;
  3234. const cofact_00 = +(m11 * det_22_33 - m12 * det_21_33 + m13 * det_21_32);
  3235. const cofact_01 = -(m10 * det_22_33 - m12 * det_20_33 + m13 * det_20_32);
  3236. const cofact_02 = +(m10 * det_21_33 - m11 * det_20_33 + m13 * det_20_31);
  3237. const cofact_03 = -(m10 * det_21_32 - m11 * det_20_32 + m12 * det_20_31);
  3238. const det = m00 * cofact_00 + m01 * cofact_01 + m02 * cofact_02 + m03 * cofact_03;
  3239. if (det === 0) {
  3240. // not invertible
  3241. other.copyFrom(this);
  3242. return this;
  3243. }
  3244. const detInv = 1 / det;
  3245. const det_12_33 = m12 * m33 - m32 * m13;
  3246. const det_11_33 = m11 * m33 - m31 * m13;
  3247. const det_11_32 = m11 * m32 - m31 * m12;
  3248. const det_10_33 = m10 * m33 - m30 * m13;
  3249. const det_10_32 = m10 * m32 - m30 * m12;
  3250. const det_10_31 = m10 * m31 - m30 * m11;
  3251. const det_12_23 = m12 * m23 - m22 * m13;
  3252. const det_11_23 = m11 * m23 - m21 * m13;
  3253. const det_11_22 = m11 * m22 - m21 * m12;
  3254. const det_10_23 = m10 * m23 - m20 * m13;
  3255. const det_10_22 = m10 * m22 - m20 * m12;
  3256. const det_10_21 = m10 * m21 - m20 * m11;
  3257. const cofact_10 = -(m01 * det_22_33 - m02 * det_21_33 + m03 * det_21_32);
  3258. const cofact_11 = +(m00 * det_22_33 - m02 * det_20_33 + m03 * det_20_32);
  3259. const cofact_12 = -(m00 * det_21_33 - m01 * det_20_33 + m03 * det_20_31);
  3260. const cofact_13 = +(m00 * det_21_32 - m01 * det_20_32 + m02 * det_20_31);
  3261. const cofact_20 = +(m01 * det_12_33 - m02 * det_11_33 + m03 * det_11_32);
  3262. const cofact_21 = -(m00 * det_12_33 - m02 * det_10_33 + m03 * det_10_32);
  3263. const cofact_22 = +(m00 * det_11_33 - m01 * det_10_33 + m03 * det_10_31);
  3264. const cofact_23 = -(m00 * det_11_32 - m01 * det_10_32 + m02 * det_10_31);
  3265. const cofact_30 = -(m01 * det_12_23 - m02 * det_11_23 + m03 * det_11_22);
  3266. const cofact_31 = +(m00 * det_12_23 - m02 * det_10_23 + m03 * det_10_22);
  3267. const cofact_32 = -(m00 * det_11_23 - m01 * det_10_23 + m03 * det_10_21);
  3268. const cofact_33 = +(m00 * det_11_22 - m01 * det_10_22 + m02 * det_10_21);
  3269. Matrix.FromValuesToRef(
  3270. cofact_00 * detInv, cofact_10 * detInv, cofact_20 * detInv, cofact_30 * detInv,
  3271. cofact_01 * detInv, cofact_11 * detInv, cofact_21 * detInv, cofact_31 * detInv,
  3272. cofact_02 * detInv, cofact_12 * detInv, cofact_22 * detInv, cofact_32 * detInv,
  3273. cofact_03 * detInv, cofact_13 * detInv, cofact_23 * detInv, cofact_33 * detInv,
  3274. other
  3275. );
  3276. return this;
  3277. }
  3278. /**
  3279. * add a value at the specified position in the current Matrix
  3280. * @param index the index of the value within the matrix. between 0 and 15.
  3281. * @param value the value to be added
  3282. * @returns the current updated matrix
  3283. */
  3284. public addAtIndex(index: number, value: number): Matrix {
  3285. this._m[index] += value;
  3286. this._markAsUpdated();
  3287. return this;
  3288. }
  3289. /**
  3290. * mutiply the specified position in the current Matrix by a value
  3291. * @param index the index of the value within the matrix. between 0 and 15.
  3292. * @param value the value to be added
  3293. * @returns the current updated matrix
  3294. */
  3295. public multiplyAtIndex(index: number, value: number): Matrix {
  3296. this._m[index] *= value;
  3297. this._markAsUpdated();
  3298. return this;
  3299. }
  3300. /**
  3301. * Inserts the translation vector (using 3 floats) in the current matrix
  3302. * @param x defines the 1st component of the translation
  3303. * @param y defines the 2nd component of the translation
  3304. * @param z defines the 3rd component of the translation
  3305. * @returns the current updated matrix
  3306. */
  3307. public setTranslationFromFloats(x: number, y: number, z: number): Matrix {
  3308. this._m[12] = x;
  3309. this._m[13] = y;
  3310. this._m[14] = z;
  3311. this._markAsUpdated();
  3312. return this;
  3313. }
  3314. /**
  3315. * Adds the translation vector (using 3 floats) in the current matrix
  3316. * @param x defines the 1st component of the translation
  3317. * @param y defines the 2nd component of the translation
  3318. * @param z defines the 3rd component of the translation
  3319. * @returns the current updated matrix
  3320. */
  3321. public addTranslationFromFloats(x: number, y: number, z: number): Matrix {
  3322. this._m[12] += x;
  3323. this._m[13] += y;
  3324. this._m[14] += z;
  3325. this._markAsUpdated();
  3326. return this;
  3327. }
  3328. /**
  3329. * Inserts the translation vector in the current matrix
  3330. * @param vector3 defines the translation to insert
  3331. * @returns the current updated matrix
  3332. */
  3333. public setTranslation(vector3: DeepImmutable<Vector3>): Matrix {
  3334. return this.setTranslationFromFloats(vector3.x, vector3.y, vector3.z);
  3335. }
  3336. /**
  3337. * Gets the translation value of the current matrix
  3338. * @returns a new Vector3 as the extracted translation from the matrix
  3339. */
  3340. public getTranslation(): Vector3 {
  3341. return new Vector3(this._m[12], this._m[13], this._m[14]);
  3342. }
  3343. /**
  3344. * Fill a Vector3 with the extracted translation from the matrix
  3345. * @param result defines the Vector3 where to store the translation
  3346. * @returns the current matrix
  3347. */
  3348. public getTranslationToRef(result: Vector3): Matrix {
  3349. result.x = this._m[12];
  3350. result.y = this._m[13];
  3351. result.z = this._m[14];
  3352. return this;
  3353. }
  3354. /**
  3355. * Remove rotation and scaling part from the matrix
  3356. * @returns the updated matrix
  3357. */
  3358. public removeRotationAndScaling(): Matrix {
  3359. const m = this.m;
  3360. Matrix.FromValuesToRef(
  3361. 1.0, 0.0, 0.0, 0.0,
  3362. 0.0, 1.0, 0.0, 0.0,
  3363. 0.0, 0.0, 1.0, 0.0,
  3364. m[12], m[13], m[14], m[15],
  3365. this
  3366. );
  3367. this._updateIdentityStatus(m[12] === 0 && m[13] === 0 && m[14] === 0 && m[15] === 1);
  3368. return this;
  3369. }
  3370. /**
  3371. * Multiply two matrices
  3372. * @param other defines the second operand
  3373. * @returns a new matrix set with the multiplication result of the current Matrix and the given one
  3374. */
  3375. public multiply(other: DeepImmutable<Matrix>): Matrix {
  3376. var result = new Matrix();
  3377. this.multiplyToRef(other, result);
  3378. return result;
  3379. }
  3380. /**
  3381. * Copy the current matrix from the given one
  3382. * @param other defines the source matrix
  3383. * @returns the current updated matrix
  3384. */
  3385. public copyFrom(other: DeepImmutable<Matrix>): Matrix {
  3386. other.copyToArray(this._m);
  3387. const o = (other as Matrix);
  3388. this._updateIdentityStatus(o._isIdentity, o._isIdentityDirty, o._isIdentity3x2, o._isIdentity3x2Dirty);
  3389. return this;
  3390. }
  3391. /**
  3392. * Populates the given array from the starting index with the current matrix values
  3393. * @param array defines the target array
  3394. * @param offset defines the offset in the target array where to start storing values
  3395. * @returns the current matrix
  3396. */
  3397. public copyToArray(array: Float32Array, offset: number = 0): Matrix {
  3398. let source = this._m;
  3399. array[offset] = source[0];
  3400. array[offset + 1] = source[1];
  3401. array[offset + 2] = source[2];
  3402. array[offset + 3] = source[3];
  3403. array[offset + 4] = source[4];
  3404. array[offset + 5] = source[5];
  3405. array[offset + 6] = source[6];
  3406. array[offset + 7] = source[7];
  3407. array[offset + 8] = source[8];
  3408. array[offset + 9] = source[9];
  3409. array[offset + 10] = source[10];
  3410. array[offset + 11] = source[11];
  3411. array[offset + 12] = source[12];
  3412. array[offset + 13] = source[13];
  3413. array[offset + 14] = source[14];
  3414. array[offset + 15] = source[15];
  3415. return this;
  3416. }
  3417. /**
  3418. * Sets the given matrix "result" with the multiplication result of the current Matrix and the given one
  3419. * @param other defines the second operand
  3420. * @param result defines the matrix where to store the multiplication
  3421. * @returns the current matrix
  3422. */
  3423. public multiplyToRef(other: DeepImmutable<Matrix>, result: Matrix): Matrix {
  3424. if (this._isIdentity) {
  3425. result.copyFrom(other);
  3426. return this;
  3427. }
  3428. if ((other as Matrix)._isIdentity) {
  3429. result.copyFrom(this);
  3430. return this;
  3431. }
  3432. this.multiplyToArray(other, result._m, 0);
  3433. result._markAsUpdated();
  3434. return this;
  3435. }
  3436. /**
  3437. * Sets the Float32Array "result" from the given index "offset" with the multiplication of the current matrix and the given one
  3438. * @param other defines the second operand
  3439. * @param result defines the array where to store the multiplication
  3440. * @param offset defines the offset in the target array where to start storing values
  3441. * @returns the current matrix
  3442. */
  3443. public multiplyToArray(other: DeepImmutable<Matrix>, result: Float32Array, offset: number): Matrix {
  3444. const m = this._m;
  3445. const otherM = other.m;
  3446. var tm0 = m[0], tm1 = m[1], tm2 = m[2], tm3 = m[3];
  3447. var tm4 = m[4], tm5 = m[5], tm6 = m[6], tm7 = m[7];
  3448. var tm8 = m[8], tm9 = m[9], tm10 = m[10], tm11 = m[11];
  3449. var tm12 = m[12], tm13 = m[13], tm14 = m[14], tm15 = m[15];
  3450. var om0 = otherM[0], om1 = otherM[1], om2 = otherM[2], om3 = otherM[3];
  3451. var om4 = otherM[4], om5 = otherM[5], om6 = otherM[6], om7 = otherM[7];
  3452. var om8 = otherM[8], om9 = otherM[9], om10 = otherM[10], om11 = otherM[11];
  3453. var om12 = otherM[12], om13 = otherM[13], om14 = otherM[14], om15 = otherM[15];
  3454. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  3455. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  3456. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  3457. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  3458. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  3459. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  3460. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  3461. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  3462. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  3463. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  3464. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  3465. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  3466. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  3467. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  3468. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  3469. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  3470. return this;
  3471. }
  3472. /**
  3473. * Check equality between this matrix and a second one
  3474. * @param value defines the second matrix to compare
  3475. * @returns true is the current matrix and the given one values are strictly equal
  3476. */
  3477. public equals(value: DeepImmutable<Matrix>): boolean {
  3478. const other = (value as Matrix);
  3479. if (!other) {
  3480. return false;
  3481. }
  3482. if (this._isIdentity || other._isIdentity) {
  3483. if (!this._isIdentityDirty && !other._isIdentityDirty) {
  3484. return this._isIdentity && other._isIdentity;
  3485. }
  3486. }
  3487. const m = this.m;
  3488. const om = other.m;
  3489. return (
  3490. m[0] === om[0] && m[1] === om[1] && m[2] === om[2] && m[3] === om[3] &&
  3491. m[4] === om[4] && m[5] === om[5] && m[6] === om[6] && m[7] === om[7] &&
  3492. m[8] === om[8] && m[9] === om[9] && m[10] === om[10] && m[11] === om[11] &&
  3493. m[12] === om[12] && m[13] === om[13] && m[14] === om[14] && m[15] === om[15]
  3494. );
  3495. }
  3496. /**
  3497. * Clone the current matrix
  3498. * @returns a new matrix from the current matrix
  3499. */
  3500. public clone(): Matrix {
  3501. const matrix = new Matrix();
  3502. matrix.copyFrom(this);
  3503. return matrix;
  3504. }
  3505. /**
  3506. * Returns the name of the current matrix class
  3507. * @returns the string "Matrix"
  3508. */
  3509. public getClassName(): string {
  3510. return "Matrix";
  3511. }
  3512. /**
  3513. * Gets the hash code of the current matrix
  3514. * @returns the hash code
  3515. */
  3516. public getHashCode(): number {
  3517. let hash = this._m[0] | 0;
  3518. for (let i = 1; i < 16; i++) {
  3519. hash = (hash * 397) ^ (this._m[i] | 0);
  3520. }
  3521. return hash;
  3522. }
  3523. /**
  3524. * Decomposes the current Matrix into a translation, rotation and scaling components
  3525. * @param scale defines the scale vector3 given as a reference to update
  3526. * @param rotation defines the rotation quaternion given as a reference to update
  3527. * @param translation defines the translation vector3 given as a reference to update
  3528. * @returns true if operation was successful
  3529. */
  3530. public decompose(scale?: Vector3, rotation?: Quaternion, translation?: Vector3): boolean {
  3531. if (this._isIdentity) {
  3532. if (translation) {
  3533. translation.setAll(0);
  3534. }
  3535. if (scale) {
  3536. scale.setAll(1);
  3537. }
  3538. if (rotation) {
  3539. rotation.copyFromFloats(0, 0, 0, 1);
  3540. }
  3541. return true;
  3542. }
  3543. const m = this._m;
  3544. if (translation) {
  3545. translation.copyFromFloats(m[12], m[13], m[14]);
  3546. }
  3547. scale = scale || MathTmp.Vector3[0];
  3548. scale.x = Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
  3549. scale.y = Math.sqrt(m[4] * m[4] + m[5] * m[5] + m[6] * m[6]);
  3550. scale.z = Math.sqrt(m[8] * m[8] + m[9] * m[9] + m[10] * m[10]);
  3551. if (this.determinant() <= 0) {
  3552. scale.y *= -1;
  3553. }
  3554. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  3555. if (rotation) {
  3556. rotation.copyFromFloats(0.0, 0.0, 0.0, 1.0);
  3557. }
  3558. return false;
  3559. }
  3560. if (rotation) {
  3561. const sx = 1 / scale.x, sy = 1 / scale.y, sz = 1 / scale.z;
  3562. Matrix.FromValuesToRef(
  3563. m[0] * sx, m[1] * sx, m[2] * sx, 0.0,
  3564. m[4] * sy, m[5] * sy, m[6] * sy, 0.0,
  3565. m[8] * sz, m[9] * sz, m[10] * sz, 0.0,
  3566. 0.0, 0.0, 0.0, 1.0,
  3567. MathTmp.Matrix[0]
  3568. );
  3569. Quaternion.FromRotationMatrixToRef(MathTmp.Matrix[0], rotation);
  3570. }
  3571. return true;
  3572. }
  3573. /**
  3574. * Gets specific row of the matrix
  3575. * @param index defines the number of the row to get
  3576. * @returns the index-th row of the current matrix as a new Vector4
  3577. */
  3578. public getRow(index: number): Nullable<Vector4> {
  3579. if (index < 0 || index > 3) {
  3580. return null;
  3581. }
  3582. var i = index * 4;
  3583. return new Vector4(this._m[i + 0], this._m[i + 1], this._m[i + 2], this._m[i + 3]);
  3584. }
  3585. /**
  3586. * Sets the index-th row of the current matrix to the vector4 values
  3587. * @param index defines the number of the row to set
  3588. * @param row defines the target vector4
  3589. * @returns the updated current matrix
  3590. */
  3591. public setRow(index: number, row: Vector4): Matrix {
  3592. return this.setRowFromFloats(index, row.x, row.y, row.z, row.w);
  3593. }
  3594. /**
  3595. * Compute the transpose of the matrix
  3596. * @returns the new transposed matrix
  3597. */
  3598. public transpose(): Matrix {
  3599. return Matrix.Transpose(this);
  3600. }
  3601. /**
  3602. * Compute the transpose of the matrix and store it in a given matrix
  3603. * @param result defines the target matrix
  3604. * @returns the current matrix
  3605. */
  3606. public transposeToRef(result: Matrix): Matrix {
  3607. Matrix.TransposeToRef(this, result);
  3608. return this;
  3609. }
  3610. /**
  3611. * Sets the index-th row of the current matrix with the given 4 x float values
  3612. * @param index defines the row index
  3613. * @param x defines the x component to set
  3614. * @param y defines the y component to set
  3615. * @param z defines the z component to set
  3616. * @param w defines the w component to set
  3617. * @returns the updated current matrix
  3618. */
  3619. public setRowFromFloats(index: number, x: number, y: number, z: number, w: number): Matrix {
  3620. if (index < 0 || index > 3) {
  3621. return this;
  3622. }
  3623. var i = index * 4;
  3624. this._m[i + 0] = x;
  3625. this._m[i + 1] = y;
  3626. this._m[i + 2] = z;
  3627. this._m[i + 3] = w;
  3628. this._markAsUpdated();
  3629. return this;
  3630. }
  3631. /**
  3632. * Compute a new matrix set with the current matrix values multiplied by scale (float)
  3633. * @param scale defines the scale factor
  3634. * @returns a new matrix
  3635. */
  3636. public scale(scale: number): Matrix {
  3637. var result = new Matrix();
  3638. this.scaleToRef(scale, result);
  3639. return result;
  3640. }
  3641. /**
  3642. * Scale the current matrix values by a factor to a given result matrix
  3643. * @param scale defines the scale factor
  3644. * @param result defines the matrix to store the result
  3645. * @returns the current matrix
  3646. */
  3647. public scaleToRef(scale: number, result: Matrix): Matrix {
  3648. for (var index = 0; index < 16; index++) {
  3649. result._m[index] = this._m[index] * scale;
  3650. }
  3651. result._markAsUpdated();
  3652. return this;
  3653. }
  3654. /**
  3655. * Scale the current matrix values by a factor and add the result to a given matrix
  3656. * @param scale defines the scale factor
  3657. * @param result defines the Matrix to store the result
  3658. * @returns the current matrix
  3659. */
  3660. public scaleAndAddToRef(scale: number, result: Matrix): Matrix {
  3661. for (var index = 0; index < 16; index++) {
  3662. result._m[index] += this._m[index] * scale;
  3663. }
  3664. result._markAsUpdated();
  3665. return this;
  3666. }
  3667. /**
  3668. * Writes to the given matrix a normal matrix, computed from this one (using values from identity matrix for fourth row and column).
  3669. * @param ref matrix to store the result
  3670. */
  3671. public toNormalMatrix(ref: Matrix): void {
  3672. const tmp = MathTmp.Matrix[0];
  3673. this.invertToRef(tmp);
  3674. tmp.transposeToRef(ref);
  3675. var m = ref._m;
  3676. Matrix.FromValuesToRef(
  3677. m[0], m[1], m[2], 0.0,
  3678. m[4], m[5], m[6], 0.0,
  3679. m[8], m[9], m[10], 0.0,
  3680. 0.0, 0.0, 0.0, 1.0,
  3681. ref
  3682. );
  3683. }
  3684. /**
  3685. * Gets only rotation part of the current matrix
  3686. * @returns a new matrix sets to the extracted rotation matrix from the current one
  3687. */
  3688. public getRotationMatrix(): Matrix {
  3689. var result = new Matrix();
  3690. this.getRotationMatrixToRef(result);
  3691. return result;
  3692. }
  3693. /**
  3694. * Extracts the rotation matrix from the current one and sets it as the given "result"
  3695. * @param result defines the target matrix to store data to
  3696. * @returns the current matrix
  3697. */
  3698. public getRotationMatrixToRef(result: Matrix): Matrix {
  3699. const scale = MathTmp.Vector3[0];
  3700. if (!this.decompose(scale)) {
  3701. Matrix.IdentityToRef(result);
  3702. return this;
  3703. }
  3704. const m = this._m;
  3705. const sx = 1 / scale.x, sy = 1 / scale.y, sz = 1 / scale.z;
  3706. Matrix.FromValuesToRef(
  3707. m[0] * sx, m[1] * sx, m[2] * sx, 0.0,
  3708. m[4] * sy, m[5] * sy, m[6] * sy, 0.0,
  3709. m[8] * sz, m[9] * sz, m[10] * sz, 0.0,
  3710. 0.0, 0.0, 0.0, 1.0,
  3711. result
  3712. );
  3713. return this;
  3714. }
  3715. /**
  3716. * Toggles model matrix from being right handed to left handed in place and vice versa
  3717. */
  3718. public toggleModelMatrixHandInPlace() {
  3719. const m = this._m;
  3720. m[2] *= -1;
  3721. m[6] *= -1;
  3722. m[8] *= -1;
  3723. m[9] *= -1;
  3724. m[14] *= -1;
  3725. this._markAsUpdated();
  3726. }
  3727. /**
  3728. * Toggles projection matrix from being right handed to left handed in place and vice versa
  3729. */
  3730. public toggleProjectionMatrixHandInPlace() {
  3731. var m = this._m;
  3732. m[8] *= -1;
  3733. m[9] *= -1;
  3734. m[10] *= -1;
  3735. m[11] *= -1;
  3736. this._markAsUpdated();
  3737. }
  3738. // Statics
  3739. /**
  3740. * Creates a matrix from an array
  3741. * @param array defines the source array
  3742. * @param offset defines an offset in the source array
  3743. * @returns a new Matrix set from the starting index of the given array
  3744. */
  3745. public static FromArray(array: DeepImmutable<ArrayLike<number>>, offset: number = 0): Matrix {
  3746. var result = new Matrix();
  3747. Matrix.FromArrayToRef(array, offset, result);
  3748. return result;
  3749. }
  3750. /**
  3751. * Copy the content of an array into a given matrix
  3752. * @param array defines the source array
  3753. * @param offset defines an offset in the source array
  3754. * @param result defines the target matrix
  3755. */
  3756. public static FromArrayToRef(array: DeepImmutable<ArrayLike<number>>, offset: number, result: Matrix) {
  3757. for (var index = 0; index < 16; index++) {
  3758. result._m[index] = array[index + offset];
  3759. }
  3760. result._markAsUpdated();
  3761. }
  3762. /**
  3763. * Stores an array into a matrix after having multiplied each component by a given factor
  3764. * @param array defines the source array
  3765. * @param offset defines the offset in the source array
  3766. * @param scale defines the scaling factor
  3767. * @param result defines the target matrix
  3768. */
  3769. public static FromFloat32ArrayToRefScaled(array: DeepImmutable<Float32Array>, offset: number, scale: number, result: Matrix) {
  3770. for (var index = 0; index < 16; index++) {
  3771. result._m[index] = array[index + offset] * scale;
  3772. }
  3773. result._markAsUpdated();
  3774. }
  3775. /**
  3776. * Gets an identity matrix that must not be updated
  3777. */
  3778. public static get IdentityReadOnly(): DeepImmutable<Matrix> {
  3779. return Matrix._identityReadOnly;
  3780. }
  3781. /**
  3782. * Stores a list of values (16) inside a given matrix
  3783. * @param initialM11 defines 1st value of 1st row
  3784. * @param initialM12 defines 2nd value of 1st row
  3785. * @param initialM13 defines 3rd value of 1st row
  3786. * @param initialM14 defines 4th value of 1st row
  3787. * @param initialM21 defines 1st value of 2nd row
  3788. * @param initialM22 defines 2nd value of 2nd row
  3789. * @param initialM23 defines 3rd value of 2nd row
  3790. * @param initialM24 defines 4th value of 2nd row
  3791. * @param initialM31 defines 1st value of 3rd row
  3792. * @param initialM32 defines 2nd value of 3rd row
  3793. * @param initialM33 defines 3rd value of 3rd row
  3794. * @param initialM34 defines 4th value of 3rd row
  3795. * @param initialM41 defines 1st value of 4th row
  3796. * @param initialM42 defines 2nd value of 4th row
  3797. * @param initialM43 defines 3rd value of 4th row
  3798. * @param initialM44 defines 4th value of 4th row
  3799. * @param result defines the target matrix
  3800. */
  3801. public static FromValuesToRef(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  3802. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  3803. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  3804. initialM41: number, initialM42: number, initialM43: number, initialM44: number, result: Matrix): void {
  3805. const m = result._m;
  3806. m[0] = initialM11; m[1] = initialM12; m[2] = initialM13; m[3] = initialM14;
  3807. m[4] = initialM21; m[5] = initialM22; m[6] = initialM23; m[7] = initialM24;
  3808. m[8] = initialM31; m[9] = initialM32; m[10] = initialM33; m[11] = initialM34;
  3809. m[12] = initialM41; m[13] = initialM42; m[14] = initialM43; m[15] = initialM44;
  3810. result._markAsUpdated();
  3811. }
  3812. /**
  3813. * Creates new matrix from a list of values (16)
  3814. * @param initialM11 defines 1st value of 1st row
  3815. * @param initialM12 defines 2nd value of 1st row
  3816. * @param initialM13 defines 3rd value of 1st row
  3817. * @param initialM14 defines 4th value of 1st row
  3818. * @param initialM21 defines 1st value of 2nd row
  3819. * @param initialM22 defines 2nd value of 2nd row
  3820. * @param initialM23 defines 3rd value of 2nd row
  3821. * @param initialM24 defines 4th value of 2nd row
  3822. * @param initialM31 defines 1st value of 3rd row
  3823. * @param initialM32 defines 2nd value of 3rd row
  3824. * @param initialM33 defines 3rd value of 3rd row
  3825. * @param initialM34 defines 4th value of 3rd row
  3826. * @param initialM41 defines 1st value of 4th row
  3827. * @param initialM42 defines 2nd value of 4th row
  3828. * @param initialM43 defines 3rd value of 4th row
  3829. * @param initialM44 defines 4th value of 4th row
  3830. * @returns the new matrix
  3831. */
  3832. public static FromValues(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  3833. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  3834. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  3835. initialM41: number, initialM42: number, initialM43: number, initialM44: number): Matrix {
  3836. var result = new Matrix();
  3837. const m = result._m;
  3838. m[0] = initialM11; m[1] = initialM12; m[2] = initialM13; m[3] = initialM14;
  3839. m[4] = initialM21; m[5] = initialM22; m[6] = initialM23; m[7] = initialM24;
  3840. m[8] = initialM31; m[9] = initialM32; m[10] = initialM33; m[11] = initialM34;
  3841. m[12] = initialM41; m[13] = initialM42; m[14] = initialM43; m[15] = initialM44;
  3842. result._markAsUpdated();
  3843. return result;
  3844. }
  3845. /**
  3846. * Creates a new matrix composed by merging scale (vector3), rotation (quaternion) and translation (vector3)
  3847. * @param scale defines the scale vector3
  3848. * @param rotation defines the rotation quaternion
  3849. * @param translation defines the translation vector3
  3850. * @returns a new matrix
  3851. */
  3852. public static Compose(scale: DeepImmutable<Vector3>, rotation: DeepImmutable<Quaternion>, translation: DeepImmutable<Vector3>): Matrix {
  3853. var result = new Matrix();
  3854. Matrix.ComposeToRef(scale, rotation, translation, result);
  3855. return result;
  3856. }
  3857. /**
  3858. * Sets a matrix to a value composed by merging scale (vector3), rotation (quaternion) and translation (vector3)
  3859. * @param scale defines the scale vector3
  3860. * @param rotation defines the rotation quaternion
  3861. * @param translation defines the translation vector3
  3862. * @param result defines the target matrix
  3863. */
  3864. public static ComposeToRef(scale: DeepImmutable<Vector3>, rotation: DeepImmutable<Quaternion>, translation: DeepImmutable<Vector3>, result: Matrix): void {
  3865. let m = result._m;
  3866. var x = rotation.x, y = rotation.y, z = rotation.z, w = rotation.w;
  3867. var x2 = x + x, y2 = y + y, z2 = z + z;
  3868. var xx = x * x2, xy = x * y2, xz = x * z2;
  3869. var yy = y * y2, yz = y * z2, zz = z * z2;
  3870. var wx = w * x2, wy = w * y2, wz = w * z2;
  3871. var sx = scale.x, sy = scale.y, sz = scale.z;
  3872. m[0] = (1 - (yy + zz)) * sx;
  3873. m[1] = (xy + wz) * sx;
  3874. m[2] = (xz - wy) * sx;
  3875. m[3] = 0;
  3876. m[4] = (xy - wz) * sy;
  3877. m[5] = (1 - (xx + zz)) * sy;
  3878. m[6] = (yz + wx) * sy;
  3879. m[7] = 0;
  3880. m[8] = (xz + wy) * sz;
  3881. m[9] = (yz - wx) * sz;
  3882. m[10] = (1 - (xx + yy)) * sz;
  3883. m[11] = 0;
  3884. m[12] = translation.x;
  3885. m[13] = translation.y;
  3886. m[14] = translation.z;
  3887. m[15] = 1;
  3888. result._markAsUpdated();
  3889. }
  3890. /**
  3891. * Creates a new identity matrix
  3892. * @returns a new identity matrix
  3893. */
  3894. public static Identity(): Matrix {
  3895. const identity = Matrix.FromValues(
  3896. 1.0, 0.0, 0.0, 0.0,
  3897. 0.0, 1.0, 0.0, 0.0,
  3898. 0.0, 0.0, 1.0, 0.0,
  3899. 0.0, 0.0, 0.0, 1.0);
  3900. identity._updateIdentityStatus(true);
  3901. return identity;
  3902. }
  3903. /**
  3904. * Creates a new identity matrix and stores the result in a given matrix
  3905. * @param result defines the target matrix
  3906. */
  3907. public static IdentityToRef(result: Matrix): void {
  3908. Matrix.FromValuesToRef(
  3909. 1.0, 0.0, 0.0, 0.0,
  3910. 0.0, 1.0, 0.0, 0.0,
  3911. 0.0, 0.0, 1.0, 0.0,
  3912. 0.0, 0.0, 0.0, 1.0,
  3913. result
  3914. );
  3915. result._updateIdentityStatus(true);
  3916. }
  3917. /**
  3918. * Creates a new zero matrix
  3919. * @returns a new zero matrix
  3920. */
  3921. public static Zero(): Matrix {
  3922. const zero = Matrix.FromValues(
  3923. 0.0, 0.0, 0.0, 0.0,
  3924. 0.0, 0.0, 0.0, 0.0,
  3925. 0.0, 0.0, 0.0, 0.0,
  3926. 0.0, 0.0, 0.0, 0.0);
  3927. zero._updateIdentityStatus(false);
  3928. return zero;
  3929. }
  3930. /**
  3931. * Creates a new rotation matrix for "angle" radians around the X axis
  3932. * @param angle defines the angle (in radians) to use
  3933. * @return the new matrix
  3934. */
  3935. public static RotationX(angle: number): Matrix {
  3936. var result = new Matrix();
  3937. Matrix.RotationXToRef(angle, result);
  3938. return result;
  3939. }
  3940. /**
  3941. * Creates a new matrix as the invert of a given matrix
  3942. * @param source defines the source matrix
  3943. * @returns the new matrix
  3944. */
  3945. public static Invert(source: DeepImmutable<Matrix>): Matrix {
  3946. var result = new Matrix();
  3947. source.invertToRef(result);
  3948. return result;
  3949. }
  3950. /**
  3951. * Creates a new rotation matrix for "angle" radians around the X axis and stores it in a given matrix
  3952. * @param angle defines the angle (in radians) to use
  3953. * @param result defines the target matrix
  3954. */
  3955. public static RotationXToRef(angle: number, result: Matrix): void {
  3956. var s = Math.sin(angle);
  3957. var c = Math.cos(angle);
  3958. Matrix.FromValuesToRef(
  3959. 1.0, 0.0, 0.0, 0.0,
  3960. 0.0, c, s, 0.0,
  3961. 0.0, -s, c, 0.0,
  3962. 0.0, 0.0, 0.0, 1.0,
  3963. result
  3964. );
  3965. result._updateIdentityStatus(c === 1 && s === 0);
  3966. }
  3967. /**
  3968. * Creates a new rotation matrix for "angle" radians around the Y axis
  3969. * @param angle defines the angle (in radians) to use
  3970. * @return the new matrix
  3971. */
  3972. public static RotationY(angle: number): Matrix {
  3973. var result = new Matrix();
  3974. Matrix.RotationYToRef(angle, result);
  3975. return result;
  3976. }
  3977. /**
  3978. * Creates a new rotation matrix for "angle" radians around the Y axis and stores it in a given matrix
  3979. * @param angle defines the angle (in radians) to use
  3980. * @param result defines the target matrix
  3981. */
  3982. public static RotationYToRef(angle: number, result: Matrix): void {
  3983. var s = Math.sin(angle);
  3984. var c = Math.cos(angle);
  3985. Matrix.FromValuesToRef(
  3986. c, 0.0, -s, 0.0,
  3987. 0.0, 1.0, 0.0, 0.0,
  3988. s, 0.0, c, 0.0,
  3989. 0.0, 0.0, 0.0, 1.0,
  3990. result
  3991. );
  3992. result._updateIdentityStatus(c === 1 && s === 0);
  3993. }
  3994. /**
  3995. * Creates a new rotation matrix for "angle" radians around the Z axis
  3996. * @param angle defines the angle (in radians) to use
  3997. * @return the new matrix
  3998. */
  3999. public static RotationZ(angle: number): Matrix {
  4000. var result = new Matrix();
  4001. Matrix.RotationZToRef(angle, result);
  4002. return result;
  4003. }
  4004. /**
  4005. * Creates a new rotation matrix for "angle" radians around the Z axis and stores it in a given matrix
  4006. * @param angle defines the angle (in radians) to use
  4007. * @param result defines the target matrix
  4008. */
  4009. public static RotationZToRef(angle: number, result: Matrix): void {
  4010. var s = Math.sin(angle);
  4011. var c = Math.cos(angle);
  4012. Matrix.FromValuesToRef(
  4013. c, s, 0.0, 0.0,
  4014. -s, c, 0.0, 0.0,
  4015. 0.0, 0.0, 1.0, 0.0,
  4016. 0.0, 0.0, 0.0, 1.0,
  4017. result
  4018. );
  4019. result._updateIdentityStatus(c === 1 && s === 0);
  4020. }
  4021. /**
  4022. * Creates a new rotation matrix for "angle" radians around the given axis
  4023. * @param axis defines the axis to use
  4024. * @param angle defines the angle (in radians) to use
  4025. * @return the new matrix
  4026. */
  4027. public static RotationAxis(axis: DeepImmutable<Vector3>, angle: number): Matrix {
  4028. var result = new Matrix();
  4029. Matrix.RotationAxisToRef(axis, angle, result);
  4030. return result;
  4031. }
  4032. /**
  4033. * Creates a new rotation matrix for "angle" radians around the given axis and stores it in a given matrix
  4034. * @param axis defines the axis to use
  4035. * @param angle defines the angle (in radians) to use
  4036. * @param result defines the target matrix
  4037. */
  4038. public static RotationAxisToRef(axis: DeepImmutable<Vector3>, angle: number, result: Matrix): void {
  4039. var s = Math.sin(-angle);
  4040. var c = Math.cos(-angle);
  4041. var c1 = 1 - c;
  4042. axis.normalize();
  4043. const m = result._m;
  4044. m[0] = (axis.x * axis.x) * c1 + c;
  4045. m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  4046. m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  4047. m[3] = 0.0;
  4048. m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  4049. m[5] = (axis.y * axis.y) * c1 + c;
  4050. m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  4051. m[7] = 0.0;
  4052. m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  4053. m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  4054. m[10] = (axis.z * axis.z) * c1 + c;
  4055. m[11] = 0.0;
  4056. m[12] = 0.0;
  4057. m[13] = 0.0;
  4058. m[14] = 0.0;
  4059. m[15] = 1.0;
  4060. result._markAsUpdated();
  4061. }
  4062. /**
  4063. * Takes normalised vectors and returns a rotation matrix to align "from" with "to".
  4064. * Taken from http://www.iquilezles.org/www/articles/noacos/noacos.htm
  4065. * @param from defines the vector to align
  4066. * @param to defines the vector to align to
  4067. * @param result defines the target matrix
  4068. */
  4069. public static RotationAlignToRef(from: DeepImmutable<Vector3>, to: DeepImmutable<Vector3>, result: Matrix): void {
  4070. const v = Vector3.Cross(to, from);
  4071. const c = Vector3.Dot(to, from);
  4072. const k = 1 / (1 + c);
  4073. const m = result._m;
  4074. m[0] = v.x * v.x * k + c; m[1] = v.y * v.x * k - v.z; m[2] = v.z * v.x * k + v.y; m[3] = 0;
  4075. m[4] = v.x * v.y * k + v.z; m[5] = v.y * v.y * k + c; m[6] = v.z * v.y * k - v.x; m[7] = 0;
  4076. m[8] = v.x * v.z * k - v.y; m[9] = v.y * v.z * k + v.x; m[10] = v.z * v.z * k + c; m[11] = 0;
  4077. m[12] = 0; m[13] = 0; m[14] = 0; m[15] = 1;
  4078. result._markAsUpdated();
  4079. }
  4080. /**
  4081. * Creates a rotation matrix
  4082. * @param yaw defines the yaw angle in radians (Y axis)
  4083. * @param pitch defines the pitch angle in radians (X axis)
  4084. * @param roll defines the roll angle in radians (X axis)
  4085. * @returns the new rotation matrix
  4086. */
  4087. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Matrix {
  4088. var result = new Matrix();
  4089. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  4090. return result;
  4091. }
  4092. /**
  4093. * Creates a rotation matrix and stores it in a given matrix
  4094. * @param yaw defines the yaw angle in radians (Y axis)
  4095. * @param pitch defines the pitch angle in radians (X axis)
  4096. * @param roll defines the roll angle in radians (X axis)
  4097. * @param result defines the target matrix
  4098. */
  4099. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Matrix): void {
  4100. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, MathTmp.Quaternion[0]);
  4101. MathTmp.Quaternion[0].toRotationMatrix(result);
  4102. }
  4103. /**
  4104. * Creates a scaling matrix
  4105. * @param x defines the scale factor on X axis
  4106. * @param y defines the scale factor on Y axis
  4107. * @param z defines the scale factor on Z axis
  4108. * @returns the new matrix
  4109. */
  4110. public static Scaling(x: number, y: number, z: number): Matrix {
  4111. var result = new Matrix();
  4112. Matrix.ScalingToRef(x, y, z, result);
  4113. return result;
  4114. }
  4115. /**
  4116. * Creates a scaling matrix and stores it in a given matrix
  4117. * @param x defines the scale factor on X axis
  4118. * @param y defines the scale factor on Y axis
  4119. * @param z defines the scale factor on Z axis
  4120. * @param result defines the target matrix
  4121. */
  4122. public static ScalingToRef(x: number, y: number, z: number, result: Matrix): void {
  4123. Matrix.FromValuesToRef(
  4124. x, 0.0, 0.0, 0.0,
  4125. 0.0, y, 0.0, 0.0,
  4126. 0.0, 0.0, z, 0.0,
  4127. 0.0, 0.0, 0.0, 1.0,
  4128. result
  4129. );
  4130. result._updateIdentityStatus(x === 1 && y === 1 && z === 1);
  4131. }
  4132. /**
  4133. * Creates a translation matrix
  4134. * @param x defines the translation on X axis
  4135. * @param y defines the translation on Y axis
  4136. * @param z defines the translationon Z axis
  4137. * @returns the new matrix
  4138. */
  4139. public static Translation(x: number, y: number, z: number): Matrix {
  4140. var result = new Matrix();
  4141. Matrix.TranslationToRef(x, y, z, result);
  4142. return result;
  4143. }
  4144. /**
  4145. * Creates a translation matrix and stores it in a given matrix
  4146. * @param x defines the translation on X axis
  4147. * @param y defines the translation on Y axis
  4148. * @param z defines the translationon Z axis
  4149. * @param result defines the target matrix
  4150. */
  4151. public static TranslationToRef(x: number, y: number, z: number, result: Matrix): void {
  4152. Matrix.FromValuesToRef(
  4153. 1.0, 0.0, 0.0, 0.0,
  4154. 0.0, 1.0, 0.0, 0.0,
  4155. 0.0, 0.0, 1.0, 0.0,
  4156. x, y, z, 1.0,
  4157. result
  4158. );
  4159. result._updateIdentityStatus(x === 0 && y === 0 && z === 0);
  4160. }
  4161. /**
  4162. * Returns a new Matrix whose values are the interpolated values for "gradient" (float) between the ones of the matrices "startValue" and "endValue".
  4163. * @param startValue defines the start value
  4164. * @param endValue defines the end value
  4165. * @param gradient defines the gradient factor
  4166. * @returns the new matrix
  4167. */
  4168. public static Lerp(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number): Matrix {
  4169. var result = new Matrix();
  4170. Matrix.LerpToRef(startValue, endValue, gradient, result);
  4171. return result;
  4172. }
  4173. /**
  4174. * Set the given matrix "result" as the interpolated values for "gradient" (float) between the ones of the matrices "startValue" and "endValue".
  4175. * @param startValue defines the start value
  4176. * @param endValue defines the end value
  4177. * @param gradient defines the gradient factor
  4178. * @param result defines the Matrix object where to store data
  4179. */
  4180. public static LerpToRef(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number, result: Matrix): void {
  4181. const resultM = result._m;
  4182. const startM = startValue.m;
  4183. const endM = endValue.m;
  4184. for (var index = 0; index < 16; index++) {
  4185. resultM[index] = startM[index] * (1.0 - gradient) + endM[index] * gradient;
  4186. }
  4187. result._markAsUpdated();
  4188. }
  4189. /**
  4190. * Builds a new matrix whose values are computed by:
  4191. * * decomposing the the "startValue" and "endValue" matrices into their respective scale, rotation and translation matrices
  4192. * * interpolating for "gradient" (float) the values between each of these decomposed matrices between the start and the end
  4193. * * recomposing a new matrix from these 3 interpolated scale, rotation and translation matrices
  4194. * @param startValue defines the first matrix
  4195. * @param endValue defines the second matrix
  4196. * @param gradient defines the gradient between the two matrices
  4197. * @returns the new matrix
  4198. */
  4199. public static DecomposeLerp(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number): Matrix {
  4200. var result = new Matrix();
  4201. Matrix.DecomposeLerpToRef(startValue, endValue, gradient, result);
  4202. return result;
  4203. }
  4204. /**
  4205. * Update a matrix to values which are computed by:
  4206. * * decomposing the the "startValue" and "endValue" matrices into their respective scale, rotation and translation matrices
  4207. * * interpolating for "gradient" (float) the values between each of these decomposed matrices between the start and the end
  4208. * * recomposing a new matrix from these 3 interpolated scale, rotation and translation matrices
  4209. * @param startValue defines the first matrix
  4210. * @param endValue defines the second matrix
  4211. * @param gradient defines the gradient between the two matrices
  4212. * @param result defines the target matrix
  4213. */
  4214. public static DecomposeLerpToRef(startValue: DeepImmutable<Matrix>, endValue: DeepImmutable<Matrix>, gradient: number, result: Matrix) {
  4215. var startScale = MathTmp.Vector3[0];
  4216. var startRotation = MathTmp.Quaternion[0];
  4217. var startTranslation = MathTmp.Vector3[1];
  4218. startValue.decompose(startScale, startRotation, startTranslation);
  4219. var endScale = MathTmp.Vector3[2];
  4220. var endRotation = MathTmp.Quaternion[1];
  4221. var endTranslation = MathTmp.Vector3[3];
  4222. endValue.decompose(endScale, endRotation, endTranslation);
  4223. var resultScale = MathTmp.Vector3[4];
  4224. Vector3.LerpToRef(startScale, endScale, gradient, resultScale);
  4225. var resultRotation = MathTmp.Quaternion[2];
  4226. Quaternion.SlerpToRef(startRotation, endRotation, gradient, resultRotation);
  4227. var resultTranslation = MathTmp.Vector3[5];
  4228. Vector3.LerpToRef(startTranslation, endTranslation, gradient, resultTranslation);
  4229. Matrix.ComposeToRef(resultScale, resultRotation, resultTranslation, result);
  4230. }
  4231. /**
  4232. * Gets a new rotation matrix used to rotate an entity so as it looks at the target vector3, from the eye vector3 position, the up vector3 being oriented like "up"
  4233. * This function works in left handed mode
  4234. * @param eye defines the final position of the entity
  4235. * @param target defines where the entity should look at
  4236. * @param up defines the up vector for the entity
  4237. * @returns the new matrix
  4238. */
  4239. public static LookAtLH(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>): Matrix {
  4240. var result = new Matrix();
  4241. Matrix.LookAtLHToRef(eye, target, up, result);
  4242. return result;
  4243. }
  4244. /**
  4245. * Sets the given "result" Matrix to a rotation matrix used to rotate an entity so that it looks at the target vector3, from the eye vector3 position, the up vector3 being oriented like "up".
  4246. * This function works in left handed mode
  4247. * @param eye defines the final position of the entity
  4248. * @param target defines where the entity should look at
  4249. * @param up defines the up vector for the entity
  4250. * @param result defines the target matrix
  4251. */
  4252. public static LookAtLHToRef(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>, result: Matrix): void {
  4253. const xAxis = MathTmp.Vector3[0];
  4254. const yAxis = MathTmp.Vector3[1];
  4255. const zAxis = MathTmp.Vector3[2];
  4256. // Z axis
  4257. target.subtractToRef(eye, zAxis);
  4258. zAxis.normalize();
  4259. // X axis
  4260. Vector3.CrossToRef(up, zAxis, xAxis);
  4261. const xSquareLength = xAxis.lengthSquared();
  4262. if (xSquareLength === 0) {
  4263. xAxis.x = 1.0;
  4264. } else {
  4265. xAxis.normalizeFromLength(Math.sqrt(xSquareLength));
  4266. }
  4267. // Y axis
  4268. Vector3.CrossToRef(zAxis, xAxis, yAxis);
  4269. yAxis.normalize();
  4270. // Eye angles
  4271. var ex = -Vector3.Dot(xAxis, eye);
  4272. var ey = -Vector3.Dot(yAxis, eye);
  4273. var ez = -Vector3.Dot(zAxis, eye);
  4274. Matrix.FromValuesToRef(
  4275. xAxis.x, yAxis.x, zAxis.x, 0.0,
  4276. xAxis.y, yAxis.y, zAxis.y, 0.0,
  4277. xAxis.z, yAxis.z, zAxis.z, 0.0,
  4278. ex, ey, ez, 1.0,
  4279. result
  4280. );
  4281. }
  4282. /**
  4283. * Gets a new rotation matrix used to rotate an entity so as it looks at the target vector3, from the eye vector3 position, the up vector3 being oriented like "up"
  4284. * This function works in right handed mode
  4285. * @param eye defines the final position of the entity
  4286. * @param target defines where the entity should look at
  4287. * @param up defines the up vector for the entity
  4288. * @returns the new matrix
  4289. */
  4290. public static LookAtRH(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>): Matrix {
  4291. var result = new Matrix();
  4292. Matrix.LookAtRHToRef(eye, target, up, result);
  4293. return result;
  4294. }
  4295. /**
  4296. * Sets the given "result" Matrix to a rotation matrix used to rotate an entity so that it looks at the target vector3, from the eye vector3 position, the up vector3 being oriented like "up".
  4297. * This function works in right handed mode
  4298. * @param eye defines the final position of the entity
  4299. * @param target defines where the entity should look at
  4300. * @param up defines the up vector for the entity
  4301. * @param result defines the target matrix
  4302. */
  4303. public static LookAtRHToRef(eye: DeepImmutable<Vector3>, target: DeepImmutable<Vector3>, up: DeepImmutable<Vector3>, result: Matrix): void {
  4304. const xAxis = MathTmp.Vector3[0];
  4305. const yAxis = MathTmp.Vector3[1];
  4306. const zAxis = MathTmp.Vector3[2];
  4307. // Z axis
  4308. eye.subtractToRef(target, zAxis);
  4309. zAxis.normalize();
  4310. // X axis
  4311. Vector3.CrossToRef(up, zAxis, xAxis);
  4312. const xSquareLength = xAxis.lengthSquared();
  4313. if (xSquareLength === 0) {
  4314. xAxis.x = 1.0;
  4315. } else {
  4316. xAxis.normalizeFromLength(Math.sqrt(xSquareLength));
  4317. }
  4318. // Y axis
  4319. Vector3.CrossToRef(zAxis, xAxis, yAxis);
  4320. yAxis.normalize();
  4321. // Eye angles
  4322. var ex = -Vector3.Dot(xAxis, eye);
  4323. var ey = -Vector3.Dot(yAxis, eye);
  4324. var ez = -Vector3.Dot(zAxis, eye);
  4325. Matrix.FromValuesToRef(
  4326. xAxis.x, yAxis.x, zAxis.x, 0.0,
  4327. xAxis.y, yAxis.y, zAxis.y, 0.0,
  4328. xAxis.z, yAxis.z, zAxis.z, 0.0,
  4329. ex, ey, ez, 1.0,
  4330. result
  4331. );
  4332. }
  4333. /**
  4334. * Create a left-handed orthographic projection matrix
  4335. * @param width defines the viewport width
  4336. * @param height defines the viewport height
  4337. * @param znear defines the near clip plane
  4338. * @param zfar defines the far clip plane
  4339. * @returns a new matrix as a left-handed orthographic projection matrix
  4340. */
  4341. public static OrthoLH(width: number, height: number, znear: number, zfar: number): Matrix {
  4342. var matrix = new Matrix();
  4343. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  4344. return matrix;
  4345. }
  4346. /**
  4347. * Store a left-handed orthographic projection to a given matrix
  4348. * @param width defines the viewport width
  4349. * @param height defines the viewport height
  4350. * @param znear defines the near clip plane
  4351. * @param zfar defines the far clip plane
  4352. * @param result defines the target matrix
  4353. */
  4354. public static OrthoLHToRef(width: number, height: number, znear: number, zfar: number, result: Matrix): void {
  4355. let n = znear;
  4356. let f = zfar;
  4357. let a = 2.0 / width;
  4358. let b = 2.0 / height;
  4359. let c = 2.0 / (f - n);
  4360. let d = -(f + n) / (f - n);
  4361. Matrix.FromValuesToRef(
  4362. a, 0.0, 0.0, 0.0,
  4363. 0.0, b, 0.0, 0.0,
  4364. 0.0, 0.0, c, 0.0,
  4365. 0.0, 0.0, d, 1.0,
  4366. result
  4367. );
  4368. result._updateIdentityStatus(a === 1 && b === 1 && c === 1 && d === 0);
  4369. }
  4370. /**
  4371. * Create a left-handed orthographic projection matrix
  4372. * @param left defines the viewport left coordinate
  4373. * @param right defines the viewport right coordinate
  4374. * @param bottom defines the viewport bottom coordinate
  4375. * @param top defines the viewport top coordinate
  4376. * @param znear defines the near clip plane
  4377. * @param zfar defines the far clip plane
  4378. * @returns a new matrix as a left-handed orthographic projection matrix
  4379. */
  4380. public static OrthoOffCenterLH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  4381. var matrix = new Matrix();
  4382. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  4383. return matrix;
  4384. }
  4385. /**
  4386. * Stores a left-handed orthographic projection into a given matrix
  4387. * @param left defines the viewport left coordinate
  4388. * @param right defines the viewport right coordinate
  4389. * @param bottom defines the viewport bottom coordinate
  4390. * @param top defines the viewport top coordinate
  4391. * @param znear defines the near clip plane
  4392. * @param zfar defines the far clip plane
  4393. * @param result defines the target matrix
  4394. */
  4395. public static OrthoOffCenterLHToRef(left: number, right: number, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  4396. let n = znear;
  4397. let f = zfar;
  4398. let a = 2.0 / (right - left);
  4399. let b = 2.0 / (top - bottom);
  4400. let c = 2.0 / (f - n);
  4401. let d = -(f + n) / (f - n);
  4402. let i0 = (left + right) / (left - right);
  4403. let i1 = (top + bottom) / (bottom - top);
  4404. Matrix.FromValuesToRef(
  4405. a, 0.0, 0.0, 0.0,
  4406. 0.0, b, 0.0, 0.0,
  4407. 0.0, 0.0, c, 0.0,
  4408. i0, i1, d, 1.0,
  4409. result
  4410. );
  4411. result._markAsUpdated();
  4412. }
  4413. /**
  4414. * Creates a right-handed orthographic projection matrix
  4415. * @param left defines the viewport left coordinate
  4416. * @param right defines the viewport right coordinate
  4417. * @param bottom defines the viewport bottom coordinate
  4418. * @param top defines the viewport top coordinate
  4419. * @param znear defines the near clip plane
  4420. * @param zfar defines the far clip plane
  4421. * @returns a new matrix as a right-handed orthographic projection matrix
  4422. */
  4423. public static OrthoOffCenterRH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  4424. var matrix = new Matrix();
  4425. Matrix.OrthoOffCenterRHToRef(left, right, bottom, top, znear, zfar, matrix);
  4426. return matrix;
  4427. }
  4428. /**
  4429. * Stores a right-handed orthographic projection into a given matrix
  4430. * @param left defines the viewport left coordinate
  4431. * @param right defines the viewport right coordinate
  4432. * @param bottom defines the viewport bottom coordinate
  4433. * @param top defines the viewport top coordinate
  4434. * @param znear defines the near clip plane
  4435. * @param zfar defines the far clip plane
  4436. * @param result defines the target matrix
  4437. */
  4438. public static OrthoOffCenterRHToRef(left: number, right: number, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  4439. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, result);
  4440. result._m[10] *= -1; // No need to call _markAsUpdated as previous function already called it and let _isIdentityDirty to true
  4441. }
  4442. /**
  4443. * Creates a left-handed perspective projection matrix
  4444. * @param width defines the viewport width
  4445. * @param height defines the viewport height
  4446. * @param znear defines the near clip plane
  4447. * @param zfar defines the far clip plane
  4448. * @returns a new matrix as a left-handed perspective projection matrix
  4449. */
  4450. public static PerspectiveLH(width: number, height: number, znear: number, zfar: number): Matrix {
  4451. var matrix = new Matrix();
  4452. let n = znear;
  4453. let f = zfar;
  4454. let a = 2.0 * n / width;
  4455. let b = 2.0 * n / height;
  4456. let c = (f + n) / (f - n);
  4457. let d = -2.0 * f * n / (f - n);
  4458. Matrix.FromValuesToRef(
  4459. a, 0.0, 0.0, 0.0,
  4460. 0.0, b, 0.0, 0.0,
  4461. 0.0, 0.0, c, 1.0,
  4462. 0.0, 0.0, d, 0.0,
  4463. matrix
  4464. );
  4465. matrix._updateIdentityStatus(false);
  4466. return matrix;
  4467. }
  4468. /**
  4469. * Creates a left-handed perspective projection matrix
  4470. * @param fov defines the horizontal field of view
  4471. * @param aspect defines the aspect ratio
  4472. * @param znear defines the near clip plane
  4473. * @param zfar defines the far clip plane
  4474. * @returns a new matrix as a left-handed perspective projection matrix
  4475. */
  4476. public static PerspectiveFovLH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  4477. var matrix = new Matrix();
  4478. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  4479. return matrix;
  4480. }
  4481. /**
  4482. * Stores a left-handed perspective projection into a given matrix
  4483. * @param fov defines the horizontal field of view
  4484. * @param aspect defines the aspect ratio
  4485. * @param znear defines the near clip plane
  4486. * @param zfar defines the far clip plane
  4487. * @param result defines the target matrix
  4488. * @param isVerticalFovFixed defines it the fov is vertically fixed (default) or horizontally
  4489. */
  4490. public static PerspectiveFovLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  4491. let n = znear;
  4492. let f = zfar;
  4493. let t = 1.0 / (Math.tan(fov * 0.5));
  4494. let a = isVerticalFovFixed ? (t / aspect) : t;
  4495. let b = isVerticalFovFixed ? t : (t * aspect);
  4496. let c = (f + n) / (f - n);
  4497. let d = -2.0 * f * n / (f - n);
  4498. Matrix.FromValuesToRef(
  4499. a, 0.0, 0.0, 0.0,
  4500. 0.0, b, 0.0, 0.0,
  4501. 0.0, 0.0, c, 1.0,
  4502. 0.0, 0.0, d, 0.0,
  4503. result
  4504. );
  4505. result._updateIdentityStatus(false);
  4506. }
  4507. /**
  4508. * Stores a left-handed perspective projection into a given matrix with depth reversed
  4509. * @param fov defines the horizontal field of view
  4510. * @param aspect defines the aspect ratio
  4511. * @param znear defines the near clip plane
  4512. * @param zfar not used as infinity is used as far clip
  4513. * @param result defines the target matrix
  4514. * @param isVerticalFovFixed defines it the fov is vertically fixed (default) or horizontally
  4515. */
  4516. public static PerspectiveFovReverseLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  4517. let t = 1.0 / (Math.tan(fov * 0.5));
  4518. let a = isVerticalFovFixed ? (t / aspect) : t;
  4519. let b = isVerticalFovFixed ? t : (t * aspect);
  4520. Matrix.FromValuesToRef(
  4521. a, 0.0, 0.0, 0.0,
  4522. 0.0, b, 0.0, 0.0,
  4523. 0.0, 0.0, -znear, 1.0,
  4524. 0.0, 0.0, 1.0, 0.0,
  4525. result
  4526. );
  4527. result._updateIdentityStatus(false);
  4528. }
  4529. /**
  4530. * Creates a right-handed perspective projection matrix
  4531. * @param fov defines the horizontal field of view
  4532. * @param aspect defines the aspect ratio
  4533. * @param znear defines the near clip plane
  4534. * @param zfar defines the far clip plane
  4535. * @returns a new matrix as a right-handed perspective projection matrix
  4536. */
  4537. public static PerspectiveFovRH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  4538. var matrix = new Matrix();
  4539. Matrix.PerspectiveFovRHToRef(fov, aspect, znear, zfar, matrix);
  4540. return matrix;
  4541. }
  4542. /**
  4543. * Stores a right-handed perspective projection into a given matrix
  4544. * @param fov defines the horizontal field of view
  4545. * @param aspect defines the aspect ratio
  4546. * @param znear defines the near clip plane
  4547. * @param zfar defines the far clip plane
  4548. * @param result defines the target matrix
  4549. * @param isVerticalFovFixed defines it the fov is vertically fixed (default) or horizontally
  4550. */
  4551. public static PerspectiveFovRHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  4552. //alternatively this could be expressed as:
  4553. // m = PerspectiveFovLHToRef
  4554. // m[10] *= -1.0;
  4555. // m[11] *= -1.0;
  4556. let n = znear;
  4557. let f = zfar;
  4558. let t = 1.0 / (Math.tan(fov * 0.5));
  4559. let a = isVerticalFovFixed ? (t / aspect) : t;
  4560. let b = isVerticalFovFixed ? t : (t * aspect);
  4561. let c = -(f + n) / (f - n);
  4562. let d = -2 * f * n / (f - n);
  4563. Matrix.FromValuesToRef(
  4564. a, 0.0, 0.0, 0.0,
  4565. 0.0, b, 0.0, 0.0,
  4566. 0.0, 0.0, c, -1.0,
  4567. 0.0, 0.0, d, 0.0,
  4568. result
  4569. );
  4570. result._updateIdentityStatus(false);
  4571. }
  4572. /**
  4573. * Stores a right-handed perspective projection into a given matrix
  4574. * @param fov defines the horizontal field of view
  4575. * @param aspect defines the aspect ratio
  4576. * @param znear defines the near clip plane
  4577. * @param zfar not used as infinity is used as far clip
  4578. * @param result defines the target matrix
  4579. * @param isVerticalFovFixed defines it the fov is vertically fixed (default) or horizontally
  4580. */
  4581. public static PerspectiveFovReverseRHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  4582. //alternatively this could be expressed as:
  4583. // m = PerspectiveFovLHToRef
  4584. // m[10] *= -1.0;
  4585. // m[11] *= -1.0;
  4586. let t = 1.0 / (Math.tan(fov * 0.5));
  4587. let a = isVerticalFovFixed ? (t / aspect) : t;
  4588. let b = isVerticalFovFixed ? t : (t * aspect);
  4589. Matrix.FromValuesToRef(
  4590. a, 0.0, 0.0, 0.0,
  4591. 0.0, b, 0.0, 0.0,
  4592. 0.0, 0.0, -znear, -1.0,
  4593. 0.0, 0.0, -1.0, 0.0,
  4594. result
  4595. );
  4596. result._updateIdentityStatus(false);
  4597. }
  4598. /**
  4599. * Stores a perspective projection for WebVR info a given matrix
  4600. * @param fov defines the field of view
  4601. * @param znear defines the near clip plane
  4602. * @param zfar defines the far clip plane
  4603. * @param result defines the target matrix
  4604. * @param rightHanded defines if the matrix must be in right-handed mode (false by default)
  4605. */
  4606. public static PerspectiveFovWebVRToRef(fov: { upDegrees: number, downDegrees: number, leftDegrees: number, rightDegrees: number }, znear: number, zfar: number, result: Matrix, rightHanded = false): void {
  4607. var rightHandedFactor = rightHanded ? -1 : 1;
  4608. var upTan = Math.tan(fov.upDegrees * Math.PI / 180.0);
  4609. var downTan = Math.tan(fov.downDegrees * Math.PI / 180.0);
  4610. var leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0);
  4611. var rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0);
  4612. var xScale = 2.0 / (leftTan + rightTan);
  4613. var yScale = 2.0 / (upTan + downTan);
  4614. const m = result._m;
  4615. m[0] = xScale;
  4616. m[1] = m[2] = m[3] = m[4] = 0.0;
  4617. m[5] = yScale;
  4618. m[6] = m[7] = 0.0;
  4619. m[8] = ((leftTan - rightTan) * xScale * 0.5);
  4620. m[9] = -((upTan - downTan) * yScale * 0.5);
  4621. m[10] = -zfar / (znear - zfar);
  4622. m[11] = 1.0 * rightHandedFactor;
  4623. m[12] = m[13] = m[15] = 0.0;
  4624. m[14] = -(2.0 * zfar * znear) / (zfar - znear);
  4625. result._markAsUpdated();
  4626. }
  4627. /**
  4628. * Computes a complete transformation matrix
  4629. * @param viewport defines the viewport to use
  4630. * @param world defines the world matrix
  4631. * @param view defines the view matrix
  4632. * @param projection defines the projection matrix
  4633. * @param zmin defines the near clip plane
  4634. * @param zmax defines the far clip plane
  4635. * @returns the transformation matrix
  4636. */
  4637. public static GetFinalMatrix(viewport: DeepImmutable<Viewport>, world: DeepImmutable<Matrix>, view: DeepImmutable<Matrix>, projection: DeepImmutable<Matrix>, zmin: number, zmax: number): Matrix {
  4638. var cw = viewport.width;
  4639. var ch = viewport.height;
  4640. var cx = viewport.x;
  4641. var cy = viewport.y;
  4642. var viewportMatrix = Matrix.FromValues(
  4643. cw / 2.0, 0.0, 0.0, 0.0,
  4644. 0.0, -ch / 2.0, 0.0, 0.0,
  4645. 0.0, 0.0, zmax - zmin, 0.0,
  4646. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1.0);
  4647. var matrix = MathTmp.Matrix[0];
  4648. world.multiplyToRef(view, matrix);
  4649. matrix.multiplyToRef(projection, matrix);
  4650. return matrix.multiply(viewportMatrix);
  4651. }
  4652. /**
  4653. * Extracts a 2x2 matrix from a given matrix and store the result in a Float32Array
  4654. * @param matrix defines the matrix to use
  4655. * @returns a new Float32Array array with 4 elements : the 2x2 matrix extracted from the given matrix
  4656. */
  4657. public static GetAsMatrix2x2(matrix: DeepImmutable<Matrix>): Float32Array {
  4658. const m = matrix.m;
  4659. return new Float32Array([m[0], m[1], m[4], m[5]]);
  4660. }
  4661. /**
  4662. * Extracts a 3x3 matrix from a given matrix and store the result in a Float32Array
  4663. * @param matrix defines the matrix to use
  4664. * @returns a new Float32Array array with 9 elements : the 3x3 matrix extracted from the given matrix
  4665. */
  4666. public static GetAsMatrix3x3(matrix: DeepImmutable<Matrix>): Float32Array {
  4667. const m = matrix.m;
  4668. return new Float32Array([
  4669. m[0], m[1], m[2],
  4670. m[4], m[5], m[6],
  4671. m[8], m[9], m[10]
  4672. ]);
  4673. }
  4674. /**
  4675. * Compute the transpose of a given matrix
  4676. * @param matrix defines the matrix to transpose
  4677. * @returns the new matrix
  4678. */
  4679. public static Transpose(matrix: DeepImmutable<Matrix>): Matrix {
  4680. var result = new Matrix();
  4681. Matrix.TransposeToRef(matrix, result);
  4682. return result;
  4683. }
  4684. /**
  4685. * Compute the transpose of a matrix and store it in a target matrix
  4686. * @param matrix defines the matrix to transpose
  4687. * @param result defines the target matrix
  4688. */
  4689. public static TransposeToRef(matrix: DeepImmutable<Matrix>, result: Matrix): void {
  4690. const rm = result._m;
  4691. const mm = matrix.m;
  4692. rm[0] = mm[0];
  4693. rm[1] = mm[4];
  4694. rm[2] = mm[8];
  4695. rm[3] = mm[12];
  4696. rm[4] = mm[1];
  4697. rm[5] = mm[5];
  4698. rm[6] = mm[9];
  4699. rm[7] = mm[13];
  4700. rm[8] = mm[2];
  4701. rm[9] = mm[6];
  4702. rm[10] = mm[10];
  4703. rm[11] = mm[14];
  4704. rm[12] = mm[3];
  4705. rm[13] = mm[7];
  4706. rm[14] = mm[11];
  4707. rm[15] = mm[15];
  4708. // identity-ness does not change when transposing
  4709. result._updateIdentityStatus((matrix as Matrix)._isIdentity, (matrix as Matrix)._isIdentityDirty);
  4710. }
  4711. /**
  4712. * Computes a reflection matrix from a plane
  4713. * @param plane defines the reflection plane
  4714. * @returns a new matrix
  4715. */
  4716. public static Reflection(plane: DeepImmutable<IPlaneLike>): Matrix {
  4717. var matrix = new Matrix();
  4718. Matrix.ReflectionToRef(plane, matrix);
  4719. return matrix;
  4720. }
  4721. /**
  4722. * Computes a reflection matrix from a plane
  4723. * @param plane defines the reflection plane
  4724. * @param result defines the target matrix
  4725. */
  4726. public static ReflectionToRef(plane: DeepImmutable<IPlaneLike>, result: Matrix): void {
  4727. plane.normalize();
  4728. var x = plane.normal.x;
  4729. var y = plane.normal.y;
  4730. var z = plane.normal.z;
  4731. var temp = -2 * x;
  4732. var temp2 = -2 * y;
  4733. var temp3 = -2 * z;
  4734. Matrix.FromValuesToRef(
  4735. temp * x + 1, temp2 * x, temp3 * x, 0.0,
  4736. temp * y, temp2 * y + 1, temp3 * y, 0.0,
  4737. temp * z, temp2 * z, temp3 * z + 1, 0.0,
  4738. temp * plane.d, temp2 * plane.d, temp3 * plane.d, 1.0,
  4739. result
  4740. );
  4741. }
  4742. /**
  4743. * Sets the given matrix as a rotation matrix composed from the 3 left handed axes
  4744. * @param xaxis defines the value of the 1st axis
  4745. * @param yaxis defines the value of the 2nd axis
  4746. * @param zaxis defines the value of the 3rd axis
  4747. * @param result defines the target matrix
  4748. */
  4749. public static FromXYZAxesToRef(xaxis: DeepImmutable<Vector3>, yaxis: DeepImmutable<Vector3>, zaxis: DeepImmutable<Vector3>, result: Matrix) {
  4750. Matrix.FromValuesToRef(
  4751. xaxis.x, xaxis.y, xaxis.z, 0.0,
  4752. yaxis.x, yaxis.y, yaxis.z, 0.0,
  4753. zaxis.x, zaxis.y, zaxis.z, 0.0,
  4754. 0.0, 0.0, 0.0, 1.0,
  4755. result
  4756. );
  4757. }
  4758. /**
  4759. * Creates a rotation matrix from a quaternion and stores it in a target matrix
  4760. * @param quat defines the quaternion to use
  4761. * @param result defines the target matrix
  4762. */
  4763. public static FromQuaternionToRef(quat: DeepImmutable<Quaternion>, result: Matrix) {
  4764. var xx = quat.x * quat.x;
  4765. var yy = quat.y * quat.y;
  4766. var zz = quat.z * quat.z;
  4767. var xy = quat.x * quat.y;
  4768. var zw = quat.z * quat.w;
  4769. var zx = quat.z * quat.x;
  4770. var yw = quat.y * quat.w;
  4771. var yz = quat.y * quat.z;
  4772. var xw = quat.x * quat.w;
  4773. result._m[0] = 1.0 - (2.0 * (yy + zz));
  4774. result._m[1] = 2.0 * (xy + zw);
  4775. result._m[2] = 2.0 * (zx - yw);
  4776. result._m[3] = 0.0;
  4777. result._m[4] = 2.0 * (xy - zw);
  4778. result._m[5] = 1.0 - (2.0 * (zz + xx));
  4779. result._m[6] = 2.0 * (yz + xw);
  4780. result._m[7] = 0.0;
  4781. result._m[8] = 2.0 * (zx + yw);
  4782. result._m[9] = 2.0 * (yz - xw);
  4783. result._m[10] = 1.0 - (2.0 * (yy + xx));
  4784. result._m[11] = 0.0;
  4785. result._m[12] = 0.0;
  4786. result._m[13] = 0.0;
  4787. result._m[14] = 0.0;
  4788. result._m[15] = 1.0;
  4789. result._markAsUpdated();
  4790. }
  4791. }
  4792. /**
  4793. * @hidden
  4794. * Same as Tmp but not exported to keep it only for math functions to avoid conflicts
  4795. */
  4796. class MathTmp {
  4797. public static Vector3: Vector3[] = ArrayTools.BuildArray(6, Vector3.Zero);
  4798. public static Matrix: Matrix[] = ArrayTools.BuildArray(2, Matrix.Identity);
  4799. public static Quaternion: Quaternion[] = ArrayTools.BuildArray(3, Quaternion.Zero);
  4800. }
  4801. /**
  4802. * @hidden
  4803. */
  4804. export class TmpVectors {
  4805. public static Vector2: Vector2[] = ArrayTools.BuildArray(3, Vector2.Zero); // 3 temp Vector2 at once should be enough
  4806. public static Vector3: Vector3[] = ArrayTools.BuildArray(13, Vector3.Zero); // 13 temp Vector3 at once should be enough
  4807. public static Vector4: Vector4[] = ArrayTools.BuildArray(3, Vector4.Zero); // 3 temp Vector4 at once should be enough
  4808. public static Quaternion: Quaternion[] = ArrayTools.BuildArray(2, Quaternion.Zero); // 2 temp Quaternion at once should be enough
  4809. public static Matrix: Matrix[] = ArrayTools.BuildArray(8, Matrix.Identity); // 8 temp Matrices at once should be enough
  4810. }
  4811. _TypeStore.RegisteredTypes["BABYLON.Vector2"] = Vector2;
  4812. _TypeStore.RegisteredTypes["BABYLON.Vector3"] = Vector3;
  4813. _TypeStore.RegisteredTypes["BABYLON.Vector4"] = Vector4;
  4814. _TypeStore.RegisteredTypes["BABYLON.Matrix"] = Matrix;