babylon.math.ts 143 KB

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