babylon.math.ts 289 KB

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