babylon.math.ts 291 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216
  1. module BABYLON {
  2. /**
  3. * Constant used to convert a value to gamma space
  4. * @ignorenaming
  5. */
  6. export const ToGammaSpace = 1 / 2.2;
  7. /**
  8. * Constant used to convert a value to linear space
  9. * @ignorenaming
  10. */
  11. export const ToLinearSpace = 2.2;
  12. /**
  13. * Constant used to define the minimal number value in Babylon.js
  14. * @ignorenaming
  15. */
  16. export const Epsilon = 0.001;
  17. /**
  18. * Class used to hold a RBG color
  19. */
  20. export class Color3 {
  21. /**
  22. * Creates a new Color3 object from red, green, blue values, all between 0 and 1
  23. * @param r defines the red component (between 0 and 1, default is 0)
  24. * @param g defines the green component (between 0 and 1, default is 0)
  25. * @param b defines the blue component (between 0 and 1, default is 0)
  26. */
  27. constructor(
  28. /**
  29. * Defines the red component (between 0 and 1, default is 0)
  30. */
  31. public r: number = 0,
  32. /**
  33. * Defines the green component (between 0 and 1, default is 0)
  34. */
  35. public g: number = 0,
  36. /**
  37. * Defines the blue component (between 0 and 1, default is 0)
  38. */
  39. public b: number = 0) {
  40. }
  41. /**
  42. * Creates a string with the Color3 current values
  43. * @returns the string representation of the Color3 object
  44. */
  45. public toString(): string {
  46. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  47. }
  48. /**
  49. * Returns the string "Color3"
  50. * @returns "Color3"
  51. */
  52. public getClassName(): string {
  53. return "Color3";
  54. }
  55. /**
  56. * Compute the Color3 hash code
  57. * @returns an unique number that can be used to hash Color3 objects
  58. */
  59. public getHashCode(): number {
  60. let hash = this.r || 0;
  61. hash = (hash * 397) ^ (this.g || 0);
  62. hash = (hash * 397) ^ (this.b || 0);
  63. return hash;
  64. }
  65. // Operators
  66. /**
  67. * Stores in the given array from the given starting index the red, green, blue values as successive elements
  68. * @param array defines the array where to store the r,g,b components
  69. * @param index defines an optional index in the target array to define where to start storing values
  70. * @returns the current Color3 object
  71. */
  72. public toArray(array: FloatArray, index: number = 0): Color3 {
  73. array[index] = this.r;
  74. array[index + 1] = this.g;
  75. array[index + 2] = this.b;
  76. return this;
  77. }
  78. /**
  79. * Returns a new Color4 object from the current Color3 and the given alpha
  80. * @param alpha defines the alpha component on the new Color4 object (default is 1)
  81. * @returns a new Color4 object
  82. */
  83. public toColor4(alpha: number = 1): Color4 {
  84. return new Color4(this.r, this.g, this.b, alpha);
  85. }
  86. /**
  87. * Returns a new array populated with 3 numeric elements : red, green and blue values
  88. * @returns the new array
  89. */
  90. public asArray(): number[] {
  91. var result = new Array<number>();
  92. this.toArray(result, 0);
  93. return result;
  94. }
  95. /**
  96. * Returns the luminance value
  97. * @returns a float value
  98. */
  99. public toLuminance(): number {
  100. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  101. }
  102. /**
  103. * Multiply each Color3 rgb values by the given Color3 rgb values in a new Color3 object
  104. * @param otherColor defines the second operand
  105. * @returns the new Color3 object
  106. */
  107. public multiply(otherColor: Color3): Color3 {
  108. return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  109. }
  110. /**
  111. * Multiply the rgb values of the Color3 and the given Color3 and stores the result in the object "result"
  112. * @param otherColor defines the second operand
  113. * @param result defines the Color3 object where to store the result
  114. * @returns the current Color3
  115. */
  116. public multiplyToRef(otherColor: Color3, result: Color3): Color3 {
  117. result.r = this.r * otherColor.r;
  118. result.g = this.g * otherColor.g;
  119. result.b = this.b * otherColor.b;
  120. return this;
  121. }
  122. /**
  123. * Determines equality between Color3 objects
  124. * @param otherColor defines the second operand
  125. * @returns true if the rgb values are equal to the given ones
  126. */
  127. public equals(otherColor: Color3): boolean {
  128. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  129. }
  130. /**
  131. * Determines equality between the current Color3 object and a set of r,b,g values
  132. * @param r defines the red component to check
  133. * @param g defines the green component to check
  134. * @param b defines the blue component to check
  135. * @returns true if the rgb values are equal to the given ones
  136. */
  137. public equalsFloats(r: number, g: number, b: number): boolean {
  138. return this.r === r && this.g === g && this.b === b;
  139. }
  140. /**
  141. * Multiplies in place each rgb value by scale
  142. * @param scale defines the scaling factor
  143. * @returns the updated Color3
  144. */
  145. public scale(scale: number): Color3 {
  146. return new Color3(this.r * scale, this.g * scale, this.b * scale);
  147. }
  148. /**
  149. * Multiplies the rgb values by scale and stores the result into "result"
  150. * @param scale defines the scaling factor
  151. * @param result defines the Color3 object where to store the result
  152. * @returns the unmodified current Color3
  153. */
  154. public scaleToRef(scale: number, result: Color3): Color3 {
  155. result.r = this.r * scale;
  156. result.g = this.g * scale;
  157. result.b = this.b * scale;
  158. return this;
  159. }
  160. /**
  161. * Scale the current Color3 values by a factor and add the result to a given Color3
  162. * @param scale defines the scale factor
  163. * @param result defines color to store the result into
  164. * @returns the unmodified current Color3
  165. */
  166. public scaleAndAddToRef(scale: number, result: Color3): Color3 {
  167. result.r += this.r * scale;
  168. result.g += this.g * scale;
  169. result.b += this.b * scale;
  170. return this;
  171. }
  172. /**
  173. * Clamps the rgb values by the min and max values and stores the result into "result"
  174. * @param min defines minimum clamping value (default is 0)
  175. * @param max defines maximum clamping value (default is 1)
  176. * @param result defines color to store the result into
  177. * @returns the original Color3
  178. */
  179. public clampToRef(min: number = 0, max: number = 1, result: Color3): Color3 {
  180. result.r = Scalar.Clamp(this.r, min, max);
  181. result.g = Scalar.Clamp(this.g, min, max);
  182. result.b = Scalar.Clamp(this.b, min, max);
  183. return this;
  184. }
  185. /**
  186. * Creates a new Color3 set with the added values of the current Color3 and of the given one
  187. * @param otherColor defines the second operand
  188. * @returns the new Color3
  189. */
  190. public add(otherColor: Color3): Color3 {
  191. return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
  192. }
  193. /**
  194. * Stores the result of the addition of the current Color3 and given one rgb values into "result"
  195. * @param otherColor defines the second operand
  196. * @param result defines Color3 object to store the result into
  197. * @returns the unmodified current Color3
  198. */
  199. public addToRef(otherColor: Color3, result: Color3): Color3 {
  200. result.r = this.r + otherColor.r;
  201. result.g = this.g + otherColor.g;
  202. result.b = this.b + otherColor.b;
  203. return this;
  204. }
  205. /**
  206. * Returns a new Color3 set with the subtracted values of the given one from the current Color3
  207. * @param otherColor defines the second operand
  208. * @returns the new Color3
  209. */
  210. public subtract(otherColor: Color3): Color3 {
  211. return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
  212. }
  213. /**
  214. * Stores the result of the subtraction of given one from the current Color3 rgb values into "result"
  215. * @param otherColor defines the second operand
  216. * @param result defines Color3 object to store the result into
  217. * @returns the unmodified current Color3
  218. */
  219. public subtractToRef(otherColor: Color3, result: Color3): Color3 {
  220. result.r = this.r - otherColor.r;
  221. result.g = this.g - otherColor.g;
  222. result.b = this.b - otherColor.b;
  223. return this;
  224. }
  225. /**
  226. * Copy the current object
  227. * @returns a new Color3 copied the current one
  228. */
  229. public clone(): Color3 {
  230. return new Color3(this.r, this.g, this.b);
  231. }
  232. /**
  233. * Copies the rgb values from the source in the current Color3
  234. * @param source defines the source Color3 object
  235. * @returns the updated Color3 object
  236. */
  237. public copyFrom(source: Color3): Color3 {
  238. this.r = source.r;
  239. this.g = source.g;
  240. this.b = source.b;
  241. return this;
  242. }
  243. /**
  244. * Updates the Color3 rgb values from the given floats
  245. * @param r defines the red component to read from
  246. * @param g defines the green component to read from
  247. * @param b defines the blue component to read from
  248. * @returns the current Color3 object
  249. */
  250. public copyFromFloats(r: number, g: number, b: number): Color3 {
  251. this.r = r;
  252. this.g = g;
  253. this.b = b;
  254. return this;
  255. }
  256. /**
  257. * Updates the Color3 rgb values from the given floats
  258. * @param r defines the red component to read from
  259. * @param g defines the green component to read from
  260. * @param b defines the blue component to read from
  261. * @returns the current Color3 object
  262. */
  263. public set(r: number, g: number, b: number): Color3 {
  264. return this.copyFromFloats(r, g, b);
  265. }
  266. /**
  267. * Compute the Color3 hexadecimal code as a string
  268. * @returns a string containing the hexadecimal representation of the Color3 object
  269. */
  270. public toHexString(): string {
  271. var intR = (this.r * 255) | 0;
  272. var intG = (this.g * 255) | 0;
  273. var intB = (this.b * 255) | 0;
  274. return "#" + Scalar.ToHex(intR) + Scalar.ToHex(intG) + Scalar.ToHex(intB);
  275. }
  276. /**
  277. * Computes a new Color3 converted from the current one to linear space
  278. * @returns a new Color3 object
  279. */
  280. public toLinearSpace(): Color3 {
  281. var convertedColor = new Color3();
  282. this.toLinearSpaceToRef(convertedColor);
  283. return convertedColor;
  284. }
  285. /**
  286. * Converts the Color3 values to linear space and stores the result in "convertedColor"
  287. * @param convertedColor defines the Color3 object where to store the linear space version
  288. * @returns the unmodified Color3
  289. */
  290. public toLinearSpaceToRef(convertedColor: Color3): Color3 {
  291. convertedColor.r = Math.pow(this.r, ToLinearSpace);
  292. convertedColor.g = Math.pow(this.g, ToLinearSpace);
  293. convertedColor.b = Math.pow(this.b, ToLinearSpace);
  294. return this;
  295. }
  296. /**
  297. * Computes a new Color3 converted from the current one to gamma space
  298. * @returns a new Color3 object
  299. */
  300. public toGammaSpace(): Color3 {
  301. var convertedColor = new Color3();
  302. this.toGammaSpaceToRef(convertedColor);
  303. return convertedColor;
  304. }
  305. /**
  306. * Converts the Color3 values to gamma space and stores the result in "convertedColor"
  307. * @param convertedColor defines the Color3 object where to store the gamma space version
  308. * @returns the unmodified Color3
  309. */
  310. public toGammaSpaceToRef(convertedColor: Color3): Color3 {
  311. convertedColor.r = Math.pow(this.r, ToGammaSpace);
  312. convertedColor.g = Math.pow(this.g, ToGammaSpace);
  313. convertedColor.b = Math.pow(this.b, ToGammaSpace);
  314. return this;
  315. }
  316. // Statics
  317. /**
  318. * Creates a new Color3 from the string containing valid hexadecimal values
  319. * @param hex defines a string containing valid hexadecimal values
  320. * @returns a new Color3 object
  321. */
  322. public static FromHexString(hex: string): Color3 {
  323. if (hex.substring(0, 1) !== "#" || hex.length !== 7) {
  324. return new Color3(0, 0, 0);
  325. }
  326. var r = parseInt(hex.substring(1, 3), 16);
  327. var g = parseInt(hex.substring(3, 5), 16);
  328. var b = parseInt(hex.substring(5, 7), 16);
  329. return Color3.FromInts(r, g, b);
  330. }
  331. /**
  332. * Creates a new Vector3 from the starting index of the given array
  333. * @param array defines the source array
  334. * @param offset defines an offset in the source array
  335. * @returns a new Color3 object
  336. */
  337. public static FromArray(array: ArrayLike<number>, offset: number = 0): Color3 {
  338. return new Color3(array[offset], array[offset + 1], array[offset + 2]);
  339. }
  340. /**
  341. * Creates a new Color3 from integer values (< 256)
  342. * @param r defines the red component to read from (value between 0 and 255)
  343. * @param g defines the green component to read from (value between 0 and 255)
  344. * @param b defines the blue component to read from (value between 0 and 255)
  345. * @returns a new Color3 object
  346. */
  347. public static FromInts(r: number, g: number, b: number): Color3 {
  348. return new Color3(r / 255.0, g / 255.0, b / 255.0);
  349. }
  350. /**
  351. * Creates a new Color3 with values linearly interpolated of "amount" between the start Color3 and the end Color3
  352. * @param start defines the start Color3 value
  353. * @param end defines the end Color3 value
  354. * @param amount defines the gradient value between start and end
  355. * @returns a new Color3 object
  356. */
  357. public static Lerp(start: Color3, end: Color3, amount: number): Color3 {
  358. var result = new Color3(0.0, 0.0, 0.0);
  359. Color3.LerpToRef(start, end, amount, result);
  360. return result;
  361. }
  362. /**
  363. * Creates a new Color3 with values linearly interpolated of "amount" between the start Color3 and the end Color3
  364. * @param left defines the start value
  365. * @param right defines the end value
  366. * @param amount defines the gradient factor
  367. * @param result defines the Color3 object where to store the result
  368. */
  369. public static LerpToRef(left: Color3, right: Color3, amount: number, result: Color3): void {
  370. result.r = left.r + ((right.r - left.r) * amount);
  371. result.g = left.g + ((right.g - left.g) * amount);
  372. result.b = left.b + ((right.b - left.b) * amount);
  373. }
  374. /**
  375. * Returns a Color3 value containing a red color
  376. * @returns a new Color3 object
  377. */
  378. public static Red(): Color3 { return new Color3(1, 0, 0); }
  379. /**
  380. * Returns a Color3 value containing a green color
  381. * @returns a new Color3 object
  382. */
  383. public static Green(): Color3 { return new Color3(0, 1, 0); }
  384. /**
  385. * Returns a Color3 value containing a blue color
  386. * @returns a new Color3 object
  387. */
  388. public static Blue(): Color3 { return new Color3(0, 0, 1); }
  389. /**
  390. * Returns a Color3 value containing a black color
  391. * @returns a new Color3 object
  392. */
  393. public static Black(): Color3 { return new Color3(0, 0, 0); }
  394. /**
  395. * Returns a Color3 value containing a white color
  396. * @returns a new Color3 object
  397. */
  398. public static White(): Color3 { return new Color3(1, 1, 1); }
  399. /**
  400. * Returns a Color3 value containing a purple color
  401. * @returns a new Color3 object
  402. */
  403. public static Purple(): Color3 { return new Color3(0.5, 0, 0.5); }
  404. /**
  405. * Returns a Color3 value containing a magenta color
  406. * @returns a new Color3 object
  407. */
  408. public static Magenta(): Color3 { return new Color3(1, 0, 1); }
  409. /**
  410. * Returns a Color3 value containing a yellow color
  411. * @returns a new Color3 object
  412. */
  413. public static Yellow(): Color3 { return new Color3(1, 1, 0); }
  414. /**
  415. * Returns a Color3 value containing a gray color
  416. * @returns a new Color3 object
  417. */
  418. public static Gray(): Color3 { return new Color3(0.5, 0.5, 0.5); }
  419. /**
  420. * Returns a Color3 value containing a teal color
  421. * @returns a new Color3 object
  422. */
  423. public static Teal(): Color3 { return new Color3(0, 1.0, 1.0); }
  424. /**
  425. * Returns a Color3 value containing a random color
  426. * @returns a new Color3 object
  427. */
  428. public static Random(): Color3 { return new Color3(Math.random(), Math.random(), Math.random()); }
  429. }
  430. /**
  431. * Class used to hold a RBGA color
  432. */
  433. export class Color4 {
  434. /**
  435. * Creates a new Color4 object from red, green, blue values, all between 0 and 1
  436. * @param r defines the red component (between 0 and 1, default is 0)
  437. * @param g defines the green component (between 0 and 1, default is 0)
  438. * @param b defines the blue component (between 0 and 1, default is 0)
  439. * @param a defines the alpha component (between 0 and 1, default is 1)
  440. */
  441. constructor(
  442. /**
  443. * Defines the red component (between 0 and 1, default is 0)
  444. */
  445. public r: number = 0,
  446. /**
  447. * Defines the green component (between 0 and 1, default is 0)
  448. */
  449. public g: number = 0,
  450. /**
  451. * Defines the blue component (between 0 and 1, default is 0)
  452. */
  453. public b: number = 0,
  454. /**
  455. * Defines the alpha component (between 0 and 1, default is 1)
  456. */
  457. public a: number = 1) {
  458. }
  459. // Operators
  460. /**
  461. * Adds in place the given Color4 values to the current Color4 object
  462. * @param right defines the second operand
  463. * @returns the current updated Color4 object
  464. */
  465. public addInPlace(right: Color4): Color4 {
  466. this.r += right.r;
  467. this.g += right.g;
  468. this.b += right.b;
  469. this.a += right.a;
  470. return this;
  471. }
  472. /**
  473. * Creates a new array populated with 4 numeric elements : red, green, blue, alpha values
  474. * @returns the new array
  475. */
  476. public asArray(): number[] {
  477. var result = new Array<number>();
  478. this.toArray(result, 0);
  479. return result;
  480. }
  481. /**
  482. * Stores from the starting index in the given array the Color4 successive values
  483. * @param array defines the array where to store the r,g,b components
  484. * @param index defines an optional index in the target array to define where to start storing values
  485. * @returns the current Color4 object
  486. */
  487. public toArray(array: number[], index: number = 0): Color4 {
  488. array[index] = this.r;
  489. array[index + 1] = this.g;
  490. array[index + 2] = this.b;
  491. array[index + 3] = this.a;
  492. return this;
  493. }
  494. /**
  495. * Creates a new Color4 set with the added values of the current Color4 and of the given one
  496. * @param right defines the second operand
  497. * @returns a new Color4 object
  498. */
  499. public add(right: Color4): Color4 {
  500. return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  501. }
  502. /**
  503. * Creates a new Color4 set with the subtracted values of the given one from the current Color4
  504. * @param right defines the second operand
  505. * @returns a new Color4 object
  506. */
  507. public subtract(right: Color4): Color4 {
  508. return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  509. }
  510. /**
  511. * Subtracts the given ones from the current Color4 values and stores the results in "result"
  512. * @param right defines the second operand
  513. * @param result defines the Color4 object where to store the result
  514. * @returns the current Color4 object
  515. */
  516. public subtractToRef(right: Color4, result: Color4): Color4 {
  517. result.r = this.r - right.r;
  518. result.g = this.g - right.g;
  519. result.b = this.b - right.b;
  520. result.a = this.a - right.a;
  521. return this;
  522. }
  523. /**
  524. * Creates a new Color4 with the current Color4 values multiplied by scale
  525. * @param scale defines the scaling factor to apply
  526. * @returns a new Color4 object
  527. */
  528. public scale(scale: number): Color4 {
  529. return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  530. }
  531. /**
  532. * Multiplies the current Color4 values by scale and stores the result in "result"
  533. * @param scale defines the scaling factor to apply
  534. * @param result defines the Color4 object where to store the result
  535. * @returns the current unmodified Color4
  536. */
  537. public scaleToRef(scale: number, result: Color4): Color4 {
  538. result.r = this.r * scale;
  539. result.g = this.g * scale;
  540. result.b = this.b * scale;
  541. result.a = this.a * scale;
  542. return this;
  543. }
  544. /**
  545. * Scale the current Color4 values by a factor and add the result to a given Color4
  546. * @param scale defines the scale factor
  547. * @param result defines the Color4 object where to store the result
  548. * @returns the unmodified current Color4
  549. */
  550. public scaleAndAddToRef(scale: number, result: Color4): Color4 {
  551. result.r += this.r * scale;
  552. result.g += this.g * scale;
  553. result.b += this.b * scale;
  554. result.a += this.a * scale;
  555. return this;
  556. }
  557. /**
  558. * Clamps the rgb values by the min and max values and stores the result into "result"
  559. * @param min defines minimum clamping value (default is 0)
  560. * @param max defines maximum clamping value (default is 1)
  561. * @param result defines color to store the result into.
  562. * @returns the cuurent Color4
  563. */
  564. public clampToRef(min: number = 0, max: number = 1, result: Color4): Color4 {
  565. result.r = Scalar.Clamp(this.r, min, max);
  566. result.g = Scalar.Clamp(this.g, min, max);
  567. result.b = Scalar.Clamp(this.b, min, max);
  568. result.a = Scalar.Clamp(this.a, min, max);
  569. return this;
  570. }
  571. /**
  572. * Multipy an Color4 value by another and return a new Color4 object
  573. * @param color defines the Color4 value to multiply by
  574. * @returns a new Color4 object
  575. */
  576. public multiply(color: Color4): Color4 {
  577. return new Color4(this.r * color.r, this.g * color.g, this.b * color.b, this.a * color.a);
  578. }
  579. /**
  580. * Multipy a Color4 value by another and push the result in a reference value
  581. * @param color defines the Color4 value to multiply by
  582. * @param result defines the Color4 to fill the result in
  583. * @returns the result Color4
  584. */
  585. public multiplyToRef(color: Color4, result: Color4): Color4 {
  586. result.r = this.r * color.r;
  587. result.g = this.g * color.g;
  588. result.b = this.b * color.b;
  589. result.a = this.a * color.a;
  590. return result;
  591. }
  592. /**
  593. * Creates a string with the Color4 current values
  594. * @returns the string representation of the Color4 object
  595. */
  596. public toString(): string {
  597. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  598. }
  599. /**
  600. * Returns the string "Color4"
  601. * @returns "Color4"
  602. */
  603. public getClassName(): string {
  604. return "Color4";
  605. }
  606. /**
  607. * Compute the Color4 hash code
  608. * @returns an unique number that can be used to hash Color4 objects
  609. */
  610. public getHashCode(): number {
  611. let hash = this.r || 0;
  612. hash = (hash * 397) ^ (this.g || 0);
  613. hash = (hash * 397) ^ (this.b || 0);
  614. hash = (hash * 397) ^ (this.a || 0);
  615. return hash;
  616. }
  617. /**
  618. * Creates a new Color4 copied from the current one
  619. * @returns a new Color4 object
  620. */
  621. public clone(): Color4 {
  622. return new Color4(this.r, this.g, this.b, this.a);
  623. }
  624. /**
  625. * Copies the given Color4 values into the current one
  626. * @param source defines the source Color4 object
  627. * @returns the current updated Color4 object
  628. */
  629. public copyFrom(source: Color4): Color4 {
  630. this.r = source.r;
  631. this.g = source.g;
  632. this.b = source.b;
  633. this.a = source.a;
  634. return this;
  635. }
  636. /**
  637. * Copies the given float values into the current one
  638. * @param r defines the red component to read from
  639. * @param g defines the green component to read from
  640. * @param b defines the blue component to read from
  641. * @param a defines the alpha component to read from
  642. * @returns the current updated Color4 object
  643. */
  644. public copyFromFloats(r: number, g: number, b: number, a: number): Color4 {
  645. this.r = r;
  646. this.g = g;
  647. this.b = b;
  648. this.a = a;
  649. return this;
  650. }
  651. /**
  652. * Copies the given float values into the current one
  653. * @param r defines the red component to read from
  654. * @param g defines the green component to read from
  655. * @param b defines the blue component to read from
  656. * @param a defines the alpha component to read from
  657. * @returns the current updated Color4 object
  658. */
  659. public set(r: number, g: number, b: number, a: number): Color4 {
  660. return this.copyFromFloats(r, g, b, a);
  661. }
  662. /**
  663. * Compute the Color4 hexadecimal code as a string
  664. * @returns a string containing the hexadecimal representation of the Color4 object
  665. */
  666. public toHexString(): string {
  667. var intR = (this.r * 255) | 0;
  668. var intG = (this.g * 255) | 0;
  669. var intB = (this.b * 255) | 0;
  670. var intA = (this.a * 255) | 0;
  671. return "#" + Scalar.ToHex(intR) + Scalar.ToHex(intG) + Scalar.ToHex(intB) + Scalar.ToHex(intA);
  672. }
  673. /**
  674. * Computes a new Color4 converted from the current one to linear space
  675. * @returns a new Color4 object
  676. */
  677. public toLinearSpace(): Color4 {
  678. var convertedColor = new Color4();
  679. this.toLinearSpaceToRef(convertedColor);
  680. return convertedColor;
  681. }
  682. /**
  683. * Converts the Color4 values to linear space and stores the result in "convertedColor"
  684. * @param convertedColor defines the Color4 object where to store the linear space version
  685. * @returns the unmodified Color4
  686. */
  687. public toLinearSpaceToRef(convertedColor: Color4): Color4 {
  688. convertedColor.r = Math.pow(this.r, ToLinearSpace);
  689. convertedColor.g = Math.pow(this.g, ToLinearSpace);
  690. convertedColor.b = Math.pow(this.b, ToLinearSpace);
  691. convertedColor.a = this.a;
  692. return this;
  693. }
  694. /**
  695. * Computes a new Color4 converted from the current one to gamma space
  696. * @returns a new Color4 object
  697. */
  698. public toGammaSpace(): Color4 {
  699. var convertedColor = new Color4();
  700. this.toGammaSpaceToRef(convertedColor);
  701. return convertedColor;
  702. }
  703. /**
  704. * Converts the Color4 values to gamma space and stores the result in "convertedColor"
  705. * @param convertedColor defines the Color4 object where to store the gamma space version
  706. * @returns the unmodified Color4
  707. */
  708. public toGammaSpaceToRef(convertedColor: Color4): Color4 {
  709. convertedColor.r = Math.pow(this.r, ToGammaSpace);
  710. convertedColor.g = Math.pow(this.g, ToGammaSpace);
  711. convertedColor.b = Math.pow(this.b, ToGammaSpace);
  712. convertedColor.a = this.a;
  713. return this;
  714. }
  715. // Statics
  716. /**
  717. * Creates a new Color4 from the string containing valid hexadecimal values
  718. * @param hex defines a string containing valid hexadecimal values
  719. * @returns a new Color4 object
  720. */
  721. public static FromHexString(hex: string): Color4 {
  722. if (hex.substring(0, 1) !== "#" || hex.length !== 9) {
  723. return new Color4(0.0, 0.0, 0.0, 0.0);
  724. }
  725. var r = parseInt(hex.substring(1, 3), 16);
  726. var g = parseInt(hex.substring(3, 5), 16);
  727. var b = parseInt(hex.substring(5, 7), 16);
  728. var a = parseInt(hex.substring(7, 9), 16);
  729. return Color4.FromInts(r, g, b, a);
  730. }
  731. /**
  732. * Creates a new Color4 object set with the linearly interpolated values of "amount" between the left Color4 object and the right Color4 object
  733. * @param left defines the start value
  734. * @param right defines the end value
  735. * @param amount defines the gradient factor
  736. * @returns a new Color4 object
  737. */
  738. public static Lerp(left: Color4, right: Color4, amount: number): Color4 {
  739. var result = new Color4(0.0, 0.0, 0.0, 0.0);
  740. Color4.LerpToRef(left, right, amount, result);
  741. return result;
  742. }
  743. /**
  744. * Set the given "result" with the linearly interpolated values of "amount" between the left Color4 object and the right Color4 object
  745. * @param left defines the start value
  746. * @param right defines the end value
  747. * @param amount defines the gradient factor
  748. * @param result defines the Color4 object where to store data
  749. */
  750. public static LerpToRef(left: Color4, right: Color4, amount: number, result: Color4): void {
  751. result.r = left.r + (right.r - left.r) * amount;
  752. result.g = left.g + (right.g - left.g) * amount;
  753. result.b = left.b + (right.b - left.b) * amount;
  754. result.a = left.a + (right.a - left.a) * amount;
  755. }
  756. /**
  757. * Creates a new Color4 from a Color3 and an alpha value
  758. * @param color3 defines the source Color3 to read from
  759. * @param alpha defines the alpha component (1.0 by default)
  760. * @returns a new Color4 object
  761. */
  762. public static FromColor3(color3: Color3, alpha: number = 1.0): Color4 {
  763. return new Color4(color3.r, color3.g, color3.b, alpha);
  764. }
  765. /**
  766. * Creates a new Color4 from the starting index element of the given array
  767. * @param array defines the source array to read from
  768. * @param offset defines the offset in the source array
  769. * @returns a new Color4 object
  770. */
  771. public static FromArray(array: ArrayLike<number>, offset: number = 0): Color4 {
  772. return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  773. }
  774. /**
  775. * Creates a new Color3 from integer values (< 256)
  776. * @param r defines the red component to read from (value between 0 and 255)
  777. * @param g defines the green component to read from (value between 0 and 255)
  778. * @param b defines the blue component to read from (value between 0 and 255)
  779. * @param a defines the alpha component to read from (value between 0 and 255)
  780. * @returns a new Color3 object
  781. */
  782. public static FromInts(r: number, g: number, b: number, a: number): Color4 {
  783. return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  784. }
  785. /**
  786. * Check the content of a given array and convert it to an array containing RGBA data
  787. * If the original array was already containing count * 4 values then it is returned directly
  788. * @param colors defines the array to check
  789. * @param count defines the number of RGBA data to expect
  790. * @returns an array containing count * 4 values (RGBA)
  791. */
  792. public static CheckColors4(colors: number[], count: number): number[] {
  793. // Check if color3 was used
  794. if (colors.length === count * 3) {
  795. var colors4 = [];
  796. for (var index = 0; index < colors.length; index += 3) {
  797. var newIndex = (index / 3) * 4;
  798. colors4[newIndex] = colors[index];
  799. colors4[newIndex + 1] = colors[index + 1];
  800. colors4[newIndex + 2] = colors[index + 2];
  801. colors4[newIndex + 3] = 1.0;
  802. }
  803. return colors4;
  804. }
  805. return colors;
  806. }
  807. }
  808. /**
  809. * Class representing a vector containing 2 coordinates
  810. */
  811. export class Vector2 {
  812. /**
  813. * Creates a new Vector2 from the given x and y coordinates
  814. * @param x defines the first coordinate
  815. * @param y defines the second coordinate
  816. */
  817. constructor(
  818. /** defines the first coordinate */
  819. public x: number = 0,
  820. /** defines the second coordinate */
  821. public y: number = 0) {
  822. }
  823. /**
  824. * Gets a string with the Vector2 coordinates
  825. * @returns a string with the Vector2 coordinates
  826. */
  827. public toString(): string {
  828. return "{X: " + this.x + " Y:" + this.y + "}";
  829. }
  830. /**
  831. * Gets class name
  832. * @returns the string "Vector2"
  833. */
  834. public getClassName(): string {
  835. return "Vector2";
  836. }
  837. /**
  838. * Gets current vector hash code
  839. * @returns the Vector2 hash code as a number
  840. */
  841. public getHashCode(): number {
  842. let hash = this.x || 0;
  843. hash = (hash * 397) ^ (this.y || 0);
  844. return hash;
  845. }
  846. // Operators
  847. /**
  848. * Sets the Vector2 coordinates in the given array or Float32Array from the given index.
  849. * @param array defines the source array
  850. * @param index defines the offset in source array
  851. * @returns the current Vector2
  852. */
  853. public toArray(array: FloatArray, index: number = 0): Vector2 {
  854. array[index] = this.x;
  855. array[index + 1] = this.y;
  856. return this;
  857. }
  858. /**
  859. * Copy the current vector to an array
  860. * @returns a new array with 2 elements: the Vector2 coordinates.
  861. */
  862. public asArray(): number[] {
  863. var result = new Array<number>();
  864. this.toArray(result, 0);
  865. return result;
  866. }
  867. /**
  868. * Sets the Vector2 coordinates with the given Vector2 coordinates
  869. * @param source defines the source Vector2
  870. * @returns the current updated Vector2
  871. */
  872. public copyFrom(source: Vector2): Vector2 {
  873. this.x = source.x;
  874. this.y = source.y;
  875. return this;
  876. }
  877. /**
  878. * Sets the Vector2 coordinates with the given floats
  879. * @param x defines the first coordinate
  880. * @param y defines the second coordinate
  881. * @returns the current updated Vector2
  882. */
  883. public copyFromFloats(x: number, y: number): Vector2 {
  884. this.x = x;
  885. this.y = y;
  886. return this;
  887. }
  888. /**
  889. * Sets the Vector2 coordinates with the given floats
  890. * @param x defines the first coordinate
  891. * @param y defines the second coordinate
  892. * @returns the current updated Vector2
  893. */
  894. public set(x: number, y: number): Vector2 {
  895. return this.copyFromFloats(x, y);
  896. }
  897. /**
  898. * Add another vector with the current one
  899. * @param otherVector defines the other vector
  900. * @returns a new Vector2 set with the addition of the current Vector2 and the given one coordinates
  901. */
  902. public add(otherVector: Vector2): Vector2 {
  903. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  904. }
  905. /**
  906. * Sets the "result" coordinates with the addition of the current Vector2 and the given one coordinates
  907. * @param otherVector defines the other vector
  908. * @param result defines the target vector
  909. * @returns the unmodified current Vector2
  910. */
  911. public addToRef(otherVector: Vector2, result: Vector2): Vector2 {
  912. result.x = this.x + otherVector.x;
  913. result.y = this.y + otherVector.y;
  914. return this;
  915. }
  916. /**
  917. * Set the Vector2 coordinates by adding the given Vector2 coordinates
  918. * @param otherVector defines the other vector
  919. * @returns the current updated Vector2
  920. */
  921. public addInPlace(otherVector: Vector2): Vector2 {
  922. this.x += otherVector.x;
  923. this.y += otherVector.y;
  924. return this;
  925. }
  926. /**
  927. * Gets a new Vector2 by adding the current Vector2 coordinates to the given Vector3 x, y coordinates
  928. * @param otherVector defines the other vector
  929. * @returns a new Vector2
  930. */
  931. public addVector3(otherVector: Vector3): Vector2 {
  932. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  933. }
  934. /**
  935. * Gets a new Vector2 set with the subtracted coordinates of the given one from the current Vector2
  936. * @param otherVector defines the other vector
  937. * @returns a new Vector2
  938. */
  939. public subtract(otherVector: Vector2): Vector2 {
  940. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  941. }
  942. /**
  943. * Sets the "result" coordinates with the subtraction of the given one from the current Vector2 coordinates.
  944. * @param otherVector defines the other vector
  945. * @param result defines the target vector
  946. * @returns the unmodified current Vector2
  947. */
  948. public subtractToRef(otherVector: Vector2, result: Vector2): Vector2 {
  949. result.x = this.x - otherVector.x;
  950. result.y = this.y - otherVector.y;
  951. return this;
  952. }
  953. /**
  954. * Sets the current Vector2 coordinates by subtracting from it the given one coordinates
  955. * @param otherVector defines the other vector
  956. * @returns the current updated Vector2
  957. */
  958. public subtractInPlace(otherVector: Vector2): Vector2 {
  959. this.x -= otherVector.x;
  960. this.y -= otherVector.y;
  961. return this;
  962. }
  963. /**
  964. * Multiplies in place the current Vector2 coordinates by the given ones
  965. * @param otherVector defines the other vector
  966. * @returns the current updated Vector2
  967. */
  968. public multiplyInPlace(otherVector: Vector2): Vector2 {
  969. this.x *= otherVector.x;
  970. this.y *= otherVector.y;
  971. return this;
  972. }
  973. /**
  974. * Returns a new Vector2 set with the multiplication of the current Vector2 and the given one coordinates
  975. * @param otherVector defines the other vector
  976. * @returns a new Vector2
  977. */
  978. public multiply(otherVector: Vector2): Vector2 {
  979. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  980. }
  981. /**
  982. * Sets "result" coordinates with the multiplication of the current Vector2 and the given one coordinates
  983. * @param otherVector defines the other vector
  984. * @param result defines the target vector
  985. * @returns the unmodified current Vector2
  986. */
  987. public multiplyToRef(otherVector: Vector2, result: Vector2): Vector2 {
  988. result.x = this.x * otherVector.x;
  989. result.y = this.y * otherVector.y;
  990. return this;
  991. }
  992. /**
  993. * Gets a new Vector2 set with the Vector2 coordinates multiplied by the given floats
  994. * @param x defines the first coordinate
  995. * @param y defines the second coordinate
  996. * @returns a new Vector2
  997. */
  998. public multiplyByFloats(x: number, y: number): Vector2 {
  999. return new Vector2(this.x * x, this.y * y);
  1000. }
  1001. /**
  1002. * Returns a new Vector2 set with the Vector2 coordinates divided by the given one coordinates
  1003. * @param otherVector defines the other vector
  1004. * @returns a new Vector2
  1005. */
  1006. public divide(otherVector: Vector2): Vector2 {
  1007. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  1008. }
  1009. /**
  1010. * Sets the "result" coordinates with the Vector2 divided by the given one coordinates
  1011. * @param otherVector defines the other vector
  1012. * @param result defines the target vector
  1013. * @returns the unmodified current Vector2
  1014. */
  1015. public divideToRef(otherVector: Vector2, result: Vector2): Vector2 {
  1016. result.x = this.x / otherVector.x;
  1017. result.y = this.y / otherVector.y;
  1018. return this;
  1019. }
  1020. /**
  1021. * Divides the current Vector2 coordinates by the given ones
  1022. * @param otherVector defines the other vector
  1023. * @returns the current updated Vector2
  1024. */
  1025. public divideInPlace(otherVector: Vector2): Vector2 {
  1026. return this.divideToRef(otherVector, this);
  1027. }
  1028. /**
  1029. * Gets a new Vector2 with current Vector2 negated coordinates
  1030. * @returns a new Vector2
  1031. */
  1032. public negate(): Vector2 {
  1033. return new Vector2(-this.x, -this.y);
  1034. }
  1035. /**
  1036. * Multiply the Vector2 coordinates by scale
  1037. * @param scale defines the scaling factor
  1038. * @returns the current updated Vector2
  1039. */
  1040. public scaleInPlace(scale: number): Vector2 {
  1041. this.x *= scale;
  1042. this.y *= scale;
  1043. return this;
  1044. }
  1045. /**
  1046. * Returns a new Vector2 scaled by "scale" from the current Vector2
  1047. * @param scale defines the scaling factor
  1048. * @returns a new Vector2
  1049. */
  1050. public scale(scale: number): Vector2 {
  1051. let result = new Vector2(0, 0);
  1052. this.scaleToRef(scale, result);
  1053. return result;
  1054. }
  1055. /**
  1056. * Scale the current Vector2 values by a factor to a given Vector2
  1057. * @param scale defines the scale factor
  1058. * @param result defines the Vector2 object where to store the result
  1059. * @returns the unmodified current Vector2
  1060. */
  1061. public scaleToRef(scale: number, result: Vector2): Vector2 {
  1062. result.x = this.x * scale;
  1063. result.y = this.y * scale;
  1064. return this;
  1065. }
  1066. /**
  1067. * Scale the current Vector2 values by a factor and add the result to a given Vector2
  1068. * @param scale defines the scale factor
  1069. * @param result defines the Vector2 object where to store the result
  1070. * @returns the unmodified current Vector2
  1071. */
  1072. public scaleAndAddToRef(scale: number, result: Vector2): Vector2 {
  1073. result.x += this.x * scale;
  1074. result.y += this.y * scale;
  1075. return this;
  1076. }
  1077. /**
  1078. * Gets a boolean if two vectors are equals
  1079. * @param otherVector defines the other vector
  1080. * @returns true if the given vector coordinates strictly equal the current Vector2 ones
  1081. */
  1082. public equals(otherVector: Vector2): boolean {
  1083. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  1084. }
  1085. /**
  1086. * Gets a boolean if two vectors are equals (using an epsilon value)
  1087. * @param otherVector defines the other vector
  1088. * @param epsilon defines the minimal distance to consider equality
  1089. * @returns true if the given vector coordinates are close to the current ones by a distance of epsilon.
  1090. */
  1091. public equalsWithEpsilon(otherVector: Vector2, epsilon: number = Epsilon): boolean {
  1092. return otherVector && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon) && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon);
  1093. }
  1094. /**
  1095. * Gets a new Vector2 from current Vector2 floored values
  1096. * @returns a new Vector2
  1097. */
  1098. public floor(): Vector2 {
  1099. return new Vector2(Math.floor(this.x), Math.floor(this.y));
  1100. }
  1101. /**
  1102. * Gets a new Vector2 from current Vector2 floored values
  1103. * @returns a new Vector2
  1104. */
  1105. public fract(): Vector2 {
  1106. return new Vector2(this.x - Math.floor(this.x), this.y - Math.floor(this.y));
  1107. }
  1108. // Properties
  1109. /**
  1110. * Gets the length of the vector
  1111. * @returns the vector length (float)
  1112. */
  1113. public length(): number {
  1114. return Math.sqrt(this.x * this.x + this.y * this.y);
  1115. }
  1116. /**
  1117. * Gets the vector squared length
  1118. * @returns the vector squared length (float)
  1119. */
  1120. public lengthSquared(): number {
  1121. return (this.x * this.x + this.y * this.y);
  1122. }
  1123. // Methods
  1124. /**
  1125. * Normalize the vector
  1126. * @returns the current updated Vector2
  1127. */
  1128. public normalize(): Vector2 {
  1129. var len = this.length();
  1130. if (len === 0) {
  1131. return this;
  1132. }
  1133. var num = 1.0 / len;
  1134. this.x *= num;
  1135. this.y *= num;
  1136. return this;
  1137. }
  1138. /**
  1139. * Gets a new Vector2 copied from the Vector2
  1140. * @returns a new Vector2
  1141. */
  1142. public clone(): Vector2 {
  1143. return new Vector2(this.x, this.y);
  1144. }
  1145. // Statics
  1146. /**
  1147. * Gets a new Vector2(0, 0)
  1148. * @returns a new Vector2
  1149. */
  1150. public static Zero(): Vector2 {
  1151. return new Vector2(0, 0);
  1152. }
  1153. /**
  1154. * Gets a new Vector2(1, 1)
  1155. * @returns a new Vector2
  1156. */
  1157. public static One(): Vector2 {
  1158. return new Vector2(1, 1);
  1159. }
  1160. /**
  1161. * Gets a new Vector2 set from the given index element of the given array
  1162. * @param array defines the data source
  1163. * @param offset defines the offset in the data source
  1164. * @returns a new Vector2
  1165. */
  1166. public static FromArray(array: ArrayLike<number>, offset: number = 0): Vector2 {
  1167. return new Vector2(array[offset], array[offset + 1]);
  1168. }
  1169. /**
  1170. * Sets "result" from the given index element of the given array
  1171. * @param array defines the data source
  1172. * @param offset defines the offset in the data source
  1173. * @param result defines the target vector
  1174. */
  1175. public static FromArrayToRef(array: ArrayLike<number>, offset: number, result: Vector2): void {
  1176. result.x = array[offset];
  1177. result.y = array[offset + 1];
  1178. }
  1179. /**
  1180. * Gets a new Vector2 located for "amount" (float) on the CatmullRom spline defined by the given four Vector2
  1181. * @param value1 defines 1st point of control
  1182. * @param value2 defines 2nd point of control
  1183. * @param value3 defines 3rd point of control
  1184. * @param value4 defines 4th point of control
  1185. * @param amount defines the interpolation factor
  1186. * @returns a new Vector2
  1187. */
  1188. public static CatmullRom(value1: Vector2, value2: Vector2, value3: Vector2, value4: Vector2, amount: number): Vector2 {
  1189. var squared = amount * amount;
  1190. var cubed = amount * squared;
  1191. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  1192. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  1193. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  1194. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  1195. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  1196. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  1197. return new Vector2(x, y);
  1198. }
  1199. /**
  1200. * 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".
  1201. * If a coordinate of "value" is lower than "min" coordinates, the returned Vector2 is given this "min" coordinate.
  1202. * If a coordinate of "value" is greater than "max" coordinates, the returned Vector2 is given this "max" coordinate
  1203. * @param value defines the value to clamp
  1204. * @param min defines the lower limit
  1205. * @param max defines the upper limit
  1206. * @returns a new Vector2
  1207. */
  1208. public static Clamp(value: Vector2, min: Vector2, max: Vector2): Vector2 {
  1209. var x = value.x;
  1210. x = (x > max.x) ? max.x : x;
  1211. x = (x < min.x) ? min.x : x;
  1212. var y = value.y;
  1213. y = (y > max.y) ? max.y : y;
  1214. y = (y < min.y) ? min.y : y;
  1215. return new Vector2(x, y);
  1216. }
  1217. /**
  1218. * Returns a new Vector2 located for "amount" (float) on the Hermite spline defined by the vectors "value1", "value3", "tangent1", "tangent2"
  1219. * @param value1 defines the 1st control point
  1220. * @param tangent1 defines the outgoing tangent
  1221. * @param value2 defines the 2nd control point
  1222. * @param tangent2 defines the incoming tangent
  1223. * @param amount defines the interpolation factor
  1224. * @returns a new Vector2
  1225. */
  1226. public static Hermite(value1: Vector2, tangent1: Vector2, value2: Vector2, tangent2: Vector2, amount: number): Vector2 {
  1227. var squared = amount * amount;
  1228. var cubed = amount * squared;
  1229. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  1230. var part2 = (-2.0 * cubed) + (3.0 * squared);
  1231. var part3 = (cubed - (2.0 * squared)) + amount;
  1232. var part4 = cubed - squared;
  1233. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  1234. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  1235. return new Vector2(x, y);
  1236. }
  1237. /**
  1238. * Returns a new Vector2 located for "amount" (float) on the linear interpolation between the vector "start" adn the vector "end".
  1239. * @param start defines the start vector
  1240. * @param end defines the end vector
  1241. * @param amount defines the interpolation factor
  1242. * @returns a new Vector2
  1243. */
  1244. public static Lerp(start: Vector2, end: Vector2, amount: number): Vector2 {
  1245. var x = start.x + ((end.x - start.x) * amount);
  1246. var y = start.y + ((end.y - start.y) * amount);
  1247. return new Vector2(x, y);
  1248. }
  1249. /**
  1250. * Gets the dot product of the vector "left" and the vector "right"
  1251. * @param left defines first vector
  1252. * @param right defines second vector
  1253. * @returns the dot product (float)
  1254. */
  1255. public static Dot(left: Vector2, right: Vector2): number {
  1256. return left.x * right.x + left.y * right.y;
  1257. }
  1258. /**
  1259. * Returns a new Vector2 equal to the normalized given vector
  1260. * @param vector defines the vector to normalize
  1261. * @returns a new Vector2
  1262. */
  1263. public static Normalize(vector: Vector2): Vector2 {
  1264. var newVector = vector.clone();
  1265. newVector.normalize();
  1266. return newVector;
  1267. }
  1268. /**
  1269. * Gets a new Vector2 set with the minimal coordinate values from the "left" and "right" vectors
  1270. * @param left defines 1st vector
  1271. * @param right defines 2nd vector
  1272. * @returns a new Vector2
  1273. */
  1274. public static Minimize(left: Vector2, right: Vector2): Vector2 {
  1275. var x = (left.x < right.x) ? left.x : right.x;
  1276. var y = (left.y < right.y) ? left.y : right.y;
  1277. return new Vector2(x, y);
  1278. }
  1279. /**
  1280. * Gets a new Vecto2 set with the maximal coordinate values from the "left" and "right" vectors
  1281. * @param left defines 1st vector
  1282. * @param right defines 2nd vector
  1283. * @returns a new Vector2
  1284. */
  1285. public static Maximize(left: Vector2, right: Vector2): Vector2 {
  1286. var x = (left.x > right.x) ? left.x : right.x;
  1287. var y = (left.y > right.y) ? left.y : right.y;
  1288. return new Vector2(x, y);
  1289. }
  1290. /**
  1291. * Gets a new Vector2 set with the transformed coordinates of the given vector by the given transformation matrix
  1292. * @param vector defines the vector to transform
  1293. * @param transformation defines the matrix to apply
  1294. * @returns a new Vector2
  1295. */
  1296. public static Transform(vector: Vector2, transformation: Matrix): Vector2 {
  1297. let r = Vector2.Zero();
  1298. Vector2.TransformToRef(vector, transformation, r);
  1299. return r;
  1300. }
  1301. /**
  1302. * Transforms the given vector coordinates by the given transformation matrix and stores the result in the vector "result" coordinates
  1303. * @param vector defines the vector to transform
  1304. * @param transformation defines the matrix to apply
  1305. * @param result defines the target vector
  1306. */
  1307. public static TransformToRef(vector: Vector2, transformation: Matrix, result: Vector2) {
  1308. const m = transformation.m;
  1309. var x = (vector.x * m[0]) + (vector.y * m[4]) + m[12];
  1310. var y = (vector.x * m[1]) + (vector.y * m[5]) + m[13];
  1311. result.x = x;
  1312. result.y = y;
  1313. }
  1314. /**
  1315. * Determines if a given vector is included in a triangle
  1316. * @param p defines the vector to test
  1317. * @param p0 defines 1st triangle point
  1318. * @param p1 defines 2nd triangle point
  1319. * @param p2 defines 3rd triangle point
  1320. * @returns true if the point "p" is in the triangle defined by the vertors "p0", "p1", "p2"
  1321. */
  1322. public static PointInTriangle(p: Vector2, p0: Vector2, p1: Vector2, p2: Vector2) {
  1323. let a = 1 / 2 * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
  1324. let sign = a < 0 ? -1 : 1;
  1325. let s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
  1326. let t = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;
  1327. return s > 0 && t > 0 && (s + t) < 2 * a * sign;
  1328. }
  1329. /**
  1330. * Gets the distance between the vectors "value1" and "value2"
  1331. * @param value1 defines first vector
  1332. * @param value2 defines second vector
  1333. * @returns the distance between vectors
  1334. */
  1335. public static Distance(value1: Vector2, value2: Vector2): number {
  1336. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  1337. }
  1338. /**
  1339. * Returns the squared distance between the vectors "value1" and "value2"
  1340. * @param value1 defines first vector
  1341. * @param value2 defines second vector
  1342. * @returns the squared distance between vectors
  1343. */
  1344. public static DistanceSquared(value1: Vector2, value2: Vector2): number {
  1345. var x = value1.x - value2.x;
  1346. var y = value1.y - value2.y;
  1347. return (x * x) + (y * y);
  1348. }
  1349. /**
  1350. * Gets a new Vector2 located at the center of the vectors "value1" and "value2"
  1351. * @param value1 defines first vector
  1352. * @param value2 defines second vector
  1353. * @returns a new Vector2
  1354. */
  1355. public static Center(value1: Vector2, value2: Vector2): Vector2 {
  1356. var center = value1.add(value2);
  1357. center.scaleInPlace(0.5);
  1358. return center;
  1359. }
  1360. /**
  1361. * Gets the shortest distance (float) between the point "p" and the segment defined by the two points "segA" and "segB".
  1362. * @param p defines the middle point
  1363. * @param segA defines one point of the segment
  1364. * @param segB defines the other point of the segment
  1365. * @returns the shortest distance
  1366. */
  1367. public static DistanceOfPointFromSegment(p: Vector2, segA: Vector2, segB: Vector2): number {
  1368. let l2 = Vector2.DistanceSquared(segA, segB);
  1369. if (l2 === 0.0) {
  1370. return Vector2.Distance(p, segA);
  1371. }
  1372. let v = segB.subtract(segA);
  1373. let t = Math.max(0, Math.min(1, Vector2.Dot(p.subtract(segA), v) / l2));
  1374. let proj = segA.add(v.multiplyByFloats(t, t));
  1375. return Vector2.Distance(p, proj);
  1376. }
  1377. }
  1378. /**
  1379. * Classed used to store (x,y,z) vector representation
  1380. * A Vector3 is the main object used in 3D geometry
  1381. * It can represent etiher the coordinates of a point the space, either a direction
  1382. * Reminder: Babylon.js uses a left handed forward facing system
  1383. */
  1384. export class Vector3 {
  1385. /**
  1386. * Creates a new Vector3 object from the given x, y, z (floats) coordinates.
  1387. * @param x defines the first coordinates (on X axis)
  1388. * @param y defines the second coordinates (on Y axis)
  1389. * @param z defines the third coordinates (on Z axis)
  1390. */
  1391. constructor(
  1392. /**
  1393. * Defines the first coordinates (on X axis)
  1394. */
  1395. public x: number = 0,
  1396. /**
  1397. * Defines the second coordinates (on Y axis)
  1398. */
  1399. public y: number = 0,
  1400. /**
  1401. * Defines the third coordinates (on Z axis)
  1402. */
  1403. public z: number = 0
  1404. ) {
  1405. }
  1406. /**
  1407. * Creates a string representation of the Vector3
  1408. * @returns a string with the Vector3 coordinates.
  1409. */
  1410. public toString(): string {
  1411. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  1412. }
  1413. /**
  1414. * Gets the class name
  1415. * @returns the string "Vector3"
  1416. */
  1417. public getClassName(): string {
  1418. return "Vector3";
  1419. }
  1420. /**
  1421. * Creates the Vector3 hash code
  1422. * @returns a number which tends to be unique between Vector3 instances
  1423. */
  1424. public getHashCode(): number {
  1425. let hash = this.x || 0;
  1426. hash = (hash * 397) ^ (this.y || 0);
  1427. hash = (hash * 397) ^ (this.z || 0);
  1428. return hash;
  1429. }
  1430. // Operators
  1431. /**
  1432. * Creates an array containing three elements : the coordinates of the Vector3
  1433. * @returns a new array of numbers
  1434. */
  1435. public asArray(): number[] {
  1436. var result: number[] = [];
  1437. this.toArray(result, 0);
  1438. return result;
  1439. }
  1440. /**
  1441. * Populates the given array or Float32Array from the given index with the successive coordinates of the Vector3
  1442. * @param array defines the destination array
  1443. * @param index defines the offset in the destination array
  1444. * @returns the current Vector3
  1445. */
  1446. public toArray(array: FloatArray, index: number = 0): Vector3 {
  1447. array[index] = this.x;
  1448. array[index + 1] = this.y;
  1449. array[index + 2] = this.z;
  1450. return this;
  1451. }
  1452. /**
  1453. * Converts the current Vector3 into a quaternion (considering that the Vector3 contains Euler angles representation of a rotation)
  1454. * @returns a new Quaternion object, computed from the Vector3 coordinates
  1455. */
  1456. public toQuaternion(): Quaternion {
  1457. return BABYLON.Quaternion.RotationYawPitchRoll(this.y, this.x, this.z);
  1458. }
  1459. /**
  1460. * Adds the given vector to the current Vector3
  1461. * @param otherVector defines the second operand
  1462. * @returns the current updated Vector3
  1463. */
  1464. public addInPlace(otherVector: Vector3): Vector3 {
  1465. return this.addInPlaceFromFloats(otherVector.x, otherVector.y, otherVector.z);
  1466. }
  1467. /**
  1468. * Adds the given coordinates to the current Vector3
  1469. * @param x defines the x coordinate of the operand
  1470. * @param y defines the y coordinate of the operand
  1471. * @param z defines the z coordinate of the operand
  1472. * @returns the current updated Vector3
  1473. */
  1474. public addInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  1475. this.x += x;
  1476. this.y += y;
  1477. this.z += z;
  1478. return this;
  1479. }
  1480. /**
  1481. * Gets a new Vector3, result of the addition the current Vector3 and the given vector
  1482. * @param otherVector defines the second operand
  1483. * @returns the resulting Vector3
  1484. */
  1485. public add(otherVector: Vector3): Vector3 {
  1486. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  1487. }
  1488. /**
  1489. * Adds the current Vector3 to the given one and stores the result in the vector "result"
  1490. * @param otherVector defines the second operand
  1491. * @param result defines the Vector3 object where to store the result
  1492. * @returns the current Vector3
  1493. */
  1494. public addToRef(otherVector: Vector3, result: Vector3): Vector3 {
  1495. return result.copyFromFloats(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  1496. }
  1497. /**
  1498. * Subtract the given vector from the current Vector3
  1499. * @param otherVector defines the second operand
  1500. * @returns the current updated Vector3
  1501. */
  1502. public subtractInPlace(otherVector: Vector3): Vector3 {
  1503. this.x -= otherVector.x;
  1504. this.y -= otherVector.y;
  1505. this.z -= otherVector.z;
  1506. return this;
  1507. }
  1508. /**
  1509. * Returns a new Vector3, result of the subtraction of the given vector from the current Vector3
  1510. * @param otherVector defines the second operand
  1511. * @returns the resulting Vector3
  1512. */
  1513. public subtract(otherVector: Vector3): Vector3 {
  1514. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  1515. }
  1516. /**
  1517. * Subtracts the given vector from the current Vector3 and stores the result in the vector "result".
  1518. * @param otherVector defines the second operand
  1519. * @param result defines the Vector3 object where to store the result
  1520. * @returns the current Vector3
  1521. */
  1522. public subtractToRef(otherVector: Vector3, result: Vector3): Vector3 {
  1523. return this.subtractFromFloatsToRef(otherVector.x, otherVector.y, otherVector.z, result);
  1524. }
  1525. /**
  1526. * Returns a new Vector3 set with the subtraction of the given floats from the current Vector3 coordinates
  1527. * @param x defines the x coordinate of the operand
  1528. * @param y defines the y coordinate of the operand
  1529. * @param z defines the z coordinate of the operand
  1530. * @returns the resulting Vector3
  1531. */
  1532. public subtractFromFloats(x: number, y: number, z: number): Vector3 {
  1533. return new Vector3(this.x - x, this.y - y, this.z - z);
  1534. }
  1535. /**
  1536. * Subtracts the given floats from the current Vector3 coordinates and set the given vector "result" with this result
  1537. * @param x defines the x coordinate of the operand
  1538. * @param y defines the y coordinate of the operand
  1539. * @param z defines the z coordinate of the operand
  1540. * @param result defines the Vector3 object where to store the result
  1541. * @returns the current Vector3
  1542. */
  1543. public subtractFromFloatsToRef(x: number, y: number, z: number, result: Vector3): Vector3 {
  1544. return result.copyFromFloats(this.x - x, this.y - y, this.z - z);
  1545. }
  1546. /**
  1547. * Gets a new Vector3 set with the current Vector3 negated coordinates
  1548. * @returns a new Vector3
  1549. */
  1550. public negate(): Vector3 {
  1551. return new Vector3(-this.x, -this.y, -this.z);
  1552. }
  1553. /**
  1554. * Multiplies the Vector3 coordinates by the float "scale"
  1555. * @param scale defines the multiplier factor
  1556. * @returns the current updated Vector3
  1557. */
  1558. public scaleInPlace(scale: number): Vector3 {
  1559. this.x *= scale;
  1560. this.y *= scale;
  1561. this.z *= scale;
  1562. return this;
  1563. }
  1564. /**
  1565. * Returns a new Vector3 set with the current Vector3 coordinates multiplied by the float "scale"
  1566. * @param scale defines the multiplier factor
  1567. * @returns a new Vector3
  1568. */
  1569. public scale(scale: number): Vector3 {
  1570. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  1571. }
  1572. /**
  1573. * Multiplies the current Vector3 coordinates by the float "scale" and stores the result in the given vector "result" coordinates
  1574. * @param scale defines the multiplier factor
  1575. * @param result defines the Vector3 object where to store the result
  1576. * @returns the current Vector3
  1577. */
  1578. public scaleToRef(scale: number, result: Vector3): Vector3 {
  1579. return result.copyFromFloats(this.x * scale, this.y * scale, this.z * scale);
  1580. }
  1581. /**
  1582. * Scale the current Vector3 values by a factor and add the result to a given Vector3
  1583. * @param scale defines the scale factor
  1584. * @param result defines the Vector3 object where to store the result
  1585. * @returns the unmodified current Vector3
  1586. */
  1587. public scaleAndAddToRef(scale: number, result: Vector3): Vector3 {
  1588. return result.addInPlaceFromFloats(this.x * scale, this.y * scale, this.z * scale);
  1589. }
  1590. /**
  1591. * Returns true if the current Vector3 and the given vector coordinates are strictly equal
  1592. * @param otherVector defines the second operand
  1593. * @returns true if both vectors are equals
  1594. */
  1595. public equals(otherVector: Vector3): boolean {
  1596. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  1597. }
  1598. /**
  1599. * Returns true if the current Vector3 and the given vector coordinates are distant less than epsilon
  1600. * @param otherVector defines the second operand
  1601. * @param epsilon defines the minimal distance to define values as equals
  1602. * @returns true if both vectors are distant less than epsilon
  1603. */
  1604. public equalsWithEpsilon(otherVector: Vector3, epsilon: number = Epsilon): boolean {
  1605. return otherVector && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon) && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon) && Scalar.WithinEpsilon(this.z, otherVector.z, epsilon);
  1606. }
  1607. /**
  1608. * Returns true if the current Vector3 coordinates equals the given floats
  1609. * @param x defines the x coordinate of the operand
  1610. * @param y defines the y coordinate of the operand
  1611. * @param z defines the z coordinate of the operand
  1612. * @returns true if both vectors are equals
  1613. */
  1614. public equalsToFloats(x: number, y: number, z: number): boolean {
  1615. return this.x === x && this.y === y && this.z === z;
  1616. }
  1617. /**
  1618. * Multiplies the current Vector3 coordinates by the given ones
  1619. * @param otherVector defines the second operand
  1620. * @returns the current updated Vector3
  1621. */
  1622. public multiplyInPlace(otherVector: Vector3): Vector3 {
  1623. this.x *= otherVector.x;
  1624. this.y *= otherVector.y;
  1625. this.z *= otherVector.z;
  1626. return this;
  1627. }
  1628. /**
  1629. * Returns a new Vector3, result of the multiplication of the current Vector3 by the given vector
  1630. * @param otherVector defines the second operand
  1631. * @returns the new Vector3
  1632. */
  1633. public multiply(otherVector: Vector3): Vector3 {
  1634. return this.multiplyByFloats(otherVector.x, otherVector.y, otherVector.z);
  1635. }
  1636. /**
  1637. * Multiplies the current Vector3 by the given one and stores the result in the given vector "result"
  1638. * @param otherVector defines the second operand
  1639. * @param result defines the Vector3 object where to store the result
  1640. * @returns the current Vector3
  1641. */
  1642. public multiplyToRef(otherVector: Vector3, result: Vector3): Vector3 {
  1643. return result.copyFromFloats(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  1644. }
  1645. /**
  1646. * Returns a new Vector3 set with the result of the mulliplication of the current Vector3 coordinates by the given floats
  1647. * @param x defines the x coordinate of the operand
  1648. * @param y defines the y coordinate of the operand
  1649. * @param z defines the z coordinate of the operand
  1650. * @returns the new Vector3
  1651. */
  1652. public multiplyByFloats(x: number, y: number, z: number): Vector3 {
  1653. return new Vector3(this.x * x, this.y * y, this.z * z);
  1654. }
  1655. /**
  1656. * Returns a new Vector3 set with the result of the division of the current Vector3 coordinates by the given ones
  1657. * @param otherVector defines the second operand
  1658. * @returns the new Vector3
  1659. */
  1660. public divide(otherVector: Vector3): Vector3 {
  1661. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  1662. }
  1663. /**
  1664. * Divides the current Vector3 coordinates by the given ones and stores the result in the given vector "result"
  1665. * @param otherVector defines the second operand
  1666. * @param result defines the Vector3 object where to store the result
  1667. * @returns the current Vector3
  1668. */
  1669. public divideToRef(otherVector: Vector3, result: Vector3): Vector3 {
  1670. return result.copyFromFloats(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  1671. }
  1672. /**
  1673. * Divides the current Vector3 coordinates by the given ones.
  1674. * @param otherVector defines the second operand
  1675. * @returns the current updated Vector3
  1676. */
  1677. public divideInPlace(otherVector: Vector3): Vector3 {
  1678. return this.divideToRef(otherVector, this);
  1679. }
  1680. /**
  1681. * Updates the current Vector3 with the minimal coordinate values between its and the given vector ones
  1682. * @param other defines the second operand
  1683. * @returns the current updated Vector3
  1684. */
  1685. public minimizeInPlace(other: Vector3): Vector3 {
  1686. return this.minimizeInPlaceFromFloats(other.x, other.y, other.z);
  1687. }
  1688. /**
  1689. * Updates the current Vector3 with the maximal coordinate values between its and the given vector ones.
  1690. * @param other defines the second operand
  1691. * @returns the current updated Vector3
  1692. */
  1693. public maximizeInPlace(other: Vector3): Vector3 {
  1694. return this.maximizeInPlaceFromFloats(other.x, other.y, other.z);
  1695. }
  1696. /**
  1697. * Updates the current Vector3 with the minimal coordinate values between its and the given coordinates
  1698. * @param x defines the x coordinate of the operand
  1699. * @param y defines the y coordinate of the operand
  1700. * @param z defines the z coordinate of the operand
  1701. * @returns the current updated Vector3
  1702. */
  1703. public minimizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  1704. if (x < this.x) { this.x = x; }
  1705. if (y < this.y) { this.y = y; }
  1706. if (z < this.z) { this.z = z; }
  1707. return this;
  1708. }
  1709. /**
  1710. * Updates the current Vector3 with the maximal coordinate values between its and the given coordinates.
  1711. * @param x defines the x coordinate of the operand
  1712. * @param y defines the y coordinate of the operand
  1713. * @param z defines the z coordinate of the operand
  1714. * @returns the current updated Vector3
  1715. */
  1716. public maximizeInPlaceFromFloats(x: number, y: number, z: number): Vector3 {
  1717. if (x > this.x) { this.x = x; }
  1718. if (y > this.y) { this.y = y; }
  1719. if (z > this.z) { this.z = z; }
  1720. return this;
  1721. }
  1722. /**
  1723. * Gets a boolean indicating that the vector is non uniform meaning x, y or z are not all the same
  1724. */
  1725. public get isNonUniform(): boolean {
  1726. let absX = Math.abs(this.x);
  1727. let absY = Math.abs(this.y);
  1728. if (absX !== absY) {
  1729. return true;
  1730. }
  1731. let absZ = Math.abs(this.z);
  1732. if (absX !== absZ) {
  1733. return true;
  1734. }
  1735. if (absY !== absZ) {
  1736. return true;
  1737. }
  1738. return false;
  1739. }
  1740. /**
  1741. * Gets a new Vector3 from current Vector3 floored values
  1742. * @returns a new Vector3
  1743. */
  1744. public floor(): Vector3 {
  1745. return new Vector3(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z));
  1746. }
  1747. /**
  1748. * Gets a new Vector3 from current Vector3 floored values
  1749. * @returns a new Vector3
  1750. */
  1751. public fract(): Vector3 {
  1752. return new Vector3(this.x - Math.floor(this.x), this.y - Math.floor(this.y), this.z - Math.floor(this.z));
  1753. }
  1754. // Properties
  1755. /**
  1756. * Gets the length of the Vector3
  1757. * @returns the length of the Vecto3
  1758. */
  1759. public length(): number {
  1760. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  1761. }
  1762. /**
  1763. * Gets the squared length of the Vector3
  1764. * @returns squared length of the Vector3
  1765. */
  1766. public lengthSquared(): number {
  1767. return (this.x * this.x + this.y * this.y + this.z * this.z);
  1768. }
  1769. /**
  1770. * Normalize the current Vector3.
  1771. * Please note that this is an in place operation.
  1772. * @returns the current updated Vector3
  1773. */
  1774. public normalize(): Vector3 {
  1775. return this.normalizeFromLength(this.length());
  1776. }
  1777. /**
  1778. * Normalize the current Vector3 with the given input length.
  1779. * Please note that this is an in place operation.
  1780. * @param len the length of the vector
  1781. * @returns the current updated Vector3
  1782. */
  1783. public normalizeFromLength(len: number): Vector3 {
  1784. if (len === 0 || len === 1.0) {
  1785. return this;
  1786. }
  1787. return this.scaleInPlace(1.0 / len);
  1788. }
  1789. /**
  1790. * Normalize the current Vector3 to a new vector
  1791. * @returns the new Vector3
  1792. */
  1793. public normalizeToNew(): Vector3 {
  1794. const normalized = new Vector3(0, 0, 0);
  1795. this.normalizeToRef(normalized);
  1796. return normalized;
  1797. }
  1798. /**
  1799. * Normalize the current Vector3 to the reference
  1800. * @param reference define the Vector3 to update
  1801. * @returns the updated Vector3
  1802. */
  1803. public normalizeToRef(reference: Vector3): Vector3 {
  1804. var len = this.length();
  1805. if (len === 0 || len === 1.0) {
  1806. return reference.copyFromFloats(this.x, this.y, this.z);
  1807. }
  1808. return this.scaleToRef(1.0 / len, reference);
  1809. }
  1810. /**
  1811. * Creates a new Vector3 copied from the current Vector3
  1812. * @returns the new Vector3
  1813. */
  1814. public clone(): Vector3 {
  1815. return new Vector3(this.x, this.y, this.z);
  1816. }
  1817. /**
  1818. * Copies the given vector coordinates to the current Vector3 ones
  1819. * @param source defines the source Vector3
  1820. * @returns the current updated Vector3
  1821. */
  1822. public copyFrom(source: Vector3): Vector3 {
  1823. return this.copyFromFloats(source.x, source.y, source.z);
  1824. }
  1825. /**
  1826. * Copies the given floats to the current Vector3 coordinates
  1827. * @param x defines the x coordinate of the operand
  1828. * @param y defines the y coordinate of the operand
  1829. * @param z defines the z coordinate of the operand
  1830. * @returns the current updated Vector3
  1831. */
  1832. public copyFromFloats(x: number, y: number, z: number): Vector3 {
  1833. this.x = x;
  1834. this.y = y;
  1835. this.z = z;
  1836. return this;
  1837. }
  1838. /**
  1839. * Copies the given floats to the current Vector3 coordinates
  1840. * @param x defines the x coordinate of the operand
  1841. * @param y defines the y coordinate of the operand
  1842. * @param z defines the z coordinate of the operand
  1843. * @returns the current updated Vector3
  1844. */
  1845. public set(x: number, y: number, z: number): Vector3 {
  1846. return this.copyFromFloats(x, y, z);
  1847. }
  1848. /**
  1849. * Copies the given float to the current Vector3 coordinates
  1850. * @param v defines the x, y and z coordinates of the operand
  1851. * @returns the current updated Vector3
  1852. */
  1853. public setAll(v: number): Vector3 {
  1854. this.x = this.y = this.z = v;
  1855. return this;
  1856. }
  1857. // Statics
  1858. /**
  1859. * Get the clip factor between two vectors
  1860. * @param vector0 defines the first operand
  1861. * @param vector1 defines the second operand
  1862. * @param axis defines the axis to use
  1863. * @param size defines the size along the axis
  1864. * @returns the clip factor
  1865. */
  1866. public static GetClipFactor(vector0: Vector3, vector1: Vector3, axis: Vector3, size: number) {
  1867. var d0 = Vector3.Dot(vector0, axis) - size;
  1868. var d1 = Vector3.Dot(vector1, axis) - size;
  1869. var s = d0 / (d0 - d1);
  1870. return s;
  1871. }
  1872. /**
  1873. * Get angle between two vectors
  1874. * @param vector0 angle between vector0 and vector1
  1875. * @param vector1 angle between vector0 and vector1
  1876. * @param normal direction of the normal
  1877. * @return the angle between vector0 and vector1
  1878. */
  1879. public static GetAngleBetweenVectors(vector0: Vector3, vector1: Vector3, normal: Vector3): number {
  1880. const v0: Vector3 = vector0.normalizeToRef(MathTmp.Vector3[1]);
  1881. const v1: Vector3 = vector1.normalizeToRef(MathTmp.Vector3[2]);
  1882. const dot: number = Vector3.Dot(v0, v1);
  1883. const n = MathTmp.Vector3[3];
  1884. Vector3.CrossToRef(v0, v1, n);
  1885. if (Vector3.Dot(n, normal) > 0) {
  1886. return Math.acos(dot);
  1887. }
  1888. return -Math.acos(dot);
  1889. }
  1890. /**
  1891. * Returns a new Vector3 set from the index "offset" of the given array
  1892. * @param array defines the source array
  1893. * @param offset defines the offset in the source array
  1894. * @returns the new Vector3
  1895. */
  1896. public static FromArray(array: ArrayLike<number>, offset: number = 0): Vector3 {
  1897. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  1898. }
  1899. /**
  1900. * Returns a new Vector3 set from the index "offset" of the given Float32Array
  1901. * This function is deprecated. Use FromArray instead
  1902. * @param array defines the source array
  1903. * @param offset defines the offset in the source array
  1904. * @returns the new Vector3
  1905. */
  1906. public static FromFloatArray(array: Float32Array, offset?: number): Vector3 {
  1907. return Vector3.FromArray(array, offset);
  1908. }
  1909. /**
  1910. * Sets the given vector "result" with the element values from the index "offset" of the given array
  1911. * @param array defines the source array
  1912. * @param offset defines the offset in the source array
  1913. * @param result defines the Vector3 where to store the result
  1914. */
  1915. public static FromArrayToRef(array: ArrayLike<number>, offset: number, result: Vector3): void {
  1916. result.x = array[offset];
  1917. result.y = array[offset + 1];
  1918. result.z = array[offset + 2];
  1919. }
  1920. /**
  1921. * Sets the given vector "result" with the element values from the index "offset" of the given Float32Array
  1922. * This function is deprecated. Use FromArrayToRef instead.
  1923. * @param array defines the source array
  1924. * @param offset defines the offset in the source array
  1925. * @param result defines the Vector3 where to store the result
  1926. */
  1927. public static FromFloatArrayToRef(array: Float32Array, offset: number, result: Vector3): void {
  1928. return Vector3.FromArrayToRef(array, offset, result);
  1929. }
  1930. /**
  1931. * Sets the given vector "result" with the given floats.
  1932. * @param x defines the x coordinate of the source
  1933. * @param y defines the y coordinate of the source
  1934. * @param z defines the z coordinate of the source
  1935. * @param result defines the Vector3 where to store the result
  1936. */
  1937. public static FromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
  1938. result.copyFromFloats(x, y, z);
  1939. }
  1940. /**
  1941. * Returns a new Vector3 set to (0.0, 0.0, 0.0)
  1942. * @returns a new empty Vector3
  1943. */
  1944. public static Zero(): Vector3 {
  1945. return new Vector3(0.0, 0.0, 0.0);
  1946. }
  1947. /**
  1948. * Returns a new Vector3 set to (1.0, 1.0, 1.0)
  1949. * @returns a new unit Vector3
  1950. */
  1951. public static One(): Vector3 {
  1952. return new Vector3(1.0, 1.0, 1.0);
  1953. }
  1954. /**
  1955. * Returns a new Vector3 set to (0.0, 1.0, 0.0)
  1956. * @returns a new up Vector3
  1957. */
  1958. public static Up(): Vector3 {
  1959. return new Vector3(0.0, 1.0, 0.0);
  1960. }
  1961. /**
  1962. * Returns a new Vector3 set to (0.0, -1.0, 0.0)
  1963. * @returns a new down Vector3
  1964. */
  1965. public static Down(): Vector3 {
  1966. return new Vector3(0.0, -1.0, 0.0);
  1967. }
  1968. /**
  1969. * Returns a new Vector3 set to (0.0, 0.0, 1.0)
  1970. * @returns a new forward Vector3
  1971. */
  1972. public static Forward(): Vector3 {
  1973. return new Vector3(0.0, 0.0, 1.0);
  1974. }
  1975. /**
  1976. * Returns a new Vector3 set to (0.0, 0.0, -1.0)
  1977. * @returns a new forward Vector3
  1978. */
  1979. public static Backward(): Vector3 {
  1980. return new Vector3(0.0, 0.0, -1.0);
  1981. }
  1982. /**
  1983. * Returns a new Vector3 set to (1.0, 0.0, 0.0)
  1984. * @returns a new right Vector3
  1985. */
  1986. public static Right(): Vector3 {
  1987. return new Vector3(1.0, 0.0, 0.0);
  1988. }
  1989. /**
  1990. * Returns a new Vector3 set to (-1.0, 0.0, 0.0)
  1991. * @returns a new left Vector3
  1992. */
  1993. public static Left(): Vector3 {
  1994. return new Vector3(-1.0, 0.0, 0.0);
  1995. }
  1996. /**
  1997. * Returns a new Vector3 set with the result of the transformation by the given matrix of the given vector.
  1998. * This method computes tranformed coordinates only, not transformed direction vectors (ie. it takes translation in account)
  1999. * @param vector defines the Vector3 to transform
  2000. * @param transformation defines the transformation matrix
  2001. * @returns the transformed Vector3
  2002. */
  2003. public static TransformCoordinates(vector: Vector3, transformation: Matrix): Vector3 {
  2004. var result = Vector3.Zero();
  2005. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  2006. return result;
  2007. }
  2008. /**
  2009. * Sets the given vector "result" coordinates with the result of the transformation by the given matrix of the given vector
  2010. * This method computes tranformed coordinates only, not transformed direction vectors (ie. it takes translation in account)
  2011. * @param vector defines the Vector3 to transform
  2012. * @param transformation defines the transformation matrix
  2013. * @param result defines the Vector3 where to store the result
  2014. */
  2015. public static TransformCoordinatesToRef(vector: Vector3, transformation: Readonly<Matrix>, result: Vector3): void {
  2016. return Vector3.TransformCoordinatesFromFloatsToRef(vector.x, vector.y, vector.z, transformation, result);
  2017. }
  2018. /**
  2019. * Sets the given vector "result" coordinates with the result of the transformation by the given matrix of the given floats (x, y, z)
  2020. * This method computes tranformed coordinates only, not transformed direction vectors
  2021. * @param x define the x coordinate of the source vector
  2022. * @param y define the y coordinate of the source vector
  2023. * @param z define the z coordinate of the source vector
  2024. * @param transformation defines the transformation matrix
  2025. * @param result defines the Vector3 where to store the result
  2026. */
  2027. public static TransformCoordinatesFromFloatsToRef(x: number, y: number, z: number, transformation: Readonly<Matrix>, result: Vector3): void {
  2028. const m = transformation.m;
  2029. var rx = x * m[0] + y * m[4] + z * m[8] + m[12];
  2030. var ry = x * m[1] + y * m[5] + z * m[9] + m[13];
  2031. var rz = x * m[2] + y * m[6] + z * m[10] + m[14];
  2032. var rw = 1 / (x * m[3] + y * m[7] + z * m[11] + m[15]);
  2033. result.x = rx * rw;
  2034. result.y = ry * rw;
  2035. result.z = rz * rw;
  2036. }
  2037. /**
  2038. * Returns a new Vector3 set with the result of the normal transformation by the given matrix of the given vector
  2039. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  2040. * @param vector defines the Vector3 to transform
  2041. * @param transformation defines the transformation matrix
  2042. * @returns the new Vector3
  2043. */
  2044. public static TransformNormal(vector: Vector3, transformation: Matrix): Vector3 {
  2045. var result = Vector3.Zero();
  2046. Vector3.TransformNormalToRef(vector, transformation, result);
  2047. return result;
  2048. }
  2049. /**
  2050. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given vector
  2051. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  2052. * @param vector defines the Vector3 to transform
  2053. * @param transformation defines the transformation matrix
  2054. * @param result defines the Vector3 where to store the result
  2055. */
  2056. public static TransformNormalToRef(vector: Vector3, transformation: Readonly<Matrix>, result: Vector3): void {
  2057. this.TransformNormalFromFloatsToRef(vector.x, vector.y, vector.z, transformation, result);
  2058. }
  2059. /**
  2060. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given floats (x, y, z)
  2061. * This methods computes transformed normalized direction vectors only (ie. it does not apply translation)
  2062. * @param x define the x coordinate of the source vector
  2063. * @param y define the y coordinate of the source vector
  2064. * @param z define the z coordinate of the source vector
  2065. * @param transformation defines the transformation matrix
  2066. * @param result defines the Vector3 where to store the result
  2067. */
  2068. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, transformation: Readonly<Matrix>, result: Vector3): void {
  2069. const m = transformation.m;
  2070. result.x = x * m[0] + y * m[4] + z * m[8];
  2071. result.y = x * m[1] + y * m[5] + z * m[9];
  2072. result.z = x * m[2] + y * m[6] + z * m[10];
  2073. }
  2074. /**
  2075. * Returns a new Vector3 located for "amount" on the CatmullRom interpolation spline defined by the vectors "value1", "value2", "value3", "value4"
  2076. * @param value1 defines the first control point
  2077. * @param value2 defines the second control point
  2078. * @param value3 defines the third control point
  2079. * @param value4 defines the fourth control point
  2080. * @param amount defines the amount on the spline to use
  2081. * @returns the new Vector3
  2082. */
  2083. public static CatmullRom(value1: Vector3, value2: Vector3, value3: Vector3, value4: Vector3, amount: number): Vector3 {
  2084. var squared = amount * amount;
  2085. var cubed = amount * squared;
  2086. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  2087. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  2088. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  2089. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  2090. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  2091. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  2092. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  2093. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  2094. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  2095. return new Vector3(x, y, z);
  2096. }
  2097. /**
  2098. * 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"
  2099. * If a coordinate value of "value" is lower than one of the "min" coordinate, then this "value" coordinate is set with the "min" one
  2100. * If a coordinate value of "value" is greater than one of the "max" coordinate, then this "value" coordinate is set with the "max" one
  2101. * @param value defines the current value
  2102. * @param min defines the lower range value
  2103. * @param max defines the upper range value
  2104. * @returns the new Vector3
  2105. */
  2106. public static Clamp(value: Vector3, min: Vector3, max: Vector3): Vector3 {
  2107. const v = new Vector3();
  2108. Vector3.ClampToRef(value, min, max, v);
  2109. return v;
  2110. }
  2111. /**
  2112. * 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"
  2113. * If a coordinate value of "value" is lower than one of the "min" coordinate, then this "value" coordinate is set with the "min" one
  2114. * If a coordinate value of "value" is greater than one of the "max" coordinate, then this "value" coordinate is set with the "max" one
  2115. * @param value defines the current value
  2116. * @param min defines the lower range value
  2117. * @param max defines the upper range value
  2118. * @param result defines the Vector3 where to store the result
  2119. */
  2120. public static ClampToRef(value: Vector3, min: Vector3, max: Vector3, result: Vector3): void {
  2121. var x = value.x;
  2122. x = (x > max.x) ? max.x : x;
  2123. x = (x < min.x) ? min.x : x;
  2124. var y = value.y;
  2125. y = (y > max.y) ? max.y : y;
  2126. y = (y < min.y) ? min.y : y;
  2127. var z = value.z;
  2128. z = (z > max.z) ? max.z : z;
  2129. z = (z < min.z) ? min.z : z;
  2130. result.copyFromFloats(x, y, z);
  2131. }
  2132. /**
  2133. * Returns a new Vector3 located for "amount" (float) on the Hermite interpolation spline defined by the vectors "value1", "tangent1", "value2", "tangent2"
  2134. * @param value1 defines the first control point
  2135. * @param tangent1 defines the first tangent vector
  2136. * @param value2 defines the second control point
  2137. * @param tangent2 defines the second tangent vector
  2138. * @param amount defines the amount on the interpolation spline (between 0 and 1)
  2139. * @returns the new Vector3
  2140. */
  2141. public static Hermite(value1: Vector3, tangent1: Vector3, value2: Vector3, tangent2: Vector3, amount: number): Vector3 {
  2142. var squared = amount * amount;
  2143. var cubed = amount * squared;
  2144. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  2145. var part2 = (-2.0 * cubed) + (3.0 * squared);
  2146. var part3 = (cubed - (2.0 * squared)) + amount;
  2147. var part4 = cubed - squared;
  2148. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  2149. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  2150. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  2151. return new Vector3(x, y, z);
  2152. }
  2153. /**
  2154. * Returns a new Vector3 located for "amount" (float) on the linear interpolation between the vectors "start" and "end"
  2155. * @param start defines the start value
  2156. * @param end defines the end value
  2157. * @param amount max defines amount between both (between 0 and 1)
  2158. * @returns the new Vector3
  2159. */
  2160. public static Lerp(start: Vector3, end: Vector3, amount: number): Vector3 {
  2161. var result = new Vector3(0, 0, 0);
  2162. Vector3.LerpToRef(start, end, amount, result);
  2163. return result;
  2164. }
  2165. /**
  2166. * Sets the given vector "result" with the result of the linear interpolation from the vector "start" for "amount" to the vector "end"
  2167. * @param start defines the start value
  2168. * @param end defines the end value
  2169. * @param amount max defines amount between both (between 0 and 1)
  2170. * @param result defines the Vector3 where to store the result
  2171. */
  2172. public static LerpToRef(start: Vector3, end: Vector3, amount: number, result: Vector3): void {
  2173. result.x = start.x + ((end.x - start.x) * amount);
  2174. result.y = start.y + ((end.y - start.y) * amount);
  2175. result.z = start.z + ((end.z - start.z) * amount);
  2176. }
  2177. /**
  2178. * Returns the dot product (float) between the vectors "left" and "right"
  2179. * @param left defines the left operand
  2180. * @param right defines the right operand
  2181. * @returns the dot product
  2182. */
  2183. public static Dot(left: Vector3, right: Vector3): number {
  2184. return (left.x * right.x + left.y * right.y + left.z * right.z);
  2185. }
  2186. /**
  2187. * Returns a new Vector3 as the cross product of the vectors "left" and "right"
  2188. * The cross product is then orthogonal to both "left" and "right"
  2189. * @param left defines the left operand
  2190. * @param right defines the right operand
  2191. * @returns the cross product
  2192. */
  2193. public static Cross(left: Vector3, right: Vector3): Vector3 {
  2194. var result = Vector3.Zero();
  2195. Vector3.CrossToRef(left, right, result);
  2196. return result;
  2197. }
  2198. /**
  2199. * Sets the given vector "result" with the cross product of "left" and "right"
  2200. * The cross product is then orthogonal to both "left" and "right"
  2201. * @param left defines the left operand
  2202. * @param right defines the right operand
  2203. * @param result defines the Vector3 where to store the result
  2204. */
  2205. public static CrossToRef(left: Vector3, right: Vector3, result: Vector3): void {
  2206. const x = left.y * right.z - left.z * right.y;
  2207. const y = left.z * right.x - left.x * right.z;
  2208. const z = left.x * right.y - left.y * right.x;
  2209. result.copyFromFloats(x, y, z);
  2210. }
  2211. /**
  2212. * Returns a new Vector3 as the normalization of the given vector
  2213. * @param vector defines the Vector3 to normalize
  2214. * @returns the new Vector3
  2215. */
  2216. public static Normalize(vector: Vector3): Vector3 {
  2217. var result = Vector3.Zero();
  2218. Vector3.NormalizeToRef(vector, result);
  2219. return result;
  2220. }
  2221. /**
  2222. * Sets the given vector "result" with the normalization of the given first vector
  2223. * @param vector defines the Vector3 to normalize
  2224. * @param result defines the Vector3 where to store the result
  2225. */
  2226. public static NormalizeToRef(vector: Vector3, result: Vector3): void {
  2227. vector.normalizeToRef(result);
  2228. }
  2229. /**
  2230. * Project a Vector3 onto screen space
  2231. * @param vector defines the Vector3 to project
  2232. * @param world defines the world matrix to use
  2233. * @param transform defines the transform (view x projection) matrix to use
  2234. * @param viewport defines the screen viewport to use
  2235. * @returns the new Vector3
  2236. */
  2237. public static Project(vector: Vector3, world: Matrix, transform: Matrix, viewport: Viewport): Vector3 {
  2238. var cw = viewport.width;
  2239. var ch = viewport.height;
  2240. var cx = viewport.x;
  2241. var cy = viewport.y;
  2242. var viewportMatrix = MathTmp.Matrix[1];
  2243. Matrix.FromValuesToRef(
  2244. cw / 2.0, 0, 0, 0,
  2245. 0, -ch / 2.0, 0, 0,
  2246. 0, 0, 0.5, 0,
  2247. cx + cw / 2.0, ch / 2.0 + cy, 0.5, 1, viewportMatrix);
  2248. var matrix = MathTmp.Matrix[0];
  2249. world.multiplyToRef(transform, matrix);
  2250. matrix.multiplyToRef(viewportMatrix, matrix);
  2251. return Vector3.TransformCoordinates(vector, matrix);
  2252. }
  2253. /**
  2254. * Unproject from screen space to object space
  2255. * @param source defines the screen space Vector3 to use
  2256. * @param viewportWidth defines the current width of the viewport
  2257. * @param viewportHeight defines the current height of the viewport
  2258. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  2259. * @param transform defines the transform (view x projection) matrix to use
  2260. * @returns the new Vector3
  2261. */
  2262. public static UnprojectFromTransform(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, transform: Matrix): Vector3 {
  2263. var matrix = MathTmp.Matrix[0];
  2264. world.multiplyToRef(transform, matrix);
  2265. matrix.invert();
  2266. source.x = source.x / viewportWidth * 2 - 1;
  2267. source.y = -(source.y / viewportHeight * 2 - 1);
  2268. var vector = Vector3.TransformCoordinates(source, matrix);
  2269. const m = matrix.m;
  2270. var num = source.x * m[3] + source.y * m[7] + source.z * m[11] + m[15];
  2271. if (Scalar.WithinEpsilon(num, 1.0)) {
  2272. vector = vector.scale(1.0 / num);
  2273. }
  2274. return vector;
  2275. }
  2276. /**
  2277. * Unproject from screen space to object space
  2278. * @param source defines the screen space Vector3 to use
  2279. * @param viewportWidth defines the current width of the viewport
  2280. * @param viewportHeight defines the current height of the viewport
  2281. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  2282. * @param view defines the view matrix to use
  2283. * @param projection defines the projection matrix to use
  2284. * @returns the new Vector3
  2285. */
  2286. public static Unproject(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Vector3 {
  2287. let result = Vector3.Zero();
  2288. Vector3.UnprojectToRef(source, viewportWidth, viewportHeight, world, view, projection, result);
  2289. return result;
  2290. }
  2291. /**
  2292. * Unproject from screen space to object space
  2293. * @param source defines the screen space Vector3 to use
  2294. * @param viewportWidth defines the current width of the viewport
  2295. * @param viewportHeight defines the current height of the viewport
  2296. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  2297. * @param view defines the view matrix to use
  2298. * @param projection defines the projection matrix to use
  2299. * @param result defines the Vector3 where to store the result
  2300. */
  2301. public static UnprojectToRef(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix, result: Vector3): void {
  2302. Vector3.UnprojectFloatsToRef(source.x, source.y, source.z, viewportWidth, viewportHeight, world, view, projection, result);
  2303. }
  2304. /**
  2305. * Unproject from screen space to object space
  2306. * @param sourceX defines the screen space x coordinate to use
  2307. * @param sourceY defines the screen space y coordinate to use
  2308. * @param sourceZ defines the screen space z coordinate to use
  2309. * @param viewportWidth defines the current width of the viewport
  2310. * @param viewportHeight defines the current height of the viewport
  2311. * @param world defines the world matrix to use (can be set to Identity to go to world space)
  2312. * @param view defines the view matrix to use
  2313. * @param projection defines the projection matrix to use
  2314. * @param result defines the Vector3 where to store the result
  2315. */
  2316. public static UnprojectFloatsToRef(sourceX: float, sourceY: float, sourceZ: float, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix, result: Vector3): void {
  2317. var matrix = MathTmp.Matrix[0];
  2318. world.multiplyToRef(view, matrix);
  2319. matrix.multiplyToRef(projection, matrix);
  2320. matrix.invert();
  2321. var screenSource = MathTmp.Vector3[0];
  2322. screenSource.x = sourceX / viewportWidth * 2 - 1;
  2323. screenSource.y = -(sourceY / viewportHeight * 2 - 1);
  2324. screenSource.z = 2 * sourceZ - 1.0;
  2325. Vector3.TransformCoordinatesToRef(screenSource, matrix, result);
  2326. const m = matrix.m;
  2327. var num = screenSource.x * m[3] + screenSource.y * m[7] + screenSource.z * m[11] + m[15];
  2328. if (Scalar.WithinEpsilon(num, 1.0)) {
  2329. result.scaleInPlace(1.0 / num);
  2330. }
  2331. }
  2332. /**
  2333. * Gets the minimal coordinate values between two Vector3
  2334. * @param left defines the first operand
  2335. * @param right defines the second operand
  2336. * @returns the new Vector3
  2337. */
  2338. public static Minimize(left: Vector3, right: Vector3): Vector3 {
  2339. var min = left.clone();
  2340. min.minimizeInPlace(right);
  2341. return min;
  2342. }
  2343. /**
  2344. * Gets the maximal coordinate values between two Vector3
  2345. * @param left defines the first operand
  2346. * @param right defines the second operand
  2347. * @returns the new Vector3
  2348. */
  2349. public static Maximize(left: Vector3, right: Vector3): Vector3 {
  2350. var max = left.clone();
  2351. max.maximizeInPlace(right);
  2352. return max;
  2353. }
  2354. /**
  2355. * Returns the distance between the vectors "value1" and "value2"
  2356. * @param value1 defines the first operand
  2357. * @param value2 defines the second operand
  2358. * @returns the distance
  2359. */
  2360. public static Distance(value1: Vector3, value2: Vector3): number {
  2361. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  2362. }
  2363. /**
  2364. * Returns the squared distance between the vectors "value1" and "value2"
  2365. * @param value1 defines the first operand
  2366. * @param value2 defines the second operand
  2367. * @returns the squared distance
  2368. */
  2369. public static DistanceSquared(value1: Vector3, value2: Vector3): number {
  2370. var x = value1.x - value2.x;
  2371. var y = value1.y - value2.y;
  2372. var z = value1.z - value2.z;
  2373. return (x * x) + (y * y) + (z * z);
  2374. }
  2375. /**
  2376. * Returns a new Vector3 located at the center between "value1" and "value2"
  2377. * @param value1 defines the first operand
  2378. * @param value2 defines the second operand
  2379. * @returns the new Vector3
  2380. */
  2381. public static Center(value1: Vector3, value2: Vector3): Vector3 {
  2382. var center = value1.add(value2);
  2383. center.scaleInPlace(0.5);
  2384. return center;
  2385. }
  2386. /**
  2387. * Given three orthogonal normalized left-handed oriented Vector3 axis in space (target system),
  2388. * RotationFromAxis() returns the rotation Euler angles (ex : rotation.x, rotation.y, rotation.z) to apply
  2389. * to something in order to rotate it from its local system to the given target system
  2390. * Note: axis1, axis2 and axis3 are normalized during this operation
  2391. * @param axis1 defines the first axis
  2392. * @param axis2 defines the second axis
  2393. * @param axis3 defines the third axis
  2394. * @returns a new Vector3
  2395. */
  2396. public static RotationFromAxis(axis1: Vector3, axis2: Vector3, axis3: Vector3): Vector3 {
  2397. var rotation = Vector3.Zero();
  2398. Vector3.RotationFromAxisToRef(axis1, axis2, axis3, rotation);
  2399. return rotation;
  2400. }
  2401. /**
  2402. * The same than RotationFromAxis but updates the given ref Vector3 parameter instead of returning a new Vector3
  2403. * @param axis1 defines the first axis
  2404. * @param axis2 defines the second axis
  2405. * @param axis3 defines the third axis
  2406. * @param ref defines the Vector3 where to store the result
  2407. */
  2408. public static RotationFromAxisToRef(axis1: Vector3, axis2: Vector3, axis3: Vector3, ref: Vector3): void {
  2409. var quat = MathTmp.Quaternion[0];
  2410. Quaternion.RotationQuaternionFromAxisToRef(axis1, axis2, axis3, quat);
  2411. quat.toEulerAnglesToRef(ref);
  2412. }
  2413. }
  2414. /**
  2415. * Vector4 class created for EulerAngle class conversion to Quaternion
  2416. */
  2417. export class Vector4 {
  2418. /**
  2419. * Creates a Vector4 object from the given floats.
  2420. * @param x x value of the vector
  2421. * @param y y value of the vector
  2422. * @param z z value of the vector
  2423. * @param w w value of the vector
  2424. */
  2425. constructor(
  2426. /** x value of the vector */
  2427. public x: number,
  2428. /** y value of the vector */
  2429. public y: number,
  2430. /** z value of the vector */
  2431. public z: number,
  2432. /** w value of the vector */
  2433. public w: number
  2434. ) { }
  2435. /**
  2436. * Returns the string with the Vector4 coordinates.
  2437. * @returns a string containing all the vector values
  2438. */
  2439. public toString(): string {
  2440. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  2441. }
  2442. /**
  2443. * Returns the string "Vector4".
  2444. * @returns "Vector4"
  2445. */
  2446. public getClassName(): string {
  2447. return "Vector4";
  2448. }
  2449. /**
  2450. * Returns the Vector4 hash code.
  2451. * @returns a unique hash code
  2452. */
  2453. public getHashCode(): number {
  2454. let hash = this.x || 0;
  2455. hash = (hash * 397) ^ (this.y || 0);
  2456. hash = (hash * 397) ^ (this.z || 0);
  2457. hash = (hash * 397) ^ (this.w || 0);
  2458. return hash;
  2459. }
  2460. // Operators
  2461. /**
  2462. * Returns a new array populated with 4 elements : the Vector4 coordinates.
  2463. * @returns the resulting array
  2464. */
  2465. public asArray(): number[] {
  2466. var result = new Array<number>();
  2467. this.toArray(result, 0);
  2468. return result;
  2469. }
  2470. /**
  2471. * Populates the given array from the given index with the Vector4 coordinates.
  2472. * @param array array to populate
  2473. * @param index index of the array to start at (default: 0)
  2474. * @returns the Vector4.
  2475. */
  2476. public toArray(array: FloatArray, index?: number): Vector4 {
  2477. if (index === undefined) {
  2478. index = 0;
  2479. }
  2480. array[index] = this.x;
  2481. array[index + 1] = this.y;
  2482. array[index + 2] = this.z;
  2483. array[index + 3] = this.w;
  2484. return this;
  2485. }
  2486. /**
  2487. * Adds the given vector to the current Vector4.
  2488. * @param otherVector the vector to add
  2489. * @returns the updated Vector4.
  2490. */
  2491. public addInPlace(otherVector: Vector4): Vector4 {
  2492. this.x += otherVector.x;
  2493. this.y += otherVector.y;
  2494. this.z += otherVector.z;
  2495. this.w += otherVector.w;
  2496. return this;
  2497. }
  2498. /**
  2499. * Returns a new Vector4 as the result of the addition of the current Vector4 and the given one.
  2500. * @param otherVector the vector to add
  2501. * @returns the resulting vector
  2502. */
  2503. public add(otherVector: Vector4): Vector4 {
  2504. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  2505. }
  2506. /**
  2507. * Updates the given vector "result" with the result of the addition of the current Vector4 and the given one.
  2508. * @param otherVector the vector to add
  2509. * @param result the vector to store the result
  2510. * @returns the current Vector4.
  2511. */
  2512. public addToRef(otherVector: Vector4, result: Vector4): Vector4 {
  2513. result.x = this.x + otherVector.x;
  2514. result.y = this.y + otherVector.y;
  2515. result.z = this.z + otherVector.z;
  2516. result.w = this.w + otherVector.w;
  2517. return this;
  2518. }
  2519. /**
  2520. * Subtract in place the given vector from the current Vector4.
  2521. * @param otherVector the vector to subtract
  2522. * @returns the updated Vector4.
  2523. */
  2524. public subtractInPlace(otherVector: Vector4): Vector4 {
  2525. this.x -= otherVector.x;
  2526. this.y -= otherVector.y;
  2527. this.z -= otherVector.z;
  2528. this.w -= otherVector.w;
  2529. return this;
  2530. }
  2531. /**
  2532. * Returns a new Vector4 with the result of the subtraction of the given vector from the current Vector4.
  2533. * @param otherVector the vector to add
  2534. * @returns the new vector with the result
  2535. */
  2536. public subtract(otherVector: Vector4): Vector4 {
  2537. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  2538. }
  2539. /**
  2540. * Sets the given vector "result" with the result of the subtraction of the given vector from the current Vector4.
  2541. * @param otherVector the vector to subtract
  2542. * @param result the vector to store the result
  2543. * @returns the current Vector4.
  2544. */
  2545. public subtractToRef(otherVector: Vector4, result: Vector4): Vector4 {
  2546. result.x = this.x - otherVector.x;
  2547. result.y = this.y - otherVector.y;
  2548. result.z = this.z - otherVector.z;
  2549. result.w = this.w - otherVector.w;
  2550. return this;
  2551. }
  2552. /**
  2553. * Returns a new Vector4 set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  2554. */
  2555. /**
  2556. * Returns a new Vector4 set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  2557. * @param x value to subtract
  2558. * @param y value to subtract
  2559. * @param z value to subtract
  2560. * @param w value to subtract
  2561. * @returns new vector containing the result
  2562. */
  2563. public subtractFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  2564. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  2565. }
  2566. /**
  2567. * Sets the given vector "result" set with the result of the subtraction of the given floats from the current Vector4 coordinates.
  2568. * @param x value to subtract
  2569. * @param y value to subtract
  2570. * @param z value to subtract
  2571. * @param w value to subtract
  2572. * @param result the vector to store the result in
  2573. * @returns the current Vector4.
  2574. */
  2575. public subtractFromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): Vector4 {
  2576. result.x = this.x - x;
  2577. result.y = this.y - y;
  2578. result.z = this.z - z;
  2579. result.w = this.w - w;
  2580. return this;
  2581. }
  2582. /**
  2583. * Returns a new Vector4 set with the current Vector4 negated coordinates.
  2584. * @returns a new vector with the negated values
  2585. */
  2586. public negate(): Vector4 {
  2587. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  2588. }
  2589. /**
  2590. * Multiplies the current Vector4 coordinates by scale (float).
  2591. * @param scale the number to scale with
  2592. * @returns the updated Vector4.
  2593. */
  2594. public scaleInPlace(scale: number): Vector4 {
  2595. this.x *= scale;
  2596. this.y *= scale;
  2597. this.z *= scale;
  2598. this.w *= scale;
  2599. return this;
  2600. }
  2601. /**
  2602. * Returns a new Vector4 set with the current Vector4 coordinates multiplied by scale (float).
  2603. * @param scale the number to scale with
  2604. * @returns a new vector with the result
  2605. */
  2606. public scale(scale: number): Vector4 {
  2607. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  2608. }
  2609. /**
  2610. * Sets the given vector "result" with the current Vector4 coordinates multiplied by scale (float).
  2611. * @param scale the number to scale with
  2612. * @param result a vector to store the result in
  2613. * @returns the current Vector4.
  2614. */
  2615. public scaleToRef(scale: number, result: Vector4): Vector4 {
  2616. result.x = this.x * scale;
  2617. result.y = this.y * scale;
  2618. result.z = this.z * scale;
  2619. result.w = this.w * scale;
  2620. return this;
  2621. }
  2622. /**
  2623. * Scale the current Vector4 values by a factor and add the result to a given Vector4
  2624. * @param scale defines the scale factor
  2625. * @param result defines the Vector4 object where to store the result
  2626. * @returns the unmodified current Vector4
  2627. */
  2628. public scaleAndAddToRef(scale: number, result: Vector4): Vector4 {
  2629. result.x += this.x * scale;
  2630. result.y += this.y * scale;
  2631. result.z += this.z * scale;
  2632. result.w += this.w * scale;
  2633. return this;
  2634. }
  2635. /**
  2636. * Boolean : True if the current Vector4 coordinates are stricly equal to the given ones.
  2637. * @param otherVector the vector to compare against
  2638. * @returns true if they are equal
  2639. */
  2640. public equals(otherVector: Vector4): boolean {
  2641. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  2642. }
  2643. /**
  2644. * Boolean : True if the current Vector4 coordinates are each beneath the distance "epsilon" from the given vector ones.
  2645. * @param otherVector vector to compare against
  2646. * @param epsilon (Default: very small number)
  2647. * @returns true if they are equal
  2648. */
  2649. public equalsWithEpsilon(otherVector: Vector4, epsilon: number = Epsilon): boolean {
  2650. return otherVector
  2651. && Scalar.WithinEpsilon(this.x, otherVector.x, epsilon)
  2652. && Scalar.WithinEpsilon(this.y, otherVector.y, epsilon)
  2653. && Scalar.WithinEpsilon(this.z, otherVector.z, epsilon)
  2654. && Scalar.WithinEpsilon(this.w, otherVector.w, epsilon);
  2655. }
  2656. /**
  2657. * Boolean : True if the given floats are strictly equal to the current Vector4 coordinates.
  2658. * @param x x value to compare against
  2659. * @param y y value to compare against
  2660. * @param z z value to compare against
  2661. * @param w w value to compare against
  2662. * @returns true if equal
  2663. */
  2664. public equalsToFloats(x: number, y: number, z: number, w: number): boolean {
  2665. return this.x === x && this.y === y && this.z === z && this.w === w;
  2666. }
  2667. /**
  2668. * Multiplies in place the current Vector4 by the given one.
  2669. * @param otherVector vector to multiple with
  2670. * @returns the updated Vector4.
  2671. */
  2672. public multiplyInPlace(otherVector: Vector4): Vector4 {
  2673. this.x *= otherVector.x;
  2674. this.y *= otherVector.y;
  2675. this.z *= otherVector.z;
  2676. this.w *= otherVector.w;
  2677. return this;
  2678. }
  2679. /**
  2680. * Returns a new Vector4 set with the multiplication result of the current Vector4 and the given one.
  2681. * @param otherVector vector to multiple with
  2682. * @returns resulting new vector
  2683. */
  2684. public multiply(otherVector: Vector4): Vector4 {
  2685. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  2686. }
  2687. /**
  2688. * Updates the given vector "result" with the multiplication result of the current Vector4 and the given one.
  2689. * @param otherVector vector to multiple with
  2690. * @param result vector to store the result
  2691. * @returns the current Vector4.
  2692. */
  2693. public multiplyToRef(otherVector: Vector4, result: Vector4): Vector4 {
  2694. result.x = this.x * otherVector.x;
  2695. result.y = this.y * otherVector.y;
  2696. result.z = this.z * otherVector.z;
  2697. result.w = this.w * otherVector.w;
  2698. return this;
  2699. }
  2700. /**
  2701. * Returns a new Vector4 set with the multiplication result of the given floats and the current Vector4 coordinates.
  2702. * @param x x value multiply with
  2703. * @param y y value multiply with
  2704. * @param z z value multiply with
  2705. * @param w w value multiply with
  2706. * @returns resulting new vector
  2707. */
  2708. public multiplyByFloats(x: number, y: number, z: number, w: number): Vector4 {
  2709. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  2710. }
  2711. /**
  2712. * Returns a new Vector4 set with the division result of the current Vector4 by the given one.
  2713. * @param otherVector vector to devide with
  2714. * @returns resulting new vector
  2715. */
  2716. public divide(otherVector: Vector4): Vector4 {
  2717. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  2718. }
  2719. /**
  2720. * Updates the given vector "result" with the division result of the current Vector4 by the given one.
  2721. * @param otherVector vector to devide with
  2722. * @param result vector to store the result
  2723. * @returns the current Vector4.
  2724. */
  2725. public divideToRef(otherVector: Vector4, result: Vector4): Vector4 {
  2726. result.x = this.x / otherVector.x;
  2727. result.y = this.y / otherVector.y;
  2728. result.z = this.z / otherVector.z;
  2729. result.w = this.w / otherVector.w;
  2730. return this;
  2731. }
  2732. /**
  2733. * Divides the current Vector3 coordinates by the given ones.
  2734. * @param otherVector vector to devide with
  2735. * @returns the updated Vector3.
  2736. */
  2737. public divideInPlace(otherVector: Vector4): Vector4 {
  2738. return this.divideToRef(otherVector, this);
  2739. }
  2740. /**
  2741. * Updates the Vector4 coordinates with the minimum values between its own and the given vector ones
  2742. * @param other defines the second operand
  2743. * @returns the current updated Vector4
  2744. */
  2745. public minimizeInPlace(other: Vector4): Vector4 {
  2746. if (other.x < this.x) { this.x = other.x; }
  2747. if (other.y < this.y) { this.y = other.y; }
  2748. if (other.z < this.z) { this.z = other.z; }
  2749. if (other.w < this.w) { this.w = other.w; }
  2750. return this;
  2751. }
  2752. /**
  2753. * Updates the Vector4 coordinates with the maximum values between its own and the given vector ones
  2754. * @param other defines the second operand
  2755. * @returns the current updated Vector4
  2756. */
  2757. public maximizeInPlace(other: Vector4): Vector4 {
  2758. if (other.x > this.x) { this.x = other.x; }
  2759. if (other.y > this.y) { this.y = other.y; }
  2760. if (other.z > this.z) { this.z = other.z; }
  2761. if (other.w > this.w) { this.w = other.w; }
  2762. return this;
  2763. }
  2764. /**
  2765. * Gets a new Vector4 from current Vector4 floored values
  2766. * @returns a new Vector4
  2767. */
  2768. public floor(): Vector4 {
  2769. return new Vector4(Math.floor(this.x), Math.floor(this.y), Math.floor(this.z), Math.floor(this.w));
  2770. }
  2771. /**
  2772. * Gets a new Vector4 from current Vector3 floored values
  2773. * @returns a new Vector4
  2774. */
  2775. public fract(): Vector4 {
  2776. 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));
  2777. }
  2778. // Properties
  2779. /**
  2780. * Returns the Vector4 length (float).
  2781. * @returns the length
  2782. */
  2783. public length(): number {
  2784. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  2785. }
  2786. /**
  2787. * Returns the Vector4 squared length (float).
  2788. * @returns the length squared
  2789. */
  2790. public lengthSquared(): number {
  2791. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  2792. }
  2793. // Methods
  2794. /**
  2795. * Normalizes in place the Vector4.
  2796. * @returns the updated Vector4.
  2797. */
  2798. public normalize(): Vector4 {
  2799. var len = this.length();
  2800. if (len === 0) {
  2801. return this;
  2802. }
  2803. return this.scaleInPlace(1.0 / len);
  2804. }
  2805. /**
  2806. * Returns a new Vector3 from the Vector4 (x, y, z) coordinates.
  2807. * @returns this converted to a new vector3
  2808. */
  2809. public toVector3(): Vector3 {
  2810. return new Vector3(this.x, this.y, this.z);
  2811. }
  2812. /**
  2813. * Returns a new Vector4 copied from the current one.
  2814. * @returns the new cloned vector
  2815. */
  2816. public clone(): Vector4 {
  2817. return new Vector4(this.x, this.y, this.z, this.w);
  2818. }
  2819. /**
  2820. * Updates the current Vector4 with the given one coordinates.
  2821. * @param source the source vector to copy from
  2822. * @returns the updated Vector4.
  2823. */
  2824. public copyFrom(source: Vector4): Vector4 {
  2825. this.x = source.x;
  2826. this.y = source.y;
  2827. this.z = source.z;
  2828. this.w = source.w;
  2829. return this;
  2830. }
  2831. /**
  2832. * Updates the current Vector4 coordinates with the given floats.
  2833. * @param x float to copy from
  2834. * @param y float to copy from
  2835. * @param z float to copy from
  2836. * @param w float to copy from
  2837. * @returns the updated Vector4.
  2838. */
  2839. public copyFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  2840. this.x = x;
  2841. this.y = y;
  2842. this.z = z;
  2843. this.w = w;
  2844. return this;
  2845. }
  2846. /**
  2847. * Updates the current Vector4 coordinates with the given floats.
  2848. * @param x float to set from
  2849. * @param y float to set from
  2850. * @param z float to set from
  2851. * @param w float to set from
  2852. * @returns the updated Vector4.
  2853. */
  2854. public set(x: number, y: number, z: number, w: number): Vector4 {
  2855. return this.copyFromFloats(x, y, z, w);
  2856. }
  2857. /**
  2858. * Copies the given float to the current Vector3 coordinates
  2859. * @param v defines the x, y, z and w coordinates of the operand
  2860. * @returns the current updated Vector3
  2861. */
  2862. public setAll(v: number): Vector4 {
  2863. this.x = this.y = this.z = this.w = v;
  2864. return this;
  2865. }
  2866. // Statics
  2867. /**
  2868. * Returns a new Vector4 set from the starting index of the given array.
  2869. * @param array the array to pull values from
  2870. * @param offset the offset into the array to start at
  2871. * @returns the new vector
  2872. */
  2873. public static FromArray(array: ArrayLike<number>, offset?: number): Vector4 {
  2874. if (!offset) {
  2875. offset = 0;
  2876. }
  2877. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  2878. }
  2879. /**
  2880. * Updates the given vector "result" from the starting index of the given array.
  2881. * @param array the array to pull values from
  2882. * @param offset the offset into the array to start at
  2883. * @param result the vector to store the result in
  2884. */
  2885. public static FromArrayToRef(array: ArrayLike<number>, offset: number, result: Vector4): void {
  2886. result.x = array[offset];
  2887. result.y = array[offset + 1];
  2888. result.z = array[offset + 2];
  2889. result.w = array[offset + 3];
  2890. }
  2891. /**
  2892. * Updates the given vector "result" from the starting index of the given Float32Array.
  2893. * @param array the array to pull values from
  2894. * @param offset the offset into the array to start at
  2895. * @param result the vector to store the result in
  2896. */
  2897. public static FromFloatArrayToRef(array: Float32Array, offset: number, result: Vector4): void {
  2898. Vector4.FromArrayToRef(array, offset, result);
  2899. }
  2900. /**
  2901. * Updates the given vector "result" coordinates from the given floats.
  2902. * @param x float to set from
  2903. * @param y float to set from
  2904. * @param z float to set from
  2905. * @param w float to set from
  2906. * @param result the vector to the floats in
  2907. */
  2908. public static FromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): void {
  2909. result.x = x;
  2910. result.y = y;
  2911. result.z = z;
  2912. result.w = w;
  2913. }
  2914. /**
  2915. * Returns a new Vector4 set to (0.0, 0.0, 0.0, 0.0)
  2916. * @returns the new vector
  2917. */
  2918. public static Zero(): Vector4 {
  2919. return new Vector4(0.0, 0.0, 0.0, 0.0);
  2920. }
  2921. /**
  2922. * Returns a new Vector4 set to (1.0, 1.0, 1.0, 1.0)
  2923. * @returns the new vector
  2924. */
  2925. public static One(): Vector4 {
  2926. return new Vector4(1.0, 1.0, 1.0, 1.0);
  2927. }
  2928. /**
  2929. * Returns a new normalized Vector4 from the given one.
  2930. * @param vector the vector to normalize
  2931. * @returns the vector
  2932. */
  2933. public static Normalize(vector: Vector4): Vector4 {
  2934. var result = Vector4.Zero();
  2935. Vector4.NormalizeToRef(vector, result);
  2936. return result;
  2937. }
  2938. /**
  2939. * Updates the given vector "result" from the normalization of the given one.
  2940. * @param vector the vector to normalize
  2941. * @param result the vector to store the result in
  2942. */
  2943. public static NormalizeToRef(vector: Vector4, result: Vector4): void {
  2944. result.copyFrom(vector);
  2945. result.normalize();
  2946. }
  2947. /**
  2948. * Returns a vector with the minimum values from the left and right vectors
  2949. * @param left left vector to minimize
  2950. * @param right right vector to minimize
  2951. * @returns a new vector with the minimum of the left and right vector values
  2952. */
  2953. public static Minimize(left: Vector4, right: Vector4): Vector4 {
  2954. var min = left.clone();
  2955. min.minimizeInPlace(right);
  2956. return min;
  2957. }
  2958. /**
  2959. * Returns a vector with the maximum values from the left and right vectors
  2960. * @param left left vector to maximize
  2961. * @param right right vector to maximize
  2962. * @returns a new vector with the maximum of the left and right vector values
  2963. */
  2964. public static Maximize(left: Vector4, right: Vector4): Vector4 {
  2965. var max = left.clone();
  2966. max.maximizeInPlace(right);
  2967. return max;
  2968. }
  2969. /**
  2970. * Returns the distance (float) between the vectors "value1" and "value2".
  2971. * @param value1 value to calulate the distance between
  2972. * @param value2 value to calulate the distance between
  2973. * @return the distance between the two vectors
  2974. */
  2975. public static Distance(value1: Vector4, value2: Vector4): number {
  2976. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  2977. }
  2978. /**
  2979. * Returns the squared distance (float) between the vectors "value1" and "value2".
  2980. * @param value1 value to calulate the distance between
  2981. * @param value2 value to calulate the distance between
  2982. * @return the distance between the two vectors squared
  2983. */
  2984. public static DistanceSquared(value1: Vector4, value2: Vector4): number {
  2985. var x = value1.x - value2.x;
  2986. var y = value1.y - value2.y;
  2987. var z = value1.z - value2.z;
  2988. var w = value1.w - value2.w;
  2989. return (x * x) + (y * y) + (z * z) + (w * w);
  2990. }
  2991. /**
  2992. * Returns a new Vector4 located at the center between the vectors "value1" and "value2".
  2993. * @param value1 value to calulate the center between
  2994. * @param value2 value to calulate the center between
  2995. * @return the center between the two vectors
  2996. */
  2997. public static Center(value1: Vector4, value2: Vector4): Vector4 {
  2998. var center = value1.add(value2);
  2999. center.scaleInPlace(0.5);
  3000. return center;
  3001. }
  3002. /**
  3003. * Returns a new Vector4 set with the result of the normal transformation by the given matrix of the given vector.
  3004. * This methods computes transformed normalized direction vectors only.
  3005. * @param vector the vector to transform
  3006. * @param transformation the transformation matrix to apply
  3007. * @returns the new vector
  3008. */
  3009. public static TransformNormal(vector: Vector4, transformation: Matrix): Vector4 {
  3010. var result = Vector4.Zero();
  3011. Vector4.TransformNormalToRef(vector, transformation, result);
  3012. return result;
  3013. }
  3014. /**
  3015. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given vector.
  3016. * This methods computes transformed normalized direction vectors only.
  3017. * @param vector the vector to transform
  3018. * @param transformation the transformation matrix to apply
  3019. * @param result the vector to store the result in
  3020. */
  3021. public static TransformNormalToRef(vector: Vector4, transformation: Matrix, result: Vector4): void {
  3022. const m = transformation.m;
  3023. var x = (vector.x * m[0]) + (vector.y * m[4]) + (vector.z * m[8]);
  3024. var y = (vector.x * m[1]) + (vector.y * m[5]) + (vector.z * m[9]);
  3025. var z = (vector.x * m[2]) + (vector.y * m[6]) + (vector.z * m[10]);
  3026. result.x = x;
  3027. result.y = y;
  3028. result.z = z;
  3029. result.w = vector.w;
  3030. }
  3031. /**
  3032. * Sets the given vector "result" with the result of the normal transformation by the given matrix of the given floats (x, y, z, w).
  3033. * This methods computes transformed normalized direction vectors only.
  3034. * @param x value to transform
  3035. * @param y value to transform
  3036. * @param z value to transform
  3037. * @param w value to transform
  3038. * @param transformation the transformation matrix to apply
  3039. * @param result the vector to store the results in
  3040. */
  3041. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, w: number, transformation: Matrix, result: Vector4): void {
  3042. const m = transformation.m;
  3043. result.x = (x * m[0]) + (y * m[4]) + (z * m[8]);
  3044. result.y = (x * m[1]) + (y * m[5]) + (z * m[9]);
  3045. result.z = (x * m[2]) + (y * m[6]) + (z * m[10]);
  3046. result.w = w;
  3047. }
  3048. }
  3049. /**
  3050. * Interface for the size containing width and height
  3051. */
  3052. export interface ISize {
  3053. /**
  3054. * Width
  3055. */
  3056. width: number;
  3057. /**
  3058. * Heighht
  3059. */
  3060. height: number;
  3061. }
  3062. /**
  3063. * Size containing widht and height
  3064. */
  3065. export class Size implements ISize {
  3066. /**
  3067. * Width
  3068. */
  3069. public width: number;
  3070. /**
  3071. * Height
  3072. */
  3073. public height: number;
  3074. /**
  3075. * Creates a Size object from the given width and height (floats).
  3076. * @param width width of the new size
  3077. * @param height height of the new size
  3078. */
  3079. public constructor(width: number, height: number) {
  3080. this.width = width;
  3081. this.height = height;
  3082. }
  3083. /**
  3084. * Returns a string with the Size width and height
  3085. * @returns a string with the Size width and height
  3086. */
  3087. public toString(): string {
  3088. return `{W: ${this.width}, H: ${this.height}}`;
  3089. }
  3090. /**
  3091. * "Size"
  3092. * @returns the string "Size"
  3093. */
  3094. public getClassName(): string {
  3095. return "Size";
  3096. }
  3097. /**
  3098. * Returns the Size hash code.
  3099. * @returns a hash code for a unique width and height
  3100. */
  3101. public getHashCode(): number {
  3102. let hash = this.width || 0;
  3103. hash = (hash * 397) ^ (this.height || 0);
  3104. return hash;
  3105. }
  3106. /**
  3107. * Updates the current size from the given one.
  3108. * @param src the given size
  3109. */
  3110. public copyFrom(src: Size) {
  3111. this.width = src.width;
  3112. this.height = src.height;
  3113. }
  3114. /**
  3115. * Updates in place the current Size from the given floats.
  3116. * @param width width of the new size
  3117. * @param height height of the new size
  3118. * @returns the updated Size.
  3119. */
  3120. public copyFromFloats(width: number, height: number): Size {
  3121. this.width = width;
  3122. this.height = height;
  3123. return this;
  3124. }
  3125. /**
  3126. * Updates in place the current Size from the given floats.
  3127. * @param width width to set
  3128. * @param height height to set
  3129. * @returns the updated Size.
  3130. */
  3131. public set(width: number, height: number): Size {
  3132. return this.copyFromFloats(width, height);
  3133. }
  3134. /**
  3135. * Multiplies the width and height by numbers
  3136. * @param w factor to multiple the width by
  3137. * @param h factor to multiple the height by
  3138. * @returns a new Size set with the multiplication result of the current Size and the given floats.
  3139. */
  3140. public multiplyByFloats(w: number, h: number): Size {
  3141. return new Size(this.width * w, this.height * h);
  3142. }
  3143. /**
  3144. * Clones the size
  3145. * @returns a new Size copied from the given one.
  3146. */
  3147. public clone(): Size {
  3148. return new Size(this.width, this.height);
  3149. }
  3150. /**
  3151. * True if the current Size and the given one width and height are strictly equal.
  3152. * @param other the other size to compare against
  3153. * @returns True if the current Size and the given one width and height are strictly equal.
  3154. */
  3155. public equals(other: Size): boolean {
  3156. if (!other) {
  3157. return false;
  3158. }
  3159. return (this.width === other.width) && (this.height === other.height);
  3160. }
  3161. /**
  3162. * The surface of the Size : width * height (float).
  3163. */
  3164. public get surface(): number {
  3165. return this.width * this.height;
  3166. }
  3167. /**
  3168. * Create a new size of zero
  3169. * @returns a new Size set to (0.0, 0.0)
  3170. */
  3171. public static Zero(): Size {
  3172. return new Size(0.0, 0.0);
  3173. }
  3174. /**
  3175. * Sums the width and height of two sizes
  3176. * @param otherSize size to add to this size
  3177. * @returns a new Size set as the addition result of the current Size and the given one.
  3178. */
  3179. public add(otherSize: Size): Size {
  3180. let r = new Size(this.width + otherSize.width, this.height + otherSize.height);
  3181. return r;
  3182. }
  3183. /**
  3184. * Subtracts the width and height of two
  3185. * @param otherSize size to subtract to this size
  3186. * @returns a new Size set as the subtraction result of the given one from the current Size.
  3187. */
  3188. public subtract(otherSize: Size): Size {
  3189. let r = new Size(this.width - otherSize.width, this.height - otherSize.height);
  3190. return r;
  3191. }
  3192. /**
  3193. * Creates a new Size set at the linear interpolation "amount" between "start" and "end"
  3194. * @param start starting size to lerp between
  3195. * @param end end size to lerp between
  3196. * @param amount amount to lerp between the start and end values
  3197. * @returns a new Size set at the linear interpolation "amount" between "start" and "end"
  3198. */
  3199. public static Lerp(start: Size, end: Size, amount: number): Size {
  3200. var w = start.width + ((end.width - start.width) * amount);
  3201. var h = start.height + ((end.height - start.height) * amount);
  3202. return new Size(w, h);
  3203. }
  3204. }
  3205. /**
  3206. * Class used to store quaternion data
  3207. * @see https://en.wikipedia.org/wiki/Quaternion
  3208. * @see http://doc.babylonjs.com/features/position,_rotation,_scaling
  3209. */
  3210. export class Quaternion {
  3211. /**
  3212. * Creates a new Quaternion from the given floats
  3213. * @param x defines the first component (0 by default)
  3214. * @param y defines the second component (0 by default)
  3215. * @param z defines the third component (0 by default)
  3216. * @param w defines the fourth component (1.0 by default)
  3217. */
  3218. constructor(
  3219. /** defines the first component (0 by default) */
  3220. public x: number = 0.0,
  3221. /** defines the second component (0 by default) */
  3222. public y: number = 0.0,
  3223. /** defines the third component (0 by default) */
  3224. public z: number = 0.0,
  3225. /** defines the fourth component (1.0 by default) */
  3226. public w: number = 1.0) {
  3227. }
  3228. /**
  3229. * Gets a string representation for the current quaternion
  3230. * @returns a string with the Quaternion coordinates
  3231. */
  3232. public toString(): string {
  3233. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  3234. }
  3235. /**
  3236. * Gets the class name of the quaternion
  3237. * @returns the string "Quaternion"
  3238. */
  3239. public getClassName(): string {
  3240. return "Quaternion";
  3241. }
  3242. /**
  3243. * Gets a hash code for this quaternion
  3244. * @returns the quaternion hash code
  3245. */
  3246. public getHashCode(): number {
  3247. let hash = this.x || 0;
  3248. hash = (hash * 397) ^ (this.y || 0);
  3249. hash = (hash * 397) ^ (this.z || 0);
  3250. hash = (hash * 397) ^ (this.w || 0);
  3251. return hash;
  3252. }
  3253. /**
  3254. * Copy the quaternion to an array
  3255. * @returns a new array populated with 4 elements from the quaternion coordinates
  3256. */
  3257. public asArray(): number[] {
  3258. return [this.x, this.y, this.z, this.w];
  3259. }
  3260. /**
  3261. * Check if two quaternions are equals
  3262. * @param otherQuaternion defines the second operand
  3263. * @return true if the current quaternion and the given one coordinates are strictly equals
  3264. */
  3265. public equals(otherQuaternion: Quaternion): boolean {
  3266. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  3267. }
  3268. /**
  3269. * Clone the current quaternion
  3270. * @returns a new quaternion copied from the current one
  3271. */
  3272. public clone(): Quaternion {
  3273. return new Quaternion(this.x, this.y, this.z, this.w);
  3274. }
  3275. /**
  3276. * Copy a quaternion to the current one
  3277. * @param other defines the other quaternion
  3278. * @returns the updated current quaternion
  3279. */
  3280. public copyFrom(other: Quaternion): Quaternion {
  3281. this.x = other.x;
  3282. this.y = other.y;
  3283. this.z = other.z;
  3284. this.w = other.w;
  3285. return this;
  3286. }
  3287. /**
  3288. * Updates the current quaternion with the given float coordinates
  3289. * @param x defines the x coordinate
  3290. * @param y defines the y coordinate
  3291. * @param z defines the z coordinate
  3292. * @param w defines the w coordinate
  3293. * @returns the updated current quaternion
  3294. */
  3295. public copyFromFloats(x: number, y: number, z: number, w: number): Quaternion {
  3296. this.x = x;
  3297. this.y = y;
  3298. this.z = z;
  3299. this.w = w;
  3300. return this;
  3301. }
  3302. /**
  3303. * Updates the current quaternion from the given float coordinates
  3304. * @param x defines the x coordinate
  3305. * @param y defines the y coordinate
  3306. * @param z defines the z coordinate
  3307. * @param w defines the w coordinate
  3308. * @returns the updated current quaternion
  3309. */
  3310. public set(x: number, y: number, z: number, w: number): Quaternion {
  3311. return this.copyFromFloats(x, y, z, w);
  3312. }
  3313. /**
  3314. * Adds two quaternions
  3315. * @param other defines the second operand
  3316. * @returns a new quaternion as the addition result of the given one and the current quaternion
  3317. */
  3318. public add(other: Quaternion): Quaternion {
  3319. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  3320. }
  3321. /**
  3322. * Add a quaternion to the current one
  3323. * @param other defines the quaternion to add
  3324. * @returns the current quaternion
  3325. */
  3326. public addInPlace(other: Quaternion): Quaternion {
  3327. this.x += other.x;
  3328. this.y += other.y;
  3329. this.z += other.z;
  3330. this.w += other.w;
  3331. return this;
  3332. }
  3333. /**
  3334. * Subtract two quaternions
  3335. * @param other defines the second operand
  3336. * @returns a new quaternion as the subtraction result of the given one from the current one
  3337. */
  3338. public subtract(other: Quaternion): Quaternion {
  3339. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  3340. }
  3341. /**
  3342. * Multiplies the current quaternion by a scale factor
  3343. * @param value defines the scale factor
  3344. * @returns a new quaternion set by multiplying the current quaternion coordinates by the float "scale"
  3345. */
  3346. public scale(value: number): Quaternion {
  3347. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  3348. }
  3349. /**
  3350. * Scale the current quaternion values by a factor and stores the result to a given quaternion
  3351. * @param scale defines the scale factor
  3352. * @param result defines the Quaternion object where to store the result
  3353. * @returns the unmodified current quaternion
  3354. */
  3355. public scaleToRef(scale: number, result: Quaternion): Quaternion {
  3356. result.x = this.x * scale;
  3357. result.y = this.y * scale;
  3358. result.z = this.z * scale;
  3359. result.w = this.w * scale;
  3360. return this;
  3361. }
  3362. /**
  3363. * Multiplies in place the current quaternion by a scale factor
  3364. * @param value defines the scale factor
  3365. * @returns the current modified quaternion
  3366. */
  3367. public scaleInPlace(value: number): Quaternion {
  3368. this.x *= value;
  3369. this.y *= value;
  3370. this.z *= value;
  3371. this.w *= value;
  3372. return this;
  3373. }
  3374. /**
  3375. * Scale the current quaternion values by a factor and add the result to a given quaternion
  3376. * @param scale defines the scale factor
  3377. * @param result defines the Quaternion object where to store the result
  3378. * @returns the unmodified current quaternion
  3379. */
  3380. public scaleAndAddToRef(scale: number, result: Quaternion): Quaternion {
  3381. result.x += this.x * scale;
  3382. result.y += this.y * scale;
  3383. result.z += this.z * scale;
  3384. result.w += this.w * scale;
  3385. return this;
  3386. }
  3387. /**
  3388. * Multiplies two quaternions
  3389. * @param q1 defines the second operand
  3390. * @returns a new quaternion set as the multiplication result of the current one with the given one "q1"
  3391. */
  3392. public multiply(q1: Quaternion): Quaternion {
  3393. var result = new Quaternion(0, 0, 0, 1.0);
  3394. this.multiplyToRef(q1, result);
  3395. return result;
  3396. }
  3397. /**
  3398. * Sets the given "result" as the the multiplication result of the current one with the given one "q1"
  3399. * @param q1 defines the second operand
  3400. * @param result defines the target quaternion
  3401. * @returns the current quaternion
  3402. */
  3403. public multiplyToRef(q1: Quaternion, result: Quaternion): Quaternion {
  3404. var x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  3405. var y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  3406. var z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  3407. var w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  3408. result.copyFromFloats(x, y, z, w);
  3409. return this;
  3410. }
  3411. /**
  3412. * Updates the current quaternion with the multiplication of itself with the given one "q1"
  3413. * @param q1 defines the second operand
  3414. * @returns the currentupdated quaternion
  3415. */
  3416. public multiplyInPlace(q1: Quaternion): Quaternion {
  3417. this.multiplyToRef(q1, this);
  3418. return this;
  3419. }
  3420. /**
  3421. * Conjugates (1-q) the current quaternion and stores the result in the given quaternion
  3422. * @param ref defines the target quaternion
  3423. * @returns the current quaternion
  3424. */
  3425. public conjugateToRef(ref: Quaternion): Quaternion {
  3426. ref.copyFromFloats(-this.x, -this.y, -this.z, this.w);
  3427. return this;
  3428. }
  3429. /**
  3430. * Conjugates in place (1-q) the current quaternion
  3431. * @returns the current updated quaternion
  3432. */
  3433. public conjugateInPlace(): Quaternion {
  3434. this.x *= -1;
  3435. this.y *= -1;
  3436. this.z *= -1;
  3437. return this;
  3438. }
  3439. /**
  3440. * Conjugates in place (1-q) the current quaternion
  3441. * @returns a new quaternion
  3442. */
  3443. public conjugate(): Quaternion {
  3444. var result = new Quaternion(-this.x, -this.y, -this.z, this.w);
  3445. return result;
  3446. }
  3447. /**
  3448. * Gets length of current quaternion
  3449. * @returns the quaternion length (float)
  3450. */
  3451. public length(): number {
  3452. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  3453. }
  3454. /**
  3455. * Normalize in place the current quaternion
  3456. * @returns the current updated quaternion
  3457. */
  3458. public normalize(): Quaternion {
  3459. var length = 1.0 / this.length();
  3460. this.x *= length;
  3461. this.y *= length;
  3462. this.z *= length;
  3463. this.w *= length;
  3464. return this;
  3465. }
  3466. /**
  3467. * Returns a new Vector3 set with the Euler angles translated from the current quaternion
  3468. * @param order is a reserved parameter and is ignore for now
  3469. * @returns a new Vector3 containing the Euler angles
  3470. */
  3471. public toEulerAngles(order = "YZX"): Vector3 {
  3472. var result = Vector3.Zero();
  3473. this.toEulerAnglesToRef(result, order);
  3474. return result;
  3475. }
  3476. /**
  3477. * Sets the given vector3 "result" with the Euler angles translated from the current quaternion
  3478. * @param result defines the vector which will be filled with the Euler angles
  3479. * @param order is a reserved parameter and is ignore for now
  3480. * @returns the current unchanged quaternion
  3481. */
  3482. public toEulerAnglesToRef(result: Vector3, order = "YZX"): Quaternion {
  3483. var qz = this.z;
  3484. var qx = this.x;
  3485. var qy = this.y;
  3486. var qw = this.w;
  3487. var sqw = qw * qw;
  3488. var sqz = qz * qz;
  3489. var sqx = qx * qx;
  3490. var sqy = qy * qy;
  3491. var zAxisY = qy * qz - qx * qw;
  3492. var limit = .4999999;
  3493. if (zAxisY < -limit) {
  3494. result.y = 2 * Math.atan2(qy, qw);
  3495. result.x = Math.PI / 2;
  3496. result.z = 0;
  3497. } else if (zAxisY > limit) {
  3498. result.y = 2 * Math.atan2(qy, qw);
  3499. result.x = -Math.PI / 2;
  3500. result.z = 0;
  3501. } else {
  3502. result.z = Math.atan2(2.0 * (qx * qy + qz * qw), (-sqz - sqx + sqy + sqw));
  3503. result.x = Math.asin(-2.0 * (qz * qy - qx * qw));
  3504. result.y = Math.atan2(2.0 * (qz * qx + qy * qw), (sqz - sqx - sqy + sqw));
  3505. }
  3506. return this;
  3507. }
  3508. /**
  3509. * Updates the given rotation matrix with the current quaternion values
  3510. * @param result defines the target matrix
  3511. * @returns the current unchanged quaternion
  3512. */
  3513. public toRotationMatrix(result: Matrix): Quaternion {
  3514. Matrix.FromQuaternionToRef(this, result);
  3515. return this;
  3516. }
  3517. /**
  3518. * Updates the current quaternion from the given rotation matrix values
  3519. * @param matrix defines the source matrix
  3520. * @returns the current updated quaternion
  3521. */
  3522. public fromRotationMatrix(matrix: Matrix): Quaternion {
  3523. Quaternion.FromRotationMatrixToRef(matrix, this);
  3524. return this;
  3525. }
  3526. // Statics
  3527. /**
  3528. * Creates a new quaternion from a rotation matrix
  3529. * @param matrix defines the source matrix
  3530. * @returns a new quaternion created from the given rotation matrix values
  3531. */
  3532. public static FromRotationMatrix(matrix: Matrix): Quaternion {
  3533. var result = new Quaternion();
  3534. Quaternion.FromRotationMatrixToRef(matrix, result);
  3535. return result;
  3536. }
  3537. /**
  3538. * Updates the given quaternion with the given rotation matrix values
  3539. * @param matrix defines the source matrix
  3540. * @param result defines the target quaternion
  3541. */
  3542. public static FromRotationMatrixToRef(matrix: Matrix, result: Quaternion): void {
  3543. var data = matrix.m;
  3544. var m11 = data[0], m12 = data[4], m13 = data[8];
  3545. var m21 = data[1], m22 = data[5], m23 = data[9];
  3546. var m31 = data[2], m32 = data[6], m33 = data[10];
  3547. var trace = m11 + m22 + m33;
  3548. var s;
  3549. if (trace > 0) {
  3550. s = 0.5 / Math.sqrt(trace + 1.0);
  3551. result.w = 0.25 / s;
  3552. result.x = (m32 - m23) * s;
  3553. result.y = (m13 - m31) * s;
  3554. result.z = (m21 - m12) * s;
  3555. } else if (m11 > m22 && m11 > m33) {
  3556. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  3557. result.w = (m32 - m23) / s;
  3558. result.x = 0.25 * s;
  3559. result.y = (m12 + m21) / s;
  3560. result.z = (m13 + m31) / s;
  3561. } else if (m22 > m33) {
  3562. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  3563. result.w = (m13 - m31) / s;
  3564. result.x = (m12 + m21) / s;
  3565. result.y = 0.25 * s;
  3566. result.z = (m23 + m32) / s;
  3567. } else {
  3568. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  3569. result.w = (m21 - m12) / s;
  3570. result.x = (m13 + m31) / s;
  3571. result.y = (m23 + m32) / s;
  3572. result.z = 0.25 * s;
  3573. }
  3574. }
  3575. /**
  3576. * Returns the dot product (float) between the quaternions "left" and "right"
  3577. * @param left defines the left operand
  3578. * @param right defines the right operand
  3579. * @returns the dot product
  3580. */
  3581. public static Dot(left: Quaternion, right: Quaternion): number {
  3582. return (left.x * right.x + left.y * right.y + left.z * right.z + left.w * right.w);
  3583. }
  3584. /**
  3585. * Checks if the two quaternions are close to each other
  3586. * @param quat0 defines the first quaternion to check
  3587. * @param quat1 defines the second quaternion to check
  3588. * @returns true if the two quaternions are close to each other
  3589. */
  3590. public static AreClose(quat0: Quaternion, quat1: Quaternion): boolean {
  3591. let dot = Quaternion.Dot(quat0, quat1);
  3592. return dot >= 0;
  3593. }
  3594. /**
  3595. * Creates an empty quaternion
  3596. * @returns a new quaternion set to (0.0, 0.0, 0.0)
  3597. */
  3598. public static Zero(): Quaternion {
  3599. return new Quaternion(0.0, 0.0, 0.0, 0.0);
  3600. }
  3601. /**
  3602. * Inverse a given quaternion
  3603. * @param q defines the source quaternion
  3604. * @returns a new quaternion as the inverted current quaternion
  3605. */
  3606. public static Inverse(q: Quaternion): Quaternion {
  3607. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  3608. }
  3609. /**
  3610. * Creates an identity quaternion
  3611. * @returns the identity quaternion
  3612. */
  3613. public static Identity(): Quaternion {
  3614. return new Quaternion(0.0, 0.0, 0.0, 1.0);
  3615. }
  3616. /**
  3617. * Gets a boolean indicating if the given quaternion is identity
  3618. * @param quaternion defines the quaternion to check
  3619. * @returns true if the quaternion is identity
  3620. */
  3621. public static IsIdentity(quaternion: Quaternion): boolean {
  3622. return quaternion && quaternion.x === 0 && quaternion.y === 0 && quaternion.z === 0 && quaternion.w === 1;
  3623. }
  3624. /**
  3625. * Creates a quaternion from a rotation around an axis
  3626. * @param axis defines the axis to use
  3627. * @param angle defines the angle to use
  3628. * @returns a new quaternion created from the given axis (Vector3) and angle in radians (float)
  3629. */
  3630. public static RotationAxis(axis: Vector3, angle: number): Quaternion {
  3631. return Quaternion.RotationAxisToRef(axis, angle, new Quaternion());
  3632. }
  3633. /**
  3634. * Creates a rotation around an axis and stores it into the given quaternion
  3635. * @param axis defines the axis to use
  3636. * @param angle defines the angle to use
  3637. * @param result defines the target quaternion
  3638. * @returns the target quaternion
  3639. */
  3640. public static RotationAxisToRef(axis: Vector3, angle: number, result: Quaternion): Quaternion {
  3641. var sin = Math.sin(angle / 2);
  3642. axis.normalize();
  3643. result.w = Math.cos(angle / 2);
  3644. result.x = axis.x * sin;
  3645. result.y = axis.y * sin;
  3646. result.z = axis.z * sin;
  3647. return result;
  3648. }
  3649. /**
  3650. * Creates a new quaternion from data stored into an array
  3651. * @param array defines the data source
  3652. * @param offset defines the offset in the source array where the data starts
  3653. * @returns a new quaternion
  3654. */
  3655. public static FromArray(array: ArrayLike<number>, offset?: number): Quaternion {
  3656. if (!offset) {
  3657. offset = 0;
  3658. }
  3659. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  3660. }
  3661. /**
  3662. * Creates a new quaternion from the given Euler float angles (y, x, z)
  3663. * @param yaw defines the rotation around Y axis
  3664. * @param pitch defines the rotation around X axis
  3665. * @param roll defines the rotation around Z axis
  3666. * @returns the new quaternion
  3667. */
  3668. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Quaternion {
  3669. var q = new Quaternion();
  3670. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, q);
  3671. return q;
  3672. }
  3673. /**
  3674. * Creates a new rotation from the given Euler float angles (y, x, z) and stores it in the target quaternion
  3675. * @param yaw defines the rotation around Y axis
  3676. * @param pitch defines the rotation around X axis
  3677. * @param roll defines the rotation around Z axis
  3678. * @param result defines the target quaternion
  3679. */
  3680. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Quaternion): void {
  3681. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  3682. var halfRoll = roll * 0.5;
  3683. var halfPitch = pitch * 0.5;
  3684. var halfYaw = yaw * 0.5;
  3685. var sinRoll = Math.sin(halfRoll);
  3686. var cosRoll = Math.cos(halfRoll);
  3687. var sinPitch = Math.sin(halfPitch);
  3688. var cosPitch = Math.cos(halfPitch);
  3689. var sinYaw = Math.sin(halfYaw);
  3690. var cosYaw = Math.cos(halfYaw);
  3691. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  3692. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  3693. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  3694. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  3695. }
  3696. /**
  3697. * Creates a new quaternion from the given Euler float angles expressed in z-x-z orientation
  3698. * @param alpha defines the rotation around first axis
  3699. * @param beta defines the rotation around second axis
  3700. * @param gamma defines the rotation around third axis
  3701. * @returns the new quaternion
  3702. */
  3703. public static RotationAlphaBetaGamma(alpha: number, beta: number, gamma: number): Quaternion {
  3704. var result = new Quaternion();
  3705. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  3706. return result;
  3707. }
  3708. /**
  3709. * Creates a new quaternion from the given Euler float angles expressed in z-x-z orientation and stores it in the target quaternion
  3710. * @param alpha defines the rotation around first axis
  3711. * @param beta defines the rotation around second axis
  3712. * @param gamma defines the rotation around third axis
  3713. * @param result defines the target quaternion
  3714. */
  3715. public static RotationAlphaBetaGammaToRef(alpha: number, beta: number, gamma: number, result: Quaternion): void {
  3716. // Produces a quaternion from Euler angles in the z-x-z orientation
  3717. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  3718. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  3719. var halfBeta = beta * 0.5;
  3720. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  3721. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  3722. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  3723. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  3724. }
  3725. /**
  3726. * 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)
  3727. * @param axis1 defines the first axis
  3728. * @param axis2 defines the second axis
  3729. * @param axis3 defines the third axis
  3730. * @returns the new quaternion
  3731. */
  3732. public static RotationQuaternionFromAxis(axis1: Vector3, axis2: Vector3, axis3: Vector3): Quaternion {
  3733. var quat = new Quaternion(0.0, 0.0, 0.0, 0.0);
  3734. Quaternion.RotationQuaternionFromAxisToRef(axis1, axis2, axis3, quat);
  3735. return quat;
  3736. }
  3737. /**
  3738. * 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
  3739. * @param axis1 defines the first axis
  3740. * @param axis2 defines the second axis
  3741. * @param axis3 defines the third axis
  3742. * @param ref defines the target quaternion
  3743. */
  3744. public static RotationQuaternionFromAxisToRef(axis1: Vector3, axis2: Vector3, axis3: Vector3, ref: Quaternion): void {
  3745. var rotMat = MathTmp.Matrix[0];
  3746. Matrix.FromXYZAxesToRef(axis1.normalize(), axis2.normalize(), axis3.normalize(), rotMat);
  3747. Quaternion.FromRotationMatrixToRef(rotMat, ref);
  3748. }
  3749. /**
  3750. * Interpolates between two quaternions
  3751. * @param left defines first quaternion
  3752. * @param right defines second quaternion
  3753. * @param amount defines the gradient to use
  3754. * @returns the new interpolated quaternion
  3755. */
  3756. public static Slerp(left: Quaternion, right: Quaternion, amount: number): Quaternion {
  3757. var result = Quaternion.Identity();
  3758. Quaternion.SlerpToRef(left, right, amount, result);
  3759. return result;
  3760. }
  3761. /**
  3762. * Interpolates between two quaternions and stores it into a target quaternion
  3763. * @param left defines first quaternion
  3764. * @param right defines second quaternion
  3765. * @param amount defines the gradient to use
  3766. * @param result defines the target quaternion
  3767. */
  3768. public static SlerpToRef(left: Quaternion, right: Quaternion, amount: number, result: Quaternion): void {
  3769. var num2;
  3770. var num3;
  3771. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  3772. var flag = false;
  3773. if (num4 < 0) {
  3774. flag = true;
  3775. num4 = -num4;
  3776. }
  3777. if (num4 > 0.999999) {
  3778. num3 = 1 - amount;
  3779. num2 = flag ? -amount : amount;
  3780. }
  3781. else {
  3782. var num5 = Math.acos(num4);
  3783. var num6 = (1.0 / Math.sin(num5));
  3784. num3 = (Math.sin((1.0 - amount) * num5)) * num6;
  3785. num2 = flag ? ((-Math.sin(amount * num5)) * num6) : ((Math.sin(amount * num5)) * num6);
  3786. }
  3787. result.x = (num3 * left.x) + (num2 * right.x);
  3788. result.y = (num3 * left.y) + (num2 * right.y);
  3789. result.z = (num3 * left.z) + (num2 * right.z);
  3790. result.w = (num3 * left.w) + (num2 * right.w);
  3791. }
  3792. /**
  3793. * Interpolate between two quaternions using Hermite interpolation
  3794. * @param value1 defines first quaternion
  3795. * @param tangent1 defines the incoming tangent
  3796. * @param value2 defines second quaternion
  3797. * @param tangent2 defines the outgoing tangent
  3798. * @param amount defines the target quaternion
  3799. * @returns the new interpolated quaternion
  3800. */
  3801. public static Hermite(value1: Quaternion, tangent1: Quaternion, value2: Quaternion, tangent2: Quaternion, amount: number): Quaternion {
  3802. var squared = amount * amount;
  3803. var cubed = amount * squared;
  3804. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  3805. var part2 = (-2.0 * cubed) + (3.0 * squared);
  3806. var part3 = (cubed - (2.0 * squared)) + amount;
  3807. var part4 = cubed - squared;
  3808. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  3809. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  3810. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  3811. var w = (((value1.w * part1) + (value2.w * part2)) + (tangent1.w * part3)) + (tangent2.w * part4);
  3812. return new Quaternion(x, y, z, w);
  3813. }
  3814. }
  3815. /**
  3816. * Class used to store matrix data (4x4)
  3817. */
  3818. export class Matrix {
  3819. private static _updateFlagSeed = 0;
  3820. private static _identityReadOnly = Matrix.Identity();
  3821. private _isIdentity = false;
  3822. private _isIdentityDirty = true;
  3823. private _isIdentity3x2 = true;
  3824. private _isIdentity3x2Dirty = true;
  3825. /**
  3826. * Gets the update flag of the matrix which is an unique number for the matrix.
  3827. * It will be incremented every time the matrix data change.
  3828. * You can use it to speed the comparison between two versions of the same matrix.
  3829. */
  3830. public updateFlag: number;
  3831. private readonly _m: Float32Array = new Float32Array(16);
  3832. /**
  3833. * Gets the internal data of the matrix
  3834. */
  3835. public get m(): Readonly<Float32Array> { return this._m; }
  3836. /** @hidden */
  3837. public _markAsUpdated() {
  3838. this.updateFlag = Matrix._updateFlagSeed++;
  3839. this._isIdentity = false;
  3840. this._isIdentity3x2 = false;
  3841. this._isIdentityDirty = true;
  3842. this._isIdentity3x2Dirty = true;
  3843. }
  3844. /** @hidden */
  3845. private _updateIdentityStatus(isIdentity: boolean, isIdentityDirty: boolean = false, isIdentity3x2: boolean = false, isIdentity3x2Dirty : boolean= true) {
  3846. this.updateFlag = Matrix._updateFlagSeed++;
  3847. this._isIdentity = isIdentity;
  3848. this._isIdentity3x2 = isIdentity || isIdentity3x2;
  3849. this._isIdentityDirty = this._isIdentity ? false : isIdentityDirty;
  3850. this._isIdentity3x2Dirty = this._isIdentity3x2 ? false : isIdentity3x2Dirty;
  3851. }
  3852. /**
  3853. * Creates an empty matrix (filled with zeros)
  3854. */
  3855. public constructor() {
  3856. this._updateIdentityStatus(false);
  3857. }
  3858. // Properties
  3859. /**
  3860. * Check if the current matrix is identity
  3861. * @returns true is the matrix is the identity matrix
  3862. */
  3863. public isIdentity(): boolean {
  3864. if (this._isIdentityDirty) {
  3865. this._isIdentityDirty = false;
  3866. const m = this._m;
  3867. this._isIdentity = (
  3868. m[0] === 1.0 && m[1] === 0.0 && m[2] === 0.0 && m[3] === 0.0 &&
  3869. m[4] === 0.0 && m[5] === 1.0 && m[6] === 0.0 && m[7] === 0.0 &&
  3870. m[8] === 0.0 && m[9] === 0.0 && m[10] === 1.0 && m[11] === 0.0 &&
  3871. m[12] === 0.0 && m[13] === 0.0 && m[14] === 0.0 && m[15] === 1.0
  3872. );
  3873. }
  3874. return this._isIdentity;
  3875. }
  3876. /**
  3877. * Check if the current matrix is identity as a texture matrix (3x2 store in 4x4)
  3878. * @returns true is the matrix is the identity matrix
  3879. */
  3880. public isIdentityAs3x2(): boolean {
  3881. if (this._isIdentity3x2Dirty) {
  3882. this._isIdentity3x2Dirty = false;
  3883. if (this._m[0] !== 1.0 || this._m[5] !== 1.0 || this._m[15] !== 1.0) {
  3884. this._isIdentity3x2 = false;
  3885. } else if (this._m[1] !== 0.0 || this._m[2] !== 0.0 || this._m[3] !== 0.0 ||
  3886. this._m[4] !== 0.0 || this._m[6] !== 0.0 || this._m[7] !== 0.0 ||
  3887. this._m[8] !== 0.0 || this._m[9] !== 0.0 || this._m[10] !== 0.0 || this._m[11] !== 0.0 ||
  3888. this._m[12] !== 0.0 || this._m[13] !== 0.0 || this._m[14] !== 0.0) {
  3889. this._isIdentity3x2 = false;
  3890. } else {
  3891. this._isIdentity3x2 = true;
  3892. }
  3893. }
  3894. return this._isIdentity3x2;
  3895. }
  3896. /**
  3897. * Gets the determinant of the matrix
  3898. * @returns the matrix determinant
  3899. */
  3900. public determinant(): number {
  3901. if (this._isIdentity === true) {
  3902. return 1;
  3903. }
  3904. const m = this._m;
  3905. const m00 = m[0], m01 = m[1], m02 = m[2], m03 = m[3];
  3906. const m10 = m[4], m11 = m[5], m12 = m[6], m13 = m[7];
  3907. const m20 = m[8], m21 = m[9], m22 = m[10], m23 = m[11];
  3908. const m30 = m[12], m31 = m[13], m32 = m[14], m33 = m[15];
  3909. // https://en.wikipedia.org/wiki/Laplace_expansion
  3910. // to compute the deterrminant of a 4x4 Matrix we compute the cofactors of any row or column,
  3911. // then we multiply each Cofactor by its corresponding matrix value and sum them all to get the determinant
  3912. // Cofactor(i, j) = sign(i,j) * det(Minor(i, j))
  3913. // where
  3914. // - sign(i,j) = (i+j) % 2 === 0 ? 1 : -1
  3915. // - Minor(i, j) is the 3x3 matrix we get by removing row i and column j from current Matrix
  3916. //
  3917. // Here we do that for the 1st row.
  3918. const det_22_33 = m22 * m33 - m32 * m23;
  3919. const det_21_33 = m21 * m33 - m31 * m23;
  3920. const det_21_32 = m21 * m32 - m31 * m22;
  3921. const det_20_33 = m20 * m33 - m30 * m23;
  3922. const det_20_32 = m20 * m32 - m22 * m30;
  3923. const det_20_31 = m20 * m31 - m30 * m21;
  3924. const cofact_00 = +(m11 * det_22_33 - m12 * det_21_33 + m13 * det_21_32);
  3925. const cofact_01 = -(m10 * det_22_33 - m12 * det_20_33 + m13 * det_20_32);
  3926. const cofact_02 = +(m10 * det_21_33 - m11 * det_20_33 + m13 * det_20_31);
  3927. const cofact_03 = -(m10 * det_21_32 - m11 * det_20_32 + m12 * det_20_31);
  3928. return m00 * cofact_00 + m01 * cofact_01 + m02 * cofact_02 + m03 * cofact_03;
  3929. }
  3930. // Methods
  3931. /**
  3932. * Returns the matrix as a Float32Array
  3933. * @returns the matrix underlying array
  3934. */
  3935. public toArray(): Readonly<Float32Array> {
  3936. return this._m;
  3937. }
  3938. /**
  3939. * Returns the matrix as a Float32Array
  3940. * @returns the matrix underlying array.
  3941. */
  3942. public asArray(): Readonly<Float32Array> {
  3943. return this._m;
  3944. }
  3945. /**
  3946. * Inverts the current matrix in place
  3947. * @returns the current inverted matrix
  3948. */
  3949. public invert(): Matrix {
  3950. this.invertToRef(this);
  3951. return this;
  3952. }
  3953. /**
  3954. * Sets all the matrix elements to zero
  3955. * @returns the current matrix
  3956. */
  3957. public reset(): Matrix {
  3958. Matrix.FromValuesToRef(
  3959. 0.0, 0.0, 0.0, 0.0,
  3960. 0.0, 0.0, 0.0, 0.0,
  3961. 0.0, 0.0, 0.0, 0.0,
  3962. 0.0, 0.0, 0.0, 0.0,
  3963. this
  3964. );
  3965. this._updateIdentityStatus(false);
  3966. return this;
  3967. }
  3968. /**
  3969. * Adds the current matrix with a second one
  3970. * @param other defines the matrix to add
  3971. * @returns a new matrix as the addition of the current matrix and the given one
  3972. */
  3973. public add(other: Matrix): Matrix {
  3974. var result = new Matrix();
  3975. this.addToRef(other, result);
  3976. return result;
  3977. }
  3978. /**
  3979. * Sets the given matrix "result" to the addition of the current matrix and the given one
  3980. * @param other defines the matrix to add
  3981. * @param result defines the target matrix
  3982. * @returns the current matrix
  3983. */
  3984. public addToRef(other: Matrix, result: Matrix): Matrix {
  3985. for (var index = 0; index < 16; index++) {
  3986. result._m[index] = this._m[index] + other._m[index];
  3987. }
  3988. result._markAsUpdated();
  3989. return this;
  3990. }
  3991. /**
  3992. * Adds in place the given matrix to the current matrix
  3993. * @param other defines the second operand
  3994. * @returns the current updated matrix
  3995. */
  3996. public addToSelf(other: Matrix): Matrix {
  3997. for (var index = 0; index < 16; index++) {
  3998. this._m[index] += other._m[index];
  3999. }
  4000. this._markAsUpdated();
  4001. return this;
  4002. }
  4003. /**
  4004. * Sets the given matrix to the current inverted Matrix
  4005. * @param other defines the target matrix
  4006. * @returns the unmodified current matrix
  4007. */
  4008. public invertToRef(other: Matrix): Matrix {
  4009. if (this._isIdentity === true) {
  4010. Matrix.IdentityToRef(other);
  4011. return this;
  4012. }
  4013. // the inverse of a Matrix is the transpose of cofactor matrix divided by the determinant
  4014. const m = this._m;
  4015. const m00 = m[0], m01 = m[1], m02 = m[2], m03 = m[3];
  4016. const m10 = m[4], m11 = m[5], m12 = m[6], m13 = m[7];
  4017. const m20 = m[8], m21 = m[9], m22 = m[10], m23 = m[11];
  4018. const m30 = m[12], m31 = m[13], m32 = m[14], m33 = m[15];
  4019. const det_22_33 = m22 * m33 - m32 * m23;
  4020. const det_21_33 = m21 * m33 - m31 * m23;
  4021. const det_21_32 = m21 * m32 - m31 * m22;
  4022. const det_20_33 = m20 * m33 - m30 * m23;
  4023. const det_20_32 = m20 * m32 - m22 * m30;
  4024. const det_20_31 = m20 * m31 - m30 * m21;
  4025. const cofact_00 = +(m11 * det_22_33 - m12 * det_21_33 + m13 * det_21_32);
  4026. const cofact_01 = -(m10 * det_22_33 - m12 * det_20_33 + m13 * det_20_32);
  4027. const cofact_02 = +(m10 * det_21_33 - m11 * det_20_33 + m13 * det_20_31);
  4028. const cofact_03 = -(m10 * det_21_32 - m11 * det_20_32 + m12 * det_20_31);
  4029. const det = m00 * cofact_00 + m01 * cofact_01 + m02 * cofact_02 + m03 * cofact_03;
  4030. if (det === 0) {
  4031. // not invertible
  4032. other.copyFrom(this);
  4033. return this;
  4034. }
  4035. const detInv = 1 / det;
  4036. const det_12_33 = m12 * m33 - m32 * m13;
  4037. const det_11_33 = m11 * m33 - m31 * m13;
  4038. const det_11_32 = m11 * m32 - m31 * m12;
  4039. const det_10_33 = m10 * m33 - m30 * m13;
  4040. const det_10_32 = m10 * m32 - m30 * m12;
  4041. const det_10_31 = m10 * m31 - m30 * m11;
  4042. const det_12_23 = m12 * m23 - m22 * m13;
  4043. const det_11_23 = m11 * m23 - m21 * m13;
  4044. const det_11_22 = m11 * m22 - m21 * m12;
  4045. const det_10_23 = m10 * m23 - m20 * m13;
  4046. const det_10_22 = m10 * m22 - m20 * m12;
  4047. const det_10_21 = m10 * m21 - m20 * m11;
  4048. const cofact_10 = -(m01 * det_22_33 - m02 * det_21_33 + m03 * det_21_32);
  4049. const cofact_11 = +(m00 * det_22_33 - m02 * det_20_33 + m03 * det_20_32);
  4050. const cofact_12 = -(m00 * det_21_33 - m01 * det_20_33 + m03 * det_20_31);
  4051. const cofact_13 = +(m00 * det_21_32 - m01 * det_20_32 + m02 * det_20_31);
  4052. const cofact_20 = +(m01 * det_12_33 - m02 * det_11_33 + m03 * det_11_32);
  4053. const cofact_21 = -(m00 * det_12_33 - m02 * det_10_33 + m03 * det_10_32);
  4054. const cofact_22 = +(m00 * det_11_33 - m01 * det_10_33 + m03 * det_10_31);
  4055. const cofact_23 = -(m00 * det_11_32 - m01 * det_10_32 + m02 * det_10_31);
  4056. const cofact_30 = -(m01 * det_12_23 - m02 * det_11_23 + m03 * det_11_22);
  4057. const cofact_31 = +(m00 * det_12_23 - m02 * det_10_23 + m03 * det_10_22);
  4058. const cofact_32 = -(m00 * det_11_23 - m01 * det_10_23 + m03 * det_10_21);
  4059. const cofact_33 = +(m00 * det_11_22 - m01 * det_10_22 + m02 * det_10_21);
  4060. Matrix.FromValuesToRef(
  4061. cofact_00 * detInv, cofact_10 * detInv, cofact_20 * detInv, cofact_30 * detInv,
  4062. cofact_01 * detInv, cofact_11 * detInv, cofact_21 * detInv, cofact_31 * detInv,
  4063. cofact_02 * detInv, cofact_12 * detInv, cofact_22 * detInv, cofact_32 * detInv,
  4064. cofact_03 * detInv, cofact_13 * detInv, cofact_23 * detInv, cofact_33 * detInv,
  4065. other
  4066. );
  4067. return this;
  4068. }
  4069. /**
  4070. * add a value at the specified position in the current Matrix
  4071. * @param index the index of the value within the matrix. between 0 and 15.
  4072. * @param value the value to be added
  4073. * @returns the current updated matrix
  4074. */
  4075. public addAtIndex(index: number, value: number): Matrix {
  4076. this._m[index] += value;
  4077. this._markAsUpdated();
  4078. return this;
  4079. }
  4080. /**
  4081. * mutiply the specified position in the current Matrix by a value
  4082. * @param index the index of the value within the matrix. between 0 and 15.
  4083. * @param value the value to be added
  4084. * @returns the current updated matrix
  4085. */
  4086. public multiplyAtIndex(index: number, value: number): Matrix {
  4087. this._m[index] *= value;
  4088. this._markAsUpdated();
  4089. return this;
  4090. }
  4091. /**
  4092. * Inserts the translation vector (using 3 floats) in the current matrix
  4093. * @param x defines the 1st component of the translation
  4094. * @param y defines the 2nd component of the translation
  4095. * @param z defines the 3rd component of the translation
  4096. * @returns the current updated matrix
  4097. */
  4098. public setTranslationFromFloats(x: number, y: number, z: number): Matrix {
  4099. this._m[12] = x;
  4100. this._m[13] = y;
  4101. this._m[14] = z;
  4102. this._markAsUpdated();
  4103. return this;
  4104. }
  4105. /**
  4106. * Inserts the translation vector in the current matrix
  4107. * @param vector3 defines the translation to insert
  4108. * @returns the current updated matrix
  4109. */
  4110. public setTranslation(vector3: Vector3): Matrix {
  4111. return this.setTranslationFromFloats(vector3.x, vector3.y, vector3.z);
  4112. }
  4113. /**
  4114. * Gets the translation value of the current matrix
  4115. * @returns a new Vector3 as the extracted translation from the matrix
  4116. */
  4117. public getTranslation(): Vector3 {
  4118. return new Vector3(this._m[12], this._m[13], this._m[14]);
  4119. }
  4120. /**
  4121. * Fill a Vector3 with the extracted translation from the matrix
  4122. * @param result defines the Vector3 where to store the translation
  4123. * @returns the current matrix
  4124. */
  4125. public getTranslationToRef(result: Vector3): Matrix {
  4126. result.x = this._m[12];
  4127. result.y = this._m[13];
  4128. result.z = this._m[14];
  4129. return this;
  4130. }
  4131. /**
  4132. * Remove rotation and scaling part from the matrix
  4133. * @returns the updated matrix
  4134. */
  4135. public removeRotationAndScaling(): Matrix {
  4136. const m = this.m;
  4137. Matrix.FromValuesToRef(
  4138. 1.0, 0.0, 0.0, 0.0,
  4139. 0.0, 1.0, 0.0, 0.0,
  4140. 0.0, 0.0, 1.0, 0.0,
  4141. m[12], m[13], m[14], m[15],
  4142. this
  4143. );
  4144. this._updateIdentityStatus(m[12] === 0 && m[13] === 0 && m[14] === 0 && m[15] === 1);
  4145. return this;
  4146. }
  4147. /**
  4148. * Multiply two matrices
  4149. * @param other defines the second operand
  4150. * @returns a new matrix set with the multiplication result of the current Matrix and the given one
  4151. */
  4152. public multiply(other: Readonly<Matrix>): Matrix {
  4153. var result = new Matrix();
  4154. this.multiplyToRef(other, result);
  4155. return result;
  4156. }
  4157. /**
  4158. * Copy the current matrix from the given one
  4159. * @param other defines the source matrix
  4160. * @returns the current updated matrix
  4161. */
  4162. public copyFrom(other: Readonly<Matrix>): Matrix {
  4163. other.copyToArray(this._m);
  4164. const o = (other as Matrix);
  4165. this._updateIdentityStatus(o._isIdentity, o._isIdentityDirty, o._isIdentity3x2, o._isIdentity3x2Dirty);
  4166. return this;
  4167. }
  4168. /**
  4169. * Populates the given array from the starting index with the current matrix values
  4170. * @param array defines the target array
  4171. * @param offset defines the offset in the target array where to start storing values
  4172. * @returns the current matrix
  4173. */
  4174. public copyToArray(array: Float32Array, offset: number = 0): Matrix {
  4175. for (var index = 0; index < 16; index++) {
  4176. array[offset + index] = this._m[index];
  4177. }
  4178. return this;
  4179. }
  4180. /**
  4181. * Sets the given matrix "result" with the multiplication result of the current Matrix and the given one
  4182. * @param other defines the second operand
  4183. * @param result defines the matrix where to store the multiplication
  4184. * @returns the current matrix
  4185. */
  4186. public multiplyToRef(other: Readonly<Matrix>, result: Matrix): Matrix {
  4187. if (this._isIdentity) {
  4188. result.copyFrom(other);
  4189. return this;
  4190. }
  4191. if ((other as Matrix)._isIdentity) {
  4192. result.copyFrom(this);
  4193. return this;
  4194. }
  4195. this.multiplyToArray(other, result._m, 0);
  4196. result._markAsUpdated();
  4197. return this;
  4198. }
  4199. /**
  4200. * Sets the Float32Array "result" from the given index "offset" with the multiplication of the current matrix and the given one
  4201. * @param other defines the second operand
  4202. * @param result defines the array where to store the multiplication
  4203. * @param offset defines the offset in the target array where to start storing values
  4204. * @returns the current matrix
  4205. */
  4206. public multiplyToArray(other: Readonly<Matrix>, result: Float32Array, offset: number): Matrix {
  4207. const m = this._m;
  4208. const otherM = other.m;
  4209. var tm0 = m[0], tm1 = m[1], tm2 = m[2], tm3 = m[3];
  4210. var tm4 = m[4], tm5 = m[5], tm6 = m[6], tm7 = m[7];
  4211. var tm8 = m[8], tm9 = m[9], tm10 = m[10], tm11 = m[11];
  4212. var tm12 = m[12], tm13 = m[13], tm14 = m[14], tm15 = m[15];
  4213. var om0 = otherM[0], om1 = otherM[1], om2 = otherM[2], om3 = otherM[3];
  4214. var om4 = otherM[4], om5 = otherM[5], om6 = otherM[6], om7 = otherM[7];
  4215. var om8 = otherM[8], om9 = otherM[9], om10 = otherM[10], om11 = otherM[11];
  4216. var om12 = otherM[12], om13 = otherM[13], om14 = otherM[14], om15 = otherM[15];
  4217. result[offset ] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  4218. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  4219. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  4220. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  4221. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  4222. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  4223. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  4224. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  4225. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  4226. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  4227. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  4228. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  4229. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  4230. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  4231. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  4232. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  4233. return this;
  4234. }
  4235. /**
  4236. * Check equality between this matrix and a second one
  4237. * @param value defines the second matrix to compare
  4238. * @returns true is the current matrix and the given one values are strictly equal
  4239. */
  4240. public equals(value: Matrix): boolean {
  4241. const other = (value as Matrix);
  4242. if (!other) {
  4243. return false;
  4244. }
  4245. if (this._isIdentity || other._isIdentity) {
  4246. if (!this._isIdentityDirty && !other._isIdentityDirty) {
  4247. return this._isIdentity && other._isIdentity;
  4248. }
  4249. }
  4250. const m = this.m;
  4251. const om = other.m;
  4252. return (
  4253. m[0] === om[0] && m[1] === om[1] && m[2] === om[2] && m[3] === om[3] &&
  4254. m[4] === om[4] && m[5] === om[5] && m[6] === om[6] && m[7] === om[7] &&
  4255. m[8] === om[8] && m[9] === om[9] && m[10] === om[10] && m[11] === om[11] &&
  4256. m[12] === om[12] && m[13] === om[13] && m[14] === om[14] && m[15] === om[15]
  4257. );
  4258. }
  4259. /**
  4260. * Clone the current matrix
  4261. * @returns a new matrix from the current matrix
  4262. */
  4263. public clone(): Matrix {
  4264. const matrix = new Matrix();
  4265. matrix.copyFrom(this);
  4266. return matrix;
  4267. }
  4268. /**
  4269. * Returns the name of the current matrix class
  4270. * @returns the string "Matrix"
  4271. */
  4272. public getClassName(): string {
  4273. return "Matrix";
  4274. }
  4275. /**
  4276. * Gets the hash code of the current matrix
  4277. * @returns the hash code
  4278. */
  4279. public getHashCode(): number {
  4280. let hash = this._m[0] || 0;
  4281. for (let i = 1; i < 16; i++) {
  4282. hash = (hash * 397) ^ (this._m[i] || 0);
  4283. }
  4284. return hash;
  4285. }
  4286. /**
  4287. * Decomposes the current Matrix into a translation, rotation and scaling components
  4288. * @param scale defines the scale vector3 given as a reference to update
  4289. * @param rotation defines the rotation quaternion given as a reference to update
  4290. * @param translation defines the translation vector3 given as a reference to update
  4291. * @returns true if operation was successful
  4292. */
  4293. public decompose(scale?: Vector3, rotation?: Quaternion, translation?: Vector3): boolean {
  4294. if (this._isIdentity) {
  4295. if (translation) {
  4296. translation.setAll(0);
  4297. }
  4298. if (scale) {
  4299. scale.setAll(1);
  4300. }
  4301. if (rotation) {
  4302. rotation.copyFromFloats(0, 0, 0, 1);
  4303. }
  4304. return true;
  4305. }
  4306. const m = this._m;
  4307. if (translation) {
  4308. translation.copyFromFloats(m[12], m[13], m[14]);
  4309. }
  4310. scale = scale || MathTmp.Vector3[0];
  4311. scale.x = Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
  4312. scale.y = Math.sqrt(m[4] * m[4] + m[5] * m[5] + m[6] * m[6]);
  4313. scale.z = Math.sqrt(m[8] * m[8] + m[9] * m[9] + m[10] * m[10]);
  4314. if (this.determinant() <= 0) {
  4315. scale.y *= -1;
  4316. }
  4317. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  4318. if (rotation) {
  4319. rotation.copyFromFloats(0.0, 0.0, 0.0, 1.0);
  4320. }
  4321. return false;
  4322. }
  4323. if (rotation) {
  4324. const sx = 1 / scale.x, sy = 1 / scale.y, sz = 1 / scale.z;
  4325. Matrix.FromValuesToRef(
  4326. m[0] * sx, m[1] * sx, m[2] * sx, 0.0,
  4327. m[4] * sy, m[5] * sy, m[6] * sy, 0.0,
  4328. m[8] * sz, m[9] * sz, m[10] * sz, 0.0,
  4329. 0.0, 0.0, 0.0, 1.0,
  4330. MathTmp.Matrix[0]
  4331. );
  4332. Quaternion.FromRotationMatrixToRef(MathTmp.Matrix[0], rotation);
  4333. }
  4334. return true;
  4335. }
  4336. /**
  4337. * Gets specific row of the matrix
  4338. * @param index defines the number of the row to get
  4339. * @returns the index-th row of the current matrix as a new Vector4
  4340. */
  4341. public getRow(index: number): Nullable<Vector4> {
  4342. if (index < 0 || index > 3) {
  4343. return null;
  4344. }
  4345. var i = index * 4;
  4346. return new Vector4(this._m[i + 0], this._m[i + 1], this._m[i + 2], this._m[i + 3]);
  4347. }
  4348. /**
  4349. * Sets the index-th row of the current matrix to the vector4 values
  4350. * @param index defines the number of the row to set
  4351. * @param row defines the target vector4
  4352. * @returns the updated current matrix
  4353. */
  4354. public setRow(index: number, row: Vector4): Matrix {
  4355. return this.setRowFromFloats(index, row.x, row.y, row.z, row.w);
  4356. }
  4357. /**
  4358. * Compute the transpose of the matrix
  4359. * @returns the new transposed matrix
  4360. */
  4361. public transpose(): Matrix {
  4362. return Matrix.Transpose(this);
  4363. }
  4364. /**
  4365. * Compute the transpose of the matrix and store it in a given matrix
  4366. * @param result defines the target matrix
  4367. * @returns the current matrix
  4368. */
  4369. public transposeToRef(result: Matrix): Matrix {
  4370. Matrix.TransposeToRef(this, result);
  4371. return this;
  4372. }
  4373. /**
  4374. * Sets the index-th row of the current matrix with the given 4 x float values
  4375. * @param index defines the row index
  4376. * @param x defines the x component to set
  4377. * @param y defines the y component to set
  4378. * @param z defines the z component to set
  4379. * @param w defines the w component to set
  4380. * @returns the updated current matrix
  4381. */
  4382. public setRowFromFloats(index: number, x: number, y: number, z: number, w: number): Matrix {
  4383. if (index < 0 || index > 3) {
  4384. return this;
  4385. }
  4386. var i = index * 4;
  4387. this._m[i + 0] = x;
  4388. this._m[i + 1] = y;
  4389. this._m[i + 2] = z;
  4390. this._m[i + 3] = w;
  4391. this._markAsUpdated();
  4392. return this;
  4393. }
  4394. /**
  4395. * Compute a new matrix set with the current matrix values multiplied by scale (float)
  4396. * @param scale defines the scale factor
  4397. * @returns a new matrix
  4398. */
  4399. public scale(scale: number): Matrix {
  4400. var result = new Matrix();
  4401. this.scaleToRef(scale, result);
  4402. return result;
  4403. }
  4404. /**
  4405. * Scale the current matrix values by a factor to a given result matrix
  4406. * @param scale defines the scale factor
  4407. * @param result defines the matrix to store the result
  4408. * @returns the current matrix
  4409. */
  4410. public scaleToRef(scale: number, result: Matrix): Matrix {
  4411. for (var index = 0; index < 16; index++) {
  4412. result._m[index] = this._m[index] * scale;
  4413. }
  4414. result._markAsUpdated();
  4415. return this;
  4416. }
  4417. /**
  4418. * Scale the current matrix values by a factor and add the result to a given matrix
  4419. * @param scale defines the scale factor
  4420. * @param result defines the Matrix to store the result
  4421. * @returns the current matrix
  4422. */
  4423. public scaleAndAddToRef(scale: number, result: Matrix): Matrix {
  4424. for (var index = 0; index < 16; index++) {
  4425. result._m[index] += this._m[index] * scale;
  4426. }
  4427. result._markAsUpdated();
  4428. return this;
  4429. }
  4430. /**
  4431. * Writes to the given matrix a normal matrix, computed from this one (using values from identity matrix for fourth row and column).
  4432. * @param ref matrix to store the result
  4433. */
  4434. public toNormalMatrix(ref: Matrix): void {
  4435. const tmp = MathTmp.Matrix[0];
  4436. this.invertToRef(tmp);
  4437. tmp.transposeToRef(ref);
  4438. var m = ref._m;
  4439. Matrix.FromValuesToRef(
  4440. m[0], m[1], m[2], 0.0,
  4441. m[4], m[5], m[6], 0.0,
  4442. m[8], m[9], m[10], 0.0,
  4443. 0.0, 0.0, 0.0, 1.0,
  4444. ref
  4445. );
  4446. }
  4447. /**
  4448. * Gets only rotation part of the current matrix
  4449. * @returns a new matrix sets to the extracted rotation matrix from the current one
  4450. */
  4451. public getRotationMatrix(): Matrix {
  4452. var result = new Matrix();
  4453. this.getRotationMatrixToRef(result);
  4454. return result;
  4455. }
  4456. /**
  4457. * Extracts the rotation matrix from the current one and sets it as the given "result"
  4458. * @param result defines the target matrix to store data to
  4459. * @returns the current matrix
  4460. */
  4461. public getRotationMatrixToRef(result: Matrix): Matrix {
  4462. const scale = MathTmp.Vector3[0];
  4463. if (!this.decompose(scale)) {
  4464. Matrix.IdentityToRef(result);
  4465. return this;
  4466. }
  4467. const m = this._m;
  4468. const sx = 1 / scale.x, sy = 1 / scale.y, sz = 1 / scale.z;
  4469. Matrix.FromValuesToRef(
  4470. m[0] * sx, m[1] * sx, m[2] * sx, 0.0,
  4471. m[4] * sy, m[5] * sy, m[6] * sy, 0.0,
  4472. m[8] * sz, m[9] * sz, m[10] * sz, 0.0,
  4473. 0.0, 0.0, 0.0, 1.0,
  4474. result
  4475. );
  4476. return this;
  4477. }
  4478. /**
  4479. * Toggles model matrix from being right handed to left handed in place and vice versa
  4480. */
  4481. public toggleModelMatrixHandInPlace() {
  4482. const m = this._m;
  4483. m[2] *= -1;
  4484. m[6] *= -1;
  4485. m[8] *= -1;
  4486. m[9] *= -1;
  4487. m[14] *= -1;
  4488. this._markAsUpdated();
  4489. }
  4490. /**
  4491. * Toggles projection matrix from being right handed to left handed in place and vice versa
  4492. */
  4493. public toggleProjectionMatrixHandInPlace() {
  4494. var m = this._m;
  4495. m[8] *= -1;
  4496. m[9] *= -1;
  4497. m[10] *= -1;
  4498. m[11] *= -1;
  4499. this._markAsUpdated();
  4500. }
  4501. // Statics
  4502. /**
  4503. * Creates a matrix from an array
  4504. * @param array defines the source array
  4505. * @param offset defines an offset in the source array
  4506. * @returns a new Matrix set from the starting index of the given array
  4507. */
  4508. public static FromArray(array: ArrayLike<number>, offset: number = 0): Matrix {
  4509. var result = new Matrix();
  4510. Matrix.FromArrayToRef(array, offset, result);
  4511. return result;
  4512. }
  4513. /**
  4514. * Copy the content of an array into a given matrix
  4515. * @param array defines the source array
  4516. * @param offset defines an offset in the source array
  4517. * @param result defines the target matrix
  4518. */
  4519. public static FromArrayToRef(array: ArrayLike<number>, offset: number, result: Matrix) {
  4520. for (var index = 0; index < 16; index++) {
  4521. result._m[index] = array[index + offset];
  4522. }
  4523. result._markAsUpdated();
  4524. }
  4525. /**
  4526. * Stores an array into a matrix after having multiplied each component by a given factor
  4527. * @param array defines the source array
  4528. * @param offset defines the offset in the source array
  4529. * @param scale defines the scaling factor
  4530. * @param result defines the target matrix
  4531. */
  4532. public static FromFloat32ArrayToRefScaled(array: Float32Array, offset: number, scale: number, result: Matrix) {
  4533. for (var index = 0; index < 16; index++) {
  4534. result._m[index] = array[index + offset] * scale;
  4535. }
  4536. result._markAsUpdated();
  4537. }
  4538. /**
  4539. * Gets an identity matrix that must not be updated
  4540. */
  4541. public static get IdentityReadOnly(): Readonly<Matrix> {
  4542. return Matrix._identityReadOnly;
  4543. }
  4544. /**
  4545. * Stores a list of values (16) inside a given matrix
  4546. * @param initialM11 defines 1st value of 1st row
  4547. * @param initialM12 defines 2nd value of 1st row
  4548. * @param initialM13 defines 3rd value of 1st row
  4549. * @param initialM14 defines 4th value of 1st row
  4550. * @param initialM21 defines 1st value of 2nd row
  4551. * @param initialM22 defines 2nd value of 2nd row
  4552. * @param initialM23 defines 3rd value of 2nd row
  4553. * @param initialM24 defines 4th value of 2nd row
  4554. * @param initialM31 defines 1st value of 3rd row
  4555. * @param initialM32 defines 2nd value of 3rd row
  4556. * @param initialM33 defines 3rd value of 3rd row
  4557. * @param initialM34 defines 4th value of 3rd row
  4558. * @param initialM41 defines 1st value of 4th row
  4559. * @param initialM42 defines 2nd value of 4th row
  4560. * @param initialM43 defines 3rd value of 4th row
  4561. * @param initialM44 defines 4th value of 4th row
  4562. * @param result defines the target matrix
  4563. */
  4564. public static FromValuesToRef(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  4565. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  4566. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  4567. initialM41: number, initialM42: number, initialM43: number, initialM44: number, result: Matrix): void {
  4568. const m = result._m;
  4569. m[0] = initialM11; m[1] = initialM12; m[2] = initialM13; m[3] = initialM14;
  4570. m[4] = initialM21; m[5] = initialM22; m[6] = initialM23; m[7] = initialM24;
  4571. m[8] = initialM31; m[9] = initialM32; m[10] = initialM33; m[11] = initialM34;
  4572. m[12] = initialM41; m[13] = initialM42; m[14] = initialM43; m[15] = initialM44;
  4573. result._markAsUpdated();
  4574. }
  4575. /**
  4576. * Creates new matrix from a list of values (16)
  4577. * @param initialM11 defines 1st value of 1st row
  4578. * @param initialM12 defines 2nd value of 1st row
  4579. * @param initialM13 defines 3rd value of 1st row
  4580. * @param initialM14 defines 4th value of 1st row
  4581. * @param initialM21 defines 1st value of 2nd row
  4582. * @param initialM22 defines 2nd value of 2nd row
  4583. * @param initialM23 defines 3rd value of 2nd row
  4584. * @param initialM24 defines 4th value of 2nd row
  4585. * @param initialM31 defines 1st value of 3rd row
  4586. * @param initialM32 defines 2nd value of 3rd row
  4587. * @param initialM33 defines 3rd value of 3rd row
  4588. * @param initialM34 defines 4th value of 3rd row
  4589. * @param initialM41 defines 1st value of 4th row
  4590. * @param initialM42 defines 2nd value of 4th row
  4591. * @param initialM43 defines 3rd value of 4th row
  4592. * @param initialM44 defines 4th value of 4th row
  4593. * @returns the new matrix
  4594. */
  4595. public static FromValues(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  4596. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  4597. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  4598. initialM41: number, initialM42: number, initialM43: number, initialM44: number): Matrix {
  4599. var result = new Matrix();
  4600. const m = result._m;
  4601. m[0] = initialM11; m[1] = initialM12; m[2] = initialM13; m[3] = initialM14;
  4602. m[4] = initialM21; m[5] = initialM22; m[6] = initialM23; m[7] = initialM24;
  4603. m[8] = initialM31; m[9] = initialM32; m[10] = initialM33; m[11] = initialM34;
  4604. m[12] = initialM41; m[13] = initialM42; m[14] = initialM43; m[15] = initialM44;
  4605. result._markAsUpdated();
  4606. return result;
  4607. }
  4608. /**
  4609. * Creates a new matrix composed by merging scale (vector3), rotation (quaternion) and translation (vector3)
  4610. * @param scale defines the scale vector3
  4611. * @param rotation defines the rotation quaternion
  4612. * @param translation defines the translation vector3
  4613. * @returns a new matrix
  4614. */
  4615. public static Compose(scale: Vector3, rotation: Quaternion, translation: Vector3): Matrix {
  4616. var result = new Matrix();
  4617. Matrix.ComposeToRef(scale, rotation, translation, result);
  4618. return result;
  4619. }
  4620. /**
  4621. * Sets a matrix to a value composed by merging scale (vector3), rotation (quaternion) and translation (vector3)
  4622. * @param scale defines the scale vector3
  4623. * @param rotation defines the rotation quaternion
  4624. * @param translation defines the translation vector3
  4625. * @param result defines the target matrix
  4626. */
  4627. public static ComposeToRef(scale: Vector3, rotation: Quaternion, translation: Vector3, result: Matrix): void {
  4628. Matrix.ScalingToRef(scale.x, scale.y, scale.z, MathTmp.Matrix[1]);
  4629. rotation.toRotationMatrix(MathTmp.Matrix[0]);
  4630. MathTmp.Matrix[1].multiplyToRef(MathTmp.Matrix[0], result);
  4631. result.setTranslation(translation);
  4632. }
  4633. /**
  4634. * Creates a new identity matrix
  4635. * @returns a new identity matrix
  4636. */
  4637. public static Identity(): Matrix {
  4638. const identity = Matrix.FromValues(
  4639. 1.0, 0.0, 0.0, 0.0,
  4640. 0.0, 1.0, 0.0, 0.0,
  4641. 0.0, 0.0, 1.0, 0.0,
  4642. 0.0, 0.0, 0.0, 1.0);
  4643. identity._updateIdentityStatus(true);
  4644. return identity;
  4645. }
  4646. /**
  4647. * Creates a new identity matrix and stores the result in a given matrix
  4648. * @param result defines the target matrix
  4649. */
  4650. public static IdentityToRef(result: Matrix): void {
  4651. Matrix.FromValuesToRef(
  4652. 1.0, 0.0, 0.0, 0.0,
  4653. 0.0, 1.0, 0.0, 0.0,
  4654. 0.0, 0.0, 1.0, 0.0,
  4655. 0.0, 0.0, 0.0, 1.0,
  4656. result
  4657. );
  4658. result._updateIdentityStatus(true);
  4659. }
  4660. /**
  4661. * Creates a new zero matrix
  4662. * @returns a new zero matrix
  4663. */
  4664. public static Zero(): Matrix {
  4665. const zero = Matrix.FromValues(
  4666. 0.0, 0.0, 0.0, 0.0,
  4667. 0.0, 0.0, 0.0, 0.0,
  4668. 0.0, 0.0, 0.0, 0.0,
  4669. 0.0, 0.0, 0.0, 0.0);
  4670. zero._updateIdentityStatus(false);
  4671. return zero;
  4672. }
  4673. /**
  4674. * Creates a new rotation matrix for "angle" radians around the X axis
  4675. * @param angle defines the angle (in radians) to use
  4676. * @return the new matrix
  4677. */
  4678. public static RotationX(angle: number): Matrix {
  4679. var result = new Matrix();
  4680. Matrix.RotationXToRef(angle, result);
  4681. return result;
  4682. }
  4683. /**
  4684. * Creates a new matrix as the invert of a given matrix
  4685. * @param source defines the source matrix
  4686. * @returns the new matrix
  4687. */
  4688. public static Invert(source: Matrix): Matrix {
  4689. var result = new Matrix();
  4690. source.invertToRef(result);
  4691. return result;
  4692. }
  4693. /**
  4694. * Creates a new rotation matrix for "angle" radians around the X axis and stores it in a given matrix
  4695. * @param angle defines the angle (in radians) to use
  4696. * @param result defines the target matrix
  4697. */
  4698. public static RotationXToRef(angle: number, result: Matrix): void {
  4699. var s = Math.sin(angle);
  4700. var c = Math.cos(angle);
  4701. Matrix.FromValuesToRef(
  4702. 1.0, 0.0, 0.0, 0.0,
  4703. 0.0, c, s, 0.0,
  4704. 0.0, -s, c, 0.0,
  4705. 0.0, 0.0, 0.0, 1.0,
  4706. result
  4707. );
  4708. result._updateIdentityStatus(c === 1 && s === 0);
  4709. }
  4710. /**
  4711. * Creates a new rotation matrix for "angle" radians around the Y axis
  4712. * @param angle defines the angle (in radians) to use
  4713. * @return the new matrix
  4714. */
  4715. public static RotationY(angle: number): Matrix {
  4716. var result = new Matrix();
  4717. Matrix.RotationYToRef(angle, result);
  4718. return result;
  4719. }
  4720. /**
  4721. * Creates a new rotation matrix for "angle" radians around the Y axis and stores it in a given matrix
  4722. * @param angle defines the angle (in radians) to use
  4723. * @param result defines the target matrix
  4724. */
  4725. public static RotationYToRef(angle: number, result: Matrix): void {
  4726. var s = Math.sin(angle);
  4727. var c = Math.cos(angle);
  4728. Matrix.FromValuesToRef(
  4729. c, 0.0, -s, 0.0,
  4730. 0.0, 1.0, 0.0, 0.0,
  4731. s, 0.0, c, 0.0,
  4732. 0.0, 0.0, 0.0, 1.0,
  4733. result
  4734. );
  4735. result._updateIdentityStatus(c === 1 && s === 0);
  4736. }
  4737. /**
  4738. * Creates a new rotation matrix for "angle" radians around the Z axis
  4739. * @param angle defines the angle (in radians) to use
  4740. * @return the new matrix
  4741. */
  4742. public static RotationZ(angle: number): Matrix {
  4743. var result = new Matrix();
  4744. Matrix.RotationZToRef(angle, result);
  4745. return result;
  4746. }
  4747. /**
  4748. * Creates a new rotation matrix for "angle" radians around the Z axis and stores it in a given matrix
  4749. * @param angle defines the angle (in radians) to use
  4750. * @param result defines the target matrix
  4751. */
  4752. public static RotationZToRef(angle: number, result: Matrix): void {
  4753. var s = Math.sin(angle);
  4754. var c = Math.cos(angle);
  4755. Matrix.FromValuesToRef(
  4756. c, s, 0.0, 0.0,
  4757. -s, c, 0.0, 0.0,
  4758. 0.0, 0.0, 1.0, 0.0,
  4759. 0.0, 0.0, 0.0, 1.0,
  4760. result
  4761. );
  4762. result._updateIdentityStatus(c === 1 && s === 0);
  4763. }
  4764. /**
  4765. * Creates a new rotation matrix for "angle" radians around the given axis
  4766. * @param axis defines the axis to use
  4767. * @param angle defines the angle (in radians) to use
  4768. * @return the new matrix
  4769. */
  4770. public static RotationAxis(axis: Vector3, angle: number): Matrix {
  4771. var result = new Matrix();
  4772. Matrix.RotationAxisToRef(axis, angle, result);
  4773. return result;
  4774. }
  4775. /**
  4776. * Creates a new rotation matrix for "angle" radians around the given axis and stores it in a given matrix
  4777. * @param axis defines the axis to use
  4778. * @param angle defines the angle (in radians) to use
  4779. * @param result defines the target matrix
  4780. */
  4781. public static RotationAxisToRef(axis: Vector3, angle: number, result: Matrix): void {
  4782. var s = Math.sin(-angle);
  4783. var c = Math.cos(-angle);
  4784. var c1 = 1 - c;
  4785. axis.normalize();
  4786. const m = result._m;
  4787. m[0] = (axis.x * axis.x) * c1 + c;
  4788. m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  4789. m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  4790. m[3] = 0.0;
  4791. m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  4792. m[5] = (axis.y * axis.y) * c1 + c;
  4793. m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  4794. m[7] = 0.0;
  4795. m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  4796. m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  4797. m[10] = (axis.z * axis.z) * c1 + c;
  4798. m[11] = 0.0;
  4799. m[15] = 1.0;
  4800. result._markAsUpdated();
  4801. }
  4802. /**
  4803. * Creates a rotation matrix
  4804. * @param yaw defines the yaw angle in radians (Y axis)
  4805. * @param pitch defines the pitch angle in radians (X axis)
  4806. * @param roll defines the roll angle in radians (X axis)
  4807. * @returns the new rotation matrix
  4808. */
  4809. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Matrix {
  4810. var result = new Matrix();
  4811. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  4812. return result;
  4813. }
  4814. /**
  4815. * Creates a rotation matrix and stores it in a given matrix
  4816. * @param yaw defines the yaw angle in radians (Y axis)
  4817. * @param pitch defines the pitch angle in radians (X axis)
  4818. * @param roll defines the roll angle in radians (X axis)
  4819. * @param result defines the target matrix
  4820. */
  4821. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Matrix): void {
  4822. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, MathTmp.Quaternion[0]);
  4823. MathTmp.Quaternion[0].toRotationMatrix(result);
  4824. }
  4825. /**
  4826. * Creates a scaling matrix
  4827. * @param x defines the scale factor on X axis
  4828. * @param y defines the scale factor on Y axis
  4829. * @param z defines the scale factor on Z axis
  4830. * @returns the new matrix
  4831. */
  4832. public static Scaling(x: number, y: number, z: number): Matrix {
  4833. var result = new Matrix();
  4834. Matrix.ScalingToRef(x, y, z, result);
  4835. return result;
  4836. }
  4837. /**
  4838. * Creates a scaling matrix and stores it in a given matrix
  4839. * @param x defines the scale factor on X axis
  4840. * @param y defines the scale factor on Y axis
  4841. * @param z defines the scale factor on Z axis
  4842. * @param result defines the target matrix
  4843. */
  4844. public static ScalingToRef(x: number, y: number, z: number, result: Matrix): void {
  4845. Matrix.FromValuesToRef(
  4846. x, 0.0, 0.0, 0.0,
  4847. 0.0, y, 0.0, 0.0,
  4848. 0.0, 0.0, z, 0.0,
  4849. 0.0, 0.0, 0.0, 1.0,
  4850. result
  4851. );
  4852. result._updateIdentityStatus(x === 1 && y === 1 && z === 1);
  4853. }
  4854. /**
  4855. * Creates a translation matrix
  4856. * @param x defines the translation on X axis
  4857. * @param y defines the translation on Y axis
  4858. * @param z defines the translationon Z axis
  4859. * @returns the new matrix
  4860. */
  4861. public static Translation(x: number, y: number, z: number): Matrix {
  4862. var result = new Matrix();
  4863. Matrix.TranslationToRef(x, y, z, result);
  4864. return result;
  4865. }
  4866. /**
  4867. * Creates a translation matrix and stores it in a given matrix
  4868. * @param x defines the translation on X axis
  4869. * @param y defines the translation on Y axis
  4870. * @param z defines the translationon Z axis
  4871. * @param result defines the target matrix
  4872. */
  4873. public static TranslationToRef(x: number, y: number, z: number, result: Matrix): void {
  4874. Matrix.FromValuesToRef(
  4875. 1.0, 0.0, 0.0, 0.0,
  4876. 0.0, 1.0, 0.0, 0.0,
  4877. 0.0, 0.0, 1.0, 0.0,
  4878. x, y, z, 1.0,
  4879. result
  4880. );
  4881. result._updateIdentityStatus(x === 0 && y === 0 && z === 0);
  4882. }
  4883. /**
  4884. * Returns a new Matrix whose values are the interpolated values for "gradient" (float) between the ones of the matrices "startValue" and "endValue".
  4885. * @param startValue defines the start value
  4886. * @param endValue defines the end value
  4887. * @param gradient defines the gradient factor
  4888. * @returns the new matrix
  4889. */
  4890. public static Lerp(startValue: Matrix, endValue: Matrix, gradient: number): Matrix {
  4891. var result = new Matrix();
  4892. Matrix.LerpToRef(startValue, endValue, gradient, result);
  4893. return result;
  4894. }
  4895. /**
  4896. * Set the given matrix "result" as the interpolated values for "gradient" (float) between the ones of the matrices "startValue" and "endValue".
  4897. * @param startValue defines the start value
  4898. * @param endValue defines the end value
  4899. * @param gradient defines the gradient factor
  4900. * @param result defines the Matrix object where to store data
  4901. */
  4902. public static LerpToRef(startValue: Matrix, endValue: Matrix, gradient: number, result: Matrix): void {
  4903. for (var index = 0; index < 16; index++) {
  4904. result._m[index] = startValue._m[index] * (1.0 - gradient) + endValue._m[index] * gradient;
  4905. }
  4906. result._markAsUpdated();
  4907. }
  4908. /**
  4909. * Builds a new matrix whose values are computed by:
  4910. * * decomposing the the "startValue" and "endValue" matrices into their respective scale, rotation and translation matrices
  4911. * * interpolating for "gradient" (float) the values between each of these decomposed matrices between the start and the end
  4912. * * recomposing a new matrix from these 3 interpolated scale, rotation and translation matrices
  4913. * @param startValue defines the first matrix
  4914. * @param endValue defines the second matrix
  4915. * @param gradient defines the gradient between the two matrices
  4916. * @returns the new matrix
  4917. */
  4918. public static DecomposeLerp(startValue: Matrix, endValue: Matrix, gradient: number): Matrix {
  4919. var result = new Matrix();
  4920. Matrix.DecomposeLerpToRef(startValue, endValue, gradient, result);
  4921. return result;
  4922. }
  4923. /**
  4924. * Update a matrix to values which are computed by:
  4925. * * decomposing the the "startValue" and "endValue" matrices into their respective scale, rotation and translation matrices
  4926. * * interpolating for "gradient" (float) the values between each of these decomposed matrices between the start and the end
  4927. * * recomposing a new matrix from these 3 interpolated scale, rotation and translation matrices
  4928. * @param startValue defines the first matrix
  4929. * @param endValue defines the second matrix
  4930. * @param gradient defines the gradient between the two matrices
  4931. * @param result defines the target matrix
  4932. */
  4933. public static DecomposeLerpToRef(startValue: Matrix, endValue: Matrix, gradient: number, result: Matrix) {
  4934. var startScale = MathTmp.Vector3[0];
  4935. var startRotation = MathTmp.Quaternion[0];
  4936. var startTranslation = MathTmp.Vector3[1];
  4937. startValue.decompose(startScale, startRotation, startTranslation);
  4938. var endScale = MathTmp.Vector3[2];
  4939. var endRotation = MathTmp.Quaternion[1];
  4940. var endTranslation = MathTmp.Vector3[3];
  4941. endValue.decompose(endScale, endRotation, endTranslation);
  4942. var resultScale = MathTmp.Vector3[4];
  4943. Vector3.LerpToRef(startScale, endScale, gradient, resultScale);
  4944. var resultRotation = MathTmp.Quaternion[2];
  4945. Quaternion.SlerpToRef(startRotation, endRotation, gradient, resultRotation);
  4946. var resultTranslation = MathTmp.Vector3[5];
  4947. Vector3.LerpToRef(startTranslation, endTranslation, gradient, resultTranslation);
  4948. Matrix.ComposeToRef(resultScale, resultRotation, resultTranslation, result);
  4949. }
  4950. /**
  4951. * 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"
  4952. * This function works in left handed mode
  4953. * @param eye defines the final position of the entity
  4954. * @param target defines where the entity should look at
  4955. * @param up defines the up vector for the entity
  4956. * @returns the new matrix
  4957. */
  4958. public static LookAtLH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
  4959. var result = new Matrix();
  4960. Matrix.LookAtLHToRef(eye, target, up, result);
  4961. return result;
  4962. }
  4963. /**
  4964. * 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".
  4965. * This function works in left handed mode
  4966. * @param eye defines the final position of the entity
  4967. * @param target defines where the entity should look at
  4968. * @param up defines the up vector for the entity
  4969. * @param result defines the target matrix
  4970. */
  4971. public static LookAtLHToRef(eye: Vector3, target: Vector3, up: Vector3, result: Matrix): void {
  4972. const xAxis = MathTmp.Vector3[0];
  4973. const yAxis = MathTmp.Vector3[1];
  4974. const zAxis = MathTmp.Vector3[2];
  4975. // Z axis
  4976. target.subtractToRef(eye, zAxis);
  4977. zAxis.normalize();
  4978. // X axis
  4979. Vector3.CrossToRef(up, zAxis, xAxis);
  4980. const xSquareLength = xAxis.lengthSquared();
  4981. if (xSquareLength === 0) {
  4982. xAxis.x = 1.0;
  4983. } else {
  4984. xAxis.normalizeFromLength(Math.sqrt(xSquareLength));
  4985. }
  4986. // Y axis
  4987. Vector3.CrossToRef(zAxis, xAxis, yAxis);
  4988. yAxis.normalize();
  4989. // Eye angles
  4990. var ex = -Vector3.Dot(xAxis, eye);
  4991. var ey = -Vector3.Dot(yAxis, eye);
  4992. var ez = -Vector3.Dot(zAxis, eye);
  4993. Matrix.FromValuesToRef(
  4994. xAxis.x, yAxis.x, zAxis.x, 0.0,
  4995. xAxis.y, yAxis.y, zAxis.y, 0.0,
  4996. xAxis.z, yAxis.z, zAxis.z, 0.0,
  4997. ex, ey, ez, 1.0,
  4998. result
  4999. );
  5000. }
  5001. /**
  5002. * 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"
  5003. * This function works in right handed mode
  5004. * @param eye defines the final position of the entity
  5005. * @param target defines where the entity should look at
  5006. * @param up defines the up vector for the entity
  5007. * @returns the new matrix
  5008. */
  5009. public static LookAtRH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
  5010. var result = new Matrix();
  5011. Matrix.LookAtRHToRef(eye, target, up, result);
  5012. return result;
  5013. }
  5014. /**
  5015. * 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".
  5016. * This function works in right handed mode
  5017. * @param eye defines the final position of the entity
  5018. * @param target defines where the entity should look at
  5019. * @param up defines the up vector for the entity
  5020. * @param result defines the target matrix
  5021. */
  5022. public static LookAtRHToRef(eye: Vector3, target: Vector3, up: Vector3, result: Matrix): void {
  5023. const xAxis = MathTmp.Vector3[0];
  5024. const yAxis = MathTmp.Vector3[1];
  5025. const zAxis = MathTmp.Vector3[2];
  5026. // Z axis
  5027. eye.subtractToRef(target, zAxis);
  5028. zAxis.normalize();
  5029. // X axis
  5030. Vector3.CrossToRef(up, zAxis, xAxis);
  5031. const xSquareLength = xAxis.lengthSquared();
  5032. if (xSquareLength === 0) {
  5033. xAxis.x = 1.0;
  5034. } else {
  5035. xAxis.normalizeFromLength(Math.sqrt(xSquareLength));
  5036. }
  5037. // Y axis
  5038. Vector3.CrossToRef(zAxis, xAxis, yAxis);
  5039. yAxis.normalize();
  5040. // Eye angles
  5041. var ex = -Vector3.Dot(xAxis, eye);
  5042. var ey = -Vector3.Dot(yAxis, eye);
  5043. var ez = -Vector3.Dot(zAxis, eye);
  5044. Matrix.FromValuesToRef(
  5045. xAxis.x, yAxis.x, zAxis.x, 0.0,
  5046. xAxis.y, yAxis.y, zAxis.y, 0.0,
  5047. xAxis.z, yAxis.z, zAxis.z, 0.0,
  5048. ex, ey, ez, 1.0,
  5049. result
  5050. );
  5051. }
  5052. /**
  5053. * Create a left-handed orthographic projection matrix
  5054. * @param width defines the viewport width
  5055. * @param height defines the viewport height
  5056. * @param znear defines the near clip plane
  5057. * @param zfar defines the far clip plane
  5058. * @returns a new matrix as a left-handed orthographic projection matrix
  5059. */
  5060. public static OrthoLH(width: number, height: number, znear: number, zfar: number): Matrix {
  5061. var matrix = new Matrix();
  5062. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  5063. return matrix;
  5064. }
  5065. /**
  5066. * Store a left-handed orthographic projection to a given matrix
  5067. * @param width defines the viewport width
  5068. * @param height defines the viewport height
  5069. * @param znear defines the near clip plane
  5070. * @param zfar defines the far clip plane
  5071. * @param result defines the target matrix
  5072. */
  5073. public static OrthoLHToRef(width: number, height: number, znear: number, zfar: number, result: Matrix): void {
  5074. let n = znear;
  5075. let f = zfar;
  5076. let a = 2.0 / width;
  5077. let b = 2.0 / height;
  5078. let c = 2.0 / (f - n);
  5079. let d = -(f + n) / (f - n);
  5080. Matrix.FromValuesToRef(
  5081. a, 0.0, 0.0, 0.0,
  5082. 0.0, b, 0.0, 0.0,
  5083. 0.0, 0.0, c, 0.0,
  5084. 0.0, 0.0, d, 1.0,
  5085. result
  5086. );
  5087. result._updateIdentityStatus(a === 1 && b === 1 && c === 1 && d === 0);
  5088. }
  5089. /**
  5090. * Create a left-handed orthographic projection matrix
  5091. * @param left defines the viewport left coordinate
  5092. * @param right defines the viewport right coordinate
  5093. * @param bottom defines the viewport bottom coordinate
  5094. * @param top defines the viewport top coordinate
  5095. * @param znear defines the near clip plane
  5096. * @param zfar defines the far clip plane
  5097. * @returns a new matrix as a left-handed orthographic projection matrix
  5098. */
  5099. public static OrthoOffCenterLH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  5100. var matrix = new Matrix();
  5101. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  5102. return matrix;
  5103. }
  5104. /**
  5105. * Stores a left-handed orthographic projection into a given matrix
  5106. * @param left defines the viewport left coordinate
  5107. * @param right defines the viewport right coordinate
  5108. * @param bottom defines the viewport bottom coordinate
  5109. * @param top defines the viewport top coordinate
  5110. * @param znear defines the near clip plane
  5111. * @param zfar defines the far clip plane
  5112. * @param result defines the target matrix
  5113. */
  5114. public static OrthoOffCenterLHToRef(left: number, right: number, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  5115. let n = znear;
  5116. let f = zfar;
  5117. let a = 2.0 / (right - left);
  5118. let b = 2.0 / (top - bottom);
  5119. let c = 2.0 / (f - n);
  5120. let d = -(f + n) / (f - n);
  5121. let i0 = (left + right) / (left - right);
  5122. let i1 = (top + bottom) / (bottom - top);
  5123. Matrix.FromValuesToRef(
  5124. a, 0.0, 0.0, 0.0,
  5125. 0.0, b, 0.0, 0.0,
  5126. 0.0, 0.0, c, 0.0,
  5127. i0, i1, d, 1.0,
  5128. result
  5129. );
  5130. result._markAsUpdated();
  5131. }
  5132. /**
  5133. * Creates a right-handed orthographic projection matrix
  5134. * @param left defines the viewport left coordinate
  5135. * @param right defines the viewport right coordinate
  5136. * @param bottom defines the viewport bottom coordinate
  5137. * @param top defines the viewport top coordinate
  5138. * @param znear defines the near clip plane
  5139. * @param zfar defines the far clip plane
  5140. * @returns a new matrix as a right-handed orthographic projection matrix
  5141. */
  5142. public static OrthoOffCenterRH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  5143. var matrix = new Matrix();
  5144. Matrix.OrthoOffCenterRHToRef(left, right, bottom, top, znear, zfar, matrix);
  5145. return matrix;
  5146. }
  5147. /**
  5148. * Stores a right-handed orthographic projection into a given matrix
  5149. * @param left defines the viewport left coordinate
  5150. * @param right defines the viewport right coordinate
  5151. * @param bottom defines the viewport bottom coordinate
  5152. * @param top defines the viewport top coordinate
  5153. * @param znear defines the near clip plane
  5154. * @param zfar defines the far clip plane
  5155. * @param result defines the target matrix
  5156. */
  5157. public static OrthoOffCenterRHToRef(left: number, right: number, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  5158. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, result);
  5159. result._m[10] *= -1; // No need to call _markAsUpdated as previous function already called it and let _isIdentityDirty to true
  5160. }
  5161. /**
  5162. * Creates a left-handed perspective projection matrix
  5163. * @param width defines the viewport width
  5164. * @param height defines the viewport height
  5165. * @param znear defines the near clip plane
  5166. * @param zfar defines the far clip plane
  5167. * @returns a new matrix as a left-handed perspective projection matrix
  5168. */
  5169. public static PerspectiveLH(width: number, height: number, znear: number, zfar: number): Matrix {
  5170. var matrix = new Matrix();
  5171. let n = znear;
  5172. let f = zfar;
  5173. let a = 2.0 * n / width;
  5174. let b = 2.0 * n / height;
  5175. let c = (f + n) / (f - n);
  5176. let d = -2.0 * f * n / (f - n);
  5177. Matrix.FromValuesToRef(
  5178. a, 0.0, 0.0, 0.0,
  5179. 0.0, b, 0.0, 0.0,
  5180. 0.0, 0.0, c, 1.0,
  5181. 0.0, 0.0, d, 0.0,
  5182. matrix
  5183. );
  5184. matrix._updateIdentityStatus(false);
  5185. return matrix;
  5186. }
  5187. /**
  5188. * Creates a left-handed perspective projection matrix
  5189. * @param fov defines the horizontal field of view
  5190. * @param aspect defines the aspect ratio
  5191. * @param znear defines the near clip plane
  5192. * @param zfar defines the far clip plane
  5193. * @returns a new matrix as a left-handed perspective projection matrix
  5194. */
  5195. public static PerspectiveFovLH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  5196. var matrix = new Matrix();
  5197. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  5198. return matrix;
  5199. }
  5200. /**
  5201. * Stores a left-handed perspective projection into a given matrix
  5202. * @param fov defines the horizontal field of view
  5203. * @param aspect defines the aspect ratio
  5204. * @param znear defines the near clip plane
  5205. * @param zfar defines the far clip plane
  5206. * @param result defines the target matrix
  5207. * @param isVerticalFovFixed defines it the fov is vertically fixed (default) or horizontally
  5208. */
  5209. public static PerspectiveFovLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  5210. let n = znear;
  5211. let f = zfar;
  5212. let t = 1.0 / (Math.tan(fov * 0.5));
  5213. let a = isVerticalFovFixed ? (t / aspect) : t;
  5214. let b = isVerticalFovFixed ? t : (t * aspect);
  5215. let c = (f + n) / (f - n);
  5216. let d = -2.0 * f * n / (f - n);
  5217. Matrix.FromValuesToRef(
  5218. a, 0.0, 0.0, 0.0,
  5219. 0.0, b, 0.0, 0.0,
  5220. 0.0, 0.0, c, 1.0,
  5221. 0.0, 0.0, d, 0.0,
  5222. result
  5223. );
  5224. result._updateIdentityStatus(false);
  5225. }
  5226. /**
  5227. * Creates a right-handed perspective projection matrix
  5228. * @param fov defines the horizontal field of view
  5229. * @param aspect defines the aspect ratio
  5230. * @param znear defines the near clip plane
  5231. * @param zfar defines the far clip plane
  5232. * @returns a new matrix as a right-handed perspective projection matrix
  5233. */
  5234. public static PerspectiveFovRH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  5235. var matrix = new Matrix();
  5236. Matrix.PerspectiveFovRHToRef(fov, aspect, znear, zfar, matrix);
  5237. return matrix;
  5238. }
  5239. /**
  5240. * Stores a right-handed perspective projection into a given matrix
  5241. * @param fov defines the horizontal field of view
  5242. * @param aspect defines the aspect ratio
  5243. * @param znear defines the near clip plane
  5244. * @param zfar defines the far clip plane
  5245. * @param result defines the target matrix
  5246. * @param isVerticalFovFixed defines it the fov is vertically fixed (default) or horizontally
  5247. */
  5248. public static PerspectiveFovRHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  5249. //alternatively this could be expressed as:
  5250. // m = PerspectiveFovLHToRef
  5251. // m[10] *= -1.0;
  5252. // m[11] *= -1.0;
  5253. let n = znear;
  5254. let f = zfar;
  5255. let t = 1.0 / (Math.tan(fov * 0.5));
  5256. let a = isVerticalFovFixed ? (t / aspect) : t;
  5257. let b = isVerticalFovFixed ? t : (t * aspect);
  5258. let c = -(f + n) / (f - n);
  5259. let d = -2 * f * n / (f - n);
  5260. Matrix.FromValuesToRef(
  5261. a, 0.0, 0.0, 0.0,
  5262. 0.0, b, 0.0, 0.0,
  5263. 0.0, 0.0, c, -1.0,
  5264. 0.0, 0.0, d, 0.0,
  5265. result
  5266. );
  5267. result._updateIdentityStatus(false);
  5268. }
  5269. /**
  5270. * Stores a perspective projection for WebVR info a given matrix
  5271. * @param fov defines the field of view
  5272. * @param znear defines the near clip plane
  5273. * @param zfar defines the far clip plane
  5274. * @param result defines the target matrix
  5275. * @param rightHanded defines if the matrix must be in right-handed mode (false by default)
  5276. */
  5277. public static PerspectiveFovWebVRToRef(fov: { upDegrees: number, downDegrees: number, leftDegrees: number, rightDegrees: number }, znear: number, zfar: number, result: Matrix, rightHanded = false): void {
  5278. var rightHandedFactor = rightHanded ? -1 : 1;
  5279. var upTan = Math.tan(fov.upDegrees * Math.PI / 180.0);
  5280. var downTan = Math.tan(fov.downDegrees * Math.PI / 180.0);
  5281. var leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0);
  5282. var rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0);
  5283. var xScale = 2.0 / (leftTan + rightTan);
  5284. var yScale = 2.0 / (upTan + downTan);
  5285. const m = result._m;
  5286. m[0] = xScale;
  5287. m[1] = m[2] = m[3] = m[4] = 0.0;
  5288. m[5] = yScale;
  5289. m[6] = m[7] = 0.0;
  5290. m[8] = ((leftTan - rightTan) * xScale * 0.5);
  5291. m[9] = -((upTan - downTan) * yScale * 0.5);
  5292. m[10] = -zfar / (znear - zfar);
  5293. m[11] = 1.0 * rightHandedFactor;
  5294. m[12] = m[13] = m[15] = 0.0;
  5295. m[14] = -(2.0 * zfar * znear) / (zfar - znear);
  5296. result._markAsUpdated();
  5297. }
  5298. /**
  5299. * Computes a complete transformation matrix
  5300. * @param viewport defines the viewport to use
  5301. * @param world defines the world matrix
  5302. * @param view defines the view matrix
  5303. * @param projection defines the projection matrix
  5304. * @param zmin defines the near clip plane
  5305. * @param zmax defines the far clip plane
  5306. * @returns the transformation matrix
  5307. */
  5308. public static GetFinalMatrix(viewport: Viewport, world: Matrix, view: Matrix, projection: Matrix, zmin: number, zmax: number): Matrix {
  5309. var cw = viewport.width;
  5310. var ch = viewport.height;
  5311. var cx = viewport.x;
  5312. var cy = viewport.y;
  5313. var viewportMatrix = Matrix.FromValues(
  5314. cw / 2.0, 0.0, 0.0, 0.0,
  5315. 0.0, -ch / 2.0, 0.0, 0.0,
  5316. 0.0, 0.0, zmax - zmin, 0.0,
  5317. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1.0);
  5318. var matrix = MathTmp.Matrix[0];
  5319. world.multiplyToRef(view, matrix);
  5320. matrix.multiplyToRef(projection, matrix);
  5321. return matrix.multiply(viewportMatrix);
  5322. }
  5323. /**
  5324. * Extracts a 2x2 matrix from a given matrix and store the result in a Float32Array
  5325. * @param matrix defines the matrix to use
  5326. * @returns a new Float32Array array with 4 elements : the 2x2 matrix extracted from the given matrix
  5327. */
  5328. public static GetAsMatrix2x2(matrix: Matrix): Float32Array {
  5329. return new Float32Array([
  5330. matrix._m[0], matrix._m[1],
  5331. matrix._m[4], matrix._m[5]
  5332. ]);
  5333. }
  5334. /**
  5335. * Extracts a 3x3 matrix from a given matrix and store the result in a Float32Array
  5336. * @param matrix defines the matrix to use
  5337. * @returns a new Float32Array array with 9 elements : the 3x3 matrix extracted from the given matrix
  5338. */
  5339. public static GetAsMatrix3x3(matrix: Matrix): Float32Array {
  5340. return new Float32Array([
  5341. matrix._m[0], matrix._m[1], matrix._m[2],
  5342. matrix._m[4], matrix._m[5], matrix._m[6],
  5343. matrix._m[8], matrix._m[9], matrix._m[10]
  5344. ]);
  5345. }
  5346. /**
  5347. * Compute the transpose of a given matrix
  5348. * @param matrix defines the matrix to transpose
  5349. * @returns the new matrix
  5350. */
  5351. public static Transpose(matrix: Matrix): Matrix {
  5352. var result = new Matrix();
  5353. Matrix.TransposeToRef(matrix, result);
  5354. return result;
  5355. }
  5356. /**
  5357. * Compute the transpose of a matrix and store it in a target matrix
  5358. * @param matrix defines the matrix to transpose
  5359. * @param result defines the target matrix
  5360. */
  5361. public static TransposeToRef(matrix: Matrix, result: Matrix): void {
  5362. const rm = result._m;
  5363. const mm = matrix._m;
  5364. rm[0] = mm[0];
  5365. rm[1] = mm[4];
  5366. rm[2] = mm[8];
  5367. rm[3] = mm[12];
  5368. rm[4] = mm[1];
  5369. rm[5] = mm[5];
  5370. rm[6] = mm[9];
  5371. rm[7] = mm[13];
  5372. rm[8] = mm[2];
  5373. rm[9] = mm[6];
  5374. rm[10] = mm[10];
  5375. rm[11] = mm[14];
  5376. rm[12] = mm[3];
  5377. rm[13] = mm[7];
  5378. rm[14] = mm[11];
  5379. rm[15] = mm[15];
  5380. // identity-ness does not change when transposing
  5381. result._updateIdentityStatus(matrix._isIdentity, matrix._isIdentityDirty);
  5382. }
  5383. /**
  5384. * Computes a reflection matrix from a plane
  5385. * @param plane defines the reflection plane
  5386. * @returns a new matrix
  5387. */
  5388. public static Reflection(plane: Plane): Matrix {
  5389. var matrix = new Matrix();
  5390. Matrix.ReflectionToRef(plane, matrix);
  5391. return matrix;
  5392. }
  5393. /**
  5394. * Computes a reflection matrix from a plane
  5395. * @param plane defines the reflection plane
  5396. * @param result defines the target matrix
  5397. */
  5398. public static ReflectionToRef(plane: Plane, result: Matrix): void {
  5399. plane.normalize();
  5400. var x = plane.normal.x;
  5401. var y = plane.normal.y;
  5402. var z = plane.normal.z;
  5403. var temp = -2 * x;
  5404. var temp2 = -2 * y;
  5405. var temp3 = -2 * z;
  5406. Matrix.FromValuesToRef(
  5407. temp * x + 1, temp2 * x, temp3 * x, 0.0,
  5408. temp * y, temp2 * y + 1, temp3 * y, 0.0,
  5409. temp * z, temp2 * z, temp3 * z + 1, 0.0,
  5410. temp * plane.d, temp2 * plane.d, temp3 * plane.d, 1.0,
  5411. result
  5412. );
  5413. }
  5414. /**
  5415. * Sets the given matrix as a rotation matrix composed from the 3 left handed axes
  5416. * @param xaxis defines the value of the 1st axis
  5417. * @param yaxis defines the value of the 2nd axis
  5418. * @param zaxis defines the value of the 3rd axis
  5419. * @param result defines the target matrix
  5420. */
  5421. public static FromXYZAxesToRef(xaxis: Vector3, yaxis: Vector3, zaxis: Vector3, result: Matrix) {
  5422. Matrix.FromValuesToRef(
  5423. xaxis.x, xaxis.y, xaxis.z, 0.0,
  5424. yaxis.x, yaxis.y, yaxis.z, 0.0,
  5425. zaxis.x, zaxis.y, zaxis.z, 0.0,
  5426. 0.0, 0.0, 0.0, 1.0,
  5427. result
  5428. );
  5429. }
  5430. /**
  5431. * Creates a rotation matrix from a quaternion and stores it in a target matrix
  5432. * @param quat defines the quaternion to use
  5433. * @param result defines the target matrix
  5434. */
  5435. public static FromQuaternionToRef(quat: Quaternion, result: Matrix) {
  5436. var xx = quat.x * quat.x;
  5437. var yy = quat.y * quat.y;
  5438. var zz = quat.z * quat.z;
  5439. var xy = quat.x * quat.y;
  5440. var zw = quat.z * quat.w;
  5441. var zx = quat.z * quat.x;
  5442. var yw = quat.y * quat.w;
  5443. var yz = quat.y * quat.z;
  5444. var xw = quat.x * quat.w;
  5445. result._m[0] = 1.0 - (2.0 * (yy + zz));
  5446. result._m[1] = 2.0 * (xy + zw);
  5447. result._m[2] = 2.0 * (zx - yw);
  5448. result._m[3] = 0.0;
  5449. result._m[4] = 2.0 * (xy - zw);
  5450. result._m[5] = 1.0 - (2.0 * (zz + xx));
  5451. result._m[6] = 2.0 * (yz + xw);
  5452. result._m[7] = 0.0;
  5453. result._m[8] = 2.0 * (zx + yw);
  5454. result._m[9] = 2.0 * (yz - xw);
  5455. result._m[10] = 1.0 - (2.0 * (yy + xx));
  5456. result._m[11] = 0.0;
  5457. result._m[12] = 0.0;
  5458. result._m[13] = 0.0;
  5459. result._m[14] = 0.0;
  5460. result._m[15] = 1.0;
  5461. result._markAsUpdated();
  5462. }
  5463. }
  5464. /**
  5465. * Represens a plane by the equation ax + by + cz + d = 0
  5466. */
  5467. export class Plane {
  5468. /**
  5469. * Normal of the plane (a,b,c)
  5470. */
  5471. public normal: Vector3;
  5472. /**
  5473. * d component of the plane
  5474. */
  5475. public d: number;
  5476. /**
  5477. * Creates a Plane object according to the given floats a, b, c, d and the plane equation : ax + by + cz + d = 0
  5478. * @param a a component of the plane
  5479. * @param b b component of the plane
  5480. * @param c c component of the plane
  5481. * @param d d component of the plane
  5482. */
  5483. constructor(a: number, b: number, c: number, d: number) {
  5484. this.normal = new Vector3(a, b, c);
  5485. this.d = d;
  5486. }
  5487. /**
  5488. * @returns the plane coordinates as a new array of 4 elements [a, b, c, d].
  5489. */
  5490. public asArray(): number[] {
  5491. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  5492. }
  5493. // Methods
  5494. /**
  5495. * @returns a new plane copied from the current Plane.
  5496. */
  5497. public clone(): Plane {
  5498. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  5499. }
  5500. /**
  5501. * @returns the string "Plane".
  5502. */
  5503. public getClassName(): string {
  5504. return "Plane";
  5505. }
  5506. /**
  5507. * @returns the Plane hash code.
  5508. */
  5509. public getHashCode(): number {
  5510. let hash = this.normal.getHashCode();
  5511. hash = (hash * 397) ^ (this.d || 0);
  5512. return hash;
  5513. }
  5514. /**
  5515. * Normalize the current Plane in place.
  5516. * @returns the updated Plane.
  5517. */
  5518. public normalize(): Plane {
  5519. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  5520. var magnitude = 0.0;
  5521. if (norm !== 0) {
  5522. magnitude = 1.0 / norm;
  5523. }
  5524. this.normal.x *= magnitude;
  5525. this.normal.y *= magnitude;
  5526. this.normal.z *= magnitude;
  5527. this.d *= magnitude;
  5528. return this;
  5529. }
  5530. /**
  5531. * Applies a transformation the plane and returns the result
  5532. * @param transformation the transformation matrix to be applied to the plane
  5533. * @returns a new Plane as the result of the transformation of the current Plane by the given matrix.
  5534. */
  5535. public transform(transformation: Matrix): Plane {
  5536. const transposedMatrix = MathTmp.Matrix[0];
  5537. Matrix.TransposeToRef(transformation, transposedMatrix);
  5538. const m = transposedMatrix.m;
  5539. var x = this.normal.x;
  5540. var y = this.normal.y;
  5541. var z = this.normal.z;
  5542. var d = this.d;
  5543. var normalX = x * m[0] + y * m[1] + z * m[2] + d * m[3];
  5544. var normalY = x * m[4] + y * m[5] + z * m[6] + d * m[7];
  5545. var normalZ = x * m[8] + y * m[9] + z * m[10] + d * m[11];
  5546. var finalD = x * m[12] + y * m[13] + z * m[14] + d * m[15];
  5547. return new Plane(normalX, normalY, normalZ, finalD);
  5548. }
  5549. /**
  5550. * Calcualtte the dot product between the point and the plane normal
  5551. * @param point point to calculate the dot product with
  5552. * @returns the dot product (float) of the point coordinates and the plane normal.
  5553. */
  5554. public dotCoordinate(point: Vector3): number {
  5555. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  5556. }
  5557. /**
  5558. * Updates the current Plane from the plane defined by the three given points.
  5559. * @param point1 one of the points used to contruct the plane
  5560. * @param point2 one of the points used to contruct the plane
  5561. * @param point3 one of the points used to contruct the plane
  5562. * @returns the updated Plane.
  5563. */
  5564. public copyFromPoints(point1: Vector3, point2: Vector3, point3: Vector3): Plane {
  5565. var x1 = point2.x - point1.x;
  5566. var y1 = point2.y - point1.y;
  5567. var z1 = point2.z - point1.z;
  5568. var x2 = point3.x - point1.x;
  5569. var y2 = point3.y - point1.y;
  5570. var z2 = point3.z - point1.z;
  5571. var yz = (y1 * z2) - (z1 * y2);
  5572. var xz = (z1 * x2) - (x1 * z2);
  5573. var xy = (x1 * y2) - (y1 * x2);
  5574. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  5575. var invPyth;
  5576. if (pyth !== 0) {
  5577. invPyth = 1.0 / pyth;
  5578. }
  5579. else {
  5580. invPyth = 0.0;
  5581. }
  5582. this.normal.x = yz * invPyth;
  5583. this.normal.y = xz * invPyth;
  5584. this.normal.z = xy * invPyth;
  5585. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  5586. return this;
  5587. }
  5588. /**
  5589. * Checks if the plane is facing a given direction
  5590. * @param direction the direction to check if the plane is facing
  5591. * @param epsilon value the dot product is compared against (returns true if dot <= epsilon)
  5592. * @returns True is the vector "direction" is the same side than the plane normal.
  5593. */
  5594. public isFrontFacingTo(direction: Vector3, epsilon: number): boolean {
  5595. var dot = Vector3.Dot(this.normal, direction);
  5596. return (dot <= epsilon);
  5597. }
  5598. /**
  5599. * Calculates the distance to a point
  5600. * @param point point to calculate distance to
  5601. * @returns the signed distance (float) from the given point to the Plane.
  5602. */
  5603. public signedDistanceTo(point: Vector3): number {
  5604. return Vector3.Dot(point, this.normal) + this.d;
  5605. }
  5606. // Statics
  5607. /**
  5608. * Creates a plane from an array
  5609. * @param array the array to create a plane from
  5610. * @returns a new Plane from the given array.
  5611. */
  5612. static FromArray(array: ArrayLike<number>): Plane {
  5613. return new Plane(array[0], array[1], array[2], array[3]);
  5614. }
  5615. /**
  5616. * Creates a plane from three points
  5617. * @param point1 point used to create the plane
  5618. * @param point2 point used to create the plane
  5619. * @param point3 point used to create the plane
  5620. * @returns a new Plane defined by the three given points.
  5621. */
  5622. static FromPoints(point1: Vector3, point2: Vector3, point3: Vector3): Plane {
  5623. var result = new Plane(0.0, 0.0, 0.0, 0.0);
  5624. result.copyFromPoints(point1, point2, point3);
  5625. return result;
  5626. }
  5627. /**
  5628. * Creates a plane from an origin point and a normal
  5629. * @param origin origin of the plane to be constructed
  5630. * @param normal normal of the plane to be constructed
  5631. * @returns a new Plane the normal vector to this plane at the given origin point.
  5632. * Note : the vector "normal" is updated because normalized.
  5633. */
  5634. static FromPositionAndNormal(origin: Vector3, normal: Vector3): Plane {
  5635. var result = new Plane(0.0, 0.0, 0.0, 0.0);
  5636. normal.normalize();
  5637. result.normal = normal;
  5638. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  5639. return result;
  5640. }
  5641. /**
  5642. * Calculates the distance from a plane and a point
  5643. * @param origin origin of the plane to be constructed
  5644. * @param normal normal of the plane to be constructed
  5645. * @param point point to calculate distance to
  5646. * @returns the signed distance between the plane defined by the normal vector at the "origin"" point and the given other point.
  5647. */
  5648. static SignedDistanceToPlaneFromPositionAndNormal(origin: Vector3, normal: Vector3, point: Vector3): number {
  5649. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  5650. return Vector3.Dot(point, normal) + d;
  5651. }
  5652. }
  5653. /**
  5654. * Class used to represent a viewport on screen
  5655. */
  5656. export class Viewport {
  5657. /**
  5658. * Creates a Viewport object located at (x, y) and sized (width, height)
  5659. * @param x defines viewport left coordinate
  5660. * @param y defines viewport top coordinate
  5661. * @param width defines the viewport width
  5662. * @param height defines the viewport height
  5663. */
  5664. constructor(
  5665. /** viewport left coordinate */
  5666. public x: number,
  5667. /** viewport top coordinate */
  5668. public y: number,
  5669. /**viewport width */
  5670. public width: number,
  5671. /** viewport height */
  5672. public height: number) {
  5673. }
  5674. /**
  5675. * Creates a new viewport using absolute sizing (from 0-> width, 0-> height instead of 0->1)
  5676. * @param renderWidthOrEngine defines either an engine or the rendering width
  5677. * @param renderHeight defines the rendering height
  5678. * @returns a new Viewport
  5679. */
  5680. public toGlobal(renderWidthOrEngine: number | Engine, renderHeight: number): Viewport {
  5681. if ((<Engine>renderWidthOrEngine).getRenderWidth) {
  5682. var engine = (<Engine>renderWidthOrEngine);
  5683. return this.toGlobal(engine.getRenderWidth(), engine.getRenderHeight());
  5684. }
  5685. let renderWidth = <number>renderWidthOrEngine;
  5686. return new Viewport(this.x * renderWidth, this.y * renderHeight, this.width * renderWidth, this.height * renderHeight);
  5687. }
  5688. /**
  5689. * Returns a new Viewport copied from the current one
  5690. * @returns a new Viewport
  5691. */
  5692. public clone(): Viewport {
  5693. return new Viewport(this.x, this.y, this.width, this.height);
  5694. }
  5695. }
  5696. /**
  5697. * Reprasents a camera frustum
  5698. */
  5699. export class Frustum {
  5700. /**
  5701. * Gets the planes representing the frustum
  5702. * @param transform matrix to be applied to the returned planes
  5703. * @returns a new array of 6 Frustum planes computed by the given transformation matrix.
  5704. */
  5705. public static GetPlanes(transform: Matrix): Plane[] {
  5706. var frustumPlanes = [];
  5707. for (var index = 0; index < 6; index++) {
  5708. frustumPlanes.push(new Plane(0.0, 0.0, 0.0, 0.0));
  5709. }
  5710. Frustum.GetPlanesToRef(transform, frustumPlanes);
  5711. return frustumPlanes;
  5712. }
  5713. /**
  5714. * Gets the near frustum plane transformed by the transform matrix
  5715. * @param transform transformation matrix to be applied to the resulting frustum plane
  5716. * @param frustumPlane the resuling frustum plane
  5717. */
  5718. public static GetNearPlaneToRef(transform: Matrix, frustumPlane: Plane): void {
  5719. const m = transform.m;
  5720. frustumPlane.normal.x = m[3] + m[2];
  5721. frustumPlane.normal.y = m[7] + m[6];
  5722. frustumPlane.normal.z = m[11] + m[10];
  5723. frustumPlane.d = m[15] + m[14];
  5724. frustumPlane.normalize();
  5725. }
  5726. /**
  5727. * Gets the far frustum plane transformed by the transform matrix
  5728. * @param transform transformation matrix to be applied to the resulting frustum plane
  5729. * @param frustumPlane the resuling frustum plane
  5730. */
  5731. public static GetFarPlaneToRef(transform: Matrix, frustumPlane: Plane): void {
  5732. const m = transform.m;
  5733. frustumPlane.normal.x = m[3] - m[2];
  5734. frustumPlane.normal.y = m[7] - m[6];
  5735. frustumPlane.normal.z = m[11] - m[10];
  5736. frustumPlane.d = m[15] - m[14];
  5737. frustumPlane.normalize();
  5738. }
  5739. /**
  5740. * Gets the left frustum plane transformed by the transform matrix
  5741. * @param transform transformation matrix to be applied to the resulting frustum plane
  5742. * @param frustumPlane the resuling frustum plane
  5743. */
  5744. public static GetLeftPlaneToRef(transform: Matrix, frustumPlane: Plane): void {
  5745. const m = transform.m;
  5746. frustumPlane.normal.x = m[3] + m[0];
  5747. frustumPlane.normal.y = m[7] + m[4];
  5748. frustumPlane.normal.z = m[11] + m[8];
  5749. frustumPlane.d = m[15] + m[12];
  5750. frustumPlane.normalize();
  5751. }
  5752. /**
  5753. * Gets the right frustum plane transformed by the transform matrix
  5754. * @param transform transformation matrix to be applied to the resulting frustum plane
  5755. * @param frustumPlane the resuling frustum plane
  5756. */
  5757. public static GetRightPlaneToRef(transform: Matrix, frustumPlane: Plane): void {
  5758. const m = transform.m;
  5759. frustumPlane.normal.x = m[3] - m[0];
  5760. frustumPlane.normal.y = m[7] - m[4];
  5761. frustumPlane.normal.z = m[11] - m[8];
  5762. frustumPlane.d = m[15] - m[12];
  5763. frustumPlane.normalize();
  5764. }
  5765. /**
  5766. * Gets the top frustum plane transformed by the transform matrix
  5767. * @param transform transformation matrix to be applied to the resulting frustum plane
  5768. * @param frustumPlane the resuling frustum plane
  5769. */
  5770. public static GetTopPlaneToRef(transform: Matrix, frustumPlane: Plane): void {
  5771. const m = transform.m;
  5772. frustumPlane.normal.x = m[3] - m[1];
  5773. frustumPlane.normal.y = m[7] - m[5];
  5774. frustumPlane.normal.z = m[11] - m[9];
  5775. frustumPlane.d = m[15] - m[13];
  5776. frustumPlane.normalize();
  5777. }
  5778. /**
  5779. * Gets the bottom frustum plane transformed by the transform matrix
  5780. * @param transform transformation matrix to be applied to the resulting frustum plane
  5781. * @param frustumPlane the resuling frustum plane
  5782. */
  5783. public static GetBottomPlaneToRef(transform: Matrix, frustumPlane: Plane): void {
  5784. const m = transform.m;
  5785. frustumPlane.normal.x = m[3] + m[1];
  5786. frustumPlane.normal.y = m[7] + m[5];
  5787. frustumPlane.normal.z = m[11] + m[9];
  5788. frustumPlane.d = m[15] + m[13];
  5789. frustumPlane.normalize();
  5790. }
  5791. /**
  5792. * Sets the given array "frustumPlanes" with the 6 Frustum planes computed by the given transformation matrix.
  5793. * @param transform transformation matrix to be applied to the resulting frustum planes
  5794. * @param frustumPlanes the resuling frustum planes
  5795. */
  5796. public static GetPlanesToRef(transform: Matrix, frustumPlanes: Plane[]): void {
  5797. // Near
  5798. Frustum.GetNearPlaneToRef(transform, frustumPlanes[0]);
  5799. // Far
  5800. Frustum.GetFarPlaneToRef(transform, frustumPlanes[1]);
  5801. // Left
  5802. Frustum.GetLeftPlaneToRef(transform, frustumPlanes[2]);
  5803. // Right
  5804. Frustum.GetRightPlaneToRef(transform, frustumPlanes[3]);
  5805. // Top
  5806. Frustum.GetTopPlaneToRef(transform, frustumPlanes[4]);
  5807. // Bottom
  5808. Frustum.GetBottomPlaneToRef(transform, frustumPlanes[5]);
  5809. }
  5810. }
  5811. /** Defines supported spaces */
  5812. export enum Space {
  5813. /** Local (object) space */
  5814. LOCAL = 0,
  5815. /** World space */
  5816. WORLD = 1,
  5817. /** Bone space */
  5818. BONE = 2
  5819. }
  5820. /** Defines the 3 main axes */
  5821. export class Axis {
  5822. /** X axis */
  5823. public static X: Vector3 = new Vector3(1.0, 0.0, 0.0);
  5824. /** Y axis */
  5825. public static Y: Vector3 = new Vector3(0.0, 1.0, 0.0);
  5826. /** Z axis */
  5827. public static Z: Vector3 = new Vector3(0.0, 0.0, 1.0);
  5828. }
  5829. /** Class used to represent a Bezier curve */
  5830. export class BezierCurve {
  5831. /**
  5832. * Returns the cubic Bezier interpolated value (float) at "t" (float) from the given x1, y1, x2, y2 floats
  5833. * @param t defines the time
  5834. * @param x1 defines the left coordinate on X axis
  5835. * @param y1 defines the left coordinate on Y axis
  5836. * @param x2 defines the right coordinate on X axis
  5837. * @param y2 defines the right coordinate on Y axis
  5838. * @returns the interpolated value
  5839. */
  5840. public static Interpolate(t: number, x1: number, y1: number, x2: number, y2: number): number {
  5841. // Extract X (which is equal to time here)
  5842. var f0 = 1 - 3 * x2 + 3 * x1;
  5843. var f1 = 3 * x2 - 6 * x1;
  5844. var f2 = 3 * x1;
  5845. var refinedT = t;
  5846. for (var i = 0; i < 5; i++) {
  5847. var refinedT2 = refinedT * refinedT;
  5848. var refinedT3 = refinedT2 * refinedT;
  5849. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  5850. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  5851. refinedT -= (x - t) * slope;
  5852. refinedT = Math.min(1, Math.max(0, refinedT));
  5853. }
  5854. // Resolve cubic bezier for the given x
  5855. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 +
  5856. 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 +
  5857. Math.pow(refinedT, 3);
  5858. }
  5859. }
  5860. /**
  5861. * Defines potential orientation for back face culling
  5862. */
  5863. export enum Orientation {
  5864. /**
  5865. * Clockwise
  5866. */
  5867. CW = 0,
  5868. /** Counter clockwise */
  5869. CCW = 1
  5870. }
  5871. /**
  5872. * Defines angle representation
  5873. */
  5874. export class Angle {
  5875. private _radians: number;
  5876. /**
  5877. * Creates an Angle object of "radians" radians (float).
  5878. */
  5879. constructor(radians: number) {
  5880. this._radians = radians;
  5881. if (this._radians < 0.0) { this._radians += (2.0 * Math.PI); }
  5882. }
  5883. /**
  5884. * Get value in degrees
  5885. * @returns the Angle value in degrees (float)
  5886. */
  5887. public degrees() {
  5888. return this._radians * 180.0 / Math.PI;
  5889. }
  5890. /**
  5891. * Get value in radians
  5892. * @returns the Angle value in radians (float)
  5893. */
  5894. public radians() {
  5895. return this._radians;
  5896. }
  5897. /**
  5898. * Gets a new Angle object valued with the angle value in radians between the two given vectors
  5899. * @param a defines first vector
  5900. * @param b defines second vector
  5901. * @returns a new Angle
  5902. */
  5903. public static BetweenTwoPoints(a: Vector2, b: Vector2): Angle {
  5904. var delta = b.subtract(a);
  5905. var theta = Math.atan2(delta.y, delta.x);
  5906. return new Angle(theta);
  5907. }
  5908. /**
  5909. * Gets a new Angle object from the given float in radians
  5910. * @param radians defines the angle value in radians
  5911. * @returns a new Angle
  5912. */
  5913. public static FromRadians(radians: number): Angle {
  5914. return new Angle(radians);
  5915. }
  5916. /**
  5917. * Gets a new Angle object from the given float in degrees
  5918. * @param degrees defines the angle value in degrees
  5919. * @returns a new Angle
  5920. */
  5921. public static FromDegrees(degrees: number): Angle {
  5922. return new Angle(degrees * Math.PI / 180.0);
  5923. }
  5924. }
  5925. /**
  5926. * This represents an arc in a 2d space.
  5927. */
  5928. export class Arc2 {
  5929. /**
  5930. * Defines the center point of the arc.
  5931. */
  5932. public centerPoint: Vector2;
  5933. /**
  5934. * Defines the radius of the arc.
  5935. */
  5936. public radius: number;
  5937. /**
  5938. * Defines the angle of the arc (from mid point to end point).
  5939. */
  5940. public angle: Angle;
  5941. /**
  5942. * Defines the start angle of the arc (from start point to middle point).
  5943. */
  5944. public startAngle: Angle;
  5945. /**
  5946. * Defines the orientation of the arc (clock wise/counter clock wise).
  5947. */
  5948. public orientation: Orientation;
  5949. /**
  5950. * Creates an Arc object from the three given points : start, middle and end.
  5951. * @param startPoint Defines the start point of the arc
  5952. * @param midPoint Defines the midlle point of the arc
  5953. * @param endPoint Defines the end point of the arc
  5954. */
  5955. constructor(
  5956. /** Defines the start point of the arc */
  5957. public startPoint: Vector2,
  5958. /** Defines the mid point of the arc */
  5959. public midPoint: Vector2,
  5960. /** Defines the end point of the arc */
  5961. public endPoint: Vector2) {
  5962. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  5963. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  5964. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  5965. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  5966. this.centerPoint = new Vector2(
  5967. (startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det,
  5968. ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det
  5969. );
  5970. this.radius = this.centerPoint.subtract(this.startPoint).length();
  5971. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  5972. var a1 = this.startAngle.degrees();
  5973. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  5974. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  5975. // angles correction
  5976. if (a2 - a1 > +180.0) { a2 -= 360.0; }
  5977. if (a2 - a1 < -180.0) { a2 += 360.0; }
  5978. if (a3 - a2 > +180.0) { a3 -= 360.0; }
  5979. if (a3 - a2 < -180.0) { a3 += 360.0; }
  5980. this.orientation = (a2 - a1) < 0 ? Orientation.CW : Orientation.CCW;
  5981. this.angle = Angle.FromDegrees(this.orientation === Orientation.CW ? a1 - a3 : a3 - a1);
  5982. }
  5983. }
  5984. /**
  5985. * Represents a 2D path made up of multiple 2D points
  5986. */
  5987. export class Path2 {
  5988. private _points = new Array<Vector2>();
  5989. private _length = 0.0;
  5990. /**
  5991. * If the path start and end point are the same
  5992. */
  5993. public closed = false;
  5994. /**
  5995. * Creates a Path2 object from the starting 2D coordinates x and y.
  5996. * @param x the starting points x value
  5997. * @param y the starting points y value
  5998. */
  5999. constructor(x: number, y: number) {
  6000. this._points.push(new Vector2(x, y));
  6001. }
  6002. /**
  6003. * Adds a new segment until the given coordinates (x, y) to the current Path2.
  6004. * @param x the added points x value
  6005. * @param y the added points y value
  6006. * @returns the updated Path2.
  6007. */
  6008. public addLineTo(x: number, y: number): Path2 {
  6009. if (this.closed) {
  6010. return this;
  6011. }
  6012. var newPoint = new Vector2(x, y);
  6013. var previousPoint = this._points[this._points.length - 1];
  6014. this._points.push(newPoint);
  6015. this._length += newPoint.subtract(previousPoint).length();
  6016. return this;
  6017. }
  6018. /**
  6019. * Adds _numberOfSegments_ segments according to the arc definition (middle point coordinates, end point coordinates, the arc start point being the current Path2 last point) to the current Path2.
  6020. * @param midX middle point x value
  6021. * @param midY middle point y value
  6022. * @param endX end point x value
  6023. * @param endY end point y value
  6024. * @param numberOfSegments (default: 36)
  6025. * @returns the updated Path2.
  6026. */
  6027. public addArcTo(midX: number, midY: number, endX: number, endY: number, numberOfSegments = 36): Path2 {
  6028. if (this.closed) {
  6029. return this;
  6030. }
  6031. var startPoint = this._points[this._points.length - 1];
  6032. var midPoint = new Vector2(midX, midY);
  6033. var endPoint = new Vector2(endX, endY);
  6034. var arc = new Arc2(startPoint, midPoint, endPoint);
  6035. var increment = arc.angle.radians() / numberOfSegments;
  6036. if (arc.orientation === Orientation.CW) { increment *= -1; }
  6037. var currentAngle = arc.startAngle.radians() + increment;
  6038. for (var i = 0; i < numberOfSegments; i++) {
  6039. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  6040. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  6041. this.addLineTo(x, y);
  6042. currentAngle += increment;
  6043. }
  6044. return this;
  6045. }
  6046. /**
  6047. * Closes the Path2.
  6048. * @returns the Path2.
  6049. */
  6050. public close(): Path2 {
  6051. this.closed = true;
  6052. return this;
  6053. }
  6054. /**
  6055. * Gets the sum of the distance between each sequential point in the path
  6056. * @returns the Path2 total length (float).
  6057. */
  6058. public length(): number {
  6059. var result = this._length;
  6060. if (!this.closed) {
  6061. var lastPoint = this._points[this._points.length - 1];
  6062. var firstPoint = this._points[0];
  6063. result += (firstPoint.subtract(lastPoint).length());
  6064. }
  6065. return result;
  6066. }
  6067. /**
  6068. * Gets the points which construct the path
  6069. * @returns the Path2 internal array of points.
  6070. */
  6071. public getPoints(): Vector2[] {
  6072. return this._points;
  6073. }
  6074. /**
  6075. * Retreives the point at the distance aways from the starting point
  6076. * @param normalizedLengthPosition the length along the path to retreive the point from
  6077. * @returns a new Vector2 located at a percentage of the Path2 total length on this path.
  6078. */
  6079. public getPointAtLengthPosition(normalizedLengthPosition: number): Vector2 {
  6080. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  6081. return Vector2.Zero();
  6082. }
  6083. var lengthPosition = normalizedLengthPosition * this.length();
  6084. var previousOffset = 0;
  6085. for (var i = 0; i < this._points.length; i++) {
  6086. var j = (i + 1) % this._points.length;
  6087. var a = this._points[i];
  6088. var b = this._points[j];
  6089. var bToA = b.subtract(a);
  6090. var nextOffset = (bToA.length() + previousOffset);
  6091. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  6092. var dir = bToA.normalize();
  6093. var localOffset = lengthPosition - previousOffset;
  6094. return new Vector2(
  6095. a.x + (dir.x * localOffset),
  6096. a.y + (dir.y * localOffset)
  6097. );
  6098. }
  6099. previousOffset = nextOffset;
  6100. }
  6101. return Vector2.Zero();
  6102. }
  6103. /**
  6104. * Creates a new path starting from an x and y position
  6105. * @param x starting x value
  6106. * @param y starting y value
  6107. * @returns a new Path2 starting at the coordinates (x, y).
  6108. */
  6109. public static StartingAt(x: number, y: number): Path2 {
  6110. return new Path2(x, y);
  6111. }
  6112. }
  6113. /**
  6114. * Represents a 3D path made up of multiple 3D points
  6115. */
  6116. export class Path3D {
  6117. private _curve = new Array<Vector3>();
  6118. private _distances = new Array<number>();
  6119. private _tangents = new Array<Vector3>();
  6120. private _normals = new Array<Vector3>();
  6121. private _binormals = new Array<Vector3>();
  6122. private _raw: boolean;
  6123. /**
  6124. * new Path3D(path, normal, raw)
  6125. * Creates a Path3D. A Path3D is a logical math object, so not a mesh.
  6126. * please read the description in the tutorial : http://doc.babylonjs.com/tutorials/How_to_use_Path3D
  6127. * @param path an array of Vector3, the curve axis of the Path3D
  6128. * @param normal (options) Vector3, the first wanted normal to the curve. Ex (0, 1, 0) for a vertical normal.
  6129. * @param raw (optional, default false) : boolean, if true the returned Path3D isn't normalized. Useful to depict path acceleration or speed.
  6130. */
  6131. constructor(
  6132. /**
  6133. * an array of Vector3, the curve axis of the Path3D
  6134. */
  6135. public path: Vector3[],
  6136. firstNormal: Nullable<Vector3> = null,
  6137. raw?: boolean
  6138. ) {
  6139. for (var p = 0; p < path.length; p++) {
  6140. this._curve[p] = path[p].clone(); // hard copy
  6141. }
  6142. this._raw = raw || false;
  6143. this._compute(firstNormal);
  6144. }
  6145. /**
  6146. * Returns the Path3D array of successive Vector3 designing its curve.
  6147. * @returns the Path3D array of successive Vector3 designing its curve.
  6148. */
  6149. public getCurve(): Vector3[] {
  6150. return this._curve;
  6151. }
  6152. /**
  6153. * Returns an array populated with tangent vectors on each Path3D curve point.
  6154. * @returns an array populated with tangent vectors on each Path3D curve point.
  6155. */
  6156. public getTangents(): Vector3[] {
  6157. return this._tangents;
  6158. }
  6159. /**
  6160. * Returns an array populated with normal vectors on each Path3D curve point.
  6161. * @returns an array populated with normal vectors on each Path3D curve point.
  6162. */
  6163. public getNormals(): Vector3[] {
  6164. return this._normals;
  6165. }
  6166. /**
  6167. * Returns an array populated with binormal vectors on each Path3D curve point.
  6168. * @returns an array populated with binormal vectors on each Path3D curve point.
  6169. */
  6170. public getBinormals(): Vector3[] {
  6171. return this._binormals;
  6172. }
  6173. /**
  6174. * Returns an array populated with distances (float) of the i-th point from the first curve point.
  6175. * @returns an array populated with distances (float) of the i-th point from the first curve point.
  6176. */
  6177. public getDistances(): number[] {
  6178. return this._distances;
  6179. }
  6180. /**
  6181. * Forces the Path3D tangent, normal, binormal and distance recomputation.
  6182. * @param path path which all values are copied into the curves points
  6183. * @param firstNormal which should be projected onto the curve
  6184. * @returns the same object updated.
  6185. */
  6186. public update(path: Vector3[], firstNormal: Nullable<Vector3> = null): Path3D {
  6187. for (var p = 0; p < path.length; p++) {
  6188. this._curve[p].x = path[p].x;
  6189. this._curve[p].y = path[p].y;
  6190. this._curve[p].z = path[p].z;
  6191. }
  6192. this._compute(firstNormal);
  6193. return this;
  6194. }
  6195. // private function compute() : computes tangents, normals and binormals
  6196. private _compute(firstNormal: Nullable<Vector3>): void {
  6197. var l = this._curve.length;
  6198. // first and last tangents
  6199. this._tangents[0] = this._getFirstNonNullVector(0);
  6200. if (!this._raw) {
  6201. this._tangents[0].normalize();
  6202. }
  6203. this._tangents[l - 1] = this._curve[l - 1].subtract(this._curve[l - 2]);
  6204. if (!this._raw) {
  6205. this._tangents[l - 1].normalize();
  6206. }
  6207. // normals and binormals at first point : arbitrary vector with _normalVector()
  6208. var tg0 = this._tangents[0];
  6209. var pp0 = this._normalVector(this._curve[0], tg0, firstNormal);
  6210. this._normals[0] = pp0;
  6211. if (!this._raw) {
  6212. this._normals[0].normalize();
  6213. }
  6214. this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
  6215. if (!this._raw) {
  6216. this._binormals[0].normalize();
  6217. }
  6218. this._distances[0] = 0.0;
  6219. // normals and binormals : next points
  6220. var prev: Vector3; // previous vector (segment)
  6221. var cur: Vector3; // current vector (segment)
  6222. var curTang: Vector3; // current tangent
  6223. // previous normal
  6224. var prevBinor: Vector3; // previous binormal
  6225. for (var i = 1; i < l; i++) {
  6226. // tangents
  6227. prev = this._getLastNonNullVector(i);
  6228. if (i < l - 1) {
  6229. cur = this._getFirstNonNullVector(i);
  6230. this._tangents[i] = prev.add(cur);
  6231. this._tangents[i].normalize();
  6232. }
  6233. this._distances[i] = this._distances[i - 1] + prev.length();
  6234. // normals and binormals
  6235. // http://www.cs.cmu.edu/afs/andrew/scs/cs/15-462/web/old/asst2camera.html
  6236. curTang = this._tangents[i];
  6237. prevBinor = this._binormals[i - 1];
  6238. this._normals[i] = Vector3.Cross(prevBinor, curTang);
  6239. if (!this._raw) {
  6240. this._normals[i].normalize();
  6241. }
  6242. this._binormals[i] = Vector3.Cross(curTang, this._normals[i]);
  6243. if (!this._raw) {
  6244. this._binormals[i].normalize();
  6245. }
  6246. }
  6247. }
  6248. // private function getFirstNonNullVector(index)
  6249. // returns the first non null vector from index : curve[index + N].subtract(curve[index])
  6250. private _getFirstNonNullVector(index: number): Vector3 {
  6251. var i = 1;
  6252. var nNVector: Vector3 = this._curve[index + i].subtract(this._curve[index]);
  6253. while (nNVector.length() === 0 && index + i + 1 < this._curve.length) {
  6254. i++;
  6255. nNVector = this._curve[index + i].subtract(this._curve[index]);
  6256. }
  6257. return nNVector;
  6258. }
  6259. // private function getLastNonNullVector(index)
  6260. // returns the last non null vector from index : curve[index].subtract(curve[index - N])
  6261. private _getLastNonNullVector(index: number): Vector3 {
  6262. var i = 1;
  6263. var nLVector: Vector3 = this._curve[index].subtract(this._curve[index - i]);
  6264. while (nLVector.length() === 0 && index > i + 1) {
  6265. i++;
  6266. nLVector = this._curve[index].subtract(this._curve[index - i]);
  6267. }
  6268. return nLVector;
  6269. }
  6270. // private function normalVector(v0, vt, va) :
  6271. // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
  6272. // if va is passed, it returns the va projection on the plane orthogonal to vt at the point v0
  6273. private _normalVector(v0: Vector3, vt: Vector3, va: Nullable<Vector3>): Vector3 {
  6274. var normal0: Vector3;
  6275. var tgl = vt.length();
  6276. if (tgl === 0.0) {
  6277. tgl = 1.0;
  6278. }
  6279. if (va === undefined || va === null) {
  6280. var point: Vector3;
  6281. if (!Scalar.WithinEpsilon(Math.abs(vt.y) / tgl, 1.0, Epsilon)) { // search for a point in the plane
  6282. point = new Vector3(0.0, -1.0, 0.0);
  6283. }
  6284. else if (!Scalar.WithinEpsilon(Math.abs(vt.x) / tgl, 1.0, Epsilon)) {
  6285. point = new Vector3(1.0, 0.0, 0.0);
  6286. }
  6287. else if (!Scalar.WithinEpsilon(Math.abs(vt.z) / tgl, 1.0, Epsilon)) {
  6288. point = new Vector3(0.0, 0.0, 1.0);
  6289. }
  6290. else {
  6291. point = Vector3.Zero();
  6292. }
  6293. normal0 = Vector3.Cross(vt, point);
  6294. }
  6295. else {
  6296. normal0 = Vector3.Cross(vt, va);
  6297. Vector3.CrossToRef(normal0, vt, normal0);
  6298. }
  6299. normal0.normalize();
  6300. return normal0;
  6301. }
  6302. }
  6303. /**
  6304. * A Curve3 object is a logical object, so not a mesh, to handle curves in the 3D geometric space.
  6305. * A Curve3 is designed from a series of successive Vector3.
  6306. * @see https://doc.babylonjs.com/how_to/how_to_use_curve3
  6307. */
  6308. export class Curve3 {
  6309. private _points: Vector3[];
  6310. private _length: number = 0.0;
  6311. /**
  6312. * Returns a Curve3 object along a Quadratic Bezier curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#quadratic-bezier-curve
  6313. * @param v0 (Vector3) the origin point of the Quadratic Bezier
  6314. * @param v1 (Vector3) the control point
  6315. * @param v2 (Vector3) the end point of the Quadratic Bezier
  6316. * @param nbPoints (integer) the wanted number of points in the curve
  6317. * @returns the created Curve3
  6318. */
  6319. public static CreateQuadraticBezier(v0: Vector3, v1: Vector3, v2: Vector3, nbPoints: number): Curve3 {
  6320. nbPoints = nbPoints > 2 ? nbPoints : 3;
  6321. var bez = new Array<Vector3>();
  6322. var equation = (t: number, val0: number, val1: number, val2: number) => {
  6323. var res = (1.0 - t) * (1.0 - t) * val0 + 2.0 * t * (1.0 - t) * val1 + t * t * val2;
  6324. return res;
  6325. };
  6326. for (var i = 0; i <= nbPoints; i++) {
  6327. bez.push(new Vector3(equation(i / nbPoints, v0.x, v1.x, v2.x), equation(i / nbPoints, v0.y, v1.y, v2.y), equation(i / nbPoints, v0.z, v1.z, v2.z)));
  6328. }
  6329. return new Curve3(bez);
  6330. }
  6331. /**
  6332. * Returns a Curve3 object along a Cubic Bezier curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#cubic-bezier-curve
  6333. * @param v0 (Vector3) the origin point of the Cubic Bezier
  6334. * @param v1 (Vector3) the first control point
  6335. * @param v2 (Vector3) the second control point
  6336. * @param v3 (Vector3) the end point of the Cubic Bezier
  6337. * @param nbPoints (integer) the wanted number of points in the curve
  6338. * @returns the created Curve3
  6339. */
  6340. public static CreateCubicBezier(v0: Vector3, v1: Vector3, v2: Vector3, v3: Vector3, nbPoints: number): Curve3 {
  6341. nbPoints = nbPoints > 3 ? nbPoints : 4;
  6342. var bez = new Array<Vector3>();
  6343. var equation = (t: number, val0: number, val1: number, val2: number, val3: number) => {
  6344. var res = (1.0 - t) * (1.0 - t) * (1.0 - t) * val0 + 3.0 * t * (1.0 - t) * (1.0 - t) * val1 + 3.0 * t * t * (1.0 - t) * val2 + t * t * t * val3;
  6345. return res;
  6346. };
  6347. for (var i = 0; i <= nbPoints; i++) {
  6348. bez.push(new Vector3(equation(i / nbPoints, v0.x, v1.x, v2.x, v3.x), equation(i / nbPoints, v0.y, v1.y, v2.y, v3.y), equation(i / nbPoints, v0.z, v1.z, v2.z, v3.z)));
  6349. }
  6350. return new Curve3(bez);
  6351. }
  6352. /**
  6353. * Returns a Curve3 object along a Hermite Spline curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#hermite-spline
  6354. * @param p1 (Vector3) the origin point of the Hermite Spline
  6355. * @param t1 (Vector3) the tangent vector at the origin point
  6356. * @param p2 (Vector3) the end point of the Hermite Spline
  6357. * @param t2 (Vector3) the tangent vector at the end point
  6358. * @param nbPoints (integer) the wanted number of points in the curve
  6359. * @returns the created Curve3
  6360. */
  6361. public static CreateHermiteSpline(p1: Vector3, t1: Vector3, p2: Vector3, t2: Vector3, nbPoints: number): Curve3 {
  6362. var hermite = new Array<Vector3>();
  6363. var step = 1.0 / nbPoints;
  6364. for (var i = 0; i <= nbPoints; i++) {
  6365. hermite.push(Vector3.Hermite(p1, t1, p2, t2, i * step));
  6366. }
  6367. return new Curve3(hermite);
  6368. }
  6369. /**
  6370. * Returns a Curve3 object along a CatmullRom Spline curve :
  6371. * @param points (array of Vector3) the points the spline must pass through. At least, four points required
  6372. * @param nbPoints (integer) the wanted number of points between each curve control points
  6373. * @param closed (boolean) optional with default false, when true forms a closed loop from the points
  6374. * @returns the created Curve3
  6375. */
  6376. public static CreateCatmullRomSpline(points: Vector3[], nbPoints: number, closed?: boolean): Curve3 {
  6377. var catmullRom = new Array<Vector3>();
  6378. var step = 1.0 / nbPoints;
  6379. var amount = 0.0;
  6380. if (closed) {
  6381. var pointsCount = points.length;
  6382. for (var i = 0; i < pointsCount; i++) {
  6383. amount = 0;
  6384. for (var c = 0; c < nbPoints; c++) {
  6385. catmullRom.push(Vector3.CatmullRom(points[i % pointsCount], points[(i + 1) % pointsCount], points[(i + 2) % pointsCount], points[(i + 3) % pointsCount], amount));
  6386. amount += step;
  6387. }
  6388. }
  6389. catmullRom.push(catmullRom[0]);
  6390. }
  6391. else {
  6392. var totalPoints = new Array<Vector3>();
  6393. totalPoints.push(points[0].clone());
  6394. Array.prototype.push.apply(totalPoints, points);
  6395. totalPoints.push(points[points.length - 1].clone());
  6396. for (var i = 0; i < totalPoints.length - 3; i++) {
  6397. amount = 0;
  6398. for (var c = 0; c < nbPoints; c++) {
  6399. catmullRom.push(Vector3.CatmullRom(totalPoints[i], totalPoints[i + 1], totalPoints[i + 2], totalPoints[i + 3], amount));
  6400. amount += step;
  6401. }
  6402. }
  6403. i--;
  6404. catmullRom.push(Vector3.CatmullRom(totalPoints[i], totalPoints[i + 1], totalPoints[i + 2], totalPoints[i + 3], amount));
  6405. }
  6406. return new Curve3(catmullRom);
  6407. }
  6408. /**
  6409. * A Curve3 object is a logical object, so not a mesh, to handle curves in the 3D geometric space.
  6410. * A Curve3 is designed from a series of successive Vector3.
  6411. * Tuto : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#curve3-object
  6412. * @param points points which make up the curve
  6413. */
  6414. constructor(points: Vector3[]) {
  6415. this._points = points;
  6416. this._length = this._computeLength(points);
  6417. }
  6418. /**
  6419. * @returns the Curve3 stored array of successive Vector3
  6420. */
  6421. public getPoints() {
  6422. return this._points;
  6423. }
  6424. /**
  6425. * @returns the computed length (float) of the curve.
  6426. */
  6427. public length() {
  6428. return this._length;
  6429. }
  6430. /**
  6431. * Returns a new instance of Curve3 object : var curve = curveA.continue(curveB);
  6432. * This new Curve3 is built by translating and sticking the curveB at the end of the curveA.
  6433. * curveA and curveB keep unchanged.
  6434. * @param curve the curve to continue from this curve
  6435. * @returns the newly constructed curve
  6436. */
  6437. public continue(curve: Curve3): Curve3 {
  6438. var lastPoint = this._points[this._points.length - 1];
  6439. var continuedPoints = this._points.slice();
  6440. var curvePoints = curve.getPoints();
  6441. for (var i = 1; i < curvePoints.length; i++) {
  6442. continuedPoints.push(curvePoints[i].subtract(curvePoints[0]).add(lastPoint));
  6443. }
  6444. var continuedCurve = new Curve3(continuedPoints);
  6445. return continuedCurve;
  6446. }
  6447. private _computeLength(path: Vector3[]): number {
  6448. var l = 0;
  6449. for (var i = 1; i < path.length; i++) {
  6450. l += (path[i].subtract(path[i - 1])).length();
  6451. }
  6452. return l;
  6453. }
  6454. }
  6455. // Vertex formats
  6456. /**
  6457. * Contains position and normal vectors for a vertex
  6458. */
  6459. export class PositionNormalVertex {
  6460. /**
  6461. * Creates a PositionNormalVertex
  6462. * @param position the position of the vertex (defaut: 0,0,0)
  6463. * @param normal the normal of the vertex (defaut: 0,1,0)
  6464. */
  6465. constructor(
  6466. /** the position of the vertex (defaut: 0,0,0) */
  6467. public position: Vector3 = Vector3.Zero(),
  6468. /** the normal of the vertex (defaut: 0,1,0) */
  6469. public normal: Vector3 = Vector3.Up()
  6470. ) {
  6471. }
  6472. /**
  6473. * Clones the PositionNormalVertex
  6474. * @returns the cloned PositionNormalVertex
  6475. */
  6476. public clone(): PositionNormalVertex {
  6477. return new PositionNormalVertex(this.position.clone(), this.normal.clone());
  6478. }
  6479. }
  6480. /**
  6481. * Contains position, normal and uv vectors for a vertex
  6482. */
  6483. export class PositionNormalTextureVertex {
  6484. /**
  6485. * Creates a PositionNormalTextureVertex
  6486. * @param position the position of the vertex (defaut: 0,0,0)
  6487. * @param normal the normal of the vertex (defaut: 0,1,0)
  6488. * @param uv the uv of the vertex (default: 0,0)
  6489. */
  6490. constructor(
  6491. /** the position of the vertex (defaut: 0,0,0) */
  6492. public position: Vector3 = Vector3.Zero(),
  6493. /** the normal of the vertex (defaut: 0,1,0) */
  6494. public normal: Vector3 = Vector3.Up(),
  6495. /** the uv of the vertex (default: 0,0) */
  6496. public uv: Vector2 = Vector2.Zero()
  6497. ) {
  6498. }
  6499. /**
  6500. * Clones the PositionNormalTextureVertex
  6501. * @returns the cloned PositionNormalTextureVertex
  6502. */
  6503. public clone(): PositionNormalTextureVertex {
  6504. return new PositionNormalTextureVertex(this.position.clone(), this.normal.clone(), this.uv.clone());
  6505. }
  6506. }
  6507. // Temporary pre-allocated objects for engine internal use
  6508. // usage in any internal function :
  6509. // var tmp = Tmp.Vector3[0]; <= gets access to the first pre-created Vector3
  6510. // There's a Tmp array per object type : int, float, Vector2, Vector3, Vector4, Quaternion, Matrix
  6511. /**
  6512. * @hidden
  6513. */
  6514. export class Tmp {
  6515. public static Color3: Color3[] = Tools.BuildArray(3, Color3.Black);
  6516. public static Color4: Color4[] = Tools.BuildArray(3, () => new Color4(0, 0, 0, 0));
  6517. public static Vector2: Vector2[] = Tools.BuildArray(3, Vector2.Zero); // 3 temp Vector2 at once should be enough
  6518. public static Vector3: Vector3[] = Tools.BuildArray(13, Vector3.Zero); // 13 temp Vector3 at once should be enough
  6519. public static Vector4: Vector4[] = Tools.BuildArray(3, Vector4.Zero); // 3 temp Vector4 at once should be enough
  6520. public static Quaternion: Quaternion[] = Tools.BuildArray(2, Quaternion.Zero); // 2 temp Quaternion at once should be enough
  6521. public static Matrix: Matrix[] = Tools.BuildArray(8, Matrix.Identity); // 8 temp Matrices at once should be enough
  6522. }
  6523. /**
  6524. * @hidden
  6525. * Same as Tmp but not exported to keep it only for math functions to avoid conflicts
  6526. */
  6527. class MathTmp {
  6528. public static Vector3: Vector3[] = Tools.BuildArray(6, Vector3.Zero);
  6529. public static Matrix: Matrix[] = Tools.BuildArray(2, Matrix.Identity);
  6530. public static Quaternion: Quaternion[] = Tools.BuildArray(3, Quaternion.Zero);
  6531. }
  6532. }