babylon.math.ts 139 KB

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