babylon.math.ts 194 KB

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