babylon.math.ts 104 KB

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