babylon.math.ts 134 KB

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