babylon.math.ts 91 KB

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