babylon.math.ts 131 KB

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