babylon.math.ts 135 KB

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