babylon.math.ts 139 KB

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