babylon.math.ts 208 KB

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