math.vector.ts 214 KB

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