babylon.math.js 127 KB

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