babylon.math.ts 72 KB

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