babylon.math.ts 127 KB

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