babylon.math.ts 71 KB

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