babylon.math.ts 239 KB

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