babylon.math.ts 112 KB

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