babylon.math.js 124 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782
  1. var BABYLON;
  2. (function (BABYLON) {
  3. var Color3 = (function () {
  4. function Color3(r, g, b) {
  5. if (r === void 0) { r = 0; }
  6. if (g === void 0) { g = 0; }
  7. if (b === void 0) { b = 0; }
  8. this.r = r;
  9. this.g = g;
  10. this.b = b;
  11. }
  12. Color3.prototype.toString = function () {
  13. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + "}";
  14. };
  15. // Operators
  16. Color3.prototype.toArray = function (array, index) {
  17. if (index === undefined) {
  18. index = 0;
  19. }
  20. array[index] = this.r;
  21. array[index + 1] = this.g;
  22. array[index + 2] = this.b;
  23. return this;
  24. };
  25. Color3.prototype.toColor4 = function (alpha) {
  26. if (alpha === void 0) { alpha = 1; }
  27. return new Color4(this.r, this.g, this.b, alpha);
  28. };
  29. Color3.prototype.asArray = function () {
  30. var result = [];
  31. this.toArray(result, 0);
  32. return result;
  33. };
  34. Color3.prototype.toLuminance = function () {
  35. return this.r * 0.3 + this.g * 0.59 + this.b * 0.11;
  36. };
  37. Color3.prototype.multiply = function (otherColor) {
  38. return new Color3(this.r * otherColor.r, this.g * otherColor.g, this.b * otherColor.b);
  39. };
  40. Color3.prototype.multiplyToRef = function (otherColor, result) {
  41. result.r = this.r * otherColor.r;
  42. result.g = this.g * otherColor.g;
  43. result.b = this.b * otherColor.b;
  44. return this;
  45. };
  46. Color3.prototype.equals = function (otherColor) {
  47. return otherColor && this.r === otherColor.r && this.g === otherColor.g && this.b === otherColor.b;
  48. };
  49. Color3.prototype.scale = function (scale) {
  50. return new Color3(this.r * scale, this.g * scale, this.b * scale);
  51. };
  52. Color3.prototype.scaleToRef = function (scale, result) {
  53. result.r = this.r * scale;
  54. result.g = this.g * scale;
  55. result.b = this.b * scale;
  56. return this;
  57. };
  58. Color3.prototype.add = function (otherColor) {
  59. return new Color3(this.r + otherColor.r, this.g + otherColor.g, this.b + otherColor.b);
  60. };
  61. Color3.prototype.addToRef = function (otherColor, result) {
  62. result.r = this.r + otherColor.r;
  63. result.g = this.g + otherColor.g;
  64. result.b = this.b + otherColor.b;
  65. return this;
  66. };
  67. Color3.prototype.subtract = function (otherColor) {
  68. return new Color3(this.r - otherColor.r, this.g - otherColor.g, this.b - otherColor.b);
  69. };
  70. Color3.prototype.subtractToRef = function (otherColor, result) {
  71. result.r = this.r - otherColor.r;
  72. result.g = this.g - otherColor.g;
  73. result.b = this.b - otherColor.b;
  74. return this;
  75. };
  76. Color3.prototype.clone = function () {
  77. return new Color3(this.r, this.g, this.b);
  78. };
  79. Color3.prototype.copyFrom = function (source) {
  80. this.r = source.r;
  81. this.g = source.g;
  82. this.b = source.b;
  83. return this;
  84. };
  85. Color3.prototype.copyFromFloats = function (r, g, b) {
  86. this.r = r;
  87. this.g = g;
  88. this.b = b;
  89. return this;
  90. };
  91. // Statics
  92. Color3.FromArray = function (array, offset) {
  93. if (offset === void 0) { offset = 0; }
  94. return new Color3(array[offset], array[offset + 1], array[offset + 2]);
  95. };
  96. Color3.FromInts = function (r, g, b) {
  97. return new Color3(r / 255.0, g / 255.0, b / 255.0);
  98. };
  99. Color3.Lerp = function (start, end, amount) {
  100. var r = start.r + ((end.r - start.r) * amount);
  101. var g = start.g + ((end.g - start.g) * amount);
  102. var b = start.b + ((end.b - start.b) * amount);
  103. return new Color3(r, g, b);
  104. };
  105. Color3.Red = function () {
  106. return new Color3(1, 0, 0);
  107. };
  108. Color3.Green = function () {
  109. return new Color3(0, 1, 0);
  110. };
  111. Color3.Blue = function () {
  112. return new Color3(0, 0, 1);
  113. };
  114. Color3.Black = function () {
  115. return new Color3(0, 0, 0);
  116. };
  117. Color3.White = function () {
  118. return new Color3(1, 1, 1);
  119. };
  120. Color3.Purple = function () {
  121. return new Color3(0.5, 0, 0.5);
  122. };
  123. Color3.Magenta = function () {
  124. return new Color3(1, 0, 1);
  125. };
  126. Color3.Yellow = function () {
  127. return new Color3(1, 1, 0);
  128. };
  129. Color3.Gray = function () {
  130. return new Color3(0.5, 0.5, 0.5);
  131. };
  132. return Color3;
  133. })();
  134. BABYLON.Color3 = Color3;
  135. var Color4 = (function () {
  136. function Color4(r, g, b, a) {
  137. this.r = r;
  138. this.g = g;
  139. this.b = b;
  140. this.a = a;
  141. }
  142. // Operators
  143. Color4.prototype.addInPlace = function (right) {
  144. this.r += right.r;
  145. this.g += right.g;
  146. this.b += right.b;
  147. this.a += right.a;
  148. return this;
  149. };
  150. Color4.prototype.asArray = function () {
  151. var result = [];
  152. this.toArray(result, 0);
  153. return result;
  154. };
  155. Color4.prototype.toArray = function (array, index) {
  156. if (index === undefined) {
  157. index = 0;
  158. }
  159. array[index] = this.r;
  160. array[index + 1] = this.g;
  161. array[index + 2] = this.b;
  162. array[index + 3] = this.a;
  163. return this;
  164. };
  165. Color4.prototype.add = function (right) {
  166. return new Color4(this.r + right.r, this.g + right.g, this.b + right.b, this.a + right.a);
  167. };
  168. Color4.prototype.subtract = function (right) {
  169. return new Color4(this.r - right.r, this.g - right.g, this.b - right.b, this.a - right.a);
  170. };
  171. Color4.prototype.subtractToRef = function (right, result) {
  172. result.r = this.r - right.r;
  173. result.g = this.g - right.g;
  174. result.b = this.b - right.b;
  175. result.a = this.a - right.a;
  176. return this;
  177. };
  178. Color4.prototype.scale = function (scale) {
  179. return new Color4(this.r * scale, this.g * scale, this.b * scale, this.a * scale);
  180. };
  181. Color4.prototype.scaleToRef = function (scale, result) {
  182. result.r = this.r * scale;
  183. result.g = this.g * scale;
  184. result.b = this.b * scale;
  185. result.a = this.a * scale;
  186. return this;
  187. };
  188. Color4.prototype.toString = function () {
  189. return "{R: " + this.r + " G:" + this.g + " B:" + this.b + " A:" + this.a + "}";
  190. };
  191. Color4.prototype.clone = function () {
  192. return new Color4(this.r, this.g, this.b, this.a);
  193. };
  194. Color4.prototype.copyFrom = function (source) {
  195. this.r = source.r;
  196. this.g = source.g;
  197. this.b = source.b;
  198. this.a = source.a;
  199. return this;
  200. };
  201. // Statics
  202. Color4.Lerp = function (left, right, amount) {
  203. var result = new Color4(0, 0, 0, 0);
  204. Color4.LerpToRef(left, right, amount, result);
  205. return result;
  206. };
  207. Color4.LerpToRef = function (left, right, amount, result) {
  208. result.r = left.r + (right.r - left.r) * amount;
  209. result.g = left.g + (right.g - left.g) * amount;
  210. result.b = left.b + (right.b - left.b) * amount;
  211. result.a = left.a + (right.a - left.a) * amount;
  212. };
  213. Color4.FromArray = function (array, offset) {
  214. if (offset === void 0) { offset = 0; }
  215. return new Color4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  216. };
  217. Color4.FromInts = function (r, g, b, a) {
  218. return new Color4(r / 255.0, g / 255.0, b / 255.0, a / 255.0);
  219. };
  220. return Color4;
  221. })();
  222. BABYLON.Color4 = Color4;
  223. var Vector2 = (function () {
  224. function Vector2(x, y) {
  225. this.x = x;
  226. this.y = y;
  227. }
  228. Vector2.prototype.toString = function () {
  229. return "{X: " + this.x + " Y:" + this.y + "}";
  230. };
  231. // Operators
  232. Vector2.prototype.toArray = function (array, index) {
  233. if (index === void 0) { index = 0; }
  234. array[index] = this.x;
  235. array[index + 1] = this.y;
  236. return this;
  237. };
  238. Vector2.prototype.asArray = function () {
  239. var result = [];
  240. this.toArray(result, 0);
  241. return result;
  242. };
  243. Vector2.prototype.copyFrom = function (source) {
  244. this.x = source.x;
  245. this.y = source.y;
  246. return this;
  247. };
  248. Vector2.prototype.copyFromFloats = function (x, y) {
  249. this.x = x;
  250. this.y = y;
  251. return this;
  252. };
  253. Vector2.prototype.add = function (otherVector) {
  254. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  255. };
  256. Vector2.prototype.addVector3 = function (otherVector) {
  257. return new Vector2(this.x + otherVector.x, this.y + otherVector.y);
  258. };
  259. Vector2.prototype.subtract = function (otherVector) {
  260. return new Vector2(this.x - otherVector.x, this.y - otherVector.y);
  261. };
  262. Vector2.prototype.subtractInPlace = function (otherVector) {
  263. this.x -= otherVector.x;
  264. this.y -= otherVector.y;
  265. return this;
  266. };
  267. Vector2.prototype.multiplyInPlace = function (otherVector) {
  268. this.x *= otherVector.x;
  269. this.y *= otherVector.y;
  270. return this;
  271. };
  272. Vector2.prototype.multiply = function (otherVector) {
  273. return new Vector2(this.x * otherVector.x, this.y * otherVector.y);
  274. };
  275. Vector2.prototype.multiplyToRef = function (otherVector, result) {
  276. result.x = this.x * otherVector.x;
  277. result.y = this.y * otherVector.y;
  278. return this;
  279. };
  280. Vector2.prototype.multiplyByFloats = function (x, y) {
  281. return new Vector2(this.x * x, this.y * y);
  282. };
  283. Vector2.prototype.divide = function (otherVector) {
  284. return new Vector2(this.x / otherVector.x, this.y / otherVector.y);
  285. };
  286. Vector2.prototype.divideToRef = function (otherVector, result) {
  287. result.x = this.x / otherVector.x;
  288. result.y = this.y / otherVector.y;
  289. return this;
  290. };
  291. Vector2.prototype.negate = function () {
  292. return new Vector2(-this.x, -this.y);
  293. };
  294. Vector2.prototype.scaleInPlace = function (scale) {
  295. this.x *= scale;
  296. this.y *= scale;
  297. return this;
  298. };
  299. Vector2.prototype.scale = function (scale) {
  300. return new Vector2(this.x * scale, this.y * scale);
  301. };
  302. Vector2.prototype.equals = function (otherVector) {
  303. return otherVector && this.x === otherVector.x && this.y === otherVector.y;
  304. };
  305. // Properties
  306. Vector2.prototype.length = function () {
  307. return Math.sqrt(this.x * this.x + this.y * this.y);
  308. };
  309. Vector2.prototype.lengthSquared = function () {
  310. return (this.x * this.x + this.y * this.y);
  311. };
  312. // Methods
  313. Vector2.prototype.normalize = function () {
  314. var len = this.length();
  315. if (len === 0)
  316. return this;
  317. var num = 1.0 / len;
  318. this.x *= num;
  319. this.y *= num;
  320. return this;
  321. };
  322. Vector2.prototype.clone = function () {
  323. return new Vector2(this.x, this.y);
  324. };
  325. // Statics
  326. Vector2.Zero = function () {
  327. return new Vector2(0, 0);
  328. };
  329. Vector2.FromArray = function (array, offset) {
  330. if (offset === void 0) { offset = 0; }
  331. return new Vector2(array[offset], array[offset + 1]);
  332. };
  333. Vector2.FromArrayToRef = function (array, offset, result) {
  334. result.x = array[offset];
  335. result.y = array[offset + 1];
  336. };
  337. Vector2.CatmullRom = function (value1, value2, value3, value4, amount) {
  338. var squared = amount * amount;
  339. var cubed = amount * squared;
  340. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) + (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) + ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  341. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) + (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) + ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  342. return new Vector2(x, y);
  343. };
  344. Vector2.Clamp = function (value, min, max) {
  345. var x = value.x;
  346. x = (x > max.x) ? max.x : x;
  347. x = (x < min.x) ? min.x : x;
  348. var y = value.y;
  349. y = (y > max.y) ? max.y : y;
  350. y = (y < min.y) ? min.y : y;
  351. return new Vector2(x, y);
  352. };
  353. Vector2.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  354. var squared = amount * amount;
  355. var cubed = amount * squared;
  356. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  357. var part2 = (-2.0 * cubed) + (3.0 * squared);
  358. var part3 = (cubed - (2.0 * squared)) + amount;
  359. var part4 = cubed - squared;
  360. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  361. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  362. return new Vector2(x, y);
  363. };
  364. Vector2.Lerp = function (start, end, amount) {
  365. var x = start.x + ((end.x - start.x) * amount);
  366. var y = start.y + ((end.y - start.y) * amount);
  367. return new Vector2(x, y);
  368. };
  369. Vector2.Dot = function (left, right) {
  370. return left.x * right.x + left.y * right.y;
  371. };
  372. Vector2.Normalize = function (vector) {
  373. var newVector = vector.clone();
  374. newVector.normalize();
  375. return newVector;
  376. };
  377. Vector2.Minimize = function (left, right) {
  378. var x = (left.x < right.x) ? left.x : right.x;
  379. var y = (left.y < right.y) ? left.y : right.y;
  380. return new Vector2(x, y);
  381. };
  382. Vector2.Maximize = function (left, right) {
  383. var x = (left.x > right.x) ? left.x : right.x;
  384. var y = (left.y > right.y) ? left.y : right.y;
  385. return new Vector2(x, y);
  386. };
  387. Vector2.Transform = function (vector, transformation) {
  388. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]);
  389. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]);
  390. return new Vector2(x, y);
  391. };
  392. Vector2.Distance = function (value1, value2) {
  393. return Math.sqrt(Vector2.DistanceSquared(value1, value2));
  394. };
  395. Vector2.DistanceSquared = function (value1, value2) {
  396. var x = value1.x - value2.x;
  397. var y = value1.y - value2.y;
  398. return (x * x) + (y * y);
  399. };
  400. return Vector2;
  401. })();
  402. BABYLON.Vector2 = Vector2;
  403. var Vector3 = (function () {
  404. function Vector3(x, y, z) {
  405. this.x = x;
  406. this.y = y;
  407. this.z = z;
  408. }
  409. Vector3.prototype.toString = function () {
  410. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "}";
  411. };
  412. // Operators
  413. Vector3.prototype.asArray = function () {
  414. var result = [];
  415. this.toArray(result, 0);
  416. return result;
  417. };
  418. Vector3.prototype.toArray = function (array, index) {
  419. if (index === void 0) { index = 0; }
  420. array[index] = this.x;
  421. array[index + 1] = this.y;
  422. array[index + 2] = this.z;
  423. return this;
  424. };
  425. Vector3.prototype.toQuaternion = function () {
  426. var result = new Quaternion(0, 0, 0, 1);
  427. var cosxPlusz = Math.cos((this.x + this.z) * 0.5);
  428. var sinxPlusz = Math.sin((this.x + this.z) * 0.5);
  429. var coszMinusx = Math.cos((this.z - this.x) * 0.5);
  430. var sinzMinusx = Math.sin((this.z - this.x) * 0.5);
  431. var cosy = Math.cos(this.y * 0.5);
  432. var siny = Math.sin(this.y * 0.5);
  433. result.x = coszMinusx * siny;
  434. result.y = -sinzMinusx * siny;
  435. result.z = sinxPlusz * cosy;
  436. result.w = cosxPlusz * cosy;
  437. return result;
  438. };
  439. Vector3.prototype.addInPlace = function (otherVector) {
  440. this.x += otherVector.x;
  441. this.y += otherVector.y;
  442. this.z += otherVector.z;
  443. return this;
  444. };
  445. Vector3.prototype.add = function (otherVector) {
  446. return new Vector3(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z);
  447. };
  448. Vector3.prototype.addToRef = function (otherVector, result) {
  449. result.x = this.x + otherVector.x;
  450. result.y = this.y + otherVector.y;
  451. result.z = this.z + otherVector.z;
  452. return this;
  453. };
  454. Vector3.prototype.subtractInPlace = function (otherVector) {
  455. this.x -= otherVector.x;
  456. this.y -= otherVector.y;
  457. this.z -= otherVector.z;
  458. return this;
  459. };
  460. Vector3.prototype.subtract = function (otherVector) {
  461. return new Vector3(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z);
  462. };
  463. Vector3.prototype.subtractToRef = function (otherVector, result) {
  464. result.x = this.x - otherVector.x;
  465. result.y = this.y - otherVector.y;
  466. result.z = this.z - otherVector.z;
  467. return this;
  468. };
  469. Vector3.prototype.subtractFromFloats = function (x, y, z) {
  470. return new Vector3(this.x - x, this.y - y, this.z - z);
  471. };
  472. Vector3.prototype.subtractFromFloatsToRef = function (x, y, z, result) {
  473. result.x = this.x - x;
  474. result.y = this.y - y;
  475. result.z = this.z - z;
  476. return this;
  477. };
  478. Vector3.prototype.negate = function () {
  479. return new Vector3(-this.x, -this.y, -this.z);
  480. };
  481. Vector3.prototype.scaleInPlace = function (scale) {
  482. this.x *= scale;
  483. this.y *= scale;
  484. this.z *= scale;
  485. return this;
  486. };
  487. Vector3.prototype.scale = function (scale) {
  488. return new Vector3(this.x * scale, this.y * scale, this.z * scale);
  489. };
  490. Vector3.prototype.scaleToRef = function (scale, result) {
  491. result.x = this.x * scale;
  492. result.y = this.y * scale;
  493. result.z = this.z * scale;
  494. };
  495. Vector3.prototype.equals = function (otherVector) {
  496. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z;
  497. };
  498. Vector3.prototype.equalsWithEpsilon = function (otherVector) {
  499. return Math.abs(this.x - otherVector.x) < BABYLON.Engine.Epsilon && Math.abs(this.y - otherVector.y) < BABYLON.Engine.Epsilon && Math.abs(this.z - otherVector.z) < BABYLON.Engine.Epsilon;
  500. };
  501. Vector3.prototype.equalsToFloats = function (x, y, z) {
  502. return this.x === x && this.y === y && this.z === z;
  503. };
  504. Vector3.prototype.multiplyInPlace = function (otherVector) {
  505. this.x *= otherVector.x;
  506. this.y *= otherVector.y;
  507. this.z *= otherVector.z;
  508. return this;
  509. };
  510. Vector3.prototype.multiply = function (otherVector) {
  511. return new Vector3(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z);
  512. };
  513. Vector3.prototype.multiplyToRef = function (otherVector, result) {
  514. result.x = this.x * otherVector.x;
  515. result.y = this.y * otherVector.y;
  516. result.z = this.z * otherVector.z;
  517. return this;
  518. };
  519. Vector3.prototype.multiplyByFloats = function (x, y, z) {
  520. return new Vector3(this.x * x, this.y * y, this.z * z);
  521. };
  522. Vector3.prototype.divide = function (otherVector) {
  523. return new Vector3(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z);
  524. };
  525. Vector3.prototype.divideToRef = function (otherVector, result) {
  526. result.x = this.x / otherVector.x;
  527. result.y = this.y / otherVector.y;
  528. result.z = this.z / otherVector.z;
  529. return this;
  530. };
  531. Vector3.prototype.MinimizeInPlace = function (other) {
  532. if (other.x < this.x)
  533. this.x = other.x;
  534. if (other.y < this.y)
  535. this.y = other.y;
  536. if (other.z < this.z)
  537. this.z = other.z;
  538. return this;
  539. };
  540. Vector3.prototype.MaximizeInPlace = function (other) {
  541. if (other.x > this.x)
  542. this.x = other.x;
  543. if (other.y > this.y)
  544. this.y = other.y;
  545. if (other.z > this.z)
  546. this.z = other.z;
  547. return this;
  548. };
  549. // Properties
  550. Vector3.prototype.length = function () {
  551. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z);
  552. };
  553. Vector3.prototype.lengthSquared = function () {
  554. return (this.x * this.x + this.y * this.y + this.z * this.z);
  555. };
  556. // Methods
  557. Vector3.prototype.normalize = function () {
  558. var len = this.length();
  559. if (len === 0)
  560. return this;
  561. var num = 1.0 / len;
  562. this.x *= num;
  563. this.y *= num;
  564. this.z *= num;
  565. return this;
  566. };
  567. Vector3.prototype.clone = function () {
  568. return new Vector3(this.x, this.y, this.z);
  569. };
  570. Vector3.prototype.copyFrom = function (source) {
  571. this.x = source.x;
  572. this.y = source.y;
  573. this.z = source.z;
  574. return this;
  575. };
  576. Vector3.prototype.copyFromFloats = function (x, y, z) {
  577. this.x = x;
  578. this.y = y;
  579. this.z = z;
  580. return this;
  581. };
  582. // Statics
  583. Vector3.FromArray = function (array, offset) {
  584. if (!offset) {
  585. offset = 0;
  586. }
  587. return new Vector3(array[offset], array[offset + 1], array[offset + 2]);
  588. };
  589. Vector3.FromArrayToRef = function (array, offset, result) {
  590. result.x = array[offset];
  591. result.y = array[offset + 1];
  592. result.z = array[offset + 2];
  593. };
  594. Vector3.FromFloatArrayToRef = function (array, offset, result) {
  595. result.x = array[offset];
  596. result.y = array[offset + 1];
  597. result.z = array[offset + 2];
  598. };
  599. Vector3.FromFloatsToRef = function (x, y, z, result) {
  600. result.x = x;
  601. result.y = y;
  602. result.z = z;
  603. };
  604. Vector3.Zero = function () {
  605. return new Vector3(0, 0, 0);
  606. };
  607. Vector3.Up = function () {
  608. return new Vector3(0, 1.0, 0);
  609. };
  610. Vector3.TransformCoordinates = function (vector, transformation) {
  611. var result = Vector3.Zero();
  612. Vector3.TransformCoordinatesToRef(vector, transformation, result);
  613. return result;
  614. };
  615. Vector3.TransformCoordinatesToRef = function (vector, transformation, result) {
  616. var x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]) + transformation.m[12];
  617. var y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]) + transformation.m[13];
  618. var z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]) + transformation.m[14];
  619. var w = (vector.x * transformation.m[3]) + (vector.y * transformation.m[7]) + (vector.z * transformation.m[11]) + transformation.m[15];
  620. result.x = x / w;
  621. result.y = y / w;
  622. result.z = z / w;
  623. };
  624. Vector3.TransformCoordinatesFromFloatsToRef = function (x, y, z, transformation, result) {
  625. var rx = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]) + transformation.m[12];
  626. var ry = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]) + transformation.m[13];
  627. var rz = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]) + transformation.m[14];
  628. var rw = (x * transformation.m[3]) + (y * transformation.m[7]) + (z * transformation.m[11]) + transformation.m[15];
  629. result.x = rx / rw;
  630. result.y = ry / rw;
  631. result.z = rz / rw;
  632. };
  633. Vector3.TransformCoordinatesToRefSIMD = function (vector, transformation, result) {
  634. var v = SIMD.float32x4.loadXYZ(vector._data, 0);
  635. var m0 = SIMD.float32x4.load(transformation.m, 0);
  636. var m1 = SIMD.float32x4.load(transformation.m, 4);
  637. var m2 = SIMD.float32x4.load(transformation.m, 8);
  638. var m3 = SIMD.float32x4.load(transformation.m, 12);
  639. var r = SIMD.float32x4.add(SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 0, 0, 0, 0), m0), SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 1, 1, 1, 1), m1)), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(v, 2, 2, 2, 2), m2), m3));
  640. r = SIMD.float32x4.div(r, SIMD.float32x4.swizzle(r, 3, 3, 3, 3));
  641. SIMD.float32x4.storeXYZ(result._data, 0, r);
  642. };
  643. Vector3.TransformCoordinatesFromFloatsToRefSIMD = function (x, y, z, transformation, result) {
  644. var v0 = SIMD.float32x4.splat(x);
  645. var v1 = SIMD.float32x4.splat(y);
  646. var v2 = SIMD.float32x4.splat(z);
  647. var m0 = SIMD.float32x4.load(transformation.m, 0);
  648. var m1 = SIMD.float32x4.load(transformation.m, 4);
  649. var m2 = SIMD.float32x4.load(transformation.m, 8);
  650. var m3 = SIMD.float32x4.load(transformation.m, 12);
  651. var r = SIMD.float32x4.add(SIMD.float32x4.add(SIMD.float32x4.mul(v0, m0), SIMD.float32x4.mul(v1, m1)), SIMD.float32x4.add(SIMD.float32x4.mul(v2, m2), m3));
  652. r = SIMD.float32x4.div(r, SIMD.float32x4.swizzle(r, 3, 3, 3, 3));
  653. SIMD.float32x4.storeXYZ(result._data, 0, r);
  654. };
  655. Vector3.TransformNormal = function (vector, transformation) {
  656. var result = Vector3.Zero();
  657. Vector3.TransformNormalToRef(vector, transformation, result);
  658. return result;
  659. };
  660. Vector3.TransformNormalToRef = function (vector, transformation, result) {
  661. result.x = (vector.x * transformation.m[0]) + (vector.y * transformation.m[4]) + (vector.z * transformation.m[8]);
  662. result.y = (vector.x * transformation.m[1]) + (vector.y * transformation.m[5]) + (vector.z * transformation.m[9]);
  663. result.z = (vector.x * transformation.m[2]) + (vector.y * transformation.m[6]) + (vector.z * transformation.m[10]);
  664. };
  665. Vector3.TransformNormalFromFloatsToRef = function (x, y, z, transformation, result) {
  666. result.x = (x * transformation.m[0]) + (y * transformation.m[4]) + (z * transformation.m[8]);
  667. result.y = (x * transformation.m[1]) + (y * transformation.m[5]) + (z * transformation.m[9]);
  668. result.z = (x * transformation.m[2]) + (y * transformation.m[6]) + (z * transformation.m[10]);
  669. };
  670. Vector3.CatmullRom = function (value1, value2, value3, value4, amount) {
  671. var squared = amount * amount;
  672. var cubed = amount * squared;
  673. var x = 0.5 * ((((2.0 * value2.x) + ((-value1.x + value3.x) * amount)) + (((((2.0 * value1.x) - (5.0 * value2.x)) + (4.0 * value3.x)) - value4.x) * squared)) + ((((-value1.x + (3.0 * value2.x)) - (3.0 * value3.x)) + value4.x) * cubed));
  674. var y = 0.5 * ((((2.0 * value2.y) + ((-value1.y + value3.y) * amount)) + (((((2.0 * value1.y) - (5.0 * value2.y)) + (4.0 * value3.y)) - value4.y) * squared)) + ((((-value1.y + (3.0 * value2.y)) - (3.0 * value3.y)) + value4.y) * cubed));
  675. var z = 0.5 * ((((2.0 * value2.z) + ((-value1.z + value3.z) * amount)) + (((((2.0 * value1.z) - (5.0 * value2.z)) + (4.0 * value3.z)) - value4.z) * squared)) + ((((-value1.z + (3.0 * value2.z)) - (3.0 * value3.z)) + value4.z) * cubed));
  676. return new Vector3(x, y, z);
  677. };
  678. Vector3.Clamp = function (value, min, max) {
  679. var x = value.x;
  680. x = (x > max.x) ? max.x : x;
  681. x = (x < min.x) ? min.x : x;
  682. var y = value.y;
  683. y = (y > max.y) ? max.y : y;
  684. y = (y < min.y) ? min.y : y;
  685. var z = value.z;
  686. z = (z > max.z) ? max.z : z;
  687. z = (z < min.z) ? min.z : z;
  688. return new Vector3(x, y, z);
  689. };
  690. Vector3.Hermite = function (value1, tangent1, value2, tangent2, amount) {
  691. var squared = amount * amount;
  692. var cubed = amount * squared;
  693. var part1 = ((2.0 * cubed) - (3.0 * squared)) + 1.0;
  694. var part2 = (-2.0 * cubed) + (3.0 * squared);
  695. var part3 = (cubed - (2.0 * squared)) + amount;
  696. var part4 = cubed - squared;
  697. var x = (((value1.x * part1) + (value2.x * part2)) + (tangent1.x * part3)) + (tangent2.x * part4);
  698. var y = (((value1.y * part1) + (value2.y * part2)) + (tangent1.y * part3)) + (tangent2.y * part4);
  699. var z = (((value1.z * part1) + (value2.z * part2)) + (tangent1.z * part3)) + (tangent2.z * part4);
  700. return new Vector3(x, y, z);
  701. };
  702. Vector3.Lerp = function (start, end, amount) {
  703. var x = start.x + ((end.x - start.x) * amount);
  704. var y = start.y + ((end.y - start.y) * amount);
  705. var z = start.z + ((end.z - start.z) * amount);
  706. return new Vector3(x, y, z);
  707. };
  708. Vector3.Dot = function (left, right) {
  709. return (left.x * right.x + left.y * right.y + left.z * right.z);
  710. };
  711. Vector3.Cross = function (left, right) {
  712. var result = Vector3.Zero();
  713. Vector3.CrossToRef(left, right, result);
  714. return result;
  715. };
  716. Vector3.CrossToRef = function (left, right, result) {
  717. result.x = left.y * right.z - left.z * right.y;
  718. result.y = left.z * right.x - left.x * right.z;
  719. result.z = left.x * right.y - left.y * right.x;
  720. };
  721. Vector3.Normalize = function (vector) {
  722. var result = Vector3.Zero();
  723. Vector3.NormalizeToRef(vector, result);
  724. return result;
  725. };
  726. Vector3.NormalizeToRef = function (vector, result) {
  727. result.copyFrom(vector);
  728. result.normalize();
  729. };
  730. Vector3.Project = function (vector, world, transform, viewport) {
  731. var cw = viewport.width;
  732. var ch = viewport.height;
  733. var cx = viewport.x;
  734. var cy = viewport.y;
  735. var viewportMatrix = Matrix.FromValues(cw / 2.0, 0, 0, 0, 0, -ch / 2.0, 0, 0, 0, 0, 1, 0, cx + cw / 2.0, ch / 2.0 + cy, 0, 1);
  736. var finalMatrix = world.multiply(transform).multiply(viewportMatrix);
  737. return Vector3.TransformCoordinates(vector, finalMatrix);
  738. };
  739. Vector3.UnprojectFromTransform = function (source, viewportWidth, viewportHeight, world, transform) {
  740. var matrix = world.multiply(transform);
  741. matrix.invert();
  742. source.x = source.x / viewportWidth * 2 - 1;
  743. source.y = -(source.y / viewportHeight * 2 - 1);
  744. var vector = Vector3.TransformCoordinates(source, matrix);
  745. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  746. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  747. vector = vector.scale(1.0 / num);
  748. }
  749. return vector;
  750. };
  751. Vector3.Unproject = function (source, viewportWidth, viewportHeight, world, view, projection) {
  752. var matrix = world.multiply(view).multiply(projection);
  753. matrix.invert();
  754. source.x = source.x / viewportWidth * 2 - 1;
  755. source.y = -(source.y / viewportHeight * 2 - 1);
  756. var vector = Vector3.TransformCoordinates(source, matrix);
  757. var num = source.x * matrix.m[3] + source.y * matrix.m[7] + source.z * matrix.m[11] + matrix.m[15];
  758. if (BABYLON.Tools.WithinEpsilon(num, 1.0)) {
  759. vector = vector.scale(1.0 / num);
  760. }
  761. return vector;
  762. };
  763. Vector3.Minimize = function (left, right) {
  764. var min = left.clone();
  765. min.MinimizeInPlace(right);
  766. return min;
  767. };
  768. Vector3.Maximize = function (left, right) {
  769. var max = left.clone();
  770. max.MaximizeInPlace(right);
  771. return max;
  772. };
  773. Vector3.Distance = function (value1, value2) {
  774. return Math.sqrt(Vector3.DistanceSquared(value1, value2));
  775. };
  776. Vector3.DistanceSquared = function (value1, value2) {
  777. var x = value1.x - value2.x;
  778. var y = value1.y - value2.y;
  779. var z = value1.z - value2.z;
  780. return (x * x) + (y * y) + (z * z);
  781. };
  782. Vector3.Center = function (value1, value2) {
  783. var center = value1.add(value2);
  784. center.scaleInPlace(0.5);
  785. return center;
  786. };
  787. return Vector3;
  788. })();
  789. BABYLON.Vector3 = Vector3;
  790. //Vector4 class created for EulerAngle class conversion to Quaternion
  791. var Vector4 = (function () {
  792. function Vector4(x, y, z, w) {
  793. this.x = x;
  794. this.y = y;
  795. this.z = z;
  796. this.w = w;
  797. }
  798. Vector4.prototype.toString = function () {
  799. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + "W:" + this.w + "}";
  800. };
  801. // Operators
  802. Vector4.prototype.asArray = function () {
  803. var result = [];
  804. this.toArray(result, 0);
  805. return result;
  806. };
  807. Vector4.prototype.toArray = function (array, index) {
  808. if (index === undefined) {
  809. index = 0;
  810. }
  811. array[index] = this.x;
  812. array[index + 1] = this.y;
  813. array[index + 2] = this.z;
  814. array[index + 3] = this.w;
  815. return this;
  816. };
  817. Vector4.prototype.addInPlace = function (otherVector) {
  818. this.x += otherVector.x;
  819. this.y += otherVector.y;
  820. this.z += otherVector.z;
  821. this.w += otherVector.w;
  822. return this;
  823. };
  824. Vector4.prototype.add = function (otherVector) {
  825. return new Vector4(this.x + otherVector.x, this.y + otherVector.y, this.z + otherVector.z, this.w + otherVector.w);
  826. };
  827. Vector4.prototype.addToRef = function (otherVector, result) {
  828. result.x = this.x + otherVector.x;
  829. result.y = this.y + otherVector.y;
  830. result.z = this.z + otherVector.z;
  831. result.w = this.w + otherVector.w;
  832. return this;
  833. };
  834. Vector4.prototype.subtractInPlace = function (otherVector) {
  835. this.x -= otherVector.x;
  836. this.y -= otherVector.y;
  837. this.z -= otherVector.z;
  838. this.w -= otherVector.w;
  839. return this;
  840. };
  841. Vector4.prototype.subtract = function (otherVector) {
  842. return new Vector4(this.x - otherVector.x, this.y - otherVector.y, this.z - otherVector.z, this.w - otherVector.w);
  843. };
  844. Vector4.prototype.subtractToRef = function (otherVector, result) {
  845. result.x = this.x - otherVector.x;
  846. result.y = this.y - otherVector.y;
  847. result.z = this.z - otherVector.z;
  848. result.w = this.w - otherVector.w;
  849. return this;
  850. };
  851. Vector4.prototype.subtractFromFloats = function (x, y, z, w) {
  852. return new Vector4(this.x - x, this.y - y, this.z - z, this.w - w);
  853. };
  854. Vector4.prototype.subtractFromFloatsToRef = function (x, y, z, w, result) {
  855. result.x = this.x - x;
  856. result.y = this.y - y;
  857. result.z = this.z - z;
  858. result.w = this.w - w;
  859. return this;
  860. };
  861. Vector4.prototype.negate = function () {
  862. return new Vector4(-this.x, -this.y, -this.z, -this.w);
  863. };
  864. Vector4.prototype.scaleInPlace = function (scale) {
  865. this.x *= scale;
  866. this.y *= scale;
  867. this.z *= scale;
  868. this.w *= scale;
  869. return this;
  870. };
  871. Vector4.prototype.scale = function (scale) {
  872. return new Vector4(this.x * scale, this.y * scale, this.z * scale, this.w * scale);
  873. };
  874. Vector4.prototype.scaleToRef = function (scale, result) {
  875. result.x = this.x * scale;
  876. result.y = this.y * scale;
  877. result.z = this.z * scale;
  878. result.w = this.w * scale;
  879. };
  880. Vector4.prototype.equals = function (otherVector) {
  881. return otherVector && this.x === otherVector.x && this.y === otherVector.y && this.z === otherVector.z && this.w === otherVector.w;
  882. };
  883. Vector4.prototype.equalsWithEpsilon = function (otherVector) {
  884. return Math.abs(this.x - otherVector.x) < BABYLON.Engine.Epsilon && Math.abs(this.y - otherVector.y) < BABYLON.Engine.Epsilon && Math.abs(this.z - otherVector.z) < BABYLON.Engine.Epsilon && Math.abs(this.w - otherVector.w) < BABYLON.Engine.Epsilon;
  885. };
  886. Vector4.prototype.equalsToFloats = function (x, y, z, w) {
  887. return this.x === x && this.y === y && this.z === z && this.w === w;
  888. };
  889. Vector4.prototype.multiplyInPlace = function (otherVector) {
  890. this.x *= otherVector.x;
  891. this.y *= otherVector.y;
  892. this.z *= otherVector.z;
  893. this.w *= otherVector.w;
  894. return this;
  895. };
  896. Vector4.prototype.multiply = function (otherVector) {
  897. return new Vector4(this.x * otherVector.x, this.y * otherVector.y, this.z * otherVector.z, this.w * otherVector.w);
  898. };
  899. Vector4.prototype.multiplyToRef = function (otherVector, result) {
  900. result.x = this.x * otherVector.x;
  901. result.y = this.y * otherVector.y;
  902. result.z = this.z * otherVector.z;
  903. result.w = this.w * otherVector.w;
  904. return this;
  905. };
  906. Vector4.prototype.multiplyByFloats = function (x, y, z, w) {
  907. return new Vector4(this.x * x, this.y * y, this.z * z, this.w * w);
  908. };
  909. Vector4.prototype.divide = function (otherVector) {
  910. return new Vector4(this.x / otherVector.x, this.y / otherVector.y, this.z / otherVector.z, this.w / otherVector.w);
  911. };
  912. Vector4.prototype.divideToRef = function (otherVector, result) {
  913. result.x = this.x / otherVector.x;
  914. result.y = this.y / otherVector.y;
  915. result.z = this.z / otherVector.z;
  916. result.w = this.w / otherVector.w;
  917. return this;
  918. };
  919. Vector4.prototype.MinimizeInPlace = function (other) {
  920. if (other.x < this.x)
  921. this.x = other.x;
  922. if (other.y < this.y)
  923. this.y = other.y;
  924. if (other.z < this.z)
  925. this.z = other.z;
  926. if (other.w < this.w)
  927. this.w = other.w;
  928. return this;
  929. };
  930. Vector4.prototype.MaximizeInPlace = function (other) {
  931. if (other.x > this.x)
  932. this.x = other.x;
  933. if (other.y > this.y)
  934. this.y = other.y;
  935. if (other.z > this.z)
  936. this.z = other.z;
  937. if (other.w > this.w)
  938. this.w = other.w;
  939. return this;
  940. };
  941. // Properties
  942. Vector4.prototype.length = function () {
  943. return Math.sqrt(this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  944. };
  945. Vector4.prototype.lengthSquared = function () {
  946. return (this.x * this.x + this.y * this.y + this.z * this.z + this.w * this.w);
  947. };
  948. // Methods
  949. Vector4.prototype.normalize = function () {
  950. var len = this.length();
  951. if (len === 0)
  952. return this;
  953. var num = 1.0 / len;
  954. this.x *= num;
  955. this.y *= num;
  956. this.z *= num;
  957. this.w *= num;
  958. return this;
  959. };
  960. Vector4.prototype.clone = function () {
  961. return new Vector4(this.x, this.y, this.z, this.w);
  962. };
  963. Vector4.prototype.copyFrom = function (source) {
  964. this.x = source.x;
  965. this.y = source.y;
  966. this.z = source.z;
  967. this.w = source.w;
  968. return this;
  969. };
  970. Vector4.prototype.copyFromFloats = function (x, y, z, w) {
  971. this.x = x;
  972. this.y = y;
  973. this.z = z;
  974. this.w = w;
  975. return this;
  976. };
  977. // Statics
  978. Vector4.FromArray = function (array, offset) {
  979. if (!offset) {
  980. offset = 0;
  981. }
  982. return new Vector4(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  983. };
  984. Vector4.FromArrayToRef = function (array, offset, result) {
  985. result.x = array[offset];
  986. result.y = array[offset + 1];
  987. result.z = array[offset + 2];
  988. result.w = array[offset + 3];
  989. };
  990. Vector4.FromFloatArrayToRef = function (array, offset, result) {
  991. result.x = array[offset];
  992. result.y = array[offset + 1];
  993. result.z = array[offset + 2];
  994. result.w = array[offset + 3];
  995. };
  996. Vector4.FromFloatsToRef = function (x, y, z, w, result) {
  997. result.x = x;
  998. result.y = y;
  999. result.z = z;
  1000. result.w = w;
  1001. };
  1002. Vector4.Zero = function () {
  1003. return new Vector4(0, 0, 0, 0);
  1004. };
  1005. Vector4.Normalize = function (vector) {
  1006. var result = Vector4.Zero();
  1007. Vector4.NormalizeToRef(vector, result);
  1008. return result;
  1009. };
  1010. Vector4.NormalizeToRef = function (vector, result) {
  1011. result.copyFrom(vector);
  1012. result.normalize();
  1013. };
  1014. Vector4.Minimize = function (left, right) {
  1015. var min = left.clone();
  1016. min.MinimizeInPlace(right);
  1017. return min;
  1018. };
  1019. Vector4.Maximize = function (left, right) {
  1020. var max = left.clone();
  1021. max.MaximizeInPlace(right);
  1022. return max;
  1023. };
  1024. Vector4.Distance = function (value1, value2) {
  1025. return Math.sqrt(Vector4.DistanceSquared(value1, value2));
  1026. };
  1027. Vector4.DistanceSquared = function (value1, value2) {
  1028. var x = value1.x - value2.x;
  1029. var y = value1.y - value2.y;
  1030. var z = value1.z - value2.z;
  1031. var w = value1.w - value2.w;
  1032. return (x * x) + (y * y) + (z * z) + (w * w);
  1033. };
  1034. Vector4.Center = function (value1, value2) {
  1035. var center = value1.add(value2);
  1036. center.scaleInPlace(0.5);
  1037. return center;
  1038. };
  1039. return Vector4;
  1040. })();
  1041. BABYLON.Vector4 = Vector4;
  1042. var Quaternion = (function () {
  1043. function Quaternion(x, y, z, w) {
  1044. if (x === void 0) { x = 0; }
  1045. if (y === void 0) { y = 0; }
  1046. if (z === void 0) { z = 0; }
  1047. if (w === void 0) { w = 1; }
  1048. this.x = x;
  1049. this.y = y;
  1050. this.z = z;
  1051. this.w = w;
  1052. }
  1053. Quaternion.prototype.toString = function () {
  1054. return "{X: " + this.x + " Y:" + this.y + " Z:" + this.z + " W:" + this.w + "}";
  1055. };
  1056. Quaternion.prototype.asArray = function () {
  1057. return [this.x, this.y, this.z, this.w];
  1058. };
  1059. Quaternion.prototype.equals = function (otherQuaternion) {
  1060. return otherQuaternion && this.x === otherQuaternion.x && this.y === otherQuaternion.y && this.z === otherQuaternion.z && this.w === otherQuaternion.w;
  1061. };
  1062. Quaternion.prototype.clone = function () {
  1063. return new Quaternion(this.x, this.y, this.z, this.w);
  1064. };
  1065. Quaternion.prototype.copyFrom = function (other) {
  1066. this.x = other.x;
  1067. this.y = other.y;
  1068. this.z = other.z;
  1069. this.w = other.w;
  1070. return this;
  1071. };
  1072. Quaternion.prototype.copyFromFloats = function (x, y, z, w) {
  1073. this.x = x;
  1074. this.y = y;
  1075. this.z = z;
  1076. this.w = w;
  1077. return this;
  1078. };
  1079. Quaternion.prototype.add = function (other) {
  1080. return new Quaternion(this.x + other.x, this.y + other.y, this.z + other.z, this.w + other.w);
  1081. };
  1082. Quaternion.prototype.subtract = function (other) {
  1083. return new Quaternion(this.x - other.x, this.y - other.y, this.z - other.z, this.w - other.w);
  1084. };
  1085. Quaternion.prototype.scale = function (value) {
  1086. return new Quaternion(this.x * value, this.y * value, this.z * value, this.w * value);
  1087. };
  1088. Quaternion.prototype.multiply = function (q1) {
  1089. var result = new Quaternion(0, 0, 0, 1.0);
  1090. this.multiplyToRef(q1, result);
  1091. return result;
  1092. };
  1093. Quaternion.prototype.multiplyToRef = function (q1, result) {
  1094. result.x = this.x * q1.w + this.y * q1.z - this.z * q1.y + this.w * q1.x;
  1095. result.y = -this.x * q1.z + this.y * q1.w + this.z * q1.x + this.w * q1.y;
  1096. result.z = this.x * q1.y - this.y * q1.x + this.z * q1.w + this.w * q1.z;
  1097. result.w = -this.x * q1.x - this.y * q1.y - this.z * q1.z + this.w * q1.w;
  1098. return this;
  1099. };
  1100. Quaternion.prototype.length = function () {
  1101. return Math.sqrt((this.x * this.x) + (this.y * this.y) + (this.z * this.z) + (this.w * this.w));
  1102. };
  1103. Quaternion.prototype.normalize = function () {
  1104. var length = 1.0 / this.length();
  1105. this.x *= length;
  1106. this.y *= length;
  1107. this.z *= length;
  1108. this.w *= length;
  1109. return this;
  1110. };
  1111. Quaternion.prototype.toEulerAngles = function () {
  1112. var result = Vector3.Zero();
  1113. this.toEulerAnglesToRef(result);
  1114. return result;
  1115. };
  1116. Quaternion.prototype.toEulerAnglesToRef = function (result) {
  1117. //result is an EulerAngles in the in the z-x-z convention
  1118. var qx = this.x;
  1119. var qy = this.y;
  1120. var qz = this.z;
  1121. var qw = this.w;
  1122. var qxy = qx * qy;
  1123. var qxz = qx * qz;
  1124. var qwy = qw * qy;
  1125. var qwz = qw * qz;
  1126. var qwx = qw * qx;
  1127. var qyz = qy * qz;
  1128. var sqx = qx * qx;
  1129. var sqy = qy * qy;
  1130. var determinant = sqx + sqy;
  1131. if (determinant !== 0.000 && determinant !== 1.000) {
  1132. result.x = Math.atan2(qxz + qwy, qwx - qyz);
  1133. result.y = Math.acos(1 - 2 * determinant);
  1134. result.z = Math.atan2(qxz - qwy, qwx + qyz);
  1135. }
  1136. else {
  1137. if (determinant === 0.0) {
  1138. result.x = 0.0;
  1139. result.y = 0.0;
  1140. 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)
  1141. }
  1142. else {
  1143. 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)
  1144. result.y = Math.PI;
  1145. result.z = 0.0;
  1146. }
  1147. }
  1148. return this;
  1149. };
  1150. Quaternion.prototype.toRotationMatrix = function (result) {
  1151. var xx = this.x * this.x;
  1152. var yy = this.y * this.y;
  1153. var zz = this.z * this.z;
  1154. var xy = this.x * this.y;
  1155. var zw = this.z * this.w;
  1156. var zx = this.z * this.x;
  1157. var yw = this.y * this.w;
  1158. var yz = this.y * this.z;
  1159. var xw = this.x * this.w;
  1160. result.m[0] = 1.0 - (2.0 * (yy + zz));
  1161. result.m[1] = 2.0 * (xy + zw);
  1162. result.m[2] = 2.0 * (zx - yw);
  1163. result.m[3] = 0;
  1164. result.m[4] = 2.0 * (xy - zw);
  1165. result.m[5] = 1.0 - (2.0 * (zz + xx));
  1166. result.m[6] = 2.0 * (yz + xw);
  1167. result.m[7] = 0;
  1168. result.m[8] = 2.0 * (zx + yw);
  1169. result.m[9] = 2.0 * (yz - xw);
  1170. result.m[10] = 1.0 - (2.0 * (yy + xx));
  1171. result.m[11] = 0;
  1172. result.m[12] = 0;
  1173. result.m[13] = 0;
  1174. result.m[14] = 0;
  1175. result.m[15] = 1.0;
  1176. return this;
  1177. };
  1178. Quaternion.prototype.fromRotationMatrix = function (matrix) {
  1179. Quaternion.FromRotationMatrixToRef(matrix, this);
  1180. return this;
  1181. };
  1182. // Statics
  1183. Quaternion.FromRotationMatrix = function (matrix) {
  1184. var result = new Quaternion();
  1185. Quaternion.FromRotationMatrixToRef(matrix, result);
  1186. return result;
  1187. };
  1188. Quaternion.FromRotationMatrixToRef = function (matrix, result) {
  1189. var data = matrix.m;
  1190. var m11 = data[0], m12 = data[4], m13 = data[8];
  1191. var m21 = data[1], m22 = data[5], m23 = data[9];
  1192. var m31 = data[2], m32 = data[6], m33 = data[10];
  1193. var trace = m11 + m22 + m33;
  1194. var s;
  1195. if (trace > 0) {
  1196. s = 0.5 / Math.sqrt(trace + 1.0);
  1197. result.w = 0.25 / s;
  1198. result.x = (m32 - m23) * s;
  1199. result.y = (m13 - m31) * s;
  1200. result.z = (m21 - m12) * s;
  1201. }
  1202. else if (m11 > m22 && m11 > m33) {
  1203. s = 2.0 * Math.sqrt(1.0 + m11 - m22 - m33);
  1204. result.w = (m32 - m23) / s;
  1205. result.x = 0.25 * s;
  1206. result.y = (m12 + m21) / s;
  1207. result.z = (m13 + m31) / s;
  1208. }
  1209. else if (m22 > m33) {
  1210. s = 2.0 * Math.sqrt(1.0 + m22 - m11 - m33);
  1211. result.w = (m13 - m31) / s;
  1212. result.x = (m12 + m21) / s;
  1213. result.y = 0.25 * s;
  1214. result.z = (m23 + m32) / s;
  1215. }
  1216. else {
  1217. s = 2.0 * Math.sqrt(1.0 + m33 - m11 - m22);
  1218. result.w = (m21 - m12) / s;
  1219. result.x = (m13 + m31) / s;
  1220. result.y = (m23 + m32) / s;
  1221. result.z = 0.25 * s;
  1222. }
  1223. };
  1224. Quaternion.Inverse = function (q) {
  1225. return new Quaternion(-q.x, -q.y, -q.z, q.w);
  1226. };
  1227. Quaternion.Identity = function () {
  1228. return new Quaternion(0, 0, 0, 1);
  1229. };
  1230. Quaternion.RotationAxis = function (axis, angle) {
  1231. var result = new Quaternion();
  1232. var sin = Math.sin(angle / 2);
  1233. result.w = Math.cos(angle / 2);
  1234. result.x = axis.x * sin;
  1235. result.y = axis.y * sin;
  1236. result.z = axis.z * sin;
  1237. return result;
  1238. };
  1239. Quaternion.FromArray = function (array, offset) {
  1240. if (!offset) {
  1241. offset = 0;
  1242. }
  1243. return new Quaternion(array[offset], array[offset + 1], array[offset + 2], array[offset + 3]);
  1244. };
  1245. Quaternion.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1246. var result = new Quaternion();
  1247. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1248. return result;
  1249. };
  1250. Quaternion.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1251. // Produces a quaternion from Euler angles in the z-y-x orientation (Tait-Bryan angles)
  1252. var halfRoll = roll * 0.5;
  1253. var halfPitch = pitch * 0.5;
  1254. var halfYaw = yaw * 0.5;
  1255. var sinRoll = Math.sin(halfRoll);
  1256. var cosRoll = Math.cos(halfRoll);
  1257. var sinPitch = Math.sin(halfPitch);
  1258. var cosPitch = Math.cos(halfPitch);
  1259. var sinYaw = Math.sin(halfYaw);
  1260. var cosYaw = Math.cos(halfYaw);
  1261. result.x = (cosYaw * sinPitch * cosRoll) + (sinYaw * cosPitch * sinRoll);
  1262. result.y = (sinYaw * cosPitch * cosRoll) - (cosYaw * sinPitch * sinRoll);
  1263. result.z = (cosYaw * cosPitch * sinRoll) - (sinYaw * sinPitch * cosRoll);
  1264. result.w = (cosYaw * cosPitch * cosRoll) + (sinYaw * sinPitch * sinRoll);
  1265. };
  1266. Quaternion.RotationAlphaBetaGamma = function (alpha, beta, gamma) {
  1267. var result = new Quaternion();
  1268. Quaternion.RotationAlphaBetaGammaToRef(alpha, beta, gamma, result);
  1269. return result;
  1270. };
  1271. Quaternion.RotationAlphaBetaGammaToRef = function (alpha, beta, gamma, result) {
  1272. // Produces a quaternion from Euler angles in the z-x-z orientation
  1273. var halfGammaPlusAlpha = (gamma + alpha) * 0.5;
  1274. var halfGammaMinusAlpha = (gamma - alpha) * 0.5;
  1275. var halfBeta = beta * 0.5;
  1276. result.x = Math.cos(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1277. result.y = Math.sin(halfGammaMinusAlpha) * Math.sin(halfBeta);
  1278. result.z = Math.sin(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1279. result.w = Math.cos(halfGammaPlusAlpha) * Math.cos(halfBeta);
  1280. };
  1281. Quaternion.Slerp = function (left, right, amount) {
  1282. var num2;
  1283. var num3;
  1284. var num = amount;
  1285. var num4 = (((left.x * right.x) + (left.y * right.y)) + (left.z * right.z)) + (left.w * right.w);
  1286. var flag = false;
  1287. if (num4 < 0) {
  1288. flag = true;
  1289. num4 = -num4;
  1290. }
  1291. if (num4 > 0.999999) {
  1292. num3 = 1 - num;
  1293. num2 = flag ? -num : num;
  1294. }
  1295. else {
  1296. var num5 = Math.acos(num4);
  1297. var num6 = (1.0 / Math.sin(num5));
  1298. num3 = (Math.sin((1.0 - num) * num5)) * num6;
  1299. num2 = flag ? ((-Math.sin(num * num5)) * num6) : ((Math.sin(num * num5)) * num6);
  1300. }
  1301. 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));
  1302. };
  1303. return Quaternion;
  1304. })();
  1305. BABYLON.Quaternion = Quaternion;
  1306. var Matrix = (function () {
  1307. function Matrix() {
  1308. this.m = new Float32Array(16);
  1309. }
  1310. // Properties
  1311. Matrix.prototype.isIdentity = function () {
  1312. if (this.m[0] !== 1.0 || this.m[5] !== 1.0 || this.m[10] !== 1.0 || this.m[15] !== 1.0)
  1313. return false;
  1314. if (this.m[1] !== 0.0 || this.m[2] !== 0.0 || this.m[3] !== 0.0 || this.m[4] !== 0.0 || this.m[6] !== 0.0 || this.m[7] !== 0.0 || this.m[8] !== 0.0 || this.m[9] !== 0.0 || this.m[11] !== 0.0 || this.m[12] !== 0.0 || this.m[13] !== 0.0 || this.m[14] !== 0.0)
  1315. return false;
  1316. return true;
  1317. };
  1318. Matrix.prototype.determinant = function () {
  1319. var temp1 = (this.m[10] * this.m[15]) - (this.m[11] * this.m[14]);
  1320. var temp2 = (this.m[9] * this.m[15]) - (this.m[11] * this.m[13]);
  1321. var temp3 = (this.m[9] * this.m[14]) - (this.m[10] * this.m[13]);
  1322. var temp4 = (this.m[8] * this.m[15]) - (this.m[11] * this.m[12]);
  1323. var temp5 = (this.m[8] * this.m[14]) - (this.m[10] * this.m[12]);
  1324. var temp6 = (this.m[8] * this.m[13]) - (this.m[9] * this.m[12]);
  1325. return ((((this.m[0] * (((this.m[5] * temp1) - (this.m[6] * temp2)) + (this.m[7] * temp3))) - (this.m[1] * (((this.m[4] * temp1) - (this.m[6] * temp4)) + (this.m[7] * temp5)))) + (this.m[2] * (((this.m[4] * temp2) - (this.m[5] * temp4)) + (this.m[7] * temp6)))) - (this.m[3] * (((this.m[4] * temp3) - (this.m[5] * temp5)) + (this.m[6] * temp6))));
  1326. };
  1327. // Methods
  1328. Matrix.prototype.toArray = function () {
  1329. return this.m;
  1330. };
  1331. Matrix.prototype.asArray = function () {
  1332. return this.toArray();
  1333. };
  1334. Matrix.prototype.invert = function () {
  1335. this.invertToRef(this);
  1336. return this;
  1337. };
  1338. Matrix.prototype.invertToRef = function (other) {
  1339. var l1 = this.m[0];
  1340. var l2 = this.m[1];
  1341. var l3 = this.m[2];
  1342. var l4 = this.m[3];
  1343. var l5 = this.m[4];
  1344. var l6 = this.m[5];
  1345. var l7 = this.m[6];
  1346. var l8 = this.m[7];
  1347. var l9 = this.m[8];
  1348. var l10 = this.m[9];
  1349. var l11 = this.m[10];
  1350. var l12 = this.m[11];
  1351. var l13 = this.m[12];
  1352. var l14 = this.m[13];
  1353. var l15 = this.m[14];
  1354. var l16 = this.m[15];
  1355. var l17 = (l11 * l16) - (l12 * l15);
  1356. var l18 = (l10 * l16) - (l12 * l14);
  1357. var l19 = (l10 * l15) - (l11 * l14);
  1358. var l20 = (l9 * l16) - (l12 * l13);
  1359. var l21 = (l9 * l15) - (l11 * l13);
  1360. var l22 = (l9 * l14) - (l10 * l13);
  1361. var l23 = ((l6 * l17) - (l7 * l18)) + (l8 * l19);
  1362. var l24 = -(((l5 * l17) - (l7 * l20)) + (l8 * l21));
  1363. var l25 = ((l5 * l18) - (l6 * l20)) + (l8 * l22);
  1364. var l26 = -(((l5 * l19) - (l6 * l21)) + (l7 * l22));
  1365. var l27 = 1.0 / ((((l1 * l23) + (l2 * l24)) + (l3 * l25)) + (l4 * l26));
  1366. var l28 = (l7 * l16) - (l8 * l15);
  1367. var l29 = (l6 * l16) - (l8 * l14);
  1368. var l30 = (l6 * l15) - (l7 * l14);
  1369. var l31 = (l5 * l16) - (l8 * l13);
  1370. var l32 = (l5 * l15) - (l7 * l13);
  1371. var l33 = (l5 * l14) - (l6 * l13);
  1372. var l34 = (l7 * l12) - (l8 * l11);
  1373. var l35 = (l6 * l12) - (l8 * l10);
  1374. var l36 = (l6 * l11) - (l7 * l10);
  1375. var l37 = (l5 * l12) - (l8 * l9);
  1376. var l38 = (l5 * l11) - (l7 * l9);
  1377. var l39 = (l5 * l10) - (l6 * l9);
  1378. other.m[0] = l23 * l27;
  1379. other.m[4] = l24 * l27;
  1380. other.m[8] = l25 * l27;
  1381. other.m[12] = l26 * l27;
  1382. other.m[1] = -(((l2 * l17) - (l3 * l18)) + (l4 * l19)) * l27;
  1383. other.m[5] = (((l1 * l17) - (l3 * l20)) + (l4 * l21)) * l27;
  1384. other.m[9] = -(((l1 * l18) - (l2 * l20)) + (l4 * l22)) * l27;
  1385. other.m[13] = (((l1 * l19) - (l2 * l21)) + (l3 * l22)) * l27;
  1386. other.m[2] = (((l2 * l28) - (l3 * l29)) + (l4 * l30)) * l27;
  1387. other.m[6] = -(((l1 * l28) - (l3 * l31)) + (l4 * l32)) * l27;
  1388. other.m[10] = (((l1 * l29) - (l2 * l31)) + (l4 * l33)) * l27;
  1389. other.m[14] = -(((l1 * l30) - (l2 * l32)) + (l3 * l33)) * l27;
  1390. other.m[3] = -(((l2 * l34) - (l3 * l35)) + (l4 * l36)) * l27;
  1391. other.m[7] = (((l1 * l34) - (l3 * l37)) + (l4 * l38)) * l27;
  1392. other.m[11] = -(((l1 * l35) - (l2 * l37)) + (l4 * l39)) * l27;
  1393. other.m[15] = (((l1 * l36) - (l2 * l38)) + (l3 * l39)) * l27;
  1394. return this;
  1395. };
  1396. Matrix.prototype.invertToRefSIMD = function (other) {
  1397. var src = this.m;
  1398. var dest = other.m;
  1399. var row0, row1, row2, row3;
  1400. var tmp1;
  1401. var minor0, minor1, minor2, minor3;
  1402. var det;
  1403. // Load the 4 rows
  1404. var src0 = SIMD.float32x4.load(src, 0);
  1405. var src1 = SIMD.float32x4.load(src, 4);
  1406. var src2 = SIMD.float32x4.load(src, 8);
  1407. var src3 = SIMD.float32x4.load(src, 12);
  1408. // Transpose the source matrix. Sort of. Not a true transpose operation
  1409. tmp1 = SIMD.float32x4.shuffle(src0, src1, 0, 1, 4, 5);
  1410. row1 = SIMD.float32x4.shuffle(src2, src3, 0, 1, 4, 5);
  1411. row0 = SIMD.float32x4.shuffle(tmp1, row1, 0, 2, 4, 6);
  1412. row1 = SIMD.float32x4.shuffle(row1, tmp1, 1, 3, 5, 7);
  1413. tmp1 = SIMD.float32x4.shuffle(src0, src1, 2, 3, 6, 7);
  1414. row3 = SIMD.float32x4.shuffle(src2, src3, 2, 3, 6, 7);
  1415. row2 = SIMD.float32x4.shuffle(tmp1, row3, 0, 2, 4, 6);
  1416. row3 = SIMD.float32x4.shuffle(row3, tmp1, 1, 3, 5, 7);
  1417. // This is a true transposition, but it will lead to an incorrect result
  1418. //tmp1 = SIMD.float32x4.shuffle(src0, src1, 0, 1, 4, 5);
  1419. //tmp2 = SIMD.float32x4.shuffle(src2, src3, 0, 1, 4, 5);
  1420. //row0 = SIMD.float32x4.shuffle(tmp1, tmp2, 0, 2, 4, 6);
  1421. //row1 = SIMD.float32x4.shuffle(tmp1, tmp2, 1, 3, 5, 7);
  1422. //tmp1 = SIMD.float32x4.shuffle(src0, src1, 2, 3, 6, 7);
  1423. //tmp2 = SIMD.float32x4.shuffle(src2, src3, 2, 3, 6, 7);
  1424. //row2 = SIMD.float32x4.shuffle(tmp1, tmp2, 0, 2, 4, 6);
  1425. //row3 = SIMD.float32x4.shuffle(tmp1, tmp2, 1, 3, 5, 7);
  1426. // ----
  1427. tmp1 = SIMD.float32x4.mul(row2, row3);
  1428. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1429. minor0 = SIMD.float32x4.mul(row1, tmp1);
  1430. minor1 = SIMD.float32x4.mul(row0, tmp1);
  1431. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1432. minor0 = SIMD.float32x4.sub(SIMD.float32x4.mul(row1, tmp1), minor0);
  1433. minor1 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor1);
  1434. minor1 = SIMD.float32x4.swizzle(minor1, 2, 3, 0, 1); // 0x4E = 01001110
  1435. // ----
  1436. tmp1 = SIMD.float32x4.mul(row1, row2);
  1437. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1438. minor0 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor0);
  1439. minor3 = SIMD.float32x4.mul(row0, tmp1);
  1440. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1441. minor0 = SIMD.float32x4.sub(minor0, SIMD.float32x4.mul(row3, tmp1));
  1442. minor3 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor3);
  1443. minor3 = SIMD.float32x4.swizzle(minor3, 2, 3, 0, 1); // 0x4E = 01001110
  1444. // ----
  1445. tmp1 = SIMD.float32x4.mul(SIMD.float32x4.swizzle(row1, 2, 3, 0, 1), row3); // 0x4E = 01001110
  1446. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1447. row2 = SIMD.float32x4.swizzle(row2, 2, 3, 0, 1); // 0x4E = 01001110
  1448. minor0 = SIMD.float32x4.add(SIMD.float32x4.mul(row2, tmp1), minor0);
  1449. minor2 = SIMD.float32x4.mul(row0, tmp1);
  1450. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1451. minor0 = SIMD.float32x4.sub(minor0, SIMD.float32x4.mul(row2, tmp1));
  1452. minor2 = SIMD.float32x4.sub(SIMD.float32x4.mul(row0, tmp1), minor2);
  1453. minor2 = SIMD.float32x4.swizzle(minor2, 2, 3, 0, 1); // 0x4E = 01001110
  1454. // ----
  1455. tmp1 = SIMD.float32x4.mul(row0, row1);
  1456. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1457. minor2 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor2);
  1458. minor3 = SIMD.float32x4.sub(SIMD.float32x4.mul(row2, tmp1), minor3);
  1459. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1460. minor2 = SIMD.float32x4.sub(SIMD.float32x4.mul(row3, tmp1), minor2);
  1461. minor3 = SIMD.float32x4.sub(minor3, SIMD.float32x4.mul(row2, tmp1));
  1462. // ----
  1463. tmp1 = SIMD.float32x4.mul(row0, row3);
  1464. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1465. minor1 = SIMD.float32x4.sub(minor1, SIMD.float32x4.mul(row2, tmp1));
  1466. minor2 = SIMD.float32x4.add(SIMD.float32x4.mul(row1, tmp1), minor2);
  1467. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1468. minor1 = SIMD.float32x4.add(SIMD.float32x4.mul(row2, tmp1), minor1);
  1469. minor2 = SIMD.float32x4.sub(minor2, SIMD.float32x4.mul(row1, tmp1));
  1470. // ----
  1471. tmp1 = SIMD.float32x4.mul(row0, row2);
  1472. tmp1 = SIMD.float32x4.swizzle(tmp1, 1, 0, 3, 2); // 0xB1 = 10110001
  1473. minor1 = SIMD.float32x4.add(SIMD.float32x4.mul(row3, tmp1), minor1);
  1474. minor3 = SIMD.float32x4.sub(minor3, SIMD.float32x4.mul(row1, tmp1));
  1475. tmp1 = SIMD.float32x4.swizzle(tmp1, 2, 3, 0, 1); // 0x4E = 01001110
  1476. minor1 = SIMD.float32x4.sub(minor1, SIMD.float32x4.mul(row3, tmp1));
  1477. minor3 = SIMD.float32x4.add(SIMD.float32x4.mul(row1, tmp1), minor3);
  1478. // Compute determinant
  1479. det = SIMD.float32x4.mul(row0, minor0);
  1480. det = SIMD.float32x4.add(SIMD.float32x4.swizzle(det, 2, 3, 0, 1), det); // 0x4E = 01001110
  1481. det = SIMD.float32x4.add(SIMD.float32x4.swizzle(det, 1, 0, 3, 2), det); // 0xB1 = 10110001
  1482. tmp1 = SIMD.float32x4.reciprocal(det);
  1483. det = SIMD.float32x4.sub(SIMD.float32x4.add(tmp1, tmp1), SIMD.float32x4.mul(det, SIMD.float32x4.mul(tmp1, tmp1)));
  1484. det = SIMD.float32x4.swizzle(det, 0, 0, 0, 0);
  1485. // These shuffles aren't necessary if the faulty transposition is done
  1486. // up at the top of this function.
  1487. //minor0 = SIMD.float32x4.swizzle(minor0, 2, 1, 0, 3);
  1488. //minor1 = SIMD.float32x4.swizzle(minor1, 2, 1, 0, 3);
  1489. //minor2 = SIMD.float32x4.swizzle(minor2, 2, 1, 0, 3);
  1490. //minor3 = SIMD.float32x4.swizzle(minor3, 2, 1, 0, 3);
  1491. // Compute final values by multiplying with 1/det
  1492. minor0 = SIMD.float32x4.mul(det, minor0);
  1493. minor1 = SIMD.float32x4.mul(det, minor1);
  1494. minor2 = SIMD.float32x4.mul(det, minor2);
  1495. minor3 = SIMD.float32x4.mul(det, minor3);
  1496. SIMD.float32x4.store(dest, 0, minor0);
  1497. SIMD.float32x4.store(dest, 4, minor1);
  1498. SIMD.float32x4.store(dest, 8, minor2);
  1499. SIMD.float32x4.store(dest, 12, minor3);
  1500. return this;
  1501. };
  1502. Matrix.prototype.setTranslation = function (vector3) {
  1503. this.m[12] = vector3.x;
  1504. this.m[13] = vector3.y;
  1505. this.m[14] = vector3.z;
  1506. return this;
  1507. };
  1508. Matrix.prototype.multiply = function (other) {
  1509. var result = new Matrix();
  1510. this.multiplyToRef(other, result);
  1511. return result;
  1512. };
  1513. Matrix.prototype.copyFrom = function (other) {
  1514. for (var index = 0; index < 16; index++) {
  1515. this.m[index] = other.m[index];
  1516. }
  1517. return this;
  1518. };
  1519. Matrix.prototype.copyToArray = function (array, offset) {
  1520. if (offset === void 0) { offset = 0; }
  1521. for (var index = 0; index < 16; index++) {
  1522. array[offset + index] = this.m[index];
  1523. }
  1524. return this;
  1525. };
  1526. Matrix.prototype.multiplyToRef = function (other, result) {
  1527. this.multiplyToArray(other, result.m, 0);
  1528. return this;
  1529. };
  1530. Matrix.prototype.multiplyToArray = function (other, result, offset) {
  1531. var tm0 = this.m[0];
  1532. var tm1 = this.m[1];
  1533. var tm2 = this.m[2];
  1534. var tm3 = this.m[3];
  1535. var tm4 = this.m[4];
  1536. var tm5 = this.m[5];
  1537. var tm6 = this.m[6];
  1538. var tm7 = this.m[7];
  1539. var tm8 = this.m[8];
  1540. var tm9 = this.m[9];
  1541. var tm10 = this.m[10];
  1542. var tm11 = this.m[11];
  1543. var tm12 = this.m[12];
  1544. var tm13 = this.m[13];
  1545. var tm14 = this.m[14];
  1546. var tm15 = this.m[15];
  1547. var om0 = other.m[0];
  1548. var om1 = other.m[1];
  1549. var om2 = other.m[2];
  1550. var om3 = other.m[3];
  1551. var om4 = other.m[4];
  1552. var om5 = other.m[5];
  1553. var om6 = other.m[6];
  1554. var om7 = other.m[7];
  1555. var om8 = other.m[8];
  1556. var om9 = other.m[9];
  1557. var om10 = other.m[10];
  1558. var om11 = other.m[11];
  1559. var om12 = other.m[12];
  1560. var om13 = other.m[13];
  1561. var om14 = other.m[14];
  1562. var om15 = other.m[15];
  1563. result[offset] = tm0 * om0 + tm1 * om4 + tm2 * om8 + tm3 * om12;
  1564. result[offset + 1] = tm0 * om1 + tm1 * om5 + tm2 * om9 + tm3 * om13;
  1565. result[offset + 2] = tm0 * om2 + tm1 * om6 + tm2 * om10 + tm3 * om14;
  1566. result[offset + 3] = tm0 * om3 + tm1 * om7 + tm2 * om11 + tm3 * om15;
  1567. result[offset + 4] = tm4 * om0 + tm5 * om4 + tm6 * om8 + tm7 * om12;
  1568. result[offset + 5] = tm4 * om1 + tm5 * om5 + tm6 * om9 + tm7 * om13;
  1569. result[offset + 6] = tm4 * om2 + tm5 * om6 + tm6 * om10 + tm7 * om14;
  1570. result[offset + 7] = tm4 * om3 + tm5 * om7 + tm6 * om11 + tm7 * om15;
  1571. result[offset + 8] = tm8 * om0 + tm9 * om4 + tm10 * om8 + tm11 * om12;
  1572. result[offset + 9] = tm8 * om1 + tm9 * om5 + tm10 * om9 + tm11 * om13;
  1573. result[offset + 10] = tm8 * om2 + tm9 * om6 + tm10 * om10 + tm11 * om14;
  1574. result[offset + 11] = tm8 * om3 + tm9 * om7 + tm10 * om11 + tm11 * om15;
  1575. result[offset + 12] = tm12 * om0 + tm13 * om4 + tm14 * om8 + tm15 * om12;
  1576. result[offset + 13] = tm12 * om1 + tm13 * om5 + tm14 * om9 + tm15 * om13;
  1577. result[offset + 14] = tm12 * om2 + tm13 * om6 + tm14 * om10 + tm15 * om14;
  1578. result[offset + 15] = tm12 * om3 + tm13 * om7 + tm14 * om11 + tm15 * om15;
  1579. return this;
  1580. };
  1581. Matrix.prototype.multiplyToArraySIMD = function (other, result, offset) {
  1582. if (offset === void 0) { offset = 0; }
  1583. var tm = this.m;
  1584. var om = other.m;
  1585. var om0 = SIMD.float32x4.load(om, 0);
  1586. var om1 = SIMD.float32x4.load(om, 4);
  1587. var om2 = SIMD.float32x4.load(om, 8);
  1588. var om3 = SIMD.float32x4.load(om, 12);
  1589. var tm0 = SIMD.float32x4.load(tm, 0);
  1590. SIMD.float32x4.store(result, offset + 0, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 0, 0, 0, 0), om0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 1, 1, 1, 1), om1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 2, 2, 2, 2), om2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm0, 3, 3, 3, 3), om3)))));
  1591. var tm1 = SIMD.float32x4.load(tm, 4);
  1592. SIMD.float32x4.store(result, offset + 4, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 0, 0, 0, 0), om0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 1, 1, 1, 1), om1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 2, 2, 2, 2), om2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm1, 3, 3, 3, 3), om3)))));
  1593. var tm2 = SIMD.float32x4.load(tm, 8);
  1594. SIMD.float32x4.store(result, offset + 8, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 0, 0, 0, 0), om0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 1, 1, 1, 1), om1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 2, 2, 2, 2), om2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm2, 3, 3, 3, 3), om3)))));
  1595. var tm3 = SIMD.float32x4.load(tm, 12);
  1596. SIMD.float32x4.store(result, offset + 12, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 0, 0, 0, 0), om0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 1, 1, 1, 1), om1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 2, 2, 2, 2), om2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(tm3, 3, 3, 3, 3), om3)))));
  1597. };
  1598. Matrix.prototype.equals = function (value) {
  1599. return value && (this.m[0] === value.m[0] && this.m[1] === value.m[1] && this.m[2] === value.m[2] && this.m[3] === value.m[3] && this.m[4] === value.m[4] && this.m[5] === value.m[5] && this.m[6] === value.m[6] && this.m[7] === value.m[7] && this.m[8] === value.m[8] && this.m[9] === value.m[9] && this.m[10] === value.m[10] && this.m[11] === value.m[11] && this.m[12] === value.m[12] && this.m[13] === value.m[13] && this.m[14] === value.m[14] && this.m[15] === value.m[15]);
  1600. };
  1601. Matrix.prototype.clone = function () {
  1602. return Matrix.FromValues(this.m[0], this.m[1], this.m[2], this.m[3], this.m[4], this.m[5], this.m[6], this.m[7], this.m[8], this.m[9], this.m[10], this.m[11], this.m[12], this.m[13], this.m[14], this.m[15]);
  1603. };
  1604. Matrix.prototype.decompose = function (scale, rotation, translation) {
  1605. translation.x = this.m[12];
  1606. translation.y = this.m[13];
  1607. translation.z = this.m[14];
  1608. var xs = BABYLON.Tools.Sign(this.m[0] * this.m[1] * this.m[2] * this.m[3]) < 0 ? -1 : 1;
  1609. var ys = BABYLON.Tools.Sign(this.m[4] * this.m[5] * this.m[6] * this.m[7]) < 0 ? -1 : 1;
  1610. var zs = BABYLON.Tools.Sign(this.m[8] * this.m[9] * this.m[10] * this.m[11]) < 0 ? -1 : 1;
  1611. scale.x = xs * Math.sqrt(this.m[0] * this.m[0] + this.m[1] * this.m[1] + this.m[2] * this.m[2]);
  1612. scale.y = ys * Math.sqrt(this.m[4] * this.m[4] + this.m[5] * this.m[5] + this.m[6] * this.m[6]);
  1613. scale.z = zs * Math.sqrt(this.m[8] * this.m[8] + this.m[9] * this.m[9] + this.m[10] * this.m[10]);
  1614. if (scale.x === 0 || scale.y === 0 || scale.z === 0) {
  1615. rotation.x = 0;
  1616. rotation.y = 0;
  1617. rotation.z = 0;
  1618. rotation.w = 1;
  1619. return false;
  1620. }
  1621. var rotationMatrix = Matrix.FromValues(this.m[0] / scale.x, this.m[1] / scale.x, this.m[2] / scale.x, 0, this.m[4] / scale.y, this.m[5] / scale.y, this.m[6] / scale.y, 0, this.m[8] / scale.z, this.m[9] / scale.z, this.m[10] / scale.z, 0, 0, 0, 0, 1);
  1622. Quaternion.FromRotationMatrixToRef(rotationMatrix, rotation);
  1623. return true;
  1624. };
  1625. // Statics
  1626. Matrix.FromArray = function (array, offset) {
  1627. var result = new Matrix();
  1628. if (!offset) {
  1629. offset = 0;
  1630. }
  1631. Matrix.FromArrayToRef(array, offset, result);
  1632. return result;
  1633. };
  1634. Matrix.FromArrayToRef = function (array, offset, result) {
  1635. for (var index = 0; index < 16; index++) {
  1636. result.m[index] = array[index + offset];
  1637. }
  1638. };
  1639. Matrix.FromValuesToRef = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44, result) {
  1640. result.m[0] = initialM11;
  1641. result.m[1] = initialM12;
  1642. result.m[2] = initialM13;
  1643. result.m[3] = initialM14;
  1644. result.m[4] = initialM21;
  1645. result.m[5] = initialM22;
  1646. result.m[6] = initialM23;
  1647. result.m[7] = initialM24;
  1648. result.m[8] = initialM31;
  1649. result.m[9] = initialM32;
  1650. result.m[10] = initialM33;
  1651. result.m[11] = initialM34;
  1652. result.m[12] = initialM41;
  1653. result.m[13] = initialM42;
  1654. result.m[14] = initialM43;
  1655. result.m[15] = initialM44;
  1656. };
  1657. Matrix.FromValues = function (initialM11, initialM12, initialM13, initialM14, initialM21, initialM22, initialM23, initialM24, initialM31, initialM32, initialM33, initialM34, initialM41, initialM42, initialM43, initialM44) {
  1658. var result = new Matrix();
  1659. result.m[0] = initialM11;
  1660. result.m[1] = initialM12;
  1661. result.m[2] = initialM13;
  1662. result.m[3] = initialM14;
  1663. result.m[4] = initialM21;
  1664. result.m[5] = initialM22;
  1665. result.m[6] = initialM23;
  1666. result.m[7] = initialM24;
  1667. result.m[8] = initialM31;
  1668. result.m[9] = initialM32;
  1669. result.m[10] = initialM33;
  1670. result.m[11] = initialM34;
  1671. result.m[12] = initialM41;
  1672. result.m[13] = initialM42;
  1673. result.m[14] = initialM43;
  1674. result.m[15] = initialM44;
  1675. return result;
  1676. };
  1677. Matrix.Compose = function (scale, rotation, translation) {
  1678. var result = Matrix.FromValues(scale.x, 0, 0, 0, 0, scale.y, 0, 0, 0, 0, scale.z, 0, 0, 0, 0, 1);
  1679. var rotationMatrix = Matrix.Identity();
  1680. rotation.toRotationMatrix(rotationMatrix);
  1681. result = result.multiply(rotationMatrix);
  1682. result.setTranslation(translation);
  1683. return result;
  1684. };
  1685. Matrix.Identity = function () {
  1686. return Matrix.FromValues(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0);
  1687. };
  1688. Matrix.IdentityToRef = function (result) {
  1689. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, result);
  1690. };
  1691. Matrix.Zero = function () {
  1692. return Matrix.FromValues(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
  1693. };
  1694. Matrix.RotationX = function (angle) {
  1695. var result = new Matrix();
  1696. Matrix.RotationXToRef(angle, result);
  1697. return result;
  1698. };
  1699. Matrix.Invert = function (source) {
  1700. var result = new Matrix();
  1701. source.invertToRef(result);
  1702. return result;
  1703. };
  1704. Matrix.RotationXToRef = function (angle, result) {
  1705. var s = Math.sin(angle);
  1706. var c = Math.cos(angle);
  1707. result.m[0] = 1.0;
  1708. result.m[15] = 1.0;
  1709. result.m[5] = c;
  1710. result.m[10] = c;
  1711. result.m[9] = -s;
  1712. result.m[6] = s;
  1713. result.m[1] = 0;
  1714. result.m[2] = 0;
  1715. result.m[3] = 0;
  1716. result.m[4] = 0;
  1717. result.m[7] = 0;
  1718. result.m[8] = 0;
  1719. result.m[11] = 0;
  1720. result.m[12] = 0;
  1721. result.m[13] = 0;
  1722. result.m[14] = 0;
  1723. };
  1724. Matrix.RotationY = function (angle) {
  1725. var result = new Matrix();
  1726. Matrix.RotationYToRef(angle, result);
  1727. return result;
  1728. };
  1729. Matrix.RotationYToRef = function (angle, result) {
  1730. var s = Math.sin(angle);
  1731. var c = Math.cos(angle);
  1732. result.m[5] = 1.0;
  1733. result.m[15] = 1.0;
  1734. result.m[0] = c;
  1735. result.m[2] = -s;
  1736. result.m[8] = s;
  1737. result.m[10] = c;
  1738. result.m[1] = 0;
  1739. result.m[3] = 0;
  1740. result.m[4] = 0;
  1741. result.m[6] = 0;
  1742. result.m[7] = 0;
  1743. result.m[9] = 0;
  1744. result.m[11] = 0;
  1745. result.m[12] = 0;
  1746. result.m[13] = 0;
  1747. result.m[14] = 0;
  1748. };
  1749. Matrix.RotationZ = function (angle) {
  1750. var result = new Matrix();
  1751. Matrix.RotationZToRef(angle, result);
  1752. return result;
  1753. };
  1754. Matrix.RotationZToRef = function (angle, result) {
  1755. var s = Math.sin(angle);
  1756. var c = Math.cos(angle);
  1757. result.m[10] = 1.0;
  1758. result.m[15] = 1.0;
  1759. result.m[0] = c;
  1760. result.m[1] = s;
  1761. result.m[4] = -s;
  1762. result.m[5] = c;
  1763. result.m[2] = 0;
  1764. result.m[3] = 0;
  1765. result.m[6] = 0;
  1766. result.m[7] = 0;
  1767. result.m[8] = 0;
  1768. result.m[9] = 0;
  1769. result.m[11] = 0;
  1770. result.m[12] = 0;
  1771. result.m[13] = 0;
  1772. result.m[14] = 0;
  1773. };
  1774. Matrix.RotationAxis = function (axis, angle) {
  1775. var s = Math.sin(-angle);
  1776. var c = Math.cos(-angle);
  1777. var c1 = 1 - c;
  1778. axis.normalize();
  1779. var result = Matrix.Zero();
  1780. result.m[0] = (axis.x * axis.x) * c1 + c;
  1781. result.m[1] = (axis.x * axis.y) * c1 - (axis.z * s);
  1782. result.m[2] = (axis.x * axis.z) * c1 + (axis.y * s);
  1783. result.m[3] = 0.0;
  1784. result.m[4] = (axis.y * axis.x) * c1 + (axis.z * s);
  1785. result.m[5] = (axis.y * axis.y) * c1 + c;
  1786. result.m[6] = (axis.y * axis.z) * c1 - (axis.x * s);
  1787. result.m[7] = 0.0;
  1788. result.m[8] = (axis.z * axis.x) * c1 - (axis.y * s);
  1789. result.m[9] = (axis.z * axis.y) * c1 + (axis.x * s);
  1790. result.m[10] = (axis.z * axis.z) * c1 + c;
  1791. result.m[11] = 0.0;
  1792. result.m[15] = 1.0;
  1793. return result;
  1794. };
  1795. Matrix.RotationYawPitchRoll = function (yaw, pitch, roll) {
  1796. var result = new Matrix();
  1797. Matrix.RotationYawPitchRollToRef(yaw, pitch, roll, result);
  1798. return result;
  1799. };
  1800. Matrix.RotationYawPitchRollToRef = function (yaw, pitch, roll, result) {
  1801. Quaternion.RotationYawPitchRollToRef(yaw, pitch, roll, this._tempQuaternion);
  1802. this._tempQuaternion.toRotationMatrix(result);
  1803. };
  1804. Matrix.Scaling = function (x, y, z) {
  1805. var result = Matrix.Zero();
  1806. Matrix.ScalingToRef(x, y, z, result);
  1807. return result;
  1808. };
  1809. Matrix.ScalingToRef = function (x, y, z, result) {
  1810. result.m[0] = x;
  1811. result.m[1] = 0;
  1812. result.m[2] = 0;
  1813. result.m[3] = 0;
  1814. result.m[4] = 0;
  1815. result.m[5] = y;
  1816. result.m[6] = 0;
  1817. result.m[7] = 0;
  1818. result.m[8] = 0;
  1819. result.m[9] = 0;
  1820. result.m[10] = z;
  1821. result.m[11] = 0;
  1822. result.m[12] = 0;
  1823. result.m[13] = 0;
  1824. result.m[14] = 0;
  1825. result.m[15] = 1.0;
  1826. };
  1827. Matrix.Translation = function (x, y, z) {
  1828. var result = Matrix.Identity();
  1829. Matrix.TranslationToRef(x, y, z, result);
  1830. return result;
  1831. };
  1832. Matrix.TranslationToRef = function (x, y, z, result) {
  1833. Matrix.FromValuesToRef(1.0, 0, 0, 0, 0, 1.0, 0, 0, 0, 0, 1.0, 0, x, y, z, 1.0, result);
  1834. };
  1835. Matrix.LookAtLH = function (eye, target, up) {
  1836. var result = Matrix.Zero();
  1837. Matrix.LookAtLHToRef(eye, target, up, result);
  1838. return result;
  1839. };
  1840. Matrix.LookAtLHToRef = function (eye, target, up, result) {
  1841. // Z axis
  1842. target.subtractToRef(eye, this._zAxis);
  1843. this._zAxis.normalize();
  1844. // X axis
  1845. Vector3.CrossToRef(up, this._zAxis, this._xAxis);
  1846. this._xAxis.normalize();
  1847. // Y axis
  1848. Vector3.CrossToRef(this._zAxis, this._xAxis, this._yAxis);
  1849. this._yAxis.normalize();
  1850. // Eye angles
  1851. var ex = -Vector3.Dot(this._xAxis, eye);
  1852. var ey = -Vector3.Dot(this._yAxis, eye);
  1853. var ez = -Vector3.Dot(this._zAxis, eye);
  1854. return Matrix.FromValuesToRef(this._xAxis.x, this._yAxis.x, this._zAxis.x, 0, this._xAxis.y, this._yAxis.y, this._zAxis.y, 0, this._xAxis.z, this._yAxis.z, this._zAxis.z, 0, ex, ey, ez, 1, result);
  1855. };
  1856. Matrix.LookAtLHToRefSIMD = function (eyeRef, targetRef, upRef, result) {
  1857. var out = result.m;
  1858. var center = SIMD.float32x4(targetRef.x, targetRef.y, targetRef.z, 0);
  1859. var eye = SIMD.float32x4(eyeRef.x, eyeRef.y, eyeRef.z, 0);
  1860. var up = SIMD.float32x4(upRef.x, upRef.y, upRef.z, 0);
  1861. // cc.kmVec3Subtract(f, pCenter, pEye);
  1862. var f = SIMD.float32x4.sub(center, eye);
  1863. // cc.kmVec3Normalize(f, f);
  1864. var tmp = SIMD.float32x4.mul(f, f);
  1865. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1866. f = SIMD.float32x4.mul(f, SIMD.float32x4.reciprocalSqrt(tmp));
  1867. // cc.kmVec3Assign(up, pUp);
  1868. // cc.kmVec3Normalize(up, up);
  1869. tmp = SIMD.float32x4.mul(up, up);
  1870. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1871. up = SIMD.float32x4.mul(up, SIMD.float32x4.reciprocalSqrt(tmp));
  1872. // cc.kmVec3Cross(s, f, up);
  1873. var s = SIMD.float32x4.sub(SIMD.float32x4.mul(SIMD.float32x4.swizzle(f, 1, 2, 0, 3), SIMD.float32x4.swizzle(up, 2, 0, 1, 3)), SIMD.float32x4.mul(SIMD.float32x4.swizzle(f, 2, 0, 1, 3), SIMD.float32x4.swizzle(up, 1, 2, 0, 3)));
  1874. // cc.kmVec3Normalize(s, s);
  1875. tmp = SIMD.float32x4.mul(s, s);
  1876. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1877. s = SIMD.float32x4.mul(s, SIMD.float32x4.reciprocalSqrt(tmp));
  1878. // cc.kmVec3Cross(u, s, f);
  1879. var u = SIMD.float32x4.sub(SIMD.float32x4.mul(SIMD.float32x4.swizzle(s, 1, 2, 0, 3), SIMD.float32x4.swizzle(f, 2, 0, 1, 3)), SIMD.float32x4.mul(SIMD.float32x4.swizzle(s, 2, 0, 1, 3), SIMD.float32x4.swizzle(f, 1, 2, 0, 3)));
  1880. // cc.kmVec3Normalize(s, s);
  1881. tmp = SIMD.float32x4.mul(s, s);
  1882. tmp = SIMD.float32x4.add(tmp, SIMD.float32x4.add(SIMD.float32x4.swizzle(tmp, 1, 2, 0, 3), SIMD.float32x4.swizzle(tmp, 2, 0, 1, 3)));
  1883. s = SIMD.float32x4.mul(s, SIMD.float32x4.reciprocalSqrt(tmp));
  1884. //cc.kmMat4Identity(pOut);
  1885. //pOut.mat[0] = s.x;
  1886. //pOut.mat[4] = s.y;
  1887. //pOut.mat[8] = s.z;
  1888. //pOut.mat[1] = u.x;
  1889. //pOut.mat[5] = u.y;
  1890. //pOut.mat[9] = u.z;
  1891. //pOut.mat[2] = -f.x;
  1892. //pOut.mat[6] = -f.y;
  1893. //pOut.mat[10] = -f.z;
  1894. var zero = SIMD.float32x4.splat(0.0);
  1895. s = SIMD.float32x4.neg(s);
  1896. var tmp01 = SIMD.float32x4.shuffle(s, u, 0, 1, 4, 5);
  1897. var tmp23 = SIMD.float32x4.shuffle(f, zero, 0, 1, 4, 5);
  1898. var a0 = SIMD.float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6);
  1899. var a1 = SIMD.float32x4.shuffle(tmp01, tmp23, 1, 3, 5, 7);
  1900. tmp01 = SIMD.float32x4.shuffle(s, u, 2, 3, 6, 7);
  1901. tmp23 = SIMD.float32x4.shuffle(f, zero, 2, 3, 6, 7);
  1902. var a2 = SIMD.float32x4.shuffle(tmp01, tmp23, 0, 2, 4, 6);
  1903. var a3 = SIMD.float32x4(0.0, 0.0, 0.0, 1.0);
  1904. // cc.kmMat4Translation(translate, -pEye.x, -pEye.y, -pEye.z);
  1905. var b0 = SIMD.float32x4(1.0, 0.0, 0.0, 0.0);
  1906. var b1 = SIMD.float32x4(0.0, 1.0, 0.0, 0.0);
  1907. var b2 = SIMD.float32x4(0.0, 0.0, 1.0, 0.0);
  1908. var b3 = SIMD.float32x4.neg(eye);
  1909. b3 = SIMD.float32x4.withW(b3, 1.0);
  1910. // cc.kmMat4Multiply(pOut, pOut, translate);
  1911. SIMD.float32x4.store(out, 0, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b0, 3, 3, 3, 3), a3)))));
  1912. SIMD.float32x4.store(out, 4, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b1, 3, 3, 3, 3), a3)))));
  1913. SIMD.float32x4.store(out, 8, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b2, 3, 3, 3, 3), a3)))));
  1914. SIMD.float32x4.store(out, 12, SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 0, 0, 0, 0), a0), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 1, 1, 1, 1), a1), SIMD.float32x4.add(SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 2, 2, 2, 2), a2), SIMD.float32x4.mul(SIMD.float32x4.swizzle(b3, 3, 3, 3, 3), a3)))));
  1915. };
  1916. Matrix.OrthoLH = function (width, height, znear, zfar) {
  1917. var matrix = Matrix.Zero();
  1918. Matrix.OrthoLHToRef(width, height, znear, zfar, matrix);
  1919. return matrix;
  1920. };
  1921. Matrix.OrthoLHToRef = function (width, height, znear, zfar, result) {
  1922. var hw = 2.0 / width;
  1923. var hh = 2.0 / height;
  1924. var id = 1.0 / (zfar - znear);
  1925. var nid = znear / (znear - zfar);
  1926. Matrix.FromValuesToRef(hw, 0, 0, 0, 0, hh, 0, 0, 0, 0, id, 0, 0, 0, nid, 1, result);
  1927. };
  1928. Matrix.OrthoOffCenterLH = function (left, right, bottom, top, znear, zfar) {
  1929. var matrix = Matrix.Zero();
  1930. Matrix.OrthoOffCenterLHToRef(left, right, bottom, top, znear, zfar, matrix);
  1931. return matrix;
  1932. };
  1933. Matrix.OrthoOffCenterLHToRef = function (left, right, bottom, top, znear, zfar, result) {
  1934. result.m[0] = 2.0 / (right - left);
  1935. result.m[1] = result.m[2] = result.m[3] = 0;
  1936. result.m[5] = 2.0 / (top - bottom);
  1937. result.m[4] = result.m[6] = result.m[7] = 0;
  1938. result.m[10] = -1.0 / (znear - zfar);
  1939. result.m[8] = result.m[9] = result.m[11] = 0;
  1940. result.m[12] = (left + right) / (left - right);
  1941. result.m[13] = (top + bottom) / (bottom - top);
  1942. result.m[14] = znear / (znear - zfar);
  1943. result.m[15] = 1.0;
  1944. };
  1945. Matrix.PerspectiveLH = function (width, height, znear, zfar) {
  1946. var matrix = Matrix.Zero();
  1947. matrix.m[0] = (2.0 * znear) / width;
  1948. matrix.m[1] = matrix.m[2] = matrix.m[3] = 0.0;
  1949. matrix.m[5] = (2.0 * znear) / height;
  1950. matrix.m[4] = matrix.m[6] = matrix.m[7] = 0.0;
  1951. matrix.m[10] = -zfar / (znear - zfar);
  1952. matrix.m[8] = matrix.m[9] = 0.0;
  1953. matrix.m[11] = 1.0;
  1954. matrix.m[12] = matrix.m[13] = matrix.m[15] = 0.0;
  1955. matrix.m[14] = (znear * zfar) / (znear - zfar);
  1956. return matrix;
  1957. };
  1958. Matrix.PerspectiveFovLH = function (fov, aspect, znear, zfar) {
  1959. var matrix = Matrix.Zero();
  1960. Matrix.PerspectiveFovLHToRef(fov, aspect, znear, zfar, matrix);
  1961. return matrix;
  1962. };
  1963. Matrix.PerspectiveFovLHToRef = function (fov, aspect, znear, zfar, result, fovMode) {
  1964. if (fovMode === void 0) { fovMode = BABYLON.Camera.FOVMODE_VERTICAL_FIXED; }
  1965. var tan = 1.0 / (Math.tan(fov * 0.5));
  1966. var v_fixed = (fovMode === BABYLON.Camera.FOVMODE_VERTICAL_FIXED);
  1967. if (v_fixed) {
  1968. result.m[0] = tan / aspect;
  1969. }
  1970. else {
  1971. result.m[0] = tan;
  1972. }
  1973. result.m[1] = result.m[2] = result.m[3] = 0.0;
  1974. if (v_fixed) {
  1975. result.m[5] = tan;
  1976. }
  1977. else {
  1978. result.m[5] = tan * aspect;
  1979. }
  1980. result.m[4] = result.m[6] = result.m[7] = 0.0;
  1981. result.m[8] = result.m[9] = 0.0;
  1982. result.m[10] = -zfar / (znear - zfar);
  1983. result.m[11] = 1.0;
  1984. result.m[12] = result.m[13] = result.m[15] = 0.0;
  1985. result.m[14] = (znear * zfar) / (znear - zfar);
  1986. };
  1987. Matrix.GetFinalMatrix = function (viewport, world, view, projection, zmin, zmax) {
  1988. var cw = viewport.width;
  1989. var ch = viewport.height;
  1990. var cx = viewport.x;
  1991. var cy = viewport.y;
  1992. var viewportMatrix = Matrix.FromValues(cw / 2.0, 0, 0, 0, 0, -ch / 2.0, 0, 0, 0, 0, zmax - zmin, 0, cx + cw / 2.0, ch / 2.0 + cy, zmin, 1);
  1993. return world.multiply(view).multiply(projection).multiply(viewportMatrix);
  1994. };
  1995. Matrix.Transpose = function (matrix) {
  1996. var result = new Matrix();
  1997. result.m[0] = matrix.m[0];
  1998. result.m[1] = matrix.m[4];
  1999. result.m[2] = matrix.m[8];
  2000. result.m[3] = matrix.m[12];
  2001. result.m[4] = matrix.m[1];
  2002. result.m[5] = matrix.m[5];
  2003. result.m[6] = matrix.m[9];
  2004. result.m[7] = matrix.m[13];
  2005. result.m[8] = matrix.m[2];
  2006. result.m[9] = matrix.m[6];
  2007. result.m[10] = matrix.m[10];
  2008. result.m[11] = matrix.m[14];
  2009. result.m[12] = matrix.m[3];
  2010. result.m[13] = matrix.m[7];
  2011. result.m[14] = matrix.m[11];
  2012. result.m[15] = matrix.m[15];
  2013. return result;
  2014. };
  2015. Matrix.Reflection = function (plane) {
  2016. var matrix = new Matrix();
  2017. Matrix.ReflectionToRef(plane, matrix);
  2018. return matrix;
  2019. };
  2020. Matrix.ReflectionToRef = function (plane, result) {
  2021. plane.normalize();
  2022. var x = plane.normal.x;
  2023. var y = plane.normal.y;
  2024. var z = plane.normal.z;
  2025. var temp = -2 * x;
  2026. var temp2 = -2 * y;
  2027. var temp3 = -2 * z;
  2028. result.m[0] = (temp * x) + 1;
  2029. result.m[1] = temp2 * x;
  2030. result.m[2] = temp3 * x;
  2031. result.m[3] = 0.0;
  2032. result.m[4] = temp * y;
  2033. result.m[5] = (temp2 * y) + 1;
  2034. result.m[6] = temp3 * y;
  2035. result.m[7] = 0.0;
  2036. result.m[8] = temp * z;
  2037. result.m[9] = temp2 * z;
  2038. result.m[10] = (temp3 * z) + 1;
  2039. result.m[11] = 0.0;
  2040. result.m[12] = temp * plane.d;
  2041. result.m[13] = temp2 * plane.d;
  2042. result.m[14] = temp3 * plane.d;
  2043. result.m[15] = 1.0;
  2044. };
  2045. Matrix._tempQuaternion = new Quaternion();
  2046. Matrix._xAxis = Vector3.Zero();
  2047. Matrix._yAxis = Vector3.Zero();
  2048. Matrix._zAxis = Vector3.Zero();
  2049. return Matrix;
  2050. })();
  2051. BABYLON.Matrix = Matrix;
  2052. var Plane = (function () {
  2053. function Plane(a, b, c, d) {
  2054. this.normal = new Vector3(a, b, c);
  2055. this.d = d;
  2056. }
  2057. Plane.prototype.asArray = function () {
  2058. return [this.normal.x, this.normal.y, this.normal.z, this.d];
  2059. };
  2060. // Methods
  2061. Plane.prototype.clone = function () {
  2062. return new Plane(this.normal.x, this.normal.y, this.normal.z, this.d);
  2063. };
  2064. Plane.prototype.normalize = function () {
  2065. var norm = (Math.sqrt((this.normal.x * this.normal.x) + (this.normal.y * this.normal.y) + (this.normal.z * this.normal.z)));
  2066. var magnitude = 0;
  2067. if (norm !== 0) {
  2068. magnitude = 1.0 / norm;
  2069. }
  2070. this.normal.x *= magnitude;
  2071. this.normal.y *= magnitude;
  2072. this.normal.z *= magnitude;
  2073. this.d *= magnitude;
  2074. return this;
  2075. };
  2076. Plane.prototype.transform = function (transformation) {
  2077. var transposedMatrix = Matrix.Transpose(transformation);
  2078. var x = this.normal.x;
  2079. var y = this.normal.y;
  2080. var z = this.normal.z;
  2081. var d = this.d;
  2082. var normalX = (((x * transposedMatrix.m[0]) + (y * transposedMatrix.m[1])) + (z * transposedMatrix.m[2])) + (d * transposedMatrix.m[3]);
  2083. var normalY = (((x * transposedMatrix.m[4]) + (y * transposedMatrix.m[5])) + (z * transposedMatrix.m[6])) + (d * transposedMatrix.m[7]);
  2084. var normalZ = (((x * transposedMatrix.m[8]) + (y * transposedMatrix.m[9])) + (z * transposedMatrix.m[10])) + (d * transposedMatrix.m[11]);
  2085. var finalD = (((x * transposedMatrix.m[12]) + (y * transposedMatrix.m[13])) + (z * transposedMatrix.m[14])) + (d * transposedMatrix.m[15]);
  2086. return new Plane(normalX, normalY, normalZ, finalD);
  2087. };
  2088. Plane.prototype.dotCoordinate = function (point) {
  2089. return ((((this.normal.x * point.x) + (this.normal.y * point.y)) + (this.normal.z * point.z)) + this.d);
  2090. };
  2091. Plane.prototype.copyFromPoints = function (point1, point2, point3) {
  2092. var x1 = point2.x - point1.x;
  2093. var y1 = point2.y - point1.y;
  2094. var z1 = point2.z - point1.z;
  2095. var x2 = point3.x - point1.x;
  2096. var y2 = point3.y - point1.y;
  2097. var z2 = point3.z - point1.z;
  2098. var yz = (y1 * z2) - (z1 * y2);
  2099. var xz = (z1 * x2) - (x1 * z2);
  2100. var xy = (x1 * y2) - (y1 * x2);
  2101. var pyth = (Math.sqrt((yz * yz) + (xz * xz) + (xy * xy)));
  2102. var invPyth;
  2103. if (pyth !== 0) {
  2104. invPyth = 1.0 / pyth;
  2105. }
  2106. else {
  2107. invPyth = 0;
  2108. }
  2109. this.normal.x = yz * invPyth;
  2110. this.normal.y = xz * invPyth;
  2111. this.normal.z = xy * invPyth;
  2112. this.d = -((this.normal.x * point1.x) + (this.normal.y * point1.y) + (this.normal.z * point1.z));
  2113. return this;
  2114. };
  2115. Plane.prototype.isFrontFacingTo = function (direction, epsilon) {
  2116. var dot = Vector3.Dot(this.normal, direction);
  2117. return (dot <= epsilon);
  2118. };
  2119. Plane.prototype.signedDistanceTo = function (point) {
  2120. return Vector3.Dot(point, this.normal) + this.d;
  2121. };
  2122. // Statics
  2123. Plane.FromArray = function (array) {
  2124. return new Plane(array[0], array[1], array[2], array[3]);
  2125. };
  2126. Plane.FromPoints = function (point1, point2, point3) {
  2127. var result = new Plane(0, 0, 0, 0);
  2128. result.copyFromPoints(point1, point2, point3);
  2129. return result;
  2130. };
  2131. Plane.FromPositionAndNormal = function (origin, normal) {
  2132. var result = new Plane(0, 0, 0, 0);
  2133. normal.normalize();
  2134. result.normal = normal;
  2135. result.d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2136. return result;
  2137. };
  2138. Plane.SignedDistanceToPlaneFromPositionAndNormal = function (origin, normal, point) {
  2139. var d = -(normal.x * origin.x + normal.y * origin.y + normal.z * origin.z);
  2140. return Vector3.Dot(point, normal) + d;
  2141. };
  2142. return Plane;
  2143. })();
  2144. BABYLON.Plane = Plane;
  2145. var Viewport = (function () {
  2146. function Viewport(x, y, width, height) {
  2147. this.x = x;
  2148. this.y = y;
  2149. this.width = width;
  2150. this.height = height;
  2151. }
  2152. Viewport.prototype.toGlobal = function (engine) {
  2153. var width = engine.getRenderWidth();
  2154. var height = engine.getRenderHeight();
  2155. return new Viewport(this.x * width, this.y * height, this.width * width, this.height * height);
  2156. };
  2157. return Viewport;
  2158. })();
  2159. BABYLON.Viewport = Viewport;
  2160. var Frustum = (function () {
  2161. function Frustum() {
  2162. }
  2163. Frustum.GetPlanes = function (transform) {
  2164. var frustumPlanes = [];
  2165. for (var index = 0; index < 6; index++) {
  2166. frustumPlanes.push(new Plane(0, 0, 0, 0));
  2167. }
  2168. Frustum.GetPlanesToRef(transform, frustumPlanes);
  2169. return frustumPlanes;
  2170. };
  2171. Frustum.GetPlanesToRef = function (transform, frustumPlanes) {
  2172. // Near
  2173. frustumPlanes[0].normal.x = transform.m[3] + transform.m[2];
  2174. frustumPlanes[0].normal.y = transform.m[7] + transform.m[6];
  2175. frustumPlanes[0].normal.z = transform.m[10] + transform.m[10];
  2176. frustumPlanes[0].d = transform.m[15] + transform.m[14];
  2177. frustumPlanes[0].normalize();
  2178. // Far
  2179. frustumPlanes[1].normal.x = transform.m[3] - transform.m[2];
  2180. frustumPlanes[1].normal.y = transform.m[7] - transform.m[6];
  2181. frustumPlanes[1].normal.z = transform.m[11] - transform.m[10];
  2182. frustumPlanes[1].d = transform.m[15] - transform.m[14];
  2183. frustumPlanes[1].normalize();
  2184. // Left
  2185. frustumPlanes[2].normal.x = transform.m[3] + transform.m[0];
  2186. frustumPlanes[2].normal.y = transform.m[7] + transform.m[4];
  2187. frustumPlanes[2].normal.z = transform.m[11] + transform.m[8];
  2188. frustumPlanes[2].d = transform.m[15] + transform.m[12];
  2189. frustumPlanes[2].normalize();
  2190. // Right
  2191. frustumPlanes[3].normal.x = transform.m[3] - transform.m[0];
  2192. frustumPlanes[3].normal.y = transform.m[7] - transform.m[4];
  2193. frustumPlanes[3].normal.z = transform.m[11] - transform.m[8];
  2194. frustumPlanes[3].d = transform.m[15] - transform.m[12];
  2195. frustumPlanes[3].normalize();
  2196. // Top
  2197. frustumPlanes[4].normal.x = transform.m[3] - transform.m[1];
  2198. frustumPlanes[4].normal.y = transform.m[7] - transform.m[5];
  2199. frustumPlanes[4].normal.z = transform.m[11] - transform.m[9];
  2200. frustumPlanes[4].d = transform.m[15] - transform.m[13];
  2201. frustumPlanes[4].normalize();
  2202. // Bottom
  2203. frustumPlanes[5].normal.x = transform.m[3] + transform.m[1];
  2204. frustumPlanes[5].normal.y = transform.m[7] + transform.m[5];
  2205. frustumPlanes[5].normal.z = transform.m[11] + transform.m[9];
  2206. frustumPlanes[5].d = transform.m[15] + transform.m[13];
  2207. frustumPlanes[5].normalize();
  2208. };
  2209. return Frustum;
  2210. })();
  2211. BABYLON.Frustum = Frustum;
  2212. var Ray = (function () {
  2213. function Ray(origin, direction, length) {
  2214. if (length === void 0) { length = Number.MAX_VALUE; }
  2215. this.origin = origin;
  2216. this.direction = direction;
  2217. this.length = length;
  2218. }
  2219. // Methods
  2220. Ray.prototype.intersectsBoxMinMax = function (minimum, maximum) {
  2221. var d = 0.0;
  2222. var maxValue = Number.MAX_VALUE;
  2223. if (Math.abs(this.direction.x) < 0.0000001) {
  2224. if (this.origin.x < minimum.x || this.origin.x > maximum.x) {
  2225. return false;
  2226. }
  2227. }
  2228. else {
  2229. var inv = 1.0 / this.direction.x;
  2230. var min = (minimum.x - this.origin.x) * inv;
  2231. var max = (maximum.x - this.origin.x) * inv;
  2232. if (max === -Infinity) {
  2233. max = Infinity;
  2234. }
  2235. if (min > max) {
  2236. var temp = min;
  2237. min = max;
  2238. max = temp;
  2239. }
  2240. d = Math.max(min, d);
  2241. maxValue = Math.min(max, maxValue);
  2242. if (d > maxValue) {
  2243. return false;
  2244. }
  2245. }
  2246. if (Math.abs(this.direction.y) < 0.0000001) {
  2247. if (this.origin.y < minimum.y || this.origin.y > maximum.y) {
  2248. return false;
  2249. }
  2250. }
  2251. else {
  2252. inv = 1.0 / this.direction.y;
  2253. min = (minimum.y - this.origin.y) * inv;
  2254. max = (maximum.y - this.origin.y) * inv;
  2255. if (max === -Infinity) {
  2256. max = Infinity;
  2257. }
  2258. if (min > max) {
  2259. temp = min;
  2260. min = max;
  2261. max = temp;
  2262. }
  2263. d = Math.max(min, d);
  2264. maxValue = Math.min(max, maxValue);
  2265. if (d > maxValue) {
  2266. return false;
  2267. }
  2268. }
  2269. if (Math.abs(this.direction.z) < 0.0000001) {
  2270. if (this.origin.z < minimum.z || this.origin.z > maximum.z) {
  2271. return false;
  2272. }
  2273. }
  2274. else {
  2275. inv = 1.0 / this.direction.z;
  2276. min = (minimum.z - this.origin.z) * inv;
  2277. max = (maximum.z - this.origin.z) * inv;
  2278. if (max === -Infinity) {
  2279. max = Infinity;
  2280. }
  2281. if (min > max) {
  2282. temp = min;
  2283. min = max;
  2284. max = temp;
  2285. }
  2286. d = Math.max(min, d);
  2287. maxValue = Math.min(max, maxValue);
  2288. if (d > maxValue) {
  2289. return false;
  2290. }
  2291. }
  2292. return true;
  2293. };
  2294. Ray.prototype.intersectsBox = function (box) {
  2295. return this.intersectsBoxMinMax(box.minimum, box.maximum);
  2296. };
  2297. Ray.prototype.intersectsSphere = function (sphere) {
  2298. var x = sphere.center.x - this.origin.x;
  2299. var y = sphere.center.y - this.origin.y;
  2300. var z = sphere.center.z - this.origin.z;
  2301. var pyth = (x * x) + (y * y) + (z * z);
  2302. var rr = sphere.radius * sphere.radius;
  2303. if (pyth <= rr) {
  2304. return true;
  2305. }
  2306. var dot = (x * this.direction.x) + (y * this.direction.y) + (z * this.direction.z);
  2307. if (dot < 0.0) {
  2308. return false;
  2309. }
  2310. var temp = pyth - (dot * dot);
  2311. return temp <= rr;
  2312. };
  2313. Ray.prototype.intersectsTriangle = function (vertex0, vertex1, vertex2) {
  2314. if (!this._edge1) {
  2315. this._edge1 = Vector3.Zero();
  2316. this._edge2 = Vector3.Zero();
  2317. this._pvec = Vector3.Zero();
  2318. this._tvec = Vector3.Zero();
  2319. this._qvec = Vector3.Zero();
  2320. }
  2321. vertex1.subtractToRef(vertex0, this._edge1);
  2322. vertex2.subtractToRef(vertex0, this._edge2);
  2323. Vector3.CrossToRef(this.direction, this._edge2, this._pvec);
  2324. var det = Vector3.Dot(this._edge1, this._pvec);
  2325. if (det === 0) {
  2326. return null;
  2327. }
  2328. var invdet = 1 / det;
  2329. this.origin.subtractToRef(vertex0, this._tvec);
  2330. var bu = Vector3.Dot(this._tvec, this._pvec) * invdet;
  2331. if (bu < 0 || bu > 1.0) {
  2332. return null;
  2333. }
  2334. Vector3.CrossToRef(this._tvec, this._edge1, this._qvec);
  2335. var bv = Vector3.Dot(this.direction, this._qvec) * invdet;
  2336. if (bv < 0 || bu + bv > 1.0) {
  2337. return null;
  2338. }
  2339. //check if the distance is longer than the predefined length.
  2340. var distance = Vector3.Dot(this._edge2, this._qvec) * invdet;
  2341. if (distance > this.length) {
  2342. return null;
  2343. }
  2344. return new BABYLON.IntersectionInfo(bu, bv, distance);
  2345. };
  2346. // Statics
  2347. Ray.CreateNew = function (x, y, viewportWidth, viewportHeight, world, view, projection) {
  2348. var start = Vector3.Unproject(new Vector3(x, y, 0), viewportWidth, viewportHeight, world, view, projection);
  2349. var end = Vector3.Unproject(new Vector3(x, y, 1), viewportWidth, viewportHeight, world, view, projection);
  2350. var direction = end.subtract(start);
  2351. direction.normalize();
  2352. return new Ray(start, direction);
  2353. };
  2354. /**
  2355. * 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
  2356. * transformed to the given world matrix.
  2357. * @param origin The origin point
  2358. * @param end The end point
  2359. * @param world a matrix to transform the ray to. Default is the identity matrix.
  2360. */
  2361. Ray.CreateNewFromTo = function (origin, end, world) {
  2362. if (world === void 0) { world = Matrix.Identity(); }
  2363. var direction = end.subtract(origin);
  2364. var length = Math.sqrt((direction.x * direction.x) + (direction.y * direction.y) + (direction.z * direction.z));
  2365. direction.normalize();
  2366. return Ray.Transform(new Ray(origin, direction, length), world);
  2367. };
  2368. Ray.Transform = function (ray, matrix) {
  2369. var newOrigin = Vector3.TransformCoordinates(ray.origin, matrix);
  2370. var newDirection = Vector3.TransformNormal(ray.direction, matrix);
  2371. return new Ray(newOrigin, newDirection, ray.length);
  2372. };
  2373. return Ray;
  2374. })();
  2375. BABYLON.Ray = Ray;
  2376. (function (Space) {
  2377. Space[Space["LOCAL"] = 0] = "LOCAL";
  2378. Space[Space["WORLD"] = 1] = "WORLD";
  2379. })(BABYLON.Space || (BABYLON.Space = {}));
  2380. var Space = BABYLON.Space;
  2381. var Axis = (function () {
  2382. function Axis() {
  2383. }
  2384. Axis.X = new Vector3(1, 0, 0);
  2385. Axis.Y = new Vector3(0, 1, 0);
  2386. Axis.Z = new Vector3(0, 0, 1);
  2387. return Axis;
  2388. })();
  2389. BABYLON.Axis = Axis;
  2390. ;
  2391. var BezierCurve = (function () {
  2392. function BezierCurve() {
  2393. }
  2394. BezierCurve.interpolate = function (t, x1, y1, x2, y2) {
  2395. // Extract X (which is equal to time here)
  2396. var f0 = 1 - 3 * x2 + 3 * x1;
  2397. var f1 = 3 * x2 - 6 * x1;
  2398. var f2 = 3 * x1;
  2399. var refinedT = t;
  2400. for (var i = 0; i < 5; i++) {
  2401. var refinedT2 = refinedT * refinedT;
  2402. var refinedT3 = refinedT2 * refinedT;
  2403. var x = f0 * refinedT3 + f1 * refinedT2 + f2 * refinedT;
  2404. var slope = 1.0 / (3.0 * f0 * refinedT2 + 2.0 * f1 * refinedT + f2);
  2405. refinedT -= (x - t) * slope;
  2406. refinedT = Math.min(1, Math.max(0, refinedT));
  2407. }
  2408. // Resolve cubic bezier for the given x
  2409. return 3 * Math.pow(1 - refinedT, 2) * refinedT * y1 + 3 * (1 - refinedT) * Math.pow(refinedT, 2) * y2 + Math.pow(refinedT, 3);
  2410. };
  2411. return BezierCurve;
  2412. })();
  2413. BABYLON.BezierCurve = BezierCurve;
  2414. (function (Orientation) {
  2415. Orientation[Orientation["CW"] = 0] = "CW";
  2416. Orientation[Orientation["CCW"] = 1] = "CCW";
  2417. })(BABYLON.Orientation || (BABYLON.Orientation = {}));
  2418. var Orientation = BABYLON.Orientation;
  2419. var Angle = (function () {
  2420. function Angle(radians) {
  2421. var _this = this;
  2422. this.degrees = function () { return _this._radians * 180 / Math.PI; };
  2423. this.radians = function () { return _this._radians; };
  2424. this._radians = radians;
  2425. if (this._radians < 0)
  2426. this._radians += (2 * Math.PI);
  2427. }
  2428. Angle.BetweenTwoPoints = function (a, b) {
  2429. var delta = b.subtract(a);
  2430. var theta = Math.atan2(delta.y, delta.x);
  2431. return new Angle(theta);
  2432. };
  2433. Angle.FromRadians = function (radians) {
  2434. return new Angle(radians);
  2435. };
  2436. Angle.FromDegrees = function (degrees) {
  2437. return new Angle(degrees * Math.PI / 180);
  2438. };
  2439. return Angle;
  2440. })();
  2441. BABYLON.Angle = Angle;
  2442. var Arc2 = (function () {
  2443. function Arc2(startPoint, midPoint, endPoint) {
  2444. this.startPoint = startPoint;
  2445. this.midPoint = midPoint;
  2446. this.endPoint = endPoint;
  2447. var temp = Math.pow(midPoint.x, 2) + Math.pow(midPoint.y, 2);
  2448. var startToMid = (Math.pow(startPoint.x, 2) + Math.pow(startPoint.y, 2) - temp) / 2.;
  2449. var midToEnd = (temp - Math.pow(endPoint.x, 2) - Math.pow(endPoint.y, 2)) / 2.;
  2450. var det = (startPoint.x - midPoint.x) * (midPoint.y - endPoint.y) - (midPoint.x - endPoint.x) * (startPoint.y - midPoint.y);
  2451. this.centerPoint = new Vector2((startToMid * (midPoint.y - endPoint.y) - midToEnd * (startPoint.y - midPoint.y)) / det, ((startPoint.x - midPoint.x) * midToEnd - (midPoint.x - endPoint.x) * startToMid) / det);
  2452. this.radius = this.centerPoint.subtract(this.startPoint).length();
  2453. this.startAngle = Angle.BetweenTwoPoints(this.centerPoint, this.startPoint);
  2454. var a1 = this.startAngle.degrees();
  2455. var a2 = Angle.BetweenTwoPoints(this.centerPoint, this.midPoint).degrees();
  2456. var a3 = Angle.BetweenTwoPoints(this.centerPoint, this.endPoint).degrees();
  2457. // angles correction
  2458. if (a2 - a1 > +180.0)
  2459. a2 -= 360.0;
  2460. if (a2 - a1 < -180.0)
  2461. a2 += 360.0;
  2462. if (a3 - a2 > +180.0)
  2463. a3 -= 360.0;
  2464. if (a3 - a2 < -180.0)
  2465. a3 += 360.0;
  2466. this.orientation = (a2 - a1) < 0 ? 0 /* CW */ : 1 /* CCW */;
  2467. this.angle = Angle.FromDegrees(this.orientation === 0 /* CW */ ? a1 - a3 : a3 - a1);
  2468. }
  2469. return Arc2;
  2470. })();
  2471. BABYLON.Arc2 = Arc2;
  2472. var PathCursor = (function () {
  2473. function PathCursor(path) {
  2474. this.path = path;
  2475. this._onchange = new Array();
  2476. this.value = 0;
  2477. this.animations = new Array();
  2478. }
  2479. PathCursor.prototype.getPoint = function () {
  2480. var point = this.path.getPointAtLengthPosition(this.value);
  2481. return new Vector3(point.x, 0, point.y);
  2482. };
  2483. PathCursor.prototype.moveAhead = function (step) {
  2484. if (step === void 0) { step = 0.002; }
  2485. this.move(step);
  2486. return this;
  2487. };
  2488. PathCursor.prototype.moveBack = function (step) {
  2489. if (step === void 0) { step = 0.002; }
  2490. this.move(-step);
  2491. return this;
  2492. };
  2493. PathCursor.prototype.move = function (step) {
  2494. if (Math.abs(step) > 1) {
  2495. throw "step size should be less than 1.";
  2496. }
  2497. this.value += step;
  2498. this.ensureLimits();
  2499. this.raiseOnChange();
  2500. return this;
  2501. };
  2502. PathCursor.prototype.ensureLimits = function () {
  2503. while (this.value > 1) {
  2504. this.value -= 1;
  2505. }
  2506. while (this.value < 0) {
  2507. this.value += 1;
  2508. }
  2509. return this;
  2510. };
  2511. // used by animation engine
  2512. PathCursor.prototype.markAsDirty = function (propertyName) {
  2513. this.ensureLimits();
  2514. this.raiseOnChange();
  2515. return this;
  2516. };
  2517. PathCursor.prototype.raiseOnChange = function () {
  2518. var _this = this;
  2519. this._onchange.forEach(function (f) { return f(_this); });
  2520. return this;
  2521. };
  2522. PathCursor.prototype.onchange = function (f) {
  2523. this._onchange.push(f);
  2524. return this;
  2525. };
  2526. return PathCursor;
  2527. })();
  2528. BABYLON.PathCursor = PathCursor;
  2529. var Path2 = (function () {
  2530. function Path2(x, y) {
  2531. this._points = new Array();
  2532. this._length = 0;
  2533. this.closed = false;
  2534. this._points.push(new Vector2(x, y));
  2535. }
  2536. Path2.prototype.addLineTo = function (x, y) {
  2537. if (closed) {
  2538. BABYLON.Tools.Error("cannot add lines to closed paths");
  2539. return this;
  2540. }
  2541. var newPoint = new Vector2(x, y);
  2542. var previousPoint = this._points[this._points.length - 1];
  2543. this._points.push(newPoint);
  2544. this._length += newPoint.subtract(previousPoint).length();
  2545. return this;
  2546. };
  2547. Path2.prototype.addArcTo = function (midX, midY, endX, endY, numberOfSegments) {
  2548. if (numberOfSegments === void 0) { numberOfSegments = 36; }
  2549. if (closed) {
  2550. BABYLON.Tools.Error("cannot add arcs to closed paths");
  2551. return this;
  2552. }
  2553. var startPoint = this._points[this._points.length - 1];
  2554. var midPoint = new Vector2(midX, midY);
  2555. var endPoint = new Vector2(endX, endY);
  2556. var arc = new Arc2(startPoint, midPoint, endPoint);
  2557. var increment = arc.angle.radians() / numberOfSegments;
  2558. if (arc.orientation === 0 /* CW */)
  2559. increment *= -1;
  2560. var currentAngle = arc.startAngle.radians() + increment;
  2561. for (var i = 0; i < numberOfSegments; i++) {
  2562. var x = Math.cos(currentAngle) * arc.radius + arc.centerPoint.x;
  2563. var y = Math.sin(currentAngle) * arc.radius + arc.centerPoint.y;
  2564. this.addLineTo(x, y);
  2565. currentAngle += increment;
  2566. }
  2567. return this;
  2568. };
  2569. Path2.prototype.close = function () {
  2570. this.closed = true;
  2571. return this;
  2572. };
  2573. Path2.prototype.length = function () {
  2574. var result = this._length;
  2575. if (!this.closed) {
  2576. var lastPoint = this._points[this._points.length - 1];
  2577. var firstPoint = this._points[0];
  2578. result += (firstPoint.subtract(lastPoint).length());
  2579. }
  2580. return result;
  2581. };
  2582. Path2.prototype.getPoints = function () {
  2583. return this._points;
  2584. };
  2585. Path2.prototype.getPointAtLengthPosition = function (normalizedLengthPosition) {
  2586. if (normalizedLengthPosition < 0 || normalizedLengthPosition > 1) {
  2587. BABYLON.Tools.Error("normalized length position should be between 0 and 1.");
  2588. return Vector2.Zero();
  2589. }
  2590. var lengthPosition = normalizedLengthPosition * this.length();
  2591. var previousOffset = 0;
  2592. for (var i = 0; i < this._points.length; i++) {
  2593. var j = (i + 1) % this._points.length;
  2594. var a = this._points[i];
  2595. var b = this._points[j];
  2596. var bToA = b.subtract(a);
  2597. var nextOffset = (bToA.length() + previousOffset);
  2598. if (lengthPosition >= previousOffset && lengthPosition <= nextOffset) {
  2599. var dir = bToA.normalize();
  2600. var localOffset = lengthPosition - previousOffset;
  2601. return new Vector2(a.x + (dir.x * localOffset), a.y + (dir.y * localOffset));
  2602. }
  2603. previousOffset = nextOffset;
  2604. }
  2605. BABYLON.Tools.Error("internal error");
  2606. return Vector2.Zero();
  2607. };
  2608. Path2.StartingAt = function (x, y) {
  2609. return new Path2(x, y);
  2610. };
  2611. return Path2;
  2612. })();
  2613. BABYLON.Path2 = Path2;
  2614. var Path3D = (function () {
  2615. function Path3D(path) {
  2616. this.path = path;
  2617. this._curve = new Array();
  2618. this._distances = new Array();
  2619. this._tangents = new Array();
  2620. this._normals = new Array();
  2621. this._binormals = new Array();
  2622. this._curve = path.slice(); // copy array
  2623. var l = this._curve.length;
  2624. // first and last tangents
  2625. this._tangents[0] = this._curve[1].subtract(this._curve[0]);
  2626. this._tangents[0].normalize();
  2627. this._tangents[l - 1] = this._curve[l - 1].subtract(this._curve[l - 2]);
  2628. this._tangents[l - 1].normalize();
  2629. // normals and binormals at first point : arbitrary vector with _normalVector()
  2630. var tg0 = this._tangents[0];
  2631. var pp0 = this._normalVector(this._curve[0], tg0);
  2632. this._normals[0] = pp0;
  2633. this._normals[0].normalize();
  2634. this._binormals[0] = Vector3.Cross(tg0, this._normals[0]);
  2635. this._normals[0].normalize();
  2636. this._distances[0] = 0;
  2637. // normals and binormals : next points
  2638. var prev; // previous vector (segment)
  2639. var cur; // current vector (segment)
  2640. var curTang; // current tangent
  2641. var prevNorm; // previous normal
  2642. var prevBinor; // previous binormal
  2643. for (var i = 1; i < l; i++) {
  2644. // tangents
  2645. prev = this._curve[i].subtract(this._curve[i - 1]);
  2646. if (i < l - 1) {
  2647. cur = this._curve[i + 1].subtract(this._curve[i]);
  2648. this._tangents[i] = prev.add(cur);
  2649. this._tangents[i].normalize();
  2650. }
  2651. this._distances[i] = this._distances[i - 1] + prev.length();
  2652. // normals and binormals
  2653. // http://www.cs.cmu.edu/afs/andrew/scs/cs/15-462/web/old/asst2camera.html
  2654. curTang = this._tangents[i];
  2655. prevNorm = this._normals[i - 1];
  2656. prevBinor = this._binormals[i - 1];
  2657. this._normals[i] = Vector3.Cross(prevBinor, curTang);
  2658. this._normals[i].normalize();
  2659. this._binormals[i] = Vector3.Cross(curTang, this._normals[i]);
  2660. this._binormals[i].normalize();
  2661. }
  2662. }
  2663. Path3D.prototype.getCurve = function () {
  2664. return this._curve;
  2665. };
  2666. Path3D.prototype.getTangents = function () {
  2667. return this._tangents;
  2668. };
  2669. Path3D.prototype.getNormals = function () {
  2670. return this._normals;
  2671. };
  2672. Path3D.prototype.getBinormals = function () {
  2673. return this._binormals;
  2674. };
  2675. Path3D.prototype.getDistances = function () {
  2676. return this._distances;
  2677. };
  2678. // private function normalVector(v0, vt) :
  2679. // returns an arbitrary point in the plane defined by the point v0 and the vector vt orthogonal to this plane
  2680. Path3D.prototype._normalVector = function (v0, vt) {
  2681. var point;
  2682. if (vt.x !== 1) {
  2683. point = new Vector3(1, 0, 0);
  2684. }
  2685. else if (vt.y !== 1) {
  2686. point = new Vector3(0, 1, 0);
  2687. }
  2688. else if (vt.z !== 1) {
  2689. point = new Vector3(0, 0, 1);
  2690. }
  2691. var normal0 = Vector3.Cross(vt, point);
  2692. normal0.normalize();
  2693. return normal0;
  2694. };
  2695. return Path3D;
  2696. })();
  2697. BABYLON.Path3D = Path3D;
  2698. var Curve3 = (function () {
  2699. function Curve3(points) {
  2700. this._points = points;
  2701. }
  2702. // QuadraticBezier(origin_V3, control_V3, destination_V3 )
  2703. Curve3.CreateQuadraticBezier = function (v0, v1, v2, nbPoints) {
  2704. nbPoints = nbPoints > 2 ? nbPoints : 3;
  2705. var bez = new Array();
  2706. var step = 1 / nbPoints;
  2707. var equation = function (t, val0, val1, val2) {
  2708. var res = (1 - t) * (1 - t) * val0 + 2 * t * (1 - t) * val1 + t * t * val2;
  2709. return res;
  2710. };
  2711. for (var i = 0; i <= 1; i += step) {
  2712. bez.push(new Vector3(equation(i, v0.x, v1.x, v2.x), equation(i, v0.y, v1.y, v2.y), equation(i, v0.z, v1.z, v2.z)));
  2713. }
  2714. return new Curve3(bez);
  2715. };
  2716. // CubicBezier(origin_V3, control1_V3, control2_V3, destination_V3)
  2717. Curve3.CreateCubicBezier = function (v0, v1, v2, v3, nbPoints) {
  2718. nbPoints = nbPoints > 3 ? nbPoints : 4;
  2719. var bez = new Array();
  2720. var step = 1 / nbPoints;
  2721. var equation = function (t, val0, val1, val2, val3) {
  2722. var res = (1 - t) * (1 - t) * (1 - t) * val0 + 3 * t * (1 - t) * (1 - t) * val1 + 3 * t * t * (1 - t) * val2 + t * t * t * val3;
  2723. return res;
  2724. };
  2725. for (var i = 0; i <= 1; i += step) {
  2726. bez.push(new Vector3(equation(i, v0.x, v1.x, v2.x, v3.x), equation(i, v0.y, v1.y, v2.y, v3.y), equation(i, v0.z, v1.z, v2.z, v3.z)));
  2727. }
  2728. return new Curve3(bez);
  2729. };
  2730. Curve3.prototype.getPoints = function () {
  2731. return this._points;
  2732. };
  2733. Curve3.prototype.continue = function (curve) {
  2734. var lastPoint = this._points[this._points.length - 1];
  2735. var continuedPoints = this._points.slice();
  2736. var curvePoints = curve.getPoints();
  2737. for (var i = 1; i < curvePoints.length; i++) {
  2738. continuedPoints.push(curvePoints[i].add(lastPoint));
  2739. }
  2740. return new Curve3(continuedPoints);
  2741. };
  2742. return Curve3;
  2743. })();
  2744. BABYLON.Curve3 = Curve3;
  2745. // SIMD
  2746. if (window.SIMD !== undefined) {
  2747. // Replace functions
  2748. Matrix.prototype.multiplyToArray = Matrix.prototype.multiplyToArraySIMD;
  2749. Matrix.prototype.invertToRef = Matrix.prototype.invertToRefSIMD;
  2750. Matrix.LookAtLHToRef = Matrix.LookAtLHToRefSIMD;
  2751. Vector3.TransformCoordinatesToRef = Vector3.TransformCoordinatesToRefSIMD;
  2752. Vector3.TransformCoordinatesFromFloatsToRef = Vector3.TransformCoordinatesFromFloatsToRefSIMD;
  2753. Object.defineProperty(BABYLON.Vector3.prototype, "x", {
  2754. get: function () {
  2755. return this._data[0];
  2756. },
  2757. set: function (value) {
  2758. if (!this._data) {
  2759. this._data = new Float32Array(3);
  2760. }
  2761. this._data[0] = value;
  2762. }
  2763. });
  2764. Object.defineProperty(BABYLON.Vector3.prototype, "y", {
  2765. get: function () {
  2766. return this._data[1];
  2767. },
  2768. set: function (value) {
  2769. this._data[1] = value;
  2770. }
  2771. });
  2772. Object.defineProperty(BABYLON.Vector3.prototype, "z", {
  2773. get: function () {
  2774. return this._data[2];
  2775. },
  2776. set: function (value) {
  2777. this._data[2] = value;
  2778. }
  2779. });
  2780. }
  2781. })(BABYLON || (BABYLON = {}));
  2782. //# sourceMappingURL=babylon.math.js.map