babylon.math.ts 290 KB

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