babylon.math.ts 75 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141
  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. BABYLON.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 multiplyInPlace(otherVector: Vector2): void {
  206. this.x *= otherVector.x;
  207. this.y *= otherVector.y;
  208. }
  209. public multiply(otherVector: Vector2): Vector2 {
  210. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  211. }
  212. public multiplyToRef(otherVector: Vector2, result: Vector2): void {
  213. result.x = this.x * otherVector.x;
  214. result.y = this.y * otherVector.y;
  215. }
  216. public multiplyByFloats(x: number, y: number): Vector2 {
  217. return new Vector2(this.x * x, this.y * y);
  218. }
  219. public divide(otherVector: Vector2): Vector2 {
  220. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  221. }
  222. public divideToRef(otherVector: Vector2, result: Vector2): void {
  223. result.x = this.x / otherVector.x;
  224. result.y = this.y / otherVector.y;
  225. }
  226. public negate(): Vector2 {
  227. return new Vector2(-this.x, -this.y);
  228. }
  229. public scaleInPlace(scale: number): void {
  230. this.x *= scale;
  231. this.y *= scale;
  232. }
  233. public scale(scale: number): Vector2 {
  234. return new Vector2(this.x * scale, this.y * scale);
  235. }
  236. public equals(otherVector: Vector2): boolean {
  237. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  238. }
  239. // Properties
  240. public length(): number {
  241. return Math.sqrt(this.x * this.x + this.y * this.y);
  242. }
  243. public lengthSquared(): number {
  244. return (this.x * this.x + this.y * this.y);
  245. }
  246. // Methods
  247. public normalize(): void {
  248. var len = this.length();
  249. if (len === 0)
  250. return;
  251. var num = 1.0 / len;
  252. this.x *= num;
  253. this.y *= num;
  254. }
  255. public clone(): Vector2 {
  256. return new Vector2(this.x, this.y);
  257. }
  258. // Statics
  259. public static Zero(): Vector2 {
  260. return new Vector2(0, 0);
  261. }
  262. public static FromArray(array: number[], offset?: number): Vector2 {
  263. if (!offset) {
  264. offset = 0;
  265. }
  266. return new Vector2(array[offset], array[offset + 1]);
  267. }
  268. public static FromArrayToRef(array: number[], offset: number, result: Vector2): void {
  269. result.x = array[offset];
  270. result.y = array[offset + 1];
  271. }
  272. public static CatmullRom(value1: Vector2, value2: Vector2, value3: Vector2, value4: Vector2, amount: number): Vector2 {
  273. var squared = amount * amount;
  274. var cubed = amount * squared;
  275. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  276. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  277. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  278. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  279. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  280. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  281. return new Vector2(x, y);
  282. }
  283. public static Clamp(value: Vector2, min: Vector2, max: Vector2): Vector2 {
  284. var x = value.x;
  285. x = (x > max.x) ? max.x : x;
  286. x = (x < min.x) ? min.x : x;
  287. var y = value.y;
  288. y = (y > max.y) ? max.y : y;
  289. y = (y < min.y) ? min.y : y;
  290. return new Vector2(x, y);
  291. }
  292. public static Hermite(value1: Vector2, tangent1: Vector2, value2: Vector2, tangent2: Vector2, amount: number): Vector2 {
  293. var squared = amount * amount;
  294. var cubed = amount * squared;
  295. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  296. var part2 = (-2.0 * cubed) + (3.0 * squared);
  297. var part3 = (cubed - (2.0 * squared)) + amount;
  298. var part4 = cubed - squared;
  299. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  300. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  301. return new Vector2(x, y);
  302. }
  303. public static Lerp(start: Vector2, end: Vector2, amount: number): Vector2 {
  304. var x = start.x + ((end.x - start.x) * amount);
  305. var y = start.y + ((end.y - start.y) * amount);
  306. return new Vector2(x, y);
  307. }
  308. public static Dot(left: Vector2, right: Vector2): number {
  309. return left.x * right.x + left.y * right.y;
  310. }
  311. public static Normalize(vector: Vector2): Vector2 {
  312. var newVector = vector.clone();
  313. newVector.normalize();
  314. return newVector;
  315. }
  316. public static Minimize(left: Vector2, right: Vector2): Vector2 {
  317. var x = (left.x < right.x) ? left.x : right.x;
  318. var y = (left.y < right.y) ? left.y : right.y;
  319. return new Vector2(x, y);
  320. }
  321. public static Maximize(left: Vector2, right: Vector2): Vector2 {
  322. var x = (left.x > right.x) ? left.x : right.x;
  323. var y = (left.y > right.y) ? left.y : right.y;
  324. return new Vector2(x, y);
  325. }
  326. public static Transform(vector: Vector2, transformation: Matrix): Vector2 {
  327. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  328. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  329. return new Vector2(x, y);
  330. }
  331. public static Distance(value1: Vector2, value2: Vector2): number {
  332. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  333. }
  334. public static DistanceSquared(value1: Vector2, value2: Vector2): number {
  335. var x = value1.x - value2.x;
  336. var y = value1.y - value2.y;
  337. return (x * x) + (y * y);
  338. }
  339. }
  340. export class Vector3 {
  341. constructor(public x: number, public y: number, public z: number) {
  342. }
  343. public toString(): string {
  344. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  345. }
  346. // Operators
  347. public asArray(): number[] {
  348. var result = [];
  349. this.toArray(result, 0);
  350. return result;
  351. }
  352. public toArray(array: number[], index?: number): void {
  353. if (index === undefined) {
  354. index = 0;
  355. }
  356. array[index] = this.x;
  357. array[index + 1] = this.y;
  358. array[index + 2] = this.z;
  359. }
  360. public addInPlace(otherVector: Vector3): void {
  361. this.x += otherVector.x;
  362. this.y += otherVector.y;
  363. this.z += otherVector.z;
  364. }
  365. public add(otherVector: Vector3): Vector3 {
  366. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  367. }
  368. public addToRef(otherVector: Vector3, result: Vector3): void {
  369. result.x = this.x + otherVector.x;
  370. result.y = this.y + otherVector.y;
  371. result.z = this.z + otherVector.z;
  372. }
  373. public subtractInPlace(otherVector: Vector3): void {
  374. this.x -= otherVector.x;
  375. this.y -= otherVector.y;
  376. this.z -= otherVector.z;
  377. }
  378. public subtract(otherVector: Vector3): Vector3 {
  379. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  380. }
  381. public subtractToRef(otherVector: Vector3, result: Vector3): void {
  382. result.x = this.x - otherVector.x;
  383. result.y = this.y - otherVector.y;
  384. result.z = this.z - otherVector.z;
  385. }
  386. public subtractFromFloats(x: number, y: number, z: number): Vector3 {
  387. return new Vector3(this.x - x, this.y - y, this.z - z);
  388. }
  389. public subtractFromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
  390. result.x = this.x - x;
  391. result.y = this.y - y;
  392. result.z = this.z - z;
  393. }
  394. public negate(): Vector3 {
  395. return new Vector3(-this.x, -this.y, -this.z);
  396. }
  397. public scaleInPlace(scale: number): void {
  398. this.x *= scale;
  399. this.y *= scale;
  400. this.z *= scale;
  401. }
  402. public scale(scale: number): Vector3 {
  403. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  404. }
  405. public scaleToRef(scale: number, result: Vector3) {
  406. result.x = this.x * scale;
  407. result.y = this.y * scale;
  408. result.z = this.z * scale;
  409. }
  410. public equals(otherVector: Vector3): boolean {
  411. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  412. }
  413. public equalsWithEpsilon(otherVector: Vector3): boolean {
  414. return Math.abs(this.x - otherVector.x) < Engine.Epsilon &&
  415. Math.abs(this.y - otherVector.y) < Engine.Epsilon &&
  416. Math.abs(this.z - otherVector.z) < Engine.Epsilon;
  417. }
  418. public equalsToFloats(x: number, y: number, z: number): boolean {
  419. return this.x === x && this.y === y && this.z === z;
  420. }
  421. public multiplyInPlace(otherVector: Vector3): void {
  422. this.x *= otherVector.x;
  423. this.y *= otherVector.y;
  424. this.z *= otherVector.z;
  425. }
  426. public multiply(otherVector: Vector3): Vector3 {
  427. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  428. }
  429. public multiplyToRef(otherVector: Vector3, result: Vector3): void {
  430. result.x = this.x * otherVector.x;
  431. result.y = this.y * otherVector.y;
  432. result.z = this.z * otherVector.z;
  433. }
  434. public multiplyByFloats(x: number, y: number, z: number): Vector3 {
  435. return new Vector3(this.x * x, this.y * y, this.z * z);
  436. }
  437. public divide(otherVector: Vector3): Vector3 {
  438. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  439. }
  440. public divideToRef(otherVector: Vector3, result: Vector3): void {
  441. result.x = this.x / otherVector.x;
  442. result.y = this.y / otherVector.y;
  443. result.z = this.z / otherVector.z;
  444. }
  445. public MinimizeInPlace(other: Vector3): void {
  446. if (other.x < this.x) this.x = other.x;
  447. if (other.y < this.y) this.y = other.y;
  448. if (other.z < this.z) this.z = other.z;
  449. }
  450. public MaximizeInPlace(other: Vector3): void {
  451. if (other.x > this.x) this.x = other.x;
  452. if (other.y > this.y) this.y = other.y;
  453. if (other.z > this.z) this.z = other.z;
  454. }
  455. // Properties
  456. public length(): number {
  457. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  458. }
  459. public lengthSquared(): number {
  460. return (this.x * this.x + this.y * this.y + this.z * this.z);
  461. }
  462. // Methods
  463. public normalize(): void {
  464. var len = this.length();
  465. if (len === 0)
  466. return;
  467. var num = 1.0 / len;
  468. this.x *= num;
  469. this.y *= num;
  470. this.z *= num;
  471. }
  472. public clone(): Vector3 {
  473. return new Vector3(this.x, this.y, this.z);
  474. }
  475. public copyFrom(source: Vector3): void {
  476. this.x = source.x;
  477. this.y = source.y;
  478. this.z = source.z;
  479. }
  480. public copyFromFloats(x: number, y: number, z: number): void {
  481. this.x = x;
  482. this.y = y;
  483. this.z = z;
  484. }
  485. // Statics
  486. public static FromArray(array: number[], offset?: number): Vector3 {
  487. if (!offset) {
  488. offset = 0;
  489. }
  490. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  491. }
  492. public static FromArrayToRef(array: number[], offset: number, result: Vector3): void {
  493. result.x = array[offset];
  494. result.y = array[offset + 1];
  495. result.z = array[offset + 2];
  496. }
  497. public static FromFloatArrayToRef(array: Float32Array, offset: number, result: Vector3): void {
  498. result.x = array[offset];
  499. result.y = array[offset + 1];
  500. result.z = array[offset + 2];
  501. }
  502. public static FromFloatsToRef(x: number, y: number, z: number, result: Vector3): void {
  503. result.x = x;
  504. result.y = y;
  505. result.z = z;
  506. }
  507. public static Zero(): Vector3 {
  508. return new Vector3(0, 0, 0);
  509. }
  510. public static Up(): Vector3 {
  511. return new Vector3(0, 1.0, 0);
  512. }
  513. public static TransformCoordinates(vector: Vector3, transformation: Matrix): Vector3 {
  514. var result = Vector3.Zero();
  515. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  516. return result;
  517. }
  518. public static TransformCoordinatesToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
  519. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  520. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  521. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  522. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  523. result.x = x / w;
  524. result.y = y / w;
  525. result.z = z / w;
  526. }
  527. public static TransformCoordinatesFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  528. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  529. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  530. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  531. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  532. result.x = rx / rw;
  533. result.y = ry / rw;
  534. result.z = rz / rw;
  535. }
  536. public static TransformNormal(vector: Vector3, transformation: Matrix): Vector3 {
  537. var result = Vector3.Zero();
  538. Vector3.TransformNormalToRef(vector, transformation, result);
  539. return result;
  540. }
  541. public static TransformNormalToRef(vector: Vector3, transformation: Matrix, result: Vector3): void {
  542. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  543. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  544. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  545. }
  546. public static TransformNormalFromFloatsToRef(x: number, y: number, z: number, transformation: Matrix, result: Vector3): void {
  547. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  548. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  549. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  550. }
  551. public static CatmullRom(value1: Vector3, value2: Vector3, value3: Vector3, value4: Vector3, amount: number): Vector3 {
  552. var squared = amount * amount;
  553. var cubed = amount * squared;
  554. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) +
  555. (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) +
  556. ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  557. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) +
  558. (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) +
  559. ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  560. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) +
  561. (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) +
  562. ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  563. return new Vector3(x, y, z);
  564. }
  565. public static Clamp(value: Vector3, min: Vector3, max: Vector3): Vector3 {
  566. var x = value.x;
  567. x = (x > max.x) ? max.x : x;
  568. x = (x < min.x) ? min.x : x;
  569. var y = value.y;
  570. y = (y > max.y) ? max.y : y;
  571. y = (y < min.y) ? min.y : y;
  572. var z = value.z;
  573. z = (z > max.z) ? max.z : z;
  574. z = (z < min.z) ? min.z : z;
  575. return new Vector3(x, y, z);
  576. }
  577. public static Hermite(value1: Vector3, tangent1: Vector3, value2: Vector3, tangent2: Vector3, amount: number): Vector3 {
  578. var squared = amount * amount;
  579. var cubed = amount * squared;
  580. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  581. var part2 = (-2.0 * cubed) + (3.0 * squared);
  582. var part3 = (cubed - (2.0 * squared)) + amount;
  583. var part4 = cubed - squared;
  584. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  585. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  586. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  587. return new Vector3(x, y, z);
  588. }
  589. public static Lerp(start: Vector3, end: Vector3, amount: number): Vector3 {
  590. var x = start.x + ((end.x - start.x) * amount);
  591. var y = start.y + ((end.y - start.y) * amount);
  592. var z = start.z + ((end.z - start.z) * amount);
  593. return new Vector3(x, y, z);
  594. }
  595. public static Dot(left: Vector3, right: Vector3): number {
  596. return (left.x * right.x + left.y * right.y + left.z * right.z);
  597. }
  598. public static Cross(left: Vector3, right: Vector3): Vector3 {
  599. var result = Vector3.Zero();
  600. Vector3.CrossToRef(left, right, result);
  601. return result;
  602. }
  603. public static CrossToRef(left: Vector3, right: Vector3, result: Vector3): void {
  604. result.x = left.y * right.z - left.z * right.y;
  605. result.y = left.z * right.x - left.x * right.z;
  606. result.z = left.x * right.y - left.y * right.x;
  607. }
  608. public static Normalize(vector: Vector3): Vector3 {
  609. var result = Vector3.Zero();
  610. Vector3.NormalizeToRef(vector, result);
  611. return result;
  612. }
  613. public static NormalizeToRef(vector: Vector3, result: Vector3): void {
  614. result.copyFrom(vector);
  615. result.normalize();
  616. }
  617. public static Project(vector: Vector3, world: Matrix, transform: Matrix, viewport: Viewport): Vector3 {
  618. var cw = viewport.width;
  619. var ch = viewport.height;
  620. var cx = viewport.x;
  621. var cy = viewport.y;
  622. var viewportMatrix = BABYLON.Matrix.FromValues(
  623. cw / 2.0, 0, 0, 0,
  624. 0, -ch / 2.0, 0, 0,
  625. 0, 0, 1, 0,
  626. cx + cw / 2.0, ch / 2.0 + cy, 0, 1);
  627. var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
  628. return Vector3.TransformCoordinates(vector, finalMatrix);
  629. }
  630. public static Unproject(source: Vector3, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Vector3 {
  631. var matrix = world.multiply(view).multiply(projection);
  632. matrix.invert();
  633. source.x = source.x / viewportWidth * 2 - 1;
  634. source.y = -(source.y / viewportHeight * 2 - 1);
  635. var vector = BABYLON.Vector3.TransformCoordinates(source, matrix);
  636. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  637. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  638. vector = vector.scale(1.0 / num);
  639. }
  640. return vector;
  641. }
  642. public static Minimize(left: Vector3, right: Vector3): Vector3 {
  643. var min = left.clone();
  644. min.MinimizeInPlace(right);
  645. return min;
  646. }
  647. public static Maximize(left: Vector3, right: Vector3): Vector3 {
  648. var max = left.clone();
  649. max.MaximizeInPlace(right);
  650. return max;
  651. }
  652. public static Distance(value1: Vector3, value2: Vector3): number {
  653. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  654. }
  655. public static DistanceSquared(value1: Vector3, value2: Vector3): number {
  656. var x = value1.x - value2.x;
  657. var y = value1.y - value2.y;
  658. var z = value1.z - value2.z;
  659. return (x * x) + (y * y) + (z * z);
  660. }
  661. public static Center(value1: Vector3, value2: Vector3): Vector3 {
  662. var center = value1.add(value2);
  663. center.scaleInPlace(0.5);
  664. return center;
  665. }
  666. }
  667. export class Quaternion {
  668. constructor(public x: number = 0, public y: number = 0, public z: number = 0, public w: number = 1) {
  669. }
  670. public toString(): string {
  671. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  672. }
  673. public asArray(): number[] {
  674. return [this.x, this.y, this.z, this.w];
  675. }
  676. public equals(otherQuaternion: Quaternion): boolean {
  677. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  678. }
  679. public clone(): Quaternion {
  680. return new Quaternion(this.x, this.y, this.z, this.w);
  681. }
  682. public copyFrom(other: Quaternion): void {
  683. this.x = other.x;
  684. this.y = other.y;
  685. this.z = other.z;
  686. this.w = other.w;
  687. }
  688. public add(other: Quaternion): Quaternion {
  689. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  690. }
  691. public subtract(other: Quaternion): Quaternion {
  692. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  693. }
  694. public scale(value: number): Quaternion {
  695. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  696. }
  697. public multiply(q1: Quaternion): Quaternion {
  698. var result = new Quaternion(0, 0, 0, 1.0);
  699. this.multiplyToRef(q1, result);
  700. return result;
  701. }
  702. public multiplyToRef(q1: Quaternion, result: Quaternion): void {
  703. result.x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  704. result.y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  705. result.z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  706. result.w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  707. }
  708. public length(): number {
  709. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  710. }
  711. public normalize(): void {
  712. var length = 1.0 / this.length();
  713. this.x *= length;
  714. this.y *= length;
  715. this.z *= length;
  716. this.w *= length;
  717. }
  718. public toEulerAngles(): Vector3 {
  719. var qx = this.x;
  720. var qy = this.y;
  721. var qz = this.z;
  722. var qw = this.w;
  723. var sqx = qx * qx;
  724. var sqy = qy * qy;
  725. var sqz = qz * qz;
  726. var yaw = Math.atan2(2.0 * (qy * qw - qx * qz), 1.0 - 2.0 * (sqy + sqz));
  727. var pitch = Math.asin(2.0 * (qx * qy + qz * qw));
  728. var roll = Math.atan2(2.0 * (qx * qw - qy * qz), 1.0 - 2.0 * (sqx + sqz));
  729. var gimbaLockTest = qx * qy + qz * qw;
  730. if (gimbaLockTest > 0.499) {
  731. yaw = 2.0 * Math.atan2(qx, qw);
  732. roll = 0;
  733. } else if (gimbaLockTest < -0.499) {
  734. yaw = -2.0 * Math.atan2(qx, qw);
  735. roll = 0;
  736. }
  737. return new Vector3(pitch, yaw, roll);
  738. }
  739. public toRotationMatrix(result: Matrix): void {
  740. var xx = this.x * this.x;
  741. var yy = this.y * this.y;
  742. var zz = this.z * this.z;
  743. var xy = this.x * this.y;
  744. var zw = this.z * this.w;
  745. var zx = this.z * this.x;
  746. var yw = this.y * this.w;
  747. var yz = this.y * this.z;
  748. var xw = this.x * this.w;
  749. result.m[0] = 1.0 - (2.0 * (yy + zz));
  750. result.m[1] = 2.0 * (xy + zw);
  751. result.m[2] = 2.0 * (zx - yw);
  752. result.m[3] = 0;
  753. result.m[4] = 2.0 * (xy - zw);
  754. result.m[5] = 1.0 - (2.0 * (zz + xx));
  755. result.m[6] = 2.0 * (yz + xw);
  756. result.m[7] = 0;
  757. result.m[8] = 2.0 * (zx + yw);
  758. result.m[9] = 2.0 * (yz - xw);
  759. result.m[10] = 1.0 - (2.0 * (yy + xx));
  760. result.m[11] = 0;
  761. result.m[12] = 0;
  762. result.m[13] = 0;
  763. result.m[14] = 0;
  764. result.m[15] = 1.0;
  765. }
  766. public fromRotationMatrix(matrix: Matrix): void {
  767. var data = matrix.m;
  768. var m11 = data[0], m12 = data[4], m13 = data[8];
  769. var m21 = data[1], m22 = data[5], m23 = data[9];
  770. var m31 = data[2], m32 = data[6], m33 = data[10];
  771. var trace = m11 + m22 + m33;
  772. var s;
  773. if (trace > 0) {
  774. s = 0.5 / Math.sqrt(trace + 1.0);
  775. this.w = 0.25 / s;
  776. this.x = (m32 - m23) * s;
  777. this.y = (m13 - m31) * s;
  778. this.z = (m21 - m12) * s;
  779. return;
  780. }
  781. if (m11 > m22 && m11 > m33) {
  782. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  783. this.w = (m32 - m23) / s;
  784. this.x = 0.25 * s;
  785. this.y = (m12 + m21) / s;
  786. this.z = (m13 + m31) / s;
  787. return;
  788. }
  789. if (m22 > m33) {
  790. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  791. this.w = (m13 - m31) / s;
  792. this.x = (m12 + m21) / s;
  793. this.y = 0.25 * s;
  794. this.z = (m23 + m32) / s;
  795. return;
  796. }
  797. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  798. this.w = (m21 - m12) / s;
  799. this.x = (m13 + m31) / s;
  800. this.y = (m23 + m32) / s;
  801. this.z = 0.25 * s;
  802. }
  803. // Statics
  804. public static RotationAxis(axis: Vector3, angle: number): Quaternion {
  805. var result = new Quaternion();
  806. var sin = Math.sin(angle / 2);
  807. result.w = Math.cos(angle / 2);
  808. result.x = axis.x * sin;
  809. result.y = axis.y * sin;
  810. result.z = axis.z * sin;
  811. return result;
  812. }
  813. public static FromArray(array: number[], offset?: number): Quaternion {
  814. if (!offset) {
  815. offset = 0;
  816. }
  817. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  818. }
  819. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Quaternion {
  820. var result = new Quaternion();
  821. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  822. return result;
  823. }
  824. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Quaternion): void {
  825. var halfRoll = roll * 0.5;
  826. var halfPitch = pitch * 0.5;
  827. var halfYaw = yaw * 0.5;
  828. var sinRoll = Math.sin(halfRoll);
  829. var cosRoll = Math.cos(halfRoll);
  830. var sinPitch = Math.sin(halfPitch);
  831. var cosPitch = Math.cos(halfPitch);
  832. var sinYaw = Math.sin(halfYaw);
  833. var cosYaw = Math.cos(halfYaw);
  834. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  835. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  836. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  837. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  838. }
  839. public static Slerp(left: Quaternion, right: Quaternion, amount: number): Quaternion {
  840. var num2;
  841. var num3;
  842. var num = amount;
  843. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  844. var flag = false;
  845. if (num4 < 0) {
  846. flag = true;
  847. num4 = -num4;
  848. }
  849. if (num4 > 0.999999) {
  850. num3 = 1 - num;
  851. num2 = flag ? -num : num;
  852. }
  853. else {
  854. var num5 = Math.acos(num4);
  855. var num6 = (1.0 / Math.sin(num5));
  856. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  857. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  858. }
  859. 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));
  860. }
  861. public static Inverse(q: Quaternion): Quaternion {
  862. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  863. }
  864. }
  865. export class Matrix {
  866. private static _tempQuaternion: Quaternion = new Quaternion();
  867. private static _xAxis: Vector3 = Vector3.Zero();
  868. private static _yAxis: Vector3 = Vector3.Zero();
  869. private static _zAxis: Vector3 = Vector3.Zero();
  870. public m: Float32Array = new Float32Array(16);
  871. // Properties
  872. public isIdentity(): boolean {
  873. if (this.m[0] != 1.0 || this.m[5] != 1.0 || this.m[10] != 1.0 || this.m[15] != 1.0)
  874. return false;
  875. if (this.m[1] != 0.0 || this.m[2] != 0.0 || this.m[3] != 0.0 ||
  876. this.m[4] != 0.0 || this.m[6] != 0.0 || this.m[7] != 0.0 ||
  877. this.m[8] != 0.0 || this.m[9] != 0.0 || this.m[11] != 0.0 ||
  878. this.m[12] != 0.0 || this.m[13] != 0.0 || this.m[14] != 0.0)
  879. return false;
  880. return true;
  881. }
  882. public determinant(): number {
  883. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  884. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  885. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  886. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  887. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  888. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  889. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) -
  890. (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) -
  891. (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  892. }
  893. // Methods
  894. public toArray(): Float32Array {
  895. return this.m;
  896. }
  897. public asArray(): Float32Array {
  898. return this.toArray();
  899. }
  900. public invert(): void {
  901. this.invertToRef(this);
  902. }
  903. public invertToRef(other: Matrix) {
  904. var l1 = this.m[0];
  905. var l2 = this.m[1];
  906. var l3 = this.m[2];
  907. var l4 = this.m[3];
  908. var l5 = this.m[4];
  909. var l6 = this.m[5];
  910. var l7 = this.m[6];
  911. var l8 = this.m[7];
  912. var l9 = this.m[8];
  913. var l10 = this.m[9];
  914. var l11 = this.m[10];
  915. var l12 = this.m[11];
  916. var l13 = this.m[12];
  917. var l14 = this.m[13];
  918. var l15 = this.m[14];
  919. var l16 = this.m[15];
  920. var l17 = (l11 * l16) - (l12 * l15);
  921. var l18 = (l10 * l16) - (l12 * l14);
  922. var l19 = (l10 * l15) - (l11 * l14);
  923. var l20 = (l9 * l16) - (l12 * l13);
  924. var l21 = (l9 * l15) - (l11 * l13);
  925. var l22 = (l9 * l14) - (l10 * l13);
  926. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  927. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  928. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  929. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  930. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  931. var l28 = (l7 * l16) - (l8 * l15);
  932. var l29 = (l6 * l16) - (l8 * l14);
  933. var l30 = (l6 * l15) - (l7 * l14);
  934. var l31 = (l5 * l16) - (l8 * l13);
  935. var l32 = (l5 * l15) - (l7 * l13);
  936. var l33 = (l5 * l14) - (l6 * l13);
  937. var l34 = (l7 * l12) - (l8 * l11);
  938. var l35 = (l6 * l12) - (l8 * l10);
  939. var l36 = (l6 * l11) - (l7 * l10);
  940. var l37 = (l5 * l12) - (l8 * l9);
  941. var l38 = (l5 * l11) - (l7 * l9);
  942. var l39 = (l5 * l10) - (l6 * l9);
  943. other.m[0] = l23 * l27;
  944. other.m[4] = l24 * l27;
  945. other.m[8] = l25 * l27;
  946. other.m[12] = l26 * l27;
  947. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  948. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  949. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  950. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  951. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  952. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  953. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  954. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  955. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  956. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  957. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  958. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  959. }
  960. public setTranslation(vector3: Vector3): void {
  961. this.m[12] = vector3.x;
  962. this.m[13] = vector3.y;
  963. this.m[14] = vector3.z;
  964. }
  965. public multiply(other: Matrix): Matrix {
  966. var result = new Matrix();
  967. this.multiplyToRef(other, result);
  968. return result;
  969. }
  970. public copyFrom(other: Matrix): void {
  971. for (var index = 0; index < 16; index++) {
  972. this.m[index] = other.m[index];
  973. }
  974. }
  975. public copyToArray(array: Float32Array, offset: number = 0): void {
  976. for (var index = 0; index < 16; index++) {
  977. array[offset + index] = this.m[index];
  978. }
  979. }
  980. public multiplyToRef(other: Matrix, result: Matrix): void {
  981. this.multiplyToArray(other, result.m, 0);
  982. }
  983. public multiplyToArray(other: Matrix, result: Float32Array, offset: number): void {
  984. var tm0 = this.m[0];
  985. var tm1 = this.m[1];
  986. var tm2 = this.m[2];
  987. var tm3 = this.m[3];
  988. var tm4 = this.m[4];
  989. var tm5 = this.m[5];
  990. var tm6 = this.m[6];
  991. var tm7 = this.m[7];
  992. var tm8 = this.m[8];
  993. var tm9 = this.m[9];
  994. var tm10 = this.m[10];
  995. var tm11 = this.m[11];
  996. var tm12 = this.m[12];
  997. var tm13 = this.m[13];
  998. var tm14 = this.m[14];
  999. var tm15 = this.m[15];
  1000. var om0 = other.m[0];
  1001. var om1 = other.m[1];
  1002. var om2 = other.m[2];
  1003. var om3 = other.m[3];
  1004. var om4 = other.m[4];
  1005. var om5 = other.m[5];
  1006. var om6 = other.m[6];
  1007. var om7 = other.m[7];
  1008. var om8 = other.m[8];
  1009. var om9 = other.m[9];
  1010. var om10 = other.m[10];
  1011. var om11 = other.m[11];
  1012. var om12 = other.m[12];
  1013. var om13 = other.m[13];
  1014. var om14 = other.m[14];
  1015. var om15 = other.m[15];
  1016. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1017. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1018. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1019. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1020. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1021. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1022. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1023. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1024. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1025. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1026. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1027. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1028. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1029. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1030. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1031. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1032. }
  1033. public equals(value: Matrix): boolean {
  1034. return value &&
  1035. (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] &&
  1036. this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] &&
  1037. this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] &&
  1038. this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  1039. }
  1040. public clone(): Matrix {
  1041. return Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3],
  1042. this.m[4], this.m[5], this.m[6], this.m[7],
  1043. this.m[8], this.m[9], this.m[10], this.m[11],
  1044. this.m[12], this.m[13], this.m[14], this.m[15]);
  1045. }
  1046. // Statics
  1047. public static FromArray(array: number[], offset?: number): Matrix {
  1048. var result = new Matrix();
  1049. if (!offset) {
  1050. offset = 0;
  1051. }
  1052. Matrix.FromArrayToRef(array, offset, result);
  1053. return result;
  1054. }
  1055. public static FromArrayToRef(array: number[], offset: number, result: Matrix) {
  1056. for (var index = 0; index < 16; index++) {
  1057. result.m[index] = array[index + offset];
  1058. }
  1059. }
  1060. public static FromValuesToRef(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  1061. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  1062. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  1063. initialM41: number, initialM42: number, initialM43: number, initialM44: number, result: Matrix): void {
  1064. result.m[0] = initialM11;
  1065. result.m[1] = initialM12;
  1066. result.m[2] = initialM13;
  1067. result.m[3] = initialM14;
  1068. result.m[4] = initialM21;
  1069. result.m[5] = initialM22;
  1070. result.m[6] = initialM23;
  1071. result.m[7] = initialM24;
  1072. result.m[8] = initialM31;
  1073. result.m[9] = initialM32;
  1074. result.m[10] = initialM33;
  1075. result.m[11] = initialM34;
  1076. result.m[12] = initialM41;
  1077. result.m[13] = initialM42;
  1078. result.m[14] = initialM43;
  1079. result.m[15] = initialM44;
  1080. }
  1081. public static FromValues(initialM11: number, initialM12: number, initialM13: number, initialM14: number,
  1082. initialM21: number, initialM22: number, initialM23: number, initialM24: number,
  1083. initialM31: number, initialM32: number, initialM33: number, initialM34: number,
  1084. initialM41: number, initialM42: number, initialM43: number, initialM44: number): Matrix {
  1085. var result = new Matrix();
  1086. result.m[0] = initialM11;
  1087. result.m[1] = initialM12;
  1088. result.m[2] = initialM13;
  1089. result.m[3] = initialM14;
  1090. result.m[4] = initialM21;
  1091. result.m[5] = initialM22;
  1092. result.m[6] = initialM23;
  1093. result.m[7] = initialM24;
  1094. result.m[8] = initialM31;
  1095. result.m[9] = initialM32;
  1096. result.m[10] = initialM33;
  1097. result.m[11] = initialM34;
  1098. result.m[12] = initialM41;
  1099. result.m[13] = initialM42;
  1100. result.m[14] = initialM43;
  1101. result.m[15] = initialM44;
  1102. return result;
  1103. }
  1104. public static Identity(): Matrix {
  1105. return Matrix.FromValues(1.0, 0, 0, 0,
  1106. 0, 1.0, 0, 0,
  1107. 0, 0, 1.0, 0,
  1108. 0, 0, 0, 1.0);
  1109. }
  1110. public static IdentityToRef(result: Matrix): void {
  1111. Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1112. 0, 1.0, 0, 0,
  1113. 0, 0, 1.0, 0,
  1114. 0, 0, 0, 1.0, result);
  1115. }
  1116. public static Zero(): Matrix {
  1117. return Matrix.FromValues(0, 0, 0, 0,
  1118. 0, 0, 0, 0,
  1119. 0, 0, 0, 0,
  1120. 0, 0, 0, 0);
  1121. }
  1122. public static RotationX(angle: number): Matrix {
  1123. var result = new Matrix();
  1124. Matrix.RotationXToRef(angle, result);
  1125. return result;
  1126. }
  1127. public static RotationXToRef(angle: number, result: Matrix): void {
  1128. var s = Math.sin(angle);
  1129. var c = Math.cos(angle);
  1130. result.m[0] = 1.0;
  1131. result.m[15] = 1.0;
  1132. result.m[5] = c;
  1133. result.m[10] = c;
  1134. result.m[9] = -s;
  1135. result.m[6] = s;
  1136. result.m[1] = 0;
  1137. result.m[2] = 0;
  1138. result.m[3] = 0;
  1139. result.m[4] = 0;
  1140. result.m[7] = 0;
  1141. result.m[8] = 0;
  1142. result.m[11] = 0;
  1143. result.m[12] = 0;
  1144. result.m[13] = 0;
  1145. result.m[14] = 0;
  1146. }
  1147. public static RotationY(angle: number): Matrix {
  1148. var result = new Matrix();
  1149. Matrix.RotationYToRef(angle, result);
  1150. return result;
  1151. }
  1152. public static RotationYToRef(angle: number, result: Matrix): void {
  1153. var s = Math.sin(angle);
  1154. var c = Math.cos(angle);
  1155. result.m[5] = 1.0;
  1156. result.m[15] = 1.0;
  1157. result.m[0] = c;
  1158. result.m[2] = -s;
  1159. result.m[8] = s;
  1160. result.m[10] = c;
  1161. result.m[1] = 0;
  1162. result.m[3] = 0;
  1163. result.m[4] = 0;
  1164. result.m[6] = 0;
  1165. result.m[7] = 0;
  1166. result.m[9] = 0;
  1167. result.m[11] = 0;
  1168. result.m[12] = 0;
  1169. result.m[13] = 0;
  1170. result.m[14] = 0;
  1171. }
  1172. public static RotationZ(angle: number): Matrix {
  1173. var result = new Matrix();
  1174. Matrix.RotationZToRef(angle, result);
  1175. return result;
  1176. }
  1177. public static RotationZToRef(angle: number, result: Matrix): void {
  1178. var s = Math.sin(angle);
  1179. var c = Math.cos(angle);
  1180. result.m[10] = 1.0;
  1181. result.m[15] = 1.0;
  1182. result.m[0] = c;
  1183. result.m[1] = s;
  1184. result.m[4] = -s;
  1185. result.m[5] = c;
  1186. result.m[2] = 0;
  1187. result.m[3] = 0;
  1188. result.m[6] = 0;
  1189. result.m[7] = 0;
  1190. result.m[8] = 0;
  1191. result.m[9] = 0;
  1192. result.m[11] = 0;
  1193. result.m[12] = 0;
  1194. result.m[13] = 0;
  1195. result.m[14] = 0;
  1196. }
  1197. public static RotationAxis(axis: Vector3, angle: number): Matrix {
  1198. var s = Math.sin(-angle);
  1199. var c = Math.cos(-angle);
  1200. var c1 = 1 - c;
  1201. axis.normalize();
  1202. var result = Matrix.Zero();
  1203. result.m[0] = (axis.x * axis.x) * c1 + c;
  1204. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1205. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1206. result.m[3] = 0.0;
  1207. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1208. result.m[5] = (axis.y * axis.y) * c1 + c;
  1209. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1210. result.m[7] = 0.0;
  1211. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1212. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1213. result.m[10] = (axis.z * axis.z) * c1 + c;
  1214. result.m[11] = 0.0;
  1215. result.m[15] = 1.0;
  1216. return result;
  1217. }
  1218. public static RotationYawPitchRoll(yaw: number, pitch: number, roll: number): Matrix {
  1219. var result = new Matrix();
  1220. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1221. return result;
  1222. }
  1223. public static RotationYawPitchRollToRef(yaw: number, pitch: number, roll: number, result: Matrix): void {
  1224. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  1225. this._tempQuaternion.toRotationMatrix(result);
  1226. }
  1227. public static Scaling(x: number, y: number, z: number): Matrix {
  1228. var result = Matrix.Zero();
  1229. Matrix.ScalingToRef(x, y, z, result);
  1230. return result;
  1231. }
  1232. public static ScalingToRef(x: number, y: number, z: number, result: Matrix): void {
  1233. result.m[0] = x;
  1234. result.m[1] = 0;
  1235. result.m[2] = 0;
  1236. result.m[3] = 0;
  1237. result.m[4] = 0;
  1238. result.m[5] = y;
  1239. result.m[6] = 0;
  1240. result.m[7] = 0;
  1241. result.m[8] = 0;
  1242. result.m[9] = 0;
  1243. result.m[10] = z;
  1244. result.m[11] = 0;
  1245. result.m[12] = 0;
  1246. result.m[13] = 0;
  1247. result.m[14] = 0;
  1248. result.m[15] = 1.0;
  1249. }
  1250. public static Translation(x: number, y: number, z: number): Matrix {
  1251. var result = Matrix.Identity();
  1252. Matrix.TranslationToRef(x, y, z, result);
  1253. return result;
  1254. }
  1255. public static TranslationToRef(x: number, y: number, z: number, result: Matrix): void {
  1256. Matrix.FromValuesToRef(1.0, 0, 0, 0,
  1257. 0, 1.0, 0, 0,
  1258. 0, 0, 1.0, 0,
  1259. x, y, z, 1.0, result);
  1260. }
  1261. public static LookAtLH(eye: Vector3, target: Vector3, up: Vector3): Matrix {
  1262. var result = Matrix.Zero();
  1263. Matrix.LookAtLHToRef(eye, target, up, result);
  1264. return result;
  1265. }
  1266. public static LookAtLHToRef(eye: Vector3, target: Vector3, up: Vector3, result: Matrix): void {
  1267. // Z axis
  1268. target.subtractToRef(eye, this._zAxis);
  1269. this._zAxis.normalize();
  1270. // X axis
  1271. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  1272. this._xAxis.normalize();
  1273. // Y axis
  1274. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  1275. this._yAxis.normalize();
  1276. // Eye angles
  1277. var ex = -Vector3.Dot(this._xAxis, eye);
  1278. var ey = -Vector3.Dot(this._yAxis, eye);
  1279. var ez = -Vector3.Dot(this._zAxis, eye);
  1280. return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0,
  1281. this._xAxis.y, this._yAxis.y, this._zAxis.y, 0,
  1282. this._xAxis.z, this._yAxis.z, this._zAxis.z, 0,
  1283. ex, ey, ez, 1, result);
  1284. }
  1285. public static OrthoLH(width: number, height: number, znear: number, zfar: number): Matrix {
  1286. var hw = 2.0 / width;
  1287. var hh = 2.0 / height;
  1288. var id = 1.0 / (zfar - znear);
  1289. var nid = znear / (znear - zfar);
  1290. return Matrix.FromValues(hw, 0, 0, 0,
  1291. 0, hh, 0, 0,
  1292. 0, 0, id, 0,
  1293. 0, 0, nid, 1);
  1294. }
  1295. public static OrthoOffCenterLH(left: number, right: number, bottom: number, top: number, znear: number, zfar: number): Matrix {
  1296. var matrix = Matrix.Zero();
  1297. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  1298. return matrix;
  1299. }
  1300. public static OrthoOffCenterLHToRef(left: number, right, bottom: number, top: number, znear: number, zfar: number, result: Matrix): void {
  1301. result.m[0] = 2.0 / (right - left);
  1302. result.m[1] = result.m[2] = result.m[3] = 0;
  1303. result.m[5] = 2.0 / (top - bottom);
  1304. result.m[4] = result.m[6] = result.m[7] = 0;
  1305. result.m[10] = -1.0 / (znear - zfar);
  1306. result.m[8] = result.m[9] = result.m[11] = 0;
  1307. result.m[12] = (left + right) / (left - right);
  1308. result.m[13] = (top + bottom) / (bottom - top);
  1309. result.m[14] = znear / (znear - zfar);
  1310. result.m[15] = 1.0;
  1311. }
  1312. public static PerspectiveLH(width: number, height: number, znear: number, zfar: number): Matrix {
  1313. var matrix = Matrix.Zero();
  1314. matrix.m[0] = (2.0 * znear) / width;
  1315. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  1316. matrix.m[5] = (2.0 * znear) / height;
  1317. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  1318. matrix.m[10] = -zfar / (znear - zfar);
  1319. matrix.m[8] = matrix.m[9] = 0.0;
  1320. matrix.m[11] = 1.0;
  1321. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  1322. matrix.m[14] = (znear * zfar) / (znear - zfar);
  1323. return matrix;
  1324. }
  1325. public static PerspectiveFovLH(fov: number, aspect: number, znear: number, zfar: number): Matrix {
  1326. var matrix = Matrix.Zero();
  1327. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  1328. return matrix;
  1329. }
  1330. public static PerspectiveFovLHToRef(fov: number, aspect: number, znear: number, zfar: number, result: Matrix): void {
  1331. var tan = 1.0 / (Math.tan(fov * 0.5));
  1332. result.m[0] = tan / aspect;
  1333. result.m[1] = result.m[2] = result.m[3] = 0.0;
  1334. result.m[5] = tan;
  1335. result.m[4] = result.m[6] = result.m[7] = 0.0;
  1336. result.m[8] = result.m[9] = 0.0;
  1337. result.m[10] = -zfar / (znear - zfar);
  1338. result.m[11] = 1.0;
  1339. result.m[12] = result.m[13] = result.m[15] = 0.0;
  1340. result.m[14] = (znear * zfar) / (znear - zfar);
  1341. }
  1342. public static GetFinalMatrix(viewport: Viewport, world: Matrix, view: Matrix, projection: Matrix, zmin: number, zmax: number): Matrix {
  1343. var cw = viewport.width;
  1344. var ch = viewport.height;
  1345. var cx = viewport.x;
  1346. var cy = viewport.y;
  1347. var viewportMatrix = Matrix.FromValues(cw / 2.0, 0, 0, 0,
  1348. 0, -ch / 2.0, 0, 0,
  1349. 0, 0, zmax - zmin, 0,
  1350. cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  1351. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  1352. }
  1353. public static Transpose(matrix: Matrix): Matrix {
  1354. var result = new Matrix();
  1355. result.m[0] = matrix.m[0];
  1356. result.m[1] = matrix.m[4];
  1357. result.m[2] = matrix.m[8];
  1358. result.m[3] = matrix.m[12];
  1359. result.m[4] = matrix.m[1];
  1360. result.m[5] = matrix.m[5];
  1361. result.m[6] = matrix.m[9];
  1362. result.m[7] = matrix.m[13];
  1363. result.m[8] = matrix.m[2];
  1364. result.m[9] = matrix.m[6];
  1365. result.m[10] = matrix.m[10];
  1366. result.m[11] = matrix.m[14];
  1367. result.m[12] = matrix.m[3];
  1368. result.m[13] = matrix.m[7];
  1369. result.m[14] = matrix.m[11];
  1370. result.m[15] = matrix.m[15];
  1371. return result;
  1372. }
  1373. public static Reflection(plane: Plane): Matrix {
  1374. var matrix = new Matrix();
  1375. Matrix.ReflectionToRef(plane, matrix);
  1376. return matrix;
  1377. }
  1378. public static ReflectionToRef(plane: Plane, result: Matrix): void {
  1379. plane.normalize();
  1380. var x = plane.normal.x;
  1381. var y = plane.normal.y;
  1382. var z = plane.normal.z;
  1383. var temp = -2 * x;
  1384. var temp2 = -2 * y;
  1385. var temp3 = -2 * z;
  1386. result.m[0] = (temp * x) + 1;
  1387. result.m[1] = temp2 * x;
  1388. result.m[2] = temp3 * x;
  1389. result.m[3] = 0.0;
  1390. result.m[4] = temp * y;
  1391. result.m[5] = (temp2 * y) + 1;
  1392. result.m[6] = temp3 * y;
  1393. result.m[7] = 0.0;
  1394. result.m[8] = temp * z;
  1395. result.m[9] = temp2 * z;
  1396. result.m[10] = (temp3 * z) + 1;
  1397. result.m[11] = 0.0;
  1398. result.m[12] = temp * plane.d;
  1399. result.m[13] = temp2 * plane.d;
  1400. result.m[14] = temp3 * plane.d;
  1401. result.m[15] = 1.0;
  1402. }
  1403. }
  1404. export class Plane {
  1405. public normal: Vector3;
  1406. public d: number;
  1407. constructor(a: number, b: number, c: number, d: number) {
  1408. this.normal = new Vector3(a, b, c);
  1409. this.d = d;
  1410. }
  1411. public asArray(): number[] {
  1412. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  1413. }
  1414. // Methods
  1415. public clone(): Plane {
  1416. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  1417. }
  1418. public normalize(): void {
  1419. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  1420. var magnitude = 0;
  1421. if (norm != 0) {
  1422. magnitude = 1.0 / norm;
  1423. }
  1424. this.normal.x *= magnitude;
  1425. this.normal.y *= magnitude;
  1426. this.normal.z *= magnitude;
  1427. this.d *= magnitude;
  1428. }
  1429. public transform(transformation: Matrix): Plane {
  1430. var transposedMatrix = BABYLON.Matrix.Transpose(transformation);
  1431. var x = this.normal.x;
  1432. var y = this.normal.y;
  1433. var z = this.normal.z;
  1434. var d = this.d;
  1435. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  1436. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  1437. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  1438. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  1439. return new BABYLON.Plane(normalX, normalY, normalZ, finalD);
  1440. }
  1441. public dotCoordinate(point): number {
  1442. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  1443. }
  1444. public copyFromPoints(point1: Vector3, point2: Vector3, point3: Vector3): void {
  1445. var x1 = point2.x - point1.x;
  1446. var y1 = point2.y - point1.y;
  1447. var z1 = point2.z - point1.z;
  1448. var x2 = point3.x - point1.x;
  1449. var y2 = point3.y - point1.y;
  1450. var z2 = point3.z - point1.z;
  1451. var yz = (y1 * z2) - (z1 * y2);
  1452. var xz = (z1 * x2) - (x1 * z2);
  1453. var xy = (x1 * y2) - (y1 * x2);
  1454. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  1455. var invPyth;
  1456. if (pyth != 0) {
  1457. invPyth = 1.0 / pyth;
  1458. }
  1459. else {
  1460. invPyth = 0;
  1461. }
  1462. this.normal.x = yz * invPyth;
  1463. this.normal.y = xz * invPyth;
  1464. this.normal.z = xy * invPyth;
  1465. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  1466. }
  1467. public isFrontFacingTo(direction: Vector3, epsilon: number): boolean {
  1468. var dot = Vector3.Dot(this.normal, direction);
  1469. return (dot <= epsilon);
  1470. }
  1471. public signedDistanceTo(point: Vector3): number {
  1472. return Vector3.Dot(point, this.normal) + this.d;
  1473. }
  1474. // Statics
  1475. static FromArray(array: number[]): Plane {
  1476. return new BABYLON.Plane(array[0], array[1], array[2], array[3]);
  1477. }
  1478. static FromPoints(point1, point2, point3): Plane {
  1479. var result = new BABYLON.Plane(0, 0, 0, 0);
  1480. result.copyFromPoints(point1, point2, point3);
  1481. return result;
  1482. }
  1483. static FromPositionAndNormal(origin: Vector3, normal: Vector3): Plane {
  1484. var result = new BABYLON.Plane(0, 0, 0, 0);
  1485. normal.normalize();
  1486. result.normal = normal;
  1487. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1488. return result;
  1489. }
  1490. static SignedDistanceToPlaneFromPositionAndNormal(origin: Vector3, normal: Vector3, point: Vector3): number {
  1491. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  1492. return Vector3.Dot(point, normal) + d;
  1493. }
  1494. }
  1495. export class Viewport {
  1496. constructor(public x: number, public y: number, public width: number, public height: number) {
  1497. }
  1498. public toGlobal(engine) {
  1499. var width = engine.getRenderWidth();
  1500. var height = engine.getRenderHeight();
  1501. return new Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  1502. }
  1503. }
  1504. export class Frustum {
  1505. public static GetPlanes(transform: Matrix): Plane[] {
  1506. var frustumPlanes = [];
  1507. for (var index = 0; index < 6; index++) {
  1508. frustumPlanes.push(new Plane(0, 0, 0, 0));
  1509. }
  1510. Frustum.GetPlanesToRef(transform, frustumPlanes);
  1511. return frustumPlanes;
  1512. }
  1513. public static GetPlanesToRef(transform: Matrix, frustumPlanes: Plane[]): void {
  1514. // Near
  1515. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  1516. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  1517. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  1518. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  1519. frustumPlanes[0].normalize();
  1520. // Far
  1521. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  1522. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  1523. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  1524. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  1525. frustumPlanes[1].normalize();
  1526. // Left
  1527. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  1528. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  1529. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  1530. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  1531. frustumPlanes[2].normalize();
  1532. // Right
  1533. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  1534. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  1535. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  1536. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  1537. frustumPlanes[3].normalize();
  1538. // Top
  1539. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  1540. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  1541. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  1542. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  1543. frustumPlanes[4].normalize();
  1544. // Bottom
  1545. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  1546. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  1547. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  1548. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  1549. frustumPlanes[5].normalize();
  1550. }
  1551. }
  1552. export class Ray {
  1553. private _edge1: Vector3;
  1554. private _edge2: Vector3;
  1555. private _pvec: Vector3;
  1556. private _tvec: Vector3;
  1557. private _qvec: Vector3;
  1558. constructor(public origin: Vector3, public direction: Vector3) {
  1559. }
  1560. // Methods
  1561. public intersectsBoxMinMax(minimum: Vector3, maximum: Vector3): boolean {
  1562. var d = 0.0;
  1563. var maxValue = Number.MAX_VALUE;
  1564. if (Math.abs(this.direction.x) < 0.0000001) {
  1565. if (this.origin.x < minimum.x || this.origin.x > maximum.x) {
  1566. return false;
  1567. }
  1568. }
  1569. else {
  1570. var inv = 1.0 / this.direction.x;
  1571. var min = (minimum.x - this.origin.x) * inv;
  1572. var max = (maximum.x - this.origin.x) * inv;
  1573. if (min > max) {
  1574. var temp = min;
  1575. min = max;
  1576. max = temp;
  1577. }
  1578. d = Math.max(min, d);
  1579. maxValue = Math.min(max, maxValue);
  1580. if (d > maxValue) {
  1581. return false;
  1582. }
  1583. }
  1584. if (Math.abs(this.direction.y) < 0.0000001) {
  1585. if (this.origin.y < minimum.y || this.origin.y > maximum.y) {
  1586. return false;
  1587. }
  1588. }
  1589. else {
  1590. inv = 1.0 / this.direction.y;
  1591. min = (minimum.y - this.origin.y) * inv;
  1592. max = (maximum.y - this.origin.y) * inv;
  1593. if (min > max) {
  1594. temp = min;
  1595. min = max;
  1596. max = temp;
  1597. }
  1598. d = Math.max(min, d);
  1599. maxValue = Math.min(max, maxValue);
  1600. if (d > maxValue) {
  1601. return false;
  1602. }
  1603. }
  1604. if (Math.abs(this.direction.z) < 0.0000001) {
  1605. if (this.origin.z < minimum.z || this.origin.z > maximum.z) {
  1606. return false;
  1607. }
  1608. }
  1609. else {
  1610. inv = 1.0 / this.direction.z;
  1611. min = (minimum.z - this.origin.z) * inv;
  1612. max = (maximum.z - this.origin.z) * inv;
  1613. if (min > max) {
  1614. temp = min;
  1615. min = max;
  1616. max = temp;
  1617. }
  1618. d = Math.max(min, d);
  1619. maxValue = Math.min(max, maxValue);
  1620. if (d > maxValue) {
  1621. return false;
  1622. }
  1623. }
  1624. return true;
  1625. }
  1626. public intersectsBox(box: BoundingBox): boolean {
  1627. return this.intersectsBoxMinMax(box.minimum, box.maximum);
  1628. }
  1629. public intersectsSphere(sphere): boolean {
  1630. var x = sphere.center.x - this.origin.x;
  1631. var y = sphere.center.y - this.origin.y;
  1632. var z = sphere.center.z - this.origin.z;
  1633. var pyth = (x * x) + (y * y) + (z * z);
  1634. var rr = sphere.radius * sphere.radius;
  1635. if (pyth <= rr) {
  1636. return true;
  1637. }
  1638. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  1639. if (dot < 0.0) {
  1640. return false;
  1641. }
  1642. var temp = pyth - (dot * dot);
  1643. return temp <= rr;
  1644. }
  1645. public intersectsTriangle(vertex0: Vector3, vertex1: Vector3, vertex2: Vector3): IntersectionInfo {
  1646. if (!this._edge1) {
  1647. this._edge1 = BABYLON.Vector3.Zero();
  1648. this._edge2 = BABYLON.Vector3.Zero();
  1649. this._pvec = BABYLON.Vector3.Zero();
  1650. this._tvec = BABYLON.Vector3.Zero();
  1651. this._qvec = BABYLON.Vector3.Zero();
  1652. }
  1653. vertex1.subtractToRef(vertex0, this._edge1);
  1654. vertex2.subtractToRef(vertex0, this._edge2);
  1655. BABYLON.Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  1656. var det = Vector3.Dot(this._edge1, this._pvec);
  1657. if (det === 0) {
  1658. return null;
  1659. }
  1660. var invdet = 1 / det;
  1661. this.origin.subtractToRef(vertex0, this._tvec);
  1662. var bu = Vector3.Dot(this._tvec, this._pvec) * invdet;
  1663. if (bu < 0 || bu > 1.0) {
  1664. return null;
  1665. }
  1666. Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  1667. var bv = Vector3.Dot(this.direction, this._qvec) * invdet;
  1668. if (bv < 0 || bu + bv > 1.0) {
  1669. return null;
  1670. }
  1671. return new IntersectionInfo(bu, bv, Vector3.Dot(this._edge2, this._qvec) * invdet);
  1672. }
  1673. // Statics
  1674. public static CreateNew(x: number, y: number, viewportWidth: number, viewportHeight: number, world: Matrix, view: Matrix, projection: Matrix): Ray {
  1675. var start = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  1676. var end = BABYLON.Vector3.Unproject(new BABYLON.Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  1677. var direction = end.subtract(start);
  1678. direction.normalize();
  1679. return new Ray(start, direction);
  1680. }
  1681. public static Transform(ray: Ray, matrix: Matrix): Ray {
  1682. var newOrigin = BABYLON.Vector3.TransformCoordinates(ray.origin, matrix);
  1683. var newDirection = BABYLON.Vector3.TransformNormal(ray.direction, matrix);
  1684. return new Ray(newOrigin, newDirection);
  1685. }
  1686. }
  1687. export enum Space {
  1688. LOCAL = 0,
  1689. WORLD = 1
  1690. }
  1691. export class Axis {
  1692. public static X: Vector3 = new BABYLON.Vector3(1, 0, 0);
  1693. public static Y: Vector3 = new BABYLON.Vector3(0, 1, 0);
  1694. public static Z: Vector3 = new BABYLON.Vector3(0, 0, 1);
  1695. };
  1696. }