babylon.math.ts 200 KB

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