babylon.math.ts 219 KB

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