babylon.math.ts 129 KB

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