babylon.math.ts 70 KB

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