babylon.math.ts 101 KB

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