babylon.math.ts 202 KB

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