babylon.math.js 128 KB

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