babylon.math.js 134 KB

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