babylon.math.js 100 KB

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