babylon.math.ts 196 KB

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