babylon.math.ts 130 KB

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