babylon.math.ts 146 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084
  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. public static WithinEpsilon(a: number, b: number, epsilon: number = 1.401298E-45): boolean {
  8. var num = a - b;
  9. return -epsilon <= num && num <= epsilon;
  10. }
  11. public static ToHex(i: number): string {
  12. var str = i.toString(16);
  13. if (i <= 15) {
  14. return ("0" + str).toUpperCase();
  15. }
  16. return str.toUpperCase();
  17. }
  18. // Returns -1 when value is a negative number and
  19. // +1 when value is a positive number.
  20. public static Sign(value: number): number {
  21. value = +value; // convert to a number
  22. if (value === 0 || isNaN(value))
  23. return value;
  24. return value > 0 ? 1 : -1;
  25. }
  26. public static Clamp(value: number, min = 0, max = 1): number {
  27. return Math.min(max, Math.max(min, value));
  28. }
  29. }
  30. export class Color3 {
  31. constructor(public r: number = 0, public g: number = 0, public b: number = 0) {
  32. }
  33. public toString(): string {
  34. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  35. }
  36. public getClassName(): string {
  37. return "Color3";
  38. }
  39. public getHashCode(): number {
  40. let hash = this.r || 0;
  41. hash = (hash * 397) ^ (this.g || 0);
  42. hash = (hash * 397) ^ (this.b || 0);
  43. return hash;
  44. }
  45. // Operators
  46. public toArray(array: number[], index?: number): Color3 {
  47. if (index === undefined) {
  48. index = 0;
  49. }
  50. array[index] = this.r;
  51. array[index + 1] = this.g;
  52. array[index + 2] = this.b;
  53. return this;
  54. }
  55. public toColor4(alpha = 1): Color4 {
  56. return new Color4(this.r, this.g, this.b, alpha);
  57. }
  58. public asArray(): number[] {
  59. var result = [];
  60. this.toArray(result, 0);
  61. return result;
  62. }
  63. public toLuminance(): number {
  64. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  65. }
  66. public multiply(otherColor: Color3): Color3 {
  67. return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  68. }
  69. public multiplyToRef(otherColor: Color3, result: Color3): Color3 {
  70. result.r = this.r * otherColor.r;
  71. result.g = this.g * otherColor.g;
  72. result.b = this.b * otherColor.b;
  73. return this;
  74. }
  75. public equals(otherColor: Color3): boolean {
  76. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  77. }
  78. public equalsFloats(r: number, g: number, b: number): boolean {
  79. return this.r === r && this.g === g && this.b === b;
  80. }
  81. public scale(scale: number): Color3 {
  82. return new Color3(this.r * scale, this.g * scale, this.b * scale);
  83. }
  84. public scaleToRef(scale: number, result: Color3): Color3 {
  85. result.r = this.r * scale;
  86. result.g = this.g * scale;
  87. result.b = this.b * scale;
  88. return this;
  89. }
  90. public add(otherColor: Color3): Color3 {
  91. return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
  92. }
  93. public addToRef(otherColor: Color3, result: Color3): Color3 {
  94. result.r = this.r + otherColor.r;
  95. result.g = this.g + otherColor.g;
  96. result.b = this.b + otherColor.b;
  97. return this;
  98. }
  99. public subtract(otherColor: Color3): Color3 {
  100. return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
  101. }
  102. public subtractToRef(otherColor: Color3, result: Color3): Color3 {
  103. result.r = this.r - otherColor.r;
  104. result.g = this.g - otherColor.g;
  105. result.b = this.b - otherColor.b;
  106. return this;
  107. }
  108. public clone(): Color3 {
  109. return new Color3(this.r, this.g, this.b);
  110. }
  111. public copyFrom(source: Color3): Color3 {
  112. this.r = source.r;
  113. this.g = source.g;
  114. this.b = source.b;
  115. return this;
  116. }
  117. public copyFromFloats(r: number, g: number, b: number): Color3 {
  118. this.r = r;
  119. this.g = g;
  120. this.b = b;
  121. return this;
  122. }
  123. public toHexString(): string {
  124. var intR = (this.r * 255) | 0;
  125. var intG = (this.g * 255) | 0;
  126. var intB = (this.b * 255) | 0;
  127. return "#" + MathTools.ToHex(intR) + MathTools.ToHex(intG) + MathTools.ToHex(intB);
  128. }
  129. public toLinearSpace(): Color3 {
  130. var convertedColor = new Color3();
  131. this.toLinearSpaceToRef(convertedColor);
  132. return convertedColor;
  133. }
  134. public toLinearSpaceToRef(convertedColor: Color3): Color3 {
  135. convertedColor.r = Math.pow(this.r, ToLinearSpace);
  136. convertedColor.g = Math.pow(this.g, ToLinearSpace);
  137. convertedColor.b = Math.pow(this.b, ToLinearSpace);
  138. return this;
  139. }
  140. public toGammaSpace(): Color3 {
  141. var convertedColor = new Color3();
  142. this.toGammaSpaceToRef(convertedColor);
  143. return convertedColor;
  144. }
  145. public toGammaSpaceToRef(convertedColor: Color3): Color3 {
  146. convertedColor.r = Math.pow(this.r, ToGammaSpace);
  147. convertedColor.g = Math.pow(this.g, ToGammaSpace);
  148. convertedColor.b = Math.pow(this.b, ToGammaSpace);
  149. return this;
  150. }
  151. // Statics
  152. public static FromHexString(hex: string): Color3 {
  153. if (hex.substring(0, 1) !== "#" || hex.length !== 7) {
  154. //Tools.Warn("Color3.FromHexString must be called with a string like #FFFFFF");
  155. return new Color3(0, 0, 0);
  156. }
  157. var r = parseInt(hex.substring(1, 3), 16);
  158. var g = parseInt(hex.substring(3, 5), 16);
  159. var b = parseInt(hex.substring(5, 7), 16);
  160. return Color3.FromInts(r, g, b);
  161. }
  162. public static FromArray(array: number[], offset: number = 0): Color3 {
  163. return new Color3(array[offset], array[offset + 1], array[offset + 2]);
  164. }
  165. public static FromInts(r: number, g: number, b: number): Color3 {
  166. return new Color3(r / 255.0, g / 255.0, b / 255.0);
  167. }
  168. public static Lerp(start: Color3, end: Color3, amount: number): Color3 {
  169. var r = start.r + ((end.r - start.r) * amount);
  170. var g = start.g + ((end.g - start.g) * amount);
  171. var b = start.b + ((end.b - start.b) * amount);
  172. return new Color3(r, g, b);
  173. }
  174. public static Red(): Color3 { return new Color3(1, 0, 0); }
  175. public static Green(): Color3 { return new Color3(0, 1, 0); }
  176. public static Blue(): Color3 { return new Color3(0, 0, 1); }
  177. public static Black(): Color3 { return new Color3(0, 0, 0); }
  178. public static White(): Color3 { return new Color3(1, 1, 1); }
  179. public static Purple(): Color3 { return new Color3(0.5, 0, 0.5); }
  180. public static Magenta(): Color3 { return new Color3(1, 0, 1); }
  181. public static Yellow(): Color3 { return new Color3(1, 1, 0); }
  182. public static Gray(): Color3 { return new Color3(0.5, 0.5, 0.5); }
  183. }
  184. export class Color4 {
  185. constructor(public r: number, public g: number, public b: number, public a: number) {
  186. }
  187. // Operators
  188. public addInPlace(right): Color4 {
  189. this.r += right.r;
  190. this.g += right.g;
  191. this.b += right.b;
  192. this.a += right.a;
  193. return this;
  194. }
  195. public asArray(): number[] {
  196. var result = [];
  197. this.toArray(result, 0);
  198. return result;
  199. }
  200. public toArray(array: number[], index?: number): Color4 {
  201. if (index === undefined) {
  202. index = 0;
  203. }
  204. array[index] = this.r;
  205. array[index + 1] = this.g;
  206. array[index + 2] = this.b;
  207. array[index + 3] = this.a;
  208. return this;
  209. }
  210. public add(right: Color4): Color4 {
  211. return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  212. }
  213. public subtract(right: Color4): Color4 {
  214. return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  215. }
  216. public subtractToRef(right: Color4, result: Color4): Color4 {
  217. result.r = this.r - right.r;
  218. result.g = this.g - right.g;
  219. result.b = this.b - right.b;
  220. result.a = this.a - right.a;
  221. return this;
  222. }
  223. public scale(scale: number): Color4 {
  224. return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  225. }
  226. public scaleToRef(scale: number, result: Color4): Color4 {
  227. result.r = this.r * scale;
  228. result.g = this.g * scale;
  229. result.b = this.b * scale;
  230. result.a = this.a * scale;
  231. return this;
  232. }
  233. /**
  234. * Multipy an RGBA Color4 value by another and return a new Color4 object
  235. * @param color The Color4 (RGBA) value to multiply by
  236. * @returns A new Color4.
  237. */
  238. public multiply(color: Color4): Color4 {
  239. return new Color4(this.r * color.r, this.g * color.g, this.b * color.b, this.a * color.a);
  240. }
  241. /**
  242. * Multipy an RGBA Color4 value by another and push the result in a reference value
  243. * @param color The Color4 (RGBA) value to multiply by
  244. * @param result The Color4 (RGBA) to fill the result in
  245. * @returns the result Color4.
  246. */
  247. public multiplyToRef(color: Color4, result: Color4): Color4 {
  248. result.r = this.r * color.r;
  249. result.g = this.g * color.g;
  250. result.b = this.b * color.b;
  251. result.a = this.a * color.a;
  252. return result;
  253. }
  254. public toString(): string {
  255. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  256. }
  257. public getClassName(): string {
  258. return "Color4";
  259. }
  260. public getHashCode(): number {
  261. let hash = this.r || 0;
  262. hash = (hash * 397) ^ (this.g || 0);
  263. hash = (hash * 397) ^ (this.b || 0);
  264. hash = (hash * 397) ^ (this.a || 0);
  265. return hash;
  266. }
  267. public clone(): Color4 {
  268. return new Color4(this.r, this.g, this.b, this.a);
  269. }
  270. public copyFrom(source: Color4): Color4 {
  271. this.r = source.r;
  272. this.g = source.g;
  273. this.b = source.b;
  274. this.a = source.a;
  275. return this;
  276. }
  277. public toHexString(): string {
  278. var intR = (this.r * 255) | 0;
  279. var intG = (this.g * 255) | 0;
  280. var intB = (this.b * 255) | 0;
  281. var intA = (this.a * 255) | 0;
  282. return "#" + MathTools.ToHex(intR) + MathTools.ToHex(intG) + MathTools.ToHex(intB) + MathTools.ToHex(intA);
  283. }
  284. // Statics
  285. public static FromHexString(hex: string): Color4 {
  286. if (hex.substring(0, 1) !== "#" || hex.length !== 9) {
  287. //Tools.Warn("Color4.FromHexString must be called with a string like #FFFFFFFF");
  288. return new Color4(0, 0, 0, 0);
  289. }
  290. var r = parseInt(hex.substring(1, 3), 16);
  291. var g = parseInt(hex.substring(3, 5), 16);
  292. var b = parseInt(hex.substring(5, 7), 16);
  293. var a = parseInt(hex.substring(7, 9), 16);
  294. return Color4.FromInts(r, g, b, a);
  295. }
  296. public static Lerp(left: Color4, right: Color4, amount: number): Color4 {
  297. var result = new Color4(0, 0, 0, 0);
  298. Color4.LerpToRef(left, right, amount, result);
  299. return result;
  300. }
  301. public static LerpToRef(left: Color4, right: Color4, amount: number, result: Color4): void {
  302. result.r = left.r + (right.r - left.r) * amount;
  303. result.g = left.g + (right.g - left.g) * amount;
  304. result.b = left.b + (right.b - left.b) * amount;
  305. result.a = left.a + (right.a - left.a) * amount;
  306. }
  307. public static FromArray(array: number[], offset: number = 0): Color4 {
  308. return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  309. }
  310. public static FromInts(r: number, g: number, b: number, a: number): Color4 {
  311. return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  312. }
  313. public static CheckColors4(colors: number[], count: number): number[] {
  314. // Check if color3 was used
  315. if (colors.length === count * 3) {
  316. var colors4 = [];
  317. for (var index = 0; index < colors.length; index += 3) {
  318. var newIndex = (index / 3) * 4;
  319. colors4[newIndex] = colors[index];
  320. colors4[newIndex + 1] = colors[index + 1];
  321. colors4[newIndex + 2] = colors[index + 2];
  322. colors4[newIndex + 3] = 1.0;
  323. }
  324. return colors4;
  325. }
  326. return colors;
  327. }
  328. }
  329. export class Vector2 {
  330. constructor(public x: number, public y: number) {
  331. }
  332. public toString(): string {
  333. return "{X: " + this.x + " Y:" + this.y + "}";
  334. }
  335. public getClassName(): string {
  336. return "Vector2";
  337. }
  338. public getHashCode(): number {
  339. let hash = this.x || 0;
  340. hash = (hash * 397) ^ (this.y || 0);
  341. return hash;
  342. }
  343. // Operators
  344. public toArray(array: number[] | Float32Array, index: number = 0): Vector2 {
  345. array[index] = this.x;
  346. array[index + 1] = this.y;
  347. return this;
  348. }
  349. public asArray(): number[] {
  350. var result = [];
  351. this.toArray(result, 0);
  352. return result;
  353. }
  354. public copyFrom(source: Vector2): Vector2 {
  355. this.x = source.x;
  356. this.y = source.y;
  357. return this;
  358. }
  359. public copyFromFloats(x: number, y: number): Vector2 {
  360. this.x = x;
  361. this.y = y;
  362. return this;
  363. }
  364. public add(otherVector: Vector2): Vector2 {
  365. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  366. }
  367. public addToRef(otherVector: Vector2, result: Vector2): Vector2 {
  368. result.x = this.x + otherVector.x;
  369. result.y = this.y + otherVector.y;
  370. return this;
  371. }
  372. public addInPlace(otherVector: Vector2): Vector2 {
  373. this.x += otherVector.x;
  374. this.y += otherVector.y;
  375. return this;
  376. }
  377. public addVector3(otherVector: Vector3): Vector2 {
  378. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  379. }
  380. public subtract(otherVector: Vector2): Vector2 {
  381. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  382. }
  383. public subtractToRef(otherVector: Vector2, result: Vector2): Vector2 {
  384. result.x = this.x - otherVector.x;
  385. result.y = this.y - otherVector.y;
  386. return this;
  387. }
  388. public subtractInPlace(otherVector: Vector2): Vector2 {
  389. this.x -= otherVector.x;
  390. this.y -= otherVector.y;
  391. return this;
  392. }
  393. public multiplyInPlace(otherVector: Vector2): Vector2 {
  394. this.x *= otherVector.x;
  395. this.y *= otherVector.y;
  396. return this;
  397. }
  398. public multiply(otherVector: Vector2): Vector2 {
  399. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  400. }
  401. public multiplyToRef(otherVector: Vector2, result: Vector2): Vector2 {
  402. result.x = this.x * otherVector.x;
  403. result.y = this.y * otherVector.y;
  404. return this;
  405. }
  406. public multiplyByFloats(x: number, y: number): Vector2 {
  407. return new Vector2(this.x * x, this.y * y);
  408. }
  409. public divide(otherVector: Vector2): Vector2 {
  410. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  411. }
  412. public divideToRef(otherVector: Vector2, result: Vector2): Vector2 {
  413. result.x = this.x / otherVector.x;
  414. result.y = this.y / otherVector.y;
  415. return this;
  416. }
  417. public negate(): Vector2 {
  418. return new Vector2(-this.x, -this.y);
  419. }
  420. public scaleInPlace(scale: number): Vector2 {
  421. this.x *= scale;
  422. this.y *= scale;
  423. return this;
  424. }
  425. public scale(scale: number): Vector2 {
  426. return new Vector2(this.x * scale, this.y * scale);
  427. }
  428. public equals(otherVector: Vector2): boolean {
  429. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  430. }
  431. public equalsWithEpsilon(otherVector: Vector2, epsilon: number = Epsilon): boolean {
  432. return otherVector && MathTools.WithinEpsilon(this.x, otherVector.x, epsilon) && MathTools.WithinEpsilon(this.y, otherVector.y, epsilon);
  433. }
  434. // Properties
  435. public length(): number {
  436. return Math.sqrt(this.x * this.x + this.y * this.y);
  437. }
  438. public lengthSquared(): number {
  439. return (this.x * this.x + this.y * this.y);
  440. }
  441. // Methods
  442. public normalize(): Vector2 {
  443. var len = this.length();
  444. if (len === 0)
  445. return this;
  446. var num = 1.0 / len;
  447. this.x *= num;
  448. this.y *= num;
  449. return this;
  450. }
  451. public clone(): Vector2 {
  452. return new Vector2(this.x, this.y);
  453. }
  454. // Statics
  455. public static Zero(): Vector2 {
  456. return new Vector2(0, 0);
  457. }
  458. public static FromArray(array: number[] | Float32Array, offset: number = 0): Vector2 {
  459. return new Vector2(array[offset], array[offset + 1]);
  460. }
  461. public static FromArrayToRef(array: number[] | Float32Array, offset: number, result: Vector2): void {
  462. result.x = array[offset];
  463. result.y = array[offset + 1];
  464. }
  465. public static CatmullRom(value1: Vector2, value2: Vector2, value3: Vector2, value4: Vector2, amount: number): Vector2 {
  466. var squared = amount * amount;
  467. var cubed = amount * squared;
  468. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  469. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  470. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  471. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  472. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  473. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  474. return new Vector2(x, y);
  475. }
  476. public static Clamp(value: Vector2, min: Vector2, max: Vector2): Vector2 {
  477. var x = value.x;
  478. x = (x > max.x) ? max.x : x;
  479. x = (x < min.x) ? min.x : x;
  480. var y = value.y;
  481. y = (y > max.y) ? max.y : y;
  482. y = (y < min.y) ? min.y : y;
  483. return new Vector2(x, y);
  484. }
  485. public static Hermite(value1: Vector2, tangent1: Vector2, value2: Vector2, tangent2: Vector2, amount: number): Vector2 {
  486. var squared = amount * amount;
  487. var cubed = amount * squared;
  488. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  489. var part2 = (-2.0 * cubed) + (3.0 * squared);
  490. var part3 = (cubed - (2.0 * squared)) + amount;
  491. var part4 = cubed - squared;
  492. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  493. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  494. return new Vector2(x, y);
  495. }
  496. public static Lerp(start: Vector2, end: Vector2, amount: number): Vector2 {
  497. var x = start.x + ((end.x - start.x) * amount);
  498. var y = start.y + ((end.y - start.y) * amount);
  499. return new Vector2(x, y);
  500. }
  501. public static Dot(left: Vector2, right: Vector2): number {
  502. return left.x * right.x + left.y * right.y;
  503. }
  504. public static Normalize(vector: Vector2): Vector2 {
  505. var newVector = vector.clone();
  506. newVector.normalize();
  507. return newVector;
  508. }
  509. public static Minimize(left: Vector2, right: Vector2): Vector2 {
  510. var x = (left.x < right.x) ? left.x : right.x;
  511. var y = (left.y < right.y) ? left.y : right.y;
  512. return new Vector2(x, y);
  513. }
  514. public static Maximize(left: Vector2, right: Vector2): Vector2 {
  515. var x = (left.x > right.x) ? left.x : right.x;
  516. var y = (left.y > right.y) ? left.y : right.y;
  517. return new Vector2(x, y);
  518. }
  519. public static Transform(vector: Vector2, transformation: Matrix): Vector2 {
  520. let r = Vector2.Zero();
  521. Vector2.TransformToRef(vector, transformation, r);
  522. return r;
  523. }
  524. public static TransformToRef(vector: Vector2, transformation: Matrix, result: Vector2) {
  525. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + transformation.m[12];
  526. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + transformation.m[13];
  527. result.x = x;
  528. result.y = y;
  529. }
  530. public static PointInTriangle(p: Vector2, p0: Vector2, p1: Vector2, p2: Vector2) {
  531. let a = 1 / 2 * (-p1.y * p2.x + p0.y * (-p1.x + p2.x) + p0.x * (p1.y - p2.y) + p1.x * p2.y);
  532. let sign = a < 0 ? -1 : 1;
  533. let s = (p0.y * p2.x - p0.x * p2.y + (p2.y - p0.y) * p.x + (p0.x - p2.x) * p.y) * sign;
  534. let t = (p0.x * p1.y - p0.y * p1.x + (p0.y - p1.y) * p.x + (p1.x - p0.x) * p.y) * sign;
  535. return s > 0 && t > 0 && (s + t) < 2 * a * sign;
  536. }
  537. public static Distance(value1: Vector2, value2: Vector2): number {
  538. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  539. }
  540. public static DistanceSquared(value1: Vector2, value2: Vector2): number {
  541. var x = value1.x - value2.x;
  542. var y = value1.y - value2.y;
  543. return (x * x) + (y * y);
  544. }
  545. public static Center(value1: Vector2, value2: Vector2): Vector2 {
  546. var center = value1.add(value2);
  547. center.scaleInPlace(0.5);
  548. return center;
  549. }
  550. public static DistanceOfPointFromSegment(p: Vector2, segA: Vector2, segB: Vector2): number {
  551. let l2 = Vector2.DistanceSquared(segA, segB);
  552. if (l2 === 0.0) {
  553. return Vector2.Distance(p, segA);
  554. }
  555. let v = segB.subtract(segA);
  556. let t = Math.max(0, Math.min(1, Vector2.Dot(p.subtract(segA), v) / l2));
  557. let proj = segA.add(v.multiplyByFloats(t, t));
  558. return Vector2.Distance(p, proj);
  559. }
  560. }
  561. export class Vector3 {
  562. constructor(public x: number, public y: number, public z: number) {
  563. }
  564. public toString(): string {
  565. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  566. }
  567. public getClassName(): string {
  568. return "Vector3";
  569. }
  570. public getHashCode(): number {
  571. let hash = this.x || 0;
  572. hash = (hash * 397) ^ (this.y || 0);
  573. hash = (hash * 397) ^ (this.z || 0);
  574. return hash;
  575. }
  576. // Operators
  577. public asArray(): number[] {
  578. var result: number[] = [];
  579. this.toArray(result, 0);
  580. return result;
  581. }
  582. public toArray(array: number[] | Float32Array, index: number = 0): Vector3 {
  583. array[index] = this.x;
  584. array[index + 1] = this.y;
  585. array[index + 2] = this.z;
  586. return this;
  587. }
  588. public toQuaternion(): Quaternion {
  589. var result = new Quaternion(0, 0, 0, 1);
  590. var cosxPlusz = Math.cos((this.x + this.z) * 0.5);
  591. var sinxPlusz = Math.sin((this.x + this.z) * 0.5);
  592. var coszMinusx = Math.cos((this.z - this.x) * 0.5);
  593. var sinzMinusx = Math.sin((this.z - this.x) * 0.5);
  594. var cosy = Math.cos(this.y * 0.5);
  595. var siny = Math.sin(this.y * 0.5);
  596. result.x = coszMinusx * siny;
  597. result.y = -sinzMinusx * siny;
  598. result.z = sinxPlusz * cosy;
  599. result.w = cosxPlusz * cosy;
  600. return result;
  601. }
  602. public addInPlace(otherVector: Vector3): Vector3 {
  603. this.x += otherVector.x;
  604. this.y += otherVector.y;
  605. this.z += otherVector.z;
  606. return this;
  607. }
  608. public add(otherVector: Vector3): Vector3 {
  609. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  610. }
  611. public addToRef(otherVector: Vector3, result: Vector3): Vector3 {
  612. result.x = this.x + otherVector.x;
  613. result.y = this.y + otherVector.y;
  614. result.z = this.z + otherVector.z;
  615. return this;
  616. }
  617. public subtractInPlace(otherVector: Vector3): Vector3 {
  618. this.x -= otherVector.x;
  619. this.y -= otherVector.y;
  620. this.z -= otherVector.z;
  621. return this;
  622. }
  623. public subtract(otherVector: Vector3): Vector3 {
  624. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  625. }
  626. public subtractToRef(otherVector: Vector3, result: Vector3): Vector3 {
  627. result.x = this.x - otherVector.x;
  628. result.y = this.y - otherVector.y;
  629. result.z = this.z - otherVector.z;
  630. return this;
  631. }
  632. public subtractFromFloats(x: number, y: number, z: number): Vector3 {
  633. return new Vector3(this.x - x, this.y - y, this.z - z);
  634. }
  635. public subtractFromFloatsToRef(x: number, y: number, z: number, result: Vector3): Vector3 {
  636. result.x = this.x - x;
  637. result.y = this.y - y;
  638. result.z = this.z - z;
  639. return this;
  640. }
  641. public negate(): Vector3 {
  642. return new Vector3(-this.x, -this.y, -this.z);
  643. }
  644. public scaleInPlace(scale: number): Vector3 {
  645. this.x *= scale;
  646. this.y *= scale;
  647. this.z *= scale;
  648. return this;
  649. }
  650. public scale(scale: number): Vector3 {
  651. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  652. }
  653. public scaleToRef(scale: number, result: Vector3) {
  654. result.x = this.x * scale;
  655. result.y = this.y * scale;
  656. result.z = this.z * scale;
  657. }
  658. public equals(otherVector: Vector3): boolean {
  659. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  660. }
  661. public equalsWithEpsilon(otherVector: Vector3, epsilon: number = Epsilon): boolean {
  662. return otherVector && MathTools.WithinEpsilon(this.x, otherVector.x, epsilon) && MathTools.WithinEpsilon(this.y, otherVector.y, epsilon) && MathTools.WithinEpsilon(this.z, otherVector.z, epsilon);
  663. }
  664. public equalsToFloats(x: number, y: number, z: number): boolean {
  665. return this.x === x && this.y === y && this.z === z;
  666. }
  667. public multiplyInPlace(otherVector: Vector3): Vector3 {
  668. this.x *= otherVector.x;
  669. this.y *= otherVector.y;
  670. this.z *= otherVector.z;
  671. return this;
  672. }
  673. public multiply(otherVector: Vector3): Vector3 {
  674. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  675. }
  676. public multiplyToRef(otherVector: Vector3, result: Vector3): Vector3 {
  677. result.x = this.x * otherVector.x;
  678. result.y = this.y * otherVector.y;
  679. result.z = this.z * otherVector.z;
  680. return this;
  681. }
  682. public multiplyByFloats(x: number, y: number, z: number): Vector3 {
  683. return new Vector3(this.x * x, this.y * y, this.z * z);
  684. }
  685. public divide(otherVector: Vector3): Vector3 {
  686. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  687. }
  688. public divideToRef(otherVector: Vector3, result: Vector3): Vector3 {
  689. result.x = this.x / otherVector.x;
  690. result.y = this.y / otherVector.y;
  691. result.z = this.z / otherVector.z;
  692. return this;
  693. }
  694. public MinimizeInPlace(other: Vector3): Vector3 {
  695. if (other.x < this.x) this.x = other.x;
  696. if (other.y < this.y) this.y = other.y;
  697. if (other.z < this.z) this.z = other.z;
  698. return this;
  699. }
  700. public MaximizeInPlace(other: Vector3): Vector3 {
  701. if (other.x > this.x) this.x = other.x;
  702. if (other.y > this.y) this.y = other.y;
  703. if (other.z > this.z) this.z = other.z;
  704. return this;
  705. }
  706. // Properties
  707. public length(): number {
  708. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  709. }
  710. public lengthSquared(): number {
  711. return (this.x * this.x + this.y * this.y + this.z * this.z);
  712. }
  713. // Methods
  714. public normalize(): Vector3 {
  715. var len = this.length();
  716. if (len === 0 || len === 1.0)
  717. return this;
  718. var num = 1.0 / len;
  719. this.x *= num;
  720. this.y *= num;
  721. this.z *= num;
  722. return this;
  723. }
  724. public clone(): Vector3 {
  725. return new Vector3(this.x, this.y, this.z);
  726. }
  727. public copyFrom(source: Vector3): Vector3 {
  728. this.x = source.x;
  729. this.y = source.y;
  730. this.z = source.z;
  731. return this;
  732. }
  733. public copyFromFloats(x: number, y: number, z: number): Vector3 {
  734. this.x = x;
  735. this.y = y;
  736. this.z = z;
  737. return this;
  738. }
  739. // Statics
  740. public static GetClipFactor(vector0: Vector3, vector1: Vector3, axis: Vector3, size) {
  741. var d0 = Vector3.Dot(vector0, axis) - size;
  742. var d1 = Vector3.Dot(vector1, axis) - size;
  743. var s = d0 / (d0 - d1);
  744. return s;
  745. }
  746. public static FromArray(array: number[] | Float32Array, offset?: number): Vector3 {
  747. if (!offset) {
  748. offset = 0;
  749. }
  750. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  751. }
  752. public static FromFloatArray(array: Float32Array, offset?: number): Vector3 {
  753. if (!offset) {
  754. offset = 0;
  755. }
  756. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  757. }
  758. public static FromArrayToRef(array: number[] | Float32Array, offset: number, result: Vector3): void {
  759. result.x = array[offset];
  760. result.y = array[offset + 1];
  761. result.z = array[offset + 2];
  762. }
  763. public static FromFloatArrayToRef(array: Float32Array, offset: number, result: Vector3): void {
  764. result.x = array[offset];
  765. result.y = array[offset + 1];
  766. result.z = array[offset + 2];
  767. }
  768. public static FromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
  769. result.x = x;
  770. result.y = y;
  771. result.z = z;
  772. }
  773. public static Zero(): Vector3 {
  774. return new Vector3(0, 0, 0);
  775. }
  776. public static Up(): Vector3 {
  777. return new Vector3(0, 1.0, 0);
  778. }
  779. public static Forward(): Vector3 {
  780. return new Vector3(0, 0, 1.0);
  781. }
  782. public static Right(): Vector3 {
  783. return new Vector3(1.0, 0, 0);
  784. }
  785. public static Left(): Vector3 {
  786. return new Vector3(-1.0, 0, 0);
  787. }
  788. public static TransformCoordinates(vector: Vector3, transformation: Matrix): Vector3 {
  789. var result = Vector3.Zero();
  790. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  791. return result;
  792. }
  793. public static TransformCoordinatesToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
  794. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  795. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  796. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  797. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  798. result.x = x / w;
  799. result.y = y / w;
  800. result.z = z / w;
  801. }
  802. public static TransformCoordinatesFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  803. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  804. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  805. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  806. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  807. result.x = rx / rw;
  808. result.y = ry / rw;
  809. result.z = rz / rw;
  810. }
  811. public static TransformNormal(vector: Vector3, transformation: Matrix): Vector3 {
  812. var result = Vector3.Zero();
  813. Vector3.TransformNormalToRef(vector, transformation, result);
  814. return result;
  815. }
  816. public static TransformNormalToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
  817. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  818. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  819. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  820. result.x = x;
  821. result.y = y;
  822. result.z = z;
  823. }
  824. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  825. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  826. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  827. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  828. }
  829. public static CatmullRom(value1: Vector3, value2: Vector3, value3: Vector3, value4: Vector3, amount: number): Vector3 {
  830. var squared = amount * amount;
  831. var cubed = amount * squared;
  832. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  833. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  834. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  835. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  836. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  837. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  838. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  839. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  840. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  841. return new Vector3(x, y, z);
  842. }
  843. public static Clamp(value: Vector3, min: Vector3, max: Vector3): Vector3 {
  844. var x = value.x;
  845. x = (x > max.x) ? max.x : x;
  846. x = (x < min.x) ? min.x : x;
  847. var y = value.y;
  848. y = (y > max.y) ? max.y : y;
  849. y = (y < min.y) ? min.y : y;
  850. var z = value.z;
  851. z = (z > max.z) ? max.z : z;
  852. z = (z < min.z) ? min.z : z;
  853. return new Vector3(x, y, z);
  854. }
  855. public static Hermite(value1: Vector3, tangent1: Vector3, value2: Vector3, tangent2: Vector3, amount: number): Vector3 {
  856. var squared = amount * amount;
  857. var cubed = amount * squared;
  858. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  859. var part2 = (-2.0 * cubed) + (3.0 * squared);
  860. var part3 = (cubed - (2.0 * squared)) + amount;
  861. var part4 = cubed - squared;
  862. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  863. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  864. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  865. return new Vector3(x, y, z);
  866. }
  867. public static Lerp(start: Vector3, end: Vector3, amount: number): Vector3 {
  868. var result = new Vector3(0, 0, 0);
  869. Vector3.LerpToRef(start, end, amount, result);
  870. return result;
  871. }
  872. public static LerpToRef(start: Vector3, end: Vector3, amount: number, result: Vector3): void {
  873. result.x = start.x + ((end.x - start.x) * amount);
  874. result.y = start.y + ((end.y - start.y) * amount);
  875. result.z = start.z + ((end.z - start.z) * amount);
  876. }
  877. public static Dot(left: Vector3, right: Vector3): number {
  878. return (left.x * right.x + left.y * right.y + left.z * right.z);
  879. }
  880. public static Cross(left: Vector3, right: Vector3): Vector3 {
  881. var result = Vector3.Zero();
  882. Vector3.CrossToRef(left, right, result);
  883. return result;
  884. }
  885. public static CrossToRef(left: Vector3, right: Vector3, result: Vector3): void {
  886. Tmp.Vector3[0].x = left.y * right.z - left.z * right.y;
  887. Tmp.Vector3[0].y = left.z * right.x - left.x * right.z;
  888. Tmp.Vector3[0].z = left.x * right.y - left.y * right.x;
  889. result.copyFrom(Tmp.Vector3[0]);
  890. }
  891. public static Normalize(vector: Vector3): Vector3 {
  892. var result = Vector3.Zero();
  893. Vector3.NormalizeToRef(vector, result);
  894. return result;
  895. }
  896. public static NormalizeToRef(vector: Vector3, result: Vector3): void {
  897. result.copyFrom(vector);
  898. result.normalize();
  899. }
  900. private static _viewportMatrixCache: Matrix;
  901. private static _matrixCache: Matrix;
  902. public static Project(vector: Vector3, world: Matrix, transform: Matrix, viewport: Viewport): Vector3 {
  903. var cw = viewport.width;
  904. var ch = viewport.height;
  905. var cx = viewport.x;
  906. var cy = viewport.y;
  907. var viewportMatrix = Vector3._viewportMatrixCache ? Vector3._viewportMatrixCache : (Vector3._viewportMatrixCache = new Matrix());
  908. Matrix.FromValuesToRef(
  909. cw / 2.0, 0, 0, 0,
  910. 0, -ch / 2.0, 0, 0,
  911. 0, 0, 1, 0,
  912. cx + cw / 2.0, ch / 2.0 + cy, 0, 1, viewportMatrix);
  913. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  914. world.multiplyToRef(transform, matrix);
  915. matrix.multiplyToRef(viewportMatrix, matrix);
  916. return Vector3.TransformCoordinates(vector, matrix);
  917. }
  918. public static UnprojectFromTransform(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, transform: Matrix): Vector3 {
  919. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  920. world.multiplyToRef(transform, matrix);
  921. matrix.invert();
  922. source.x = source.x / viewportWidth * 2 - 1;
  923. source.y = -(source.y / viewportHeight * 2 - 1);
  924. var vector = Vector3.TransformCoordinates(source, matrix);
  925. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  926. if (MathTools.WithinEpsilon(num, 1.0)) {
  927. vector = vector.scale(1.0 / num);
  928. }
  929. return vector;
  930. }
  931. public static Unproject(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Vector3 {
  932. var matrix = Vector3._matrixCache ? Vector3._matrixCache : (Vector3._matrixCache = new Matrix());
  933. world.multiplyToRef(view, matrix)
  934. matrix.multiplyToRef(projection, matrix);
  935. matrix.invert();
  936. var screenSource = new Vector3(source.x / viewportWidth * 2 - 1, -(source.y / viewportHeight * 2 - 1), source.z);
  937. var vector = Vector3.TransformCoordinates(screenSource, matrix);
  938. var num = screenSource.x * matrix.m[3] + screenSource.y * matrix.m[7] + screenSource.z * matrix.m[11] + matrix.m[15];
  939. if (MathTools.WithinEpsilon(num, 1.0)) {
  940. vector = vector.scale(1.0 / num);
  941. }
  942. return vector;
  943. }
  944. public static Minimize(left: Vector3, right: Vector3): Vector3 {
  945. var min = left.clone();
  946. min.MinimizeInPlace(right);
  947. return min;
  948. }
  949. public static Maximize(left: Vector3, right: Vector3): Vector3 {
  950. var max = left.clone();
  951. max.MaximizeInPlace(right);
  952. return max;
  953. }
  954. public static Distance(value1: Vector3, value2: Vector3): number {
  955. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  956. }
  957. public static DistanceSquared(value1: Vector3, value2: Vector3): number {
  958. var x = value1.x - value2.x;
  959. var y = value1.y - value2.y;
  960. var z = value1.z - value2.z;
  961. return (x * x) + (y * y) + (z * z);
  962. }
  963. public static Center(value1: Vector3, value2: Vector3): Vector3 {
  964. var center = value1.add(value2);
  965. center.scaleInPlace(0.5);
  966. return center;
  967. }
  968. /**
  969. * Given three orthogonal normalized left-handed oriented Vector3 axis in space (target system),
  970. * RotationFromAxis() returns the rotation Euler angles (ex : rotation.x, rotation.y, rotation.z) to apply
  971. * to something in order to rotate it from its local system to the given target system.
  972. */
  973. public static RotationFromAxis(axis1: Vector3, axis2: Vector3, axis3: Vector3): Vector3 {
  974. var rotation = Vector3.Zero();
  975. Vector3.RotationFromAxisToRef(axis1, axis2, axis3, rotation);
  976. return rotation;
  977. }
  978. /**
  979. * The same than RotationFromAxis but updates the passed ref Vector3 parameter.
  980. */
  981. public static RotationFromAxisToRef(axis1: Vector3, axis2: Vector3, axis3: Vector3, ref: Vector3): void {
  982. var u = axis1.normalize();
  983. var w = axis3.normalize();
  984. // world axis
  985. var X = Axis.X;
  986. var Y = Axis.Y;
  987. // equation unknowns and vars
  988. var yaw = 0.0;
  989. var pitch = 0.0;
  990. var roll = 0.0;
  991. var x = 0.0;
  992. var y = 0.0;
  993. var z = 0.0;
  994. var t = 0.0;
  995. var sign = -1.0;
  996. var nbRevert = 0;
  997. var cross: Vector3 = Tmp.Vector3[0];
  998. var dot = 0.0;
  999. // step 1 : rotation around w
  1000. // Rv3(u) = u1, and u1 belongs to plane xOz
  1001. // Rv3(w) = w1 = w invariant
  1002. var u1: Vector3 = Tmp.Vector3[1];
  1003. if (MathTools.WithinEpsilon(w.z, 0, Epsilon)) {
  1004. z = 1.0;
  1005. }
  1006. else if (MathTools.WithinEpsilon(w.x, 0, Epsilon)) {
  1007. x = 1.0;
  1008. }
  1009. else {
  1010. t = w.z / w.x;
  1011. x = - t * Math.sqrt(1 / (1 + t * t));
  1012. z = Math.sqrt(1 / (1 + t * t));
  1013. }
  1014. u1.x = x;
  1015. u1.y = y;
  1016. u1.z = z;
  1017. u1.normalize();
  1018. Vector3.CrossToRef(u, u1, cross); // returns same direction as w (=local z) if positive angle : cross(source, image)
  1019. cross.normalize();
  1020. if (Vector3.Dot(w, cross) < 0) {
  1021. sign = 1.0;
  1022. }
  1023. dot = Vector3.Dot(u, u1);
  1024. dot = (Math.min(1.0, Math.max(-1.0, dot))); // to force dot to be in the range [-1, 1]
  1025. roll = Math.acos(dot) * sign;
  1026. if (Vector3.Dot(u1, X) < 0) { // checks X orientation
  1027. roll = Math.PI + roll;
  1028. u1 = u1.scaleInPlace(-1);
  1029. nbRevert++;
  1030. }
  1031. // step 2 : rotate around u1
  1032. // Ru1(w1) = Ru1(w) = w2, and w2 belongs to plane xOz
  1033. // u1 is yet in xOz and invariant by Ru1, so after this step u1 and w2 will be in xOz
  1034. var w2: Vector3 = Tmp.Vector3[2];
  1035. var v2: Vector3 = Tmp.Vector3[3];
  1036. x = 0.0;
  1037. y = 0.0;
  1038. z = 0.0;
  1039. sign = -1.0;
  1040. if (MathTools.WithinEpsilon(w.z, 0, Epsilon)) {
  1041. x = 1.0;
  1042. }
  1043. else {
  1044. t = u1.z / u1.x;
  1045. x = - t * Math.sqrt(1 / (1 + t * t));
  1046. z = Math.sqrt(1 / (1 + t * t));
  1047. }
  1048. w2.x = x;
  1049. w2.y = y;
  1050. w2.z = z;
  1051. w2.normalize();
  1052. Vector3.CrossToRef(w2, u1, v2); // v2 image of v1 through rotation around u1
  1053. v2.normalize();
  1054. Vector3.CrossToRef(w, w2, cross); // returns same direction as u1 (=local x) if positive angle : cross(source, image)
  1055. cross.normalize();
  1056. if (Vector3.Dot(u1, cross) < 0) {
  1057. sign = 1.0;
  1058. }
  1059. dot = Vector3.Dot(w, w2);
  1060. dot = (Math.min(1.0, Math.max(-1.0, dot))); // to force dot to be in the range [-1, 1]
  1061. pitch = Math.acos(dot) * sign;
  1062. if (Vector3.Dot(v2, Y) < 0) { // checks for Y orientation
  1063. pitch = Math.PI + pitch;
  1064. nbRevert++;
  1065. }
  1066. // step 3 : rotate around v2
  1067. // Rv2(u1) = X, same as Rv2(w2) = Z, with X=(1,0,0) and Z=(0,0,1)
  1068. sign = -1.0;
  1069. Vector3.CrossToRef(X, u1, cross); // returns same direction as Y if positive angle : cross(source, image)
  1070. cross.normalize();
  1071. if (Vector3.Dot(cross, Y) < 0) {
  1072. sign = 1.0;
  1073. }
  1074. dot = Vector3.Dot(u1, X);
  1075. dot = (Math.min(1.0, Math.max(-1.0, dot))); // to force dot to be in the range [-1, 1]
  1076. yaw = - Math.acos(dot) * sign; // negative : plane zOx oriented clockwise
  1077. if (dot < 0 && nbRevert < 2) {
  1078. yaw = Math.PI + yaw;
  1079. }
  1080. ref.x = pitch;
  1081. ref.y = yaw;
  1082. ref.z = roll;
  1083. }
  1084. }
  1085. //Vector4 class created for EulerAngle class conversion to Quaternion
  1086. export class Vector4 {
  1087. constructor(public x: number, public y: number, public z: number, public w: number) { }
  1088. public toString(): string {
  1089. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1090. }
  1091. public getClassName(): string {
  1092. return "Vector4";
  1093. }
  1094. public getHashCode(): number {
  1095. let hash = this.x || 0;
  1096. hash = (hash * 397) ^ (this.y || 0);
  1097. hash = (hash * 397) ^ (this.z || 0);
  1098. hash = (hash * 397) ^ (this.w || 0);
  1099. return hash;
  1100. }
  1101. // Operators
  1102. public asArray(): number[] {
  1103. var result = [];
  1104. this.toArray(result, 0);
  1105. return result;
  1106. }
  1107. public toArray(array: number[], index?: number): Vector4 {
  1108. if (index === undefined) {
  1109. index = 0;
  1110. }
  1111. array[index] = this.x;
  1112. array[index + 1] = this.y;
  1113. array[index + 2] = this.z;
  1114. array[index + 3] = this.w;
  1115. return this;
  1116. }
  1117. public addInPlace(otherVector: Vector4): Vector4 {
  1118. this.x += otherVector.x;
  1119. this.y += otherVector.y;
  1120. this.z += otherVector.z;
  1121. this.w += otherVector.w;
  1122. return this;
  1123. }
  1124. public add(otherVector: Vector4): Vector4 {
  1125. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  1126. }
  1127. public addToRef(otherVector: Vector4, result: Vector4): Vector4 {
  1128. result.x = this.x + otherVector.x;
  1129. result.y = this.y + otherVector.y;
  1130. result.z = this.z + otherVector.z;
  1131. result.w = this.w + otherVector.w;
  1132. return this;
  1133. }
  1134. public subtractInPlace(otherVector: Vector4): Vector4 {
  1135. this.x -= otherVector.x;
  1136. this.y -= otherVector.y;
  1137. this.z -= otherVector.z;
  1138. this.w -= otherVector.w;
  1139. return this;
  1140. }
  1141. public subtract(otherVector: Vector4): Vector4 {
  1142. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  1143. }
  1144. public subtractToRef(otherVector: Vector4, result: Vector4): Vector4 {
  1145. result.x = this.x - otherVector.x;
  1146. result.y = this.y - otherVector.y;
  1147. result.z = this.z - otherVector.z;
  1148. result.w = this.w - otherVector.w;
  1149. return this;
  1150. }
  1151. public subtractFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  1152. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  1153. }
  1154. public subtractFromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): Vector4 {
  1155. result.x = this.x - x;
  1156. result.y = this.y - y;
  1157. result.z = this.z - z;
  1158. result.w = this.w - w;
  1159. return this;
  1160. }
  1161. public negate(): Vector4 {
  1162. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  1163. }
  1164. public scaleInPlace(scale: number): Vector4 {
  1165. this.x *= scale;
  1166. this.y *= scale;
  1167. this.z *= scale;
  1168. this.w *= scale;
  1169. return this;
  1170. }
  1171. public scale(scale: number): Vector4 {
  1172. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  1173. }
  1174. public scaleToRef(scale: number, result: Vector4) {
  1175. result.x = this.x * scale;
  1176. result.y = this.y * scale;
  1177. result.z = this.z * scale;
  1178. result.w = this.w * scale;
  1179. }
  1180. public equals(otherVector: Vector4): boolean {
  1181. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  1182. }
  1183. public equalsWithEpsilon(otherVector: Vector4, epsilon: number = Epsilon): boolean {
  1184. return otherVector
  1185. && MathTools.WithinEpsilon(this.x, otherVector.x, epsilon)
  1186. && MathTools.WithinEpsilon(this.y, otherVector.y, epsilon)
  1187. && MathTools.WithinEpsilon(this.z, otherVector.z, epsilon)
  1188. && MathTools.WithinEpsilon(this.w, otherVector.w, epsilon);
  1189. }
  1190. public equalsToFloats(x: number, y: number, z: number, w: number): boolean {
  1191. return this.x === x && this.y === y && this.z === z && this.w === w;
  1192. }
  1193. public multiplyInPlace(otherVector: Vector4): Vector4 {
  1194. this.x *= otherVector.x;
  1195. this.y *= otherVector.y;
  1196. this.z *= otherVector.z;
  1197. this.w *= otherVector.w;
  1198. return this;
  1199. }
  1200. public multiply(otherVector: Vector4): Vector4 {
  1201. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  1202. }
  1203. public multiplyToRef(otherVector: Vector4, result: Vector4): Vector4 {
  1204. result.x = this.x * otherVector.x;
  1205. result.y = this.y * otherVector.y;
  1206. result.z = this.z * otherVector.z;
  1207. result.w = this.w * otherVector.w;
  1208. return this;
  1209. }
  1210. public multiplyByFloats(x: number, y: number, z: number, w: number): Vector4 {
  1211. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  1212. }
  1213. public divide(otherVector: Vector4): Vector4 {
  1214. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  1215. }
  1216. public divideToRef(otherVector: Vector4, result: Vector4): Vector4 {
  1217. result.x = this.x / otherVector.x;
  1218. result.y = this.y / otherVector.y;
  1219. result.z = this.z / otherVector.z;
  1220. result.w = this.w / otherVector.w;
  1221. return this;
  1222. }
  1223. public MinimizeInPlace(other: Vector4): Vector4 {
  1224. if (other.x < this.x) this.x = other.x;
  1225. if (other.y < this.y) this.y = other.y;
  1226. if (other.z < this.z) this.z = other.z;
  1227. if (other.w < this.w) this.w = other.w;
  1228. return this;
  1229. }
  1230. public MaximizeInPlace(other: Vector4): Vector4 {
  1231. if (other.x > this.x) this.x = other.x;
  1232. if (other.y > this.y) this.y = other.y;
  1233. if (other.z > this.z) this.z = other.z;
  1234. if (other.w > this.w) this.w = other.w;
  1235. return this;
  1236. }
  1237. // Properties
  1238. public length(): number {
  1239. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  1240. }
  1241. public lengthSquared(): number {
  1242. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  1243. }
  1244. // Methods
  1245. public normalize(): Vector4 {
  1246. var len = this.length();
  1247. if (len === 0)
  1248. return this;
  1249. var num = 1.0 / len;
  1250. this.x *= num;
  1251. this.y *= num;
  1252. this.z *= num;
  1253. this.w *= num;
  1254. return this;
  1255. }
  1256. public toVector3(): Vector3 {
  1257. return new Vector3(this.x, this.y, this.z);
  1258. }
  1259. public clone(): Vector4 {
  1260. return new Vector4(this.x, this.y, this.z, this.w);
  1261. }
  1262. public copyFrom(source: Vector4): Vector4 {
  1263. this.x = source.x;
  1264. this.y = source.y;
  1265. this.z = source.z;
  1266. this.w = source.w;
  1267. return this;
  1268. }
  1269. public copyFromFloats(x: number, y: number, z: number, w: number): Vector4 {
  1270. this.x = x;
  1271. this.y = y;
  1272. this.z = z;
  1273. this.w = w;
  1274. return this;
  1275. }
  1276. // Statics
  1277. public static FromArray(array: number[], offset?: number): Vector4 {
  1278. if (!offset) {
  1279. offset = 0;
  1280. }
  1281. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1282. }
  1283. public static FromArrayToRef(array: number[], offset: number, result: Vector4): void {
  1284. result.x = array[offset];
  1285. result.y = array[offset + 1];
  1286. result.z = array[offset + 2];
  1287. result.w = array[offset + 3];
  1288. }
  1289. public static FromFloatArrayToRef(array: Float32Array, offset: number, result: Vector4): void {
  1290. result.x = array[offset];
  1291. result.y = array[offset + 1];
  1292. result.z = array[offset + 2];
  1293. result.w = array[offset + 3];
  1294. }
  1295. public static FromFloatsToRef(x: number, y: number, z: number, w: number, result: Vector4): void {
  1296. result.x = x;
  1297. result.y = y;
  1298. result.z = z;
  1299. result.w = w;
  1300. }
  1301. public static Zero(): Vector4 {
  1302. return new Vector4(0, 0, 0, 0);
  1303. }
  1304. public static Normalize(vector: Vector4): Vector4 {
  1305. var result = Vector4.Zero();
  1306. Vector4.NormalizeToRef(vector, result);
  1307. return result;
  1308. }
  1309. public static NormalizeToRef(vector: Vector4, result: Vector4): void {
  1310. result.copyFrom(vector);
  1311. result.normalize();
  1312. }
  1313. public static Minimize(left: Vector4, right: Vector4): Vector4 {
  1314. var min = left.clone();
  1315. min.MinimizeInPlace(right);
  1316. return min;
  1317. }
  1318. public static Maximize(left: Vector4, right: Vector4): Vector4 {
  1319. var max = left.clone();
  1320. max.MaximizeInPlace(right);
  1321. return max;
  1322. }
  1323. public static Distance(value1: Vector4, value2: Vector4): number {
  1324. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  1325. }
  1326. public static DistanceSquared(value1: Vector4, value2: Vector4): number {
  1327. var x = value1.x - value2.x;
  1328. var y = value1.y - value2.y;
  1329. var z = value1.z - value2.z;
  1330. var w = value1.w - value2.w;
  1331. return (x * x) + (y * y) + (z * z) + (w * w);
  1332. }
  1333. public static Center(value1: Vector4, value2: Vector4): Vector4 {
  1334. var center = value1.add(value2);
  1335. center.scaleInPlace(0.5);
  1336. return center;
  1337. }
  1338. }
  1339. export interface ISize {
  1340. width: number;
  1341. height: number;
  1342. }
  1343. export class Size implements ISize {
  1344. width: number;
  1345. height: number;
  1346. public constructor(width: number, height: number) {
  1347. this.width = width;
  1348. this.height = height;
  1349. }
  1350. public toString(): string {
  1351. return `{W: ${this.width}, H: ${this.height}}`;
  1352. }
  1353. public getClassName(): string {
  1354. return "Size";
  1355. }
  1356. public getHashCode(): number {
  1357. let hash = this.width || 0;
  1358. hash = (hash * 397) ^ (this.height || 0);
  1359. return hash;
  1360. }
  1361. public copyFrom(src: Size) {
  1362. this.width = src.width;
  1363. this.height = src.height;
  1364. }
  1365. public copyFromFloats(width: number, height: number) {
  1366. this.width = width;
  1367. this.height = height;
  1368. }
  1369. public multiplyByFloats(w: number, h: number): Size {
  1370. return new Size(this.width * w, this.height * h);
  1371. }
  1372. public clone(): Size {
  1373. return new Size(this.width, this.height);
  1374. }
  1375. public equals(other: Size): boolean {
  1376. if (!other) {
  1377. return false;
  1378. }
  1379. return (this.width === other.width) && (this.height === other.height);
  1380. }
  1381. public get surface(): number {
  1382. return this.width * this.height;
  1383. }
  1384. public static Zero(): Size {
  1385. return new Size(0, 0);
  1386. }
  1387. public add(otherSize: Size): Size {
  1388. let r = new Size(this.width + otherSize.width, this.height + otherSize.height);
  1389. return r;
  1390. }
  1391. public substract(otherSize: Size): Size {
  1392. let r = new Size(this.width - otherSize.width, this.height - otherSize.height);
  1393. return r;
  1394. }
  1395. public static Lerp(start: Size, end: Size, amount: number): Size {
  1396. var w = start.width + ((end.width - start.width) * amount);
  1397. var h = start.height + ((end.height - start.height) * amount);
  1398. return new Size(w, h);
  1399. }
  1400. }
  1401. export class Quaternion {
  1402. constructor(public x: number = 0, public y: number = 0, public z: number = 0, public w: number = 1) {
  1403. }
  1404. public toString(): string {
  1405. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1406. }
  1407. public getClassName(): string {
  1408. return "Quaternion";
  1409. }
  1410. public getHashCode(): number {
  1411. let hash = this.x || 0;
  1412. hash = (hash * 397) ^ (this.y || 0);
  1413. hash = (hash * 397) ^ (this.z || 0);
  1414. hash = (hash * 397) ^ (this.w || 0);
  1415. return hash;
  1416. }
  1417. public asArray(): number[] {
  1418. return [this.x, this.y, this.z, this.w];
  1419. }
  1420. public equals(otherQuaternion: Quaternion): boolean {
  1421. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  1422. }
  1423. public clone(): Quaternion {
  1424. return new Quaternion(this.x, this.y, this.z, this.w);
  1425. }
  1426. public copyFrom(other: Quaternion): Quaternion {
  1427. this.x = other.x;
  1428. this.y = other.y;
  1429. this.z = other.z;
  1430. this.w = other.w;
  1431. return this;
  1432. }
  1433. public copyFromFloats(x: number, y: number, z: number, w: number): Quaternion {
  1434. this.x = x;
  1435. this.y = y;
  1436. this.z = z;
  1437. this.w = w;
  1438. return this;
  1439. }
  1440. public add(other: Quaternion): Quaternion {
  1441. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  1442. }
  1443. public subtract(other: Quaternion): Quaternion {
  1444. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  1445. }
  1446. public scale(value: number): Quaternion {
  1447. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  1448. }
  1449. public multiply(q1: Quaternion): Quaternion {
  1450. var result = new Quaternion(0, 0, 0, 1.0);
  1451. this.multiplyToRef(q1, result);
  1452. return result;
  1453. }
  1454. public multiplyToRef(q1: Quaternion, result: Quaternion): Quaternion {
  1455. var x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  1456. var y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  1457. var z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  1458. var w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  1459. result.copyFromFloats(x, y, z, w);
  1460. return this;
  1461. }
  1462. public multiplyInPlace(q1: Quaternion): Quaternion {
  1463. this.multiplyToRef(q1, this);
  1464. return this;
  1465. }
  1466. public conjugateToRef(ref: Quaternion): Quaternion {
  1467. ref.copyFromFloats(-this.x, -this.y, -this.z, this.w);
  1468. return this;
  1469. }
  1470. public conjugateInPlace(): Quaternion {
  1471. this.x *= -1;
  1472. this.y *= -1;
  1473. this.z *= -1;
  1474. return this;
  1475. }
  1476. public conjugate(): Quaternion {
  1477. var result = new Quaternion(-this.x, -this.y, -this.z, this.w);
  1478. return result;
  1479. }
  1480. public length(): number {
  1481. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  1482. }
  1483. public normalize(): Quaternion {
  1484. var length = 1.0 / this.length();
  1485. this.x *= length;
  1486. this.y *= length;
  1487. this.z *= length;
  1488. this.w *= length;
  1489. return this;
  1490. }
  1491. public toEulerAngles(order = "YZX"): Vector3 {
  1492. var result = Vector3.Zero();
  1493. this.toEulerAnglesToRef(result, order);
  1494. return result;
  1495. }
  1496. public toEulerAnglesToRef(result: Vector3, order = "YZX"): Quaternion {
  1497. var qz = this.z;
  1498. var qx = this.x;
  1499. var qy = this.y;
  1500. var qw = this.w;
  1501. var sqw = qw * qw;
  1502. var sqz = qz * qz;
  1503. var sqx = qx * qx;
  1504. var sqy = qy * qy;
  1505. var zAxisY = qy*qz - qx*qw;
  1506. var limit = .4999999;
  1507. if(zAxisY < -limit){
  1508. result.y = 2 * Math.atan2(qy,qw);
  1509. result.x = Math.PI/2;
  1510. result.z = 0;
  1511. }else if(zAxisY > limit){
  1512. result.y = 2 * Math.atan2(qy,qw);
  1513. result.x = -Math.PI/2;
  1514. result.z = 0;
  1515. }else{
  1516. result.z = Math.atan2(2.0 * (qx * qy + qz * qw), (-sqz - sqx + sqy + sqw));
  1517. result.x = Math.asin(-2.0 * (qz * qy - qx * qw));
  1518. result.y = Math.atan2(2.0 * (qz * qx + qy * qw), (sqz - sqx - sqy + sqw));
  1519. }
  1520. return this;
  1521. }
  1522. public toRotationMatrix(result: Matrix): Quaternion {
  1523. var xx = this.x * this.x;
  1524. var yy = this.y * this.y;
  1525. var zz = this.z * this.z;
  1526. var xy = this.x * this.y;
  1527. var zw = this.z * this.w;
  1528. var zx = this.z * this.x;
  1529. var yw = this.y * this.w;
  1530. var yz = this.y * this.z;
  1531. var xw = this.x * this.w;
  1532. result.m[0] = 1.0 - (2.0 * (yy + zz));
  1533. result.m[1] = 2.0 * (xy + zw);
  1534. result.m[2] = 2.0 * (zx - yw);
  1535. result.m[3] = 0;
  1536. result.m[4] = 2.0 * (xy - zw);
  1537. result.m[5] = 1.0 - (2.0 * (zz + xx));
  1538. result.m[6] = 2.0 * (yz + xw);
  1539. result.m[7] = 0;
  1540. result.m[8] = 2.0 * (zx + yw);
  1541. result.m[9] = 2.0 * (yz - xw);
  1542. result.m[10] = 1.0 - (2.0 * (yy + xx));
  1543. result.m[11] = 0;
  1544. result.m[12] = 0;
  1545. result.m[13] = 0;
  1546. result.m[14] = 0;
  1547. result.m[15] = 1.0;
  1548. return this;
  1549. }
  1550. public fromRotationMatrix(matrix: Matrix): Quaternion {
  1551. Quaternion.FromRotationMatrixToRef(matrix, this);
  1552. return this;
  1553. }
  1554. // Statics
  1555. public static FromRotationMatrix(matrix: Matrix): Quaternion {
  1556. var result = new Quaternion();
  1557. Quaternion.FromRotationMatrixToRef(matrix, result);
  1558. return result;
  1559. }
  1560. public static FromRotationMatrixToRef(matrix: Matrix, result: Quaternion): void {
  1561. var data = matrix.m;
  1562. var m11 = data[0], m12 = data[4], m13 = data[8];
  1563. var m21 = data[1], m22 = data[5], m23 = data[9];
  1564. var m31 = data[2], m32 = data[6], m33 = data[10];
  1565. var trace = m11 + m22 + m33;
  1566. var s;
  1567. if (trace > 0) {
  1568. s = 0.5 / Math.sqrt(trace + 1.0);
  1569. result.w = 0.25 / s;
  1570. result.x = (m32 - m23) * s;
  1571. result.y = (m13 - m31) * s;
  1572. result.z = (m21 - m12) * s;
  1573. } else if (m11 > m22 && m11 > m33) {
  1574. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  1575. result.w = (m32 - m23) / s;
  1576. result.x = 0.25 * s;
  1577. result.y = (m12 + m21) / s;
  1578. result.z = (m13 + m31) / s;
  1579. } else if (m22 > m33) {
  1580. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  1581. result.w = (m13 - m31) / s;
  1582. result.x = (m12 + m21) / s;
  1583. result.y = 0.25 * s;
  1584. result.z = (m23 + m32) / s;
  1585. } else {
  1586. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  1587. result.w = (m21 - m12) / s;
  1588. result.x = (m13 + m31) / s;
  1589. result.y = (m23 + m32) / s;
  1590. result.z = 0.25 * s;
  1591. }
  1592. }
  1593. public static Inverse(q: Quaternion): Quaternion {
  1594. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  1595. }
  1596. public static Identity(): Quaternion {
  1597. return new Quaternion(0, 0, 0, 1);
  1598. }
  1599. public static RotationAxis(axis: Vector3, angle: number): Quaternion {
  1600. return Quaternion.RotationAxisToRef(axis, angle, new Quaternion());
  1601. }
  1602. public static RotationAxisToRef(axis: Vector3, angle: number, result: Quaternion): Quaternion {
  1603. var sin = Math.sin(angle / 2);
  1604. axis.normalize();
  1605. result.w = Math.cos(angle / 2);
  1606. result.x = axis.x * sin;
  1607. result.y = axis.y * sin;
  1608. result.z = axis.z * sin;
  1609. return result;
  1610. }
  1611. public static FromArray(array: number[], offset?: number): Quaternion {
  1612. if (!offset) {
  1613. offset = 0;
  1614. }
  1615. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1616. }
  1617. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Quaternion {
  1618. var q = new Quaternion();
  1619. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, q);
  1620. return q;
  1621. }
  1622. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Quaternion): void {
  1623. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  1624. var halfRoll = roll * 0.5;
  1625. var halfPitch = pitch * 0.5;
  1626. var halfYaw = yaw * 0.5;
  1627. var sinRoll = Math.sin(halfRoll);
  1628. var cosRoll = Math.cos(halfRoll);
  1629. var sinPitch = Math.sin(halfPitch);
  1630. var cosPitch = Math.cos(halfPitch);
  1631. var sinYaw = Math.sin(halfYaw);
  1632. var cosYaw = Math.cos(halfYaw);
  1633. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  1634. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  1635. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  1636. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  1637. }
  1638. public static RotationAlphaBetaGamma(alpha: number, beta: number, gamma: number): Quaternion {
  1639. var result = new Quaternion();
  1640. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  1641. return result;
  1642. }
  1643. public static RotationAlphaBetaGammaToRef(alpha: number, beta: number, gamma: number, result: Quaternion): void {
  1644. // Produces a quaternion from Euler angles in the z-x-z orientation
  1645. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  1646. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  1647. var halfBeta = beta * 0.5;
  1648. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1649. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1650. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1651. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1652. }
  1653. public static Slerp(left: Quaternion, right: Quaternion, amount: number): Quaternion {
  1654. var result = Quaternion.Identity();
  1655. Quaternion.SlerpToRef(left, right, amount, result);
  1656. return result;
  1657. }
  1658. public static SlerpToRef(left: Quaternion, right: Quaternion, amount: number, result:Quaternion): void {
  1659. var num2;
  1660. var num3;
  1661. var num = amount;
  1662. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  1663. var flag = false;
  1664. if (num4 < 0) {
  1665. flag = true;
  1666. num4 = -num4;
  1667. }
  1668. if (num4 > 0.999999) {
  1669. num3 = 1 - num;
  1670. num2 = flag ? -num : num;
  1671. }
  1672. else {
  1673. var num5 = Math.acos(num4);
  1674. var num6 = (1.0 / Math.sin(num5));
  1675. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  1676. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  1677. }
  1678. result.x = (num3 * left.x) + (num2 * right.x);
  1679. result.y = (num3 * left.y) + (num2 * right.y);
  1680. result.z = (num3 * left.z) + (num2 * right.z);
  1681. result.w = (num3 * left.w) + (num2 * right.w);
  1682. }
  1683. }
  1684. export class Matrix {
  1685. private static _tempQuaternion: Quaternion = new Quaternion();
  1686. private static _xAxis: Vector3 = Vector3.Zero();
  1687. private static _yAxis: Vector3 = Vector3.Zero();
  1688. private static _zAxis: Vector3 = Vector3.Zero();
  1689. public m: Float32Array = new Float32Array(16);
  1690. // Properties
  1691. public isIdentity(): boolean {
  1692. if (this.m[0] !== 1.0 || this.m[5] !== 1.0 || this.m[10] !== 1.0 || this.m[15] !== 1.0)
  1693. return false;
  1694. if (this.m[1] !== 0.0 || this.m[2] !== 0.0 || this.m[3] !== 0.0 ||
  1695. this.m[4] !== 0.0 || this.m[6] !== 0.0 || this.m[7] !== 0.0 ||
  1696. this.m[8] !== 0.0 || this.m[9] !== 0.0 || this.m[11] !== 0.0 ||
  1697. this.m[12] !== 0.0 || this.m[13] !== 0.0 || this.m[14] !== 0.0)
  1698. return false;
  1699. return true;
  1700. }
  1701. public determinant(): number {
  1702. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  1703. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  1704. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  1705. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  1706. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  1707. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  1708. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  1709. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  1710. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  1711. }
  1712. // Methods
  1713. public toArray(): Float32Array {
  1714. return this.m;
  1715. }
  1716. public asArray(): Float32Array {
  1717. return this.toArray();
  1718. }
  1719. public invert(): Matrix {
  1720. this.invertToRef(this);
  1721. return this;
  1722. }
  1723. public reset(): Matrix {
  1724. for (var index = 0; index < 16; index++) {
  1725. this.m[index] = 0;
  1726. }
  1727. return this;
  1728. }
  1729. public add(other: Matrix): Matrix {
  1730. var result = new Matrix();
  1731. this.addToRef(other, result);
  1732. return result;
  1733. }
  1734. public addToRef(other: Matrix, result: Matrix): Matrix {
  1735. for (var index = 0; index < 16; index++) {
  1736. result.m[index] = this.m[index] + other.m[index];
  1737. }
  1738. return this;
  1739. }
  1740. public addToSelf(other: Matrix): Matrix {
  1741. for (var index = 0; index < 16; index++) {
  1742. this.m[index] += other.m[index];
  1743. }
  1744. return this;
  1745. }
  1746. public invertToRef(other: Matrix): Matrix {
  1747. var l1 = this.m[0];
  1748. var l2 = this.m[1];
  1749. var l3 = this.m[2];
  1750. var l4 = this.m[3];
  1751. var l5 = this.m[4];
  1752. var l6 = this.m[5];
  1753. var l7 = this.m[6];
  1754. var l8 = this.m[7];
  1755. var l9 = this.m[8];
  1756. var l10 = this.m[9];
  1757. var l11 = this.m[10];
  1758. var l12 = this.m[11];
  1759. var l13 = this.m[12];
  1760. var l14 = this.m[13];
  1761. var l15 = this.m[14];
  1762. var l16 = this.m[15];
  1763. var l17 = (l11 * l16) - (l12 * l15);
  1764. var l18 = (l10 * l16) - (l12 * l14);
  1765. var l19 = (l10 * l15) - (l11 * l14);
  1766. var l20 = (l9 * l16) - (l12 * l13);
  1767. var l21 = (l9 * l15) - (l11 * l13);
  1768. var l22 = (l9 * l14) - (l10 * l13);
  1769. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  1770. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  1771. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  1772. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  1773. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  1774. var l28 = (l7 * l16) - (l8 * l15);
  1775. var l29 = (l6 * l16) - (l8 * l14);
  1776. var l30 = (l6 * l15) - (l7 * l14);
  1777. var l31 = (l5 * l16) - (l8 * l13);
  1778. var l32 = (l5 * l15) - (l7 * l13);
  1779. var l33 = (l5 * l14) - (l6 * l13);
  1780. var l34 = (l7 * l12) - (l8 * l11);
  1781. var l35 = (l6 * l12) - (l8 * l10);
  1782. var l36 = (l6 * l11) - (l7 * l10);
  1783. var l37 = (l5 * l12) - (l8 * l9);
  1784. var l38 = (l5 * l11) - (l7 * l9);
  1785. var l39 = (l5 * l10) - (l6 * l9);
  1786. other.m[0] = l23 * l27;
  1787. other.m[4] = l24 * l27;
  1788. other.m[8] = l25 * l27;
  1789. other.m[12] = l26 * l27;
  1790. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  1791. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  1792. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  1793. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  1794. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  1795. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  1796. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  1797. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  1798. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  1799. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  1800. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  1801. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  1802. return this;
  1803. }
  1804. public setTranslation(vector3: Vector3): Matrix {
  1805. this.m[12] = vector3.x;
  1806. this.m[13] = vector3.y;
  1807. this.m[14] = vector3.z;
  1808. return this;
  1809. }
  1810. public getTranslation(): Vector3 {
  1811. return new Vector3(this.m[12], this.m[13], this.m[14]);
  1812. }
  1813. public multiply(other: Matrix): Matrix {
  1814. var result = new Matrix();
  1815. this.multiplyToRef(other, result);
  1816. return result;
  1817. }
  1818. public copyFrom(other: Matrix): Matrix {
  1819. for (var index = 0; index < 16; index++) {
  1820. this.m[index] = other.m[index];
  1821. }
  1822. return this;
  1823. }
  1824. public copyToArray(array: Float32Array, offset: number = 0): Matrix {
  1825. for (var index = 0; index < 16; index++) {
  1826. array[offset + index] = this.m[index];
  1827. }
  1828. return this;
  1829. }
  1830. public multiplyToRef(other: Matrix, result: Matrix): Matrix {
  1831. this.multiplyToArray(other, result.m, 0);
  1832. return this;
  1833. }
  1834. public multiplyToArray(other: Matrix, result: Float32Array, offset: number): Matrix {
  1835. var tm0 = this.m[0];
  1836. var tm1 = this.m[1];
  1837. var tm2 = this.m[2];
  1838. var tm3 = this.m[3];
  1839. var tm4 = this.m[4];
  1840. var tm5 = this.m[5];
  1841. var tm6 = this.m[6];
  1842. var tm7 = this.m[7];
  1843. var tm8 = this.m[8];
  1844. var tm9 = this.m[9];
  1845. var tm10 = this.m[10];
  1846. var tm11 = this.m[11];
  1847. var tm12 = this.m[12];
  1848. var tm13 = this.m[13];
  1849. var tm14 = this.m[14];
  1850. var tm15 = this.m[15];
  1851. var om0 = other.m[0];
  1852. var om1 = other.m[1];
  1853. var om2 = other.m[2];
  1854. var om3 = other.m[3];
  1855. var om4 = other.m[4];
  1856. var om5 = other.m[5];
  1857. var om6 = other.m[6];
  1858. var om7 = other.m[7];
  1859. var om8 = other.m[8];
  1860. var om9 = other.m[9];
  1861. var om10 = other.m[10];
  1862. var om11 = other.m[11];
  1863. var om12 = other.m[12];
  1864. var om13 = other.m[13];
  1865. var om14 = other.m[14];
  1866. var om15 = other.m[15];
  1867. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1868. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1869. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1870. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1871. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1872. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1873. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1874. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1875. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1876. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1877. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1878. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1879. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1880. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1881. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1882. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1883. return this;
  1884. }
  1885. public equals(value: Matrix): boolean {
  1886. return value &&
  1887. (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
  1888. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  1889. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  1890. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  1891. }
  1892. public clone(): Matrix {
  1893. return Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  1894. this.m[4], this.m[5], this.m[6], this.m[7],
  1895. this.m[8], this.m[9], this.m[10], this.m[11],
  1896. this.m[12], this.m[13], this.m[14], this.m[15]);
  1897. }
  1898. public getClassName(): string {
  1899. return "Matrix";
  1900. }
  1901. public getHashCode(): number {
  1902. let hash = this.m[0] || 0;
  1903. for (let i = 1; i < 16; i++) {
  1904. hash = (hash * 397) ^ (this.m[i] || 0);
  1905. }
  1906. return hash;
  1907. }
  1908. public decompose(scale: Vector3, rotation: Quaternion, translation: Vector3): boolean {
  1909. translation.x = this.m[12];
  1910. translation.y = this.m[13];
  1911. translation.z = this.m[14];
  1912. var xs = MathTools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
  1913. var ys = MathTools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
  1914. var zs = MathTools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
  1915. scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
  1916. scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
  1917. scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
  1918. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  1919. rotation.x = 0;
  1920. rotation.y = 0;
  1921. rotation.z = 0;
  1922. rotation.w = 1;
  1923. return false;
  1924. }
  1925. Matrix.FromValuesToRef(
  1926. this.m[0] / scale.x, this.m[1] / scale.x, this.m[2] / scale.x, 0,
  1927. this.m[4] / scale.y, this.m[5] / scale.y, this.m[6] / scale.y, 0,
  1928. this.m[8] / scale.z, this.m[9] / scale.z, this.m[10] / scale.z, 0,
  1929. 0, 0, 0, 1, Tmp.Matrix[0]);
  1930. Quaternion.FromRotationMatrixToRef(Tmp.Matrix[0], rotation);
  1931. return true;
  1932. }
  1933. public getRotationMatrix(): Matrix{
  1934. var result = Matrix.Identity();
  1935. this.getRotationMatrixToRef(result);
  1936. return result;
  1937. }
  1938. public getRotationMatrixToRef(result:Matrix): void{
  1939. var m = this.m;
  1940. var xs = m[0] * m[1] * m[2] * m[3] < 0 ? -1 : 1;
  1941. var ys = m[4] * m[5] * m[6] * m[7] < 0 ? -1 : 1;
  1942. var zs = m[8] * m[9] * m[10] * m[11] < 0 ? -1 : 1;
  1943. var sx = xs * Math.sqrt(m[0] * m[0] + m[1] * m[1] + m[2] * m[2]);
  1944. var sy = ys * Math.sqrt(m[4] * m[4] + m[5] * m[5] + m[6] * m[6]);
  1945. var sz = zs * Math.sqrt(m[8] * m[8] + m[9] * m[9] + m[10] * m[10]);
  1946. Matrix.FromValuesToRef(
  1947. m[0] / sx, m[1] / sx, m[2] / sx, 0,
  1948. m[4] / sy, m[5] / sy, m[6] / sy, 0,
  1949. m[8] / sz, m[9] / sz, m[10] / sz, 0,
  1950. 0, 0, 0, 1, result);
  1951. }
  1952. // Statics
  1953. public static FromArray(array: number[], offset?: number): Matrix {
  1954. var result = new Matrix();
  1955. if (!offset) {
  1956. offset = 0;
  1957. }
  1958. Matrix.FromArrayToRef(array, offset, result);
  1959. return result;
  1960. }
  1961. public static FromArrayToRef(array: number[], offset: number, result: Matrix) {
  1962. for (var index = 0; index < 16; index++) {
  1963. result.m[index] = array[index + offset];
  1964. }
  1965. }
  1966. public static FromFloat32ArrayToRefScaled(array: Float32Array, offset: number, scale: number, result: Matrix) {
  1967. for (var index = 0; index < 16; index++) {
  1968. result.m[index] = array[index + offset] * scale;
  1969. }
  1970. }
  1971. public static FromValuesToRef(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  1972. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  1973. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  1974. initialM41: number, initialM42: number, initialM43: number, initialM44: number, result: Matrix): void {
  1975. result.m[0] = initialM11;
  1976. result.m[1] = initialM12;
  1977. result.m[2] = initialM13;
  1978. result.m[3] = initialM14;
  1979. result.m[4] = initialM21;
  1980. result.m[5] = initialM22;
  1981. result.m[6] = initialM23;
  1982. result.m[7] = initialM24;
  1983. result.m[8] = initialM31;
  1984. result.m[9] = initialM32;
  1985. result.m[10] = initialM33;
  1986. result.m[11] = initialM34;
  1987. result.m[12] = initialM41;
  1988. result.m[13] = initialM42;
  1989. result.m[14] = initialM43;
  1990. result.m[15] = initialM44;
  1991. }
  1992. public getRow(index: number): Vector4 {
  1993. if (index < 0 || index > 3) {
  1994. return null;
  1995. }
  1996. var i = index * 4;
  1997. return new Vector4(this.m[i + 0], this.m[i + 1], this.m[i + 2], this.m[i + 3]);
  1998. }
  1999. public setRow(index: number, row: Vector4): Matrix {
  2000. if (index < 0 || index > 3) {
  2001. return this;
  2002. }
  2003. var i = index * 4;
  2004. this.m[i + 0] = row.x;
  2005. this.m[i + 1] = row.y;
  2006. this.m[i + 2] = row.z;
  2007. this.m[i + 3] = row.w;
  2008. return this;
  2009. }
  2010. public static FromValues(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  2011. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  2012. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  2013. initialM41: number, initialM42: number, initialM43: number, initialM44: number): Matrix {
  2014. var result = new Matrix();
  2015. result.m[0] = initialM11;
  2016. result.m[1] = initialM12;
  2017. result.m[2] = initialM13;
  2018. result.m[3] = initialM14;
  2019. result.m[4] = initialM21;
  2020. result.m[5] = initialM22;
  2021. result.m[6] = initialM23;
  2022. result.m[7] = initialM24;
  2023. result.m[8] = initialM31;
  2024. result.m[9] = initialM32;
  2025. result.m[10] = initialM33;
  2026. result.m[11] = initialM34;
  2027. result.m[12] = initialM41;
  2028. result.m[13] = initialM42;
  2029. result.m[14] = initialM43;
  2030. result.m[15] = initialM44;
  2031. return result;
  2032. }
  2033. public static Compose(scale: Vector3, rotation: Quaternion, translation: Vector3): Matrix {
  2034. var result = Matrix.FromValues(scale.x, 0, 0, 0,
  2035. 0, scale.y, 0, 0,
  2036. 0, 0, scale.z, 0,
  2037. 0, 0, 0, 1);
  2038. var rotationMatrix = Matrix.Identity();
  2039. rotation.toRotationMatrix(rotationMatrix);
  2040. result = result.multiply(rotationMatrix);
  2041. result.setTranslation(translation);
  2042. return result;
  2043. }
  2044. public static Identity(): Matrix {
  2045. return Matrix.FromValues(1.0, 0, 0, 0,
  2046. 0, 1.0, 0, 0,
  2047. 0, 0, 1.0, 0,
  2048. 0, 0, 0, 1.0);
  2049. }
  2050. public static IdentityToRef(result: Matrix): void {
  2051. Matrix.FromValuesToRef(1.0, 0, 0, 0,
  2052. 0, 1.0, 0, 0,
  2053. 0, 0, 1.0, 0,
  2054. 0, 0, 0, 1.0, result);
  2055. }
  2056. public static Zero(): Matrix {
  2057. return Matrix.FromValues(0, 0, 0, 0,
  2058. 0, 0, 0, 0,
  2059. 0, 0, 0, 0,
  2060. 0, 0, 0, 0);
  2061. }
  2062. public static RotationX(angle: number): Matrix {
  2063. var result = new Matrix();
  2064. Matrix.RotationXToRef(angle, result);
  2065. return result;
  2066. }
  2067. public static Invert(source: Matrix): Matrix {
  2068. var result = new Matrix();
  2069. source.invertToRef(result);
  2070. return result;
  2071. }
  2072. public static RotationXToRef(angle: number, result: Matrix): void {
  2073. var s = Math.sin(angle);
  2074. var c = Math.cos(angle);
  2075. result.m[0] = 1.0;
  2076. result.m[15] = 1.0;
  2077. result.m[5] = c;
  2078. result.m[10] = c;
  2079. result.m[9] = -s;
  2080. result.m[6] = s;
  2081. result.m[1] = 0;
  2082. result.m[2] = 0;
  2083. result.m[3] = 0;
  2084. result.m[4] = 0;
  2085. result.m[7] = 0;
  2086. result.m[8] = 0;
  2087. result.m[11] = 0;
  2088. result.m[12] = 0;
  2089. result.m[13] = 0;
  2090. result.m[14] = 0;
  2091. }
  2092. public static RotationY(angle: number): Matrix {
  2093. var result = new Matrix();
  2094. Matrix.RotationYToRef(angle, result);
  2095. return result;
  2096. }
  2097. public static RotationYToRef(angle: number, result: Matrix): void {
  2098. var s = Math.sin(angle);
  2099. var c = Math.cos(angle);
  2100. result.m[5] = 1.0;
  2101. result.m[15] = 1.0;
  2102. result.m[0] = c;
  2103. result.m[2] = -s;
  2104. result.m[8] = s;
  2105. result.m[10] = c;
  2106. result.m[1] = 0;
  2107. result.m[3] = 0;
  2108. result.m[4] = 0;
  2109. result.m[6] = 0;
  2110. result.m[7] = 0;
  2111. result.m[9] = 0;
  2112. result.m[11] = 0;
  2113. result.m[12] = 0;
  2114. result.m[13] = 0;
  2115. result.m[14] = 0;
  2116. }
  2117. public static RotationZ(angle: number): Matrix {
  2118. var result = new Matrix();
  2119. Matrix.RotationZToRef(angle, result);
  2120. return result;
  2121. }
  2122. public static RotationZToRef(angle: number, result: Matrix): void {
  2123. var s = Math.sin(angle);
  2124. var c = Math.cos(angle);
  2125. result.m[10] = 1.0;
  2126. result.m[15] = 1.0;
  2127. result.m[0] = c;
  2128. result.m[1] = s;
  2129. result.m[4] = -s;
  2130. result.m[5] = c;
  2131. result.m[2] = 0;
  2132. result.m[3] = 0;
  2133. result.m[6] = 0;
  2134. result.m[7] = 0;
  2135. result.m[8] = 0;
  2136. result.m[9] = 0;
  2137. result.m[11] = 0;
  2138. result.m[12] = 0;
  2139. result.m[13] = 0;
  2140. result.m[14] = 0;
  2141. }
  2142. public static RotationAxis(axis: Vector3, angle: number): Matrix {
  2143. var result = Matrix.Zero();
  2144. Matrix.RotationAxisToRef(axis, angle, result);
  2145. return result;
  2146. }
  2147. public static RotationAxisToRef(axis: Vector3, angle: number, result: Matrix): void {
  2148. var s = Math.sin(-angle);
  2149. var c = Math.cos(-angle);
  2150. var c1 = 1 - c;
  2151. axis.normalize();
  2152. result.m[0] = (axis.x * axis.x) * c1 + c;
  2153. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  2154. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  2155. result.m[3] = 0.0;
  2156. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  2157. result.m[5] = (axis.y * axis.y) * c1 + c;
  2158. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  2159. result.m[7] = 0.0;
  2160. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  2161. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  2162. result.m[10] = (axis.z * axis.z) * c1 + c;
  2163. result.m[11] = 0.0;
  2164. result.m[15] = 1.0;
  2165. }
  2166. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Matrix {
  2167. var result = new Matrix();
  2168. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  2169. return result;
  2170. }
  2171. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Matrix): void {
  2172. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  2173. this._tempQuaternion.toRotationMatrix(result);
  2174. }
  2175. public static Scaling(x: number, y: number, z: number): Matrix {
  2176. var result = Matrix.Zero();
  2177. Matrix.ScalingToRef(x, y, z, result);
  2178. return result;
  2179. }
  2180. public static ScalingToRef(x: number, y: number, z: number, result: Matrix): void {
  2181. result.m[0] = x;
  2182. result.m[1] = 0;
  2183. result.m[2] = 0;
  2184. result.m[3] = 0;
  2185. result.m[4] = 0;
  2186. result.m[5] = y;
  2187. result.m[6] = 0;
  2188. result.m[7] = 0;
  2189. result.m[8] = 0;
  2190. result.m[9] = 0;
  2191. result.m[10] = z;
  2192. result.m[11] = 0;
  2193. result.m[12] = 0;
  2194. result.m[13] = 0;
  2195. result.m[14] = 0;
  2196. result.m[15] = 1.0;
  2197. }
  2198. public static Translation(x: number, y: number, z: number): Matrix {
  2199. var result = Matrix.Identity();
  2200. Matrix.TranslationToRef(x, y, z, result);
  2201. return result;
  2202. }
  2203. public static TranslationToRef(x: number, y: number, z: number, result: Matrix): void {
  2204. Matrix.FromValuesToRef(1.0, 0, 0, 0,
  2205. 0, 1.0, 0, 0,
  2206. 0, 0, 1.0, 0,
  2207. x, y, z, 1.0, result);
  2208. }
  2209. public static Lerp(startValue: Matrix, endValue: Matrix, gradient: number): Matrix {
  2210. var result = Matrix.Zero();
  2211. for (var index = 0; index < 16; index++) {
  2212. result.m[index] = startValue.m[index] * (1.0 - gradient) + endValue.m[index] * gradient;
  2213. }
  2214. return result;
  2215. }
  2216. public static DecomposeLerp(startValue: Matrix, endValue: Matrix, gradient: number): Matrix {
  2217. var startScale = new Vector3(0, 0, 0);
  2218. var startRotation = new Quaternion();
  2219. var startTranslation = new Vector3(0, 0, 0);
  2220. startValue.decompose(startScale, startRotation, startTranslation);
  2221. var endScale = new Vector3(0, 0, 0);
  2222. var endRotation = new Quaternion();
  2223. var endTranslation = new Vector3(0, 0, 0);
  2224. endValue.decompose(endScale, endRotation, endTranslation);
  2225. var resultScale = Vector3.Lerp(startScale, endScale, gradient);
  2226. var resultRotation = Quaternion.Slerp(startRotation, endRotation, gradient);
  2227. var resultTranslation = Vector3.Lerp(startTranslation, endTranslation, gradient);
  2228. return Matrix.Compose(resultScale, resultRotation, resultTranslation);
  2229. }
  2230. public static LookAtLH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
  2231. var result = Matrix.Zero();
  2232. Matrix.LookAtLHToRef(eye, target, up, result);
  2233. return result;
  2234. }
  2235. public static LookAtLHToRef(eye: Vector3, target: Vector3, up: Vector3, result: Matrix): void {
  2236. // Z axis
  2237. target.subtractToRef(eye, this._zAxis);
  2238. this._zAxis.normalize();
  2239. // X axis
  2240. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  2241. if (this._xAxis.lengthSquared() === 0) {
  2242. this._xAxis.x = 1.0;
  2243. } else {
  2244. this._xAxis.normalize();
  2245. }
  2246. // Y axis
  2247. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  2248. this._yAxis.normalize();
  2249. // Eye angles
  2250. var ex = -Vector3.Dot(this._xAxis, eye);
  2251. var ey = -Vector3.Dot(this._yAxis, eye);
  2252. var ez = -Vector3.Dot(this._zAxis, eye);
  2253. return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0,
  2254. this._xAxis.y, this._yAxis.y, this._zAxis.y, 0,
  2255. this._xAxis.z, this._yAxis.z, this._zAxis.z, 0,
  2256. ex, ey, ez, 1, result);
  2257. }
  2258. public static LookAtRH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
  2259. var result = Matrix.Zero();
  2260. Matrix.LookAtRHToRef(eye, target, up, result);
  2261. return result;
  2262. }
  2263. public static LookAtRHToRef(eye: Vector3, target: Vector3, up: Vector3, result: Matrix): void {
  2264. // Z axis
  2265. eye.subtractToRef(target, this._zAxis);
  2266. this._zAxis.normalize();
  2267. // X axis
  2268. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  2269. if (this._xAxis.lengthSquared() === 0) {
  2270. this._xAxis.x = 1.0;
  2271. } else {
  2272. this._xAxis.normalize();
  2273. }
  2274. // Y axis
  2275. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  2276. this._yAxis.normalize();
  2277. // Eye angles
  2278. var ex = -Vector3.Dot(this._xAxis, eye);
  2279. var ey = -Vector3.Dot(this._yAxis, eye);
  2280. var ez = -Vector3.Dot(this._zAxis, eye);
  2281. return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0,
  2282. this._xAxis.y, this._yAxis.y, this._zAxis.y, 0,
  2283. this._xAxis.z, this._yAxis.z, this._zAxis.z, 0,
  2284. ex, ey, ez, 1, result);
  2285. }
  2286. public static OrthoLH(width: number, height: number, znear: number, zfar: number): Matrix {
  2287. var matrix = Matrix.Zero();
  2288. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  2289. return matrix;
  2290. }
  2291. public static OrthoLHToRef(width: number, height: number, znear: number, zfar: number, result: Matrix): void {
  2292. let n = znear;
  2293. let f = zfar;
  2294. let a = 2.0 / width;
  2295. let b = 2.0 / height;
  2296. let c = 2.0 / (f - n);
  2297. let d = -(f + n)/(f - n);
  2298. BABYLON.Matrix.FromValuesToRef(
  2299. a, 0, 0, 0,
  2300. 0, b, 0, 0,
  2301. 0, 0, c, 0,
  2302. 0, 0, d, 1,
  2303. result
  2304. );
  2305. }
  2306. public static OrthoOffCenterLH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  2307. var matrix = Matrix.Zero();
  2308. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  2309. return matrix;
  2310. }
  2311. public static OrthoOffCenterLHToRef(left: number, right, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  2312. let n = znear;
  2313. let f = zfar;
  2314. let a = 2.0 / (right - left);
  2315. let b = 2.0 / (top - bottom);
  2316. let c = 2.0 / (f - n);
  2317. let d = -(f + n)/(f - n);
  2318. let i0 = (left + right) / (left - right);
  2319. let i1 = (top + bottom) / (bottom - top);
  2320. BABYLON.Matrix.FromValuesToRef(
  2321. a, 0, 0, 0,
  2322. 0, b, 0, 0,
  2323. 0, 0, c, 0,
  2324. i0, i1, d, 1,
  2325. result
  2326. );
  2327. }
  2328. public static OrthoOffCenterRH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  2329. var matrix = Matrix.Zero();
  2330. Matrix.OrthoOffCenterRHToRef(left, right, bottom, top, znear, zfar, matrix);
  2331. return matrix;
  2332. }
  2333. public static OrthoOffCenterRHToRef(left: number, right, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  2334. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, result);
  2335. result.m[10] *= -1.0;
  2336. }
  2337. public static PerspectiveLH(width: number, height: number, znear: number, zfar: number): Matrix {
  2338. var matrix = Matrix.Zero();
  2339. let n = znear;
  2340. let f = zfar;
  2341. let a = 2.0 * n / width;
  2342. let b = 2.0 * n / height;
  2343. let c = (f + n)/(f - n);
  2344. let d = -2.0 * f * n/(f - n);
  2345. BABYLON.Matrix.FromValuesToRef(
  2346. a, 0, 0, 0,
  2347. 0, b, 0, 0,
  2348. 0, 0, c, 1,
  2349. 0, 0, d, 0,
  2350. matrix
  2351. );
  2352. return matrix;
  2353. }
  2354. public static PerspectiveFovLH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  2355. var matrix = Matrix.Zero();
  2356. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  2357. return matrix;
  2358. }
  2359. public static PerspectiveFovLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  2360. let n = znear;
  2361. let f = zfar;
  2362. let t = 1.0 / (Math.tan(fov * 0.5));
  2363. let a = isVerticalFovFixed ? (t / aspect) : t;
  2364. let b = isVerticalFovFixed ? t : (t * aspect);
  2365. let c = (f + n)/(f - n);
  2366. let d = -2.0 * f * n/(f - n);
  2367. BABYLON.Matrix.FromValuesToRef(
  2368. a, 0, 0, 0,
  2369. 0, b, 0, 0,
  2370. 0, 0, c, 1,
  2371. 0, 0, d, 0,
  2372. result
  2373. );
  2374. }
  2375. public static PerspectiveFovRH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  2376. var matrix = Matrix.Zero();
  2377. Matrix.PerspectiveFovRHToRef(fov, aspect, znear, zfar, matrix);
  2378. return matrix;
  2379. }
  2380. public static PerspectiveFovRHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  2381. //alternatively this could be expressed as:
  2382. // m = PerspectiveFovLHToRef
  2383. // m[10] *= -1.0;
  2384. // m[11] *= -1.0;
  2385. let n = znear;
  2386. let f = zfar;
  2387. let t = 1.0 / (Math.tan(fov * 0.5));
  2388. let a = isVerticalFovFixed ? (t / aspect) : t;
  2389. let b = isVerticalFovFixed ? t : (t * aspect);
  2390. let c = -(f + n)/(f - n);
  2391. let d = -2*f*n/(f - n);
  2392. BABYLON.Matrix.FromValuesToRef(
  2393. a, 0, 0, 0,
  2394. 0, b, 0, 0,
  2395. 0, 0, c,-1,
  2396. 0, 0, d, 0,
  2397. result
  2398. );
  2399. }
  2400. public static PerspectiveFovWebVRToRef(fov, znear: number, zfar: number, result: Matrix, isVerticalFovFixed = true): void {
  2401. //left handed
  2402. var upTan = Math.tan(fov.upDegrees * Math.PI / 180.0);
  2403. var downTan = Math.tan(fov.downDegrees * Math.PI / 180.0);
  2404. var leftTan = Math.tan(fov.leftDegrees * Math.PI / 180.0);
  2405. var rightTan = Math.tan(fov.rightDegrees * Math.PI / 180.0);
  2406. var xScale = 2.0 / (leftTan + rightTan);
  2407. var yScale = 2.0 / (upTan + downTan);
  2408. result.m[0] = xScale;
  2409. result.m[1] = result.m[2] = result.m[3] = result.m[4] = 0.0;
  2410. result.m[5] = yScale;
  2411. result.m[6] = result.m[7] = 0.0;
  2412. result.m[8] = ((leftTan - rightTan) * xScale * 0.5);
  2413. result.m[9] = -((upTan - downTan) * yScale * 0.5);
  2414. result.m[10] = -(znear + zfar) / (zfar - znear);
  2415. // result.m[10] = -zfar / (znear - zfar);
  2416. result.m[11] = 1.0;
  2417. result.m[12] = result.m[13] = result.m[15] = 0.0;
  2418. result.m[14] = -(2.0 * zfar * znear) / (zfar - znear);
  2419. // result.m[14] = (znear * zfar) / (znear - zfar);
  2420. }
  2421. public static GetFinalMatrix(viewport: Viewport, world: Matrix, view: Matrix, projection: Matrix, zmin: number, zmax: number): Matrix {
  2422. var cw = viewport.width;
  2423. var ch = viewport.height;
  2424. var cx = viewport.x;
  2425. var cy = viewport.y;
  2426. var viewportMatrix = Matrix.FromValues(cw / 2.0, 0, 0, 0,
  2427. 0, -ch / 2.0, 0, 0,
  2428. 0, 0, zmax - zmin, 0,
  2429. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  2430. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  2431. }
  2432. public static GetAsMatrix2x2(matrix: Matrix): Float32Array {
  2433. return new Float32Array([
  2434. matrix.m[0], matrix.m[1],
  2435. matrix.m[4], matrix.m[5]
  2436. ]);
  2437. }
  2438. public static GetAsMatrix3x3(matrix: Matrix): Float32Array {
  2439. return new Float32Array([
  2440. matrix.m[0], matrix.m[1], matrix.m[2],
  2441. matrix.m[4], matrix.m[5], matrix.m[6],
  2442. matrix.m[8], matrix.m[9], matrix.m[10]
  2443. ]);
  2444. }
  2445. public static Transpose(matrix: Matrix): Matrix {
  2446. var result = new Matrix();
  2447. result.m[0] = matrix.m[0];
  2448. result.m[1] = matrix.m[4];
  2449. result.m[2] = matrix.m[8];
  2450. result.m[3] = matrix.m[12];
  2451. result.m[4] = matrix.m[1];
  2452. result.m[5] = matrix.m[5];
  2453. result.m[6] = matrix.m[9];
  2454. result.m[7] = matrix.m[13];
  2455. result.m[8] = matrix.m[2];
  2456. result.m[9] = matrix.m[6];
  2457. result.m[10] = matrix.m[10];
  2458. result.m[11] = matrix.m[14];
  2459. result.m[12] = matrix.m[3];
  2460. result.m[13] = matrix.m[7];
  2461. result.m[14] = matrix.m[11];
  2462. result.m[15] = matrix.m[15];
  2463. return result;
  2464. }
  2465. public static Reflection(plane: Plane): Matrix {
  2466. var matrix = new Matrix();
  2467. Matrix.ReflectionToRef(plane, matrix);
  2468. return matrix;
  2469. }
  2470. public static ReflectionToRef(plane: Plane, result: Matrix): void {
  2471. plane.normalize();
  2472. var x = plane.normal.x;
  2473. var y = plane.normal.y;
  2474. var z = plane.normal.z;
  2475. var temp = -2 * x;
  2476. var temp2 = -2 * y;
  2477. var temp3 = -2 * z;
  2478. result.m[0] = (temp * x) + 1;
  2479. result.m[1] = temp2 * x;
  2480. result.m[2] = temp3 * x;
  2481. result.m[3] = 0.0;
  2482. result.m[4] = temp * y;
  2483. result.m[5] = (temp2 * y) + 1;
  2484. result.m[6] = temp3 * y;
  2485. result.m[7] = 0.0;
  2486. result.m[8] = temp * z;
  2487. result.m[9] = temp2 * z;
  2488. result.m[10] = (temp3 * z) + 1;
  2489. result.m[11] = 0.0;
  2490. result.m[12] = temp * plane.d;
  2491. result.m[13] = temp2 * plane.d;
  2492. result.m[14] = temp3 * plane.d;
  2493. result.m[15] = 1.0;
  2494. }
  2495. public static FromXYZAxesToRef(xaxis: Vector3, yaxis: Vector3, zaxis: Vector3, mat: Matrix) {
  2496. mat.m[0] = xaxis.x;
  2497. mat.m[1] = xaxis.y;
  2498. mat.m[2] = xaxis.z;
  2499. mat.m[3] = 0;
  2500. mat.m[4] = yaxis.x;
  2501. mat.m[5] = yaxis.y;
  2502. mat.m[6] = yaxis.z;
  2503. mat.m[7] = 0;
  2504. mat.m[8] = zaxis.x;
  2505. mat.m[9] = zaxis.y;
  2506. mat.m[10] = zaxis.z;
  2507. mat.m[11] = 0;
  2508. mat.m[12] = 0;
  2509. mat.m[13] = 0;
  2510. mat.m[14] = 0;
  2511. mat.m[15] = 1;
  2512. }
  2513. public static FromQuaternionToRef(quat:Quaternion, result:Matrix){
  2514. var xx = quat.x * quat.x;
  2515. var yy = quat.y * quat.y;
  2516. var zz = quat.z * quat.z;
  2517. var xy = quat.x * quat.y;
  2518. var zw = quat.z * quat.w;
  2519. var zx = quat.z * quat.x;
  2520. var yw = quat.y * quat.w;
  2521. var yz = quat.y * quat.z;
  2522. var xw = quat.x * quat.w;
  2523. result.m[0] = 1.0 - (2.0 * (yy + zz));
  2524. result.m[1] = 2.0 * (xy + zw);
  2525. result.m[2] = 2.0 * (zx - yw);
  2526. result.m[3] = 0;
  2527. result.m[4] = 2.0 * (xy - zw);
  2528. result.m[5] = 1.0 - (2.0 * (zz + xx));
  2529. result.m[6] = 2.0 * (yz + xw);
  2530. result.m[7] = 0;
  2531. result.m[8] = 2.0 * (zx + yw);
  2532. result.m[9] = 2.0 * (yz - xw);
  2533. result.m[10] = 1.0 - (2.0 * (yy + xx));
  2534. result.m[11] = 0;
  2535. result.m[12] = 0;
  2536. result.m[13] = 0;
  2537. result.m[14] = 0;
  2538. result.m[15] = 1.0;
  2539. }
  2540. }
  2541. export class Plane {
  2542. public normal: Vector3;
  2543. public d: number;
  2544. constructor(a: number, b: number, c: number, d: number) {
  2545. this.normal = new Vector3(a, b, c);
  2546. this.d = d;
  2547. }
  2548. public asArray(): number[] {
  2549. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  2550. }
  2551. // Methods
  2552. public clone(): Plane {
  2553. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  2554. }
  2555. public getClassName(): string {
  2556. return "Plane";
  2557. }
  2558. public getHashCode(): number {
  2559. let hash = this.normal.getHashCode();
  2560. hash = (hash * 397) ^ (this.d || 0);
  2561. return hash;
  2562. }
  2563. public normalize(): Plane {
  2564. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  2565. var magnitude = 0;
  2566. if (norm !== 0) {
  2567. magnitude = 1.0 / norm;
  2568. }
  2569. this.normal.x *= magnitude;
  2570. this.normal.y *= magnitude;
  2571. this.normal.z *= magnitude;
  2572. this.d *= magnitude;
  2573. return this;
  2574. }
  2575. public transform(transformation: Matrix): Plane {
  2576. var transposedMatrix = Matrix.Transpose(transformation);
  2577. var x = this.normal.x;
  2578. var y = this.normal.y;
  2579. var z = this.normal.z;
  2580. var d = this.d;
  2581. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  2582. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  2583. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  2584. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  2585. return new Plane(normalX, normalY, normalZ, finalD);
  2586. }
  2587. public dotCoordinate(point): number {
  2588. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  2589. }
  2590. public copyFromPoints(point1: Vector3, point2: Vector3, point3: Vector3): Plane {
  2591. var x1 = point2.x - point1.x;
  2592. var y1 = point2.y - point1.y;
  2593. var z1 = point2.z - point1.z;
  2594. var x2 = point3.x - point1.x;
  2595. var y2 = point3.y - point1.y;
  2596. var z2 = point3.z - point1.z;
  2597. var yz = (y1 * z2) - (z1 * y2);
  2598. var xz = (z1 * x2) - (x1 * z2);
  2599. var xy = (x1 * y2) - (y1 * x2);
  2600. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  2601. var invPyth;
  2602. if (pyth !== 0) {
  2603. invPyth = 1.0 / pyth;
  2604. }
  2605. else {
  2606. invPyth = 0;
  2607. }
  2608. this.normal.x = yz * invPyth;
  2609. this.normal.y = xz * invPyth;
  2610. this.normal.z = xy * invPyth;
  2611. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  2612. return this;
  2613. }
  2614. public isFrontFacingTo(direction: Vector3, epsilon: number): boolean {
  2615. var dot = Vector3.Dot(this.normal, direction);
  2616. return (dot <= epsilon);
  2617. }
  2618. public signedDistanceTo(point: Vector3): number {
  2619. return Vector3.Dot(point, this.normal) + this.d;
  2620. }
  2621. // Statics
  2622. static FromArray(array: number[]): Plane {
  2623. return new Plane(array[0], array[1], array[2], array[3]);
  2624. }
  2625. static FromPoints(point1, point2, point3): Plane {
  2626. var result = new Plane(0, 0, 0, 0);
  2627. result.copyFromPoints(point1, point2, point3);
  2628. return result;
  2629. }
  2630. static FromPositionAndNormal(origin: Vector3, normal: Vector3): Plane {
  2631. var result = new Plane(0, 0, 0, 0);
  2632. normal.normalize();
  2633. result.normal = normal;
  2634. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2635. return result;
  2636. }
  2637. static SignedDistanceToPlaneFromPositionAndNormal(origin: Vector3, normal: Vector3, point: Vector3): number {
  2638. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2639. return Vector3.Dot(point, normal) + d;
  2640. }
  2641. }
  2642. export class Viewport {
  2643. constructor(public x: number, public y: number, public width: number, public height: number) {
  2644. }
  2645. public toGlobal(renderWidth: number, renderHeight: number): Viewport {
  2646. return new Viewport(this.x * renderWidth, this.y * renderHeight, this.width * renderWidth, this.height * renderHeight);
  2647. }
  2648. }
  2649. export class Frustum {
  2650. public static GetPlanes(transform: Matrix): Plane[] {
  2651. var frustumPlanes = [];
  2652. for (var index = 0; index < 6; index++) {
  2653. frustumPlanes.push(new Plane(0, 0, 0, 0));
  2654. }
  2655. Frustum.GetPlanesToRef(transform, frustumPlanes);
  2656. return frustumPlanes;
  2657. }
  2658. public static GetPlanesToRef(transform: Matrix, frustumPlanes: Plane[]): void {
  2659. // Near
  2660. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  2661. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  2662. frustumPlanes[0].normal.z = transform.m[11] + transform.m[10];
  2663. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  2664. frustumPlanes[0].normalize();
  2665. // Far
  2666. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  2667. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  2668. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  2669. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  2670. frustumPlanes[1].normalize();
  2671. // Left
  2672. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  2673. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  2674. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  2675. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  2676. frustumPlanes[2].normalize();
  2677. // Right
  2678. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  2679. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  2680. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  2681. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  2682. frustumPlanes[3].normalize();
  2683. // Top
  2684. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  2685. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  2686. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  2687. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  2688. frustumPlanes[4].normalize();
  2689. // Bottom
  2690. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  2691. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  2692. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  2693. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  2694. frustumPlanes[5].normalize();
  2695. }
  2696. }
  2697. export enum Space {
  2698. LOCAL = 0,
  2699. WORLD = 1
  2700. }
  2701. export class Axis {
  2702. public static X: Vector3 = new Vector3(1, 0, 0);
  2703. public static Y: Vector3 = new Vector3(0, 1, 0);
  2704. public static Z: Vector3 = new Vector3(0, 0, 1);
  2705. };
  2706. export class BezierCurve {
  2707. public static interpolate(t: number, x1: number, y1: number, x2: number, y2: number): number {
  2708. // Extract X (which is equal to time here)
  2709. var f0 = 1 - 3 * x2 + 3 * x1;
  2710. var f1 = 3 * x2 - 6 * x1;
  2711. var f2 = 3 * x1;
  2712. var refinedT = t;
  2713. for (var i = 0; i < 5; i++) {
  2714. var refinedT2 = refinedT * refinedT;
  2715. var refinedT3 = refinedT2 * refinedT;
  2716. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  2717. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  2718. refinedT -= (x - t) * slope;
  2719. refinedT = Math.min(1, Math.max(0, refinedT));
  2720. }
  2721. // Resolve cubic bezier for the given x
  2722. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 +
  2723. 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 +
  2724. Math.pow(refinedT, 3);
  2725. }
  2726. }
  2727. export enum Orientation {
  2728. CW = 0,
  2729. CCW = 1
  2730. }
  2731. export class Angle {
  2732. private _radians: number;
  2733. constructor(radians: number) {
  2734. this._radians = radians;
  2735. if (this._radians < 0) this._radians += (2 * Math.PI);
  2736. }
  2737. public degrees = () => this._radians * 180 / Math.PI;
  2738. public radians = () => this._radians;
  2739. public static BetweenTwoPoints(a: Vector2, b: Vector2): Angle {
  2740. var delta = b.subtract(a);
  2741. var theta = Math.atan2(delta.y, delta.x);
  2742. return new Angle(theta);
  2743. }
  2744. public static FromRadians(radians: number): Angle {
  2745. return new Angle(radians);
  2746. }
  2747. public static FromDegrees(degrees: number): Angle {
  2748. return new Angle(degrees * Math.PI / 180);
  2749. }
  2750. }
  2751. export class Arc2 {
  2752. centerPoint: Vector2;
  2753. radius: number;
  2754. angle: Angle;
  2755. startAngle: Angle;
  2756. orientation: Orientation;
  2757. constructor(public startPoint: Vector2, public midPoint: Vector2, public endPoint: Vector2) {
  2758. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  2759. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  2760. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  2761. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  2762. this.centerPoint = new Vector2(
  2763. (startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det,
  2764. ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det
  2765. );
  2766. this.radius = this.centerPoint.subtract(this.startPoint).length();
  2767. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  2768. var a1 = this.startAngle.degrees();
  2769. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  2770. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  2771. // angles correction
  2772. if (a2 - a1 > +180.0) a2 -= 360.0;
  2773. if (a2 - a1 < -180.0) a2 += 360.0;
  2774. if (a3 - a2 > +180.0) a3 -= 360.0;
  2775. if (a3 - a2 < -180.0) a3 += 360.0;
  2776. this.orientation = (a2 - a1) < 0 ? Orientation.CW : Orientation.CCW;
  2777. this.angle = Angle.FromDegrees(this.orientation === Orientation.CW ? a1 - a3 : a3 - a1);
  2778. }
  2779. }
  2780. export class Path2 {
  2781. private _points = new Array<Vector2>();
  2782. private _length = 0;
  2783. public closed = false;
  2784. constructor(x: number, y: number) {
  2785. this._points.push(new Vector2(x, y));
  2786. }
  2787. public addLineTo(x: number, y: number): Path2 {
  2788. if (closed) {
  2789. //Tools.Error("cannot add lines to closed paths");
  2790. return this;
  2791. }
  2792. var newPoint = new Vector2(x, y);
  2793. var previousPoint = this._points[this._points.length - 1];
  2794. this._points.push(newPoint);
  2795. this._length += newPoint.subtract(previousPoint).length();
  2796. return this;
  2797. }
  2798. public addArcTo(midX: number, midY: number, endX: number, endY: number, numberOfSegments = 36): Path2 {
  2799. if (closed) {
  2800. //Tools.Error("cannot add arcs to closed paths");
  2801. return this;
  2802. }
  2803. var startPoint = this._points[this._points.length - 1];
  2804. var midPoint = new Vector2(midX, midY);
  2805. var endPoint = new Vector2(endX, endY);
  2806. var arc = new Arc2(startPoint, midPoint, endPoint);
  2807. var increment = arc.angle.radians() / numberOfSegments;
  2808. if (arc.orientation === Orientation.CW) increment *= -1;
  2809. var currentAngle = arc.startAngle.radians() + increment;
  2810. for (var i = 0; i < numberOfSegments; i++) {
  2811. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  2812. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  2813. this.addLineTo(x, y);
  2814. currentAngle += increment;
  2815. }
  2816. return this;
  2817. }
  2818. public close(): Path2 {
  2819. this.closed = true;
  2820. return this;
  2821. }
  2822. public length(): number {
  2823. var result = this._length;
  2824. if (!this.closed) {
  2825. var lastPoint = this._points[this._points.length - 1];
  2826. var firstPoint = this._points[0];
  2827. result += (firstPoint.subtract(lastPoint).length());
  2828. }
  2829. return result;
  2830. }
  2831. public getPoints(): Vector2[] {
  2832. return this._points;
  2833. }
  2834. public getPointAtLengthPosition(normalizedLengthPosition: number): Vector2 {
  2835. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  2836. //Tools.Error("normalized length position should be between 0 and 1.");
  2837. return Vector2.Zero();
  2838. }
  2839. var lengthPosition = normalizedLengthPosition * this.length();
  2840. var previousOffset = 0;
  2841. for (var i = 0; i < this._points.length; i++) {
  2842. var j = (i + 1) % this._points.length;
  2843. var a = this._points[i];
  2844. var b = this._points[j];
  2845. var bToA = b.subtract(a);
  2846. var nextOffset = (bToA.length() + previousOffset);
  2847. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  2848. var dir = bToA.normalize();
  2849. var localOffset = lengthPosition - previousOffset;
  2850. return new Vector2(
  2851. a.x + (dir.x * localOffset),
  2852. a.y + (dir.y * localOffset)
  2853. );
  2854. }
  2855. previousOffset = nextOffset;
  2856. }
  2857. //Tools.Error("internal error");
  2858. return Vector2.Zero();
  2859. }
  2860. public static StartingAt(x: number, y: number): Path2 {
  2861. return new Path2(x, y);
  2862. }
  2863. }
  2864. export class Path3D {
  2865. private _curve = new Array<Vector3>();
  2866. private _distances = new Array<number>();
  2867. private _tangents = new Array<Vector3>();
  2868. private _normals = new Array<Vector3>();
  2869. private _binormals = new Array<Vector3>();
  2870. private _raw: boolean;
  2871. /**
  2872. * new Path3D(path, normal, raw)
  2873. * Creates a Path3D. A Path3D is a logical math object, so not a mesh.
  2874. * please read the description in the tutorial : http://doc.babylonjs.com/tutorials/How_to_use_Path3D
  2875. * path : an array of Vector3, the curve axis of the Path3D
  2876. * normal (optional) : Vector3, the first wanted normal to the curve. Ex (0, 1, 0) for a vertical normal.
  2877. * raw (optional, default false) : boolean, if true the returned Path3D isn't normalized. Useful to depict path acceleration or speed.
  2878. */
  2879. constructor(public path: Vector3[], firstNormal?: Vector3, raw?: boolean) {
  2880. for (var p = 0; p < path.length; p++) {
  2881. this._curve[p] = path[p].clone(); // hard copy
  2882. }
  2883. this._raw = raw || false;
  2884. this._compute(firstNormal);
  2885. }
  2886. /**
  2887. * Returns the Path3D array of successive Vector3 designing its curve.
  2888. */
  2889. public getCurve(): Vector3[] {
  2890. return this._curve;
  2891. }
  2892. /**
  2893. * Returns an array populated with tangent vectors on each Path3D curve point.
  2894. */
  2895. public getTangents(): Vector3[] {
  2896. return this._tangents;
  2897. }
  2898. /**
  2899. * Returns an array populated with normal vectors on each Path3D curve point.
  2900. */
  2901. public getNormals(): Vector3[] {
  2902. return this._normals;
  2903. }
  2904. /**
  2905. * Returns an array populated with binormal vectors on each Path3D curve point.
  2906. */
  2907. public getBinormals(): Vector3[] {
  2908. return this._binormals;
  2909. }
  2910. /**
  2911. * Returns an array populated with distances (float) of the i-th point from the first curve point.
  2912. */
  2913. public getDistances(): number[] {
  2914. return this._distances;
  2915. }
  2916. /**
  2917. * Forces the Path3D tangent, normal, binormal and distance recomputation.
  2918. * Returns the same object updated.
  2919. */
  2920. public update(path: Vector3[], firstNormal?: Vector3): Path3D {
  2921. for (var p = 0; p < path.length; p++) {
  2922. this._curve[p].x = path[p].x;
  2923. this._curve[p].y = path[p].y;
  2924. this._curve[p].z = path[p].z;
  2925. }
  2926. this._compute(firstNormal);
  2927. return this;
  2928. }
  2929. // private function compute() : computes tangents, normals and binormals
  2930. private _compute(firstNormal) {
  2931. var l = this._curve.length;
  2932. // first and last tangents
  2933. this._tangents[0] = this._getFirstNonNullVector(0);
  2934. if (!this._raw) {
  2935. this._tangents[0].normalize();
  2936. }
  2937. this._tangents[l - 1] = this._curve[l - 1].subtract(this._curve[l - 2]);
  2938. if (!this._raw) {
  2939. this._tangents[l - 1].normalize();
  2940. }
  2941. // normals and binormals at first point : arbitrary vector with _normalVector()
  2942. var tg0 = this._tangents[0];
  2943. var pp0 = this._normalVector(this._curve[0], tg0, firstNormal);
  2944. this._normals[0] = pp0;
  2945. if (!this._raw) {
  2946. this._normals[0].normalize();
  2947. }
  2948. this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
  2949. if (!this._raw) {
  2950. this._binormals[0].normalize();
  2951. }
  2952. this._distances[0] = 0.0;
  2953. // normals and binormals : next points
  2954. var prev: Vector3; // previous vector (segment)
  2955. var cur: Vector3; // current vector (segment)
  2956. var curTang: Vector3; // current tangent
  2957. // previous normal
  2958. var prevBinor: Vector3; // previous binormal
  2959. for (var i = 1; i < l; i++) {
  2960. // tangents
  2961. prev = this._getLastNonNullVector(i);
  2962. if (i < l - 1) {
  2963. cur = this._getFirstNonNullVector(i);
  2964. this._tangents[i] = prev.add(cur);
  2965. this._tangents[i].normalize();
  2966. }
  2967. this._distances[i] = this._distances[i - 1] + prev.length();
  2968. // normals and binormals
  2969. // http://www.cs.cmu.edu/afs/andrew/scs/cs/15-462/web/old/asst2camera.html
  2970. curTang = this._tangents[i];
  2971. prevBinor = this._binormals[i - 1];
  2972. this._normals[i] = Vector3.Cross(prevBinor, curTang);
  2973. if (!this._raw) {
  2974. this._normals[i].normalize();
  2975. }
  2976. this._binormals[i] = Vector3.Cross(curTang, this._normals[i]);
  2977. if (!this._raw) {
  2978. this._binormals[i].normalize();
  2979. }
  2980. }
  2981. }
  2982. // private function getFirstNonNullVector(index)
  2983. // returns the first non null vector from index : curve[index + N].subtract(curve[index])
  2984. private _getFirstNonNullVector(index: number): Vector3 {
  2985. var i = 1;
  2986. var nNVector: Vector3 = this._curve[index + i].subtract(this._curve[index]);
  2987. while (nNVector.length() === 0 && index + i + 1 < this._curve.length) {
  2988. i++;
  2989. nNVector = this._curve[index + i].subtract(this._curve[index]);
  2990. }
  2991. return nNVector;
  2992. }
  2993. // private function getLastNonNullVector(index)
  2994. // returns the last non null vector from index : curve[index].subtract(curve[index - N])
  2995. private _getLastNonNullVector(index: number): Vector3 {
  2996. var i = 1;
  2997. var nLVector: Vector3 = this._curve[index].subtract(this._curve[index - i]);
  2998. while (nLVector.length() === 0 && index > i + 1) {
  2999. i++;
  3000. nLVector = this._curve[index].subtract(this._curve[index - i]);
  3001. }
  3002. return nLVector;
  3003. }
  3004. // private function normalVector(v0, vt, va) :
  3005. // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
  3006. // if va is passed, it returns the va projection on the plane orthogonal to vt at the point v0
  3007. private _normalVector(v0: Vector3, vt: Vector3, va: Vector3): Vector3 {
  3008. var normal0: Vector3;
  3009. var tgl = vt.length();
  3010. if (tgl === 0.0) {
  3011. tgl = 1.0;
  3012. }
  3013. if (va === undefined || va === null) {
  3014. var point: Vector3;
  3015. if (!MathTools.WithinEpsilon(Math.abs(vt.y) / tgl, 1.0, Epsilon)) { // search for a point in the plane
  3016. point = new Vector3(0.0, -1.0, 0.0);
  3017. }
  3018. else if (!MathTools.WithinEpsilon(Math.abs(vt.x) / tgl, 1.0, Epsilon)) {
  3019. point = new Vector3(1.0, 0.0, 0.0);
  3020. }
  3021. else if (!MathTools.WithinEpsilon(Math.abs(vt.z) / tgl, 1.0, Epsilon)) {
  3022. point = new Vector3(0.0, 0.0, 1.0);
  3023. }
  3024. normal0 = Vector3.Cross(vt, point);
  3025. }
  3026. else {
  3027. normal0 = Vector3.Cross(vt, va);
  3028. Vector3.CrossToRef(normal0, vt, normal0);
  3029. }
  3030. normal0.normalize();
  3031. return normal0;
  3032. }
  3033. }
  3034. export class Curve3 {
  3035. private _points: Vector3[];
  3036. private _length: number = 0.0;
  3037. /**
  3038. * Returns a Curve3 object along a Quadratic Bezier curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#quadratic-bezier-curve
  3039. * @param v0 (Vector3) the origin point of the Quadratic Bezier
  3040. * @param v1 (Vector3) the control point
  3041. * @param v2 (Vector3) the end point of the Quadratic Bezier
  3042. * @param nbPoints (integer) the wanted number of points in the curve
  3043. */
  3044. public static CreateQuadraticBezier(v0: Vector3, v1: Vector3, v2: Vector3, nbPoints: number): Curve3 {
  3045. nbPoints = nbPoints > 2 ? nbPoints : 3;
  3046. var bez = new Array<Vector3>();
  3047. var equation = (t: number, val0: number, val1: number, val2: number) => {
  3048. var res = (1.0 - t) * (1.0 - t) * val0 + 2.0 * t * (1.0 - t) * val1 + t * t * val2;
  3049. return res;
  3050. }
  3051. for (var i = 0; i <= nbPoints; i++) {
  3052. 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)));
  3053. }
  3054. return new Curve3(bez);
  3055. }
  3056. /**
  3057. * Returns a Curve3 object along a Cubic Bezier curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#cubic-bezier-curve
  3058. * @param v0 (Vector3) the origin point of the Cubic Bezier
  3059. * @param v1 (Vector3) the first control point
  3060. * @param v2 (Vector3) the second control point
  3061. * @param v3 (Vector3) the end point of the Cubic Bezier
  3062. * @param nbPoints (integer) the wanted number of points in the curve
  3063. */
  3064. public static CreateCubicBezier(v0: Vector3, v1: Vector3, v2: Vector3, v3: Vector3, nbPoints: number): Curve3 {
  3065. nbPoints = nbPoints > 3 ? nbPoints : 4;
  3066. var bez = new Array<Vector3>();
  3067. var equation = (t: number, val0: number, val1: number, val2: number, val3: number) => {
  3068. 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;
  3069. return res;
  3070. }
  3071. for (var i = 0; i <= nbPoints; i++) {
  3072. 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)));
  3073. }
  3074. return new Curve3(bez);
  3075. }
  3076. /**
  3077. * Returns a Curve3 object along a Hermite Spline curve : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#hermite-spline
  3078. * @param p1 (Vector3) the origin point of the Hermite Spline
  3079. * @param t1 (Vector3) the tangent vector at the origin point
  3080. * @param p2 (Vector3) the end point of the Hermite Spline
  3081. * @param t2 (Vector3) the tangent vector at the end point
  3082. * @param nbPoints (integer) the wanted number of points in the curve
  3083. */
  3084. public static CreateHermiteSpline(p1: Vector3, t1: Vector3, p2: Vector3, t2: Vector3, nbPoints: number): Curve3 {
  3085. var hermite = new Array<Vector3>();
  3086. var step = 1.0 / nbPoints;
  3087. for (var i = 0; i <= nbPoints; i++) {
  3088. hermite.push(Vector3.Hermite(p1, t1, p2, t2, i * step));
  3089. }
  3090. return new Curve3(hermite);
  3091. }
  3092. /**
  3093. * A Curve3 object is a logical object, so not a mesh, to handle curves in the 3D geometric space.
  3094. * A Curve3 is designed from a series of successive Vector3.
  3095. * Tuto : http://doc.babylonjs.com/tutorials/How_to_use_Curve3#curve3-object
  3096. */
  3097. constructor(points: Vector3[]) {
  3098. this._points = points;
  3099. this._length = this._computeLength(points);
  3100. }
  3101. /**
  3102. * Returns the Curve3 stored array of successive Vector3
  3103. */
  3104. public getPoints() {
  3105. return this._points;
  3106. }
  3107. /**
  3108. * Returns the computed length (float) of the curve.
  3109. */
  3110. public length() {
  3111. return this._length;
  3112. }
  3113. /**
  3114. * Returns a new instance of Curve3 object : var curve = curveA.continue(curveB);
  3115. * This new Curve3 is built by translating and sticking the curveB at the end of the curveA.
  3116. * curveA and curveB keep unchanged.
  3117. */
  3118. public continue(curve: Curve3): Curve3 {
  3119. var lastPoint = this._points[this._points.length - 1];
  3120. var continuedPoints = this._points.slice();
  3121. var curvePoints = curve.getPoints();
  3122. for (var i = 1; i < curvePoints.length; i++) {
  3123. continuedPoints.push(curvePoints[i].subtract(curvePoints[0]).add(lastPoint));
  3124. }
  3125. var continuedCurve = new Curve3(continuedPoints);
  3126. return continuedCurve;
  3127. }
  3128. private _computeLength(path: Vector3[]): number {
  3129. var l = 0;
  3130. for (var i = 1; i < path.length; i++) {
  3131. l += (path[i].subtract(path[i - 1])).length();
  3132. }
  3133. return l;
  3134. }
  3135. }
  3136. // SphericalHarmonics
  3137. export class SphericalHarmonics {
  3138. public L00: Vector3 = Vector3.Zero();
  3139. public L1_1: Vector3 = Vector3.Zero();
  3140. public L10: Vector3 = Vector3.Zero();
  3141. public L11: Vector3 = Vector3.Zero();
  3142. public L2_2: Vector3 = Vector3.Zero();
  3143. public L2_1: Vector3 = Vector3.Zero();
  3144. public L20: Vector3 = Vector3.Zero();
  3145. public L21: Vector3 = Vector3.Zero();
  3146. public L22: Vector3 = Vector3.Zero();
  3147. public addLight(direction: Vector3, color: Color3, deltaSolidAngle: number): void {
  3148. var colorVector = new Vector3(color.r, color.g, color.b);
  3149. var c = colorVector.scale(deltaSolidAngle);
  3150. this.L00 = this.L00.add(c.scale(0.282095));
  3151. this.L1_1 = this.L1_1.add(c.scale(0.488603 * direction.y));
  3152. this.L10 = this.L10.add(c.scale(0.488603 * direction.z));
  3153. this.L11 = this.L11.add(c.scale(0.488603 * direction.x));
  3154. this.L2_2 = this.L2_2.add(c.scale(1.092548 * direction.x * direction.y));
  3155. this.L2_1 = this.L2_1.add(c.scale(1.092548 * direction.y * direction.z));
  3156. this.L21 = this.L21.add(c.scale(1.092548 * direction.x * direction.z));
  3157. this.L20 = this.L20.add(c.scale(0.315392 * (3.0 * direction.z * direction.z - 1.0)));
  3158. this.L22 = this.L22.add(c.scale(0.546274 * (direction.x * direction.x - direction.y * direction.y)));
  3159. }
  3160. public scale(scale: number): void {
  3161. this.L00 = this.L00.scale(scale);
  3162. this.L1_1 = this.L1_1.scale(scale);
  3163. this.L10 = this.L10.scale(scale);
  3164. this.L11 = this.L11.scale(scale);
  3165. this.L2_2 = this.L2_2.scale(scale);
  3166. this.L2_1 = this.L2_1.scale(scale);
  3167. this.L20 = this.L20.scale(scale);
  3168. this.L21 = this.L21.scale(scale);
  3169. this.L22 = this.L22.scale(scale);
  3170. }
  3171. }
  3172. // SphericalPolynomial
  3173. export class SphericalPolynomial {
  3174. public x: Vector3 = Vector3.Zero();
  3175. public y: Vector3 = Vector3.Zero();
  3176. public z: Vector3 = Vector3.Zero();
  3177. public xx: Vector3 = Vector3.Zero();
  3178. public yy: Vector3 = Vector3.Zero();
  3179. public zz: Vector3 = Vector3.Zero();
  3180. public xy: Vector3 = Vector3.Zero();
  3181. public yz: Vector3 = Vector3.Zero();
  3182. public zx: Vector3 = Vector3.Zero();
  3183. public addAmbient(color: Color3): void {
  3184. var colorVector = new Vector3(color.r, color.g, color.b);
  3185. this.xx = this.xx.add(colorVector);
  3186. this.yy = this.yy.add(colorVector);
  3187. this.zz = this.zz.add(colorVector);
  3188. }
  3189. public static getSphericalPolynomialFromHarmonics(harmonics: SphericalHarmonics): SphericalPolynomial {
  3190. var result = new SphericalPolynomial();
  3191. result.x = harmonics.L11.scale(1.02333);
  3192. result.y = harmonics.L1_1.scale(1.02333);
  3193. result.z = harmonics.L10.scale(1.02333);
  3194. result.xx = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).add(harmonics.L22.scale(0.429043));
  3195. result.yy = harmonics.L00.scale(0.886277).subtract(harmonics.L20.scale(0.247708)).subtract(harmonics.L22.scale(0.429043));
  3196. result.zz = harmonics.L00.scale(0.886277).add(harmonics.L20.scale(0.495417));
  3197. result.yz = harmonics.L2_1.scale(0.858086);
  3198. result.zx = harmonics.L21.scale(0.858086);
  3199. result.xy = harmonics.L2_2.scale(0.858086);
  3200. return result;
  3201. }
  3202. }
  3203. // Vertex formats
  3204. export class PositionNormalVertex {
  3205. constructor(public position: Vector3 = Vector3.Zero(), public normal: Vector3 = Vector3.Up()) {
  3206. }
  3207. public clone(): PositionNormalVertex {
  3208. return new PositionNormalVertex(this.position.clone(), this.normal.clone());
  3209. }
  3210. }
  3211. export class PositionNormalTextureVertex {
  3212. constructor(public position: Vector3 = Vector3.Zero(), public normal: Vector3 = Vector3.Up(), public uv: Vector2 = Vector2.Zero()) {
  3213. }
  3214. public clone(): PositionNormalTextureVertex {
  3215. return new PositionNormalTextureVertex(this.position.clone(), this.normal.clone(), this.uv.clone());
  3216. }
  3217. }
  3218. // Temporary pre-allocated objects for engine internal use
  3219. // usage in any internal function :
  3220. // var tmp = Tmp.Vector3[0]; <= gets access to the first pre-created Vector3
  3221. // There's a Tmp array per object type : int, float, Vector2, Vector3, Vector4, Quaternion, Matrix
  3222. export class Tmp {
  3223. public static Color3: Color3[] = [Color3.Black(), Color3.Black(), Color3.Black()];
  3224. public static Vector2: Vector2[] = [Vector2.Zero(), Vector2.Zero(), Vector2.Zero()]; // 3 temp Vector2 at once should be enough
  3225. public static Vector3: Vector3[] = [Vector3.Zero(), Vector3.Zero(), Vector3.Zero(),
  3226. Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero(), Vector3.Zero()]; // 9 temp Vector3 at once should be enough
  3227. public static Vector4: Vector4[] = [Vector4.Zero(), Vector4.Zero(), Vector4.Zero()]; // 3 temp Vector4 at once should be enough
  3228. public static Quaternion: Quaternion[] = [new Quaternion(0, 0, 0, 0)]; // 1 temp Quaternion at once should be enough
  3229. public static Matrix: Matrix[] = [Matrix.Zero(), Matrix.Zero(),
  3230. Matrix.Zero(), Matrix.Zero(),
  3231. Matrix.Zero(), Matrix.Zero(),
  3232. Matrix.Zero(), Matrix.Zero()]; // 6 temp Matrices at once should be enough
  3233. }
  3234. }