babylon.math.ts 74 KB

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