babylon.math.ts 128 KB

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