babylon.math.ts 116 KB

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