babylon.math.ts 210 KB

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