babylon.math.ts 95 KB

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