babylon.math.ts 268 KB

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