babylon.math.ts 109 KB

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