babylon.math.ts 104 KB

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